PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / crontab / logger / file.php

file.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/crontab/logger/file.php

184 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Cron file logger.
16 *
17 * @since 1.7
18 */
19 class VBOCrontabLoggerFile implements VBOCrontabLogger
20 {
21 /**
22 * The destination folder of the log file.
23 * In case the destination folder is not passed, the system temporary folder will be used.
24 * As last resort the current folder will be used.
25 *
26 * @var string
27 */
28 protected $folder;
29
30 /**
31 * The name of the log file "crons.log.php" by default.
32 *
33 * @var string
34 */
35 protected $filename;
36
37 /**
38 * The maximum size (in bytes) that the log file can assume.
39 * After exceeding the specified threshold, a temporary backup file is created
40 * and the target file is flushed.
41 *
42 * The maximum default size is equal to 1MB.
43 *
44 * @var int
45 */
46 protected $maxSize;
47
48 /**
49 * The file pointer for bytes streaming.
50 *
51 * @var mixed
52 */
53 private $stream;
54
55 /**
56 * Class constructor.
57 *
58 * @param array $options An array of settings.
59 */
60 public function __construct(array $options = [])
61 {
62 $this->folder = $options['folder'] ?? null;
63 $this->filename = $options['filename'] ?? 'crons.log.php';
64 $this->maxSize = abs($options['maxsize'] ?? (1024 * 1024));
65
66 if (!$this->folder) {
67 // in case the folder is empty, use the default one
68 $this->folder = JFactory::getApplication()->get('tmp_path', dirname(__FILE__));
69 }
70 }
71
72 /**
73 * Class destructor.
74 */
75 public function __destruct()
76 {
77 $this->closeStream();
78 }
79
80 /**
81 * @inheritDoc
82 */
83 public function log(string $message, string $status = 'info')
84 {
85 try {
86 // open the stream
87 $stream = $this->openStream();
88
89 // add heading before the message
90 $log = JFactory::getDate()->format('Y-m-d H:i:s') . ' ' . str_pad(strtoupper($status), 9) . ' ' . trim($message);
91
92 // append log to file
93 fwrite($stream, $log . "\n");
94 } catch (Exception $error) {
95 // unable to log, go ahead silently
96 }
97 }
98
99 /**
100 * Returns the full path of the target file.
101 *
102 * @return string
103 *
104 * @throws Exception
105 */
106 public function getPath()
107 {
108 // in case the folder does not exist, create it
109 if (!JFolder::exists($this->folder)) {
110 $created = JFolder::create($this->folder);
111
112 if (!$created) {
113 throw new RuntimeException('Unable to create the folder: ' . $this->folder, 500);
114 }
115 }
116
117 // construct the full path
118 return JPath::clean($this->folder . '/' . $this->filename);
119 }
120
121 /**
122 * Opens the file stream.
123 *
124 * @return mixed
125 */
126 protected function openStream()
127 {
128 if ($this->stream) {
129 // do not open again
130 return $this->stream;
131 }
132
133 // construct the full path
134 $path = $this->getPath();
135
136 // check whether the file already exists
137 $exists = JFile::exists($path);
138
139 // in case the file already exists, make sure its size doesn't exceed the maximum threshold
140 if ($exists && $this->maxSize && $this->maxSize < filesize($path)) {
141 // extract folder and name from file path
142 $folder = dirname($path);
143 $file = basename($path);
144
145 // rename file to support a temporary backup
146 rename($path, JPath::clean($folder . '/backup_' . $file));
147
148 // create from scratch
149 $exists = false;
150 }
151
152 if (!$exists) {
153 // create a heading for the log file
154 $heading = 'Date' . str_repeat(' ', 16) . 'Level' . str_repeat(' ', 5) . "Message";
155 $heading .= "\n" . str_repeat('-', strlen($heading)) . "\n";
156
157 if (preg_match("/\.php$/", $path)) {
158 // safely hide the contents of the file
159 $heading = '<?php exit; ?>' . "\n\n" . $heading;
160 }
161
162 // add heading to log file
163 JFile::write($path, $heading);
164 }
165
166 // open the stream in append mode
167 $this->stream = fopen($path, 'a');
168
169 return $this->stream;
170 }
171
172 /**
173 * Closes the file stream.
174 *
175 * @return void
176 */
177 protected function closeStream()
178 {
179 if ($this->stream) {
180 fclose($this->stream);
181 }
182 }
183 }
184