Location: PHPKode > scripts > Overall Tag View > overall-tag-view/OverallTagView.class.php
<?php
/**
  * PHP-Class OverallTagView Version 1.0 , released 17-NOV-2006
  * Author: Dror Golan, hide@address.com
  *
  * License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html)
  *
  * If you find it useful, you might rate it on http://www.phpclasses.org
  * If you use this class in a productional environment, you may drop me a note, so I can add a link to the page.
  *
  *
  * License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html)
  *
  * This program is free software;
  *
  * you can redistribute it and/or modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2 of the License,
  * or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along with this program;
  * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  **/

  
  
/**
  *  This class recives an HTML tag and URL as Inputs and create an associated array
  *  of the Tag's attributes according to the page source.
  *  the Overall tag is reflected using the show_overall_view() Method 
  *  output array stores all Tag attribute values according to thier original order at the
  *  Web Page : e.g. : Width of 2nd Tag [outputTagArray[2]["width"]
  *  The class comes with an examplefile.
  **/
  
  
class OverallTagView { 

    ////////////////////////////////
	//
	//	PUBLIC PARAMS
	//

		  /**
		  * @shortdesc URL of target Web page
          * @type String
		  * @public
          * @default value : www.google.com
          **/
		  var $url;
		  /**
		  * @shortdesc description of Tag to fetch
          * @type String
		  * @public
          * @default value : <img>
          **/
          var $tag;
		  /**
		  * @shortdesc Associative Array of filtered Tag's Attributes
          * @type String Array
		  * @private
          **/
           var $outputTagArray;
		   /**
		  * @shortdesc HTML src of Target url
          * @type String
		  * @public
          **/
           var $pageSrc;


// Constructor 
function OverallTagView ($URL="http://www.google.com",$useCurl=False) {

// fetch src of Target URL using Curl or fopen method.

$this->url = $URL;
if ($useCurl)
   $this->pageSrc = $this->open_external_url($this->url,"curl");
else
   $this->pageSrc = $this->open_external_url($this->url,"fopen");    
} 

////////////////////////////////
	//
	//	PUBLIC METHODS
	//

		/**
		  *
		  * @shortdesc fetch a web page source
		  * Parameters : 
		  * url : full description (incl Http://) of URL
		  * method : "curl" (in case enabled) or "fopen" (default)
		  * @public
		  * @return HTML-Output
		  *
		  **/
		  
function open_external_url($url, $method = "fopen")
{
   $data = '';
   if(strtolower($method) == "curl")
   {
       $ch = curl_init($url);
       ob_start();
       curl_exec($ch);
       curl_close($ch);
       $data = ob_get_contents();
       ob_end_clean();
   }
   else if(strtolower($method) == "fopen")
   {
       $file = fopen($url, "r");
       while(!feof($file)) {
           $data = $data . fgets($file, 4096);
       }
       fclose ($file);
   }
   return $data;
} 

/**
		  *
		  * @shortdesc strip inner text from HTML Tags
		  * url : full description (incl Http://) of URL
		  * method : "curl" (in case enabled) or "fopen" (default)
		  * @public
		  *
		  **/

function strip_text($a){
$i=-1;$n='';$ok=1;
while(isset($a{++$i})){
   if($ok&&$a{$i}!='<'){continue;}
   elseif($a{$i}=='>'){$ok=1;$n.='>';continue;}
   elseif($a{$i}=='<'){$ok=0;}
   if(!$ok){$n.=$a{$i};}}
  return $n;} 
  
/**
		  *
		  * @shortdesc create associative array from selected tag
		  * according to the Tag's attribues
		  * Parameters : 
		  * tag : full description (<xxx>) of Tag , default is : <img>
		  * @public
		  *
		  **/
  
function fetchTagIntoArray($tag = "<img>") {
   $this->tag = $tag;
   $data =   $this->strip_text($this->pageSrc);
   $data = ">".$data;
   $striped_data = strip_tags($data,$this->tag);
   $this->outputTagArray = explode("><", $striped_data); 
    
    $my_array = $this->outputTagArray;     
		foreach ($my_array as $main_key=>$main_value){
          $my_space_array[$main_key]= explode(" ",$main_value);
			foreach ($my_space_array[$main_key] as $sub_key=>$sub_value){
  				$my_pre_fetched_tag_array = explode("=",$sub_value);
  				// check for null attributes ...
  				if (($my_pre_fetched_tag_array[1] != '""') && ($my_pre_fetched_tag_array[1] != NULL))
  					$my_tag_array[$main_key][$my_pre_fetched_tag_array[0]] = $my_pre_fetched_tag_array[1];
																		 }
													}
   $this->outputTagArray = 	$my_tag_array;
   
}   												
/**
		  *
		  * @shortdesc Show Overall Tag view using an HTML Table
		  * Parameters : 
		  * appendDomain : Boolean (default Fals) , Append domain ($this-URL) to the preview pane
		  *                use this option when Images's link are relative and not Absolute
		  * @public
		  *
		  **/
		   
function show_overall_view($appendDomain = false) {  

 // print Image Table ...
 
 $my_tag_array = $this->outputTagArray;
 
 
 $HtmlSrc = '<h3> Overall View of <font color="red">&lt;'.substr($this->tag, 1, -1).'&gt</font> Tag for :'.$this->url.'</h3>';
 $HtmlSrc .= '<table>';
 foreach ($my_tag_array as $main_key=>$main_value)
     {
     $HtmlSrc .= '<tr><td bgcolor="#00ffff" width=30>'.$main_key.'</td><td bgcolor="#c0c0c0">Preview:</td>';
	 ksort($main_value);
     foreach ($main_value as $key=>$value){
	    $HtmlSrc .= '<td bgcolor="#c0c0c0"><font color="red">'.$key.'</font></td>';
		                                  }
		if ($this->tag=='<img>') {  
		if ($appendDomain)                               
		 $HtmlSrc .= '</tr><tr><td></td><td><img src='.$this->url.'\\'.$main_value["src"].'></td>'; 
		else
		 $HtmlSrc .= '</tr><tr><td></td><td><img src='.$main_value["src"].'></td>'; 
		}
		 else
		   $HtmlSrc .= '</tr><tr><td></td><td>N/A</td>';
		                                   
		 foreach ($main_value as $key1=>$value1){
		 if (strtolower($key1) != "src")
	        $HtmlSrc .= '<td bgcolor="#00ffff"><font color="blue">'.$value1.'</font></td>';
		 else
		     $HtmlSrc .= '<td bgcolor="#00ffff"><a href="'.$value1.'">link</a></td>';	
			                                  }
		 $HtmlSrc .= '</tr>';          
		    
	 }
	 
         $HtmlSrc .= '</table>';  
 
 
 echo $HtmlSrc;
 
 
} // show_overall_view



 
}  // End of Class


?>



Return current item: Overall Tag View