| 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 WordPress platform. |
| 16 |
* |
| 17 |
* @since 1.5 |
| 18 |
*/ |
| 19 |
class VBOPlatformOrgWordpressUri 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 |
/** |
| 52 |
* If a post ID is provided, and if the query does not contain any language, |
| 53 |
* access the shortcode details and eventually append the proper language. |
| 54 |
* This is because the WP router will use the post ID only as a fallback, |
| 55 |
* by always giving higher priority to view and lang query arguments. |
| 56 |
* |
| 57 |
* @since 1.8.11 |
| 58 |
*/ |
| 59 |
if ($itemid && !preg_match('/(&|\?)lang=/', $query)) |
| 60 |
{ |
| 61 |
// access the shortcode model |
| 62 |
$model = JModel::getInstance('vikbooking', 'shortcode', 'admin'); |
| 63 |
|
| 64 |
// get shortcode record by post ID, or eventually get an empty object |
| 65 |
$shortcode = $model->getItem(['post_id' => $itemid], true); |
| 66 |
|
| 67 |
// make sure the shortcode is not using "any" language (default value also for empty object) |
| 68 |
if ($shortcode->lang != '*') |
| 69 |
{ |
| 70 |
// append shortcode language to query to ensure obtaining a properly rewritten URL |
| 71 |
$query .= '&lang=' . $shortcode->lang; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if (is_null($itemid) && $app->isClient('site')) |
| 76 |
{ |
| 77 |
// no item id, get it from the request |
| 78 |
$itemid = $app->input->getInt('Itemid', 0); |
| 79 |
} |
| 80 |
|
| 81 |
if ($itemid) |
| 82 |
{ |
| 83 |
if ($query) |
| 84 |
{ |
| 85 |
// check if the query string contains a '?' |
| 86 |
if (strpos($query, '?') !== false) |
| 87 |
{ |
| 88 |
// the query already starts with 'index.php?' or '?' |
| 89 |
$query .= '&'; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
// the query string is probably equals to 'index.php' |
| 94 |
$query .= '?'; |
| 95 |
} |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
// empty query, create the default string |
| 100 |
$query = 'index.php?'; |
| 101 |
} |
| 102 |
|
| 103 |
// the item id is set, append it at the end of the query string |
| 104 |
$query .= 'Itemid=' . $itemid; |
| 105 |
} |
| 106 |
|
| 107 |
// check if the query already specifies the Itemid |
| 108 |
if (!preg_match("/&Itemid=[\d]*/", $query)) |
| 109 |
{ |
| 110 |
// try to extract view from query |
| 111 |
if (preg_match("/&view=([a-z0-9_]+)(?:&|$)/", $query, $match)) |
| 112 |
{ |
| 113 |
$view = end($match); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
$view = null; |
| 118 |
} |
| 119 |
|
| 120 |
// import shortcodes model |
| 121 |
$model = JModel::getInstance('vikbooking', 'shortcodes', 'admin'); |
| 122 |
$itemid = $model->best($view); |
| 123 |
|
| 124 |
if ($itemid) |
| 125 |
{ |
| 126 |
// update query with Itemid found |
| 127 |
$query .= (strpos($query, '?') === false ? '?' : '&') . 'Itemid=' . $itemid; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// route URL |
| 132 |
return JRoute::rewrite($query, false); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Routes an admin URL for being used outside from the website (complete URI). |
| 137 |
* |
| 138 |
* @param mixed $query The query string or an associative array of data. |
| 139 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 140 |
* |
| 141 |
* @return string The complete routed URI. |
| 142 |
*/ |
| 143 |
public function admin($query = '', $xhtml = true) |
| 144 |
{ |
| 145 |
$app = JFactory::getApplication(); |
| 146 |
|
| 147 |
if (is_array($query)) |
| 148 |
{ |
| 149 |
// make sure the array is not empty |
| 150 |
if ($query) |
| 151 |
{ |
| 152 |
$query = '?' . http_build_query($query); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
$query = ''; |
| 157 |
} |
| 158 |
|
| 159 |
// the query is an array, build the query string |
| 160 |
$query = 'index.php' . $query; |
| 161 |
} |
| 162 |
|
| 163 |
// replace initial index.php with admin.php |
| 164 |
$query = preg_replace("/^index\.php/", 'admin.php', $query); |
| 165 |
|
| 166 |
// replace option=com_vikbooking with page=vikbooking |
| 167 |
$query = preg_replace("/(&|\?)option=com_vikbooking/", '$1page=vikbooking', $query); |
| 168 |
|
| 169 |
// replace option=com_vikchannelmanager with page=vikchannelmanager |
| 170 |
$query = preg_replace("/(&|\?)option=com_vikchannelmanager/", '$1page=vikchannelmanager', $query); |
| 171 |
|
| 172 |
// finalise admin URI |
| 173 |
$uri = admin_url($query); |
| 174 |
|
| 175 |
if ($xhtml) |
| 176 |
{ |
| 177 |
// try to make "&" XML safe |
| 178 |
$uri = preg_replace("/&(?!amp;)/", '&', $uri); |
| 179 |
} |
| 180 |
|
| 181 |
return $uri; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Prepares a plain/routed URL to be used for an AJAX request. |
| 186 |
* |
| 187 |
* @param mixed $query The query string or a routed URL. |
| 188 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 189 |
* |
| 190 |
* @return string The AJAX end-point URI. |
| 191 |
*/ |
| 192 |
public function ajax($query = '', $xhtml = false) |
| 193 |
{ |
| 194 |
// instantiate path based on specified query |
| 195 |
$path = new JUri($query); |
| 196 |
|
| 197 |
// delete option var from query |
| 198 |
$path->delVar('option'); |
| 199 |
|
| 200 |
// force action in query |
| 201 |
$plugin_action = strpos($query, 'vikchannelmanager') !== false ? 'vikchannelmanager' : 'vikbooking'; |
| 202 |
$path->setVar('action', $plugin_action); |
| 203 |
|
| 204 |
// force application client in case of front-end |
| 205 |
if (JFactory::getApplication()->isClient('site')) |
| 206 |
{ |
| 207 |
$path->setVar('vik_ajax_client', 'site'); |
| 208 |
} |
| 209 |
|
| 210 |
// create AJAX URI |
| 211 |
$uri = admin_url('admin-ajax.php') . '?' . $path->getQuery(); |
| 212 |
|
| 213 |
if ($xhtml) |
| 214 |
{ |
| 215 |
// try to make "&" XML safe |
| 216 |
$uri = preg_replace("/&(?!amp;)/", '&', $uri); |
| 217 |
} |
| 218 |
|
| 219 |
return $uri; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Includes the CSRF-proof token within the specified query string/URL. |
| 224 |
* |
| 225 |
* @param mixed $query The query string or a routed URL. |
| 226 |
* @param boolean $xhtml Replace & by & for XML compliance. |
| 227 |
* |
| 228 |
* @return string The resulting path. |
| 229 |
*/ |
| 230 |
public function addCSRF($query = '', $xhtml = false) |
| 231 |
{ |
| 232 |
JLoader::import('adapter.session.session'); |
| 233 |
|
| 234 |
// safely append the CSRF token within the query string |
| 235 |
$uri = JUri::getInstance($query); |
| 236 |
$uri->setVar(JSession::getFormTokenName(), JSession::getFormToken()); |
| 237 |
|
| 238 |
if ($xhtml) |
| 239 |
{ |
| 240 |
// try to make "&" XML safe |
| 241 |
$uri = preg_replace("/&(?!amp;)/", '&', (string) $uri); |
| 242 |
} |
| 243 |
|
| 244 |
return (string) $uri; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the platform base path. |
| 249 |
* |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
public function getAbsolutePath() |
| 253 |
{ |
| 254 |
return ABSPATH; |
| 255 |
} |
| 256 |
} |
| 257 |
|