PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url_helper.php

https://github.com/coderkid/No-CMS
PHP | 558 lines | 432 code | 21 blank | 105 comment | 12 complexity | cc17ec893cb0192edf9aa22be2536982 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-2.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 - 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 $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. $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. // Find and replace any URLs.
  339. if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
  340. {
  341. // Set our target HTML if using popup links.
  342. $target = ($popup) ? ' target="_blank"' : '';
  343. // We process the links in reverse order (last -> first) so that
  344. // the returned string offsets from preg_match_all() are not
  345. // moved as we add more HTML.
  346. foreach (array_reverse($matches) as $match)
  347. {
  348. // $match[0] is the matched string/link
  349. // $match[1] is either a protocol prefix or 'www.'
  350. //
  351. // With PREG_OFFSET_CAPTURE, both of the above is an array,
  352. // where the actual value is held in [0] and its offset at the [1] index.
  353. $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>';
  354. $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0]));
  355. }
  356. }
  357. // Find and replace any emails.
  358. if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE))
  359. {
  360. foreach (array_reverse($matches[0]) as $match)
  361. {
  362. if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE)
  363. {
  364. $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0]));
  365. }
  366. }
  367. }
  368. return $str;
  369. }
  370. }
  371. // ------------------------------------------------------------------------
  372. if ( ! function_exists('prep_url'))
  373. {
  374. /**
  375. * Prep URL
  376. *
  377. * Simply adds the http:// part if no scheme is included
  378. *
  379. * @param string the URL
  380. * @return string
  381. */
  382. function prep_url($str = '')
  383. {
  384. if ($str === 'http://' OR $str === '')
  385. {
  386. return '';
  387. }
  388. $url = parse_url($str);
  389. if ( ! $url OR ! isset($url['scheme']))
  390. {
  391. return 'http://'.$str;
  392. }
  393. return $str;
  394. }
  395. }
  396. // ------------------------------------------------------------------------
  397. if ( ! function_exists('url_title'))
  398. {
  399. /**
  400. * Create URL Title
  401. *
  402. * Takes a "title" string as input and creates a
  403. * human-friendly URL string with a "separator" string
  404. * as the word separator.
  405. *
  406. * @todo Remove old 'dash' and 'underscore' usage in 3.1+.
  407. * @param string $str Input string
  408. * @param string $separator Word separator
  409. * (usually '-' or '_')
  410. * @param bool $lowercase Wether to transform the output string to lowercase
  411. * @return string
  412. */
  413. function url_title($str, $separator = '-', $lowercase = FALSE)
  414. {
  415. if ($separator === 'dash')
  416. {
  417. $separator = '-';
  418. }
  419. elseif ($separator === 'underscore')
  420. {
  421. $separator = '_';
  422. }
  423. $q_separator = preg_quote($separator, '#');
  424. $trans = array(
  425. '&.+?;' => '',
  426. '[^a-z0-9 _-]' => '',
  427. '\s+' => $separator,
  428. '('.$q_separator.')+' => $separator
  429. );
  430. $str = strip_tags($str);
  431. foreach ($trans as $key => $val)
  432. {
  433. $str = preg_replace('#'.$key.'#i', $val, $str);
  434. }
  435. if ($lowercase === TRUE)
  436. {
  437. $str = strtolower($str);
  438. }
  439. return trim(trim($str, $separator));
  440. }
  441. }
  442. // ------------------------------------------------------------------------
  443. if ( ! function_exists('redirect'))
  444. {
  445. /**
  446. * Header Redirect
  447. *
  448. * Header redirect in two flavors
  449. * For very fine grained control over headers, you could use the Output
  450. * Library's set_header() function.
  451. *
  452. * @param string $uri URL
  453. * @param string $method Redirect method
  454. * 'auto', 'location' or 'refresh'
  455. * @param int $code HTTP Response status code
  456. * @return void
  457. */
  458. function redirect($uri = '', $method = 'auto', $code = NULL)
  459. {
  460. if ( ! preg_match('#^(\w+:)?//#i', $uri))
  461. {
  462. $uri = site_url($uri);
  463. }
  464. // IIS environment likely? Use 'refresh' for better compatibility
  465. if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
  466. {
  467. $method = 'refresh';
  468. }
  469. elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
  470. {
  471. // Reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
  472. $code = (isset($_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_PROTOCOL'])
  473. && $_SERVER['REQUEST_METHOD'] === 'POST'
  474. && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
  475. ? 303 : 302;
  476. }
  477. switch ($method)
  478. {
  479. case 'refresh':
  480. header('Refresh:0;url='.$uri);
  481. break;
  482. default:
  483. header('Location: '.$uri, TRUE, $code);
  484. break;
  485. }
  486. exit;
  487. }
  488. }
  489. /* End of file url_helper.php */
  490. /* Location: ./system/helpers/url_helper.php */