1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663:
<?php
define('SERVER_URL', $_SERVER['PHP_SELF']);
class Url
{
const FORBID_JS_REGEX = '(?!javascript:)';
const PROTOCOL_REGEX = '[a-z0-9-_]+(?::[a-z0-9-_]+)*://';
const USER_REGEX = '[a-z0-9-_]+(?::[a-z0-9-_]+)?@';
const DOMAIN_REGEX = '(?:[a-z0-9-_~]+\.)*[a-z0-9-_~]+(?::[0-9]{1,5})?/';
const FOLDERS_REGEX = '/*(?:[A-Za-z0-9~_\.+@,-]+/+)*';
const FILE_REGEX = '[A-Za-z0-9-+_,~:/\.\%!=]+';
const ARGS_REGEX = '(/([\w/_\.#-]*(\?)?(\S+)?[^\.\s])?)?';
const STATUS_OK = 200;
const STATUS_FOUND = 302;
private static $root = TPL_PATH_TO_ROOT;
private static $server = SERVER_URL;
private $url = '';
private $is_relative = false;
private $path_to_root = '';
private $server_url = '';
public function __construct($url = '.', $path_to_root = null, $server_url = null)
{
if (!empty($url))
{
if ($path_to_root !== null)
{
$this->path_to_root = $path_to_root;
}
else
{
$this->path_to_root = self::$root;
}
if ($server_url !== null)
{
$this->server_url = $server_url;
}
else
{
$this->server_url = self::$server;
}
$anchor = '';
if (($pos = TextHelper::strpos($url, '#')) !== false)
{
if ($pos == 0)
{
$this->url = $url;
$this->is_relative = false;
return;
}
else
{
$anchor = TextHelper::substr($url, $pos);
$url = TextHelper::substr($url, 0, $pos);
}
}
if (preg_match('`^[a-z0-9]+\:(?!//).+`iuU', $url) > 0)
{
$this->url = $url;
return;
}
else if (TextHelper::strpos($url, 'www.') === 0)
{
$url = 'http://' . $url;
}
$url = preg_replace('`^https?://' . AppContext::get_request()->get_site_domain_name() . GeneralConfig::load()->get_site_path() . '`uUi', '', self::compress($url));
if (!TextHelper::strpos($url, '://') && TextHelper::substr($url, 0, 2) != '//')
{
$this->is_relative = true;
if (TextHelper::substr($url, 0, 1) == '/')
{
$this->url = $url;
}
else
{
$this->url = $this->root_to_local() . $url;
}
}
else
{
$this->is_relative = false;
$this->url = $url;
}
$this->url = self::compress($this->url) . $anchor;
}
}
public function is_relative()
{
return $this->is_relative;
}
public function relative()
{
if ($this->is_relative())
{
return $this->url;
}
else
{
return $this->absolute();
}
}
public function rel()
{
if ($this->is_relative())
{
return TPL_PATH_TO_ROOT . '/' . ltrim($this->relative(), '/');
}
else
{
return $this->absolute();
}
}
public function absolute()
{
if ($this->is_relative())
{
return self::compress($this->get_absolute_root() . $this->url);
}
else
{
return $this->url;
}
}
public function root_to_local()
{
$local_path = $this->server_url;
$local_path = TextHelper::substr(trim($local_path, '/'), TextHelper::strlen(trim(GeneralConfig::load()->get_site_path(), '/')));
$file_begun = TextHelper::strrpos($local_path, '/');
if ($file_begun >= 0)
{
$local_path = TextHelper::substr($local_path, 0, $file_begun) . '/';
}
return '/' . ltrim($local_path, '/');
}
public static function encode_rewrite($url)
{
$url = utf8_decode(TextHelper::html_entity_decode($url));
$url = TextHelper::strtolower(strtr($url, utf8_decode('²ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöø°ÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ()[]\'"~$&%*@ç!?;,:/\^¨€{}<>«»`|+.= #'), '2aaaaaaaaaaaaoooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn -- c --- e --- '));
$url = str_replace(' ', '', $url);
$url = str_replace('---', '-', $url);
$url = str_replace('--', '-', $url);
$url = trim($url,'-');
return $url;
}
public static function check_url_validity($url)
{
if (empty($url))
return false;
$status = 0;
if (!($url instanceof Url))
{
if ($url[0] == '/' && file_exists(PATH_TO_ROOT . $url))
return true;
$url = new Url($url);
}
$file = new File($url->relative());
if ($file->exists())
return true;
$folder = new Folder($url->relative());
if ($folder->exists())
return true;
if ($url->absolute())
{
if (function_exists('stream_context_set_default'))
stream_context_set_default(array('http' => array('method' => 'HEAD', 'timeout' => 1)));
if (function_exists('get_headers'))
{
$file_headers = @get_headers($url->absolute(), true);
if (isset($file_headers[0]))
{
if(preg_match('/^HTTP\/[12]\.[01] (\d\d\d)/u', $file_headers[0], $matches))
$status = (int)$matches[1];
}
else
$status = self::STATUS_OK;
}
else
$status = self::STATUS_OK;
}
return in_array($status, array(self::STATUS_OK, self::STATUS_FOUND));
}
public static function get_url_file_size($url)
{
$file_size = 0;
if (!($url instanceof Url))
$url = new Url($url);
$file = new File($url->rel());
if ($file->exists())
$file_size = $file->get_file_size();
if (empty($file_size) && $url->absolute())
{
if (function_exists('stream_context_set_default'))
stream_context_set_default(array('http' => array('method' => 'HEAD', 'timeout' => 1)));
if (function_exists('get_headers'))
{
$file_headers = @get_headers($url->absolute(), true);
if (isset($file_headers[0]))
{
$status = 0;
if(preg_match('/^HTTP\/[12]\.[01] (\d\d\d)/u', $file_headers[0], $matches))
$status = (int)$matches[1];
if ($status == self::STATUS_OK && isset($file_headers['Content-Length']))
$file_size = $file_headers['Content-Length'];
}
}
}
return $file_size;
}
public static function compress($url)
{
$args = '';
if (($pos = TextHelper::strpos($url, '?')) !== false)
{
$args = TextHelper::substr($url, $pos);
$url = TextHelper::substr($url, 0, $pos);
}
$url = preg_replace(array('`([^:]|^)/+`u', '`(?<!\.)\./`u'), array('$1/', ''), $url);
do
{
$url = preg_replace('`/?[^/]+/\.\.`u', '', $url);
}
while (preg_match('`/?[^/]+/\.\.`u', $url) > 0);
return $url . $args;
}
public static function get_absolute_root()
{
$config = GeneralConfig::load();
return trim($config->get_complete_site_url(), '/');
}
public static function html_convert_root_relative2absolute($html_text, $path_to_root = PATH_TO_ROOT, $server_url = SERVER_URL)
{
$path_to_root_bak = self::$root;
$server_url_bak = self::$server;
self::$root = $path_to_root;
self::$server = $server_url;
$result = preg_replace_callback(self::build_html_match_regex(true),
array('Url', 'convert_url_to_absolute'), $html_text);
self::$root = $path_to_root_bak;
self::$server = $server_url_bak;
return $result;
}
public static function html_convert_absolute2root_relative($html_text, $path_to_root = PATH_TO_ROOT, $server_url = SERVER_URL)
{
$path_to_root_bak = self::$root;
$server_url_bak = self::$server;
self::$root = $path_to_root;
self::$server = $server_url;
$result = preg_replace_callback(self::build_html_match_regex(),
array('Url', 'convert_url_to_root_relative'), $html_text);
self::$root = $path_to_root_bak;
self::$server = $server_url_bak;
return $result;
}
public static function html_convert_root_relative2relative($html_text, $path_to_root = PATH_TO_ROOT, $server_url = SERVER_URL)
{
$path_to_root_bak = self::$root;
$server_url_bak = self::$server;
self::$root = $path_to_root;
self::$server = $server_url;
$result = preg_replace_callback(self::build_html_match_regex(true),
array('Url', 'convert_url_to_relative'), $html_text);
self::$root = $path_to_root_bak;
self::$server = $server_url_bak;
return $result;
}
public static function get_relative($url, $path_to_root = null, $server_url = null)
{
$o_url = new Url($url, $path_to_root, $server_url);
return $o_url->relative();
}
public static function get_wellformness_regex($protocol = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$user = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $domain = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$folders = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $file = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$args = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $forbid_js = true)
{
if ($forbid_js)
{
$protocol_regex_secured = self::FORBID_JS_REGEX . self::PROTOCOL_REGEX;
}
else
{
$protocol_regex_secured = self::PROTOCOL_REGEX;
}
$regex = RegexHelper::set_subregex_multiplicity($protocol_regex_secured, $protocol) .
RegexHelper::set_subregex_multiplicity(self::USER_REGEX, $user) .
RegexHelper::set_subregex_multiplicity(self::DOMAIN_REGEX, $domain) .
RegexHelper::set_subregex_multiplicity(self::FOLDERS_REGEX, $folders) .
RegexHelper::set_subregex_multiplicity(self::FILE_REGEX, $file);
$regex .= RegexHelper::set_subregex_multiplicity(self::ARGS_REGEX, $args);
return $regex;
}
public static function check_wellformness($url, $protocol = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$user = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $domain = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$folders = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $file = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL,
$args = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $anchor = RegexHelper::REGEX_MULTIPLICITY_OPTIONNAL, $forbid_js = true)
{
return (bool) preg_match('`^' . self::get_wellformness_regex($protocol, $user, $domain,
$folders, $file, $args, $anchor, $forbid_js) . '$`iu', $url);
}
private static function convert_url_to_absolute($url_params)
{
$url = new Url($url_params[2]);
$url_params[2] = $url->absolute();
return $url_params[1] . $url_params[2] . $url_params[3];
}
private static function convert_url_to_root_relative($url_params)
{
$url = new Url($url_params[2]);
$url_params[2] = $url->relative();
return $url_params[1] . $url_params[2] . $url_params[3];
}
private static function convert_url_to_relative($url_params)
{
$url = new Url($url_params[2]);
if ($url->is_relative())
{
$url_params[2] = self::compress(self::$root . $url->relative());
}
return $url_params[1] . $url_params[2] . $url_params[3];
}
private static function build_html_match_regex($only_match_relative = false)
{
static $regex_match_all = null;
static $regex_only_match_relative = null;
if ((!$only_match_relative && $regex_match_all === null) || ($only_match_relative && $regex_only_match_relative === null))
{
$regex = array();
$nodes = array('a', 'img', 'form', 'object', 'param name="movie"');
$attributes = array('href', 'src', 'action', 'data', 'value');
$nodes_length = count($nodes);
for ($i = 0; $i < $nodes_length; $i++)
{
$a_regex = '`(<' . $nodes[$i] . ' [^>]*(?<= )' . $attributes[$i] . '=")(';
if ($only_match_relative)
{
$a_regex .= '/';
}
$a_regex .= '[^"]+)(")`isuU';
$regex[] = $a_regex;
}
$regex[] = '`(<script><!--\s*insert(?:Sound|Movie|Swf|Youtube)Player\\(")(' . ($only_match_relative ? '/' : '') . '[^"]+)("\\)\s*--></script>)`isuU';
if ($only_match_relative)
{
$regex_only_match_relative = $regex;
}
else
{
$regex_match_all = $regex;
}
}
if ($only_match_relative)
{
return $regex_only_match_relative;
}
else
{
return $regex_match_all;
}
}
public static function to_rel($url)
{
if (!($url instanceof Url))
{
$url = new Url($url);
}
return $url->rel();
}
public static function to_relative($url)
{
if (!($url instanceof Url))
{
$url = new Url($url);
}
return $url->relative();
}
public static function to_absolute($url)
{
if (!($url instanceof Url))
{
$url = new Url($url);
}
return $url->absolute();
}
public static function is_current_url($check_url, $real_url = false)
{
$general_config = GeneralConfig::load();
$site_path = $general_config->get_default_site_path();
$current_url = str_replace($site_path, '', REWRITED_SCRIPT);
$other_home_page = trim($general_config->get_other_home_page(), '/');
$path = trim($current_url, '/');
if (!empty($path) || (!empty($other_home_page) && $path == $other_home_page))
{
$module_name = explode('/', $path);
$running_module_name = $module_name[0];
}
else
{
$module_home_page = $general_config->get_module_home_page();
if (empty($other_home_page) && !empty($module_home_page))
$running_module_name = $module_home_page;
else
$running_module_name = '';
}
$current_url = ($current_url == '/' && $running_module_name) ? '/' . $running_module_name . '/' : $current_url;
if ($real_url)
{
return $current_url == $check_url;
}
return TextHelper::strpos($current_url, $check_url) !== false;
}
}
?>