PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url_helper.php

https://github.com/CodeIgniter-TW/Web
PHP | 571 lines | 447 code | 21 blank | 103 comment | 12 complexity | 441b6b2eac3fbf486f4fa6b1998656cb MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. defined('BASEPATH') OR exit('No direct script access allowed');
  28. /**
  29. * CodeIgniter URL Helpers
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Helpers
  33. * @category Helpers
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/helpers/url_helper.html
  36. */
  37. // ------------------------------------------------------------------------
  38. if ( ! function_exists('site_url'))
  39. {
  40. /**
  41. * Site URL
  42. *
  43. * Create a local URL based on your basepath. Segments can be passed via the
  44. * first parameter either as a string or an array.
  45. *
  46. * @param string
  47. * @return string
  48. */
  49. function site_url($uri = '')
  50. {
  51. $CI =& get_instance();
  52. return $CI->config->site_url($uri);
  53. }
  54. }
  55. // ------------------------------------------------------------------------
  56. if ( ! function_exists('base_url'))
  57. {
  58. /**
  59. * Base URL
  60. *
  61. * Create a local URL based on your basepath.
  62. * Segments can be passed in as a string or an array, same as site_url
  63. * or a URL to a file can be passed in, e.g. to an image file.
  64. *
  65. * @param string
  66. * @return string
  67. */
  68. function base_url($uri = '')
  69. {
  70. $CI =& get_instance();
  71. return $CI->config->base_url($uri);
  72. }
  73. }
  74. // ------------------------------------------------------------------------
  75. if ( ! function_exists('current_url'))
  76. {
  77. /**
  78. * Current URL
  79. *
  80. * Returns the full URL (including segments) of the page where this
  81. * function is placed
  82. *
  83. * @return string
  84. */
  85. function current_url()
  86. {
  87. $CI =& get_instance();
  88. return $CI->config->site_url($CI->uri->uri_string());
  89. }
  90. }
  91. // ------------------------------------------------------------------------
  92. if ( ! function_exists('uri_string'))
  93. {
  94. /**
  95. * URL String
  96. *
  97. * Returns the URI segments.
  98. *
  99. * @return string
  100. */
  101. function uri_string()
  102. {
  103. $CI =& get_instance();
  104. return $CI->uri->uri_string();
  105. }
  106. }
  107. // ------------------------------------------------------------------------
  108. if ( ! function_exists('index_page'))
  109. {
  110. /**
  111. * Index page
  112. *
  113. * Returns the "index_page" from your config file
  114. *
  115. * @return string
  116. */
  117. function index_page()
  118. {
  119. $CI =& get_instance();
  120. return $CI->config->item('index_page');
  121. }
  122. }
  123. // ------------------------------------------------------------------------
  124. if ( ! function_exists('anchor'))
  125. {
  126. /**
  127. * Anchor Link
  128. *
  129. * Creates an anchor based on the local URL.
  130. *
  131. * @param string the URL
  132. * @param string the link title
  133. * @param mixed any attributes
  134. * @return string
  135. */
  136. function anchor($uri = '', $title = '', $attributes = '')
  137. {
  138. $title = (string) $title;
  139. if ( ! is_array($uri))
  140. {
  141. $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
  142. }
  143. else
  144. {
  145. $site_url = site_url($uri);
  146. }
  147. if ($title === '')
  148. {
  149. $title = $site_url;
  150. }
  151. if ($attributes !== '')
  152. {
  153. $attributes = _stringify_attributes($attributes);
  154. }
  155. return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  156. }
  157. }
  158. // ------------------------------------------------------------------------
  159. if ( ! function_exists('anchor_popup'))
  160. {
  161. /**
  162. * Anchor Link - Pop-up version
  163. *
  164. * Creates an anchor based on the local URL. The link
  165. * opens a new window based on the attributes specified.
  166. *
  167. * @param string the URL
  168. * @param string the link title
  169. * @param mixed any attributes
  170. * @return string
  171. */
  172. function anchor_popup($uri = '', $title = '', $attributes = FALSE)
  173. {
  174. $title = (string) $title;
  175. $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
  176. if ($title === '')
  177. {
  178. $title = $site_url;
  179. }
  180. if ($attributes === FALSE)
  181. {
  182. return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
  183. }
  184. if ( ! is_array($attributes))
  185. {
  186. $attributes = array($attributes);
  187. // Ref: http://www.w3schools.com/jsref/met_win_open.asp
  188. $window_name = '_blank';
  189. }
  190. elseif ( ! empty($attributes['window_name']))
  191. {
  192. $window_name = $attributes['window_name'];
  193. unset($attributes['window_name']);
  194. }
  195. foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0') as $key => $val)
  196. {
  197. $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
  198. unset($attributes[$key]);
  199. }
  200. $attributes = _stringify_attributes($attributes);
  201. return '<a href="'.$site_url
  202. .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
  203. .$attributes.'>'.$title.'</a>';
  204. }
  205. }
  206. // ------------------------------------------------------------------------
  207. if ( ! function_exists('mailto'))
  208. {
  209. /**
  210. * Mailto Link
  211. *
  212. * @param string the email address
  213. * @param string the link title
  214. * @param mixed any attributes
  215. * @return string
  216. */
  217. function mailto($email, $title = '', $attributes = '')
  218. {
  219. $title = (string) $title;
  220. if ($title === '')
  221. {
  222. $title = $email;
  223. }
  224. return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
  225. }
  226. }
  227. // ------------------------------------------------------------------------
  228. if ( ! function_exists('safe_mailto'))
  229. {
  230. /**
  231. * Encoded Mailto Link
  232. *
  233. * Create a spam-protected mailto link written in Javascript
  234. *
  235. * @param string the email address
  236. * @param string the link title
  237. * @param mixed any attributes
  238. * @return string
  239. */
  240. function safe_mailto($email, $title = '', $attributes = '')
  241. {
  242. $title = (string) $title;
  243. if ($title === '')
  244. {
  245. $title = $email;
  246. }
  247. $x = str_split('<a href="mailto:', 1);
  248. for ($i = 0, $l = strlen($email); $i < $l; $i++)
  249. {
  250. $x[] = '|'.ord($email[$i]);
  251. }
  252. $x[] = '"';
  253. if ($attributes !== '')
  254. {
  255. if (is_array($attributes))
  256. {
  257. foreach ($attributes as $key => $val)
  258. {
  259. $x[] = ' '.$key.'="';
  260. for ($i = 0, $l = strlen($val); $i < $l; $i++)
  261. {
  262. $x[] = '|'.ord($val[$i]);
  263. }
  264. $x[] = '"';
  265. }
  266. }
  267. else
  268. {
  269. for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
  270. {
  271. $x[] = $attributes[$i];
  272. }
  273. }
  274. }
  275. $x[] = '>';
  276. $temp = array();
  277. for ($i = 0, $l = strlen($title); $i < $l; $i++)
  278. {
  279. $ordinal = ord($title[$i]);
  280. if ($ordinal < 128)
  281. {
  282. $x[] = '|'.$ordinal;
  283. }
  284. else
  285. {
  286. if (count($temp) === 0)
  287. {
  288. $count = ($ordinal < 224) ? 2 : 3;
  289. }
  290. $temp[] = $ordinal;
  291. if (count($temp) === $count)
  292. {
  293. $number = ($count === 3)
  294. ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
  295. : (($temp[0] % 32) * 64) + ($temp[1] % 64);
  296. $x[] = '|'.$number;
  297. $count = 1;
  298. $temp = array();
  299. }
  300. }
  301. }
  302. $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
  303. $x = array_reverse($x);
  304. ob_start();
  305. ?><script type="text/javascript">
  306. //<![CDATA[
  307. var l=new Array();
  308. <?php
  309. for ($i = 0, $c = count($x); $i < $c; $i++) { ?>l[<?php echo $i; ?>]='<?php echo $x[$i]; ?>';<?php } ?>
  310. for (var i = l.length-1; i >= 0; i=i-1){
  311. if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
  312. else document.write(unescape(l[i]));}
  313. //]]>
  314. </script><?php
  315. $buffer = ob_get_contents();
  316. ob_end_clean();
  317. return $buffer;
  318. }
  319. }
  320. // ------------------------------------------------------------------------
  321. if ( ! function_exists('auto_link'))
  322. {
  323. /**
  324. * Auto-linker
  325. *
  326. * Automatically links URL and Email addresses.
  327. * Note: There's a bit of extra code here to deal with
  328. * URLs or emails that end in a period. We'll strip these
  329. * off and add them after the link.
  330. *
  331. * @param string the string
  332. * @param string the type: email, url, or both
  333. * @param bool whether to create pop-up links
  334. * @return string
  335. */
  336. function auto_link($str, $type = 'both', $popup = FALSE)
  337. {
  338. if ($type !== 'email' && preg_match_all('#(^|\s|\(|\b)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i', $str, $matches))
  339. {
  340. $pop = ($popup) ? ' target="_blank" ' : '';
  341. for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
  342. {
  343. if (preg_match('/(\.|\,)$/i', $matches[6][$i], $m))
  344. {
  345. $punct = $m[1];
  346. $matches[6][$i] = substr($matches[6][$i], 0, -1);
  347. }
  348. else
  349. {
  350. $punct = '';
  351. }
  352. $str = str_replace($matches[0][$i],
  353. $matches[1][$i].'<a href="http'.$matches[4][$i].'://'
  354. .$matches[5][$i].$matches[6][$i].'"'.$pop.'>http'
  355. .$matches[4][$i].'://'.$matches[5][$i]
  356. .$matches[6][$i].'</a>'.$punct,
  357. $str);
  358. }
  359. }
  360. if ($type !== 'url' && preg_match_all('/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]+)/i', $str, $matches))
  361. {
  362. for ($i = 0, $c = count($matches); $i < $c; $i++)
  363. {
  364. if (preg_match('/(\.|\,)$/i', $matches[3][$i], $m))
  365. {
  366. $punct = $m[1];
  367. $matches[3][$i] = substr($matches[3][$i], 0, -1);
  368. }
  369. else
  370. {
  371. $punct = '';
  372. }
  373. if (filter_var(($m = $matches[1][$i].'@'.$matches[2][$i].'.'.$matches[3][$i]), FILTER_VALIDATE_EMAIL) !== FALSE)
  374. {
  375. $str = str_replace($matches[0][$i], safe_mailto($m).$punct, $str);
  376. }
  377. }
  378. }
  379. return $str;
  380. }
  381. }
  382. // ------------------------------------------------------------------------
  383. if ( ! function_exists('prep_url'))
  384. {
  385. /**
  386. * Prep URL
  387. *
  388. * Simply adds the http:// part if no scheme is included
  389. *
  390. * @param string the URL
  391. * @return string
  392. */
  393. function prep_url($str = '')
  394. {
  395. if ($str === 'http://' OR $str === '')
  396. {
  397. return '';
  398. }
  399. $url = parse_url($str);
  400. if ( ! $url OR ! isset($url['scheme']))
  401. {
  402. return 'http://'.$str;
  403. }
  404. return $str;
  405. }
  406. }
  407. // ------------------------------------------------------------------------
  408. if ( ! function_exists('url_title'))
  409. {
  410. /**
  411. * Create URL Title
  412. *
  413. * Takes a "title" string as input and creates a
  414. * human-friendly URL string with a "separator" string
  415. * as the word separator.
  416. *
  417. * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
  418. * @param string $str Input string
  419. * @param string $separator Word separator
  420. * (usually '-' or '_')
  421. * @param bool $lowercase Wether to transform the output string to lowercase
  422. * @return string
  423. */
  424. function url_title($str, $separator = '-', $lowercase = FALSE)
  425. {
  426. if ($separator === 'dash')
  427. {
  428. $separator = '-';
  429. }
  430. elseif ($separator === 'underscore')
  431. {
  432. $separator = '_';
  433. }
  434. $q_separator = preg_quote($separator, '#');
  435. $trans = array(
  436. '&.+?;' => '',
  437. '[^a-z0-9 _-]' => '',
  438. '\s+' => $separator,
  439. '('.$q_separator.')+' => $separator
  440. );
  441. $str = strip_tags($str);
  442. foreach ($trans as $key => $val)
  443. {
  444. $str = preg_replace('#'.$key.'#i', $val, $str);
  445. }
  446. if ($lowercase === TRUE)
  447. {
  448. $str = strtolower($str);
  449. }
  450. return trim(trim($str, $separator));
  451. }
  452. }
  453. // ------------------------------------------------------------------------
  454. if ( ! function_exists('redirect'))
  455. {
  456. /**
  457. * Header Redirect
  458. *
  459. * Header redirect in two flavors
  460. * For very fine grained control over headers, you could use the Output
  461. * Library's set_header() function.
  462. *
  463. * @param string $uri URL
  464. * @param string $method Redirect method
  465. * 'auto', 'location' or 'refresh'
  466. * @param int $code HTTP Response status code
  467. * @return void
  468. */
  469. function redirect($uri = '', $method = 'auto', $code = NULL)
  470. {
  471. if ( ! preg_match('#^(\w+:)?//#i', $uri))
  472. {
  473. $uri = site_url($uri);
  474. }
  475. // IIS environment likely? Use 'refresh' for better compatibility
  476. if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
  477. {
  478. $method = 'refresh';
  479. }
  480. elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
  481. {
  482. // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
  483. $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
  484. && $_SERVER['REQUEST_METHOD'] === 'POST'
  485. && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
  486. ? 303 : 302;
  487. }
  488. switch ($method)
  489. {
  490. case 'refresh':
  491. header('Refresh:0;url='.$uri);
  492. break;
  493. default:
  494. header('Location: '.$uri, TRUE, $code);
  495. break;
  496. }
  497. exit;
  498. }
  499. }
  500. /* End of file url_helper.php */
  501. /* Location: ./system/helpers/url_helper.php */