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 / site / helpers / libraries / update / urihandler.php
vikappointments / site / helpers / libraries / update Last commit date
adapters 4 days ago adapter.php 4 days ago factory.php 4 days ago index.html 4 days ago rule.php 4 days ago urihandler.php 4 days ago
urihandler.php
276 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 if (!class_exists('UriUpdateHandler'))
15 {
16 /**
17 * This class is used to handle the extra query field for the extension updates.
18 *
19 * USAGE:
20 *
21 * $update = new UriUpdateHandler(); // or new UriUpdateHandler('com_example')
22 *
23 * $update->addExtraField('order_number', $order_number);
24 * $update->addExtraField('domain', $domain);
25 *
26 * // OR
27 *
28 * $update->setExtraFields(array(
29 * 'order_number' => $order_number,
30 * 'domain' => $domain,
31 * ));
32 *
33 * $update->register();
34 *
35 * In case the component didn't own the <schemapath> XML tag, it suggested to launch
36 * also the function below to update correctly the schema version:
37 *
38 * $update->checkSchema($component_version);
39 *
40 * @since 1.6
41 */
42 class UriUpdateHandler
43 {
44 /**
45 * The component (or plugin/module) instance that need to be updated.
46 *
47 * @var mixed
48 */
49 private $component = null;
50
51 /**
52 * The query string containing the extra fields to append to the update URI.
53 *
54 * @var string
55 */
56 private $extraFields = '';
57
58 /**
59 * Class constructor.
60 * @param mixed $element The element to load. Null to load the current component.
61 *
62 * @uses getComponent()
63 */
64 public function __construct($element = null)
65 {
66 $this->getComponent($element);
67 }
68
69 /**
70 * Load the specified/current component.
71 *
72 * @param mixed $element The element to load. Null to load the current component.
73 *
74 * @return mixed The loaded component.
75 */
76 public function getComponent($element = null)
77 {
78 if ($element === null)
79 {
80 $element = JFactory::getApplication()->input->get('option');
81 }
82
83 JLoader::import('joomla.application.component.helper');
84 $this->component = JComponentHelper::getComponent($element);
85
86 return $this->component;
87 }
88
89 /**
90 * Returns the registered extra fields.
91 *
92 * @param boolean $array True to return an array of vars, false to obtain
93 * the full query string.
94 *
95 * @return string|array The query string or an associative array of vars.
96 *
97 * @since 1.7.3
98 */
99 public function getExtraFields($array = false)
100 {
101 if (!$array)
102 {
103 // return query string
104 return $this->extraFields;
105 }
106
107 // create a new URI instance and fetch registered vars
108 $uri = new JUri($this->extraFields);
109 return $uri->getQuery($array);
110 }
111
112 /**
113 * Set the parameters into the additional query string.
114 *
115 * @param array $params The associative array to push.
116 *
117 * @return self This object to support chaining.
118 *
119 * @uses addExtraField()
120 */
121 public function setExtraFields(array $params = array())
122 {
123 $this->extraFields = '';
124
125 foreach ($params as $key => $val )
126 {
127 $this->addExtraField($key, $val);
128 }
129
130 return $this;
131 }
132
133 /**
134 * Push a single value into the additional query string.
135 *
136 * @param string $key The name of the query param.
137 * @param mixed $val The value of the query param. If an array is specified,
138 * the values will be added recursively.
139 *
140 * @return self This object to support chaining.
141 */
142 public function addExtraField($key, $val)
143 {
144 if (is_scalar($val))
145 {
146 $this->extraFields .= (empty($this->extraFields) ? '' : '&amp;') . $key . "=" . urlencode($val);
147 }
148 else
149 {
150 foreach ($val as $inner)
151 {
152 $this->addExtraField($key . '[]', $inner);
153 }
154 }
155
156 return $this;
157 }
158
159 /**
160 * Commit the changes by updating the extra_fields column of the
161 * #__update_sites_extensions database table.
162 *
163 * @return boolean True on success, otherwise false.
164 */
165 public function register()
166 {
167 if (!$this->component)
168 {
169 return false;
170 }
171
172 $dbo = JFactory::getDbo();
173
174 // load the update site record, if it exists
175 $q = $dbo->getQuery(true);
176
177 $q->select($dbo->qn('update_site_id'))
178 ->from($dbo->qn('#__update_sites_extensions'))
179 ->where($dbo->qn('extension_id') . ' = ' . $dbo->q($this->component->id));
180
181 $dbo->setQuery($q);
182 $updateSite = $dbo->loadResult();
183
184 $success = false;
185
186 if ($updateSite)
187 {
188 // update the update site record
189 $q = $dbo->getQuery(true);
190
191 $q->update($dbo->qn('#__update_sites'))
192 ->set($dbo->qn('extra_query') . ' = ' . $dbo->q($this->extraFields))
193 ->set($dbo->qn('enabled') . ' = 1')
194 ->set($dbo->qn('last_check_timestamp') . ' = 0')
195 ->where($dbo->qn('update_site_id') . ' = ' . $dbo->q($updateSite));
196
197 $dbo->setQuery($q);
198 $dbo->execute();
199
200 $success = (bool) $dbo->getAffectedRows();
201
202 // Delete any existing updates (essentially flushes the updates cache for this update site)
203 $q = $dbo->getQuery(true);
204
205 $q->delete('#__updates')
206 ->where($dbo->qn('update_site_id') . ' = ' . $dbo->q($updateSite));
207
208 $dbo->setQuery($q);
209 $dbo->execute();
210 }
211
212 return $success;
213 }
214
215 /**
216 * Check the schema of the extension to make sure the system will use
217 * the current version.
218 *
219 * @param string $version The current version of the component.
220 *
221 * @return boolean True if the schema has been altered, otherwise false.
222 */
223 public function checkSchema($version)
224 {
225 if (!$this->component)
226 {
227 return false;
228 }
229
230 $ok = false;
231
232 $dbo = JFactory::getDbo();
233
234 $q = $dbo->getQuery(true);
235
236 $q->select($dbo->qn('version_id'))
237 ->from($dbo->qn('#__schemas'))
238 ->where($dbo->qn('extension_id') . ' = ' . (int) $this->component->id);
239
240 $dbo->setQuery($q, 0, 1);
241 $current = $dbo->loadResult();
242
243 if ($current)
244 {
245 if ($current == $version)
246 {
247 $ok = true;
248 }
249 else
250 {
251 $q->clear()
252 ->delete($dbo->qn('#__schemas'))
253 ->where($dbo->qn('extension_id') . ' = ' . (int) $this->component->id);
254
255 $dbo->setQuery($q);
256 $dbo->execute();
257 }
258 }
259
260 if (!$ok)
261 {
262 $q->clear()
263 ->insert($dbo->qn('#__schemas'))
264 ->columns(array($dbo->qn('extension_id'), $dbo->qn('version_id')))
265 ->values($this->component->id . ', ' . $dbo->q($version));
266
267 $dbo->setQuery($q);
268 $ok = (bool) $dbo->execute();
269 }
270
271 return $ok;
272 }
273
274 }
275 }
276