PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/url_helper.php

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