Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sascha Tayefeh http://www.tayefeh.de

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Blacklist String Validator Class

Check string for forbidden stuff. The constructur needs one single string argument, that contains the URI to the blacklist.txt file. This list contains one word per line of words that must not occur in the string to validate.

   1  
   2  <?php
   3     /*
   4     CheckContent
   5     (c) 2007 by S TAYEFEH
   6  
   7     Check string for forbidden stuff.
   8     The constructur needs one single string argument, 
   9     that contains the URI to the blacklist.txt file. 
  10     This list contains one word per line of words that 
  11     must not occur in the string to validate.
  12  
  13     Example use:
  14  
  15        require_once "CheckContent.php";
  16        $val = new CheckContent("pbblacklist.txt");
  17        if ($val->validate("blabla")) echo "ok";
  18        else echo "failed";
  19  
  20     */
  21  
  22     class CheckContent
  23     {
  24        public $blacklistFN; // The textfile with the list of prohibited expressions
  25        public $content="";  // The content to check - A single string
  26        private $blacklistA=array();
  27        private $isValid=TRUE;
  28  
  29        // constructor
  30        public function CheckContent($blacklist)
  31        {
  32  	 $this->isValid=TRUE;
  33  	 $this->blacklistFN=$blacklist;
  34  	 $this->content="EMPTY";
  35        }
  36  
  37        // Main Method
  38        public function validate($content)
  39        {
  40  	 $this->content=$content;
  41  	 if(!$this->readFile()) return FALSE;
  42  
  43  	 foreach ($this->blacklistA as $a)
  44  	 {
  45  	    if(preg_match('/'.$a.'/i',$content)) $this->isValid=FALSE;
  46  	 }
  47  	 return $this->isValid;
  48        }
  49  
  50        // Read File
  51        private function readFile()
  52        {
  53  	 $blacklistTemp=array();
  54  	 $blacklistS=FALSE;
  55  	 $blacklistS=file_get_contents($this->blacklistFN);
  56  	 if (!$blacklistS) 
  57  	  {
  58  	     echo "*** ERROR from CheckContent:: - Could not read blacklist file: ".$this->blacklistFN;
  59  	     return FALSE;
  60  	  }
  61  	 $blacklistTemp=explode("\n",$blacklistS);
  62  	 // Clean array
  63  	 $this->blacklistA=array();
  64  	 // Only lines that contain letters or digits
  65  	 foreach ($blacklistTemp as $a) if( ereg("[a-zA-Z0-9]",$a) ) array_push($this->blacklistA,trim($a));
  66  	 sort($this->blacklistA); // Cosmetic ;-)
  67  	 return TRUE;
  68        }
  69     }
  70  
  71  

SocketJpeg

   1  
   2  <?
   3  /*
   4   socketJpeg.php
   5   2007 by Sascha Tayefeh
   6  
   7   This script
   8   1. Opens a socket to a server
   9   2. Sends a GET-request
  10   3. Reads the header
  11   4. Sends a jpeg-header to your browser
  12   5. Sends the jpeg to your server
  13  
  14  */
  15  
  16  $server="www.ilenvo.de";
  17  $pic ="/kunden/sascha/pb/blog/1170195444-viper.jpg";
  18  
  19  $fp = fsockopen($server, 80, $errno, $errstr, 30);
  20  if (!$fp) {
  21     echo "$errstr ($errno)<br />\n";
  22  } else {
  23     $out = "GET $pic HTTP/1.1\r\n";
  24     $out .= "Host: $server\r\n";
  25     $out .= "Connection: Close\r\n\r\n";
  26  
  27     fwrite($fp, $out);
  28     $img="";
  29     $fill=0;
  30     while (!feof($fp)) {
  31        /*
  32  
  33        $buffer = fgets($fp, 1024);
  34        echo strlen($buffer)." - ".$buffer;
  35        echo "<br>";
  36        */
  37  
  38        /* Comment this for printing the header */
  39        if($fill==0) 
  40        { 
  41  	 $buffer = fgets($fp, 1024);
  42  	 if (strlen($buffer)==2) $fill=1;
  43        } else if($fill==1)
  44        {
  45  	 $img.=fgets($fp, 1096); 
  46        }
  47        /**/
  48     }
  49     fclose($fp);
  50  
  51     $len=strlen($img);
  52     header('Content-type: image/jpeg');
  53     header("Content-Length: $len");
  54     echo $img;
  55  
  56  }
  57  
  58  
  59  ?>
  60  

PHP Exif Reader Demo


