1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691:
<?php
if ( !function_exists('dbg_error_log') ) {
function dbg_error_log() {
global $c;
$args = func_get_args();
$type = "DBG";
$component = array_shift($args);
if ( substr( $component, 0, 3) == "LOG" ) {
$type = 'LOG';
$component = substr($component,4);
}
else if ( $component == "ERROR" ) {
$type = "***";
}
else if ( isset($c->dbg["ALL"]) ) {
$type = "ALL";
}
else if ( !isset($c->dbg[strtolower($component)]) ) return;
$argc = func_num_args();
if ( 2 <= $argc ) {
$format = array_shift($args);
}
else {
$format = "%s";
}
@error_log( $c->sysabbr.": $type: $component:". vsprintf( $format, $args ) );
}
}
if ( !function_exists('fatal') ) {
function fatal() {
global $c;
$args = func_get_args();
$argc = func_num_args();
if ( 2 <= $argc ) {
$format = array_shift($args);
}
else {
$format = "%s";
}
@error_log( $c->sysabbr.": FATAL: $component:". vsprintf( $format, $args ) );
@error_log( "================= Stack Trace ===================" );
$trace = array_reverse(debug_backtrace());
array_pop($trace);
foreach( $trace AS $k => $v ) {
@error_log( sprintf(" ===> %s[%d] calls %s%s%s()",
$v['file'],
$v['line'],
(isset($v['class'])?$v['class']:''),
(isset($v['type'])?$v['type']:''),
(isset($v['function'])?$v['function']:'')
));
}
echo "Fatal Error";
exit();
}
}
if ( !function_exists('trace_bug') ) {
function trace_bug() {
global $c;
$args = func_get_args();
$argc = func_num_args();
if ( 2 <= $argc ) {
$format = array_shift($args);
}
else {
$format = "%s";
}
@error_log( $c->sysabbr.": BUG: $component:". vsprintf( $format, $args ) );
@error_log( "================= Stack Trace ===================" );
$trace = array_reverse(debug_backtrace());
array_pop($trace);
foreach( $trace AS $k => $v ) {
@error_log( sprintf(" ===> %s[%d] calls %s%s%s()",
$v['file'],
$v['line'],
(isset($v['class'])?$v['class']:''),
(isset($v['type'])?$v['type']:''),
(isset($v['function'])?$v['function']:'')
));
}
}
}
if ( !function_exists('apache_request_headers') ) {
eval('
function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
$out[$key]=$value;
}
}
return $out;
}
');
}
if ( !function_exists('dbg_log_array') ) {
function dbg_log_array( $component, $name, $arr, $recursive = false ) {
if ( !isset($arr) || (gettype($arr) != 'array' && gettype($arr) != 'object') ) {
dbg_error_log( $component, "%s: array is not set, or is not an array!", $name);
return;
}
foreach ($arr as $key => $value) {
dbg_error_log( $component, "%s: >>%s<< = >>%s<<", $name, $key,
(gettype($value) == 'array' || gettype($value) == 'object' ? gettype($value) : $value) );
if ( $recursive && (gettype($value) == 'array' || (gettype($value) == 'object' && "$key" != 'self' && "$key" != 'parent') ) ) {
dbg_log_array( $component, "$name"."[$key]", $value, $recursive );
}
}
}
}
if ( !function_exists("session_simple_md5") ) {
function session_simple_md5( $instr ) {
global $c;
if ( isset($c->dbg['password']) ) dbg_error_log( "Login", "Making plain MD5: instr=$instr, md5($instr)=".md5($instr) );
return ( '*MD5*'. md5($instr) );
}
}
if ( !function_exists("session_salted_md5") ) {
function session_salted_md5( $instr, $salt = "" ) {
if ( $salt == "" ) $salt = substr( md5(rand(100000,999999)), 2, 8);
global $c;
if ( isset($c->dbg['password']) ) dbg_error_log( "Login", "Making salted MD5: salt=$salt, instr=$instr, md5($salt$instr)=".md5($salt . $instr) );
return ( sprintf("*%s*%s", $salt, md5($salt . $instr) ) );
}
}
if ( !function_exists("session_salted_sha1") ) {
function session_salted_sha1( $instr, $salt = "" ) {
if ( $salt == "" ) $salt = substr( str_replace('*','',base64_encode(sha1(rand(100000,9999999),true))), 2, 9);
global $c;
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) );
return ( sprintf("*%s*{SSHA}%s", $salt, base64_encode(sha1($instr.$salt, true) . $salt ) ) );
}
}
if ( !function_exists("session_validate_password") ) {
function session_validate_password( $they_sent, $we_have ) {
global $c;
if ( preg_match('/^\*\*.+$/', $we_have ) ) {
return ( "**$they_sent" == $we_have );
}
if ( isset($c->wp_includes) && substring($we_have,0,1) == '$' ) {
@require_once($c->wp_includes .'/class-phpass.php');
if ( class_exists('PasswordHash') ) {
$wp_hasher = new PasswordHash(8, true);
return $wp_hasher->CheckPassword($password, $hash);
}
}
if ( preg_match('/^\*(.+)\*{[A-Z]+}.+$/', $we_have, $regs ) ) {
if ( function_exists("session_salted_sha1") ) {
$salt = $regs[1];
$sha1_sent = session_salted_sha1( $they_sent, $salt ) ;
return ( $sha1_sent == $we_have );
}
else {
dbg_error_log( "ERROR", "Password is salted SHA-1 but you are using PHP4!" );
echo <<<EOERRMSG
<html>
<head>
<title>Salted SHA1 Password format not supported with PHP4</title>
</head>
<body>
<h1>Salted SHA1 Password format not supported with PHP4</h1>
<p>At some point you have used PHP5 to set the password for this user and now you are
using PHP4. You will need to assign a new password to this user using PHP4, or ensure
you use PHP5 everywhere (recommended).</p>
<p>AWL has now switched to using salted SHA-1 passwords by preference in a format
compatible with OpenLDAP.</p>
</body>
</html>
EOERRMSG;
exit;
}
}
if ( preg_match('/^\*MD5\*.+$/', $we_have, $regs ) ) {
$md5_sent = session_simple_md5( $they_sent ) ;
return ( $md5_sent == $we_have );
}
else if ( preg_match('/^\*(.+)\*.+$/', $we_have, $regs ) ) {
$salt = $regs[1];
$md5_sent = session_salted_md5( $they_sent, $salt ) ;
return ( $md5_sent == $we_have );
}
return false;
}
}
if ( !function_exists("replace_uri_params") ) {
function replace_uri_params( $uri, $replacements ) {
$replaced = $uri;
foreach( $replacements AS $param => $new_value ) {
$rxp = preg_replace( '/([\[\]])/', '\\\\$1', $param );
$regex = "/([&?])($rxp)=([^&]+)/";
dbg_error_log("core", "Looking for [%s] to replace with [%s] regex is %s and searching [%s]", $param, $new_value, $regex, $replaced );
if ( preg_match( $regex, $replaced ) )
$replaced = preg_replace( $regex, "\$1$param=$new_value", $replaced);
else
$replaced .= "&$param=$new_value";
}
if ( ! preg_match( '/\?/', $replaced ) ) {
$replaced = preg_replace("/&(.+)$/", "?\$1", $replaced);
}
$replaced = str_replace("&", "--AmPeRsAnD--", $replaced);
$replaced = str_replace("&", "&", $replaced);
$replaced = str_replace("--AmPeRsAnD--", "&", $replaced);
dbg_error_log("core", "URI <<$uri>> morphed to <<$replaced>>");
return $replaced;
}
}
if ( !function_exists("uuid") ) {
function uuid() {
return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 4095),
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
);
}
}
if ( !function_exists("translate") ) {
require("Translation.php");
}
if ( !function_exists("clone") && version_compare(phpversion(), '5.0') < 0) {
eval( 'function clone($object) { return $object; }' );
}
if ( !function_exists("quoted_printable_encode") ) {
function quoted_printable_encode($string) {
return preg_replace('/[^\r\n]{73}[^=\r\n]{2}/', "$0=\r\n", str_replace("%","=",str_replace("%20"," ",rawurlencode($string))));
}
}
if ( !function_exists("check_by_regex") ) {
function check_by_regex( $val, $regex ) {
if ( is_null($val) ) return null;
switch( $regex ) {
case 'int': $regex = '#^\d+$#'; break;
}
if ( is_array($val) ) {
foreach( $val AS $k => $v ) {
$val[$k] = check_by_regex($v,$regex);
}
}
else if ( ! is_object($val) ) {
if ( preg_match( $regex, $val, $matches) ) {
$val = $matches[0];
}
else {
$val = '';
}
}
return $val;
}
}
if ( !function_exists("param_to_global") ) {
function param_to_global( ) {
$args = func_get_args();
$varname = array_shift($args);
$GLOBALS[$varname] = null;
$match_regex = null;
$argc = func_num_args();
if ( $argc > 1 ) {
$match_regex = array_shift($args);
}
$args[] = $varname;
foreach( $args AS $k => $name ) {
if ( isset($_POST[$name]) ) {
$result = $_POST[$name];
break;
}
else if ( isset($_GET[$name]) ) {
$result = $_GET[$name];
break;
}
}
if ( !isset($result) ) return null;
if ( isset($match_regex) ) {
$result = check_by_regex( $result, $match_regex );
}
$GLOBALS[$varname] = $result;
return $result;
}
}
if ( !function_exists("get_fields") ) {
$_AWL_field_cache = array();
function get_fields( $tablename ) {
global $_AWL_field_cache;
if ( !isset($_AWL_field_cache[$tablename]) ) {
dbg_error_log( "core", ":get_fields: Loading fields for table '$tablename'" );
$qry = new AwlQuery();
$db = $qry->GetConnection();
$qry->SetSQL($db->GetFields($tablename));
$qry->Exec("core");
$fields = array();
while( $row = $qry->Fetch() ) {
$fields[$row->fieldname] = $row->typename . ($row->precision >= 0 ? sprintf('(%d)',$row->precision) : '');
}
$_AWL_field_cache[$tablename] = $fields;
}
return $_AWL_field_cache[$tablename];
}
}
if ( !function_exists("force_utf8") ) {
function define_byte_mappings() {
global $byte_map, $nibble_good_chars;
$ascii_char = '[\x00-\x7F]';
$cont_byte = '[\x80-\xBF]';
$utf8_2 = '[\xC0-\xDF]' . $cont_byte;
$utf8_3 = '[\xE0-\xEF]' . $cont_byte . '{2}';
$utf8_4 = '[\xF0-\xF7]' . $cont_byte . '{3}';
$utf8_5 = '[\xF8-\xFB]' . $cont_byte . '{4}';
$nibble_good_chars = "/^($ascii_char+|$utf8_2|$utf8_3|$utf8_4|$utf8_5)(.*)$/s";
$byte_map = array(
"\x80" => "\xE2\x82\xAC",
"\x82" => "\xE2\x80\x9A",
"\x83" => "\xC6\x92",
"\x84" => "\xE2\x80\x9E",
"\x85" => "\xE2\x80\xA6",
"\x86" => "\xE2\x80\xA0",
"\x87" => "\xE2\x80\xA1",
"\x88" => "\xCB\x86",
"\x89" => "\xE2\x80\xB0",
"\x8A" => "\xC5\xA0",
"\x8B" => "\xE2\x80\xB9",
"\x8C" => "\xC5\x92",
"\x8E" => "\xC5\xBD",
"\x91" => "\xE2\x80\x98",
"\x92" => "\xE2\x80\x99",
"\x93" => "\xE2\x80\x9C",
"\x94" => "\xE2\x80\x9D",
"\x95" => "\xE2\x80\xA2",
"\x96" => "\xE2\x80\x93",
"\x97" => "\xE2\x80\x94",
"\x98" => "\xCB\x9C",
"\x99" => "\xE2\x84\xA2",
"\x9A" => "\xC5\xA1",
"\x9B" => "\xE2\x80\xBA",
"\x9C" => "\xC5\x93",
"\x9E" => "\xC5\xBE",
"\x9F" => "\xC5\xB8",
);
for( $i=160; $i < 256; $i++ ) {
$ch = chr($i);
$byte_map[$ch] = iconv('ISO-8859-1', 'UTF-8', $ch);
}
}
define_byte_mappings();
function force_utf8( $input ) {
global $byte_map, $nibble_good_chars;
$output = '';
$char = '';
$rest = '';
while( $input != '' ) {
if ( preg_match( $nibble_good_chars, $input, $matches ) ) {
$output .= $matches[1];
$rest = $matches[2];
}
else {
preg_match( '/^(.)(.*)$/s', $input, $matches );
$char = $matches[1];
$rest = $matches[2];
if ( isset($byte_map[$char]) ) {
$output .= $byte_map[$char];
}
else {
$output .= $char;
}
}
$input = $rest;
}
return $output;
}
}
function olson_from_tzstring( $tzstring ) {
global $c;
if ( function_exists('timezone_identifiers_list') && in_array($tzstring,timezone_identifiers_list()) ) return $tzstring;
if ( preg_match( '{((Antarctica|America|Africa|Atlantic|Asia|Australia|Indian|Europe|Pacific)/(([^/]+)/)?[^/]+)$}', $tzstring, $matches ) ) {
return $matches[1];
}
switch( $tzstring ) {
case 'New Zealand Standard Time': case 'New Zealand Daylight Time':
return 'Pacific/Auckland';
break;
case 'Central Standard Time': case 'Central Daylight Time': case 'US/Central':
return 'America/Chicago';
break;
case 'Eastern Standard Time': case 'Eastern Daylight Time': case 'US/Eastern':
case '(UTC-05:00) Eastern Time (US & Canada)':
return 'America/New_York';
break;
case 'Pacific Standard Time': case 'Pacific Daylight Time': case 'US/Pacific':
return 'America/Los_Angeles';
break;
case 'Mountain Standard Time': case 'Mountain Daylight Time': case 'US/Mountain': case 'Mountain Time':
return 'America/Denver';
break;
case '(GMT-07.00) Arizona':
return 'America/Phoenix';
break;
default:
if ( isset($c->timezone_translations) && is_array($c->timezone_translations)
&& !empty($c->timezone_translations[$tzstring]) )
return $c->timezone_translations[$tzstring];
}
return null;
}
if ( !function_exists("deprecated") ) {
function deprecated( $method ) {
global $c;
if ( isset($c->dbg['ALL']) || isset($c->dbg['deprecated']) ) {
$stack = debug_backtrace();
array_shift($stack);
if ( preg_match( '{/inc/iCalendar.php$}', $stack[0]['file'] ) && $stack[0]['line'] > __LINE__ ) return;
@error_log( sprintf( $c->sysabbr.':DEPRECATED: Call to deprecated method "%s"', $method));
foreach( $stack AS $k => $v ) {
@error_log( sprintf( $c->sysabbr.': ==> called from line %4d of %s', $v['line'], $v['file']));
}
}
}
}
if ( !function_exists("gzdecode") ) {
function gzdecode( $instring ) {
global $c;
if ( !isset($c->use_pipe_gunzip) || $c->use_pipe_gunzip ) {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/dev/null", "a")
);
$process = proc_open('gunzip',$descriptorspec, $pipes);
if ( is_resource($process) ) {
fwrite($pipes[0],$instring);
fclose($pipes[0]);
$outstring = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
return $outstring;
}
return '';
}
else {
$g=tempnam('./','gz');
file_put_contents($g,$instring);
ob_start();
readgzfile($g);
$d=ob_get_clean();
unlink($g);
return $d;
}
}
}
function awl_version() {
global $c;
$c->awl_library_version = 0.56;
return $c->awl_library_version;
}