PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.1
VikBooking Hotel Booking Engine & PMS v1.7.1
1.8.16 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 All 37 releases
vikbooking / site / helpers / operator.php

operator.php in VikBooking Hotel Booking Engine & PMS 1.7.1, at site/helpers/operator.php

505 lines 13.3 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 com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Operator and tool permission types class handler.
15 *
16 * @since 1.11 (J) - 1.1 (WP)
17 */
18 final class VikBookingOperator
19 {
20 /**
21 * The singleton instance of the class.
22 *
23 * @var VikBookingOperator
24 */
25 protected static $instance = null;
26
27 /**
28 * The various tool permission types.
29 *
30 * @var array
31 *
32 * @since 1.16.9 (J) - 1.6.9 (WP)
33 */
34 protected $toolPermissionTypes = [];
35
36 /**
37 * Class constructor is protected.
38 *
39 * @see getInstance()
40 */
41 protected function __construct()
42 {
43 // load all the operator tools and related permission types
44 $this->loadToolPermissionTypes();
45 }
46
47 /**
48 * Returns the global VikBookingOperator object, either
49 * a new instance or the existing instance
50 * if the class was already instantiated.
51 *
52 * @return self A new instance of the class.
53 */
54 public static function getInstance()
55 {
56 if (is_null(static::$instance)) {
57 static::$instance = new static();
58 }
59
60 return static::$instance;
61 }
62
63 /**
64 * Returns the list of all the operators.
65 *
66 * @return array The list of operators
67 */
68 public function getAll()
69 {
70 $dbo = JFactory::getDbo();
71
72 $operators = [];
73
74 $q = "SELECT * FROM `#__vikbooking_operators` ORDER BY `first_name` ASC, `last_name` ASC;";
75 $dbo->setQuery($q);
76 $all = $dbo->loadAssocList();
77 foreach ($all as $o) {
78 $operators[$o['id']] = $o;
79 }
80
81 return $operators;
82 }
83
84 /**
85 * Fetches the information and permissions of one operator by ID.
86 *
87 * @param int $id The operator ID.
88 *
89 * @return array
90 */
91 public function getOne($id)
92 {
93 $dbo = JFactory::getDbo();
94
95 $dbo->setQuery(
96 $dbo->getQuery(true)
97 ->select('*')
98 ->from($dbo->qn('#__vikbooking_operators'))
99 ->where($dbo->qn('id') . ' = ' . (int) $id)
100 );
101
102 $operator = $dbo->loadAssoc();
103
104 if (!$operator) {
105 return [];
106 }
107
108 $operator['perms'] = !empty($operator['perms']) ? (array) json_decode($operator['perms'], true) : [];
109
110 return $operator;
111 }
112
113 /**
114 * Returns the list of all the operators filtered by a specific
115 * permission type and with an array of their own permissions.
116 *
117 * @param string $permtype The type of permission to look for.
118 * @param array $operators The list of operators.
119 *
120 * @return array The list of operators.
121 */
122 public function getOperatorsFromPermissions($permtype = '', $operators = [])
123 {
124 if (!$operators) {
125 $operators = $this->getAll();
126 }
127
128 foreach ($operators as $k => $v) {
129 $perms = !empty($v['perms']) ? json_decode($v['perms'], true) : [];
130 $perms = !is_array($perms) ? [] : $perms;
131 $operators[$k]['perms'] = $perms;
132 $enabled = false;
133 foreach ($perms as $perm) {
134 if ($permtype && ($perm['type'] ?? '') == $permtype) {
135 // turn the permissions into an associative array for just the requested tool
136 $operators[$k]['perms'] = $perm['perms'];
137 $enabled = true;
138 break;
139 }
140 }
141 if (!$enabled) {
142 unset($operators[$k]);
143 }
144 }
145
146 return array_values($operators);
147 }
148
149 /**
150 * Tells whether the operator is logged in by renewing
151 * the session cookie if the auth method is through code.
152 * Returns the information of the logged in operator or false.
153 *
154 * @return mixed Array if the operator is logged in, false otherwise.
155 */
156 public function getOperatorAccount()
157 {
158 $dbo = JFactory::getDbo();
159 $user = JFactory::getUser();
160
161 /**
162 * Beware of a possible magic method __get() in JUser.
163 * Assign the id property to a variable for evaluation.
164 */
165 $ujid = $user->id;
166
167 if (!$user->guest && !empty($ujid)) {
168 $q = "SELECT * FROM `#__vikbooking_operators` WHERE `ujid`=".(int)$user->id." LIMIT 1;";
169 $dbo->setQuery($q);
170 $record = $dbo->loadAssoc();
171 if ($record) {
172 // user is logged in to the site account
173 return $record;
174 }
175 }
176
177 $session = JFactory::getSession();
178 $sessval = $session->get('vboOpFngpt', '');
179 $cookie = JFactory::getApplication()->input->cookie;
180 $cksess = $cookie->get('vboOpFngpt', '', 'string');
181
182 if (!empty($sessval)) {
183 // look up for this fingerprint in the session
184 $q = "SELECT * FROM `#__vikbooking_operators` WHERE `fingpt`=" . $dbo->q($sessval);
185 $dbo->setQuery($q, 0, 1);
186 $record = $dbo->loadAssoc();
187 if ($record) {
188 // user is logged in through the session
189 return $record;
190 }
191 }
192
193 if (!empty($cksess)) {
194 // look up for this fingerprint in the cookie
195 $q = "SELECT * FROM `#__vikbooking_operators` WHERE `fingpt`=" . $dbo->q($cksess);
196 $dbo->setQuery($q, 0, 1);
197 $record = $dbo->loadAssoc();
198 if ($record) {
199 // user is logged in through the cookie, renew it for 2 weeks before returning the array
200 VikRequest::setCookie('vboOpFngpt', $cksess, (time() + (86400 * 14)), '/', '', false, true);
201 return $record;
202 }
203 }
204
205 // user is not logged in
206 return false;
207 }
208
209 /**
210 * Login request through the authentication code.
211 * If code is valid, the session and cookie are set.
212 *
213 * @param string $code the authentication code to check
214 *
215 * @return boolean true if the code exists, false otherwise
216 */
217 public function authOperator($code)
218 {
219 if (empty($code)) {
220 return false;
221 }
222
223 $dbo = JFactory::getDbo();
224 $session = JFactory::getSession();
225 $cookie = JFactory::getApplication()->input->cookie;
226
227 $q = "SELECT * FROM `#__vikbooking_operators` WHERE `code`=" . $dbo->q($code);
228 $dbo->setQuery($q, 0, 1);
229 $operator = $dbo->loadAssoc();
230
231 // ensure a valid code was provided
232 if ($operator) {
233 // send cookie
234 VikRequest::setCookie('vboOpFngpt', $operator['fingpt'], (time() + (86400 * 14)), '/', '', false, true);
235
236 // update session value
237 $session->set('vboOpFngpt', $operator['fingpt']);
238
239 return true;
240 }
241
242 // invalid code provided
243 return false;
244 }
245
246 /**
247 * Unsets the session value and sends an expired cookie.
248 * This action completely logs out an operator that should log back in.
249 *
250 * @return void
251 */
252 public function logoutOperator()
253 {
254 $app = JFactory::getApplication();
255 $session = JFactory::getSession();
256 $cookie = $app->input->cookie;
257
258 $session->set('vboOpFngpt', '');
259 VikRequest::setCookie('vboOpFngpt', $operator['fingpt'], (time() - (86400 * 14)), '/', '', false, true);
260
261 // logout from the site
262 $app->logout(JFactory::getUser()->id);
263 }
264
265 /**
266 * Checks whether the given operator has the permissions to access
267 * the given view type (permission type). Permissions are converted
268 * into an array if still JSON encoded, and updated via reference.
269 *
270 * @param array $operator the operator record
271 * @param string $permtype the name of the view to access
272 *
273 * @return boolean
274 */
275 public function checkPermissions(&$operator, $permtype)
276 {
277 if (!is_array($operator) || !$operator || empty($operator['perms'])) {
278 return false;
279 }
280
281 if (is_scalar($operator['perms'])) {
282 $operator['perms'] = json_decode($operator['perms'], true);
283 $operator['perms'] = is_array($operator['perms']) ? $operator['perms'] : [];
284 }
285
286 foreach ($operator['perms'] as $k => $perm) {
287 if (isset($perm['type']) && $perm['type'] == $permtype) {
288 // has permission for this View, take only these perms
289 $operator['perms'] = $operator['perms'][$k]['perms'];
290 return true;
291 }
292 }
293
294 // permission not found
295 return false;
296 }
297
298 /**
299 * Returns the data for the given tool identifier.
300 *
301 * @param string $tool The tool identifier.
302 *
303 * @return array
304 *
305 * @since 1.16.9 (J) - 1.6.9 (WP)
306 */
307 public function getToolData($tool)
308 {
309 return $this->toolPermissionTypes[$tool] ?? [];
310 }
311
312 /**
313 * Returns the name for the given tool identifier.
314 * If a raw language constant is detected, the raw
315 * tool identifier value is returned (front-end).
316 *
317 * @param string $tool The tool identifier.
318 *
319 * @return string
320 *
321 * @since 1.16.9 (J) - 1.6.9 (WP)
322 */
323 public function getToolName($tool)
324 {
325 $tool_name = $this->toolPermissionTypes[$tool]['name'] ?? $tool;
326
327 return preg_match("/^VB[A-Z0-9_]+$/", $tool_name) ? ucfirst(str_replace('_', ' ', $tool)) : $tool_name;
328 }
329
330 /**
331 * Returns a list of permission types for a specific tool (i.e. "tableaux") or all tools.
332 * Allows third-party plugins to define their own tool permission types.
333 *
334 * @param string $tool the tool identifier.
335 *
336 * @return array
337 *
338 * @since 1.16.9 (J) - 1.6.9 (WP)
339 */
340 public function getToolPermissionTypes($tool = null)
341 {
342 $default_tool_perms = $this->loadToolPermissionTypes();
343
344 if (!$tool) {
345 return $default_tool_perms;
346 }
347
348 return $default_tool_perms[$tool] ?? [];
349 }
350
351 /**
352 * Renders a native operator tool through a callback (i.e. "finance").
353 * Alternatively, tools could be rendered through a View (i.e. "tableaux").
354 * Lastly, third-party plugins could load their own operator tools.
355 *
356 * @param string $tool the tool identifier.
357 * @param array $operator the operator record calling the tool.
358 * @param object $permissinos the operator-tool permissions registry.
359 * @param string $tool_uri the base URI to the given tool.
360 *
361 * @return void
362 *
363 * @since 1.16.9 (J) - 1.6.9 (WP)
364 */
365 public function renderNativeToolLayout($tool, $operator, $permissions, $tool_uri)
366 {
367 // prepare the layout data
368 $layout_data = [
369 'tool' => $tool,
370 'operator' => $operator,
371 'permissions' => $permissions,
372 'tool_uri' => $tool_uri,
373 ];
374
375 // render the requested tool layout
376 echo JLayoutHelper::render('tools.' . $tool, $layout_data, null, [
377 'component' => 'com_vikbooking',
378 'client' => 'site',
379 ]);
380 }
381
382 /**
383 * Loads a list of default permission types for all the available tools.
384 * Allows third-party plugins to define their own tools and permission types.
385 *
386 * @return array
387 *
388 * @since 1.16.9 (J) - 1.6.9 (WP)
389 */
390 protected function loadToolPermissionTypes()
391 {
392 if ($this->toolPermissionTypes) {
393 // return the cached list
394 return $this->toolPermissionTypes;
395 }
396
397 $dbo = JFactory::getDbo();
398
399 // fetch all rooms
400 $dbo->setQuery(
401 $dbo->getQuery(true)
402 ->select([$dbo->qn('id'), $dbo->qn('name')])
403 ->from($dbo->qn('#__vikbooking_rooms'))
404 ->order($dbo->qn('avail') . ' DESC')
405 ->order($dbo->qn('name') . ' ASC')
406 );
407
408 $rooms = [];
409
410 foreach ($dbo->loadAssocList() as $room) {
411 $rooms[$room['id']] = $room['name'];
412 }
413
414 // build the default tool permission types
415 $this->toolPermissionTypes = [
416 'tableaux' => [
417 'name' => JText::translate('VBOOPERPERMTABLEAUX'),
418 'permissions' => [
419 'days' => [
420 'type' => 'number',
421 'label' => JText::translate('VBOPERMTBLXDAYS'),
422 'help' => JText::translate('VBO_PERM_NEGATIVE_DAYS'),
423 'step' => 1,
424 'min' => -365,
425 'max' => 365,
426 'default' => 14,
427 ],
428 'rooms' => [
429 'type' => 'select',
430 'label' => JText::translate('VBOPERMTBLXROOMS'),
431 'multiple' => true,
432 'assets' => true,
433 'asset_options' => [
434 'placeholder' => JText::translate('VBCOUPONALLVEHICLES'),
435 'allowClear' => true,
436 ],
437 'options' => $rooms,
438 ],
439 'guestname' => [
440 'type' => 'select',
441 'label' => JText::translate('VBOCUSTOMERDETAILS'),
442 'options' => [
443 1 => JText::translate('VBYES'),
444 0 => JText::translate('VBNO'),
445 ],
446 ],
447 'roomextras' => [
448 'type' => 'select',
449 'label' => JText::translate('VBPEDITBUSYEXTRACOSTS'),
450 'options' => [
451 1 => JText::translate('VBYES'),
452 0 => JText::translate('VBNO'),
453 ],
454 ],
455 ],
456 // this native tool is rendered through a View
457 'rendering_type' => 'view',
458 ],
459 'finance' => [
460 'name' => JText::translate('VBO_W_FINANCE_TITLE'),
461 'permissions' => [
462 'rooms' => [
463 'type' => 'select',
464 'label' => JText::translate('VBOPERMTBLXROOMS'),
465 'multiple' => true,
466 'assets' => true,
467 'asset_options' => [
468 'placeholder' => JText::translate('VBCOUPONALLVEHICLES'),
469 'allowClear' => true,
470 ],
471 'options' => $rooms,
472 ],
473 ],
474 // this native tool is rendered through a layout thanks to a callback
475 'rendering_type' => 'layout',
476 'rendering_callback' => function ($tool, $operator, $permissions, $tool_uri) {
477 VikBooking::getOperatorInstance()->renderNativeToolLayout($tool, $operator, $permissions, $tool_uri);
478 },
479 ],
480 ];
481
482 /**
483 * Trigger event to let other plugins register additional tools and permissions.
484 * Custom operator tools do NOT need to define a rendering type or callback.
485 *
486 * @return array A list of custom tools and related permission types.
487 */
488 $custom_tools = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadOperatorToolPermissionTypes');
489
490 foreach ($custom_tools as $tool) {
491 if (!is_array($tool)) {
492 continue;
493 }
494 foreach ($tool as $tool_name => $tool_perms) {
495 if (!is_numeric($tool_name) && is_array($tool_perms)) {
496 // set valid tool, by also allowing to overwrite the default ones
497 $this->toolPermissionTypes[$tool_name] = $tool_perms;
498 }
499 }
500 }
501
502 return $this->toolPermissionTypes;
503 }
504 }
505