A rude Exif-Reader. Currently, only local files are being uploaded and proceeded. Hey, it's just a demo, do not implement it for public access.

   1  
   2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   4  <head>
   5  <title>Extract Exif-Data Demo</title>
   6  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
   7  </head>
   8  <body>
   9  
  10  <?php
  11  
  12  
  13  
  14     function printExif($fileName)
  15     {
  16        // Written in 2007 by Sascha Tayefeh
  17        // http://www.tayefeh.de
  18        // This code is FREE for all
  19        if(function_exists('exif_read_data'))
  20        echo "exif library present";
  21        else
  22        echo "exif library absent";
  23  
  24        $exifDat=@exif_read_data($fileName);
  25  
  26        $myExif=array(
  27  	 "FocalLength" => "Focal Length",
  28  	 "ISOSpeedRatings" => "ISO",
  29  	 "ExposureTime" => "Shutter",
  30  	 "Make" => "Manufacturer",
  31  	 "Model" => "Model",
  32  	 "Software" => "Software",
  33  	 "FNumber"	=> "Focal Number",
  34  	 "Country"	=> "Country",
  35  	 "Lens"	=> "Lens",
  36  	 "LensId"	=> "Lens Id"
  37        );
  38  
  39        if($exifDat)
  40        {
  41  	 foreach ($myExif as $key => $value)
  42  	 if(isset($exifDat[$key]) && $exifDat[$key]) 
  43  	    echo "<br/><strong>$value</strong>: ".$exifDat[$key];
  44  
  45  	 if($exifDat['COMPUTED']['ApertureFNumber']) 
  46  	    echo "<br /><strong>Aperture</strong>: ".$exifDat['COMPUTED']['ApertureFNumber'];	
  47  
  48  	 if($exifDat['Flash']) echo "<br/><strong>Flash</strong>: shot";
  49  	 else echo "<br/><strong>Flash</strong>: not shot";
  50  
  51  	 if($exifDat['DateTimeOriginal']) 
  52  	 {
  53  	    $myDate[1]=explode(' ',$exifDat['DateTimeOriginal']);
  54  	    $myDate[0]=explode(':',$myDate[1][0]);
  55  
  56  	    echo "<br/><strong>Date</strong>: ".$myDate[0][2]."-".$myDate[0][1]."-".$myDate[0][0];
  57  	    echo "<br/><strong>Time</strong>: ".$myDate[1][1];
  58  	 }
  59  
  60        }
  61     } // End Function
  62  
  63     if (!isset($_POST['go']))
  64     {
  65     ?>
  66     <h1>Extract some Exif-Data from file</h1>
  67     <form 	enctype="multipart/form-data" 
  68  action="<? echo $_SERVER['PHP_SELF']; ?>" 
  69  method="post"
  70  >
  71  <dl>
  72  <dt>ImageFile URL:</dt>
  73  <dd><input type="file" name="imgFile" size="40" /></dd>
  74  <dt>&nbsp;</dt>
  75  <dd><input type="submit" value="Start Upload (may take a while)" /></dd>
  76  </dl>
  77  <input style="display: none" name="go" value="analyse" />
  78  </form>
  79  <?
  80     } else {
  81     ?>
  82     <h1>Exif-Data:</h1>
  83     <?
  84     $img=$_FILES['imgFile']['tmp_name'];
  85     printExif($img);
  86  ?>
  87  
  88  <?
  89     } // END IF POST
  90  ?>
  91  
  92  </body>
  93  </html>
  94  

Read rpc list from file and ping all - for your blog!

This is a PHP5-based XML-RPC ping script. It reads a one-column fully qualified URL-list from a file ($pingListFile). Such a script is extremely useful for bloggers ;-)

