06.28 php saved
kiger
Tags add more
toolkit  
Note
kiger did not leave a note
  1. <?php
  2. class Toolkit extends Object
  3. {
  4.     /**
  5.     * Returns a singleton instance.
  6.     *
  7.     * @return Configure instance
  8.     * @access public
  9.     */
  10.     function &getInstance() {
  11.         static $instance = array();
  12.         if (!$instance) {
  13.             $instance =& new Toolkit();
  14.         }
  15.         return $instance;
  16.     }
  17.  
  18.     /**
  19.     * put your comment there...
  20.     *
  21.     * @param mixed $length
  22.     */
  23.     function hash($string, $type='sha256', $salt=true)
  24.     {
  25.         if ($salt === true)
  26.         {
  27.             $salt = Configure::read('OS.salt');
  28.  
  29.             $string = $salt . $string;
  30.         }
  31.  
  32.         if ($type === 'sha256')
  33.         {
  34.             if (function_exists('hash'))
  35.             {
  36.                 return hash('SHA256', $string);
  37.  
  38.             }
  39.             elseif (function_exists('mhash'))
  40.             {
  41.                 return bin2hex(mhash(MHASH_SHA256, $string));
  42.             }
  43.  
  44.             $type = 'sha1';
  45.         }
  46.  
  47.         if ($type === 'sha1')
  48.         {
  49.             if (function_exists('hash'))
  50.             {
  51.                 return hash('sha1', $string);
  52.             }
  53.             elseif (function_exists('sha1'))
  54.             {
  55.                 return sha1($string);
  56.             }
  57.  
  58.             $type = 'md5';
  59.         }
  60.  
  61.         if ($type === 'md5')
  62.         {
  63.             if (function_exists('hash'))
  64.             {
  65.                 return hash('md5', $string);
  66.             }
  67.             elseif (function_exists('md5'))
  68.             {
  69.                 return md5($string);
  70.             }
  71.         }
  72.     }
  73.  
  74.     /**
  75.     * put your comment there...
  76.     *
  77.     * @param mixed $length
  78.     */
  79.     function randomString($length = 32)
  80.     {
  81.         $charset = 'abcdfghijklmnopqrstvwxyz0123456789';
  82.  
  83.         $key = '';
  84.  
  85.         $charsetLength = strlen($charset)-1;
  86.  
  87.         for ($i=0; $i<$length; $i++)
  88.         {
  89.             $key .= $charset[(mt_rand(0,$charsetLength))];
  90.         }
  91.  
  92.         return $key;
  93.     }
  94.  
  95.     /**
  96.     * put your comment there...
  97.     *
  98.     * @param mixed $length
  99.     */
  100.     function randomNumber($length = 32, $security = 'high')
  101.     {
  102.         if ($security === 'high')
  103.         {
  104.             $charset = '0123456789';
  105.  
  106.             $key = '';
  107.  
  108.             for ($i=0; $i<$length; $i++)
  109.             {
  110.                 $key .= mt_rand(0,9);
  111.             }
  112.         }
  113.         else
  114.         if ($security === 'low')
  115.         {
  116.             $max = substr('9999999999', 0, strlen(mt_getrandmax())-1);
  117.  
  118.             $maxLength = strlen($max);
  119.  
  120.             $count = ($length>$maxLength) ? ceil($length/$maxLength) : 1;
  121.  
  122.             $key = '';
  123.  
  124.             for ($i=1; $i<=$count; $i++)
  125.             {
  126.                 $key .= sprintf('%0' . $maxLength . 's', mt_rand(0,$max));
  127.             }
  128.  
  129.             $key = substr($key, 0, $length);
  130.         }
  131.  
  132.         return $key;
  133.     }
  134.  
  135.     /**
  136.     * put your comment there...
  137.     *
  138.     * @param mixed $length
  139.     */
  140.     function randomPassword($length = 10)
  141.     {
  142.         $password = '';
  143.  
  144.         $vowels = array('a', 'e', 'i', 'o', 'u');
  145.  
  146.         $cons = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'tr',
  147.         'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl');
  148.  
  149.         for($i = 0; $i < $length; $i++)
  150.         {
  151.             $password .= $cons[mt_rand(0, 31)] . $vowels[mt_rand(0, 4)];
  152.         }
  153.  
  154.         return substr($password, 0, $length);
  155.     }
  156.  
  157.     /**
  158.     * Converts a file size in bytes to a readable size.
  159.     *
  160.     * @param mixed $size
  161.     * @param mixed $retstring
  162.     * @return string
  163.     */
  164.     function toReadableSize($size, $retstring = null)
  165.     {
  166.         // adapted from code at http://aidanlister.com/repos/v/function.size_readable.php
  167.  
  168.         $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  169.  
  170.         if ($retstring === null) { $retstring = '%01.2f %s'; }
  171.  
  172.         $lastsizestring = end($sizes);
  173.  
  174.         foreach ($sizes as $sizestring)
  175.         {
  176.                if ($size < 1024) { break; }
  177.  
  178.                if ($sizestring != $lastsizestring) { $size /= 1024; }
  179.         }
  180.  
  181.         if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
  182.  
  183.         return sprintf($retstring, $size, $sizestring);
  184.     }
  185. }
  186. ?>
Parsed in 0.217 seconds, using GeSHi 1.0.7.14

Modify this Paste