1
0
mirror of https://github.com/e107inc/e107.git synced 2025-02-07 07:47:59 +01:00
php-e107/e107_handlers/e107Url.php

236 lines
5.8 KiB
PHP
Raw Normal View History

2008-11-25 16:26:03 +00:00
<?php
/*
* e107 website system
*
* Copyright (C) 2001-2008 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* URL Handler
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/e107Url.php,v $
2009-09-25 20:17:48 +00:00
* $Revision: 1.13 $
* $Date: 2009-09-25 20:17:48 $
2009-09-14 18:19:17 +00:00
* $Author: secretr $
2008-11-25 16:26:03 +00:00
*/
2009-09-14 18:19:17 +00:00
if (!defined('e107_INIT')) { exit; }
2008-11-25 16:26:03 +00:00
class eURL
{
2009-09-25 20:17:48 +00:00
/**
* @var array
*/
protected $_link_handlers = array();
2008-11-25 16:26:03 +00:00
/**
2009-09-14 18:19:17 +00:00
* Create site url
* Example:
* <code>e107::getUrl()->create('core::news', 'main', 'action=extend&id=1&sef=Item-SEF-URL');</code>
* <code>e107::getUrl()->create('myplug', 'main', 'action=myaction&id=1');</code>
*
2008-11-25 16:26:03 +00:00
* @param string $section
* @param string $urlType
* @param string|array $urlItems
* @return string URL or '#url-not-found' on error
2008-11-25 16:26:03 +00:00
*/
2009-09-14 18:19:17 +00:00
public function create($section, $urlType, $urlItems = array())
2008-11-25 16:26:03 +00:00
{
if (!is_array($urlItems))
{
2008-12-02 13:50:17 +00:00
//strange looking... well, I like it
parse_str($urlItems, $urlItems);
2008-11-25 16:26:03 +00:00
}
2009-09-25 20:17:48 +00:00
if($link = call_user_func($this->getHandlerFunction($section, $urlType), $urlItems))
{
return $link;
}
return '#url-not-found';
2008-11-25 16:26:03 +00:00
}
2009-09-14 18:19:17 +00:00
/**
2009-09-25 20:17:48 +00:00
* Alias of {@link create()}
2009-09-14 18:19:17 +00:00
* @param string $section
* @param string $urlType
* @param array $urlItems [optional]
* @return string URL
*/
public function getUrl($section, $urlType, $urlItems = array())
{
return $this->create($section, $urlType, $urlItems);
}
2008-11-25 16:26:03 +00:00
/**
2009-09-14 18:19:17 +00:00
* Parse Request
*
* @param string $section
* @param string $urlType
* @param string $request
* @return mixed parsed url
*/
2009-09-14 18:19:17 +00:00
public function parseRequest($section, $urlType, $request = '')
2008-11-25 16:26:03 +00:00
{
if (empty($request))
2008-11-25 16:26:03 +00:00
{
$request = e_QUERY;
2008-11-25 16:26:03 +00:00
}
2009-09-25 20:17:48 +00:00
return call_user_func('parse_'.$this->getHandlerFunction($section, $urlType), $request);
}
/**
* Get required profile function string
* NOTE: function existence is not granted
*
* @param string $section
* @param string $urlType
* @return string handler function
*/
public function getHandlerFunction($section, $urlType)
{
$handlerId = $this->toHandlerId($section, $urlType);
if (!isset($this->_link_handlers[$handlerId]))
2008-11-26 13:28:35 +00:00
{
$this->_link_handlers[$handlerId] = $this->_initHandler($section, $urlType);
2008-11-26 13:28:35 +00:00
}
2009-09-14 18:19:17 +00:00
2009-09-25 20:17:48 +00:00
return $this->_link_handlers[$handlerId];
}
2009-09-25 20:17:48 +00:00
/**
* Try to load required url profile script
*
* @param string $section
* @param string $urlType
* @return string function name
*/
2009-09-14 18:19:17 +00:00
protected function _initHandler($section, $urlType)
{
2009-09-25 20:17:48 +00:00
$section = $this->formatSection($section);
2008-12-02 13:50:17 +00:00
list($type, $section) = explode(':', $section, 2);
2008-11-26 13:28:35 +00:00
$handler = 'url_' . $section . '_' . $urlType;
2008-11-25 16:26:03 +00:00
// Check to see if custom code is active and exists
2009-09-25 20:17:48 +00:00
$val = e107::findPref('url_config/'.$section);
if ($val)
2008-11-25 16:26:03 +00:00
{
$filePath = str_replace(
array(
2008-12-02 13:50:17 +00:00
'core-custom:',
'core-profile:',
'plugin-custom:',
'plugin-profile:'
2008-12-02 13:50:17 +00:00
),
2009-09-25 20:17:48 +00:00
array(
2008-12-02 13:50:17 +00:00
e_FILE.'e_url/custom/core/',
e_FILE.'e_url/core/'.$section.'/',
e_FILE.'e_url/custom/plugin/',
e_PLUGIN.$section.'/e_url/',
2008-12-02 13:50:17 +00:00
),
2009-09-25 20:17:48 +00:00
$val
);
$fileName = $filePath.'/'.$urlType.'.php';
2008-12-02 13:50:17 +00:00
2008-11-25 16:26:03 +00:00
if (is_readable($fileName))
{
include_once ($fileName);
}
if (function_exists($handler))
{
return $handler;
}
}
2008-12-02 13:50:17 +00:00
//Search the default url config - the last station
$handlerId = $section . '/' . $urlType;
2009-09-25 20:17:48 +00:00
$fileName = ($type === 'core' ? e_FILE."e_url/core/{$handlerId}.php" : e_PLUGIN."{$section}/e_url/{$urlType}.php");
2008-11-25 16:26:03 +00:00
if (is_readable($fileName))
{
include_once ($fileName);
}
return $handler;
}
2009-09-25 20:17:48 +00:00
/**
* Format section to 'core|plugin:section_name' to be safe used in
* all internal routines.
* This method is here because plugins are allowed to omit 'plugin:' prefix.
*
* @param string $section
* @return string formatted section
*/
public function formatSection($section)
{
if (strpos($section, ':') === false)
{
$section = 'plugin:'.$section;
}
return $section;
}
/**
* Create the unique key for $_link_handlers
*
* @param string $section
* @param string $urlType
* @return string
*/
public function toHandlerId($section, $urlType)
{
return $this->formatSection($section). '/' . $urlType;
}
/**
* Get profile currently used for a given section
*
* @param string $section
* @param boolean $strict true - return null if not found, false - return 'main' if not found
* @return string section id
*/
public function getProfileId($section, $strict = false)
{
$val = e107::findPref('url_config/'.str_replace(array('core:', 'plugin:'), '', $section));
if($val)
{
$val = explode(':', $val, 2);
return $val[1];
}
return (!$strict || e107::getConfig()->isData('url_config/'.$section) ? 'main' : null);
}
/**
* Proxy for undefined methods. It allows quick (less arguments)
* call to {@link create()}.
* NOTE that passed to {@link create()} second argument
* ($urlType) is always 'main'!
*
* Example:
* <code>
* echo e107::getUrl()->createCoreNews('action=month&value=092009');
* //calls internal $this->create('core:news', 'main', 'action=month&value=092009');
*
* echo e107::getUrl()->createTagwords('q=tag word');
* //calls internal $this->create('plugin:tagwords', 'main', 'q=month&value=092009');
* </code>
* @param string $method
* @param array $arguments array(0 => request string|array)
* @return string URL
* @throws Exception
*/
2008-11-25 16:26:03 +00:00
function __call($method, $arguments) {
2009-09-25 20:17:48 +00:00
if (strpos($method, "createCore") === 0)
{
2009-09-25 20:17:48 +00:00
$section = strtolower(substr($method, 10));
return $this->create('core:'.$section, 'main', varset($arguments[0]));
}
2009-09-25 20:17:48 +00:00
elseif (strpos($method, "create") === 0)
{
2009-09-25 20:17:48 +00:00
$section = strtolower(substr($method, 6));
return $this->create('plugin:'.$section, 'main', varset($arguments[0]));
}
2009-09-25 20:17:48 +00:00
throw new Exception('Method '.$method.' does not exist!');//FIXME - e107Exception handler
2008-11-25 16:26:03 +00:00
}
}