13if ( !function_exists(
'dbg_error_log') ) {
29 function dbg_error_log() {
31 $args = func_get_args();
33 if (func_num_args() > 1) {
34 $component = array_shift($args);
36 $component =
'UNKNOWN';
38 if ( substr( $component, 0, 3) ==
"LOG" ) {
41 $component = substr($component,4);
43 else if ( $component ==
"ERROR" ) {
46 else if ( isset($c->dbg[
"ALL"]) ) {
49 else if ( !isset($c->dbg[strtolower($component)]) )
return;
52 if ( isset($c->dbg[strtolower($component)]) && $c->dbg[strtolower($component)] === 0 )
return;
55 if ( isset($c->dbg_filter[
"remoteIP"]) && !in_array($_SERVER[
'REMOTE_ADDR'], $c->dbg_filter[
"remoteIP"]) )
return;
56 if ( isset($c->dbg_filter[
"authenticatedUser"]) ) {
57 if ( !isset($session->username) )
return;
58 if ( !in_array($session->username, $c->dbg_filter[
"authenticatedUser"]) )
return;
61 if ( count($args) >= 2 ) {
62 $format = array_shift($args);
63 @error_log( $c->sysabbr.
": $type: $component:". vsprintf( $format, $args ) );
66 @error_log( $c->sysabbr.
": $type: $component:" . $args[0]);
72if ( !function_exists(
'fatal') ) {
75 $args = func_get_args();
76 $argc = func_num_args();
78 $format = array_shift($args);
83 @error_log( $c->sysabbr.
": FATAL: $component:". vsprintf( $format, $args ) );
85 @error_log(
"================= Stack Trace ===================" );
87 $trace = array_reverse(debug_backtrace());
89 foreach( $trace AS $k => $v ) {
90 @error_log( sprintf(
" ===> %s[%d] calls %s%s%s()",
93 (isset($v[
'class'])?$v[
'class']:
''),
94 (isset($v[
'type'])?$v[
'type']:
''),
95 (isset($v[
'function'])?$v[
'function']:
'')
104if ( !function_exists(
'trace_bug') ) {
108 function trace_bug() {
110 $args = func_get_args();
111 $argc = func_num_args();
113 $format = array_shift($args);
118 @error_log( $c->sysabbr.
": BUG: $component:". vsprintf( $format, $args ) );
120 @error_log(
"================= Stack Trace ===================" );
122 $trace = array_reverse(debug_backtrace());
124 foreach( $trace AS $k => $v ) {
125 @error_log( sprintf(
" ===> %s[%d] calls %s%s%s()",
128 (isset($v[
'class'])?$v[
'class']:
''),
129 (isset($v[
'type'])?$v[
'type']:
''),
130 (isset($v[
'function'])?$v[
'function']:
'')
137if ( !function_exists(
'apache_request_headers') ) {
143 function apache_request_headers() {
144 foreach($_SERVER as $key=>$value) {
145 if (substr($key,0,5)=="HTTP_") {
146 $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
157if ( !function_exists(
'dbg_log_array') ) {
166 function dbg_log_array( $component, $name, $arr, $recursive =
false ) {
167 if ( !isset($arr) || (gettype($arr) !=
'array' && gettype($arr) !=
'object') ) {
168 dbg_error_log( $component,
"%s: array is not set, or is not an array!", $name);
171 foreach ($arr as $key => $value) {
172 dbg_error_log( $component,
"%s: >>%s<< = >>%s<<", $name, $key,
173 (gettype($value) ==
'array' || gettype($value) ==
'object' ? gettype($value) : $value) );
174 if ( $recursive && (gettype($value) ==
'array' || (gettype($value) ==
'object' &&
"$key" !=
'self' &&
"$key" !=
'parent') ) ) {
175 dbg_log_array( $component,
"$name".
"[$key]", $value, $recursive );
183if ( !function_exists(
"session_simple_md5") ) {
190 function session_simple_md5( $instr ) {
192 if ( isset($c->dbg[
'password']) ) dbg_error_log(
"Login",
"Making plain MD5: instr=$instr, md5($instr)=".md5($instr) );
193 return (
'*MD5*'. md5($instr) );
199if ( !function_exists(
"session_salted_md5") ) {
209 function session_salted_md5( $instr, $salt =
"" ) {
210 if ( $salt ==
"" ) $salt = substr( md5(rand(100000,999999)), 2, 8);
212 if ( isset($c->dbg[
'password']) ) dbg_error_log(
"Login",
"Making salted MD5: salt=$salt, instr=$instr, md5($salt$instr)=".md5($salt . $instr) );
213 return ( sprintf(
"*%s*%s", $salt, md5($salt . $instr) ) );
219if ( !function_exists(
"session_salted_sha1") ) {
233 function session_salted_sha1( $instr, $salt =
"" ) {
234 if ( $salt ==
"" ) $salt = substr( str_replace(
'*',
'',base64_encode(sha1(rand(100000,9999999),
true))), 2, 9);
236 if ( isset($c->dbg[
'password']) ) dbg_error_log(
"Login",
"Making salted SHA1: salt=$salt, instr=$instr, encoded($instr$salt)=".base64_encode(sha1($instr . $salt,
true).$salt) );
237 return ( sprintf(
"*%s*{SSHA}%s", $salt, base64_encode(sha1($instr.$salt,
true) . $salt ) ) );
242if ( !function_exists(
"session_validate_password") ) {
250 function session_validate_password( $they_sent, $we_have ) {
252 if ( preg_match(
'/^\*\*.+$/', $we_have ) ) {
254 return (
"**$they_sent" == $we_have );
257 if ( isset($c->wp_includes) && substring($we_have,0,1) ==
'$' ) {
259 @require_once($c->wp_includes .
'/class-phpass.php');
261 if ( class_exists(
'PasswordHash') ) {
262 $wp_hasher =
new PasswordHash(8,
true);
263 return $wp_hasher->CheckPassword($password, $hash);
267 if ( preg_match(
'/^\*(.+)\*{[A-Z]+}.+$/', $we_have, $regs ) ) {
268 if ( function_exists(
"session_salted_sha1") ) {
271 $sha1_sent = session_salted_sha1( $they_sent, $salt ) ;
272 return ( $sha1_sent == $we_have );
275 dbg_error_log(
"ERROR",
"Password is salted SHA-1 but you are using PHP4!" );
279<title>Salted SHA1 Password format not supported with PHP4</title>
282<h1>Salted SHA1 Password format not supported with PHP4</h1>
283<p>At some point you have used PHP5 to
set the password
for this user and now you are
284 using PHP4. You will need to assign a
new password to
this user
using PHP4, or ensure
285 you use PHP5 everywhere (recommended).</p>
286<p>AWL has now switched to
using salted SHA-1 passwords by preference in a format
287 compatible with OpenLDAP.</p>
295 if ( preg_match(
'/^\*MD5\*.+$/', $we_have, $regs ) ) {
297 $md5_sent = session_simple_md5( $they_sent ) ;
298 return ( $md5_sent == $we_have );
300 else if ( preg_match(
'/^\*(.+)\*.+$/', $we_have, $regs ) ) {
303 $md5_sent = session_salted_md5( $they_sent, $salt ) ;
304 return ( $md5_sent == $we_have );
315if ( !function_exists(
"replace_uri_params") ) {
323 function replace_uri_params( $uri, $replacements ) {
325 foreach( $replacements AS $param => $new_value ) {
326 $rxp = preg_replace(
'/([\[\]])/',
'\\\\$1', $param );
327 $regex =
"/([&?])($rxp)=([^&]+)/";
328 dbg_error_log(
"core",
"Looking for [%s] to replace with [%s] regex is %s and searching [%s]", $param, $new_value, $regex, $replaced );
329 if ( preg_match( $regex, $replaced ) )
330 $replaced = preg_replace( $regex,
"\$1$param=$new_value", $replaced);
332 $replaced .=
"&$param=$new_value";
334 if ( ! preg_match(
'/\?/', $replaced ) ) {
335 $replaced = preg_replace(
"/&(.+)$/",
"?\$1", $replaced);
337 $replaced = str_replace(
"&",
"--AmPeRsAnD--", $replaced);
338 $replaced = str_replace(
"&",
"&", $replaced);
339 $replaced = str_replace(
"--AmPeRsAnD--",
"&", $replaced);
340 dbg_error_log(
"core",
"URI <<$uri>> morphed to <<$replaced>>");
346if ( !function_exists(
"uuid") ) {
380 return sprintf(
'%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
381 mt_rand(0, 65535), mt_rand(0, 65535),
384 bindec(substr_replace(sprintf(
'%016b', mt_rand(0, 65535)),
'01', 6, 2)),
388 mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
393if ( !function_exists(
"translate") ) {
394 require(
"Translation.php");
397 if ( !function_exists(
"clone") && version_compare(phpversion(),
'5.0') < 0) {
403 eval(
'function clone($object) { return $object; }' );
406if ( !function_exists(
"quoted_printable_encode") ) {
412 function quoted_printable_encode($string) {
413 return preg_replace(
'/[^\r\n]{73}[^=\r\n]{2}/',
"$0=\r\n", str_replace(
"%",
"=",str_replace(
"%20",
" ",rawurlencode($string))));
418if ( !function_exists(
"check_by_regex") ) {
424 function check_by_regex( $val, $regex ) {
425 if ( is_null($val) )
return null;
427 case 'int': $regex =
'#^\d+$#';
break;
429 if ( is_array($val) ) {
430 foreach( $val AS $k => $v ) {
431 $val[$k] = check_by_regex($v,$regex);
434 else if ( ! is_object($val) ) {
435 if ( preg_match( $regex, $val, $matches) ) {
447if ( !function_exists(
"param_to_global") ) {
458 function param_to_global( ) {
459 $args = func_get_args();
461 $varname = array_shift($args);
462 $GLOBALS[$varname] =
null;
465 $argc = func_num_args();
467 $match_regex = array_shift($args);
471 foreach( $args AS $k => $name ) {
472 if ( isset($_POST[$name]) ) {
473 $result = $_POST[$name];
476 else if ( isset($_GET[$name]) ) {
477 $result = $_GET[$name];
481 if ( !isset($result) )
return null;
483 if ( isset($match_regex) ) {
484 $result = check_by_regex( $result, $match_regex );
487 $GLOBALS[$varname] = $result;
493if ( !function_exists(
"awl_get_fields") ) {
497 $_AWL_field_cache = array();
504 function awl_get_fields( $tablename ) {
505 global $_AWL_field_cache;
507 if ( !isset($_AWL_field_cache[$tablename]) ) {
508 dbg_error_log(
"core",
":awl_get_fields: Loading fields for table '$tablename'" );
510 $db = $qry->GetConnection();
511 $qry->SetSQL($db->GetFields($tablename));
514 while( $row = $qry->Fetch() ) {
515 $fields[$row->fieldname] = $row->typename . ($row->precision >= 0 ? sprintf(
'(%d)',$row->precision) :
'');
517 $_AWL_field_cache[$tablename] = $fields;
519 return $_AWL_field_cache[$tablename];
524if ( !function_exists(
"force_utf8") ) {
525 function define_byte_mappings() {
526 global $byte_map, $nibble_good_chars;
528 # Needed for using Grant McLean's byte mappings code
529 $ascii_char =
'[\x00-\x7F]';
530 $cont_byte =
'[\x80-\xBF]';
532 $utf8_2 =
'[\xC0-\xDF]' . $cont_byte;
533 $utf8_3 =
'[\xE0-\xEF]' . $cont_byte .
'{2}';
534 $utf8_4 =
'[\xF0-\xF7]' . $cont_byte .
'{3}';
535 $utf8_5 =
'[\xF8-\xFB]' . $cont_byte .
'{4}';
537 $nibble_good_chars =
"/^($ascii_char+|$utf8_2|$utf8_3|$utf8_4|$utf8_5)(.*)$/s";
541 "\x80" =>
"\xE2\x82\xAC", # EURO SIGN
542 "\x82" =>
"\xE2\x80\x9A", # SINGLE LOW-9 QUOTATION MARK
543 "\x83" =>
"\xC6\x92", # LATIN SMALL LETTER F WITH HOOK
544 "\x84" =>
"\xE2\x80\x9E", # DOUBLE LOW-9 QUOTATION MARK
545 "\x85" =>
"\xE2\x80\xA6", # HORIZONTAL ELLIPSIS
546 "\x86" =>
"\xE2\x80\xA0", # DAGGER
547 "\x87" =>
"\xE2\x80\xA1", # DOUBLE DAGGER
548 "\x88" =>
"\xCB\x86", # MODIFIER LETTER CIRCUMFLEX ACCENT
549 "\x89" =>
"\xE2\x80\xB0", # PER MILLE SIGN
550 "\x8A" =>
"\xC5\xA0", # LATIN CAPITAL LETTER S WITH CARON
551 "\x8B" =>
"\xE2\x80\xB9", # SINGLE LEFT-POINTING ANGLE QUOTATION MARK
552 "\x8C" =>
"\xC5\x92", # LATIN CAPITAL LIGATURE OE
553 "\x8E" =>
"\xC5\xBD", # LATIN CAPITAL LETTER Z WITH CARON
554 "\x91" =>
"\xE2\x80\x98", # LEFT SINGLE QUOTATION MARK
555 "\x92" =>
"\xE2\x80\x99", # RIGHT SINGLE QUOTATION MARK
556 "\x93" =>
"\xE2\x80\x9C", # LEFT DOUBLE QUOTATION MARK
557 "\x94" =>
"\xE2\x80\x9D", # RIGHT DOUBLE QUOTATION MARK
558 "\x95" =>
"\xE2\x80\xA2", # BULLET
559 "\x96" =>
"\xE2\x80\x93", # EN DASH
560 "\x97" =>
"\xE2\x80\x94", # EM DASH
561 "\x98" =>
"\xCB\x9C", # SMALL TILDE
562 "\x99" =>
"\xE2\x84\xA2", # TRADE MARK SIGN
563 "\x9A" =>
"\xC5\xA1", # LATIN SMALL LETTER S WITH CARON
564 "\x9B" =>
"\xE2\x80\xBA", # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
565 "\x9C" =>
"\xC5\x93", # LATIN SMALL LIGATURE OE
566 "\x9E" =>
"\xC5\xBE", # LATIN SMALL LETTER Z WITH CARON
567 "\x9F" =>
"\xC5\xB8", # LATIN CAPITAL LETTER Y WITH DIAERESIS
570 for( $i=160; $i < 256; $i++ ) {
572 $byte_map[$ch] = iconv(
'ISO-8859-1',
'UTF-8', $ch);
575 define_byte_mappings();
577 function force_utf8( $input ) {
578 global $byte_map, $nibble_good_chars;
583 while( $input !=
'' ) {
584 if ( preg_match( $nibble_good_chars, $input, $matches ) ) {
585 $output .= $matches[1];
589 preg_match(
'/^(.)(.*)$/s', $input, $matches );
592 if ( isset($byte_map[$char]) ) {
593 $output .= $byte_map[$char];
596 # Must be valid UTF8 already
608$timezone_identifiers_list_cache = timezone_identifiers_list();
609$timezone_identifiers_list_cache = isset($timezone_identifiers_list_cache) ? $timezone_identifiers_list_cache : [];
614function olson_from_tzstring( $tzstring ) {
615 global $c, $timezone_identifiers_list_cache;
617 if ( in_array($tzstring,$timezone_identifiers_list_cache) )
return $tzstring;
618 if ( preg_match(
'{((Antarctica|America|Africa|Atlantic|Asia|Australia|Indian|Europe|Pacific)/(([^/]+)/)?[^/]+)$}', $tzstring, $matches ) ) {
622 switch( $tzstring ) {
623 case 'New Zealand Standard Time':
case 'New Zealand Daylight Time':
624 return 'Pacific/Auckland';
626 case 'Central Standard Time':
case 'Central Daylight Time':
case 'US/Central':
627 return 'America/Chicago';
629 case 'Eastern Standard Time':
case 'Eastern Daylight Time':
case 'US/Eastern':
630 case '(UTC-05:00) Eastern Time (US & Canada)':
631 return 'America/New_York';
633 case 'Pacific Standard Time':
case 'Pacific Daylight Time':
case 'US/Pacific':
634 return 'America/Los_Angeles';
636 case 'Mountain Standard Time':
case 'Mountain Daylight Time':
case 'US/Mountain':
case 'Mountain Time':
637 return 'America/Denver';
641 case '(GMT-07.00) Arizona':
642 return 'America/Phoenix';
645 if ( isset($c->timezone_translations) && is_array($c->timezone_translations)
646 && !empty($c->timezone_translations[$tzstring]) )
647 return $c->timezone_translations[$tzstring];
652if ( !function_exists(
"deprecated") ) {
653 function deprecated( $method ) {
655 if ( isset($c->dbg[
'ALL']) || isset($c->dbg[
'deprecated']) ) {
656 $stack = debug_backtrace();
658 if ( preg_match(
'{/inc/iCalendar.php$}', $stack[0][
'file'] ) && $stack[0][
'line'] > __LINE__ )
return;
659 @error_log( sprintf( $c->sysabbr.
':DEPRECATED: Call to deprecated method "%s"', $method));
660 foreach( $stack AS $k => $v ) {
661 @error_log( sprintf( $c->sysabbr.
': ==> called from line %4d of %s', $v[
'line'], $v[
'file']));
668if ( !function_exists(
"gzdecode") ) {
669 function gzdecode( $instring ) {
671 if ( !isset($c->use_pipe_gunzip) || $c->use_pipe_gunzip ) {
672 $descriptorspec = array(
673 0 => array(
"pipe",
"r"),
674 1 => array(
"pipe",
"w"),
675 2 => array(
"file",
"/dev/null",
"a")
677 $process = proc_open(
'gunzip',$descriptorspec, $pipes);
678 if ( is_resource($process) ) {
679 fwrite($pipes[0],$instring);
682 $outstring = stream_get_contents($pipes[1]);
685 proc_close($process);
691 $g=tempnam(
'./',
'gz');
692 file_put_contents($g,$instring);
705function awl_version() {
707$c->awl_library_version = 0.64;
708 return $c->awl_library_version;