PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / webhook.php
vikappointments / admin / models Last commit date
apiban.php 3 days ago apilog.php 3 days ago apiplugin.php 3 days ago apiuser.php 3 days ago apiuseroptions.php 3 days ago backup.php 3 days ago caldays.php 3 days ago calendar.php 3 days ago city.php 3 days ago closure.php 3 days ago configapp.php 3 days ago configcldays.php 3 days ago configcron.php 3 days ago configemp.php 3 days ago configsmsapi.php 3 days ago configuration.php 3 days ago conversion.php 3 days ago country.php 3 days ago coupon.php 3 days ago couponemployee.php 3 days ago coupongroup.php 3 days ago couponservice.php 3 days ago cronjob.php 3 days ago cronjoblog.php 3 days ago customer.php 3 days ago customf.php 3 days ago customfservice.php 3 days ago customizer.php 3 days ago empgroup.php 3 days ago employee.php 3 days ago empsettings.php 3 days ago file.php 3 days ago findreservation.php 3 days ago group.php 3 days ago import.php 3 days ago index.html 3 days ago invoice.php 3 days ago langcustomf.php 3 days ago langempgroup.php 3 days ago langemployee.php 3 days ago langgroup.php 3 days ago langmedia.php 3 days ago langoption.php 3 days ago langoptiongroup.php 3 days ago langoptionvar.php 3 days ago langpackage.php 3 days ago langpackgroup.php 3 days ago langpayment.php 3 days ago langservice.php 3 days ago langstatuscode.php 3 days ago langsubscr.php 3 days ago langtax.php 3 days ago langtaxrule.php 3 days ago location.php 3 days ago mailtext.php 3 days ago makerecurrence.php 3 days ago media.php 3 days ago multiorder.php 3 days ago option.php 3 days ago optiongroup.php 3 days ago optionvar.php 3 days ago orderstatus.php 3 days ago package.php 3 days ago packageservice.php 3 days ago packgroup.php 3 days ago packorder.php 3 days ago packorderitem.php 3 days ago payment.php 3 days ago rate.php 3 days ago reportsemp.php 3 days ago reportsser.php 3 days ago reservation.php 3 days ago resoptassoc.php 3 days ago restriction.php 3 days ago review.php 3 days ago serempassoc.php 3 days ago seroptassoc.php 3 days ago serrateassoc.php 3 days ago serrestrassoc.php 3 days ago service.php 3 days ago state.php 3 days ago statswidget.php 3 days ago statuscode.php 3 days ago subscription.php 3 days ago subscrorder.php 3 days ago tag.php 3 days ago tax.php 3 days ago taxrule.php 3 days ago updateprogram.php 3 days ago usernote.php 3 days ago waitinglist.php 3 days ago webhook.php 3 days ago worktime.php 3 days ago
webhook.php
250 lines
1 <?php
2 /**
3 * @package VikAppointments
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 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments web hook model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelWebhook extends JModelVAP
22 {
23 /**
24 * Basic save implementation.
25 *
26 * @param mixed $data Either an array or an object of data to save.
27 *
28 * @return mixed The ID of the record on success, false otherwise.
29 */
30 public function save($data)
31 {
32 $data = (array) $data;
33
34 $config = VAPFactory::getConfig();
35
36 $maxfail = $config->getUint('webhooksmaxfail', 0);
37
38 // in case of failures counter, make sure the web hook didn't exceed the maximum threshold
39 if (isset($data['failed']) && $maxfail > 0 && $data['failed'] >= $maxfail)
40 {
41 // threshold reached, auto-unpublish the web hook
42 $data['published'] = 0;
43 }
44
45 // attempt to save the web hook
46 $id = parent::save($data);
47
48 if (!$id)
49 {
50 // an error occurred, do not go ahead
51 return false;
52 }
53
54 $logspath = $config->get('webhookslogspath');
55
56 if (!$logspath)
57 {
58 // use default path when missing
59 $logspath = JFactory::getApplication()->get('log_path', '');
60 }
61
62 if (!$config->getBool('webhooksuselog'))
63 {
64 // logs disabled, avoid storing them
65 $data['log'] = '';
66 }
67
68 // register logs
69 if (!empty($data['log']) && $logspath && is_dir($logspath))
70 {
71 // check how the logs should be grouped
72 $group = $config->getString('webhooksgroup', 'day');
73
74 $now = JFactory::getDate();
75
76 switch ($group)
77 {
78 case 'week':
79 $filename = $now->format('Y-W');
80 break;
81
82 case 'month':
83 $filename = $now->format('Y-m');
84 break;
85
86 default:
87 $filename = $now->format('Y-m-d');
88 }
89
90 // in case the log key was not specified, we need to load it
91 if (!isset($data['logkey']))
92 {
93 // load item details
94 $data['logkey'] = $this->getItem($id, true)->logkey;
95 }
96
97 // build log file name
98 $filename = 'webhook_' . $id . '_' . $filename . ($data['logkey'] ? '_' . $data['logkey'] : '') . '.log';
99
100 $date = $now->format('c');
101
102 // create log body
103 $log = $date . "\n" . str_repeat('-', strlen($date)) . "\n\n";
104 $log .= is_string($data['log']) ? $data['log'] : print_r($data['log'], true);
105 $log .= "\n\n";
106
107 // open log file and append log data (create if the file doesn't exist)
108 $handle = fopen(rtrim($logspath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename, 'a');
109 fwrite($handle, $log);
110 fclose($handle);
111 }
112
113 return $id;
114 }
115
116 /**
117 * Basic item loading implementation.
118 *
119 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
120 * If not set the instance property value is used.
121 * @param boolean $new True to return an empty object if missing.
122 *
123 * @return mixed The record object on success, null otherwise.
124 */
125 public function getItem($pk, $new = false)
126 {
127 // load item through parent
128 $item = parent::getItem($pk, $new);
129
130 if ($item)
131 {
132 // decode encoded parameters
133 $item->params = $item->params ? (array) json_decode($item->params, true) : array();
134 }
135
136 return $item;
137 }
138
139 /**
140 * Returns a list of registered log files for the given web hook.
141 *
142 * @param integer $id The web hook PK.
143 *
144 * @return array An array of log files.
145 */
146 public function getLogFiles($id)
147 {
148 if (!$id)
149 {
150 // we cannot have logs for a web hook before its creation
151 return array();
152 }
153
154 // fetch logs path
155 $config = VAPFactory::getConfig();
156 $dir = $config->get('webhookslogspath');
157
158 if (!$dir)
159 {
160 // log path not specified, use the default one
161 $dir = JFactory::getApplication()->get('log_path', '');
162 }
163
164 if (!$dir || !is_dir($dir))
165 {
166 // invalid folder
167 return array();
168 }
169
170 $logs = array();
171
172 // load all log files contained within this folder that starts with "webhook_[ID]"
173 $files = glob(rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'webhook_' . (int) $id . '*.log');
174
175 foreach ($files as $file)
176 {
177 $name = basename($file);
178
179 if (preg_match("/^webhook_[0-9]+_([0-9\-]+)_/", $name, $match))
180 {
181 // extract readable name from file
182 $name = end($match);
183 }
184
185 $logs[$file] = $name;
186 }
187
188 return $logs;
189 }
190
191 /**
192 * Loads the log details from the specified path.
193 *
194 * @param string $file The file path.
195 *
196 * @return mixed The log details on success, false otherwise.
197 */
198 public function getLog($file)
199 {
200 $config = VAPFactory::getConfig();
201 $dir = $config->get('webhookslogspath');
202
203 if (!$dir)
204 {
205 // log path not specified, use the default one
206 $dir = JFactory::getApplication()->get('log_path', '');
207 }
208
209 // make sure the file exists
210 if (is_file($file))
211 {
212 // file found, make sure we are not trying to exceed to a different path
213 if (strpos($file, $dir) !== 0)
214 {
215 // trying to access a different folder
216 return false;
217 }
218 }
219 else
220 {
221 // file not found, probably we received only the file name
222 $file = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
223
224 if (!is_file($file))
225 {
226 // file not found
227 return false;
228 }
229 }
230
231 $handle = fopen($file, 'r');
232
233 if (!$handle)
234 {
235 return '';
236 }
237
238 $buffer = '';
239
240 while (!feof($handle))
241 {
242 $buffer .= fread($handle, 8192);
243 }
244
245 fclose($handle);
246
247 return $buffer;
248 }
249 }
250