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 | { |
| 207 | $attributes = array(); |
| 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 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 216 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | { |
| 218 | $attributes = _parse_attributes($attributes); |
| 219 | } |
| 220 | |
Andrey Andreev | ae31eb5 | 2012-05-17 14:54:15 +0300 | [diff] [blame] | 221 | 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] | 222 | } |
| 223 | } |
| 224 | |
| 225 | // ------------------------------------------------------------------------ |
| 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | if ( ! function_exists('mailto')) |
| 228 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 229 | /** |
| 230 | * Mailto Link |
| 231 | * |
| 232 | * @param string the email address |
| 233 | * @param string the link title |
| 234 | * @param mixed any attributes |
| 235 | * @return string |
| 236 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | function mailto($email, $title = '', $attributes = '') |
| 238 | { |
| 239 | $title = (string) $title; |
| 240 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 241 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | { |
| 243 | $title = $email; |
| 244 | } |
| 245 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 246 | return '<a href="mailto:'.$email.'"'._parse_attributes($attributes).'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | // ------------------------------------------------------------------------ |
| 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | if ( ! function_exists('safe_mailto')) |
| 253 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 254 | /** |
| 255 | * Encoded Mailto Link |
| 256 | * |
| 257 | * Create a spam-protected mailto link written in Javascript |
| 258 | * |
| 259 | * @param string the email address |
| 260 | * @param string the link title |
| 261 | * @param mixed any attributes |
| 262 | * @return string |
| 263 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | function safe_mailto($email, $title = '', $attributes = '') |
| 265 | { |
| 266 | $title = (string) $title; |
| 267 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 268 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
| 270 | $title = $email; |
| 271 | } |
| 272 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 273 | $x = str_split('<a href="mailto:', 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 275 | for ($i = 0, $l = strlen($email); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 277 | $x[] = '|'.ord($email[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | $x[] = '"'; |
| 281 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 282 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | { |
| 284 | if (is_array($attributes)) |
| 285 | { |
| 286 | foreach ($attributes as $key => $val) |
| 287 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 288 | $x[] = ' '.$key.'="'; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 289 | for ($i = 0, $l = strlen($val); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 291 | $x[] = '|'.ord($val[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | } |
| 293 | $x[] = '"'; |
| 294 | } |
| 295 | } |
| 296 | else |
| 297 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 298 | for ($i = 0, $l = strlen($attributes); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 300 | $x[] = $attributes[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | $x[] = '>'; |
| 306 | |
| 307 | $temp = array(); |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 308 | for ($i = 0, $l = strlen($title); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | { |
| 310 | $ordinal = ord($title[$i]); |
| 311 | |
| 312 | if ($ordinal < 128) |
| 313 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 314 | $x[] = '|'.$ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 318 | if (count($temp) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
| 320 | $count = ($ordinal < 224) ? 2 : 3; |
| 321 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | $temp[] = $ordinal; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 324 | if (count($temp) === $count) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 326 | $number = ($count === 3) |
| 327 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 328 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
| 329 | $x[] = '|'.$number; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | $count = 1; |
| 331 | $temp = array(); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; |
| 337 | |
| 338 | $x = array_reverse($x); |
| 339 | ob_start(); |
| 340 | |
| 341 | ?><script type="text/javascript"> |
| 342 | //<![CDATA[ |
| 343 | var l=new Array(); |
| 344 | <?php |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 345 | 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] | 346 | |
| 347 | for (var i = l.length-1; i >= 0; i=i-1){ |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 348 | 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] | 349 | else document.write(unescape(l[i]));} |
| 350 | //]]> |
| 351 | </script><?php |
| 352 | |
| 353 | $buffer = ob_get_contents(); |
| 354 | ob_end_clean(); |
| 355 | return $buffer; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // ------------------------------------------------------------------------ |
| 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | if ( ! function_exists('auto_link')) |
| 362 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 363 | /** |
| 364 | * Auto-linker |
| 365 | * |
| 366 | * Automatically links URL and Email addresses. |
| 367 | * Note: There's a bit of extra code here to deal with |
| 368 | * URLs or emails that end in a period. We'll strip these |
| 369 | * off and add them after the link. |
| 370 | * |
| 371 | * @param string the string |
| 372 | * @param string the type: email, url, or both |
| 373 | * @param bool whether to create pop-up links |
| 374 | * @return string |
| 375 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | function auto_link($str, $type = 'both', $popup = FALSE) |
| 377 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 378 | 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] | 379 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 380 | $pop = ($popup) ? ' target="_blank" ' : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 382 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
| 383 | { |
| 384 | if (preg_match('|\.$|', $matches[6][$i])) |
| 385 | { |
| 386 | $period = '.'; |
| 387 | $matches[6][$i] = substr($matches[6][$i], 0, -1); |
| 388 | } |
| 389 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | { |
| 391 | $period = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 393 | |
| 394 | $str = str_replace($matches[0][$i], |
| 395 | $matches[1][$i].'<a href="http'.$matches[4][$i].'://' |
| 396 | .$matches[5][$i].$matches[6][$i].'"'.$pop.'>http' |
| 397 | .$matches[4][$i].'://'.$matches[5][$i] |
| 398 | .$matches[6][$i].'</a>'.$period, |
| 399 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 403 | 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] | 404 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 405 | for ($i = 0, $c = count($matches); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 407 | if (preg_match('|\.$|', $matches[3][$i])) |
| 408 | { |
| 409 | $period = '.'; |
| 410 | $matches[3][$i] = substr($matches[3][$i], 0, -1); |
| 411 | } |
| 412 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | { |
| 414 | $period = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | } |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 416 | |
| 417 | $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] | 418 | } |
| 419 | } |
| 420 | |
| 421 | return $str; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // ------------------------------------------------------------------------ |
| 426 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | if ( ! function_exists('prep_url')) |
| 428 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 429 | /** |
| 430 | * Prep URL |
| 431 | * |
| 432 | * Simply adds the http:// part if no scheme is included |
| 433 | * |
| 434 | * @param string the URL |
| 435 | * @return string |
| 436 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | function prep_url($str = '') |
| 438 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 439 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | { |
| 441 | return ''; |
| 442 | } |
| 443 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 444 | $url = parse_url($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 446 | if ( ! $url OR ! isset($url['scheme'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 448 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | return $str; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // ------------------------------------------------------------------------ |
| 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | if ( ! function_exists('url_title')) |
| 458 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 459 | /** |
| 460 | * Create URL Title |
| 461 | * |
| 462 | * Takes a "title" string as input and creates a |
| 463 | * human-friendly URL string with a "separator" string |
| 464 | * as the word separator. |
| 465 | * |
| 466 | * @param string the string |
| 467 | * @param string the separator |
| 468 | * @param bool |
| 469 | * @return string |
| 470 | */ |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 471 | function url_title($str, $separator = '-', $lowercase = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 473 | if ($separator === 'dash') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 475 | $separator = '-'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 477 | elseif ($separator === 'underscore') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 479 | $separator = '_'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 481 | |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 482 | $q_separator = preg_quote($separator); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | |
| 484 | $trans = array( |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 485 | '&.+?;' => '', |
| 486 | '[^a-z0-9 _-]' => '', |
| 487 | '\s+' => $separator, |
| 488 | '('.$q_separator.')+' => $separator |
| 489 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | |
| 491 | $str = strip_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | foreach ($trans as $key => $val) |
| 493 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 494 | $str = preg_replace('#'.$key.'#i', $val, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Derek Jones | 40a2fc8 | 2008-12-09 19:41:25 +0000 | [diff] [blame] | 497 | if ($lowercase === TRUE) |
| 498 | { |
| 499 | $str = strtolower($str); |
| 500 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 501 | |
Phil Sturgeon | a2bd363 | 2012-03-04 15:32:58 +0000 | [diff] [blame] | 502 | return trim(trim($str, $separator)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | // ------------------------------------------------------------------------ |
| 507 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | if ( ! function_exists('redirect')) |
| 509 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 510 | /** |
| 511 | * Header Redirect |
| 512 | * |
| 513 | * Header redirect in two flavors |
| 514 | * For very fine grained control over headers, you could use the Output |
| 515 | * Library's set_header() function. |
| 516 | * |
| 517 | * @param string the URL |
| 518 | * @param string the method: location or refresh |
| 519 | * @param int |
| 520 | * @return string |
| 521 | */ |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 522 | function redirect($uri = '', $method = 'auto', $http_response_code = 302) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | { |
Derek Jones | 534be03 | 2009-02-10 18:47:47 +0000 | [diff] [blame] | 524 | if ( ! preg_match('#^https?://#i', $uri)) |
| 525 | { |
| 526 | $uri = site_url($uri); |
| 527 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 528 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 529 | // IIS environment likely? Use 'refresh' for better compatibility |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 530 | if (DIRECTORY_SEPARATOR !== '/' && $method === 'auto') |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 531 | { |
| 532 | $method = 'refresh'; |
| 533 | } |
| 534 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | switch($method) |
| 536 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 537 | case 'refresh': |
| 538 | header('Refresh:0;url='.$uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | break; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 540 | default: |
| 541 | header('Location: '.$uri, TRUE, $http_response_code); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | exit; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // ------------------------------------------------------------------------ |
| 549 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | if ( ! function_exists('_parse_attributes')) |
| 551 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 552 | /** |
| 553 | * Parse out the attributes |
| 554 | * |
| 555 | * Some of the functions use this |
| 556 | * |
| 557 | * @param array |
| 558 | * @param bool |
| 559 | * @return string |
| 560 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | function _parse_attributes($attributes, $javascript = FALSE) |
| 562 | { |
| 563 | if (is_string($attributes)) |
| 564 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 565 | return ($attributes !== '') ? ' '.$attributes : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | $att = ''; |
| 569 | foreach ($attributes as $key => $val) |
| 570 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 571 | if ($javascript === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 573 | $att .= $key.'='.$val.','; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | } |
| 575 | else |
| 576 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 577 | $att .= ' '.$key.'="'.$val.'"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 581 | if ($javascript === TRUE && $att !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 583 | return substr($att, 0, -1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | return $att; |
| 587 | } |
| 588 | } |
| 589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | /* End of file url_helper.php */ |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 591 | /* Location: ./system/helpers/url_helper.php */ |