PHP Hash Maker - MD5, SHA1 etc


2009-08-22 Digg! icurtain Delcious icurtain Technorati icurtain


The Hash maker is a quick and easy way to either make hashes and use them directly or try to work out what your lost password was

I find it quite useful to be able to generate hashes for passwords directly for the purposes of resetting passwords directly in a database


dbPHP - Object Relational Mapping for PHP


2009-05-16 Digg! icurtain Delcious icurtain Technorati icurtain


banner


PHP Colour Changing Background Algorithm


2009-03-24 Digg! icurtain Delcious icurtain Technorati icurtain


This returns the average colour of an image in PHP using GD libraries and then, assuming you ahve no sense of design, you could use this to set the background colour of a page. This code can be slotted directly into the ImageHandler class also on this site

  1.    public function getAverageColour(){ 
  2.       $colours = array('red'=>0,'green'=>0,'blue'=>0); 
  3.       $count = 0; 
  4.       $rgbstring = null; 
  5.        
  6.       for ($i=0;$iimage);$i++){ 
  7.          for ($j=0;$jimage);$j++){ 
  8.             $rgb = imagecolorat($this->image, $i, $j); 
  9.             $ici = imagecolorsforindex($this->image, $rgb); 
  10.             //print_r($ici); 
  11.             foreach($colours as $key => $value){ 
  12.                $colours[$key] = $colours[$key] + $ici[$key]; 
  13.             } 
  14.          $count++; 
  15.          } 
  16.       } 
  17.       foreach($colours as $key => $value){ 
  18.                $colours[$key] = dechex(intval($colours[$key] / $count)); 
  19.                $rgbstring = $rgbstring . sprintf("%02s", $colours[$key]); 
  20.             } 
  21.       return $rgbstring;    
  22.    }

PHP Image handler.. and why PHP is limited in usefulness


2008-03-07 Digg! icurtain Delcious icurtain Technorati icurtain


This little application was an attempt to map an image into an array of pixel objects using the PHP GD libraries and it's made me painfully aware of how limited PHP is in it's ability to handle large or complex data structures.

Below is a simple piece of code that attempts to copy each pixel value from an image into a pixel object and store it in a 2 dimensional array - anything over 150x150px and you run out of memory/if you don't store it then it takes forever to process

