PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/url_helper.php

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