| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 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 |
* Implements the URI interface for the Joomla platform. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
class VBOPlatformOrgJoomlaUri extends VBOPlatformUriAware |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Rewrites an internal URI that needs to be used outside of the website. |
| 23 |
* This means that the routed URI MUST start with the base path of the site. |
| 24 |
* |
| 25 |
* @param mixed $query The query string or an associative array of data. |
| 26 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 27 |
* @param mixed $itemid The itemid to use. If null, the current one will be used. |
| 28 |
* |
| 29 |
* @return string The complete routed URI. |
| 30 |
*/ |
| 31 |
public function route($query = '', $xhtml = true, $itemid = null) |
| 32 |
{ |
| 33 |
$app = JFactory::getApplication(); |
| 34 |
|
| 35 |
if (is_array($query)) |
| 36 |
{ |
| 37 |
// make sure the array is not empty |
| 38 |
if ($query) |
| 39 |
{ |
| 40 |
$query = '?' . http_build_query($query); |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
$query = ''; |
| 45 |
} |
| 46 |
|
| 47 |
// the query is an array, build the query string |
| 48 |
$query = 'index.php' . $query; |
| 49 |
} |
| 50 |
|
| 51 |
if (is_null($itemid) && $app->isClient('site')) |
| 52 |
{ |
| 53 |
// no item id, get it from the request |
| 54 |
$itemid = $app->input->getInt('Itemid', 0); |
| 55 |
} |
| 56 |
|
| 57 |
if ($itemid) |
| 58 |
{ |
| 59 |
if ($query) |
| 60 |
{ |
| 61 |
// check if the query string contains a '?' |
| 62 |
if (strpos($query, '?') !== false) |
| 63 |
{ |
| 64 |
// the query already starts with 'index.php?' or '?' |
| 65 |
$query .= '&'; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
// the query string is probably equals to 'index.php' |
| 70 |
$query .= '?'; |
| 71 |
} |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
// empty query, create the default string |
| 76 |
$query = 'index.php?'; |
| 77 |
} |
| 78 |
|
| 79 |
// the item id is set, append it at the end of the query string |
| 80 |
$query .= 'Itemid=' . $itemid; |
| 81 |
} |
| 82 |
|
| 83 |
// get base path |
| 84 |
$uri = JUri::getInstance(); |
| 85 |
$base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port')); |
| 86 |
|
| 87 |
if (method_exists('JRoute', 'link') && $app->isClient('administrator')) |
| 88 |
{ |
| 89 |
/** |
| 90 |
* Rewrite site URL also from the back-end. |
| 91 |
* Available starting from Joomla! 3.9.0. |
| 92 |
*/ |
| 93 |
$uri = $base . JRoute::link('site', $query, $xhtml); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
// route the query string and append it to the base path to create the final URI |
| 98 |
$uri = $base . JRoute::rewrite($query, $xhtml); |
| 99 |
} |
| 100 |
|
| 101 |
// remove administrator/ from URL in case this method is called from admin |
| 102 |
if ($app->isClient('administrator') && preg_match("/\/administrator\//i", $uri)) |
| 103 |
{ |
| 104 |
$adminPos = strrpos($uri, 'administrator/'); |
| 105 |
$uri = substr_replace($uri, '', $adminPos, 14); |
| 106 |
} |
| 107 |
|
| 108 |
return $uri; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Routes an admin URL for being used outside from the website (complete URI). |
| 113 |
* |
| 114 |
* @param mixed $query The query string or an associative array of data. |
| 115 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 116 |
* |
| 117 |
* @return string The complete routed URI. |
| 118 |
*/ |
| 119 |
public function admin($query = '', $xhtml = true) |
| 120 |
{ |
| 121 |
$app = JFactory::getApplication(); |
| 122 |
|
| 123 |
if (is_array($query)) |
| 124 |
{ |
| 125 |
// make sure the array is not empty |
| 126 |
if ($query) |
| 127 |
{ |
| 128 |
$query = '?' . http_build_query($query); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
$query = ''; |
| 133 |
} |
| 134 |
|
| 135 |
// the query is an array, build the query string |
| 136 |
$query = 'index.php' . $query; |
| 137 |
} |
| 138 |
|
| 139 |
// finalise admin URI |
| 140 |
$uri = JUri::root() . 'administrator/' . $query; |
| 141 |
|
| 142 |
if ($xhtml) |
| 143 |
{ |
| 144 |
// try to make "&" XML safe |
| 145 |
$uri = preg_replace("/&(?!amp;)/", '&', $uri); |
| 146 |
} |
| 147 |
|
| 148 |
return $uri; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Prepares a plain/routed URL to be used for an AJAX request. |
| 153 |
* |
| 154 |
* @param mixed $query The query string or a routed URL. |
| 155 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 156 |
* |
| 157 |
* @return string The AJAX end-point URI. |
| 158 |
*/ |
| 159 |
public function ajax($query = '', $xhtml = false) |
| 160 |
{ |
| 161 |
if (preg_match("/^index\.php/", $query) && JFactory::getApplication()->isClient('site')) |
| 162 |
{ |
| 163 |
// rewrite plain URL |
| 164 |
$uri = JRoute::rewrite($query, $xhtml); |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
// routed URL given, use it directly |
| 169 |
$uri = $query; |
| 170 |
|
| 171 |
if ($xhtml) |
| 172 |
{ |
| 173 |
// try to make "&" XML safe |
| 174 |
$uri = preg_replace("/&(?!amp;)/", '&', $uri); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $uri; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Includes the CSRF-proof token within the specified query string/URL. |
| 183 |
* |
| 184 |
* @param mixed $query The query string or a routed URL. |
| 185 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 186 |
* |
| 187 |
* @return string The resulting path. |
| 188 |
*/ |
| 189 |
public function addCSRF($query = '', $xhtml = false) |
| 190 |
{ |
| 191 |
// safely append the CSRF token within the query string |
| 192 |
$uri = JUri::getInstance($query); |
| 193 |
$uri->setVar(JSession::getFormToken(), 1); |
| 194 |
|
| 195 |
if ($xhtml) |
| 196 |
{ |
| 197 |
// try to make "&" XML safe |
| 198 |
$uri = str_replace('&', '&', (string) $uri); |
| 199 |
} |
| 200 |
|
| 201 |
return (string) $uri; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the platform base path. |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function getAbsolutePath() |
| 210 |
{ |
| 211 |
return JPATH_SITE; |
| 212 |
} |
| 213 |
} |
| 214 |
|