00001 <?php
00011
00012
00013
00014
00015
00016
00046 function url($controller, $action='')
00047 {
00048
00049 if(strstr($controller, '://') !== false) return $controller;
00050
00051
00052
00053 if(strstr($controller, '/') !== false) {
00054 $url = $controller;
00055 if(defined('DISPATCH_URL') && $action !== true) {
00056 $url = DISPATCH_URL.$url;
00057 }
00058 return rtrim(DIR_WS_BASE, '/').$url;
00059 }
00060
00061 $controller = strtolower($controller);
00062 $action = strtolower($action);
00063
00064
00065
00066
00067 $urls = Registry::get('pronto:urls');
00068 foreach($urls as $regex=>$c) {
00069
00070
00071
00072
00073 $regex = preg_replace('|\([^\.][^\*].*\)|', '', $regex);
00074 $regex = str_replace('//', '/', $regex);
00075
00076 $a = '';
00077 if(is_array($c)) list($c,$a) = $c;
00078 $c = strtolower($c);
00079 $a = strtolower($a);
00080
00081 if($a) {
00082 if($c == $controller && $a == $action) {
00083 return url($regex);
00084 }
00085 } else if($c == $controller) {
00086 return url(str_replace(
00087 array('(.*)', '__'),
00088 array($action, '/'), $regex));
00089 }
00090 }
00091
00092 return url('/');
00093 }
00094
00112 function absolute_url($controller, $action='')
00113 {
00114
00115 if(strstr($controller, '://') !== false) return $controller;
00116
00117 if(defined('SITE_URL_BASE') && SITE_URL_BASE != '') {
00118 $url = rtrim(SITE_URL_BASE, '/');
00119 } else {
00120 $proto = $_SERVER['HTTPS'] ? 'https' : 'http';
00121 $host = $_SERVER['SERVER_NAME'];
00122 $port = $_SERVER['SERVER_PORT'];
00123
00124 $url = $proto.'://'.$host;
00125 if($proto == 'http' && $port != 80) $url .= ":$port";
00126 if($proto == 'https' && $port != 443) $url .= ":$port";
00127 }
00128
00129 return $url.url($controller, $action);
00130 }
00131
00132
00133
00134
00135
00136
00137
00144 function debug($var, $queue=true)
00145 {
00146 $web =& Registry::get('pronto:web');
00147 if($queue && is_object($web)) {
00148 $bt = debug_backtrace();
00149 $loc = "{$bt[0]['file']}:{$bt[0]['line']}";
00150 $web->debug_messages[] = array('loc'=>$loc, 'msg'=>print_r($var, true));
00151 } else {
00152 if(is_object($web)) {
00153 echo '<pre>';
00154 print_r($var);
00155 echo '</pre>';
00156 } else {
00157 print_r($var);
00158 }
00159 }
00160 }
00161
00168 function error($message)
00169 {
00170 if(DEBUG !== true) return;
00171 trigger_error($message, E_USER_ERROR);
00172 }
00173
00177 function backtrace()
00178 {
00179 $out = "Method/Function\t\t\tCaller\n";
00180 $out .= "---------------\t\t\t------\n";
00181 $bt = $backtrace ? $backtrace : debug_backtrace();
00182 array_shift($bt);
00183 foreach($bt as $tp) {
00184 $fn = $caller = '';
00185 if($tp['object']) {
00186 $fn .= get_class($tp['object']).'::';
00187 } else if($tp['class']) {
00188 $fn .= "{$tp['class']}::";
00189 }
00190 if($tp['function']) $fn .= "{$tp['function']}()";
00191 if($tp['file']) $caller .= "{$tp['file']}:{$tp['line']}";
00192 $out .= "$fn\t\t\t$caller\n";
00193 }
00194 return $out;
00195 }
00196
00201 function pronto_exception($e)
00202 {
00203 pronto_error($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine(), null, $e->getTrace());
00204 }
00205
00209 function pronto_error($errno, $message, $file, $line, $context=null, $backtrace=null)
00210 {
00211
00212 if(error_reporting() == 0) return;
00213
00214
00215 if(in_array($errno, array(2048,8,8192))) {
00216 return;
00217 }
00218
00219
00220 $web =& Registry::get('pronto:web');
00221 if(DEBUG === true && is_object($web) && $web->ajax) {
00222
00223 $msg = "$file:$line\\n\\n" . strip_tags($message);
00224 $web->ajax_exec("alert('".str_replace("'", "\\'", $msg)."');");
00225 die;
00226 }
00227
00228 if($errno == E_USER_ERROR) {
00229 $display_source = 'none';
00230 } else {
00231 $display_source = 'block';
00232 }
00233
00234 $constants = @get_defined_constants(true);
00235 $constants = $constants['user'];
00236 $data = array('$_POST'=>$_POST, '$_GET'=>$_GET, '$_COOKIE'=>$_COOKIE, '$_SESSION'=>$_SESSION, '$_SERVER'=>$_SERVER, '$_ENV'=>$_ENV, 'CONSTANTS'=>$constants);
00237
00238 $tvars = array(
00239 'errno' => $errno,
00240 'file' => $file,
00241 'line' => $line,
00242 'message' => $message,
00243 'uri' => $_SERVER['REQUEST_URI'],
00244 'method' => $_SERVER['REQUEST_METHOD']
00245 );
00246
00247 if(is_file($file)) {
00248 $fh = file($file);
00249 $len = sizeof($fh);
00250 $tvars['source_start'] = max(0, $line-5);
00251 $tvars['source_lines'] = array_slice($fh, $tvars['source_start'], 10);
00252 }
00253
00254
00255 $tvars['backtrace'] = array();
00256 $bt = $backtrace ? $backtrace : debug_backtrace();
00257 array_shift($bt);
00258 foreach($bt as $tp) {
00259 $fn = $caller = '';
00260 if($tp['object']) {
00261 $fn .= get_class($tp['object']).'::';
00262 } else if($tp['class']) {
00263 $fn .= "{$tp['class']}::";
00264 }
00265 if($tp['function']) $fn .= "{$tp['function']}()";
00266 if($tp['file']) $caller .= "{$tp['file']}:{$tp['line']}";
00267 $tvars['backtrace'][] = array('function'=>$fn, 'caller'=>$caller);
00268 }
00269
00270
00271 $tvars['data'] = array();
00272 foreach($data as $name=>$global) {
00273 $tvars['data'][$name] = array();
00274 if(!is_array($global)) continue;
00275 foreach($global as $k=>$v) $tvars['data'][$name][$k] = var_export($v, true);
00276 }
00277
00278 $tvars['output'] = ob_get_contents();
00279 ob_end_clean();
00280
00281
00282
00283 extract($tvars, EXTR_OVERWRITE);
00284 ob_start();
00285 if(DEBUG === true) {
00286
00287 if(is_object($web)) {
00288 require(DIR_FS_PRONTO.DS.'core'.DS.'error'.DS.'html.php');
00289 } else {
00290 require(DIR_FS_PRONTO.DS.'core'.DS.'error'.DS.'text.php');
00291 }
00292 $content = ob_get_contents();
00293 ob_end_clean();
00294 echo "\n$content\n";
00295 } else {
00296
00297 require(DIR_FS_PRONTO.DS.'core'.DS.'error'.DS.'text.php');
00298 $content = ob_get_contents();
00299 ob_end_clean();
00300 $email = defined('TECH_EMAIL') ? TECH_EMAIL : ADMIN_EMAIL;
00301 @mail($email, SITE_NAME.": Error", $content);
00302
00303 if(is_object($web)) {
00304
00305
00306 $web->internalerror();
00307 }
00308 }
00309 die;
00310 }
00311
00312
00313
00314
00315
00316
00317
00321 function array_hash($arr)
00322 {
00323 $hash = array();
00324 foreach($arr as $el) {
00325 $hash[$el] = $el;
00326 }
00327 return $hash;
00328 }
00329
00334 function array_yesno()
00335 {
00336 return array('1'=>__('Yes'), '0'=>__('No'));
00337 }
00338
00343 function yesno($int)
00344 {
00345 return $int > 0 ? __('Yes') : __('No');
00346 }
00347
00356 function array_extract($arr, $key)
00357 {
00358 $ret = array();
00359 foreach($arr as $v) $ret[] = $v[$key];
00360 return $ret;
00361 }
00362
00366 function array_sub($arr, $key)
00367 {
00368 return array_extract($arr, $key);
00369 }
00370
00376 function array_divide($arr, $size)
00377 {
00378 $ret = $a = array();
00379 $row = $col = 0;
00380 for($i = 0; $i < count($arr); $i++) {
00381 if($col % $size == 0) {
00382 if($a) $ret[$row++] = $a;
00383 $col = 0;
00384 $a = array();
00385 }
00386 $a[$col++] = $arr[$i];
00387 }
00388 $ret[$row] = $a;
00389 return $ret;
00390 }
00391
00402 function array_insert(&$arr, $key, $val, $after)
00403 {
00404 $new = array();
00405 foreach($arr as $k=>$v) {
00406 $new[$k] = $v;
00407 if($k == $after) $new[$key] = $val;
00408 }
00409 $arr = $new;
00410 }
00411
00418 function assert_type(&$var, $type)
00419 {
00420 switch($type) {
00421 case 'array':
00422
00423 if(!is_array($var)) $var = $var ? array($var) : array();
00424 break;
00425 case 'string':
00426 if(!is_string($var)) $var = is_array($var) ? "".current($var) : "$var";
00427 break;
00428 case 'int':
00429 if(!is_int($var)) $var = is_array($var) ? (int)current($var) : (int)$var;
00430 break;
00431 case 'float':
00432 if(!is_float($var)) $var = is_array($var) ? (float)current($var) : (float)$var;
00433 break;
00434 case 'date':
00435 if(!preg_match('|^[12][0-9]{3}-[01][0-9]-[0123][0-9]$|', $var)) $var = date('Y-m-d');
00436 break;
00437 }
00438 return $var;
00439 }
00440
00444 function a($key)
00445 {
00446 $access =& Registry::get('pronto:access');
00447
00448
00449
00450 return is_object($access) ? $access->has_access($key) : true;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460 if(!function_exists('mb_strlen')) {
00461 function mb_send_mail($to, $subj, $message, $hdrs='', $param='') {
00462 return mail($to, $subj, $message, $hdrs, $param);
00463 }
00464 function mb_strlen($s) {
00465 return strlen($s);
00466 }
00467 function mb_strpos($haystack, $needle, $offset=0) {
00468 return strpos($haystack, $needle, $offset);
00469 }
00470 function mb_strrpos($haystack, $needle, $offset=0) {
00471 return strrpos($haystack, $needle, $offset);
00472 }
00473 function mb_substr($str, $start, $length=0) {
00474 return substr($str, $start, $length);
00475 }
00476 function mb_strtolower($str) {
00477 return strtolower($str);
00478 }
00479 function mb_strtoupper($str) {
00480 return strtoupper($str);
00481 }
00482 function mb_substr_count($haystack, $needle) {
00483 return substr_count($haystack, $needle);
00484 }
00485
00486
00487 }
00488
00489 ?>