snippets / Xrogaan / 

All Xrogaan's snippets (3)

  1. Tick system

    This object make a tick system that could make operate some jobs at same time. This is like a cron job, but more simpler.

      1 <?php
    2
    3 class tick {
    4 private $ticks = array();
    5 private $ok;
    6
    7 static public $instance = false;
    8
    9 public static function GetInstance() {
    10 if (!self::$instance) {
    11 self::$instance = new tick();
    12 }
    13 return self::$instance;
    14 }
    15
    16 private function __construct() {
    17 $this->ok = true;
    18 }
    19
    20 /**
    21 * Adding a job to the tick list $tickname
    22 *
    23 * @param string $tickname
    24 * @param string $jobname
    25 * @param string $function Function to call
    26 * @param array $params Parameters send to the $function
    27 * @param boolean $temporary Make the job temporary (will be deleted on next execution of the tick)
    28 * @param object $object Reference to an object if $function is a method
    29 * @return boolean
    30 */
    31 public function addJob($tickname,$jobname,$function,$params,$temporary=0,$object=null) {
    32 if (isset($this->ticks[$tickname])) {
    33 if (!is_null($object)) {
    34 $this->ticks[$tickname]['jobs'][] = array(
    35 'jname' => $jobname,
    36 'function' => $function,
    37 'params' => $params,
    38 'temporary' => $temporary,
    39 'objectRef' => &$object
    40 );
    41 } else {
    42 $this->ticks[$tickname]['jobs'][] = array(
    43 'jname' => $jobname,
    44 'function' => $function,
    45 'params' => $params,
    46 'temporary' => $temporary
    47 );
    48 }
    49 return true;
    50 }
    51 return false;
    52 }
    53
    54 /**
    55 * Create a tick if not exist
    56 *
    57 * @param string $name
    58 * @param int $timer Number of seconds before the next tick
    59 * @return boolean
    60 */
    61 public function setTick($name,$timer) {
    62 if (!isset($this->ticks[$name])) {
    63 $this->ticks[$name] = array(
    64 'timer' => $timer,
    65 'reference' => time(),
    66 'tickOn' => strtotime("+$timer seconds"),
    67 'jobs' => array()
    68 );
    69 return true;
    70 }
    71 return false;
    72 }
    73
    74 /**
    75 * Delete a tick
    76 *
    77 * @param string $name
    78 * @return boolean
    79 */
    80 public function delTick($name) {
    81 if (isset($this->ticks[$name])) {
    82 unset($this->ticks[$name]);
    83 return true;
    84 }
    85 return false;
    86 }
    87
    88 public function doAllTicks() {
    89 $curtime = time();
    90
    91 foreach($this->ticks as $name => $options) {
    92
    93 if ($options['tickOn']<=$curtime) {
    94 echo $options['tickOn']," - $curtime\n";
    95
    96 foreach ($options['jobs'] as $functions) {
    97
    98 if (!$functions['temporary']) {
    99 $jlist[] = $functions;
    100 }
    101
    102 if (!$this->ircmain->ok) {
    103 continue;
    104 }
    105
    106 switch (isset($functions['objectRef'])) {
    107 case true:
    108 if (!is_object($functions['objectRef'])) {
    109 throw new Exception('The objectRef for tick '.$name.' at job '.$functions['jname'].' is not an object.',0);
    110 }
    111 //print_r($functions);break;
    112 call_user_func_array(array($functions['objectRef'],$functions['function']),$functions['params']);
    113 break;
    114
    115 case false:
    116 call_user_func_array($functions['function'],$functions['params']);
    117 break;
    118 }
    119
    120 }
    121
    122 $options['jobs'] = $jlist;
    123
    124 $this->ticks[$name] = array(
    125 'timer' => $options['timer'],
    126 'reference' => time(),
    127 'tickOn' => strtotime("+{$options['timer']} seconds"),
    128 'jobs' => $options['jobs']
    129 );
    130
    131 echo "newTickOn {$this->ticks[$name]['tickOn']}\n";
    132
    133 }
    134
    135 }
    136
    137 return true;
    138
    139 }
    140 }
    141
    142 ?>
    first posted by Xrogaan to php php tick cron job ... saved by 1 person ... 0 comments ... 11 months, 1 week
  2. Generate random password

    A fully password generator.

     1 <?php
    2
    3 /**
    4 * Generate random password
    5 *
    6 * @param integer $length Length of the generated password
    7 * @param boolean $allow_uppercase
    8 * @param boolean $allow_lowercase
    9 * @param boolean $allow_numbers
    10 * @param boolean $allow_special
    11 * @param boolean $fix_similar If true, check if string contains mistakeable chars, add if accepted
    12 * @param string $valid_charset
    13 * @return string
    14 */
    15 function rpassword($length = 8, $allow_uppercase = 1, $allow_lowercase = 1, $allow_numbers = 1, $allow_special = 0, $fix_similar = 0, $valid_charset = "") {
    16 // Create a list of usable chars based upon the parameters
    17 if (!$valid_charset) {
    18 if ($allow_uppercase) $valid_charset .= 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
    19 if ($allow_lowercase) $valid_charset .= 'abcdefghijklmnopqrstuvxyz';
    20 if ($allow_numbers) $valid_charset .= '0123456789';
    21 if ($allow_special) $valid_charset .= '!#$%&()*+-./;<=>@\_';
    22 }
    23 // Find the charset length
    24 $charset_length = strlen($valid_charset);
    25 // If no chars is allowed, return false
    26 if ($charset_length == 0) return false;
    27 // Initialize the password and loop till we have all
    28 $password = "";
    29 while(strlen($password) < $length) {
    30 // Pull out a random char
    31 $char = $valid_charset[mt_rand(0, ($charset_length-1))];
    32
    33 if (($fix_similar && !strpos('O01lI5S', $char)) || !$fix_similar) $password .= $char;
    34 }
    35
    36 return $password;
    37 }
  3. Fonctions pratique pour debuguage

    backtrace() peut-être utilisé un peut partout pour trouver ce qui ne va pas dans son code. Très utile lorsque l'on travaille en orienté objet.

     1 <?php
    2 // print_r avec joli formatage
    3 function xdump($v) { echo '<pre>'; print_r($v); echo '</pre>'; }
    4
    5 // Même chose mais avec var_dump
    6 function xvdump($v) { echo '<pre>'; var_dump($v); echo '</pre>'; }
    7
    8 // affiche un backtrace et meurt
    9 function backtrace($err='') {
    10 echo '<div id="err">';
    11 if ($err != '') echo "<strong>Erreur : $err</strong><br/>\n";
    12 echo "<strong>Traceback </strong>:<pre>";
    13 foreach( debug_backtrace() as $v ) xdump( $v );
    14 echo "</pre></div>";
    15 die;
    16 }
    17 ?>
    first posted by Xrogaan to php pratique debug ... saved by 3 persons ... 0 comments ... 1 year, 2 months
showing 10, 25, 50 items per pages

Pages : 1