00001 <?php
00025 if(!defined('ZIG_URL_SEGMENT_PATH'))define('ZIG_URL_SEGMENT_PATH', true);
00026 if(!defined('ZIG_URL_SEGMENT_ALL'))define('ZIG_URL_SEGMENT_ALL', false);
00027 function strchr_occurrences($str, $chr){
00028 $i=0;
00029 while(($rest = strstr($str, $chr)) !== false){
00030 $str = substr($rest, 1);
00031 $i++;
00032 }
00033 return $i;
00034 }
00055 function url_segment(){
00056 $list_args = func_get_args();
00057 switch(func_num_args()){
00058 case 2:
00059 $url = $list_args[0];
00060 $segment = $list_args[1];
00061 break;
00062 case 1:
00063 if(isset($_SERVER['SCRIPT_URI'])){
00064 $url = $_SERVER['SCRIPT_URI'].'?'.$_SERVER['QUERY_STRING'];
00065 }else{
00066
00067
00068 if(preg_match('/HTTP\/(.+)/', $_SERVER['SERVER_PROTOCOL'])){
00069 $protocol = 'http';
00070 }else{
00071 $protocol = 'https';
00072 }
00073
00074 if($_SERVER['SERVER_PORT'] != 80)$url = $protocol.'://'.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].'/'.$_SERVER['REQUEST_URI'];
00075 else $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
00076
00077 }
00078 $segment = $list_args[0];
00079 break;
00080 case 0:
00081 if(isset($_SERVER['SCRIPT_URI'])){
00082 $url = $_SERVER['SCRIPT_URI'].'?'.$_SERVER['QUERY_STRING'];
00083 }else{
00084
00085
00086 if(preg_match('/HTTP\/(.+)/', $_SERVER['SERVER_PROTOCOL'])){
00087 $protocol = 'http';
00088 }else{
00089 $protocol = 'https';
00090 }
00091
00092 if($_SERVER['SERVER_PORT'] != 80)$url = $protocol.'://'.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].'/'.$_SERVER['REQUEST_URI'];
00093 else $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
00094
00095 }
00096 $segment = true;
00097 break;
00098 default:
00099 perror("url_segment Takes only 1 or 2 arguments ".count($list_args).' Given');
00100 return false;
00101 }
00102 if(is_string($segment) && $segment == 'url')return $url;
00103 $url = parse_url($url);
00104 if(isset($url['path']))$url['path'] = '/'.ltrim($url['path'], '/');
00105 switch(gettype($segment)){
00106 case 'string':
00107 if(!isset($url[$segment]) || @strlen($segment)<=0){return false;}
00108 else{
00109 return $url[$segment];
00110 }
00111 break;
00112 case 'integer':
00113 $path_segment = explode('/', trim(@$_SERVER['PATH_INFO'], '/'));
00114 if(!isset($path_segment[$segment])){return false;}
00115 else{
00116 return $path_segment[$segment];
00117 }
00118 break;
00119 case 'boolean':
00120 switch($segment){
00121 case true:
00122 $path_segment = explode('/', trim(@$_SERVER['PATH_INFO'], '/'));
00123 return $path_segment;
00124 break;
00125 case false:
00126 return $url;
00127 break;
00128 }
00129 break;
00130 default:
00131 perror('The segment must be a String or an integer '.gettype($segment).' provided');
00132 return false;
00133 }
00134 }
00140 function getUrlRoot(){
00141 return pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
00142 }
00151 function getCurrentUrl($bindGet = false){
00152 if(!$bindGet)
00153 return getUrlRoot().'/'.implode('/', url_segment(ZIG_URL_SEGMENT_PATH));
00154 else{
00155 $base = getUrlRoot().'/'.implode('/', url_segment(ZIG_URL_SEGMENT_PATH));
00156 if(is_array($this->request->get->items) && count($this->request->get->items) >= 1){
00157 $base .= "?";
00158 foreach($this->request->get->items as $key => $val){
00159 $base .= "$key=$val";
00160 }
00161 }
00162 return $base;
00163 }
00164 }
00172 function array_fix($key, $val){
00173 if(count($key) != count($val)){
00174 perror('invalid arguments sent to array_fix(array, array)<br />Length of 2 inputed arrays (key, value), must be same<br />
00175 $key array is of length '.count($key).'<br />and contains<pre>'.print_r($key, true).
00176 '</pre>and<br />$val array is of length '.count($val).'<br />and contains<pre>'.print_r($val, true).'</pre>');
00177 return false;
00178 }else{
00179 for($i=0;$i<count($key);$i++){
00180 $ret[$key[$i]] = $val[$i];
00181 }
00182 }
00183 return $ret;
00184 }
00192 function zero_fill($str){
00193 $str = (string)$str;
00194 if(strlen($str)>4){return $str;}
00195 while(strlen($str) < 4){
00196 $str = '0'.$str;
00197 }
00198 return $str;
00199 }
00200 if(!function_exists('class_exists')){
00201 function class_exists($className){
00202 return in_array($className, get_declared_classes());
00203 }
00204 }
00205
00206 ?>