The only way to implement this kind of functionality within PHP is to write a specific extension for that purpose.. but then that's not really PHP anymore and it means you have to mess around with your server configuration in order to make it work... oh for java on cheap boxes :(

  1.          
  2.  
  3.            
  4.  
  5.          class Pixel{ 
  6.  
  7.            
  8.  
  9.                      private $red = null; 
  10.  
  11.                      private $green = null; 
  12.  
  13.                      private $blue = null; 
  14.  
  15.                     
  16.  
  17.                      public function setFromCI($ci){ 
  18.  
  19.                                  $this->setRed($ci['red']); 
  20.  
  21.                                  $this->setGreen($ci['green']); 
  22.  
  23.                                  $this->setBlue($ci['blue']); 
  24.  
  25.            
  26.  
  27.                      } 
  28.  
  29.                     
  30.  
  31.                      public function getRed(){ 
  32.  
  33.                                  return $this->red; 
  34.  
  35.                      } 
  36.  
  37.                     
  38.  
  39.                      public function setRed($red){ 
  40.  
  41.                                  $this->red = $red; 
  42.  
  43.                      } 
  44.  
  45.                     
  46.  
  47.                      public function getGreen(){ 
  48.  
  49.                                  return $this->green; 
  50.  
  51.                      } 
  52.  
  53.                     
  54.  
  55.                      public function setGreen($green){ 
  56.  
  57.                                  $this->green = $green; 
  58.  
  59.                      } 
  60.  
  61.            
  62.  
  63.                      public function getBlue(){ 
  64.  
  65.                                  return $this->blue; 
  66.  
  67.                      } 
  68.  
  69.            
  70.  
  71.                      public function setBlue($blue){ 
  72.  
  73.                                  $this->blue = $blue; 
  74.  
  75.                      } 
  76.  
  77.            
  78.  
  79.          } 
  80.  
  81.            
  82.  
  83.            
  84.  
  85.          class Picture{ 
  86.  
  87.            
  88.  
  89.                      private $image = null; 
  90.  
  91.                      private $imageArray = null; 
  92.  
  93.            
  94.  
  95.                      public function loadImage($url){ 
  96.  
  97.                                              $this->image = imagecreatefromjpeg($url); 
  98.  
  99.                                             
  100.  
  101.                                              //print_r($this->image); 
  102.  
  103.                      } 
  104.  
  105.            
  106.  
  107.                      public function stats(){ 
  108.  
  109.                                  echo 'width: '.imagesx($this->image).' height: '.imagesy($this->image); 
  110.  
  111.                      } 
  112.  
  113.            
  114.  
  115.                      public function toArray(){ 
  116.  
  117.                                  $this->imageArray=null; 
  118.  
  119.                                  $x = imagesx($this->image); 
  120.  
  121.                                  $y = imagesy($this->image); 
  122.  
  123.                                 
  124.  
  125.                                 
  126.  
  127.                                  for ($i=0;$i<$x;$i++){ 
  128.  
  129.                                              for ($j=0;$j<$y;$j++){ 
  130.  
  131.                                                          $p = new Pixel(); 
  132.  
  133.                                                          $rgb = imagecolorat($this->image, $i, $j); 
  134.  
  135.                                                          $p->setFromCI(imagecolorsforindex($this->image, $rgb)); 
  136.  
  137.                                                          //echo 'i'.$i.'j'.$j.'-'; 
  138.  
  139.                                                          $this->imageArray[$i][$j] = $p; 
  140.  
  141.                                              } 
  142.  
  143.                                  }           
  144.  
  145.            
  146.  
  147.                      } 
  148.  
  149.            
  150.  
  151.                      public function fromArray(){ 
  152.  
  153.                                  $x = count($this->imageArray); 
  154.  
  155.                                  $y = count($this->imageArray,COUNT_RECURSIVE)/$x-1; 
  156.  
  157.                                  echo ' x '.$x.' y '.$y; 
  158.  
  159.                                  //$img = ImageCreateTrueColor($width, $height) 
  160.  
  161.                     
  162.  
  163.                      } 
  164.  
  165.                     
  166.  
  167.                      public function getPixelAt($i, $j){ 
  168.  
  169.                                  if($this->imageArray[$i][$j]!=null){ 
  170.  
  171.                                              return $this->imageArray[$i][$j]; 
  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.          $pic = new Picture(); 
  200.  
  201.          $pic->loadImage('home.jpg'); 
  202.  
  203.          $pic->toArray(); 
  204.  
  205.          echo $pic->stats(); 
  206.  
  207.            
  208.  
  209.          $x = $pic->getPixelAt(114,114); 
  210.  
  211.          print('
    '); 
  212.  
  213.          echo $x->getGreen(); 
  214.  
  215.          print('
    '); 
  216.  
  217.          echo $x->getBlue(); 
  218.  
  219.          print('
    '); 
  220.  
  221.          echo $x->getRed(); 
  222.  
  223.          print('
    '); 
  224.  
  225.            
  226.  
  227.          $pic->fromArray(); 
  228.  
  229.            
  230.  
  231.          ?>

PHP Get/Set Generator - getters and setters for public functions


2008-07-23 Digg! icurtain Delcious icurtain Technorati icurtain


get/set code generator for PHP - all Java development environments allow you to generate getter and setter code automatically

enter comma seperated variable names: size, length, width, etc


Projects for July 2008


2008-07-13 Digg! icurtain Delcious icurtain Technorati icurtain


Finish ORM classes for database and XML
Work on existing systems to implement full XML output from logic classes to display classes

PHP Variable Variables..


2008-07-01 Digg! icurtain Delcious icurtain Technorati icurtain


$var = "value";
$value = "some value";
echo $$var;

For some insane reason PHP allows you to have variable variables.. if you ever use these you should probably be prohibited from using a computer again


Session Cookie Manager class for cookies in PHP


2008-06-27 Digg! icurtain Delcious icurtain Technorati icurtain



PHP ArrayList Class


2008-06-25 Digg! icurtain Delcious icurtain Technorati icurtain


PHP ArrayList Class - Having spent the last year working in Java, working with PHP now makes me cry and weep in anguish.. for many reasons, one of the main ones being its' complete inability to store more than 1 thing in anything other than an array

I have attempted to create a class that handles data in a marginally more friendly manner allowing you to interate through etc.. it's still a bit BETA but it speeds up my development times.. so here it is

  1. class ArrayList { 
  2.  
  3. //Copyright Mike Lang - icurtain.co.uk - please retain this header and give credit if used. 
  4. //This is a little class I've written to make my life easier and a little more like Java 
  5. //for those occasions when i just can't afford Java hosting 
  6. //It doesnt work the same as a Java arrayList.. it's just a high level aproximation of it 
  7. //if you have any comments or suggestions for improving or changing this class feel free to mail me 
  8. //mike [at] bluemedia dot co dot uk 
  9.  
  10.    //ARRAY LIST CLASS STARTS COUNTING AT 0!!!!! 
  11.    private $arrayList = array(); 
  12.    private $pointer = 0; 
  13.  
  14.    public function version(){ 
  15.    echo 'version 1.0'; 
  16.    } 
  17.     
  18.    public function add($item){ 
  19.       //$this->arrayList[sizeof($this->arrayList)] = $item; 
  20.       array_push($this->arrayList, $item); 
  21.    } 
  22.  
  23.    public function addAtPos($position, $item){ 
  24.       if($position < count($this->arrayList) && $position >= 0) 
  25.       { 
  26.       $this->add($item); 
  27.       $this->shift(count($this->arrayList)-1, $position); 
  28.       } 
  29.       else 
  30.       { 
  31.       throw new Exception('List out of bounds'); 
  32.       } 
  33.    } 
  34.  
  35.    public function getList(){ 
  36.       return $this->arrayList; 
  37.    } 
  38.  
  39.    public function hasValue(){ 
  40.       if(isset($this->arrayList[$this->pointer])) 
  41.          { 
  42.             return true; 
  43.          } 
  44.       else 
  45.          { 
  46.             return false; 
  47.          } 
  48.     } 
  49.  
  50.     public function hasNext(){ 
  51.       if($this->pointer <= count($this->arrayList)-1) 
  52.          { 
  53.             return true; 
  54.          } 
  55.       else 
  56.          { 
  57.             return false; 
  58.          } 
  59.     } 
  60.  
  61.  
  62.    public function next(){ 
  63.       if(isset($this->arrayList[$this->pointer])) 
  64.       { 
  65.          //return $this->arrayList[($this->pointer++)-1] = $value; 
  66.       $this->pointer++; 
  67.          return($this->arrayList[$this->pointer-1]); 
  68.       } 
  69.       else 
  70.       { 
  71.          return null; 
  72.       } 
  73.       } 
  74.  
  75.    public function shift($origin, $dest){ 
  76.          //wont shift from last element 
  77.          if($origin > count($this->arrayList) || $origin < 0 || $dest > count($this->arrayList) || $dest < 0) 
  78.          { 
  79.          throw new Exception('List out of bounds'); 
  80.          } 
  81.          if($origin > $dest) 
  82.             { 
  83.             $temp = $this->arrayList[$origin]; 
  84.             $this->shiftUp($origin, $dest); 
  85.             $this->arrayList[$dest] = $temp; 
  86.             } 
  87.             else 
  88.             { 
  89.             $temp = $this->arrayList[$origin]; 
  90.             $this->shiftDown($origin, $dest); 
  91.             $this->arrayList[$dest] = $temp; 
  92.             } 
  93.    } 
  94.  
  95.    private function shiftUp($origin, $dest) 
  96.          { 
  97.             for($i=$origin;$i>$dest;$i--) 
  98.                               { 
  99.                               $this->arrayList[$i] = $this->arrayList[$i-1]; 
  100.                               } 
  101.          } 
  102.  
  103.    private function shiftDown($origin, $dest) 
  104.          { 
  105.             for($i=$origin;$i<$dest;$i++) 
  106.                               { 
  107.                               $this->arrayList[$i] = $this->arrayList[$i+1]; 
  108.                               } 
  109.          } 
  110.  
  111.    public function remove($item){ 
  112.       if(array_key_exists($item, $this->arrayList)){ 
  113.          unset($this->arrayList[$item]); 
  114.       } 
  115.       else  
  116.       { 
  117.       throw new Exception('key not found'); 
  118.       } 
  119.    } 
  120.  
  121.    public function addArray($array){ 
  122.     foreach ($array as $item) { 
  123.          $this->add($item); 
  124.             } 
  125.    } 
  126.     
  127.    public function reverse(){ 
  128.       $this->arrayList = array_reverse($this->arrayList); 
  129.    } 
  130.  
  131.       public function size(){ 
  132.       return count($this->arrayList); 
  133.    } 
  134.  
  135.       public function reset(){ 
  136.       $this->pointer = 0; 
  137.    } 
  138.  
  139.    public function end(){ 
  140.       $this->pointer = count($this->arrayList) -1; 
  141.    } 
  142.  
  143. ?>

With it you can while($arrayList->hasNext()){} and do all kinds of other crazy things that will bring endless listing joy to your life - mail me if you have any improvement suggestions


PHP Luhn Checker - validates credit cards etc


2008-06-09 Digg! icurtain Delcious icurtain Technorati icurtain


This section of code validates a credit card or any other luhn number - useful as an initial card number check for merchant interfaces etc

the function returns either 0 or 1 depending on whether the luhn check was successful. The luhn check does a load of recursive calculation and then divides by 10 and checks if there is a remainder

  1. function luhnCheck($number){ 
  2.     
  3.    $sum = 0; 
  4.    $alt = false; 
  5.     
  6.    for($i = strlen($number) - 1; $i >= 0; $i--){ 
  7.       $n = substr($number, $i, 1); 
  8.       if($alt){ 
  9.          //square n 
  10.          $n *= 2; 
  11.          if($n > 9) { 
  12.             //calculate remainder 
  13.             $n = ($n % 10) +1; 
  14.          } 
  15.       } 
  16.       $sum += $n; 
  17.       $alt = !$alt; 
  18.    } 
  19.    //echo $sum; 
  20.    //if $sum divides by 10 with no remainder then it's valid    
  21.    return ($sum % 10 == 0); 
  22. }

Object Relational Mapping in PHP


2008-01-15 Digg! icurtain Delcious icurtain Technorati icurtain


http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software#PHP


SecPay 3D OsCommerce


2007-07-31 Digg! icurtain Delcious icurtain Technorati icurtain


code i need for tuesday

class Sec3d{ function getSecureEnrollmentReponse { if(isset($_POST['valid']) { $secEnRes[valid] = $_POST['valid']; $secEnRes[trans_id] = $_POST['trans_id']; $secEnRes[test_status] = $_POST['test_status']; $secEnRes[hash] = $_POST['hash']; $secEnRes[mpi_status_code] = $_POST['mpi_status_code']; $secEnRes[mpi_message] = $_POST['mpi_message']; return($secEnRes); } function doSecureEnrollmentResponse($secEnRes){ switch($secEnRes[mpi_status_code]) case 200: echo 'Payer Verification Required'; break; case 212: echo 'Cardholder not enrolled'; break; } }

PHP Class Example


2007-07-21 Digg! icurtain Delcious icurtain Technorati icurtain


instanceVar; } public static function staticFunction(){ return self::$staticVar; } } $object = new TestClass(); //call transient on object $object -> transientFunction(); //call function from class TestClass::staticFunction(); ?>