PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/url_helper.php

https://gitlab.com/RikaPM/manik
PHP | 595 lines | 330 code | 77 blank | 188 comment | 77 complexity | cf0bdedd7755c662066a2a1805be5e70 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 EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter URL Helpers
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/url_helper.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Site URL
  29. *
  30. * Create a local URL based on your basepath. Segments can be passed via the
  31. * first parameter either as a string or an array.
  32. *
  33. * @access public
  34. * @param string
  35. * @return string
  36. */
  37. if ( ! function_exists('site_url'))
  38. {
  39. function site_url($uri = '')
  40. {
  41. $CI =& get_instance();
  42. return $CI->config->site_url($uri);
  43. }
  44. }
  45. // ------------------------------------------------------------------------
  46. /**
  47. * Base URL
  48. *
  49. * Create a local URL based on your basepath.
  50. * Segments can be passed in as a string or an array, same as site_url
  51. * or a URL to a file can be passed in, e.g. to an image file.
  52. *
  53. * @access public
  54. * @param string
  55. * @return string
  56. */
  57. if ( ! function_exists('base_url'))
  58. {
  59. function base_url($uri = '')
  60. {
  61. $CI =& get_instance();
  62. return $CI->config->base_url($uri);
  63. }
  64. }
  65. // ------------------------------------------------------------------------
  66. /**
  67. * Current URL
  68. *
  69. * Returns the full URL (including segments) of the page where this
  70. * function is placed
  71. *
  72. * @access public
  73. * @return string
  74. */
  75. if ( ! function_exists('current_url'))
  76. {
  77. function current_url()
  78. {
  79. $CI =& get_instance();
  80. return $CI->config->site_url($CI->uri->uri_string());
  81. }
  82. }
  83. // ------------------------------------------------------------------------
  84. /**
  85. * URL String
  86. *
  87. * Returns the URI segments.
  88. *
  89. * @access public
  90. * @return string
  91. */
  92. if ( ! function_exists('uri_string'))
  93. {
  94. function uri_string()
  95. {
  96. $CI =& get_instance();
  97. return $CI->uri->uri_string();
  98. }
  99. }
  100. // ------------------------------------------------------------------------
  101. /**
  102. * Index page
  103. *
  104. * Returns the "index_page" from your config file
  105. *
  106. * @access public
  107. * @return string
  108. */
  109. if ( ! function_exists('index_page'))
  110. {
  111. function index_page()
  112. {
  113. $CI =& get_instance();
  114. return $CI->config->item('index_page');
  115. }
  116. }
  117. // ------------------------------------------------------------------------
  118. /**
  119. * Anchor Link
  120. *
  121. * Creates an anchor based on the local URL.
  122. *
  123. * @access public
  124. * @param string the URL
  125. * @param string the link title
  126. * @param mixed any attributes
  127. * @return string
  128. */
  129. if ( ! function_exists('anchor'))
  130. {
  131. function anchor($uri = '', $title = '', $attributes = '')
  132. {
  133. $title = (string) $title;
  134. if ( ! is_array($uri))
  135. {
  136. $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
  137. }
  138. else
  139. {
  140. $site_url = site_url($uri);
  141. }
  142. if ($title == '')
  143. {
  144. $title = $site_url;
  145. }
  146. if ($attributes != '')
  147. {
  148. $attributes = _parse_attributes($attributes);
  149. }
  150. return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  151. }
  152. }
  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. * @access public
  161. * @param string the URL
  162. * @param string the link title
  163. * @param mixed any attributes
  164. * @return string
  165. */
  166. if ( ! function_exists('anchor_popup'))
  167. {
  168. function anchor_popup($uri = '', $title = '', $attributes = FALSE)
  169. {
  170. $title = (string) $title;
  171. $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
  172. if ($title == '')
  173. {
  174. $title = $site_url;
  175. }
  176. if ($attributes === FALSE)
  177. {
  178. return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>";
  179. }
  180. if ( ! is_array($attributes))
  181. {
  182. $attributes = array();
  183. }
  184. foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
  185. {
  186. $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
  187. unset($attributes[$key]);
  188. }
  189. if ($attributes != '')
  190. {
  191. $attributes = _parse_attributes($attributes);
  192. }
  193. return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>";
  194. }
  195. }
  196. // ------------------------------------------------------------------------
  197. /**
  198. * Mailto Link
  199. *
  200. * @access public
  201. * @param string the email address
  202. * @param string the link title
  203. * @param mixed any attributes
  204. * @return string
  205. */
  206. if ( ! function_exists('mailto'))
  207. {
  208. function mailto($email, $title = '', $attributes = '')
  209. {
  210. $title = (string) $title;
  211. if ($title == "")
  212. {
  213. $title = $email;
  214. }
  215. $attributes = _parse_attributes($attributes);
  216. return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>';
  217. }
  218. }
  219. // ------------------------------------------------------------------------
  220. /**
  221. * Encoded Mailto Link
  222. *
  223. * Create a spam-protected mailto link written in Javascript
  224. *
  225. * @access public
  226. * @param string the email address
  227. * @param string the link title
  228. * @param mixed any attributes
  229. * @return string
  230. */
  231. if ( ! function_exists('safe_mailto'))
  232. {
  233. function safe_mailto($email, $title = '', $attributes = '')
  234. {
  235. $title = (string) $title;
  236. if ($title == "")
  237. {
  238. $title = $email;
  239. }
  240. for ($i = 0; $i < 16; $i++)
  241. {
  242. $x[] = substr('<a href="mailto:', $i, 1);
  243. }
  244. for ($i = 0; $i < strlen($email); $i++)
  245. {
  246. $x[] = "|".ord(substr($email, $i, 1));
  247. }
  248. $x[] = '"';
  249. if ($attributes != '')
  250. {
  251. if (is_array($attributes))
  252. {
  253. foreach ($attributes as $key => $val)
  254. {
  255. $x[] = ' '.$key.'="';
  256. for ($i = 0; $i < strlen($val); $i++)
  257. {
  258. $x[] = "|".ord(substr($val, $i, 1));
  259. }
  260. $x[] = '"';
  261. }
  262. }
  263. else
  264. {
  265. for ($i = 0; $i < strlen($attributes); $i++)
  266. {
  267. $x[] = substr($attributes, $i, 1);
  268. }
  269. }
  270. }
  271. $x[] = '>';
  272. $temp = array();
  273. for ($i = 0; $i < strlen($title); $i++)
  274. {
  275. $ordinal = ord($title[$i]);
  276. if ($ordinal < 128)
  277. {
  278. $x[] = "|".$ordinal;
  279. }
  280. else
  281. {
  282. if (count($temp) == 0)
  283. {
  284. $count = ($ordinal < 224) ? 2 : 3;
  285. }
  286. $temp[] = $ordinal;
  287. if (count($temp) == $count)
  288. {
  289. $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
  290. $x[] = "|".$number;
  291. $count = 1;
  292. $temp = array();
  293. }
  294. }
  295. }
  296. $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
  297. $x = array_reverse($x);
  298. ob_start();
  299. ?><script type="text/javascript">
  300. //<![CDATA[
  301. var l=new Array();
  302. <?php
  303. $i = 0;
  304. foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
  305. for (var i = l.length-1; i >= 0; i=i-1){
  306. if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
  307. else document.write(unescape(l[i]));}
  308. //]]>
  309. </script><?php
  310. $buffer = ob_get_contents();
  311. ob_end_clean();
  312. return $buffer;
  313. }
  314. }
  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. * @access public
  325. * @param string the string
  326. * @param string the type: email, url, or both
  327. * @param bool whether to create pop-up links
  328. * @return string
  329. */
  330. if ( ! function_exists('auto_link'))
  331. {
  332. function auto_link($str, $type = 'both', $popup = FALSE)
  333. {
  334. if ($type != 'email')
  335. {
  336. if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
  337. {
  338. $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
  339. for ($i = 0; $i < count($matches['0']); $i++)
  340. {
  341. $period = '';
  342. if (preg_match("|\.$|", $matches['6'][$i]))
  343. {
  344. $period = '.';
  345. $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
  346. }
  347. $str = str_replace($matches['0'][$i],
  348. $matches['1'][$i].'<a href="http'.
  349. $matches['4'][$i].'://'.
  350. $matches['5'][$i].
  351. $matches['6'][$i].'"'.$pop.'>http'.
  352. $matches['4'][$i].'://'.
  353. $matches['5'][$i].
  354. $matches['6'][$i].'</a>'.
  355. $period, $str);
  356. }
  357. }
  358. }
  359. if ($type != 'url')
  360. {
  361. if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches))
  362. {
  363. for ($i = 0; $i < count($matches['0']); $i++)
  364. {
  365. $period = '';
  366. if (preg_match("|\.$|", $matches['3'][$i]))
  367. {
  368. $period = '.';
  369. $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
  370. }
  371. $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str);
  372. }
  373. }
  374. }
  375. return $str;
  376. }
  377. }
  378. // ------------------------------------------------------------------------
  379. /**
  380. * Prep URL
  381. *
  382. * Simply adds the http:// part if no scheme is included
  383. *
  384. * @access public
  385. * @param string the URL
  386. * @return string
  387. */
  388. if ( ! function_exists('prep_url'))
  389. {
  390. function prep_url($str = '')
  391. {
  392. if ($str == 'http://' OR $str == '')
  393. {
  394. return '';
  395. }
  396. $url = parse_url($str);
  397. if ( ! $url OR ! isset($url['scheme']))
  398. {
  399. $str = 'http://'.$str;
  400. }
  401. return $str;
  402. }
  403. }
  404. // ------------------------------------------------------------------------
  405. /**
  406. * Create URL Title
  407. *
  408. * Takes a "title" string as input and creates a
  409. * human-friendly URL string with a "separator" string
  410. * as the word separator.
  411. *
  412. * @access public
  413. * @param string the string
  414. * @param string the separator
  415. * @return string
  416. */
  417. if ( ! function_exists('url_title'))
  418. {
  419. function url_title($str, $separator = '-', $lowercase = FALSE)
  420. {
  421. if ($separator == 'dash')
  422. {
  423. $separator = '-';
  424. }
  425. else if ($separator == 'underscore')
  426. {
  427. $separator = '_';
  428. }
  429. $q_separator = preg_quote($separator);
  430. $trans = array(
  431. '&.+?;' => '',
  432. '[^a-z0-9 _-]' => '',
  433. '\s+' => $separator,
  434. '('.$q_separator.')+' => $separator
  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($str, $separator);
  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 */