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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter URL Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/helpers/url_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('site_url')) |
| 53 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Site URL |
| 56 | * |
| 57 | * Create a local URL based on your basepath. Segments can be passed via the |
| 58 | * first parameter either as a string or an array. |
| 59 | * |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 60 | * @param string $uri |
| 61 | * @param string $protocol |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 62 | * @return string |
| 63 | */ |
Andrey Andreev | 56c8ca6 | 2013-07-18 03:21:16 +0300 | [diff] [blame] | 64 | function site_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 66 | return get_instance()->config->site_url($uri, $protocol); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | if ( ! function_exists('base_url')) |
| 73 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 74 | /** |
| 75 | * Base URL |
| 76 | * |
| 77 | * Create a local URL based on your basepath. |
| 78 | * Segments can be passed in as a string or an array, same as site_url |
| 79 | * or a URL to a file can be passed in, e.g. to an image file. |
| 80 | * |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 81 | * @param string $uri |
| 82 | * @param string $protocol |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 83 | * @return string |
| 84 | */ |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 85 | function base_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 87 | return get_instance()->config->base_url($uri, $protocol); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | if ( ! function_exists('current_url')) |
| 94 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Current URL |
| 97 | * |
| 98 | * Returns the full URL (including segments) of the page where this |
| 99 | * function is placed |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | function current_url() |
| 104 | { |
Andrey Andreev | 4ea76cc | 2014-01-08 21:49:23 +0200 | [diff] [blame] | 105 | $CI =& get_instance(); |
| 106 | return $CI->config->site_url($CI->uri->uri_string()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | // ------------------------------------------------------------------------ |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | if ( ! function_exists('uri_string')) |
| 113 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 114 | /** |
| 115 | * URL String |
| 116 | * |
| 117 | * Returns the URI segments. |
| 118 | * |
| 119 | * @return string |
| 120 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | function uri_string() |
| 122 | { |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 123 | return get_instance()->uri->uri_string(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
| 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | if ( ! function_exists('index_page')) |
| 130 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 131 | /** |
| 132 | * Index page |
| 133 | * |
| 134 | * Returns the "index_page" from your config file |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | function index_page() |
| 139 | { |
Andrey Andreev | 119d8a7 | 2014-01-08 15:27:53 +0200 | [diff] [blame] | 140 | return get_instance()->config->item('index_page'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // ------------------------------------------------------------------------ |
| 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | if ( ! function_exists('anchor')) |
| 147 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 148 | /** |
| 149 | * Anchor Link |
| 150 | * |
| 151 | * Creates an anchor based on the local URL. |
| 152 | * |
| 153 | * @param string the URL |
| 154 | * @param string the link title |
| 155 | * @param mixed any attributes |
| 156 | * @return string |
| 157 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | function anchor($uri = '', $title = '', $attributes = '') |
| 159 | { |
| 160 | $title = (string) $title; |
| 161 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 162 | $site_url = is_array($uri) |
| 163 | ? site_url($uri) |
Andrey Andreev | c14aa9b | 2015-04-12 14:23:47 +0300 | [diff] [blame] | 164 | : (preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 166 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | { |
| 168 | $title = $site_url; |
| 169 | } |
| 170 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 171 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | { |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 173 | $attributes = _stringify_attributes($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>'; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // ------------------------------------------------------------------------ |
| 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | if ( ! function_exists('anchor_popup')) |
| 183 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 184 | /** |
| 185 | * Anchor Link - Pop-up version |
| 186 | * |
| 187 | * Creates an anchor based on the local URL. The link |
| 188 | * opens a new window based on the attributes specified. |
| 189 | * |
| 190 | * @param string the URL |
| 191 | * @param string the link title |
| 192 | * @param mixed any attributes |
| 193 | * @return string |
| 194 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | function anchor_popup($uri = '', $title = '', $attributes = FALSE) |
| 196 | { |
| 197 | $title = (string) $title; |
Aaron Adams | 16800e4 | 2012-12-07 22:39:23 -0500 | [diff] [blame] | 198 | $site_url = preg_match('#^(\w+:)?//#i', $uri) ? $uri : site_url($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 200 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | $title = $site_url; |
| 203 | } |
| 204 | |
| 205 | if ($attributes === FALSE) |
| 206 | { |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 207 | 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] | 208 | } |
| 209 | |
| 210 | if ( ! is_array($attributes)) |
| 211 | { |
Andrey Andreev | f0a8410 | 2012-06-16 20:52:20 +0300 | [diff] [blame] | 212 | $attributes = array($attributes); |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 213 | |
| 214 | // Ref: http://www.w3schools.com/jsref/met_win_open.asp |
| 215 | $window_name = '_blank'; |
| 216 | } |
| 217 | elseif ( ! empty($attributes['window_name'])) |
| 218 | { |
| 219 | $window_name = $attributes['window_name']; |
| 220 | unset($attributes['window_name']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | } |
Andrey Andreev | 5286ef0 | 2014-07-06 19:57:59 +0300 | [diff] [blame] | 222 | else |
| 223 | { |
| 224 | $window_name = '_blank'; |
| 225 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | |
Mian Saleem | fc88a5f | 2014-07-31 22:08:07 +0800 | [diff] [blame] | 227 | foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'menubar' => 'no', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0') as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 229 | $atts[$key] = isset($attributes[$key]) ? $attributes[$key] : $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | unset($attributes[$key]); |
| 231 | } |
| 232 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 233 | $attributes = _stringify_attributes($attributes); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 235 | return '<a href="'.$site_url |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 236 | .'" 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] | 237 | .$attributes.'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | // ------------------------------------------------------------------------ |
| 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | if ( ! function_exists('mailto')) |
| 244 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 245 | /** |
| 246 | * Mailto Link |
| 247 | * |
| 248 | * @param string the email address |
| 249 | * @param string the link title |
| 250 | * @param mixed any attributes |
| 251 | * @return string |
| 252 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | function mailto($email, $title = '', $attributes = '') |
| 254 | { |
| 255 | $title = (string) $title; |
| 256 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 257 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | { |
| 259 | $title = $email; |
| 260 | } |
| 261 | |
Eric Barnes | acedd2b | 2012-07-29 00:15:40 -0400 | [diff] [blame] | 262 | return '<a href="mailto:'.$email.'"'._stringify_attributes($attributes).'>'.$title.'</a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // ------------------------------------------------------------------------ |
| 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | if ( ! function_exists('safe_mailto')) |
| 269 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 270 | /** |
| 271 | * Encoded Mailto Link |
| 272 | * |
| 273 | * Create a spam-protected mailto link written in Javascript |
| 274 | * |
| 275 | * @param string the email address |
| 276 | * @param string the link title |
| 277 | * @param mixed any attributes |
| 278 | * @return string |
| 279 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | function safe_mailto($email, $title = '', $attributes = '') |
| 281 | { |
| 282 | $title = (string) $title; |
| 283 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 284 | if ($title === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | $title = $email; |
| 287 | } |
| 288 | |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 289 | $x = str_split('<a href="mailto:', 1); |
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 | for ($i = 0, $l = strlen($email); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 293 | $x[] = '|'.ord($email[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | $x[] = '"'; |
| 297 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 298 | if ($attributes !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | { |
| 300 | if (is_array($attributes)) |
| 301 | { |
| 302 | foreach ($attributes as $key => $val) |
| 303 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 304 | $x[] = ' '.$key.'="'; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 305 | for ($i = 0, $l = strlen($val); $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[] = '|'.ord($val[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | } |
| 309 | $x[] = '"'; |
| 310 | } |
| 311 | } |
| 312 | else |
| 313 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 314 | for ($i = 0, $l = strlen($attributes); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 316 | $x[] = $attributes[$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | $x[] = '>'; |
| 322 | |
| 323 | $temp = array(); |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 324 | for ($i = 0, $l = strlen($title); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | { |
| 326 | $ordinal = ord($title[$i]); |
| 327 | |
| 328 | if ($ordinal < 128) |
| 329 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 330 | $x[] = '|'.$ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | } |
| 332 | else |
| 333 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 334 | if (count($temp) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | $count = ($ordinal < 224) ? 2 : 3; |
| 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | $temp[] = $ordinal; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 340 | if (count($temp) === $count) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 342 | $number = ($count === 3) |
| 343 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 344 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
| 345 | $x[] = '|'.$number; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | $count = 1; |
| 347 | $temp = array(); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; |
| 353 | |
| 354 | $x = array_reverse($x); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 356 | $output = "<script type=\"text/javascript\">\n" |
| 357 | ."\t//<![CDATA[\n" |
| 358 | ."\tvar l=new Array();\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 360 | for ($i = 0, $c = count($x); $i < $c; $i++) |
| 361 | { |
| 362 | $output .= "\tl[".$i."] = '".$x[$i]."';\n"; |
| 363 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | |
Andrey Andreev | cfaf8c4 | 2014-02-15 21:58:45 +0200 | [diff] [blame] | 365 | $output .= "\n\tfor (var i = l.length-1; i >= 0; i=i-1) {\n" |
| 366 | ."\t\tif (l[i].substring(0, 1) === '|') document.write(\"&#\"+unescape(l[i].substring(1))+\";\");\n" |
| 367 | ."\t\telse document.write(unescape(l[i]));\n" |
| 368 | ."\t}\n" |
| 369 | ."\t//]]>\n" |
| 370 | .'</script>'; |
| 371 | |
| 372 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | // ------------------------------------------------------------------------ |
| 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | if ( ! function_exists('auto_link')) |
| 379 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 380 | /** |
| 381 | * Auto-linker |
| 382 | * |
| 383 | * Automatically links URL and Email addresses. |
| 384 | * Note: There's a bit of extra code here to deal with |
| 385 | * URLs or emails that end in a period. We'll strip these |
| 386 | * off and add them after the link. |
| 387 | * |
| 388 | * @param string the string |
| 389 | * @param string the type: email, url, or both |
| 390 | * @param bool whether to create pop-up links |
| 391 | * @return string |
| 392 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | function auto_link($str, $type = 'both', $popup = FALSE) |
| 394 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 395 | // Find and replace any URLs. |
Andrey Andreev | 8c9e510 | 2017-11-10 15:02:42 +0200 | [diff] [blame] | 396 | if ($type !== 'email' && preg_match_all('#(\w*://|www\.)[a-z0-9]+(-+[a-z0-9]+)*(\.[a-z0-9]+(-+[a-z0-9]+)*)+(/([^\s()<>;]+\w)?/?)?#i', $str, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 398 | // Set our target HTML if using popup links. |
Andrey Andreev | 4dab9f8 | 2018-01-31 23:56:21 +0200 | [diff] [blame] | 399 | $target = ($popup) ? ' target="_blank" rel="noopener"' : ''; |
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 | // We process the links in reverse order (last -> first) so that |
| 402 | // the returned string offsets from preg_match_all() are not |
| 403 | // moved as we add more HTML. |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 404 | foreach (array_reverse($matches) as $match) |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 405 | { |
Andrey Andreev | 606fee0 | 2013-01-28 12:57:13 +0200 | [diff] [blame] | 406 | // $match[0] is the matched string/link |
| 407 | // $match[1] is either a protocol prefix or 'www.' |
| 408 | // |
| 409 | // With PREG_OFFSET_CAPTURE, both of the above is an array, |
| 410 | // where the actual value is held in [0] and its offset at the [1] index. |
| 411 | $a = '<a href="'.(strpos($match[1][0], '/') ? '' : 'http://').$match[0][0].'"'.$target.'>'.$match[0][0].'</a>'; |
| 412 | $str = substr_replace($str, $a, $match[0][1], strlen($match[0][0])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
Eric Roberts | 32a28f5 | 2013-01-19 02:59:14 -0600 | [diff] [blame] | 415 | |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 416 | // Find and replace any emails. |
| 417 | 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] | 418 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 419 | foreach (array_reverse($matches[0]) as $match) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 421 | if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== FALSE) |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 422 | { |
Eric Roberts | 8093bd7 | 2013-01-17 18:12:47 -0600 | [diff] [blame] | 423 | $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] | 424 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
Eric Roberts | 32a28f5 | 2013-01-19 02:59:14 -0600 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 473 | * @todo Remove old 'dash' and 'underscore' usage in 3.1+. |
| 474 | * @param string $str Input string |
| 475 | * @param string $separator Word separator |
| 476 | * (usually '-' or '_') |
Michael Foss | 9409a2a | 2015-06-12 20:22:39 -0400 | [diff] [blame] | 477 | * @param bool $lowercase Whether to transform the output string to lowercase |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 478 | * @return string |
| 479 | */ |
tubalmartin | 1a69710 | 2012-03-04 16:01:11 +0100 | [diff] [blame] | 480 | function url_title($str, $separator = '-', $lowercase = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 482 | if ($separator === 'dash') |
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 | $separator = '-'; |
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 | elseif ($separator === 'underscore') |
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 | $separator = '_'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | } |
Andrey Andreev | 1222087 | 2012-03-26 21:38:56 +0300 | [diff] [blame] | 490 | |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 491 | $q_separator = preg_quote($separator, '#'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | |
| 493 | $trans = array( |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 494 | '&.+?;' => '', |
dimonneon | 32bdf1c | 2015-07-10 21:47:04 +0300 | [diff] [blame] | 495 | '[^\w\d _-]' => '', |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 496 | '\s+' => $separator, |
| 497 | '('.$q_separator.')+' => $separator |
| 498 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | |
| 500 | $str = strip_tags($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | foreach ($trans as $key => $val) |
| 502 | { |
dimonneon | 28c8305 | 2015-07-13 11:54:05 +0300 | [diff] [blame] | 503 | $str = preg_replace('#'.$key.'#i'.(UTF8_ENABLED ? 'u' : ''), $val, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Derek Jones | 40a2fc8 | 2008-12-09 19:41:25 +0000 | [diff] [blame] | 506 | if ($lowercase === TRUE) |
| 507 | { |
| 508 | $str = strtolower($str); |
| 509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 510 | |
Phil Sturgeon | a2bd363 | 2012-03-04 15:32:58 +0000 | [diff] [blame] | 511 | return trim(trim($str, $separator)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | // ------------------------------------------------------------------------ |
| 516 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | if ( ! function_exists('redirect')) |
| 518 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 519 | /** |
| 520 | * Header Redirect |
| 521 | * |
| 522 | * Header redirect in two flavors |
| 523 | * For very fine grained control over headers, you could use the Output |
| 524 | * Library's set_header() function. |
| 525 | * |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 526 | * @param string $uri URL |
| 527 | * @param string $method Redirect method |
| 528 | * 'auto', 'location' or 'refresh' |
| 529 | * @param int $code HTTP Response status code |
| 530 | * @return void |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 531 | */ |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 532 | function redirect($uri = '', $method = 'auto', $code = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | { |
Aaron Adams | 16800e4 | 2012-12-07 22:39:23 -0500 | [diff] [blame] | 534 | if ( ! preg_match('#^(\w+:)?//#i', $uri)) |
Derek Jones | 534be03 | 2009-02-10 18:47:47 +0000 | [diff] [blame] | 535 | { |
| 536 | $uri = site_url($uri); |
| 537 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 539 | // IIS environment likely? Use 'refresh' for better compatibility |
vlakoff | aab26a1 | 2012-09-11 13:10:21 +0200 | [diff] [blame] | 540 | 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] | 541 | { |
| 542 | $method = 'refresh'; |
| 543 | } |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 544 | elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code))) |
| 545 | { |
Andrey Andreev | a0a73c9 | 2014-01-09 19:21:26 +0200 | [diff] [blame] | 546 | if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1') |
| 547 | { |
| 548 | $code = ($_SERVER['REQUEST_METHOD'] !== 'GET') |
| 549 | ? 303 // reference: http://en.wikipedia.org/wiki/Post/Redirect/Get |
| 550 | : 307; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | $code = 302; |
| 555 | } |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 556 | } |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 557 | |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 558 | switch ($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | { |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 560 | case 'refresh': |
| 561 | header('Refresh:0;url='.$uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | break; |
Andrey Andreev | debcc36 | 2012-01-07 02:05:29 +0200 | [diff] [blame] | 563 | default: |
Andrey Andreev | 2fce2a9 | 2012-06-27 01:07:56 +0300 | [diff] [blame] | 564 | header('Location: '.$uri, TRUE, $code); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | exit; |
| 568 | } |
| 569 | } |