PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/url_helper.php

https://bitbucket.org/masangga/laperbanget
PHP | 594 lines | 330 code | 77 blank | 187 comment | 77 complexity | fd6d0e6dd1815972111cebd8559ac952 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. * Create a local URL based on your basepath.
  49. * Segments can be passed in as a string or an array, same as site_url
  50. * or a URL to a file can be passed in, e.g. to an image file.
  51. *
  52. * @access public
  53. * @param string
  54. * @return string
  55. */
  56. if ( ! function_exists('base_url'))
  57. {
  58. function base_url($uri = '')
  59. {
  60. $CI =& get_instance();
  61. return $CI->config->base_url($uri);
  62. }
  63. }
  64. // ------------------------------------------------------------------------
  65. /**
  66. * Current URL
  67. *
  68. * Returns the full URL (including segments) of the page where this
  69. * function is placed
  70. *
  71. * @access public
  72. * @return string
  73. */
  74. if ( ! function_exists('current_url'))
  75. {
  76. function current_url()
  77. {
  78. $CI =& get_instance();
  79. return $CI->config->site_url($CI->uri->uri_string());
  80. }
  81. }
  82. // ------------------------------------------------------------------------
  83. /**
  84. * URL String
  85. *
  86. * Returns the URI segments.
  87. *
  88. * @access public
  89. * @return string
  90. */
  91. if ( ! function_exists('uri_string'))
  92. {
  93. function uri_string()
  94. {
  95. $CI =& get_instance();
  96. return $CI->uri->uri_string();
  97. }
  98. }
  99. // ------------------------------------------------------------------------
  100. /**
  101. * Index page
  102. *
  103. * Returns the "index_page" from your config file
  104. *
  105. * @access public
  106. * @return string
  107. */
  108. if ( ! function_exists('index_page'))
  109. {
  110. function index_page()
  111. {
  112. $CI =& get_instance();
  113. return $CI->config->item('index_page');
  114. }
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Anchor Link
  119. *
  120. * Creates an anchor based on the local URL.
  121. *
  122. * @access public
  123. * @param string the URL
  124. * @param string the link title
  125. * @param mixed any attributes
  126. * @return string
  127. */
  128. if ( ! function_exists('anchor'))
  129. {
  130. function anchor($uri = '', $title = '', $attributes = '')
  131. {
  132. $title = (string) $title;
  133. if ( ! is_array($uri))
  134. {
  135. $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
  136. }
  137. else
  138. {
  139. $site_url = site_url($uri);
  140. }
  141. if ($title == '')
  142. {
  143. $title = $site_url;
  144. }
  145. if ($attributes != '')
  146. {
  147. $attributes = _parse_attributes($attributes);
  148. }
  149. return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  150. }
  151. }
  152. // ------------------------------------------------------------------------
  153. /**
  154. * Anchor Link - Pop-up version
  155. *
  156. * Creates an anchor based on the local URL. The link
  157. * opens a new window based on the attributes specified.
  158. *
  159. * @access public
  160. * @param string the URL
  161. * @param string the link title
  162. * @param mixed any attributes
  163. * @return string
  164. */
  165. if ( ! function_exists('anchor_popup'))
  166. {
  167. function anchor_popup($uri = '', $title = '', $attributes = FALSE)
  168. {
  169. $title = (string) $title;
  170. $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
  171. if ($title == '')
  172. {
  173. $title = $site_url;
  174. }
  175. if ($attributes === FALSE)
  176. {
  177. return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
  178. }
  179. if ( ! is_array($attributes))
  180. {
  181. $attributes = array();
  182. }
  183. foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
  184. {
  185. $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
  186. unset($attributes[$key]);
  187. }
  188. if ($attributes != '')
  189. {
  190. $attributes = _parse_attributes($attributes);
  191. }
  192. return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
  193. }
  194. }
  195. // ------------------------------------------------------------------------
  196. /**
  197. * Mailto Link
  198. *
  199. * @access public
  200. * @param string the email address
  201. * @param string the link title
  202. * @param mixed any attributes
  203. * @return string
  204. */
  205. if ( ! function_exists('mailto'))
  206. {
  207. function mailto($email, $title = '', $attributes = '')
  208. {
  209. $title = (string) $title;
  210. if ($title == "")
  211. {
  212. $title = $email;
  213. }
  214. $attributes = _parse_attributes($attributes);
  215. return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
  216. }
  217. }
  218. // ------------------------------------------------------------------------
  219. /**
  220. * Encoded Mailto Link
  221. *
  222. * Create a spam-protected mailto link written in Javascript
  223. *
  224. * @access public
  225. * @param string the email address
  226. * @param string the link title
  227. * @param mixed any attributes
  228. * @return string
  229. */
  230. if ( ! function_exists('safe_mailto'))
  231. {
  232. function safe_mailto($email, $title = '', $attributes = '')
  233. {
  234. $title = (string) $title;
  235. if ($title == "")
  236. {
  237. $title = $email;
  238. }
  239. for ($i = 0; $i < 16; $i++)
  240. {
  241. $x[] = substr('<a href="mailto:', $i, 1);
  242. }
  243. for ($i = 0; $i < strlen($email); $i++)
  244. {
  245. $x[] = "|".ord(substr($email, $i, 1));
  246. }
  247. $x[] = '"';
  248. if ($attributes != '')
  249. {
  250. if (is_array($attributes))
  251. {
  252. foreach ($attributes as $key => $val)
  253. {
  254. $x[] = ' '.$key.'="';
  255. for ($i = 0; $i < strlen($val); $i++)
  256. {
  257. $x[] = "|".ord(substr($val, $i, 1));
  258. }
  259. $x[] = '"';
  260. }
  261. }
  262. else
  263. {
  264. for ($i = 0; $i < strlen($attributes); $i++)
  265. {
  266. $x[] = substr($attributes, $i, 1);
  267. }
  268. }
  269. }
  270. $x[] = '>';
  271. $temp = array();
  272. for ($i = 0; $i < strlen($title); $i++)
  273. {
  274. $ordinal = ord($title[$i]);
  275. if ($ordinal < 128)
  276. {
  277. $x[] = "|".$ordinal;
  278. }
  279. else
  280. {
  281. if (count($temp) == 0)
  282. {
  283. $count = ($ordinal < 224) ? 2 : 3;
  284. }
  285. $temp[] = $ordinal;
  286. if (count($temp) == $count)
  287. {
  288. $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($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. ob_start();
  298. ?><script type="text/javascript">
  299. //<![CDATA[
  300. var l=new Array();
  301. <?php
  302. $i = 0;
  303. foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
  304. for (var i = l.length-1; i >= 0; i=i-1){
  305. if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
  306. else document.write(unescape(l[i]));}
  307. //]]>
  308. </script><?php
  309. $buffer = ob_get_contents();
  310. ob_end_clean();
  311. return $buffer;
  312. }
  313. }
  314. // ------------------------------------------------------------------------
  315. /**
  316. * Auto-linker
  317. *
  318. * Automatically links URL and Email addresses.
  319. * Note: There's a bit of extra code here to deal with
  320. * URLs or emails that end in a period. We'll strip these
  321. * off and add them after the link.
  322. *
  323. * @access public
  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. if ( ! function_exists('auto_link'))
  330. {
  331. function auto_link($str, $type = 'both', $popup = FALSE)
  332. {
  333. if ($type != 'email')
  334. {
  335. if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
  336. {
  337. $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
  338. for ($i = 0; $i < count($matches['0']); $i++)
  339. {
  340. $period = '';
  341. if (preg_match("|\.$|", $matches['6'][$i]))
  342. {
  343. $period = '.';
  344. $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
  345. }
  346. $str = str_replace($matches['0'][$i],
  347. $matches['1'][$i].'<a href="http'.
  348. $matches['4'][$i].'://'.
  349. $matches['5'][$i].
  350. $matches['6'][$i].'"'.$pop.'>http'.
  351. $matches['4'][$i].'://'.
  352. $matches['5'][$i].
  353. $matches['6'][$i].'</a>'.
  354. $period, $str);
  355. }
  356. }
  357. }
  358. if ($type != 'url')
  359. {
  360. if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
  361. {
  362. for ($i = 0; $i < count($matches['0']); $i++)
  363. {
  364. $period = '';
  365. if (preg_match("|\.$|", $matches['3'][$i]))
  366. {
  367. $period = '.';
  368. $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
  369. }
  370. $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
  371. }
  372. }
  373. }
  374. return $str;
  375. }
  376. }
  377. // ------------------------------------------------------------------------
  378. /**
  379. * Prep URL
  380. *
  381. * Simply adds the http:// part if no scheme is included
  382. *
  383. * @access public
  384. * @param string the URL
  385. * @return string
  386. */
  387. if ( ! function_exists('prep_url'))
  388. {
  389. function prep_url($str = '')
  390. {
  391. if ($str == 'http://' OR $str == '')
  392. {
  393. return '';
  394. }
  395. $url = parse_url($str);
  396. if ( ! $url OR ! isset($url['scheme']))
  397. {
  398. $str = 'http://'.$str;
  399. }
  400. return $str;
  401. }
  402. }
  403. // ------------------------------------------------------------------------
  404. /**
  405. * Create URL Title
  406. *
  407. * Takes a "title" string as input and creates a
  408. * human-friendly URL string with a "separator" string
  409. * as the word separator.
  410. *
  411. * @access public
  412. * @param string the string
  413. * @param string the separator
  414. * @return string
  415. */
  416. if ( ! function_exists('url_title'))
  417. {
  418. function url_title($str, $separator = '-', $lowercase = FALSE)
  419. {
  420. if ($separator == 'dash')
  421. {
  422. $separator = '-';
  423. }
  424. else if ($separator == 'underscore')
  425. {
  426. $separator = '_';
  427. }
  428. $q_separator = preg_quote($separator);
  429. $trans = array(
  430. '&.+?;' => '',
  431. '[^a-z0-9 _-]' => '',
  432. '\s+' => $separator,
  433. '('.$q_separator.')+' => $separator
  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($str, $separator);
  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 */