PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / sef / router.php
vikappointments / site / helpers / libraries / sef Last commit date
router 1 month ago factory.php 1 month ago helper.php 1 month ago index.html 1 month ago router.php 1 month ago
router.php
137 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 /**
15 * Factory class used to load the correct router handler
16 * according to the platform version.
17 *
18 * @since 1.7
19 */
20 trait VAPSefRouter
21 {
22 /**
23 * Method used to find the item ID that matches the specified type.
24 *
25 * @param string $type The item type.
26 * @param array $args An associative array with the arguments to match.
27 * @param array $exclude A list of attributes that should be empty on the searched items.
28 *
29 * @return integer The item ID that matches the type, "zero" otherwise.
30 *
31 * @uses matchItemArguments()
32 */
33 protected function getProperItemID($type, array $args = array(), array $exclude = array())
34 {
35 $user = JFactory::getUser();
36
37 /**
38 * Compare the authorised view levels of the current user against
39 * the access level of the menu item for which we are searching.
40 * This will avoid displaying a login page in place of the expected one.
41 */
42 $levels = $user->getAuthorisedViewLevels();
43
44 foreach ($this->menu->getMenu() as $itemid => $item)
45 {
46 if (isset($item->query['option']) && isset($item->query['view'])
47 && $item->query['option'] == 'com_vikappointments' && $item->query['view'] == $type
48 && ($item->language == '*' || $item->language == $this->langtag)
49 && in_array($item->access, $levels)
50 && $this->matchItemArguments($item, $args, $exclude))
51 {
52 return $itemid;
53 }
54 }
55
56 return 0;
57 }
58
59 /**
60 * Checks if the item matches all the specified arguments.
61 * The arguments must be contained within the query property.
62 *
63 * @param object $item The menu item object.
64 * @param array $args The associative array to check.
65 * @param array $exclude A list of attributes that should be empty on the searched items.
66 *
67 * @return boolean True if the item matches, false otherwise.
68 */
69 protected function matchItemArguments($item, $args, array $exclude = array())
70 {
71 if (!count($args) && !count($exclude))
72 {
73 // always compatible in case of empty arguments
74 return true;
75 }
76
77 if (!isset($item->query))
78 {
79 // do not accept in case of empty query
80 return false;
81 }
82
83 // iterate query to search for non scalar values
84 foreach ($item->query as $k => $v)
85 {
86 /**
87 * Make sure the attribute specified by the menu item
88 * is not contained within the "excluded" list.
89 */
90 if (!empty($v) && in_array($k, $exclude))
91 {
92 return false;
93 }
94
95 if (is_array($v))
96 {
97 // validate array to make sure it matches the searched query
98 if (!isset($args[$k]) || array_diff_assoc($args[$k], $v))
99 {
100 // missing match
101 return false;
102 }
103
104 // unset value to avoid warnings in the next check
105 unset($item->query[$k]);
106 unset($args[$k]);
107 }
108 }
109
110 // the difference between the array must return an empty array
111 return !array_diff_assoc($args, $item->query);
112 }
113
114 /**
115 * Checks whether the router should be used.
116 *
117 * @return boolean
118 */
119 protected function isActive()
120 {
121 // use router only in case both SEF and URL Rewrite settings
122 // have been enabled from the Joomla! global configuration
123 if (!$this->app->get('sef') || !$this->app->get('sef_rewrite'))
124 {
125 return false;
126 }
127
128 // security check to turn off the router in case of issues
129 if (VAPFactory::getConfig()->getBool('router', true) === false)
130 {
131 return false;
132 }
133
134 return true;
135 }
136 }
137