function search($needle, $haystack, $sort_flag = null, $compare_function = null) { $len = strlen($needle); if (!$len) { return false; } if (!is_array($haystack)) { return false; } if (is_null($compare_function)) { $compare_function = 'strncmp'; } if ($sort_flag) { sort($haystack); } fbDebug::dump($haystack, '$haystack'); $high = count($haystack) - 1; if ($high < 0) { return false; } $mid = 0; $low = 0; fbDebug::log(sprintf("%3d: low=%5s high=%5s mid=%5s arr[mid]=%5s\n", __LINE__, $low, $high, $mid, $haystack[$mid])); while ($high >= $low) { $mid = $high + $low >> 1; $t = @$compare_function($needle, $haystack[$mid], $len); if ($t < 0) { $high = $mid - 1; } elseif ($t > 0) { $low = $mid + 1; } else { fbDebug::log(sprintf("%3d: low=%5s high=%5s mid=%5s arr[mid]=%5s\n", __LINE__, $low, $high, $mid, $haystack[$mid])); return $mid; } fbDebug::log(sprintf("%3d: low=%5s high=%5s mid=%5s arr[mid]=%5s\n", __LINE__, $low, $high, $mid, $haystack[$mid])); } fbDebug::log(sprintf("%3d: low=%5s high=%5s mid=%5s arr[mid]=%5s\n", __LINE__, $low, $high, $mid, $haystack[$mid])); return $mid; }
function getCountryIdByIP($ip) { static $cache = array(); $ipf = fbGeoIP_Free_Ascii::_formatIP4address($ip); if (!$ipf) { return false; } /* if (isset($cache[$ipf])) { return $cache[$ipf]; } */ // \todo make a parameter $file = FREEBEER_BASE . '/etc/geo/geo-ips.txt'; if (!is_file($file)) { trigger_error(sprintf("File not found: '%s'", $file)); return false; } $bsf =& new fbBinarySearch_File($file); $bsf->setReadLength(64); // 36 for unix, 37 for ms-dos/windows $found = $bsf->search($ipf); $midval = $bsf->getRecordNumber(); $rec_len = $bsf->getRecordLength(); if ($found === false) { return false; } fbDebug::log(sprintf("ip=%s found=%s midval=%s rec_len=%s\n\n", $ip, $found, $midval, $rec_len)); $matches = preg_split('/\\s+/', $found); if (count($matches) < 3) { trigger_error(sprintf('Read error reading \'%s\': File does not contain whitespace', $file)); return false; } if ($ipf < $matches[0] || $ipf > $matches[1]) { trigger_error(sprintf('Unexpected result: \'%s\' (\'%s\') is not between \'%s\' and \'%s\'', $ip, $ipf, $matches[0], $matches[1]), E_USER_NOTICE); return false; } // $cache[$ipf] = trim($matches[2]); return trim($matches[2]); }
function getCountryIdByIP($ip) { static $cache = array(); $ipf = fbGeoIP_Free_Binary::_formatIP4address($ip); if (!$ipf) { return false; } /* if (isset($cache[$ipf])) { return $cache[$ipf]; } */ // \todo make a parameter $file = FREEBEER_BASE . '/etc/geo/geo-ips-binary.txt'; if (!is_file($file)) { trigger_error(sprintf("File not found: '%s'", $file)); return false; } $bsf =& new fbBinarySearch_File($file); $bsf->setRecordLength(10); $found = $bsf->search($ipf); $midval = $bsf->getRecordNumber(); $rec_len = $bsf->getRecordLength(); if ($found === false) { return false; } fbDebug::log(sprintf("ip=%s found=%s midval=%s rec_len=%s\n\n", $ip, $found, $midval, $rec_len)); $a = unpack('C*', $found); $cc = sprintf('%c%c', $a[9], $a[10]); $ips = sprintf('%d.%d.%d.%d', $a[1], $a[2], $a[3], $a[4]); $ipe = sprintf('%d.%d.%d.%d', $a[5], $a[6], $a[7], $a[8]); if ($ipf < substr($found, 0, 4) || $ipf > substr($found, 4, 4)) { trigger_error(sprintf('Unexpected result: \'%s\' is not between \'%s\' and \'%s\'', $ip, $ips, $ipe), E_USER_NOTICE); return false; } // $cache[$ipf] = $cc; return $cc; }
function assertHandler($file, $line, $code) { if ((fbDebug::getLevel() & FB_DEBUG_NO_ASSERTS) == FB_DEBUG_NO_ASSERTS) { return; } fbDebug::log(fbDebug::pre(sprintf("%-30s: Assertion Failed: '%s'\t(%s)\n", basename($file) . '(' . $line . ')', $code, $file))); fbDebug::stackdump(2); }
function fb_adodb_outp($msg, $newline) { if (!(fbDebug::getLevel() & FB_DEBUG_ADODB)) { global $_SERVER; // < 4.1.0 if ($newline) { $msg .= "<br>\n"; } if (isset($_SERVER['HTTP_USER_AGENT'])) { echo $msg; } else { echo strip_tags($msg); } flush(); return; } // \todo make fbDebug browser aware if (isset($_SERVER['HTTP_USER_AGENT'])) { // if (function_exists('html_entity_decode')) { // } $msg = wordwrap($msg, 132); fbDebug::log("\n<pre>\n" . $msg . "\n</pre>\n"); } else { if ($newline) { $msg .= "\n"; } fbDebug::log(strip_tags($msg)); } }
<?php // $CVSHeader: _freebeer/www/demo/Debug.php,v 1.2 2004/03/07 17:51:33 ross Exp $ // Copyright (c) 2002-2004, Ross Smith. All rights reserved. // Licensed under the BSD or LGPL License. See license.txt for details. require_once './_demo.php'; require_once FREEBEER_BASE . '/lib/Debug.php'; fbDebug::setLevel(FB_DEBUG_ALL); echo html_header_demo('fbDebug demo'); echo "<pre>\n"; echo 'fbDebug::getLevel()=', fbDebug::getLevel(), "\n"; fbDebug::log('log'); assert('1 == 2'); print_r(debug_backtrace()); fbDebug::stackdump(); fbDebug::trace("Hey you!"); ?> </pre> <address> $CVSHeader: _freebeer/www/demo/Debug.php,v 1.2 2004/03/07 17:51:33 ross Exp $ </address> </body> </html>
function _elapsed() { if (ADODB_debug::time_sql()) { $this->_timer->stop(); fbDebug::log(fbDebug::pre(sprintf("Elapsed time: %s\n", $this->_timer->toString()))); } }