File indexing completed on 2024-05-12 17:26:16

0001 <?php
0002 
0003 /*
0004  *   TRT GFX 3.0.1 (beta build) BackToSlash
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://www.gfx3.org
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 
0015 /*
0016  * Static class used as a helper for managing various web page properties.
0017  */
0018 class EPageProperties {
0019   
0020   public static $request_uri = '';
0021   
0022   public static function get_page_name(){
0023     
0024     $name = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
0025     $name = substr($name,strrpos($name,"?"));
0026     $name = substr($name,strrpos($name,"/"));
0027     
0028     return $name;
0029   }
0030   
0031   /*
0032    * Returns the exact website in which is running this system.
0033    * For example: http://www.gfx3.org
0034    */
0035   public static function get_current_website_url(){
0036     $pageURL = 'http';
0037     //TODO: implements https?
0038     $pageURL .= "://";
0039     $pageStripped = explode("/", $_SERVER["REQUEST_URI"]);
0040     $pageStripped = $pageStripped[0];
0041     if ($_SERVER["SERVER_PORT"] != "80") {
0042       $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
0043     } else {
0044       $pageURL .= $_SERVER["SERVER_NAME"].$pageStripped;
0045     }
0046     
0047     return $pageURL;
0048   }
0049   
0050   public static function get_previous_page(){
0051     $prevpage = $_SERVER['HTTP_REFERER'];
0052     $prevpage = EHeaderDataParser::erase_get_data($prevpage);
0053     return $prevpage;
0054   }
0055   
0056   /**
0057    * @param string $domain Pass $_SERVER['SERVER_NAME'] here
0058    * @param bool $debug
0059    *
0060    * @debug bool $debug
0061    * @return string
0062    */
0063   public static function get_domain($domain, $debug = false)
0064   {
0065     $original = $domain = strtolower($domain);
0066 
0067     if (filter_var($domain, FILTER_VALIDATE_IP)) { return $domain; }
0068 
0069     $debug ? print('<strong style="color:green">&raquo;</strong> Parsing: '.$original) : false;
0070 
0071     $arr = array_slice(array_filter(explode('.', $domain, 4), function($value){
0072       return $value !== 'www';
0073     }), 0); //rebuild array indexes
0074 
0075     if (count($arr) > 2)
0076     {
0077       $count = count($arr);
0078       $_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]);
0079 
0080       $debug ? print(" (parts count: {$count})") : false;
0081 
0082       if (count($_sub) === 2) // two level TLD
0083       {
0084         $removed = array_shift($arr);
0085         if ($count === 4) // got a subdomain acting as a domain
0086         {
0087           $removed = array_shift($arr);
0088         }
0089         $debug ? print("<br>\n" . '[*] Two level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
0090       }
0091       elseif (count($_sub) === 1) // one level TLD
0092       {
0093         $removed = array_shift($arr); //remove the subdomain
0094 
0095         if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters
0096         {
0097           array_unshift($arr, $removed);
0098         }
0099         else
0100         {
0101           // non country TLD according to IANA
0102           $tlds = array(
0103             'aero',
0104             'arpa',
0105             'asia',
0106             'biz',
0107             'cat',
0108             'com',
0109             'coop',
0110             'edu',
0111             'gov',
0112             'info',
0113             'jobs',
0114             'mil',
0115             'mobi',
0116             'museum',
0117             'name',
0118             'net',
0119             'org',
0120             'post',
0121             'pro',
0122             'tel',
0123             'travel',
0124             'xxx',
0125           );
0126 
0127           if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country
0128           {
0129             array_shift($arr);
0130           }
0131         }
0132         $debug ? print("<br>\n" .'[*] One level TLD: <strong>'.join('.', $_sub).'</strong> ') : false;
0133       }
0134       else // more than 3 levels, something is wrong
0135       {
0136         for ($i = count($_sub); $i > 1; $i--)
0137         {
0138           $removed = array_shift($arr);
0139         }
0140         $debug ? print("<br>\n" . '[*] Three level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
0141       }
0142     }
0143     elseif (count($arr) === 2)
0144     {
0145       $arr0 = array_shift($arr);
0146 
0147       if (strpos(join('.', $arr), '.') === false
0148         && in_array($arr[0], array('localhost','test','invalid')) === false) // not a reserved domain
0149       {
0150         $debug ? print("<br>\n" .'Seems invalid domain: <strong>'.join('.', $arr).'</strong> re-adding: <strong>'.$arr0.'</strong> ') : false;
0151         // seems invalid domain, restore it
0152         array_unshift($arr, $arr0);
0153       }
0154     }
0155 
0156     $debug ? print("<br>\n".'<strong style="color:gray">&laquo;</strong> Done parsing: <span style="color:red">' . $original . '</span> as <span style="color:blue">'. join('.', $arr) ."</span><br>\n") : false;
0157 
0158     return join('.', $arr);
0159   }
0160   
0161   /**
0162    * Get domain without .com or .co.uk etc...
0163    * "www.example.com" -> "example"
0164    * 
0165    * can be faulty...
0166    */
0167   public static function get_clear_domain($domain)
0168   {
0169     $chunks = explode('.',$domain);
0170     if(isset($chunks[0])){
0171       if($chunks[0]=='www' and isset($chunks[1])){
0172         return $chunks[1];
0173       } else {
0174         return $chunks[0];
0175       }
0176     }
0177   }
0178   
0179 }
0180 
0181 ?>