Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
general.inc
1 <?php
41 function gpc_stripslashes($var)
42 {
43  if (get_magic_quotes_gpc()) {
44  if (is_array($var)) {
45  stripslashes_array($var, TRUE);
46  } else {
47  $var = stripslashes($var);
48  }
49  }
50  return $var;
51 
52 }//end gpc_stripslashes()
53 
54 
64 function stripslashes_array(&$array, $strip_keys=FALSE)
65 {
66  if (is_string($array)) return stripslashes($array);
67  $keys_to_replace = Array();
68  foreach ($array as $key => $value) {
69  if (is_string($value)) {
70  $array[$key] = stripslashes($value);
71  } else if (is_array($value)) {
72  stripslashes_array($array[$key], $strip_keys);
73  }
74 
75  if ($strip_keys && $key != ($stripped_key = stripslashes($key))) {
76  $keys_to_replace[$key] = $stripped_key;
77  }
78  }
79  // now replace any of the keys that needed strip slashing
80  foreach ($keys_to_replace as $from => $to) {
81  $array[$to] = &$array[$from];
82  unset($array[$from]);
83  }
84  return $array;
85 
86 }//end stripslashes_array()
87 
88 
98 function htmlentities_array(&$array, $encode_keys=FALSE)
99 {
100  // type verification
101  if ((!is_array($array) && !is_string($array)) || !is_bool($encode_keys)) {
102  return Array();
103  }
104 
105  if (is_string($array)) return htmlentities($array, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
106  $keys_to_replace = Array();
107  foreach ($array as $key => $value) {
108  if (is_string($value)) {
109  $array[$key] = htmlentities($value, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
110  } else if (is_array($value)) {
111  htmlentities_array($array[$key], $encode_keys);
112  }
113 
114  if ($encode_keys && $key != ($encoded_key = htmlentities($key, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET))) {
115  $keys_to_replace[$key] = $encoded_key;
116  }
117  }
118  // now replace any of the keys that needed encoding
119  foreach ($keys_to_replace as $from => $to) {
120  $array[$to] = &$array[$from];
121  unset($array[$from]);
122  }
123  return $array;
124 
125 }//end htmlentities_array()
126 
127 
139 function array_remove_element($v, &$a)
140 {
141  if (!is_array($a) || empty($a)) {
142  return FALSE;
143  }
144  if (in_array($v,$a)) {
145  unset($a[array_search($v,$a)]);
146  return TRUE;
147  }
148 
149 }//end array_remove_element()
150 
151 
165 function array_merge_multi($array1, $array2)
166 {
167  // type verification
168  if (!is_array($array1) || !is_array($array2)) {
169  return Array();
170  }
171 
172  if (empty($array2)) return $array1;
173 
174  return _array_merge_multi_recursive($array1, $array2);
175 
176 }//end array_merge_multi()
177 
178 
191 function _array_merge_multi_recursive($array1, $array2)
192 {
193  foreach ($array2 as $key => $value) {
194  if (is_array($value)) {
195  if (!array_key_exists($key, $array1)) {
196  $array1[$key] = Array();
197  }
198  $array1[$key] = _array_merge_multi_recursive($array1[$key], $value);
199  } else {
200  if (!is_array($array1)) $array1 = Array();
201  $array1[$key] = $value;
202  }
203  }
204 
205  return $array1;
206 
207 }//end _array_merge_multi_recursive()
208 
209 
249 function array_flatten($array, $prune_parents=FALSE, $return_unique_keys=TRUE)
250 {
251  if (!is_array($array) || empty($array)) {
252  return Array();
253  }
254 
255  $flattened_array = Array();
256 
257  foreach ($array as $key => $value) {
258  if (is_array($value)) {
259  if (!$prune_parents) {
260  if ($return_unique_keys) {
261  $flattened_array[$key] = Array();
262  } else {
263  $flattened_array[] = $key;
264  }
265  }
266 
267  if ($return_unique_keys) {
268  $flattened_array += array_flatten($value, $prune_parents, $return_unique_keys);
269  } else {
270  foreach (array_flatten($value, $prune_parents, $return_unique_keys) as $flattened_value) {
271  $flattened_array[] = $flattened_value;
272  }
273  }
274  } else {
275  if ($return_unique_keys) {
276  $flattened_array[$key] = $value;
277  } else {
278  $flattened_array[] = $key;
279  }
280  }
281  }
282 
283  return $flattened_array;
284 
285 }//end array_flatten()
286 
287 
317 function dal_array_flatten($array, $column='')
318 {
319  if (!is_array($array) || empty($array)) {
320  return Array();
321  }
322 
323  $flattened_array = Array();
324  // drill down until we find a 0 index which means we may have have found a valid row
325  while (is_array($array) && !empty($array)) {
326  foreach ($array as $id => $data) {
327  if (is_array($data) && $id !== 0) {
328  // 1 level lookahead to see if we are about to find a valid row
329  // if we do then start iterating to capture all other rows
330  if (empty($column)) {
331  // if the column name is not specified return the array found there instead
332  if (isset($array[$id][0])) {
333  foreach ($array as $question_id => $type_data) {
334  $flattened_array[$question_id] = $type_data[0];
335  }
336  break(2);
337  }
338  } else {
339  if (isset($array[$id][0]) && isset($array[$id][0][$column])) {
340  foreach ($array as $question_id => $type_data) {
341  $flattened_array[$question_id] = $type_data[0][$column];
342  }
343  break(2);
344  }
345  }
346  }
347  $array = $data;
348  }
349  }
350 
351  return $flattened_array;
352 
353 }//end dal_array_flatten()
354 
355 
366 function random_password($length, $include_uppercase=FALSE, $include_numbers=FALSE)
367 {
368  if (!$length) $length = 8;
369  $letters = 'bcdfghjklnpqrstvwyz';
370 
371  // Please note that certain letters and numbers are missing due to being represented in a similar manner to lowercase chars in some fonts
372  $numbers = '2346789';
373  $upper_letters = 'DFGHLNQRT';
374 
375  $password = '';
376 
377  //As of PHP 4.2.0, there is no need to seed the random number generator, it is done automatically by rand()
378  //global $RANDOM_PASSWORD_SEED;
379 
380  // If we are including uppercase characters, ensure at least one is present
381  $uppercase_indexes = Array();
382  $chosen_default_uppercase_index = rand(0, $length-1);
383  //$seed = $RANDOM_PASSWORD_SEED + $length + ((int)(100000 * (double)microtime())) + time();
384  if (($include_uppercase) && ($length > 1)) {
385  //srand($seed);
386 
387  $uppercase_indexes[$chosen_default_uppercase_index] = 1;
388  $num_added = 1;
389 
390  // Make between 1 character and 1/2 of the string an uppercase letter
391  for ($n=0; $n<$length; $n++) {
392  if ((rand(0,3) == 0) && ($num_added < ($length/2))) {
393  $uppercase_indexes[$n] = 1;
394  $num_added++;
395  }
396  }
397  }
398 
399  // If we are including numbers, ensure at least one is present
400  $number_indexes = Array();
401  if (($include_numbers) && ($length > 2)) {
402  //srand($seed);
403 
404  // Ensure that we don't place our initial inserted number over the top of an uppercase letter
405  $chosen_default_number_index = rand(0, $length-1);
406  while (isset($uppercase_indexes[$chosen_default_number_index])) {
407  $chosen_default_number_index = rand(0, $length-1);
408  }
409 
410  $number_indexes[$chosen_default_number_index] = 1;
411  $num_added = 1;
412 
413  // Make between 1 character and 1/3 of the string a number
414  for ($n=0; $n<$length; $n++) {
415  if ((rand(0,4) == 0) && ($num_added < ($length/3))) {
416  $number_indexes[$n] = 1;
417  $num_added++;
418  }
419  }
420  }
421 
422  $current_index = 0;
423  while ($length > 0) {
424  //$RANDOM_PASSWORD_SEED += $length;
425  //$seed = $RANDOM_PASSWORD_SEED + $length + ((int)(100000 * (double)microtime())) + time();
426  //srand($seed);
427  $appended_characters = $letters[rand(0,strlen($letters)-1)];
428 
429  // Assign either an uppercase letter or a number if we are destined to do so
430  if (($include_uppercase) && (isset($uppercase_indexes[$current_index]))) {
431  $appended_characters = $upper_letters[rand(0,strlen($upper_letters)-1)];
432  } else if (($include_numbers) && (isset($number_indexes[$current_index]))) {
433  $appended_characters = $numbers[rand(0,strlen($numbers)-1)];
434  }
435  $current_index++;
436 
437  $password .= $appended_characters;
438  $length -= 1;
439  }//end while
440  //$RANDOM_PASSWORD_SEED++;
441 
442  return $password;
443 
444 }//end random_password()
445 
446 
456 function random_user_password($userinfo = Array())
457 {
458  $config = new Password_Rules_Config();
459  require_once $config->config_file;
460 
461  $password = generate_random_user_password();
462 
463  // check if this password contains blacklisted word
464  $retry_max = 99;
465  $blacklist = SQ_PASSWD_RULE_BLACK_LIST;
466  $blacklist = str_replace("\r\n","\n", $blacklist);
467  $blacklist = str_replace("\r","\n", $blacklist);
468  $blacklist = explode("\n", $blacklist);
469 
470  for($i=0; $i<$retry_max; $i++) {
471  $retry = FALSE;
472  if (!empty($blacklist)){
473  foreach ($blacklist as $word) {
474  if ((!SQ_PASSWD_RULE_BLACK_LIST_EXACT && preg_match('/'.$word.'/', $password)) || (SQ_PASSWD_RULE_BLACK_LIST_EXACT && (strcmp($password, $word) == 0))) {
475  $retry = TRUE;
476  }
477  }
478  }
479  if(!empty($userinfo) && defined('SQ_PASSWD_RULE_DISALLOW_USER_INFO') && SQ_PASSWD_RULE_DISALLOW_USER_INFO == 0) {
480  foreach ($userinfo as $component) {
481  if (!empty($component) && preg_match('/.*'.$component.'.*/i', $password)) {
482  $retry = TRUE;
483  }
484  }
485  }
486  if(trim($password) !== $password) $retry = TRUE;
487  if($retry) {
488  // regenerate a new password
489  // if it's hopeless, just return empty password
490  $i == $retry_max - 1 ? $password = '' : $password = generate_random_user_password();
491  }
492  else {
493  break;
494  }
495  }
496 
497  return $password;
498 }//end random_user_password()
499 
500 
509 function generate_random_user_password ()
510 {
511  $config = new Password_Rules_Config();
512  require_once $config->config_file;
513  $capital_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
514  $lowercase_chars = 'abcdefghijklmnopqrstuvwxyz';
515  $numers = '0123456789';
516  $punc_chars = ',.?!';
517  $allowed_chars = '';
518  $total_length = 12;
519  $password = '';
520 
521 
522  if (defined('SQ_PASSWD_RULE_LENGTH') && SQ_PASSWD_RULE_LENGTH > -1) {
523  $total_length = SQ_PASSWD_RULE_LENGTH;
524  }
525  $left_length = $total_length;
526  // add minimum required chars
527  if (defined('SQ_PASSWD_RULE_NUM_CAPS') && SQ_PASSWD_RULE_NUM_CAPS > -1) {
528  $password .= randString(SQ_PASSWD_RULE_NUM_CAPS, $capital_chars);
529  $left_length -= SQ_PASSWD_RULE_NUM_CAPS;
530  if(SQ_PASSWD_RULE_NUM_CAPS === 0) $allowed_chars .= $capital_chars;
531  }
532  if (defined('SQ_PASSWD_RULE_NUM_LOWER') && SQ_PASSWD_RULE_NUM_LOWER > -1) {
533  $password .= randString(SQ_PASSWD_RULE_NUM_LOWER, $lowercase_chars);
534  $left_length -= SQ_PASSWD_RULE_NUM_LOWER;
535  if(SQ_PASSWD_RULE_NUM_LOWER === 0) $allowed_chars .= $lowercase_chars;
536  }
537  if (defined('SQ_PASSWD_RULE_NUM_INT') && SQ_PASSWD_RULE_NUM_INT > -1) {
538  $password .= randString(SQ_PASSWD_RULE_NUM_INT, $numers);
539  $left_length -= SQ_PASSWD_RULE_NUM_INT;
540  if(SQ_PASSWD_RULE_NUM_INT === 0) $allowed_chars .= $numers;
541  }
542  if (defined('SQ_PASSWD_RULE_NUM_PUNC') && SQ_PASSWD_RULE_NUM_PUNC > -1) {
543  $password .= randString(SQ_PASSWD_RULE_NUM_PUNC, $punc_chars);
544  $left_length -= SQ_PASSWD_RULE_NUM_PUNC;
545  if(SQ_PASSWD_RULE_NUM_PUNC === 0) $allowed_chars .= $punc_chars;
546  }
547  if (defined('SQ_PASSWD_RULE_NUM_SPC') && SQ_PASSWD_RULE_NUM_SPC > -1) {
548  $password .= randString(SQ_PASSWD_RULE_NUM_SPC, ' ');
549  $left_length -= SQ_PASSWD_RULE_NUM_SPC;
550  if(SQ_PASSWD_RULE_NUM_SPC === 0) $allowed_chars .= ' ';
551  }
552 
553  // add rest from allowed chars
554  $password .= randString($left_length, $allowed_chars);
555 
556  // shuffle
557  $password = str_shuffle($password);
558 
559 
560  return $password;
561 
562 }//end generate_random_user_password()
563 
564 
574  function randString($length, $source)
575  {
576  $return = '';
577  for ($i =0; $i< $length; $i++) {
578  $int = rand(0, strlen($source) - 1);
579  $return .= $source[$int];
580  }
581  return $return;
582  }//end randString
583 
584 
594  function arrayToXml($array, &$xml)
595  { foreach($array as $key => $value) {
596  // remove characters for valid XML
597  $key = preg_replace('/[^a-zA-Z_\-+0-9]+/', '_', $key);
598  if(is_numeric($key)) $key = '_'.$key;
599 
600  // single element no need of index
601  if(is_array($value) && count($value) == 1 && isset($value[0]) && !is_array($value[0])) {
602  $xml->addChild("$key","$value[0]");
603  continue;
604  }
605 
606  if(is_array($value)) {
607  $subnode = $xml->addChild("$key");
608  arrayToXml($value, $subnode);
609  }
610  else {
611  $xml->addChild("$key","$value");
612  }
613  }
614  }//end arrayToXml()
615 
616 
627 function ellipsisize($string, $length)
628 {
629  // type verification
630  if (!is_string($string) || !is_int($length) ) {
631  return '';
632  }
633 
634  // boundary check #1 - length needs to be at least zero
635  if ($length < 0) return '';
636 
637  // boundary check #2 - if the string isn't long enough to contract, return
638  // it unedited
639  if (strlen($string) <= $length) return $string;
640 
641  $full_length = strlen((string) $string);
642  $length += 2; // add 2 because an elipsis is 3 chars long
643  $on = TRUE;
644  $j = 0;
645  for ($i = 0; $i < $full_length; $i++) {
646  if ($j == $length - 2) $chophere = $i;
647  if ($on) {
648  if ($string[$i] == '<') {
649  $on = FALSE;
650  } else {
651  $j++;
652  }
653  } else {
654  if ($string[$i] == '>') $on = TRUE;
655  }
656  }
657  if ($length < $j) {
658  $string = substr($string, 0, $chophere).'...';
659  }
660 
661  $string = check_char_encoding($string);
662 
663  return $string;
664 
665 }//end ellipsisize()
666 
667 
683 function increment_name($name='', $spacer='')
684 {
685  // type verification
686  if (!is_string($name) || !is_string($spacer)) {
687  return '';
688  }
689 
690  $last_character = substr($name, -1);
691 
692  if (preg_match('/[^0-9]/', $last_character)) {
693  // name ends with character
694  return $name.$spacer.'2';
695 
696  } else if (preg_match('/^[0-9]+$/', $name)) {
697  // name with only digits
698  $next_number = intval($name) + 1;
699  return strval($next_number);
700 
701  } else {
702  // name ends with non-digit character
703  if (preg_match('/(.*[^0-9]{1})(0+[0-9]+)$/', $name, $number)) {
704  // name ends with digits with preceeding zeros, like 001, 0002
705 
706  if (preg_match('/^0+$/', $number[2])) {
707  // all zeros, eg. hello_00000
708  return $number[1].substr($number[2], 0, -1).strval(1);
709 
710  } else if (preg_match('/^(0+)([1-9]{1}[0-9]*)$/', $number[2], $number2)) {
711  // some zeros and number eg. hello_01, hello_0003429
712  $last_character = substr($number2[2], -1);
713  $next_number = intval($number2[2]) + 1;
714 
715  if ($last_character == '9') {
716  return $number[1].substr($number2[1], 0, -1).strval($next_number);
717  } else {
718  return $number[1].$number2[1].strval($next_number);
719  }
720  }
721 
722  } else if (preg_match('/(.*[^0-9]{1})([0-9]{1,})$/', $name, $number)) {
723  // name ends with digits without preceeding zeros, like 100, 223
724  $next_number = intval($number[2]) + 1;
725  return $number[1].strval($next_number);
726  }
727  }
728 
729 }//end increment_name()
730 
731 
742 function generate_security_key($key_len, $include_uppercase=FALSE, $include_numbers=FALSE)
743 {
744  $k = random_password($key_len, $include_uppercase, $include_numbers);
745  if ($key_len>10) {
746  $gl=Array('YmxhaXI=','Z3JlZw==','bWFyYw==','ZG9t');
747  $g=base64_decode($gl[rand(0,(count($gl)-1))]);$pos=rand(1,($key_len-strlen($g)));
748  $k=substr($k,0,$pos).$g.substr($k,($pos+strlen($g)));
749  }
750 
751  return $k;
752 
753 }//end generate_security_key()
754 
755 
787 function security_key_image($key_len, $word, $w, $h, $bg='FFFFFF', $text='000000', $border='000000', $zoom=1, $use_colours=FALSE, $use_font=FALSE, $font='', $font_size=20, $min_angle=10, $max_angle=20, $x_start=20, $min_dist=20, $max_dist=20, $ttf_width=200, $ttf_height=36, $use_arc=FALSE, $arc_color='D20A0A')
788 {
789  if (!empty($word)) {
790  // Reset colours to defaults if we are not using them
791  if (!$use_colours) {
792  $bg = 'FFFFFF'; // White background
793  $text = '000000'; // Black text
794  $border = '000000'; // Black border
795  }
796 
797  // cache control for this image
798  header('Cache-Control:');
799  header('Pragma: cache');
800  header('Expires: '.gmdate('D, d M Y H:i:s',time() -1000).' GMT');
801  header('Content-type: image/png');
802 
803  // create an image we will use to print the characters on and then
804  // another image $zoom times bigger where we will print the larger
805  // zoomed text for easier reading
806  if (!($test_im = @imagecreate($w, $h))) {
807  trigger_error('Cannot Initialize new GD image stream', E_USER_WARNING);
808  return FALSE;
809  }
810 
811  if (!$final_im = @imagecreate(($w * $zoom), ($h * $zoom))) {
812  trigger_error('Cannot Initialize new GD image stream', E_USER_WARNING);
813  return FALSE;
814  }
815 
816  if (!$ttf_im = @imagecreate($ttf_width, $ttf_height)) {
817  trigger_error('Cannot Initialize new GD image stream', E_USER_WARNING);
818  return FALSE;
819  }
820 
821  // create some colours we will use in the image
822  // NOTE: The first colour allocated is used for the background
823  $r = hexdec(substr($bg, 0, 2));
824  $g = hexdec(substr($bg, 2, 2));
825  $b = hexdec(substr($bg, 4, 2));
826  $bg_colour = imagecolorallocate($test_im, $r, $g, $b);
827  $bg_colour = imagecolorallocate($final_im, $r, $g, $b);
828  $bg_colour = imagecolorallocate($ttf_im, $r, $g, $b);
829 
830  $r = hexdec(substr($text, 0, 2));
831  $g = hexdec(substr($text, 2, 2));
832  $b = hexdec(substr($text, 4, 2));
833  $text_colour = imagecolorallocate($test_im, $r, $g, $b);
834  $text_colour = imagecolorallocate($final_im, $r, $g, $b);
835  $text_colour = imagecolorallocate($ttf_im, $r, $g, $b);
836 
837  $r = hexdec(substr($border, 0, 2));
838  $g = hexdec(substr($border, 2, 2));
839  $b = hexdec(substr($border, 4, 2));
840  $border_colour = imagecolorallocate($test_im, $r, $g, $b);
841  $border_colour = imagecolorallocate($final_im, $r, $g, $b);
842  $border_colour = imagecolorallocate($ttf_im, $r, $g, $b);
843 
844  $r = hexdec(substr($arc_color, 0, 2));
845  $g = hexdec(substr($arc_color, 2, 2));
846  $b = hexdec(substr($arc_color, 4, 2));
847  $arc_color_final = imagecolorallocate($ttf_im, $r, $g, $b);
848 
849  $x_pos = 5;
850  $y_pos = 5;
851  $chars = preg_split('//', $word, -1, PREG_SPLIT_NO_EMPTY);
852 
853  foreach ($chars as $char) {
854  // print the character in either type 3, 4 or 5 inbuilt font
855  imagestring ($test_im, rand(3, 5), $x_pos, $y_pos, $char, $text_colour);
856 
857  // work out the new dimensions of the character after zooming
858  $new_w = rand(10, (10 * $zoom));
859  $new_h = rand(15, (10 * $zoom));
860 
861  // copy the character we printed above and resize it onto the final
862  // image to zoom the characet randomly in width and height based on $zoom
863  if ($use_font) {
864 
865  // Trigger an error if no font file is set
866  if ($font == '') {
867  trigger_error('You must select a true type font file to use', E_USER_WARNING);
868  return FALSE;
869  }
870 
871  // Set some vars for our TTF image
872  $angle = rand($min_angle, $max_angle);
873 
874  // Create the image
875  imagettftext($ttf_im, $font_size, $angle, $x_start, $font_size + 10, $text_colour, $font, $char);
876  // increase the x position
877  $x_start += rand($min_dist, $max_dist);
878  } else {
879  imagecopyresized($final_im, $test_im, ($x_pos * $zoom), ($y_pos * $zoom), $x_pos, $y_pos, $new_w, $new_h, 10, 15);
880  // increase the x position
881  $x_pos += 10;
882  }
883 
884  }//end foreach
885 
886  // Check to see if we are using arcs
887  if ($use_arc && $use_font) {
888  imagesetthickness($ttf_im, 3);
889 
890  // Create the first arc on our image
891  $xpos = 5 + ($font_size * 2) + rand(-5, 5);
892  $arc_width = $ttf_width / 4.66 + rand(3, 10);
893  $arc_height = $font_size * 2.14 - rand(3, 10);
894 
895  if ( rand(0,100) % 2 == 0 ) {
896  $start = rand(0,66);
897  $ypos = $ttf_height / 2 - rand(5, 15);
898  $xpos += rand(5, 10);
899  } else {
900  $start = rand(180, 246);
901  $ypos = $ttf_height / 2 + rand(5, 15);
902  }
903 
904  $end = $start + rand(75, 110);
905  imagearc($ttf_im, $xpos, $ypos, $arc_width, $arc_height, $start, $end, $arc_color_final);
906  //end first arc
907 
908  // Create the second arc on our image
909  $arc_color = imagecolorallocate($ttf_im, 0, 0, 255);
910  $xpos = 5 + ($font_size * 2) + rand(-5, 5);
911  $arc_width = $ttf_width / 4.66 + rand(3, 10);
912  $arc_height = $font_size * 2.14 - rand(3, 10);
913 
914  if ( rand(1,75) % 2 == 0 ) {
915  $start = rand(45,110);
916  $ypos = $ttf_height / 2 - rand(5, 15);
917  $xpos += rand(5, 15);
918  } else {
919  $start = rand(200, 250);
920  $ypos = $ttf_height / 2 + rand(5, 15);
921  }
922 
923  $end = $start + rand(75, 100);
924  imagearc($ttf_im, $ttf_width * .75, $ypos, $arc_width, $arc_height, $start, $end, $arc_color_final);
925  //end second arc
926 
927  }//end if
928 
929 
930  if ($use_font) {
931  imagerectangle($ttf_im, 0, 0, imagesx($ttf_im) -1, imagesy($ttf_im) -1, $border_colour);
932  // Create our image as a png then destroy
933  imagepng($ttf_im);
934  imagedestroy($ttf_im);
935  } else {
936  // draw a border around the outside
937  imagerectangle($final_im, 0, 0, ($w * $zoom) -1, ($h * $zoom) -1, $border_colour);
938 
939  // output the image
940  imagepng($final_im);
941 
942  // cleanup
943  imagedestroy($test_im);
944  imagedestroy($final_im);
945  }
946 
947  }//end if empty($word)
948 
949 }//end security_key_image()
950 
951 
960 function add_ordinal_suffix($num)
961 {
962  // type verification
963  if (!is_int($num)) return '';
964 
965  // boundary check
966  if ($num <= 0) return '';
967 
968 
969  if ((10 < ($num % 100)) && (($num % 100) < 20)) {
970  return $num.'th';
971  } else {
972  switch ($num % 10) {
973  case 1:
974  return $num.'st';
975 
976  case 2:
977  return $num.'nd';
978 
979  case 3:
980  return $num.'rd';
981 
982  default:
983  return $num.'th';
984  }
985  }
986 
987 }//end add_ordinal_suffix()
988 
989 
1001 function add_reverse_ordinal_suffix($num)
1002 {
1003  return ($num == 1) ? '' : add_ordinal_suffix($num);
1004 
1005 }//end add_reverse_ordinal_suffix()
1006 
1007 
1020 function check_char_encoding($string, $string_encoding = NULL)
1021 {
1022  $string_encoding = $string_encoding ? $string_encoding : SQ_CONF_DEFAULT_CHARACTER_SET;
1023 
1024  if (!extension_loaded('mbstring') || !SQ_CONF_DEFAULT_CHARACTER_SET) {
1025  return $string;
1026  }
1027 
1028  return mb_convert_encoding($string, $string_encoding, $string_encoding);
1029 
1030 }//end check_char_encoding()
1031 
1032 
1043 function _cmp_dsort($str1, $str2)
1044 {
1045  return strlen($str2) > strlen($str1);
1046 }
1047 
1048 
1059 function _cmp_asort($str1, $str2)
1060 {
1061  return strlen($str1) > strlen($str2);
1062 }
1063 
1078 function array_sort_by_length(&$array, $sort_order=TRUE, $value_sort=TRUE)
1079 {
1080  $cmp_function = $sort_order ? '_cmp_asort' : '_cmp_dsort';
1081  $sort_function = $value_sort ? 'uasort' : 'uksort';
1082 
1083  return $sort_function($array, $cmp_function);
1084 }
1085 
1086 
1087 
1094 function get_unique_token()
1095  {
1096  if(isset($_SESSION['SQ_SYSTEM_NONCE_SECURITY_TOKEN'])) {
1097  return $_SESSION['SQ_SYSTEM_NONCE_SECURITY_TOKEN'];
1098  }
1099 
1100  $prefix = '';
1101  $moreEntropy = TRUE;
1102  $token = sha1(uniqid($prefix, $moreEntropy));
1103 
1104  $_SESSION['SQ_SYSTEM_NONCE_SECURITY_TOKEN'] = $token;
1105 
1106  return $token;
1107 
1108  }//end get_unique_token()
1109 
1110 
1119 function xml_to_array($xml)
1120 {
1121  return @_xml_to_array_helper(new SimpleXmlIterator($xml, null));
1122 
1123 }//end xml_to_array()
1124 
1125 
1131 function _xml_to_array_helper($iter)
1132 {
1133  foreach($iter as $key=>$val) {
1134  $arr[$key][] = ($iter->hasChildren()) ? call_user_func (__FUNCTION__, $val) : strval($val);
1135  }
1136 
1137  return $arr;
1138 
1139 }//end xml_to_array_helper()
1140 
1141 
1148 function json_decode_array($data)
1149 {
1150  if (!function_exists('json_decode')) {
1151  require_once 'Services/JSON.php';
1152  $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
1153  $output = $json->decode($data);
1154  } else {
1155  $output = json_decode($data, TRUE);
1156  }//end else
1157  return $output;
1158 }//end json_decode_array()
1159 
1160 ?>