Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter URL Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/url_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | if ( ! function_exists('site_url')) |
| 41 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 42 | /** |
| 43 | * Site URL |
| 44 | * |
| 45 | * Create a local URL based on your basepath. Segments can be passed via the |
| 46 | * first parameter either as a string or an array. |
| 47 | * |
| 48 | * @param string |
| 49 | * @return string |
| 50 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | function site_url($uri = '') |
| 52 | { |
| 53 | $CI =& get_instance(); |
| 54 | return $CI->config->site_url($uri); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | if ( ! function_exists('base_url')) |
| 61 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 62 | /** |
| 63 | * Base URL |
| 64 | * |
| 65 | * Create a local URL based on your basepath. |
| 66 | * Segments can be passed in as a string or an array, same as site_url |
| 67 | * or a URL to a file can be passed in, e.g. to an image file. |
| 68 | * |
| 69 | * @param string |
| 70 | * @return string |
| 71 | */ |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 72 | function base_url($uri = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
| 74 | $CI =& get_instance(); |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 75 | return $CI->config->base_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | // ------------------------------------------------------------------------ |
| 80 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | if ( ! function_exists('current_url')) |
| 82 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 83 | /** |
| 84 | * Current URL |
| 85 | * |
| 86 | * Returns the full URL (including segments) of the page where this |
| 87 | * function is placed |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | function current_url() |
| 92 | { |
| 93 | $CI =& get_instance(); |
| 94 | return $CI->config->site_url($CI->uri->uri_string()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // ------------------------------------------------------------------------ |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 99 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | if ( ! function_exists('uri_string')) |
| 101 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 102 | /** |
| 103 | * URL String |
| 104 | * |
| 105 | * Returns the URI segments. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | function uri_string() |
| 110 | { |
| 111 | $CI =& get_instance(); |
| 112 | return $CI->uri->uri_string(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | if ( ! function_exists('index_page')) |
| 119 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 120 | /** |
| 121 | * Index page |
| 122 | * |
| 123 | * Returns the "index_page" from your config file |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | function index_page() |
| 128 | { |
| 129 | $CI =& get_instance(); |
| 130 | return $CI->config->item('index_page'); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
| 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | if ( ! function_exists('anchor')) |
| 137 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 138 | /** |
| 139 | * Anchor Link |
| 140 | * |
| 141 | * Creates an anchor based on the local URL. |
| 142 | * |
| 143 | * @param string the URL |
| 144 | * @param string the link title |
| 145 | * @param mixed any attributes |
| 146 | * @return string |
| 147 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | function anchor($uri = '', $title = '', $attributes = '') |
| 149 | { |
| 150 | $title = (string) $title; |
| 151 | |
| 152 | if ( ! is_array($uri)) |
| 153 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 154 | $site_url = preg_match('!^\w+://! i', $uri) ? $uri : site_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | { |
| 158 | $site_url = site_url($uri); |
| 159 | } |
| 160 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 161 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
| 163 | $title = $site_url; |
| 164 | } |
| 165 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 166 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | { |
| 168 | $attributes = _parse_attributes($attributes); |
| 169 | } |
| 170 | |
| 171 | return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>'; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // ------------------------------------------------------------------------ |
| 176 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | if ( ! function_exists('anchor_popup')) |
| 178 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Anchor Link - Pop-up version |
| 181 | * |
| 182 | * Creates an anchor based on the local URL. The link |
| 183 | * opens a new window based on the attributes specified. |
| 184 | * |
| 185 | * @param string the URL |
| 186 | * @param string the link title |
| 187 | * @param mixed any attributes |
| 188 | * @return string |
| 189 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | function anchor_popup($uri = '', $title = '', $attributes = FALSE) |
| 191 | { |
| 192 | $title = (string) $title; |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 193 | $site_url = preg_match('!^\w+://! i', $uri) ? $uri : site_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 195 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | { |
| 197 | $title = $site_url; |
| 198 | } |
| 199 | |
| 200 | if ($attributes === FALSE) |
| 201 | { |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 202 | return '<a href="javascript:void(0);" onclick="window.open(\''.$site_url."', '_blank');\">".$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if ( ! is_array($attributes)) |
| 206 | { |
Andrey Andreev | f0a8410 | 2012-06-16 20:52:20 +0300 | [diff] [blame^] | 207 | $attributes = array($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val) |
| 211 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 212 | $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | unset($attributes[$key]); |
| 214 | } |
| 215 | |
Andrey Andreev | f0a8410 | 2012-06-16 20:52:20 +0300 | [diff] [blame^] | 216 | $attributes = empty($attributes) ? '' : _parse_attributes($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 218 | return '<a href="javascript:void(0);" onclick="window.open(\''.$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"".$attributes.'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | // ------------------------------------------------------------------------ |
| 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | if ( ! function_exists('mailto')) |
| 225 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 226 | /** |
| 227 | * Mailto Link |
| 228 | * |
| 229 | * @param string the email address |
| 230 | * @param string the link title |
| 231 | * @param mixed any attributes |
| 232 | * @return string |
| 233 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | function mailto($email, $title = '', $attributes = '') |
| 235 | { |
| 236 | $title = (string) $title; |
| 237 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 238 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
| 240 | $title = $email; |
| 241 | } |
| 242 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 243 | return '<a href="mailto:'.$email.'"'._parse_attributes($attributes).'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | // ------------------------------------------------------------------------ |
| 248 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | if ( ! function_exists('safe_mailto')) |
| 250 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 251 | /** |
| 252 | * Encoded Mailto Link |
| 253 | * |
| 254 | * Create a spam-protected mailto link written in Javascript |
| 255 | * |
| 256 | * @param string the email address |
| 257 | * @param string the link title |
| 258 | * @param mixed any attributes |
| 259 | * @return string |
| 260 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | function safe_mailto($email, $title = '', $attributes = '') |
| 262 | { |
| 263 | $title = (string) $title; |
| 264 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 265 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
| 267 | $title = $email; |
| 268 | } |
| 269 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 270 | $x = str_split('<a href="mailto:', 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 272 | for ($i = 0, $l = strlen($email); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 274 | $x[] = '|'.ord($email[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | $x[] = '"'; |
| 278 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 279 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | { |
| 281 | if (is_array($attributes)) |
| 282 | { |
| 283 | foreach ($attributes as $key => $val) |
| 284 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 285 | $x[] = ' '.$key.'="'; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 286 | for ($i = 0, $l = strlen($val); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 288 | $x[] = '|'.ord($val[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | } |
| 290 | $x[] = '"'; |
| 291 | } |
| 292 | } |
| 293 | else |
| 294 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 295 | for ($i = 0, $l = strlen($attributes); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 297 | $x[] = $attributes[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | $x[] = '>'; |
| 303 | |
| 304 | $temp = array(); |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 305 | for ($i = 0, $l = strlen($title); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | { |
| 307 | $ordinal = ord($title[$i]); |
| 308 | |
| 309 | if ($ordinal < 128) |
| 310 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 311 | $x[] = '|'.$ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | } |
| 313 | else |
| 314 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 315 | if (count($temp) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
| 317 | $count = ($ordinal < 224) ? 2 : 3; |
| 318 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 319 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | $temp[] = $ordinal; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 321 | if (count($temp) === $count) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 323 | $number = ($count === 3) |
| 324 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 325 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
| 326 | $x[] = '|'.$number; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | $count = 1; |
| 328 | $temp = array(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; |
| 334 | |
| 335 | $x = array_reverse($x); |
| 336 | ob_start(); |
| 337 | |
| 338 | ?><script type="text/javascript"> |
| 339 | //<![CDATA[ |
| 340 | var l=new Array(); |
| 341 | <?php |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 342 | for ($i = 0, $c = count($x); $i < $c; $i++) { ?>l[<?php echo $i; ?>]='<?php echo $x[$i]; ?>';<?php } ?> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | |
| 344 | for (var i = l.length-1; i >= 0; i=i-1){ |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 345 | if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | else document.write(unescape(l[i]));} |
| 347 | //]]> |
| 348 | </script><?php |
| 349 | |
| 350 | $buffer = ob_get_contents(); |
| 351 | ob_end_clean(); |
| 352 | return $buffer; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // ------------------------------------------------------------------------ |
| 357 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | if ( ! function_exists('auto_link')) |
| 359 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 360 | /** |
| 361 | * Auto-linker |
| 362 | * |
| 363 | * Automatically links URL and Email addresses. |
| 364 | * Note: There's a bit of extra code here to deal with |
| 365 | * URLs or emails that end in a period. We'll strip these |
| 366 | * off and add them after the link. |
| 367 | * |
| 368 | * @param string the string |
| 369 | * @param string the type: email, url, or both |
| 370 | * @param bool whether to create pop-up links |
| 371 | * @return string |
| 372 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | function auto_link($str, $type = 'both', $popup = FALSE) |
| 374 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 375 | if ($type !== 'email' && preg_match_all('#(^|\s|\(|\b)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i', $str, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 377 | $pop = ($popup) ? ' target="_blank" ' : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 379 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
| 380 | { |
| 381 | if (preg_match('|\.$|', $matches[6][$i])) |
| 382 | { |
| 383 | $period = '.'; |
| 384 | $matches[6][$i] = substr($matches[6][$i], 0, -1); |
| 385 | } |
| 386 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
| 388 | $period = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | } |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 390 | |
| 391 | $str = str_replace($matches[0][$i], |
| 392 | $matches[1][$i].'<a href="http'.$matches[4][$i].'://' |
| 393 | .$matches[5][$i].$matches[6][$i].'"'.$pop.'>http' |
| 394 | .$matches[4][$i].'://'.$matches[5][$i] |
| 395 | .$matches[6][$i].'</a>'.$period, |
| 396 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 400 | if ($type !== 'url' && preg_match_all('/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i', $str, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 402 | for ($i = 0, $c = count($matches); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 404 | if (preg_match('|\.$|', $matches[3][$i])) |
| 405 | { |
| 406 | $period = '.'; |
| 407 | $matches[3][$i] = substr($matches[3][$i], 0, -1); |
| 408 | } |
| 409 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
| 411 | $period = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | } |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 413 | |
| 414 | $str = str_replace($matches[0][$i], safe_mailto($matches[1][$i].'@'.$matches[2][$i].'.'.$matches[3][$i]).$period, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | return $str; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // ------------------------------------------------------------------------ |
| 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | if ( ! function_exists('prep_url')) |
| 425 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 426 | /** |
| 427 | * Prep URL |
| 428 | * |
| 429 | * Simply adds the http:// part if no scheme is included |
| 430 | * |
| 431 | * @param string the URL |
| 432 | * @return string |
| 433 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | function prep_url($str = '') |
| 435 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 436 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | { |
| 438 | return ''; |
| 439 | } |
| 440 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 441 | $url = parse_url($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 443 | if ( ! $url OR ! isset($url['scheme'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 445 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return $str; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // ------------------------------------------------------------------------ |
| 453 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | if ( ! function_exists('url_title')) |
| 455 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 456 | /** |
| 457 | * Create URL Title |
| 458 | * |
| 459 | * Takes a "title" string as input and creates a |
| 460 | * human-friendly URL string with a "separator" string |
| 461 | * as the word separator. |
| 462 | * |
| 463 | * @param string the string |
| 464 | * @param string the separator |
| 465 | * @param bool |
| 466 | * @return string |
| 467 | */ |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 468 | function url_title($str, $separator = '-', $lowercase = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 470 | if ($separator === 'dash') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 472 | $separator = '-'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 474 | elseif ($separator === 'underscore') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 476 | $separator = '_'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 478 | |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 479 | $q_separator = preg_quote($separator); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | |
| 481 | $trans = array( |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 482 | '&.+?;' => '', |
| 483 | '[^a-z0-9 _-]' => '', |
| 484 | '\s+' => $separator, |
| 485 | '('.$q_separator.')+' => $separator |
| 486 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | |
| 488 | $str = strip_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | foreach ($trans as $key => $val) |
| 490 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 491 | $str = preg_replace('#'.$key.'#i', $val, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Derek Jones | 40a2fc8 | 2008-12-09 19:41:25 +0000 | [diff] [blame] | 494 | if ($lowercase === TRUE) |
| 495 | { |
| 496 | $str = strtolower($str); |
| 497 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 498 | |
Phil Sturgeon | a2bd363 | 2012-03-04 15:32:58 +0000 | [diff] [blame] | 499 | return trim(trim($str, $separator)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | // ------------------------------------------------------------------------ |
| 504 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | if ( ! function_exists('redirect')) |
| 506 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 507 | /** |
| 508 | * Header Redirect |
| 509 | * |
| 510 | * Header redirect in two flavors |
| 511 | * For very fine grained control over headers, you could use the Output |
| 512 | * Library's set_header() function. |
| 513 | * |
| 514 | * @param string the URL |
| 515 | * @param string the method: location or refresh |
| 516 | * @param int |
| 517 | * @return string |
| 518 | */ |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 519 | function redirect($uri = '', $method = 'auto', $http_response_code = 302) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | { |
Derek Jones | 534be03 | 2009-02-10 18:47:47 +0000 | [diff] [blame] | 521 | if ( ! preg_match('#^https?://#i', $uri)) |
| 522 | { |
| 523 | $uri = site_url($uri); |
| 524 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 525 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 526 | // IIS environment likely? Use 'refresh' for better compatibility |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 527 | if (DIRECTORY_SEPARATOR !== '/' && $method === 'auto') |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 528 | { |
| 529 | $method = 'refresh'; |
| 530 | } |
| 531 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 532 | switch($method) |
| 533 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 534 | case 'refresh': |
| 535 | header('Refresh:0;url='.$uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | break; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 537 | default: |
| 538 | header('Location: '.$uri, TRUE, $http_response_code); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | exit; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // ------------------------------------------------------------------------ |
| 546 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | if ( ! function_exists('_parse_attributes')) |
| 548 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 549 | /** |
| 550 | * Parse out the attributes |
| 551 | * |
| 552 | * Some of the functions use this |
| 553 | * |
| 554 | * @param array |
| 555 | * @param bool |
| 556 | * @return string |
| 557 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | function _parse_attributes($attributes, $javascript = FALSE) |
| 559 | { |
| 560 | if (is_string($attributes)) |
| 561 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 562 | return ($attributes !== '') ? ' '.$attributes : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | $att = ''; |
| 566 | foreach ($attributes as $key => $val) |
| 567 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 568 | if ($javascript === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 570 | $att .= $key.'='.$val.','; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | } |
| 572 | else |
| 573 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 574 | $att .= ' '.$key.'="'.$val.'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 578 | if ($javascript === TRUE && $att !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 579 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 580 | return substr($att, 0, -1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | return $att; |
| 584 | } |
| 585 | } |
| 586 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | /* End of file url_helper.php */ |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 588 | /* Location: ./system/helpers/url_helper.php */ |