[ PHPXref.com ] [ Generated: Sun Jul 20 18:31:52 2008 ] [ Mailing List 1.03 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/lib/ -> common.php (source)

   1  <?php
   2  	function FileExt($filename) {
   3          if (!strstr($filename, "."))
   4              return array("0"=>$filename,"1"=>"");
   5          $filename = strrev($filename);
   6          $extpos = strpos($filename , ".");
   7          $file = strrev(substr($filename , $extpos + 1));
   8          $ext = strrev(substr($filename ,  0 , $extpos));
   9          return array("0"=>$file,"1"=>$ext);
  10      }
  11  function UploadFile($source, $destination , $name ="") {
  12      $name = $name ? $name : basename($source);
  13      $name = FileExt($name);
  14      $name[2]= $name[0];
  15      $counter = 0 ;
  16      while (file_exists( $destination . $name[0] . "." . $name[1] )) {
  17          $name[0] = $name[2] . $counter;
  18          $counter ++;
  19      }
  20      copy($source , $destination . $name[0] . "." . $name[1] );
  21      @chmod($destination . $name[0] . "." . $name[1] , 0777);
  22  }
  23  function UploadFileFromWeb($source, $destination , $name) {
  24      $name = FileExt($name);
  25      $name[2]= $name[0];
  26      $counter = 0 ;
  27      while (file_exists( $destination . $name[0] . "." . $name[1] )) {
  28          $name[0] = $name[2] . $counter;
  29          $counter ++;
  30      }
  31      SaveFileContents($destination . $name[0] . "." . $name[1] , $source);
  32      @chmod($destination . $name[0] . "." . $name[1] , 0777);
  33  }
  34  function GetFileContents($file_name) {
  35       $file = fopen($file_name,"r");
  36      if (!$file)
  37          return null;
  38      if (strstr($file_name,"://"))
  39          while (!feof($file))
  40              $result .= fread($file,1024);
  41      else
  42          $result = @fread($file,filesize($file_name));
  43      fclose($file);
  44      return $result;
  45  }
  46  function SaveFileContents($file_name,$content) {
  47      $file = fopen($file_name,"w");
  48      fwrite($file,$content);
  49      fclose($file);
  50  }
  51  function Debug($what,$pre = 1,$die = 0) {
  52      if (PB_DEBUG_EXT == 1) {
  53          if ($pre == 1)
  54              echo "<pre style=\"background-color:white;\">";
  55          print_r($what);
  56          if ($pre == 1)
  57              echo "</pre>";
  58          if ($die == 1)
  59              die;
  60      }
  61  }
  62  function SendMail($to,$from,$subject,$message,$to_name,$from_name) {    
  63      if ($to_name)
  64          $to = "$to_name <$to>";
  65      $headers  = "MIME-Version: 1.0\n";
  66      $headers .= "Content-type: text; charset=iso-8859-1\n";
  67      if ($from_name) {
  68          $headers .= "From: $from_name <$from>\n";
  69          $headers .=    "Reply-To: $from_name <$from>\n";
  70      }
  71      else {
  72          $headers .= "From: $from\n";
  73          $headers .=    "Reply-To: $from\n";
  74      }
  75      $headers .=    "X-Mailer: PHP/" . phpversion();
  76      return mail($to, $subject, $message,$headers);        
  77  }
  78  function FillVars($var,$fields,$with) {
  79      $fields = explode (",",$fields);
  80      foreach ($fields as $field)
  81          if (!$var[$field])
  82              !$var[$field] = $with;
  83      return $var;
  84  }
  85  function CleanupString($string,$strip_tags = TRUE) {
  86      $string = addslashes(trim($string));
  87      if ($strip_tags)
  88          $string = strip_tags($string);
  89      return $string;
  90  }
  91  define("RX_EMAIL","^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$");
  92  define("RX_CHARS","[a-z\ ]");
  93  define("RX_DIGITS","[a-z0-9]"); 
  94  define("RX_ALPHA","[^a-z0-9_]");
  95  define("RX_ZIP","[0-9\-]"); 
  96  define("RX_PHONE","[0-9\-\+\(\)]");
  97  function CheckString($string,$min,$max,$regexp = "",$rx_result = FALSE) {
  98      if (get_magic_quotes_gpc() == 0)
  99          $string = CleanupString($string);
 100      if ($regexp == RX_DIGITS) 
 101          $string == intval($string);
 102      if (strlen($string) < $min)
 103          return 1;
 104      elseif (($max != 0) && (strlen($string) > $max))
 105          return 2;
 106      elseif ($regexp != "") {
 107              if ($regexp == RX_DIGITS) {
 108                  return $string == intval($string) ? 3 : 0; 
 109              } else
 110              if ($rx_result == eregi($regexp,$string))
 111                  return 3;
 112          }
 113      return 0;
 114  }
 115  function ValidateVars($source,$vars) {
 116      $vars = explode(",",$vars);
 117      foreach ($vars as $var) {
 118          list($name,$type,$min,$max) = explode(":",$var);
 119          switch ($type) {
 120              case "S":
 121                  $type = RX_CHARS;
 122                  $rx_result = FALSE;
 123              break;
 124              case "I":
 125                  $type = RX_DIGITS;
 126                  $rx_result = false;
 127              break;
 128              case "E":
 129                  $type = RX_EMAIL;
 130                  $rx_result = FALSE;
 131              break;
 132              case "P":
 133                  $type = RX_PHONE;
 134                  $rx_result = TRUE;
 135              break;
 136              case "Z":
 137                  $type = RX_ZIP;
 138                  $rx_result = FALSE;
 139              break;
 140              case "A":
 141                  $type = "";
 142              break;
 143              case "F":
 144                  $type = RX_ALPHA;
 145                  $rx_result = TRUE;
 146              break;
 147          }
 148          if (($result = CheckString($source[strtolower($name)],$min,$max,$type,$rx_result)) != 0)
 149              $errors[] = $name;
 150      }    
 151      return is_array($errors) ? $errors : 0;
 152  }
 153  function ResizeImage($source,$destination,$size) {
 154      if (PB_IMAGE_MAGICK == 1)
 155          system( PB_IMAGE_MAGICK_PATH . "convert $source -resize {$size}x{$size} $destination");
 156      else
 157          copy($source,$destination);
 158  }
 159  function GetMicroTime() {
 160      list($usec,$sec) = explode(" ",microtime());
 161      return (float) $usec + (float) $sec;
 162  }
 163  function GetArrayPart($input,$from,$count) {
 164      $return = array();
 165      $max = count($input);
 166      for ($i = $from; $i < $from + $count; $i++ ) 
 167          if ($i<$max)
 168              $return[] = $input[$i];
 169      return $return;    
 170  }
 171  function ReplaceAllImagesPath($htmldata,$image_path) {
 172      $htmldata = stripslashes($htmldata);
 173      $htmldata = str_replace("<IMG","<img",$htmldata);
 174      preg_match_all("'<img.*?>'si",$htmldata,$images);
 175      foreach ($images[0] as $image)
 176          $htmldata = str_replace($image,ReplaceImagePath($image,$image_path),$htmldata);
 177      return $htmldata;
 178  }
 179  function ReplaceImagePath($image,$replace) {
 180      $image = stripslashes($image);
 181      $image = str_replace("<","",$image);
 182      $image = str_replace(">","",$image);
 183      $image_arr = explode(" ",$image);
 184      for ($i = 0;$i < count($image_arr) ;$i++ ) {
 185          if (stristr($image_arr[$i],"src")) {
 186              $image_arr[$i] = explode("=",$image_arr[$i]);
 187              $image_arr[$i][1] = str_replace("'","",$image_arr[$i][1]);
 188              $image_arr[$i][1] = str_replace("\"","",$image_arr[$i][1]);
 189              $image_arr[$i][1] = strrev(substr(strrev($image_arr[$i][1]),0,strpos(strrev($image_arr[$i][1]),"/")));
 190              $image_arr[$i][1] = "\"" . $replace . $image_arr[$i][1] . "\"";
 191              $image_arr[$i] = implode ("=",$image_arr[$i]);
 192          }        
 193      }    
 194      return "<" . implode(" ",$image_arr) . ">";
 195  }
 196  function DowloadAllImages($images,$path) {    
 197      foreach ($images as $image)
 198          @SaveFileContents($path ."/".ExtractFileNameFromPath($image),@implode("",@file($image)));    
 199  }
 200  function GetAllImagesPath($htmldata) {
 201      $htmldata = stripslashes($htmldata);
 202      $htmldata = str_replace("<IMG","<img",$htmldata);
 203      preg_match_all("'<img.*?>'si",$htmldata,$images);
 204      foreach ($images[0] as $image)
 205          $images_path[] = GetImageName($image);
 206      return $images_path;
 207  }
 208  function GetImagePath($image) {
 209      $image = stripslashes($image);
 210      $image = str_replace("<","",$image);
 211      $image = str_replace(">","",$image);
 212      $image_arr = explode(" ",$image);
 213      for ($i = 0;$i < count($image_arr) ;$i++ ) {
 214          if (stristr($image_arr[$i],"src")) {
 215              $image_arr[$i] = explode("=",$image_arr[$i]);
 216              $image_arr[$i][1] = str_replace("'","",$image_arr[$i][1]);
 217              $image_arr[$i][1] = str_replace("\"","",$image_arr[$i][1]);
 218              return strrev(substr(strrev($image_arr[$i][1]),0,strpos(strrev($image_arr[$i][1]),"/")));;
 219          }        
 220      }    
 221      return "";
 222  }
 223  function GetImageName($image) {
 224      $image = stripslashes($image);
 225      $image = str_replace("<","",$image);
 226      $image = str_replace(">","",$image);
 227      $image_arr = explode(" ",$image);
 228      for ($i = 0;$i < count($image_arr) ;$i++ ) {
 229          if (stristr($image_arr[$i],"src")) {
 230              $image_arr[$i] = explode("=",$image_arr[$i]);
 231              $image_arr[$i][1] = str_replace("'","",$image_arr[$i][1]);
 232              $image_arr[$i][1] = str_replace("\"","",$image_arr[$i][1]);
 233              return $image_arr[$i][1];
 234          }        
 235      }    
 236      return "";
 237  }
 238  function ExtractFileNameFromPath($file) {
 239      return basename($file);
 240  }
 241  function RemoveArraySlashes($array) {
 242      if ($array)        
 243          foreach ($array as $key => $item)
 244              if (is_array($item)) 
 245                  $array[$key] = RemoveArraySlashes($item);
 246              else        
 247                  $array[$key] = stripslashes($item);
 248      return $array;
 249  }
 250  function AddArraySlashes($array) {
 251      if ($array)        
 252          foreach ($array as $key => $item)
 253              if (is_array($item)) 
 254                  $array[$key] = AddArraySlashes($item);
 255              else        
 256                  $array[$key] = addslashes($item);
 257      return $array;
 258  }
 259  function Ahtmlentities($array) {
 260      if (is_array($array))        
 261          foreach ($array as $key => $item)
 262              if (is_array($item)) 
 263                  $array[$key] = ahtmlentities($item);
 264              else        
 265                  $array[$key] = htmlentities(stripslashes($item),ENT_COMPAT);
 266      else
 267          return htmlentities(stripslashes($array),ENT_COMPAT);
 268      return $array;
 269  }
 270  function AStripSlasshes($array) {
 271      if (is_array($array))        
 272          foreach ($array as $key => $item)
 273              if (is_array($item)) 
 274                  $array[$key] = AStripSlasshes($item);
 275              else        
 276                  $array[$key] = stripslashes($item);
 277      else
 278          return stripslashes($array);
 279      return $array;
 280  }
 281  function Ahtml_entity_decode($array) {
 282      if ($array)    
 283          foreach ($array as $key => $item)
 284              if (is_array($item))
 285                  $array[$key] = ahtml_entity_decode($item);
 286              else        
 287                  $array[$key] = html_entity_decode($item,ENT_COMPAT);
 288      return $array;
 289  }
 290  function array2xml ($name, $value, $indent = 1)
 291  {
 292   $indentstring = "\t";
 293   for ($i = 0; $i < $indent; $i++)
 294   {
 295     $indentstring .= $indentstring;
 296   }
 297   if (!is_array($value))
 298   {
 299     $xml = $indentstring.'<'.$name.'>'.$value.'</'.$name.'>'."\n";
 300   }
 301   else
 302   {
 303     if($indent === 1)
 304     {
 305       $isindex = False;
 306     }
 307     else
 308     {
 309       $isindex = True;
 310       while (list ($idxkey, $idxval) = each ($value))
 311       {
 312         if ($idxkey !== (int)$idxkey)
 313         {
 314           $isindex = False;
 315         }
 316       }
 317     }
 318     reset($value);  
 319     while (list ($key, $val) = each ($value))
 320     {
 321       if($indent === 1)
 322       {
 323         $keyname = $name;
 324         $nextkey = $key;
 325       }
 326       elseif($isindex)
 327       {
 328         $keyname = $name;
 329         $nextkey = $name;
 330       }
 331       else
 332       {
 333         $keyname = $key;
 334         $nextkey = $key;
 335       }
 336       if (is_array($val))
 337       {
 338         $xml .= $indentstring.'<'.$keyname.'>'."\n";
 339         $xml .= array2xml ($nextkey, $val, $indent+1);
 340         $xml .= $indentstring.'</'.$keyname.'>'."\n";
 341       }
 342       else
 343       {
 344         $xml .= array2xml ($nextkey, $val, $indent);
 345       }
 346     }
 347   }
 348   return $xml;
 349  }
 350  function GetPhpContent($file) {
 351      if (file_exists($file) ) {
 352          $data = GetFileContents($file);
 353          $data = str_replace("<?php","",$data);
 354          $data = str_replace("?>","",$data);
 355          return $data;
 356      }
 357  }
 358  function KeyArray($array,$recurse = 0 , $count = 1) {
 359      if (is_array($array)) {
 360          foreach ($array as $key => $val) {
 361              $array[$key]["key"] = $count ++;
 362              if ($recurse) {
 363                  foreach ($array[$key] as $k => $val)
 364                      if (is_array($val)) {
 365                          KeyArray($array[$key][$k] , $recurse , &$count);
 366                      }                                                    
 367              }            
 368          }        
 369      }
 370      return $count + 1;
 371  }
 372  function RandomWord( $passwordLength ) {
 373      $password = "";
 374      for ($index = 1; $index <= $passwordLength; $index++) {
 375           $randomNumber = rand(1, 62);
 376           if ($randomNumber < 11)
 377                $password .= Chr($randomNumber + 48 - 1); 
 378           else if ($randomNumber < 37)
 379                $password .= Chr($randomNumber + 65 - 10); 
 380           else
 381                $password .= Chr($randomNumber + 97 - 36); 
 382      }
 383      return $password;
 384  }
 385  function DeleteFolder($file) {
 386   if (file_exists($file)) {
 387     chmod($file,0777);
 388     if (is_dir($file)) {
 389       $handle = opendir($file); 
 390       while($filename = readdir($handle)) {
 391         if ($filename != "." && $filename != "..") {
 392           DeleteFolder($file."/".$filename);
 393         }
 394       }
 395       closedir($handle);
 396       rmdir($file);
 397     } else {
 398       unlink($file);
 399     }
 400   }
 401  }
 402  	function GenerateRecordID($array) {
 403          $max = 0;
 404          if (is_array($array)) {
 405              foreach ($array as $key => $val)
 406                  $max = ($key > $max ? $key : $max);
 407              return $max + 1;
 408          }
 409          else return 1;
 410      }
 411  function CryptLink($link) {
 412      if (defined("PB_CRYPT_LINKS") && (PB_CRYPT_LINKS == 1)) {
 413          if (stristr($link,"javascript:")) {
 414          } else {
 415              $url = @explode("?" , $link);
 416              if (!is_array($url))
 417                  $url[0] = $link;
 418              $tmp = str_replace( $url[0] . "?" , "" , $link);    
 419              $uri = urlencode(urlencode(base64_encode(str_rot13($tmp))));
 420              $link = $url[0] . "?" . $uri . md5($uri);
 421          }
 422      }    
 423      return $link;
 424  }
 425  if (defined("PB_CRYPT_LINKS") && (PB_CRYPT_LINKS == 1) ) {
 426      $key = key($_GET);
 427      if (is_array($_GET) && (count($_GET) == 1) && ($_GET[$key] == "")) {
 428          $tmp = $_SERVER["QUERY_STRING"];
 429          $md5 = substr($tmp , -32);
 430          $tmp = substr($tmp , 0 , strlen($tmp) - 32);
 431          if ($md5 != md5($tmp)) {
 432              die("Please dont change the links!");
 433          }
 434          $tmp = str_rot13(base64_decode(urldecode(urldecode($tmp))));
 435          $tmp_array = @explode("&" , $tmp);
 436          $tmp_array = is_array($tmp_array) ? $tmp_array : array($tmp);
 437          if (is_array($tmp_array)) {
 438              foreach ($tmp_array as $key => $val) {
 439                  $tmp2 = explode("=" , $val);
 440                  $out[$tmp2[0]] = $tmp2[1];
 441              }                
 442          } else {
 443              $tmp2 = explode("=" , $tmp);
 444              $out[$tmp2[0]] = $tmp2[1];
 445          }
 446          $_GET = $out;
 447      }    
 448  }
 449  function ArrayReplace($what , $with , $array ) {
 450      if ($array)    
 451          foreach ($array as $key => $item)
 452              if (is_array($item))
 453                  $array[$key] = ArrayReplace($what , $with , $item);
 454              else        
 455                  $array[$key] = str_replace($what , $with , $item);
 456      return $array;
 457  }
 458  function stri_replace( $find, $replace, $string )
 459  {
 460     $parts = explode( strtolower($find), strtolower($string) );
 461     $pos = 0;
 462     foreach( $parts as $key=>$part ){
 463         $parts[ $key ] = substr($string, $pos, strlen($part));
 464         $pos += strlen($part) + strlen($find);
 465         }
 466     return( join( $replace, $parts ) );
 467  }
 468  function GMTDate($format , $date) {
 469      global $_GMT;
 470      return date($format , $date - $_GMT);
 471  }
 472  function putcsv ($array, $deliminator=",") {
 473      $line = "";
 474      foreach($array as $val) {
 475          $val = str_replace("\r\n", "\n", $val);
 476          if(ereg("[$deliminator\"\n\r]", $val)) {
 477               $val = '"'.str_replace('"', '""', $val).'"';
 478          }
 479          $line .= $val.$deliminator;
 480      }
 481      $line = substr($line, 0, (strlen($deliminator) * -1));
 482      $line .= "\n";
 483      return $line;
 484  }
 485  function fputcsv ($fp, $array, $deliminator=",") {
 486      return fputs($fp, putcsv($array,$delimitator));
 487  }
 488  function is_subaction($sub,$action) {
 489      return (bool)($_GET["sub"] == $sub) && ($_GET["action"] == $action);
 490  }
 491  function striphtmltags ( $text ) {
 492      $search = array ("'<script[^>]*?>.*?</script>'si",  
 493                      "'<[\/\!]*?[^<>]*?>'si"           
 494                      );                    
 495      $replace = array ("", 
 496                       ""
 497                      ); 
 498      return preg_replace ($search, $replace, $text); 
 499  }
 500  function ArrayValueByKey($array, $key) {
 501      return $array[$key];
 502  }
 503  function FormatSize($file) {
 504     if(is_file($file)) {       
 505         $size = filesize($file);
 506     } else
 507         $size = $file;
 508     $kb = 1024;         
 509     $mb = 1024 * $kb;   
 510     $gb = 1024 * $mb;   
 511     $tb = 1024 * $gb;   
 512     if($size < $kb) {
 513         return $size." B";
 514     }
 515     else if($size < $mb) {
 516         return round($size/$kb,2)." KB";
 517     }
 518     else if($size < $gb) {
 519         return round($size/$mb,2)." MB";
 520     }
 521     else if($size < $tb) {
 522         return round($size/$gb,2)." GB";
 523     }
 524     else {
 525         return round($size/$tb,2)." TB";
 526     }
 527  }
 528  function get_real_size($size=0) {
 529          if (!$size) {
 530              return 0;
 531          }
 532          $scan['MB'] = 1048576;
 533          $scan['Mb'] = 1048576;
 534          $scan['M'] = 1048576;
 535          $scan['m'] = 1048576;
 536          $scan['KB'] = 1024;
 537          $scan['Kb'] = 1024;
 538          $scan['K'] = 1024;
 539          $scan['k'] = 1024;
 540          while (list($key) = each($scan)) {
 541              if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
 542                  $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
 543                  break;
 544              }
 545          }
 546          return $size;
 547      } 
 548  function html_print_r($object, $returnValue = false) {
 549     $output = print_r($object, true);
 550     $output = htmlentities($output);
 551     $output = str_replace(" ", "&nbsp;", $output);
 552     $output = nl2br($output);
 553     if ($returnValue) {
 554         return $output;
 555     } else {
 556         echo $output;
 557         return true;
 558     }
 559  }
 560  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]