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