Location: PHPKode > scripts > URL CSS Parser > url-css-parser/urlcss.php
<?
/**
* +------------------------------------------------------------------------------+
* URL CSS PARSER
* File         : urlcss.php
* Description  : Class to accept a URL and retrieve all embedded CSS files
* Author(s)    : Ajith Chacko (cyberajith at gmail.com)
* Web          : http://www.moltenthoughts.com
* Release Date : Oct 10/2006
* Last Updated : Oct 10/2006
* Licence      : GNU General Public License (GPL)
* Version      : 1.0
* +------------------------------------------------------------------------------+
*
* Have you ever spend time reading the source of webpages for URLs and then typing them 
* into the address bar to view what the styles said? Then this tool is for you. The URL 
* CSS PARSER helps you to easily view and retrieve CSS contents from a desired URL. 
*
* To Do List:
*  - Hyperlink Validity Check 
*  - Handling CSS links coming from parent folders 
*  - Checking CSS inside other include file types 
*  - Parser for visual display of the CSS Styles
*
*
* This has been a quick hack and there are lots of improvements necessary.
* If you used this piece of code, please let me know ur thots! ...complaints, advises, suggestions, wish-lists, thots...
* I couldnt be more happy if someone at least tried it! :)
* If you make any changes to this code let me know so that i can keep the class updated and add credits.
* 
* Ajith Chacko (cyberajith at gmail.com)
*
*
**/ 

class urlcss
{
   var $path;
   var $arr = false;
   
   function urlcss($u="") {
      $this->path =$u;
   }
   
   function getCSSArray() {
      $this->arr = true;
      return $this->listCSSFiles();
      
   }
     
   function listCSSFiles() {
      $contents = $this->getURLContents($this->path);
      if($this->arr) {
         $cssArr = $this->CSSArray($contents);
         return $cssArr;
      } else {
         $cssLinks = $this->getCSSLinks($contents);
         echo $cssLinks;
      }
   }
   
   function getURLContents($path) {
      $handle = @fopen($path, "rb");
      if(!$handle) die("Cannot open resource");
      
      $contents = '';
      while (!feof($handle)) {
        $contents .= fread($handle, 1024);
      }
      return $contents;
   }
   
   function getCSSContent($path) {
      return $this->getURLContents($path);
   }
   
   
   function CSSArray($contents) {
      preg_match_all("|[\"\'][\S]*[.]css|",$contents,$cssArray);
      $cssLink = array();
      $linkContents ="";
      for($x=0;$x<count($cssArray[0]);$x++) {
         $cssLink[$x] =  str_replace("\'","",str_replace("\"","",$cssArray[0][$x]));
      }
      if($cssLink) {
         $x=0;
         foreach($cssLink as $k=>$cssPath) {
            //to do - add check to see if the css link is a valid one
            $hyperlink = $this->makeHyperLink($cssPath);
            $linkContents[$x] =  $hyperlink;
            $x++;
         }
      }
      return $linkContents;
   }
   
   function getCSSLinks($contents)
   {
      preg_match_all("|[\"\'][\S]*[.]css|",$contents,$cssArray);
      $cssLink = array();
      $linkContents ="";
      for($x=0;$x<count($cssArray[0]);$x++) {
         $cssLink[$x] =  str_replace("\'","",str_replace("\"","",$cssArray[0][$x]));
      }
      echo "<h3>" .$this->path . "</h3>";
      $linkContents .=  "<ul>";
      if($cssLink) {
         foreach($cssLink as $k=>$cssPath) {
            $hyperlink = $this->makeHyperLink($cssPath);
            $linkContents .=  "<li><a href='$hyperlink'>".$hyperlink."</a>";
         }
      }
      else die("No CSS Links Found");
      $linkContents .=  "</ul>";
      return $linkContents;
   }   
   
   function makeHyperLink($cssPath) {
      global $path;
      $cssUrlType = $this->getURLType($cssPath);
       switch($cssUrlType) {
         case 0:
            return $this->createFullLink($cssPath);
            break;   
         
         case 1:
            return $this->createHashLink();
            break;
            
         case 2:
            return $this->createRelativeLink($cssPath);
            break;
         
         case 3:
            return $this->createSubFolderPath($cssPath);
            break;
         
         default:
            return $this->createHashLink();
            break;
       }
   }
   
   function createSubFolderPath($cssPath) {
      $domain_parts = pathinfo($this->path);
      $css_parts = pathinfo($cssPath);
      preg_match('/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?/i', $this->path, $site_parts);
      
      if(($domain_parts['dirname']=="http:") || ($domain_parts['dirname']=="https:"))
         $root = $site_parts[0] . "/";
      else 
         $root = $domain_parts['dirname'] . "/";
      $link = $root . $css_parts['dirname']."/". $css_parts['basename'];
      return $link;     
   }
   
   function createRelativeLink($cssPath) {
      $domain_parts = pathinfo($this->path);
      $css_parts = pathinfo($cssPath);
      preg_match('/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?/i', $this->path, $site_parts);
      $link = $site_parts[0] . $css_parts['dirname']."/". $css_parts['basename'];
      return $link;   
   }
   
   function createFullLink($cssPath) {
      return $cssPath;
   }
   
   function createHashLink() {
      return "#";
   }
   
   function getURLType($cssPath) {
      if(substr_count($cssPath,"http://"))
         return 0;
      
      if(substr_count($cssPath,".."))
         return 1;
         
      if((substr($cssPath,0,1)=="/") || (substr($cssPath,0,1)=="\\"))
         return 2;
      
         return 3;   
   }
}
?>
Return current item: URL CSS Parser