Please, edit the variables at the top of the file to fit your needs.

   1  
   2  <?php
   3     // Please, edit these variables to your needs
   4  
   5     $blogTitle="Title Of your blog";
   6     $blogUrl="http://www.yourblog.url/";
   7     $pingListFile="pinglist.txt";
   8     $showDebugInfo=FALSE; // Do you want verbose output?
   9  
  10     // Stop editing here
  11  
  12     // PingRPC.php
  13     //
  14     // 2007 by Sascha Tayefeh
  15     // http://www.tayefeh.de
  16     //
  17     // This is a PHP5-based XML-RPC ping script. It reads a one-column
  18     // fully qualified URL-list from a file ($pingListFile). Here is
  19     // an example how this file must look like:
  20     // ----------------------
  21     // http://rpc.icerocket.com:10080/
  22     // http://rpc.pingomatic.com/
  23     // http://rpc.technorati.com/rpc/ping
  24     // http://rpc.weblogs.com/RPC2
  25     // ----------------------
  26  
  27     $replacementCount=0; 
  28     $userAgent="pingrpc.php by tayefeh";
  29  
  30     // Read pinglist file. Must contain one fully qualified URL
  31     // (e.g: http://rpc.technorati.com/rpc/ping) PER LINE (-> 
  32     // delimiter is an ASCII-linebreak)
  33     $fp=fopen($pingListFile,"r");
  34     while ( ! feof( $fp) )
  35     {
  36        $line = trim(fgets( $fp, 4096));
  37        // get the hostname
  38        $host=$line; // Make a copy of $line
  39        $host=preg_replace('/^.*http:\/\//','',$host); // Delete anything before http://
  40        $host=preg_replace('/\/.*$/','',$host); // Delete anything after behind the hostname
  41  
  42        // get the path 
  43        $path=$line; // Make another copy of $line
  44        $path=preg_replace('/^.*http:\/\/[a-zA-Z0-9\-_\.]*\.[a-zA-Z]{1,3}\//','',$path,-1,$replacementCount); // Delete anything before the path
  45        if(!$replacementCount) $path=''; // if there was no replacement (i.e. no explicit path), act appropiately
  46        if($host) $myList[$host]=$path;
  47     }
  48     echo "<h1>Ping process started</h1>";
  49  
  50     echo "<p>Reading URLs from file $pingListFile: ";
  51     echo count($myList)." urls read.</p>";
  52  
  53     // Use DOM to create the XML-File
  54     $xml= new DOMDocument('1.0');
  55     $xml->formatOutput=true;
  56     $xml->preserveWhiteSpace=false;
  57     $xml->substituteEntities=false;
  58  
  59     // Create the xml structure
  60     $methodCall=$xml->appendChild($xml->createElement('methodCall'));
  61     $methodName=$methodCall->appendChild($xml->createElement('methodName'));
  62     $params=$methodCall->appendChild($xml->createElement('params'));
  63     $param[1]=$params->appendChild($xml->createElement('param'));
  64     $value[1]=$param[1]->appendChild($xml->createElement('value'));
  65     $param[2]=$params->appendChild($xml->createElement('param'));
  66     $value[2]=$param[2]->appendChild($xml->createElement('value'));
  67  
  68     // Set the node values
  69     $methodName->nodeValue="weblogUpdates.ping";
  70     $value[1]->nodeValue=$blogTitle;
  71     $value[2]->nodeValue=$blogUrl;
  72  
  73     $xmlrpcReq = $xml->saveXML(); // Write the document into a string
  74     $xmlrpcLength = strlen( $xmlrpcReq ); // Get the string length.
  75  
  76     echo "Here&apos;s the xml-message I generated (size: $xmlrpcLength bytes):";
  77  
  78     echo "\n<pre>\n";
  79     echo htmlentities($xmlrpcReq);
  80     echo "</pre>";
  81  
  82     echo "<dl>";
  83  
  84     // Proceed every link read from file
  85     foreach ( $myList as $host => $path)
  86     {
  87        if($showDebugInfo) echo "<hr/>";
  88  
  89        echo "<dt><strong>Pinging host: $host  </strong>";
  90        $httpReq  = "POST /" . $path . " HTTP/1.0\r\n";
  91        $httpReq .= "User-Agent: " . $userAgent. "\r\n";
  92        $httpReq .= "Host: " . $host . "\r\n";
  93        $httpReq .= "Content-Type: text/xml\r\n";
  94        $httpReq .= "Content-length: $xmlrpcLength\r\n\r\n";
  95        $httpReq .= "$xmlrpcReq\r\n";
  96        echo "</dt>";
  97  
  98        if($showDebugInfo)
  99        {
 100  	 echo "<dd><strong>Request:</strong><pre><span style=\"color: #cc9900\">".htmlentities($httpReq)."</span></pre>";
 101  	 echo "<strong>Answer</strong>:<span style=\"color: #99cc00\"><pre>";
 102        }
 103  
 104        // Actually, send ping
 105        if ( $pinghandle = @fsockopen( $host, 80 ) )
 106        {
 107  	 @fputs( $pinghandle, $httpReq );
 108  	 while ( ! feof( $pinghandle ) )
 109  	 { 
 110  	    $pingresponse = @fgets( $pinghandle, 128 );
 111  	    if($showDebugInfo) echo htmlentities($pingresponse);
 112  	 }
 113  	 @fclose( $pinghandle );
 114        }
 115        if($showDebugInfo) echo "</span></pre></dd>";
 116     }
 117     echo "</dl>";
 118     echo "<p>FINISHED</p>";
 119  
 120  ?> 
 121  


Here's an example input-file (pinglist.txt):
   1  
   2  http://blogsearch.google.com/ping/RPC2
   3  http://rpc.technorati.com/rpc/ping
   4  http://www.newsisfree.com/xmlrpctest.php
   5  http://ping.bitacoras.com
   6  http://ping.blo.gs/
   7  http://ping.bloggers.jp/rpc/
   8  http://api.moreover.com/ping
   9  http://api.my.yahoo.com/RPC2
  10  http://api.my.yahoo.com/rss/ping
  11  http://www.bitacoles.net/ping.php
  12  http://bitacoras.net/ping
  13  http://blogdb.jp/xmlrpc
  14  http://www.blogdigger.com/RPC2
  15  http://www.blogoole.com/ping/
  16  http://www.blogoon.net/ping/
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS