New class purposal: UrlParser
| From: | Roberto Bertó | Date: | Sat, 04 Jan 2003 13:59:43 +0000 |
| Subject: | New class purposal: UrlParser | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-12092@lists.php.net to get a copy of this message | ||
Hi all,
I am turning a class I wrote to PEAR, since I guess there are lots of
guys that use something like it. I search at PEAR and didnt find
anything like this package.
Its UrlParser, that, convert http://example.com/path/to/my/name to
path=to and my=name
I am sending the code I am converting to PEAR format.
I hope you approve it.
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
//
+----------------------------------------------------------------------+
// | PHP version
4 |
//
+----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP
Group |
//
+----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP
license, |
// | that is bundled with this package in the file LICENSE, and
is |
// | available at through the world-wide-web
at |
// |
http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable
to |
// | obtain it through the world-wide-web, please send a note
to |
// | license@php.net so we can mail you a copy
immediately. |
//
+----------------------------------------------------------------------+
// | Authors: Roberto Berto
<darkelder.php.net> |
//
+----------------------------------------------------------------------+
//
// $Id$
require_once "PEAR.php";
/**
* UrlParser fake URLs turned into data
*
*
*
-------------------------------------------------------------------------
* LICENSE (LGPL)
* Copyright (C) 2002-2003 Roberto Berto
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
* Or at http://www.gnu.org/licenses/lgpl.txt Get
a fake URL like
* http://www.example.com/index.php/section/fruits/subsection/orange
* and turn into: array("section" => "fruits", "subsection" =>
"orange")
*
-------------------------------------------------------------------------
*
*
* Also, UrlParser can can add the array to $_REQUEST. and/or to
* $_GET and/or $_POST.
*
* From a given URL (http://www.example.com/path/to/sub/path) or Path
* (/path/to/sub/path) it turn path=to and sub=path, from the right
* to left.
*
* If the number of subpaths isn't a pair, it will forget the
* last one. Eg: /iAmAlone/fruit/orange/color/red will be turned
* into fruit=orange and color=red and iAmAlone will not be
* used.
*
* If no URL or Path was set, it will try to guess the current
* URL from PHP ENV.
*
* Note that it works with full URL and with PATHs.
*
*
* METHODS:
* array parse([string URL])
* return an key=>val array with the URL or from PHP ENV
*
* void putGet(array)
* will merge the given UrlParser array with the $_GET var
*
* void putRequest(array)
* will merge the given UrlParser array with $_REQUEST var
*
* void putPost(array)
* merge the given UrlParser array with $_POST
*
* void putAll(array)
* merge given UrlParser array with $_GET, $_REQUEST and $_POST
*
* void putGlobal(array)
* will put each key to global scope (not recommended)
*
* USAGE / Example:
* $url = "http://www.example.to/me/Name/she/HerName/Section/Hat";
* require_once "UrlParser/UrlParser.php";
*
* $urlParsed = UrlParser::parse($url);
* print_r($urlParsed);
*
* UrlParser::putGet($urlParsed);
* print $_GET["me"]; // will print "Name"
*/
class UrlParser
{
function parse($url = NULL)
{
// guessing current URL
if (empty($url))
{
$url = $_SERVER["PATH_INFO"];
}
// running defined config
if (CMS_MODULES_URLPARSER == TRUE)
{
$parsed = $this->parse($url);
}
if (CMS_MODULES_URLPARSER_GET == TRUE)
{
$get = TRUE;
} else {
$get = FALSE;
}
if (CMS_MODULES_URLPARSER_REQUEST == TRUE)
{
$request = TRUE;
} else {
$request = FALSE;
}
$this->merge($parsed,$get,$request);
if (CMS_MODULES_URLPARSER_SMARTY == TRUE)
{
$this->smarty($parsed);
}
}
/**
* URI Parse
*
* Parse a URL and return an associative array
*
* @param string $uri uri to turn into an
associative array
* @return array a key=>value array of the URL
*/
function parse($uri)
{
// cant have a \ at the end
$uri = preg_replace("/\/$/","",$uri);
$elements = explode("/",$uri);
for ($i = sizeof($elements) - 1; $i >= 0; $i--)
{
$x++;
if ($x % 2 == 0)
{
if (!empty($elements[$i]))
{
$parsed["{$elements[$i]}"] = $cache;
}
} else {
$cache = urldecode($elements[$i]);
}
}
return $parsed;
}
/**
* Merge
*
* Merge the URI parsed into $_GET and/or/nor REQUEST
*
* @param string $uri uri to pass to $this->parse
* @param bool $get if true, merge _$GET with the
parsed
* @param bool $request if true, merge $_REQUEST with
the parsed
*/
function merge($parsed,$get,$request)
{
if ($get == TRUE)
{
$_GET = array_merge($_GET,$parsed);
}
if ($request == TRUE)
{
$_REQUEST = array_merge($_REQUEST,$parsed);
}
}
function smarty($parsed)
{
if (sizeof($parsed) == 0) {
return FALSE;
}
global $cms;
foreach ($parsed as $key => $val)
{
$cms->smarty->assign("url_" . $key,$val);
}
}
}
?>
--
Atenciosamente,
---------------------------------------------------------
Roberto Berto - roberto@desenvolve.com.br
Gerente de Contas / Desenvolve
Hospedagem a R$ 7,60 por mês, com toda a nossa qualidade!
Confira: http://www.desenvolve.com.br
DESENVOLVE Soluções de Internet
http://www.desenvolve.com.br
TEL/FAX: 51 32270599 Rua Duque de Caxias, 1594 conj. 204
CEP 90010-281 - Porto Alegre - RS
---------------------------------------------------------