PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / webhook.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 4 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 4 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 4 months ago reportsser.php 4 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month 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