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

/system/helpers/url_helper.php

https://bitbucket.org/zhemel/cloudengine
PHP | 593 lines | 334 code | 75 blank | 184 comment | 77 complexity | 760c93dcaba9477d50370490d7b9a808 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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. //<![CDATA[
  297. var l=new Array();
  298. <?php
  299. $i = 0;
  300. foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
  301. for (var i = l.length-1; i >= 0; i=i-1){
  302. if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
  303. else document.write(unescape(l[i]));}
  304. //]]>
  305. </script><?php
  306. $buffer = ob_get_contents();
  307. ob_end_clean();
  308. return $buffer;
  309. }
  310. }
  311. // ------------------------------------------------------------------------
  312. /**
  313. * Auto-linker
  314. *
  315. * Automatically links URL and Email addresses.
  316. * Note: There's a bit of extra code here to deal with
  317. * URLs or emails that end in a period. We'll strip these
  318. * off and add them after the link.
  319. *
  320. * @access public
  321. * @param string the string
  322. * @param string the type: email, url, or both
  323. * @param bool whether to create pop-up links
  324. * @return string
  325. */
  326. if ( ! function_exists('auto_link'))
  327. {
  328. function auto_link($str, $type = 'both', $popup = FALSE)
  329. {
  330. if ($type != 'email')
  331. {
  332. if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
  333. {
  334. $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
  335. for ($i = 0; $i < count($matches['0']); $i++)
  336. {
  337. $period = '';
  338. if (preg_match("|\.$|", $matches['6'][$i]))
  339. {
  340. $period = '.';
  341. $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
  342. }
  343. $str = str_replace($matches['0'][$i],
  344. $matches['1'][$i].'<a href="http'.
  345. $matches['4'][$i].'://'.
  346. $matches['5'][$i].
  347. $matches['6'][$i].'"'.$pop.'>http'.
  348. $matches['4'][$i].'://'.
  349. $matches['5'][$i].
  350. $matches['6'][$i].'</a>'.
  351. $period, $str);
  352. }
  353. }
  354. }
  355. if ($type != 'url')
  356. {
  357. if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
  358. {
  359. for ($i = 0; $i < count($matches['0']); $i++)
  360. {
  361. $period = '';
  362. if (preg_match("|\.$|", $matches['3'][$i]))
  363. {
  364. $period = '.';
  365. $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
  366. }
  367. $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
  368. }
  369. }
  370. }
  371. return $str;
  372. }
  373. }
  374. // ------------------------------------------------------------------------
  375. /**
  376. * Prep URL
  377. *
  378. * Simply adds the http:// part if missing
  379. *
  380. * @access public
  381. * @param string the URL
  382. * @return string
  383. */
  384. if ( ! function_exists('prep_url'))
  385. {
  386. function prep_url($str = '')
  387. {
  388. if ($str == 'http://' OR $str == '')
  389. {
  390. return '';
  391. }
  392. if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://')
  393. {
  394. $str = 'http://'.$str;
  395. }
  396. return $str;
  397. }
  398. }
  399. // ------------------------------------------------------------------------
  400. /**
  401. * Create URL Title
  402. *
  403. * Takes a "title" string as input and creates a
  404. * human-friendly URL string with either a dash
  405. * or an underscore as the word separator.
  406. *
  407. * @access public
  408. * @param string the string
  409. * @param string the separator: dash, or underscore
  410. * @return string
  411. */
  412. if ( ! function_exists('url_title'))
  413. {
  414. function url_title($str, $separator = 'dash', $lowercase = FALSE)
  415. {
  416. if ($separator == 'dash')
  417. {
  418. $search = '_';
  419. $replace = '-';
  420. }
  421. else
  422. {
  423. $search = '-';
  424. $replace = '_';
  425. }
  426. $trans = array(
  427. '&\#\d+?;' => '',
  428. '&\S+?;' => '',
  429. '\s+' => $replace,
  430. '[^a-z0-9\-\._]' => '',
  431. $replace.'+' => $replace,
  432. $replace.'$' => $replace,
  433. '^'.$replace => $replace,
  434. '\.+$' => ''
  435. );
  436. $str = strip_tags($str);
  437. foreach ($trans as $key => $val)
  438. {
  439. $str = preg_replace("#".$key."#i", $val, $str);
  440. }
  441. if ($lowercase === TRUE)
  442. {
  443. $str = strtolower($str);
  444. }
  445. return trim(stripslashes($str));
  446. }
  447. }
  448. // ------------------------------------------------------------------------
  449. /**
  450. * Header Redirect
  451. *
  452. * Header redirect in two flavors
  453. * For very fine grained control over headers, you could use the Output
  454. * Library's set_header() function.
  455. *
  456. * @access public
  457. * @param string the URL
  458. * @param string the method: location or redirect
  459. * @return string
  460. */
  461. if ( ! function_exists('redirect'))
  462. {
  463. function redirect($uri = '', $method = 'location', $http_response_code = 302)
  464. {
  465. if ( ! preg_match('#^https?://#i', $uri))
  466. {
  467. $uri = site_url($uri);
  468. }
  469. switch($method)
  470. {
  471. case 'refresh' : header("Refresh:0;url=".$uri);
  472. break;
  473. default : header("Location: ".$uri, TRUE, $http_response_code);
  474. break;
  475. }
  476. exit;
  477. }
  478. }
  479. // ------------------------------------------------------------------------
  480. /**
  481. * Parse out the attributes
  482. *
  483. * Some of the functions use this
  484. *
  485. * @access private
  486. * @param array
  487. * @param bool
  488. * @return string
  489. */
  490. if ( ! function_exists('_parse_attributes'))
  491. {
  492. function _parse_attributes($attributes, $javascript = FALSE)
  493. {
  494. if (is_string($attributes))
  495. {
  496. return ($attributes != '') ? ' '.$attributes : '';
  497. }
  498. $att = '';
  499. foreach ($attributes as $key => $val)
  500. {
  501. if ($javascript == TRUE)
  502. {
  503. $att .= $key . '=' . $val . ',';
  504. }
  505. else
  506. {
  507. $att .= ' ' . $key . '="' . $val . '"';
  508. }
  509. }
  510. if ($javascript == TRUE AND $att != '')
  511. {
  512. $att = substr($att, 0, -1);
  513. }
  514. return $att;
  515. }
  516. }
  517. /* End of file url_helper.php */
  518. /* Location: ./system/helpers/url_helper.php */