Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter URL Helpers |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Helpers |
| 34 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/helpers/url_helper.html |
| 37 | */ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | if ( ! function_exists('site_url')) |
| 42 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Site URL |
| 45 | * |
| 46 | * Create a local URL based on your basepath. Segments can be passed via the |
| 47 | * first parameter either as a string or an array. |
| 48 | * |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 49 | * @param string $uri |
| 50 | * @param string $protocol |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 51 | * @return string |
| 52 | */ |
Andrey Andreev | 56c8ca6 | 2013-07-18 03:21:16 +0300 | [diff] [blame] | 53 | function site_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 55 | return get_instance()->config->site_url($uri, $protocol); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | if ( ! function_exists('base_url')) |
| 62 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 63 | /** |
| 64 | * Base URL |
| 65 | * |
| 66 | * Create a local URL based on your basepath. |
| 67 | * Segments can be passed in as a string or an array, same as site_url |
| 68 | * or a URL to a file can be passed in, e.g. to an image file. |
| 69 | * |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 70 | * @param string $uri |
| 71 | * @param string $protocol |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 72 | * @return string |
| 73 | */ |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 74 | function base_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 76 | return get_instance()->config->base_url($uri, $protocol); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | if ( ! function_exists('current_url')) |
| 83 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 84 | /** |
| 85 | * Current URL |
| 86 | * |
| 87 | * Returns the full URL (including segments) of the page where this |
| 88 | * function is placed |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | function current_url() |
| 93 | { |
Andrey Andreev | 4ea76cc | 2014-01-08 21:49:23 +0200 | [diff] [blame] | 94 | $CI =& get_instance(); |
| 95 | return $CI->config->site_url($CI->uri->uri_string()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | // ------------------------------------------------------------------------ |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 100 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | if ( ! function_exists('uri_string')) |
| 102 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 103 | /** |
| 104 | * URL String |
| 105 | * |
| 106 | * Returns the URI segments. |
| 107 | * |
| 108 | * @return string |
| 109 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | function uri_string() |
| 111 | { |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 112 | return get_instance()->uri->uri_string(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | { |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 129 | return get_instance()->config->item('index_page'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | // ------------------------------------------------------------------------ |
| 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | if ( ! function_exists('anchor')) |
| 136 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 137 | /** |
| 138 | * Anchor Link |
| 139 | * |
| 140 | * Creates an anchor based on the local URL. |
| 141 | * |
| 142 | * @param string the URL |
| 143 | * @param string the link title |
| 144 | * @param mixed any attributes |
| 145 | * @return string |
| 146 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | function anchor($uri = '', $title = '', $attributes = '') |
| 148 | { |
| 149 | $title = (string) $title; |
| 150 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame^] | 151 | $site_url = is_array($uri) |
| 152 | ? site_url($uri) |
| 153 | : preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 155 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
| 157 | $title = $site_url; |
| 158 | } |
| 159 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 160 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | { |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 162 | $attributes = _stringify_attributes($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>'; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------------ |
| 170 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | if ( ! function_exists('anchor_popup')) |
| 172 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 173 | /** |
| 174 | * Anchor Link - Pop-up version |
| 175 | * |
| 176 | * Creates an anchor based on the local URL. The link |
| 177 | * opens a new window based on the attributes specified. |
| 178 | * |
| 179 | * @param string the URL |
| 180 | * @param string the link title |
| 181 | * @param mixed any attributes |
| 182 | * @return string |
| 183 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | function anchor_popup($uri = '', $title = '', $attributes = FALSE) |
| 185 | { |
| 186 | $title = (string) $title; |
Aaron Adams | 16800e4 | 2012-12-07 22:39:23 -0500 | [diff] [blame] | 187 | $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 189 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
| 191 | $title = $site_url; |
| 192 | } |
| 193 | |
| 194 | if ($attributes === FALSE) |
| 195 | { |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 196 | 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] | 197 | } |
| 198 | |
| 199 | if ( ! is_array($attributes)) |
| 200 | { |
Andrey Andreev | f0a8410 | 2012-06-16 20:52:20 +0300 | [diff] [blame] | 201 | $attributes = array($attributes); |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 202 | |
| 203 | // Ref: http://www.w3schools.com/jsref/met_win_open.asp |
| 204 | $window_name = '_blank'; |
| 205 | } |
| 206 | elseif ( ! empty($attributes['window_name'])) |
| 207 | { |
| 208 | $window_name = $attributes['window_name']; |
| 209 | unset($attributes['window_name']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 212 | 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] | 213 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 214 | $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | unset($attributes[$key]); |
| 216 | } |
| 217 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 218 | $attributes = _stringify_attributes($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 220 | return '<a href="'.$site_url |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 221 | .'" onclick="window.open(\''.$site_url."', '".$window_name."', '"._stringify_attributes($atts, TRUE)."'); return false;\"" |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 222 | .$attributes.'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // ------------------------------------------------------------------------ |
| 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | if ( ! function_exists('mailto')) |
| 229 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 230 | /** |
| 231 | * Mailto Link |
| 232 | * |
| 233 | * @param string the email address |
| 234 | * @param string the link title |
| 235 | * @param mixed any attributes |
| 236 | * @return string |
| 237 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | function mailto($email, $title = '', $attributes = '') |
| 239 | { |
| 240 | $title = (string) $title; |
| 241 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 242 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
| 244 | $title = $email; |
| 245 | } |
| 246 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 247 | return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | // ------------------------------------------------------------------------ |
| 252 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | if ( ! function_exists('safe_mailto')) |
| 254 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 255 | /** |
| 256 | * Encoded Mailto Link |
| 257 | * |
| 258 | * Create a spam-protected mailto link written in Javascript |
| 259 | * |
| 260 | * @param string the email address |
| 261 | * @param string the link title |
| 262 | * @param mixed any attributes |
| 263 | * @return string |
| 264 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | function safe_mailto($email, $title = '', $attributes = '') |
| 266 | { |
| 267 | $title = (string) $title; |
| 268 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 269 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | { |
| 271 | $title = $email; |
| 272 | } |
| 273 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 274 | $x = str_split('<a href="mailto:', 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 276 | for ($i = 0, $l = strlen($email); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 278 | $x[] = '|'.ord($email[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | $x[] = '"'; |
| 282 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 283 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | { |
| 285 | if (is_array($attributes)) |
| 286 | { |
| 287 | foreach ($attributes as $key => $val) |
| 288 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 289 | $x[] = ' '.$key.'="'; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 290 | for ($i = 0, $l = strlen($val); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 292 | $x[] = '|'.ord($val[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
| 294 | $x[] = '"'; |
| 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 299 | for ($i = 0, $l = strlen($attributes); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 301 | $x[] = $attributes[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | $x[] = '>'; |
| 307 | |
| 308 | $temp = array(); |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 309 | for ($i = 0, $l = strlen($title); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | { |
| 311 | $ordinal = ord($title[$i]); |
| 312 | |
| 313 | if ($ordinal < 128) |
| 314 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 315 | $x[] = '|'.$ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | } |
| 317 | else |
| 318 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 319 | if (count($temp) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | $count = ($ordinal < 224) ? 2 : 3; |
| 322 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | $temp[] = $ordinal; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 325 | if (count($temp) === $count) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 327 | $number = ($count === 3) |
| 328 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 329 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
| 330 | $x[] = '|'.$number; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | $count = 1; |
| 332 | $temp = array(); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; |
| 338 | |
| 339 | $x = array_reverse($x); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 341 | $output = "<script type=\"text/javascript\">\n" |
| 342 | ."\t//<![CDATA[\n" |
| 343 | ."\tvar l=new Array();\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 345 | for ($i = 0, $c = count($x); $i < $c; $i++) |
| 346 | { |
| 347 | $output .= "\tl[".$i."] = '".$x[$i]."';\n"; |
| 348 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 350 | $output .= "\n\tfor (var i = l.length-1; i >= 0; i=i-1) {\n" |
| 351 | ."\t\tif (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");\n" |
| 352 | ."\t\telse document.write(unescape(l[i]));\n" |
| 353 | ."\t}\n" |
| 354 | ."\t//]]>\n" |
| 355 | .'</script>'; |
| 356 | |
| 357 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | // ------------------------------------------------------------------------ |
| 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | if ( ! function_exists('auto_link')) |
| 364 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 365 | /** |
| 366 | * Auto-linker |
| 367 | * |
| 368 | * Automatically links URL and Email addresses. |
| 369 | * Note: There's a bit of extra code here to deal with |
| 370 | * URLs or emails that end in a period. We'll strip these |
| 371 | * off and add them after the link. |
| 372 | * |
| 373 | * @param string the string |
| 374 | * @param string the type: email, url, or both |
| 375 | * @param bool whether to create pop-up links |
| 376 | * @return string |
| 377 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | function auto_link($str, $type = 'both', $popup = FALSE) |
| 379 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 380 | // Find and replace any URLs. |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 381 | if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[^\s()<>;]+\w#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 383 | // Set our target HTML if using popup links. |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 384 | $target = ($popup) ? ' target="_blank"' : ''; |
Eric Roberts | 32a28f5 | 2013-01-19 02:59:14 -0600 | [diff] [blame] | 385 | |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 386 | // We process the links in reverse order (last -> first) so that |
| 387 | // the returned string offsets from preg_match_all() are not |
| 388 | // moved as we add more HTML. |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 389 | foreach (array_reverse($matches) as $match) |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 390 | { |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 391 | // $match[0] is the matched string/link |
| 392 | // $match[1] is either a protocol prefix or 'www.' |
| 393 | // |
| 394 | // With PREG_OFFSET_CAPTURE, both of the above is an array, |
| 395 | // where the actual value is held in [0] and its offset at the [1] index. |
| 396 | $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>'; |
| 397 | $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Eric Roberts | 32a28f5 | 2013-01-19 02:59:14 -0600 | [diff] [blame] | 400 | |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 401 | // Find and replace any emails. |
| 402 | if ($type !== 'url' && preg_match_all('#([\w\.\-\+]+@[a-z0-9\-]+\.[a-z0-9\-\.]+[^[:punct:]\s])#i', $str, $matches, PREG_OFFSET_CAPTURE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 404 | foreach (array_reverse($matches[0]) as $match) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 406 | if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE) |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 407 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 408 | $str = substr_replace($str, safe_mailto($match[0]), $match[1], strlen($match[0])); |
Andrey Andreev | 929e124 | 2012-10-19 10:09:28 +0300 | [diff] [blame] | 409 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
Eric Roberts | 32a28f5 | 2013-01-19 02:59:14 -0600 | [diff] [blame] | 412 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | return $str; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // ------------------------------------------------------------------------ |
| 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | if ( ! function_exists('prep_url')) |
| 420 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 421 | /** |
| 422 | * Prep URL |
| 423 | * |
| 424 | * Simply adds the http:// part if no scheme is included |
| 425 | * |
| 426 | * @param string the URL |
| 427 | * @return string |
| 428 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | function prep_url($str = '') |
| 430 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 431 | if ($str === 'http://' OR $str === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | { |
| 433 | return ''; |
| 434 | } |
| 435 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 436 | $url = parse_url($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | |
Robin Sowell | d2167a0 | 2010-09-14 15:05:42 -0400 | [diff] [blame] | 438 | if ( ! $url OR ! isset($url['scheme'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 440 | return 'http://'.$str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return $str; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // ------------------------------------------------------------------------ |
| 448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | if ( ! function_exists('url_title')) |
| 450 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 451 | /** |
| 452 | * Create URL Title |
| 453 | * |
| 454 | * Takes a "title" string as input and creates a |
| 455 | * human-friendly URL string with a "separator" string |
| 456 | * as the word separator. |
| 457 | * |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 458 | * @todo Remove old 'dash' and 'underscore' usage in 3.1+. |
| 459 | * @param string $str Input string |
| 460 | * @param string $separator Word separator |
| 461 | * (usually '-' or '_') |
| 462 | * @param bool $lowercase Wether to transform the output string to lowercase |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 463 | * @return string |
| 464 | */ |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 465 | function url_title($str, $separator = '-', $lowercase = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 467 | if ($separator === 'dash') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 469 | $separator = '-'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 471 | elseif ($separator === 'underscore') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 473 | $separator = '_'; |
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 | |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 476 | $q_separator = preg_quote($separator, '#'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | |
| 478 | $trans = array( |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame^] | 479 | '&.+?;' => '', |
| 480 | '[^a-z0-9 _-]' => '', |
| 481 | '\s+' => $separator, |
| 482 | '('.$q_separator.')+' => $separator |
| 483 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | |
| 485 | $str = strip_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | foreach ($trans as $key => $val) |
| 487 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 488 | $str = preg_replace('#'.$key.'#i', $val, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Derek Jones | 40a2fc8 | 2008-12-09 19:41:25 +0000 | [diff] [blame] | 491 | if ($lowercase === TRUE) |
| 492 | { |
| 493 | $str = strtolower($str); |
| 494 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Phil Sturgeon | a2bd363 | 2012-03-04 15:32:58 +0000 | [diff] [blame] | 496 | return trim(trim($str, $separator)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | // ------------------------------------------------------------------------ |
| 501 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | if ( ! function_exists('redirect')) |
| 503 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 504 | /** |
| 505 | * Header Redirect |
| 506 | * |
| 507 | * Header redirect in two flavors |
| 508 | * For very fine grained control over headers, you could use the Output |
| 509 | * Library's set_header() function. |
| 510 | * |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 511 | * @param string $uri URL |
| 512 | * @param string $method Redirect method |
| 513 | * 'auto', 'location' or 'refresh' |
| 514 | * @param int $code HTTP Response status code |
| 515 | * @return void |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 516 | */ |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 517 | function redirect($uri = '', $method = 'auto', $code = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | { |
Aaron Adams | 16800e4 | 2012-12-07 22:39:23 -0500 | [diff] [blame] | 519 | if ( ! preg_match('#^(\w+:)?//#i', $uri)) |
Derek Jones | 534be03 | 2009-02-10 18:47:47 +0000 | [diff] [blame] | 520 | { |
| 521 | $uri = site_url($uri); |
| 522 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 524 | // IIS environment likely? Use 'refresh' for better compatibility |
vlakoff | aab26a1 | 2012-09-11 13:10:21 +0200 | [diff] [blame] | 525 | if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE) |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 526 | { |
| 527 | $method = 'refresh'; |
| 528 | } |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 529 | elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code))) |
| 530 | { |
Andrey Andreev | a0a73c9 | 2014-01-09 19:21:26 +0200 | [diff] [blame] | 531 | if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1') |
| 532 | { |
| 533 | $code = ($_SERVER['REQUEST_METHOD'] !== 'GET') |
| 534 | ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get |
| 535 | : 307; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | $code = 302; |
| 540 | } |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 541 | } |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 542 | |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 543 | switch ($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 545 | case 'refresh': |
| 546 | header('Refresh:0;url='.$uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | break; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 548 | default: |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 549 | header('Location: '.$uri, TRUE, $code); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | exit; |
| 553 | } |
| 554 | } |
| 555 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | /* End of file url_helper.php */ |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 557 | /* Location: ./system/helpers/url_helper.php */ |