PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/system/helpers/url_helper.php

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