PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url_helper.php

https://github.com/betchi/CodeIgniter
PHP | 557 lines | 342 code | 44 blank | 171 comment | 32 complexity | 901f7bb7d9e55a85fbe6c794e5a2c117 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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 - 2014, 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 $uri
  47. * @param string $protocol
  48. * @return string
  49. */
  50. function site_url($uri = '', $protocol = NULL)
  51. {
  52. return get_instance()->config->site_url($uri, $protocol);
  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 $uri
  66. * @param string $protocol
  67. * @return string
  68. */
  69. function base_url($uri = '', $protocol = NULL)
  70. {
  71. return get_instance()->config->base_url($uri, $protocol);
  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. return get_instance()->uri->uri_string();
  104. }
  105. }
  106. // ------------------------------------------------------------------------
  107. if ( ! function_exists('index_page'))
  108. {
  109. /**
  110. * Index page
  111. *
  112. * Returns the "index_page" from your config file
  113. *
  114. * @return string
  115. */
  116. function index_page()
  117. {
  118. return get_instance()->config->item('index_page');
  119. }
  120. }
  121. // ------------------------------------------------------------------------
  122. if ( ! function_exists('anchor'))
  123. {
  124. /**
  125. * Anchor Link
  126. *
  127. * Creates an anchor based on the local URL.
  128. *
  129. * @param string the URL
  130. * @param string the link title
  131. * @param mixed any attributes
  132. * @return string
  133. */
  134. function anchor($uri = '', $title = '', $attributes = '')
  135. {
  136. $title = (string) $title;
  137. $site_url = is_array($uri)
  138. ? site_url($uri)
  139. : preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
  140. if ($title === '')
  141. {
  142. $title = $site_url;
  143. }
  144. if ($attributes !== '')
  145. {
  146. $attributes = _stringify_attributes($attributes);
  147. }
  148. return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  149. }
  150. }
  151. // ------------------------------------------------------------------------
  152. if ( ! function_exists('anchor_popup'))
  153. {
  154. /**
  155. * Anchor Link - Pop-up version
  156. *
  157. * Creates an anchor based on the local URL. The link
  158. * opens a new window based on the attributes specified.
  159. *
  160. * @param string the URL
  161. * @param string the link title
  162. * @param mixed any attributes
  163. * @return string
  164. */
  165. function anchor_popup($uri = '', $title = '', $attributes = FALSE)
  166. {
  167. $title = (string) $title;
  168. $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri);
  169. if ($title === '')
  170. {
  171. $title = $site_url;
  172. }
  173. if ($attributes === FALSE)
  174. {
  175. return '<a href="'.$site_url.'" onclick="window.open(\''.$site_url."', '_blank'); return false;\">".$title.'</a>';
  176. }
  177. if ( ! is_array($attributes))
  178. {
  179. $attributes = array($attributes);
  180. // Ref: http://www.w3schools.com/jsref/met_win_open.asp
  181. $window_name = '_blank';
  182. }
  183. elseif ( ! empty($attributes['window_name']))
  184. {
  185. $window_name = $attributes['window_name'];
  186. unset($attributes['window_name']);
  187. }
  188. foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0') as $key => $val)
  189. {
  190. $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val;
  191. unset($attributes[$key]);
  192. }
  193. $attributes = _stringify_attributes($attributes);
  194. return '<a href="'.$site_url
  195. .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\""
  196. .$attributes.'>'.$title.'</a>';
  197. }
  198. }
  199. // ------------------------------------------------------------------------
  200. if ( ! function_exists('mailto'))
  201. {
  202. /**
  203. * Mailto Link
  204. *
  205. * @param string the email address
  206. * @param string the link title
  207. * @param mixed any attributes
  208. * @return string
  209. */
  210. function mailto($email, $title = '', $attributes = '')
  211. {
  212. $title = (string) $title;
  213. if ($title === '')
  214. {
  215. $title = $email;
  216. }
  217. return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>';
  218. }
  219. }
  220. // ------------------------------------------------------------------------
  221. if ( ! function_exists('safe_mailto'))
  222. {
  223. /**
  224. * Encoded Mailto Link
  225. *
  226. * Create a spam-protected mailto link written in Javascript
  227. *
  228. * @param string the email address
  229. * @param string the link title
  230. * @param mixed any attributes
  231. * @return string
  232. */
  233. function safe_mailto($email, $title = '', $attributes = '')
  234. {
  235. $title = (string) $title;
  236. if ($title === '')
  237. {
  238. $title = $email;
  239. }
  240. $x = str_split('<a href="mailto:', 1);
  241. for ($i = 0, $l = strlen($email); $i < $l; $i++)
  242. {
  243. $x[] = '|'.ord($email[$i]);
  244. }
  245. $x[] = '"';
  246. if ($attributes !== '')
  247. {
  248. if (is_array($attributes))
  249. {
  250. foreach ($attributes as $key => $val)
  251. {
  252. $x[] = ' '.$key.'="';
  253. for ($i = 0, $l = strlen($val); $i < $l; $i++)
  254. {
  255. $x[] = '|'.ord($val[$i]);
  256. }
  257. $x[] = '"';
  258. }
  259. }
  260. else
  261. {
  262. for ($i = 0, $l = strlen($attributes); $i < $l; $i++)
  263. {
  264. $x[] = $attributes[$i];
  265. }
  266. }
  267. }
  268. $x[] = '>';
  269. $temp = array();
  270. for ($i = 0, $l = strlen($title); $i < $l; $i++)
  271. {
  272. $ordinal = ord($title[$i]);
  273. if ($ordinal < 128)
  274. {
  275. $x[] = '|'.$ordinal;
  276. }
  277. else
  278. {
  279. if (count($temp) === 0)
  280. {
  281. $count = ($ordinal < 224) ? 2 : 3;
  282. }
  283. $temp[] = $ordinal;
  284. if (count($temp) === $count)
  285. {
  286. $number = ($count === 3)
  287. ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
  288. : (($temp[0] % 32) * 64) + ($temp[1] % 64);
  289. $x[] = '|'.$number;
  290. $count = 1;
  291. $temp = array();
  292. }
  293. }
  294. }
  295. $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
  296. $x = array_reverse($x);
  297. $output = "<script type=\"text/javascript\">\n"
  298. ."\t//<![CDATA[\n"
  299. ."\tvar l=new Array();\n";
  300. for ($i = 0, $c = count($x); $i < $c; $i++)
  301. {
  302. $output .= "\tl[".$i."] = '".$x[$i]."';\n";
  303. }
  304. $output .= "\n\tfor (var i = l.length-1; i >= 0; i=i-1) {\n"
  305. ."\t\tif (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");\n"
  306. ."\t\telse document.write(unescape(l[i]));\n"
  307. ."\t}\n"
  308. ."\t//]]>\n"
  309. .'</script>';
  310. return $output;
  311. }
  312. }
  313. // ------------------------------------------------------------------------
  314. if ( ! function_exists('auto_link'))
  315. {
  316. /**
  317. * Auto-linker
  318. *
  319. * Automatically links URL and Email addresses.
  320. * Note: There's a bit of extra code here to deal with
  321. * URLs or emails that end in a period. We'll strip these
  322. * off and add them after the link.
  323. *
  324. * @param string the string
  325. * @param string the type: email, url, or both
  326. * @param bool whether to create pop-up links
  327. * @return string
  328. */
  329. function auto_link($str, $type = 'both', $popup = FALSE)
  330. {
  331. // Find and replace any URLs.
  332. if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
  333. {
  334. // Set our target HTML if using popup links.
  335. $target = ($popup) ? ' target="_blank"' : '';
  336. // We process the links in reverse order (last -> first) so that
  337. // the returned string offsets from preg_match_all() are not
  338. // moved as we add more HTML.
  339. foreach (array_reverse($matches) as $match)
  340. {
  341. // $match[0] is the matched string/link
  342. // $match[1] is either a protocol prefix or 'www.'
  343. //
  344. // With PREG_OFFSET_CAPTURE, both of the above is an array,
  345. // where the actual value is held in [0] and its offset at the [1] index.
  346. $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
  347. $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
  348. }
  349. }
  350. // Find and replace any emails.
  351. if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE))
  352. {
  353. foreach (array_reverse($matches[0]) as $match)
  354. {
  355. if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
  356. {
  357. $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
  358. }
  359. }
  360. }
  361. return $str;
  362. }
  363. }
  364. // ------------------------------------------------------------------------
  365. if ( ! function_exists('prep_url'))
  366. {
  367. /**
  368. * Prep URL
  369. *
  370. * Simply adds the http:// part if no scheme is included
  371. *
  372. * @param string the URL
  373. * @return string
  374. */
  375. function prep_url($str = '')
  376. {
  377. if ($str === 'http://' OR $str === '')
  378. {
  379. return '';
  380. }
  381. $url = parse_url($str);
  382. if ( ! $url OR ! isset($url['scheme']))
  383. {
  384. return 'http://'.$str;
  385. }
  386. return $str;
  387. }
  388. }
  389. // ------------------------------------------------------------------------
  390. if ( ! function_exists('url_title'))
  391. {
  392. /**
  393. * Create URL Title
  394. *
  395. * Takes a "title" string as input and creates a
  396. * human-friendly URL string with a "separator" string
  397. * as the word separator.
  398. *
  399. * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
  400. * @param string $str Input string
  401. * @param string $separator Word separator
  402. * (usually '-' or '_')
  403. * @param bool $lowercase Wether to transform the output string to lowercase
  404. * @return string
  405. */
  406. function url_title($str, $separator = '-', $lowercase = FALSE)
  407. {
  408. if ($separator === 'dash')
  409. {
  410. $separator = '-';
  411. }
  412. elseif ($separator === 'underscore')
  413. {
  414. $separator = '_';
  415. }
  416. $q_separator = preg_quote($separator, '#');
  417. $trans = array(
  418. '&.+?;' => '',
  419. '[^a-z0-9 _-]' => '',
  420. '\s+' => $separator,
  421. '('.$q_separator.')+' => $separator
  422. );
  423. $str = strip_tags($str);
  424. foreach ($trans as $key => $val)
  425. {
  426. $str = preg_replace('#'.$key.'#i', $val, $str);
  427. }
  428. if ($lowercase === TRUE)
  429. {
  430. $str = strtolower($str);
  431. }
  432. return trim(trim($str, $separator));
  433. }
  434. }
  435. // ------------------------------------------------------------------------
  436. if ( ! function_exists('redirect'))
  437. {
  438. /**
  439. * Header Redirect
  440. *
  441. * Header redirect in two flavors
  442. * For very fine grained control over headers, you could use the Output
  443. * Library's set_header() function.
  444. *
  445. * @param string $uri URL
  446. * @param string $method Redirect method
  447. * 'auto', 'location' or 'refresh'
  448. * @param int $code HTTP Response status code
  449. * @return void
  450. */
  451. function redirect($uri = '', $method = 'auto', $code = NULL)
  452. {
  453. if ( ! preg_match('#^(\w+:)?//#i', $uri))
  454. {
  455. $uri = site_url($uri);
  456. }
  457. // IIS environment likely? Use 'refresh' for better compatibility
  458. if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
  459. {
  460. $method = 'refresh';
  461. }
  462. elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
  463. {
  464. if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
  465. {
  466. $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
  467. ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
  468. : 307;
  469. }
  470. else
  471. {
  472. $code = 302;
  473. }
  474. }
  475. switch ($method)
  476. {
  477. case 'refresh':
  478. header('Refresh:0;url='.$uri);
  479. break;
  480. default:
  481. header('Location: '.$uri, TRUE, $code);
  482. break;
  483. }
  484. exit;
  485. }
  486. }
  487. /* End of file url_helper.php */
  488. /* Location: ./system/helpers/url_helper.php */