Skip to content

Commit cf63f95

Browse files
author
Marco Cesarato
committed
fix: request uri on subdir and add check domain methods
1 parent 161d3d9 commit cf63f95

1 file changed

Lines changed: 59 additions & 16 deletions

File tree

includes/classes/Request.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* Request Class.
77
*
88
* @author Marco Cesarato <cesarato.developer@gmail.com>
9-
* @copyright Copyright (c) 2019
109
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
1110
*
1211
* @see https://github.com/marcocesarato/Database-Web-API
@@ -108,6 +107,9 @@ public static function parseUrlRewrite()
108107
'auth/check' => 'check_auth=1&format=%s',
109108
// Auth
110109
'auth' => 'auth=1&format=%s',
110+
111+
/* Token required requests */
112+
111113
// Dataset + P1 + P2 + P3 + P4 (Custom requests)
112114
'([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)' => 'custom=%s&db=%s&table=%s&where[%s]=%s&format=%s',
113115
// Dataset + Table + Column + Value
@@ -158,18 +160,22 @@ public static function parseUrlRewrite()
158160
public static function getRequestURI()
159161
{
160162
$base = '';
161-
$doc_root = realpath(preg_replace('/' . preg_quote($_SERVER['SCRIPT_NAME'], '/') . '$/', '', $_SERVER['SCRIPT_FILENAME']));
162-
if (realpath(__API_ROOT__) != realpath($_SERVER['DOCUMENT_ROOT'])) {
163-
$base = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', __API_ROOT__) . '/';
164-
} elseif (realpath(__API_ROOT__) != $doc_root) {
165-
$base = str_replace($doc_root, '', __API_ROOT__) . '/';
163+
$root = dirname($_SERVER['SCRIPT_FILENAME']);
164+
$doc_root = realpath(preg_replace('/' . preg_quote($_SERVER['SCRIPT_NAME'], '/') . '$/', '', $root));
165+
166+
if ($root != realpath($_SERVER['DOCUMENT_ROOT'])) {
167+
$base = str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', $root) . '/';
168+
} elseif ($root != $doc_root) {
169+
$base = str_replace($doc_root, '', $root) . '/';
166170
}
167171
$base = str_replace('\\', '/', $base);
168172

169173
$request_uri = str_replace($base, '', $_SERVER['REQUEST_URI']);
170174
$request_uri = explode('?', $request_uri, 2);
171175
$request_uri = $request_uri[0];
172176

177+
$request_uri = str_replace(basename(__API_ROOT__), '/', $request_uri);
178+
173179
return $request_uri;
174180
}
175181

@@ -216,8 +222,6 @@ public static function blockBots()
216222
* @param $data mixed data to sanitize
217223
*
218224
* @return $data sanitized data
219-
*
220-
* @author Marco Cesarato <cesarato.developer@gmail.com>
221225
*/
222226
public static function sanitizeHtmlentities($data)
223227
{
@@ -490,8 +494,6 @@ private static function sanitizeParams($params)
490494
* @param $data mixed data to sanitize
491495
*
492496
* @return $data sanitized data
493-
*
494-
* @author Marco Cesarato <cesarato.developer@gmail.com>
495497
*/
496498
public static function sanitizeRXSS($data)
497499
{
@@ -512,8 +514,6 @@ public static function sanitizeRXSS($data)
512514
* @param $data mixed data to sanitize
513515
*
514516
* @return $data sanitized data
515-
*
516-
* @author Marco Cesarato <cesarato.developer@gmail.com>
517517
*/
518518
private static function sanitizeXSS($data)
519519
{
@@ -549,8 +549,6 @@ private static function sanitizeXSS($data)
549549
* @param $data mixed data to sanitize
550550
*
551551
* @return $data sanitized data
552-
*
553-
* @author Marco Cesarato <cesarato.developer@gmail.com>
554552
*/
555553
public static function sanitizeStriptags($data)
556554
{
@@ -571,8 +569,6 @@ public static function sanitizeStriptags($data)
571569
* @param $data mixed data to sanitize
572570
*
573571
* @return $data sanitized data
574-
*
575-
* @author Marco Cesarato <cesarato.developer@gmail.com>
576572
*/
577573
public static function sanitizeStripslashes($data)
578574
{
@@ -622,6 +618,53 @@ public static function isConsole()
622618

623619
return false;
624620
}
621+
622+
/**
623+
* Check customer domain.
624+
*
625+
* @param array|string $customers
626+
*
627+
* @return bool
628+
*/
629+
public static function checkDomain($customers)
630+
{
631+
if (!is_array($customers)) {
632+
$customers = [$customers];
633+
}
634+
635+
foreach ($customers as $customer) {
636+
// Compare
637+
if ((self::cleanHost($_SERVER['SERVER_NAME']) == self::cleanHost($customer))) {
638+
return true;
639+
}
640+
}
641+
642+
return false;
643+
}
644+
645+
/**
646+
* Clean Host URL.
647+
*
648+
* @param $url
649+
*
650+
* @return string
651+
*/
652+
public static function cleanHost($url)
653+
{
654+
// In case scheme relative URI is passed, e.g., //www.google.com/
655+
$input = trim($url, '/');
656+
// If scheme not included, prepend it
657+
if (!preg_match('#^http(s)?://#', $input)) {
658+
$input = 'http://' . $input;
659+
}
660+
$urlParts = parse_url($input);
661+
// Remove www
662+
$domain = preg_replace('/^www\./', '', $urlParts['host']);
663+
// Lower case
664+
$domain = strtolower($domain);
665+
666+
return $domain;
667+
}
625668
}
626669

627670
$request = new Request();

0 commit comments

Comments
 (0)