41 function gpc_stripslashes($var)
43 if (get_magic_quotes_gpc()) {
45 stripslashes_array($var, TRUE);
47 $var = stripslashes($var);
64 function stripslashes_array(&$array, $strip_keys=FALSE)
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);
75 if ($strip_keys && $key != ($stripped_key = stripslashes($key))) {
76 $keys_to_replace[$key] = $stripped_key;
80 foreach ($keys_to_replace as $from => $to) {
81 $array[$to] = &$array[$from];
98 function htmlentities_array(&$array, $encode_keys=FALSE)
101 if ((!is_array($array) && !is_string($array)) || !is_bool($encode_keys)) {
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);
114 if ($encode_keys && $key != ($encoded_key = htmlentities($key, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET))) {
115 $keys_to_replace[$key] = $encoded_key;
119 foreach ($keys_to_replace as $from => $to) {
120 $array[$to] = &$array[$from];
121 unset($array[$from]);
139 function array_remove_element($v, &$a)
141 if (!is_array($a) || empty($a)) {
144 if (in_array($v,$a)) {
145 unset($a[array_search($v,$a)]);
165 function array_merge_multi($array1, $array2)
168 if (!is_array($array1) || !is_array($array2)) {
172 if (empty($array2))
return $array1;
174 return _array_merge_multi_recursive($array1, $array2);
191 function _array_merge_multi_recursive($array1, $array2)
193 foreach ($array2 as $key => $value) {
194 if (is_array($value)) {
195 if (!array_key_exists($key, $array1)) {
196 $array1[$key] = Array();
198 $array1[$key] = _array_merge_multi_recursive($array1[$key], $value);
200 if (!is_array($array1)) $array1 = Array();
201 $array1[$key] = $value;
249 function array_flatten($array, $prune_parents=FALSE, $return_unique_keys=TRUE)
251 if (!is_array($array) || empty($array)) {
255 $flattened_array = Array();
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();
263 $flattened_array[] = $key;
267 if ($return_unique_keys) {
268 $flattened_array += array_flatten($value, $prune_parents, $return_unique_keys);
270 foreach (array_flatten($value, $prune_parents, $return_unique_keys) as $flattened_value) {
271 $flattened_array[] = $flattened_value;
275 if ($return_unique_keys) {
276 $flattened_array[$key] = $value;
278 $flattened_array[] = $key;
283 return $flattened_array;
317 function dal_array_flatten($array, $column=
'')
319 if (!is_array($array) || empty($array)) {
323 $flattened_array = Array();
325 while (is_array($array) && !empty($array)) {
326 foreach ($array as $id => $data) {
327 if (is_array($data) && $id !== 0) {
330 if (empty($column)) {
332 if (isset($array[$id][0])) {
333 foreach ($array as $question_id => $type_data) {
334 $flattened_array[$question_id] = $type_data[0];
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];
351 return $flattened_array;
366 function random_password($length, $include_uppercase=FALSE, $include_numbers=FALSE)
368 if (!$length) $length = 8;
369 $letters =
'bcdfghjklnpqrstvwyz';
372 $numbers =
'2346789';
373 $upper_letters =
'DFGHLNQRT';
381 $uppercase_indexes = Array();
382 $chosen_default_uppercase_index = rand(0, $length-1);
384 if (($include_uppercase) && ($length > 1)) {
387 $uppercase_indexes[$chosen_default_uppercase_index] = 1;
391 for ($n=0; $n<$length; $n++) {
392 if ((rand(0,3) == 0) && ($num_added < ($length/2))) {
393 $uppercase_indexes[$n] = 1;
400 $number_indexes = Array();
401 if (($include_numbers) && ($length > 2)) {
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);
410 $number_indexes[$chosen_default_number_index] = 1;
414 for ($n=0; $n<$length; $n++) {
415 if ((rand(0,4) == 0) && ($num_added < ($length/3))) {
416 $number_indexes[$n] = 1;
423 while ($length > 0) {
427 $appended_characters = $letters[rand(0,strlen($letters)-1)];
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)];
437 $password .= $appended_characters;
456 function random_user_password($userinfo = Array())
459 require_once $config->config_file;
461 $password = generate_random_user_password();
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);
470 for($i=0; $i<$retry_max; $i++) {
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))) {
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)) {
486 if(trim($password) !== $password) $retry = TRUE;
490 $i == $retry_max - 1 ? $password =
'' : $password = generate_random_user_password();
509 function generate_random_user_password ()
512 require_once $config->config_file;
513 $capital_chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
514 $lowercase_chars =
'abcdefghijklmnopqrstuvwxyz';
515 $numers =
'0123456789';
516 $punc_chars =
',.?!';
522 if (defined(
'SQ_PASSWD_RULE_LENGTH') && SQ_PASSWD_RULE_LENGTH > -1) {
523 $total_length = SQ_PASSWD_RULE_LENGTH;
525 $left_length = $total_length;
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;
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;
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;
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;
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 .=
' ';
554 $password .= randString($left_length, $allowed_chars);
557 $password = str_shuffle($password);
574 function randString($length, $source)
577 for ($i =0; $i< $length; $i++) {
578 $int = rand(0, strlen($source) - 1);
579 $return .= $source[$int];
594 function arrayToXml($array, &$xml)
595 {
foreach($array as $key => $value) {
597 $key = preg_replace(
'/[^a-zA-Z_\-+0-9]+/',
'_', $key);
598 if(is_numeric($key)) $key =
'_'.$key;
601 if(is_array($value) && count($value) == 1 && isset($value[0]) && !is_array($value[0])) {
602 $xml->addChild(
"$key",
"$value[0]");
606 if(is_array($value)) {
607 $subnode = $xml->addChild(
"$key");
608 arrayToXml($value, $subnode);
611 $xml->addChild(
"$key",
"$value");
627 function ellipsisize($string, $length)
630 if (!is_string($string) || !is_int($length) ) {
635 if ($length < 0)
return '';
639 if (strlen($string) <= $length)
return $string;
641 $full_length = strlen((
string) $string);
645 for ($i = 0; $i < $full_length; $i++) {
646 if ($j == $length - 2) $chophere = $i;
648 if ($string[$i] ==
'<') {
654 if ($string[$i] ==
'>') $on = TRUE;
658 $string = substr($string, 0, $chophere).
'...';
661 $string = check_char_encoding($string);
683 function increment_name($name=
'', $spacer=
'')
686 if (!is_string($name) || !is_string($spacer)) {
690 $last_character = substr($name, -1);
692 if (preg_match(
'/[^0-9]/', $last_character)) {
694 return $name.$spacer.
'2';
696 }
else if (preg_match(
'/^[0-9]+$/', $name)) {
698 $next_number = intval($name) + 1;
699 return strval($next_number);
703 if (preg_match(
'/(.*[^0-9]{1})(0+[0-9]+)$/', $name, $number)) {
706 if (preg_match(
'/^0+$/', $number[2])) {
708 return $number[1].substr($number[2], 0, -1).strval(1);
710 }
else if (preg_match(
'/^(0+)([1-9]{1}[0-9]*)$/', $number[2], $number2)) {
712 $last_character = substr($number2[2], -1);
713 $next_number = intval($number2[2]) + 1;
715 if ($last_character ==
'9') {
716 return $number[1].substr($number2[1], 0, -1).strval($next_number);
718 return $number[1].$number2[1].strval($next_number);
722 }
else if (preg_match(
'/(.*[^0-9]{1})([0-9]{1,})$/', $name, $number)) {
724 $next_number = intval($number[2]) + 1;
725 return $number[1].strval($next_number);
742 function generate_security_key($key_len, $include_uppercase=FALSE, $include_numbers=FALSE)
744 $k = random_password($key_len, $include_uppercase, $include_numbers);
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)));
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')
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');
806 if (!($test_im = @imagecreate($w, $h))) {
807 trigger_error(
'Cannot Initialize new GD image stream', E_USER_WARNING);
811 if (!$final_im = @imagecreate(($w * $zoom), ($h * $zoom))) {
812 trigger_error(
'Cannot Initialize new GD image stream', E_USER_WARNING);
816 if (!$ttf_im = @imagecreate($ttf_width, $ttf_height)) {
817 trigger_error(
'Cannot Initialize new GD image stream', E_USER_WARNING);
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);
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);
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);
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);
851 $chars = preg_split(
'//', $word, -1, PREG_SPLIT_NO_EMPTY);
853 foreach ($chars as $char) {
855 imagestring ($test_im, rand(3, 5), $x_pos, $y_pos, $char, $text_colour);
858 $new_w = rand(10, (10 * $zoom));
859 $new_h = rand(15, (10 * $zoom));
867 trigger_error(
'You must select a true type font file to use', E_USER_WARNING);
872 $angle = rand($min_angle, $max_angle);
875 imagettftext($ttf_im, $font_size, $angle, $x_start, $font_size + 10, $text_colour, $font, $char);
877 $x_start += rand($min_dist, $max_dist);
879 imagecopyresized($final_im, $test_im, ($x_pos * $zoom), ($y_pos * $zoom), $x_pos, $y_pos, $new_w, $new_h, 10, 15);
887 if ($use_arc && $use_font) {
888 imagesetthickness($ttf_im, 3);
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);
895 if ( rand(0,100) % 2 == 0 ) {
897 $ypos = $ttf_height / 2 - rand(5, 15);
898 $xpos += rand(5, 10);
900 $start = rand(180, 246);
901 $ypos = $ttf_height / 2 + rand(5, 15);
904 $end = $start + rand(75, 110);
905 imagearc($ttf_im, $xpos, $ypos, $arc_width, $arc_height, $start, $end, $arc_color_final);
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);
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);
919 $start = rand(200, 250);
920 $ypos = $ttf_height / 2 + rand(5, 15);
923 $end = $start + rand(75, 100);
924 imagearc($ttf_im, $ttf_width * .75, $ypos, $arc_width, $arc_height, $start, $end, $arc_color_final);
931 imagerectangle($ttf_im, 0, 0, imagesx($ttf_im) -1, imagesy($ttf_im) -1, $border_colour);
934 imagedestroy($ttf_im);
937 imagerectangle($final_im, 0, 0, ($w * $zoom) -1, ($h * $zoom) -1, $border_colour);
943 imagedestroy($test_im);
944 imagedestroy($final_im);
960 function add_ordinal_suffix($num)
963 if (!is_int($num))
return '';
966 if ($num <= 0)
return '';
969 if ((10 < ($num % 100)) && (($num % 100) < 20)) {
1001 function add_reverse_ordinal_suffix($num)
1003 return ($num == 1) ?
'' : add_ordinal_suffix($num);
1020 function check_char_encoding($string, $string_encoding = NULL)
1022 $string_encoding = $string_encoding ? $string_encoding : SQ_CONF_DEFAULT_CHARACTER_SET;
1024 if (!extension_loaded(
'mbstring') || !SQ_CONF_DEFAULT_CHARACTER_SET) {
1028 return mb_convert_encoding($string, $string_encoding, $string_encoding);
1043 function _cmp_dsort($str1, $str2)
1045 return strlen($str2) > strlen($str1);
1059 function _cmp_asort($str1, $str2)
1061 return strlen($str1) > strlen($str2);
1078 function array_sort_by_length(&$array, $sort_order=TRUE, $value_sort=TRUE)
1080 $cmp_function = $sort_order ?
'_cmp_asort' :
'_cmp_dsort';
1081 $sort_function = $value_sort ?
'uasort' :
'uksort';
1083 return $sort_function($array, $cmp_function);
1094 function get_unique_token()
1096 if(isset($_SESSION[
'SQ_SYSTEM_NONCE_SECURITY_TOKEN'])) {
1097 return $_SESSION[
'SQ_SYSTEM_NONCE_SECURITY_TOKEN'];
1101 $moreEntropy = TRUE;
1102 $token = sha1(uniqid($prefix, $moreEntropy));
1104 $_SESSION[
'SQ_SYSTEM_NONCE_SECURITY_TOKEN'] = $token;
1119 function xml_to_array($xml)
1121 return @_xml_to_array_helper(
new SimpleXmlIterator($xml, null));
1131 function _xml_to_array_helper($iter)
1133 foreach($iter as $key=>$val) {
1134 $arr[$key][] = ($iter->hasChildren()) ? call_user_func (__FUNCTION__, $val) : strval($val);
1148 function json_decode_array($data)
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);
1155 $output = json_decode($data, TRUE);