[ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 3 /* Reminder: always indent with 4 spaces (no tabs). */ 4 // +---------------------------------------------------------------------------+ 5 // | Geeklog 1.4 | 6 // +---------------------------------------------------------------------------+ 7 // | url.class.php | 8 // | | 9 // | class to allow for spider friendly URL's | 10 // +---------------------------------------------------------------------------+ 11 // | Copyright (C) 2002 by the following authors: | 12 // | | 13 // | Authors: Tony Bibbs - tony@tonybibbs.com | 14 // | | 15 // +---------------------------------------------------------------------------+ 16 // | | 17 // | This program is free software; you can redistribute it and/or | 18 // | modify it under the terms of the GNU General Public License | 19 // | as published by the Free Software Foundation; either version 2 | 20 // | of the License, or (at your option) any later version. | 21 // | | 22 // | This program is distributed in the hope that it will be useful, | 23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 // | GNU General Public License for more details. | 26 // | | 27 // | You should have received a copy of the GNU General Public License | 28 // | along with this program; if not, write to the Free Software Foundation, | 29 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 30 // | | 31 // +---------------------------------------------------------------------------+ 32 // 33 // $Id: url.class.php,v 1.11 2006/09/09 12:52:06 dhaun Exp $ 34 35 /** 36 * This class will allow you to use friendlier URL's, like: 37 * http://www.example.com/index.php/arg_value_1/arg_value_2/ instead of 38 * uglier http://www.example.com?arg1=value1&arg2=value2. 39 * NOTE: this does not currently work under windows as there is a well documented 40 * bug with IIS and PATH_INFO. Not sure yet if this will work with windows under 41 * apache. This was built so you could use this class and just disable it 42 * if you are an IIS user. 43 * 44 * @author Tony Bibbs <tony@tonybibbs.com> 45 * 46 */ 47 class url { 48 /** 49 * @access private 50 */ 51 var $_arguments = array(); // Array of argument names 52 /** 53 * @access private 54 */ 55 var $_enabled = true; 56 57 /** 58 * Constructor 59 * 60 * @param boolean $enabled whether rewriting is enabled 61 * 62 */ 63 function url($enabled=true) 64 { 65 $this->setEnabled($enabled); 66 $this->_arguments = array(); 67 if ($this->_enabled) { 68 $this->_getArguments(); 69 } 70 } 71 72 /** 73 * Grabs any variables from the query string 74 * 75 * @access private 76 */ 77 function _getArguments() 78 { 79 if (isset ($_SERVER['PATH_INFO'])) { 80 if ($_SERVER['PATH_INFO'] == '') 81 { 82 if (isset ($_ENV['ORIG_PATH_INFO'])) 83 { 84 $this->_arguments = explode('/', $_ENV['ORIG_PATH_INFO']); 85 } else { 86 $this->_arguments = array(); 87 } 88 } else { 89 $this->_arguments = explode ('/', $_SERVER['PATH_INFO']); 90 } 91 array_shift ($this->_arguments); 92 } else if (isset ($_ENV['ORIG_PATH_INFO'])) { 93 $this->_arguments = explode('/', substr($_ENV['ORIG_PATH_INFO'],1)); 94 } else { 95 $this->_arguments = array (); 96 } 97 } 98 99 /** 100 * Enables url rewriting, otherwise URL's are passed back 101 * 102 * @param boolean $switch turns URL rewriting on/off 103 * 104 */ 105 function setEnabled($switch) 106 { 107 if ($switch) { 108 $this->_enabled = true; 109 } else { 110 $this->_enabled = false; 111 } 112 } 113 114 /** 115 * Returns whether or not URL rewriting is enabled 116 * 117 * @return boolean true if URl rewriting is enabled, otherwise false 118 * 119 */ 120 function isEnabled() 121 { 122 return $this->_enabled; 123 } 124 125 /** 126 * Returns the number of variables found in query string 127 * 128 * This is particularly useful just before calling setArgNames() method 129 * 130 * @return int Number of arguments found in URL 131 * 132 */ 133 function numArguments() 134 { 135 return count($this->_arguments); 136 } 137 138 /** 139 * Assigns logical names to query string variables 140 * 141 * @param array $names String array of names to assign to variables pulled from query string 142 * @return boolean true on success otherwise false 143 * 144 */ 145 function setArgNames($names) 146 { 147 if (count($names) < count($this->_arguments)) { 148 print "URL Class: number of names passed to setArgNames must be equal or greater than number of arguments found in URL"; 149 exit; 150 } 151 if (is_array($names)) { 152 $newArray = array(); 153 for ($i = 1; $i <= count($this->_arguments); $i++) { 154 $newArray[current($names)] = current($this->_arguments); 155 next($names); 156 next($this->_arguments); 157 } 158 $this->_arguments = $newArray; 159 reset($this->_arguments); 160 } else { 161 return false; 162 } 163 return true; 164 } 165 166 /** 167 * Gets the value for an argument 168 * 169 * @param string $name Name of argument to fetch value for 170 * @return mixed returns value for a given argument 171 * 172 */ 173 function getArgument($name) 174 { 175 // if in GET VARS array return it 176 if (!empty($_GET[$name])) { 177 return $_GET[$name]; 178 } 179 180 // ok, pull from query string 181 if (in_array($name,array_keys($this->_arguments))) { 182 return $this->_arguments[$name]; 183 } 184 185 return ''; 186 } 187 188 /** 189 * Builds crawler friendly URL if URL rewriting is enabled 190 * 191 * This function will attempt to build a crawler friendly URL. If this feature is 192 * disabled because of platform issue it just returns original $url value 193 * 194 * @param string $url URL to try and convert 195 * @return string rewritten if _isenabled is true otherwise original url 196 * 197 */ 198 function buildURL($url) 199 { 200 if (!$this->isEnabled()) { 201 return $url; 202 } 203 204 $pos = strpos($url,'?'); 205 $query_string = substr($url,$pos+1); 206 $finalList = array(); 207 $paramList = explode('&',$query_string); 208 for ($i = 1; $i <= count($paramList); $i++) { 209 $keyValuePairs = explode('=',current($paramList)); 210 if (is_array($keyValuePairs)) { 211 $argName = current($keyValuePairs); 212 next($keyValuePairs); 213 $finalList[$argName] = current($keyValuePairs); 214 } 215 next($paramList); 216 } 217 $newArgs = '/'; 218 for ($i = 1; $i <= count($finalList); $i++) { 219 $newArgs .= current($finalList); 220 if ($i <> count($finalList)) { 221 $newArgs .= '/'; 222 } 223 next($finalList); 224 } 225 return str_replace('?' . $query_string,$newArgs,$url); 226 } 227 } 228 229 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |