322 lines
7.2 KiB
PHP
322 lines
7.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* NRE
|
|
*
|
|
* @author coderkun <olli@coderkun.de>
|
|
* @copyright 2013 coderkun (http://www.coderkun.de)
|
|
* @license http://www.gnu.org/licenses/gpl.html
|
|
* @link http://www.coderkun.de/projects/nre
|
|
*/
|
|
|
|
namespace nre\core;
|
|
|
|
|
|
/**
|
|
* Class to create web links based on the current request.
|
|
*
|
|
* @author coderkun <olli@coderkun.de>
|
|
*/
|
|
class Linker
|
|
{
|
|
/**
|
|
* Current request
|
|
*
|
|
* @var Request
|
|
*/
|
|
private $request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Construct a new linker.
|
|
*
|
|
* @param Request $request Current request
|
|
*/
|
|
function __construct(\nre\requests\WebRequest $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Process a title and optional a date to create link parameters.
|
|
*
|
|
* @param string $title Titel
|
|
* @param string $date Date
|
|
* @return string Created link parameters
|
|
*/
|
|
public static function getLinkParams($title, $date=null)
|
|
{
|
|
// Parameters
|
|
$param = '';
|
|
|
|
// Mask special sign seperately
|
|
$specials = array('/', '?', '&');
|
|
foreach($specials as &$special) {
|
|
$title = str_replace($special, rawurlencode(rawurlencode($special)), $title);
|
|
}
|
|
|
|
// Process title
|
|
$param .= str_replace(
|
|
' ',
|
|
'-',
|
|
substr(
|
|
$title,
|
|
0,
|
|
\nre\configs\CoreConfig::$classes['linker']['url']['length']
|
|
)
|
|
);
|
|
|
|
// Process date
|
|
if(!empty($date)) {
|
|
$param = substr($date, 0, 10).\nre\configs\CoreConfig::$classes['linker']['url']['delimiter'].$param;
|
|
}
|
|
|
|
|
|
// Mask and return parameters
|
|
return array(rawurlencode($param));
|
|
}
|
|
|
|
|
|
/**
|
|
* Extract date and title from a parameter string.
|
|
*
|
|
* @param string $dateTitle Parameter string with date and title
|
|
* @return array Extracted date and title as associative array
|
|
*/
|
|
public static function extractDateTitle($dateTitle)
|
|
{
|
|
// Get delimiter
|
|
$delimiter = \nre\configs\CoreConfig::$classes[strtolower(get_class())]['url']['delimiter'];
|
|
|
|
// Split
|
|
$dateTitle = explode($delimiter, $dateTitle);
|
|
if(count($dateTitle) < 4) {
|
|
throw new IdNotFoundException(implode($delimiter, $dateTitle));
|
|
}
|
|
|
|
// Get parts
|
|
$date = urldecode(implode($delimiter, array_slice($dateTitle, 0, 3)));
|
|
$title = urldecode(implode($delimiter, array_slice($dateTitle, 3)));
|
|
|
|
|
|
// Return date and title
|
|
return array(
|
|
'date' => $date,
|
|
'title' => $title
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Create a web link.
|
|
*
|
|
* @param array $params Parameters to use
|
|
* @param int $offset Ignore first parameters
|
|
* @param bool $exclusiveParams Use only the given parameters
|
|
* @param array $getParams GET-parameter to use
|
|
* @param bool $exclusiveGetParams Use only the given GET-parameters
|
|
* @param string $anchor Target anchor
|
|
* @param bool $absolute Include hostname etc. for an absolute URL
|
|
* @return string Created link
|
|
*/
|
|
public function link($params=null, $offset=0, $exclusiveParams=true, $getParams=null, $exclusiveGetParams=true, $anchor=null, $absolute=false)
|
|
{
|
|
// Make current request to response
|
|
$response = new \nre\responses\WebResponse();
|
|
|
|
|
|
// Check parameters
|
|
if(is_null($params)) {
|
|
$params = array();
|
|
}
|
|
elseif(!is_array($params)) {
|
|
$params = array($params);
|
|
}
|
|
|
|
// Set parameters from request
|
|
$reqParams = array_slice($this->request->getParams(), 1, $offset);
|
|
if(count($reqParams) < $offset && $offset > 0) {
|
|
$reqParams[] = $this->request->getParam(1, 'intermediate');
|
|
}
|
|
if(count($reqParams) < $offset && $offset > 1) {
|
|
$reqParams[] = $this->request->getParam(2, 'action');
|
|
}
|
|
$params = array_map('rawurlencode', $params);
|
|
$params = array_merge($reqParams, $params);
|
|
|
|
// Use Layout
|
|
$layout = \nre\configs\AppConfig::$defaults['toplevel'];
|
|
if(!empty($getParams) && array_key_exists('layout', $getParams)) {
|
|
$layout = $getParams['layout'];
|
|
}
|
|
array_unshift($params, $layout);
|
|
|
|
// Use parameters from request only
|
|
if(!$exclusiveParams)
|
|
{
|
|
$params = array_merge(
|
|
$params,
|
|
array_slice(
|
|
$this->request->getParams(),
|
|
count($params)
|
|
)
|
|
);
|
|
}
|
|
|
|
// Set parameters
|
|
call_user_func_array(
|
|
array(
|
|
$response,
|
|
'addParams'
|
|
),
|
|
$params
|
|
);
|
|
|
|
|
|
// Check GET-parameters
|
|
if(is_null($getParams)) {
|
|
$getParams = array();
|
|
}
|
|
elseif(!is_array($params)) {
|
|
$params = array($params);
|
|
}
|
|
if(!$exclusiveGetParams)
|
|
{
|
|
$getParams = array_merge(
|
|
$this->request->getGetParams(),
|
|
$getParams
|
|
);
|
|
}
|
|
|
|
// Set GET-parameters
|
|
$response->addGetParams($getParams);
|
|
|
|
|
|
// Create and return link
|
|
return self::createLink($this->request, $response, $anchor, $absolute);
|
|
}
|
|
|
|
|
|
/**
|
|
* Create a link from an URL.
|
|
*
|
|
* @param string $url URL to create link from
|
|
* @param bool $absolute Create absolute URL
|
|
* @return string Created link
|
|
*/
|
|
public function hardlink($url, $absolute=false)
|
|
{
|
|
return $this->createUrl($url, $this->request, $absolute);
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Create a link.
|
|
*
|
|
* @param Request $request Current request
|
|
* @param Response $response Current response
|
|
* @param bool $absolute Create absolute link
|
|
* @param string $anchor Anchor on target
|
|
* @return string Created link
|
|
*/
|
|
private static function createLink(Request $request, Response $response, $anchor=null, $absolute=false)
|
|
{
|
|
// Check response
|
|
if(is_null($response)) {
|
|
return null;
|
|
}
|
|
|
|
|
|
// Get parameters
|
|
$params = $response->getParams(1);
|
|
|
|
// Check Action
|
|
if(count($params) == 2 && $params[1] == \nre\configs\CoreConfig::$defaults['action']) {
|
|
array_pop($params);
|
|
}
|
|
|
|
// Set parameters
|
|
$link = implode('/', $params);
|
|
|
|
// Apply reverse-routes
|
|
$link = $request->applyReverseRoutes($link);
|
|
|
|
|
|
// Get GET-parameters
|
|
$getParams = $response->getGetParams();
|
|
|
|
// Layout überprüfen
|
|
if(array_key_exists('layout', $getParams) && $getParams['layout'] == \nre\configs\AppConfig::$defaults['toplevel']) {
|
|
unset($getParams['layout']);
|
|
}
|
|
|
|
// Set GET-parameters
|
|
if(array_key_exists('url', $getParams)) {
|
|
unset($getParams['url']);
|
|
}
|
|
if(count($getParams) > 0) {
|
|
$link .= '?'.http_build_query($getParams);
|
|
}
|
|
|
|
// Add anchor
|
|
if(!is_null($anchor)) {
|
|
$link .= "#$anchor";
|
|
}
|
|
|
|
|
|
// Create URL
|
|
$url = self::createUrl($link, $request, $absolute);
|
|
|
|
|
|
return $url;
|
|
}
|
|
|
|
|
|
/**
|
|
* Adapt a link to the environment.
|
|
*
|
|
* @param string $url URL
|
|
* @param Request $request Current request
|
|
* @param bool $absolute Create absolute URL
|
|
* @return string Adapted URL
|
|
*/
|
|
private static function createUrl($url, Request $request, $absolute=false)
|
|
{
|
|
// Variables
|
|
$server = $_SERVER['SERVER_NAME'];
|
|
$uri = $_SERVER['REQUEST_URI'];
|
|
$prefix = '';
|
|
|
|
|
|
// Determine prefix
|
|
$ppos = array(strlen($uri));
|
|
if(($p = strpos($uri, '/'.$request->getParam(1, 'intermediate'))) !== false) {
|
|
$ppos[] = $p;
|
|
}
|
|
$prefix = substr($uri, 0, min($ppos));
|
|
|
|
// Create absolute URL
|
|
if($absolute) {
|
|
$prefix = "http://$server/".trim($prefix, '/');
|
|
}
|
|
|
|
// Put URL together
|
|
$url = rtrim($prefix, '/').'/'.ltrim($url, '/');
|
|
|
|
|
|
// Return URL
|
|
return $url;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|