vortice_book /sample/core/classes/Link.php

Language PHP Lines 233
MD5 Hash 9b9e26a71993e66361872f3726563192
Repository https://github.com/caferrari/vortice_book.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/* 
 * Copyright (c) 2008, Carlos AndrĂŠ Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
 * All rights reserved. 
 */

/**
 * Link class, Work with uri encode and decode
 *
 * @version	1
 * @package	Framework
 * @author	Carlos AndrĂŠ Ferrari <carlos@ferrari.eti.br>
 */
class Link{

	/**
	* Temporary link encoded
	*
	* @var		string
	* @access	private
	*/
	private $el = '';
	
	/**
	* Constructor, Create a URI
	*
	* @param	string	$page		format: [model][+controller:[action]]
	* @param	string	$pars		link params
	* @return	void
	*/
	function __construct($page='', $pars=''){
		$this->el = $this->createLink($page, $pars);
	}
	
	/**
	* Encode a link
	*
	* @param	array	$url		array with url data
	* @return	string
	* @static
	*/
	static function default_encode($url){
		$url = unserialize($url);
		$page = $url['url'];
		$page = explode(':', $page);
		$pars = '';
		
		switch (count($page)){
			case 3:
				if ($page[1]==='') $page[1] = default_controller;
				break;
			case 2:
				if ($page[1]==='') $page[1] = '';
				break;
		}
		
		$page = implode($page, '/');
		
		if (isset($url->pars)){
			$pars = http_build_query($url['pars']);
			$pars = str_replace('=', ':', $pars);
			$pars = str_replace('&', '/', $pars);
		}
		
		$l = $page . '/' . $pars . '/';
		return preg_replace('@\/+@', '/', $l);
	}
	
	/**
	* Decode a link
	*
	* @param	string	$url		encoded uri
	* @return	string
	* @static
	*/
	static function default_decode($url){
		$pars = array();
		$page = array();
		$partes = explode('/', $url);
		foreach ($partes as $p)
			if (preg_match('@.*:.*@', $p)){
				$p = explode(':', $p); 
				$pars[$p[0]] = $p[1];
			}else
				$page[] = $p;
		$page = implode($page, ':');

		$json = serialize(array('url' => $page, 'pars' => $pars));
		return $json;
	}
	
	/**
	* Create a URI
	*
	* @param	string	$page		[model][+controller+[action]]
	* @param	string	$pars	link params
	* @return	string
	* @static
	*/
	static function createLink($page='', $pars=''){
		if (!preg_match('@^([a-z\-]+)(:[a-z\-]+)?$@', $page))
			return preg_replace('@\/+@', '/', virtualroot . ((request_lang !== default_lang) ? request_lang : '') . '/' . $page . '/');
		$page = explode(':', $page);
		if (count($page) === 2){
			if ($page[0] === default_controller && $page[1]===default_action) $page=array();
			else if ($page[1] === default_action) unset($page[1]);
		}else{
			if ($page[0] === default_controller) $page = array();
		}
		$page = array_values($page);

		parse_str(is_array($pars) ? http_build_query($pars) : $pars, $p);
		if (count($page) === 0 || $page[0] === '') $page='';
		else $page = implode(':', $page);
		$url = $pars ? serialize(array('url' => $page, 'pars' => $p)) : serialize(array('url' => $page));
		$link = (function_exists('link_encode')) ? link_encode($url) : Link::default_encode($url);
		if (request_lang != default_lang)
			return virtualroot . request_lang . '/' . $link;
		
		return virtualroot . $link;
	}
	
	/**
	* Translate the requested URI, check for internationalization request and create the uri constant
	*
	* @return	void
	* @static
	*/
	static function translateUri(){
		$q = $_SERVER['REQUEST_URI'];
		
		if (!ajax && !post && !preg_match('@\/$@', $q)){
			header ('HTTP/1.1 301 Moved Permanently');
			header ('Location: ' . $q . '/');
			exit();
		}
		
		if (virtualroot !== '/') $q = preg_replace('/^'.addcslashes(virtualroot, '/').'/', '', $q);
		$q = preg_replace('@^\/|\/$@', '', $q);
		$tmp = explode('/', $q);
		if ($tmp[0] !== ''){
			if (preg_match('@^([a-z]{2}|[a-z]{2}-[a-z]{2})$@', $tmp[0]) && !file_exists(root . 'app/controller/' . ucfirst($tmp[0]).'Controller.php') && !is_dir(root . 'app/modules/' . $tmp[0])){
				define('request_lang', $tmp[0]);
				unset($tmp[0]);
				$q = implode('/', $tmp);
				if (request_lang==default_lang){
					header('Location: ' . virtualroot . $q , true);
					exit();
				}
			}else $q = implode('/', $tmp);
		}
		define('uri', urldecode($q));
		if (!defined('request_lang')) define('request_lang', default_lang);
	}
	
	/**
	* Convert the requested uri to global constants (module, controller, action) and usefull parameters
	*
	* @return	void
	* @static
	*/
	static function parseQuery(){
		global $_PAR;
		$q = uri;
		$route = Route::exec();
		if ($route) $q = $route;
		else{
			if (function_exists('link_decode'))
				try {
					$q = unserialize(link_decode($q));
				}catch (Exception $e){
					throw new BaseException('Invalid URL', 'URL checksum failed!', '400');
				}
			else
				$q = unserialize(Link::default_decode($q));
		}
		
		$controller = $action = '';

		$q['url'] = isset($q['url']) ? $q['url'] : '';
		if (isset($q['pars'])) $_PAR = (array)$q['pars'];

		if ($q['url'] !== ''){
			$url = explode(':', $q['url']);
			if (count($url)==2){
				$controller = $url[0];
				$action = $url[1];
			}else
				if ($url[0]{0} === '.') $action = $url[0];
				else $controller = $url[0];
		}

		$tmp = explode('.', $action);
		if (count($tmp) > 1){
			$action = ($tmp[0] === '') ? '' : $tmp[0];
			Vortice::setRenderMode($tmp[1]);
		}else{
			$action = $tmp[0];
			$tmp = explode('.', $controller);
			if (count($tmp) > 1){
				$controller = ($tmp[0] === '') ? '' : $tmp[0];
				Vortice::setRenderMode($tmp[1]);
			}else $controller = $tmp[0];
		}
		
		if (!routed && Vortice::$rendermode==='html'){
			if ($controller===default_controller && ($action===default_action || $action==='')){
				header('Location: ' . new Link(default_controller . ':' . default_action, $_PAR), true, 301);
				exit();
			}
			if ($controller!==default_controller && $action===default_action){
				header('Location: ' . new Link($controller . ':' . default_action, $_PAR), true, 301);
				exit();
			}
		}
		
		if ($action==='') $action = default_action;
		if ($controller==='') $controller = default_controller;

		define ('controller', $controller);
		define ('action', $action);
	}
	
	/**
	* Return the encoded uri.
	*
	* @return	string
	*/
	public function __toString(){
        return $this->el;
    }
}
Back to Top