admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter URL Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/helpers/url_helper.html |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Site URL |
| 32 | * |
| 33 | * Create a local URL based on your basepath. Segments can be passed via the |
| 34 | * first parameter either as a string or an array. |
| 35 | * |
| 36 | * @access public |
| 37 | * @param string |
| 38 | * @return string |
| 39 | */ |
| 40 | function site_url($uri = '') |
| 41 | { |
admin | 75198f9 | 2006-10-07 03:16:53 +0000 | [diff] [blame^] | 42 | $CI =& get_instance(); |
| 43 | return $CI->config->site_url($uri); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | /** |
| 49 | * Base URL |
| 50 | * |
| 51 | * Returns the "base_url" item from your config file |
| 52 | * |
| 53 | * @access public |
| 54 | * @return string |
| 55 | */ |
| 56 | function base_url() |
| 57 | { |
admin | 75198f9 | 2006-10-07 03:16:53 +0000 | [diff] [blame^] | 58 | $CI =& get_instance(); |
| 59 | return $CI->config->slash_item('base_url'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | /** |
| 65 | * Index page |
| 66 | * |
| 67 | * Returns the "index_page" from your config file |
| 68 | * |
| 69 | * @access public |
| 70 | * @return string |
| 71 | */ |
| 72 | function index_page() |
| 73 | { |
admin | 75198f9 | 2006-10-07 03:16:53 +0000 | [diff] [blame^] | 74 | $CI =& get_instance(); |
| 75 | return $CI->config->item('index_page'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // ------------------------------------------------------------------------ |
| 79 | |
| 80 | /** |
| 81 | * Anchor Link |
| 82 | * |
| 83 | * Creates an anchor based on the local URL. |
| 84 | * |
| 85 | * @access public |
| 86 | * @param string the URL |
| 87 | * @param string the link title |
| 88 | * @param mixed any attributes |
| 89 | * @return string |
| 90 | */ |
| 91 | function anchor($uri = '', $title = '', $attributes = '') |
| 92 | { |
admin | 4d07c15 | 2006-09-06 02:14:53 +0000 | [diff] [blame] | 93 | if ( ! is_array($uri)) |
| 94 | { |
| 95 | $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | $site_url = site_url($uri); |
| 100 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 101 | |
| 102 | if ($title == '') |
| 103 | { |
| 104 | $title = $site_url; |
| 105 | } |
| 106 | |
| 107 | if ($attributes == '') |
| 108 | { |
| 109 | $attributes = ' title="'.$title.'"'; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | if (is_array($attributes)) |
| 114 | { |
| 115 | $attributes = parse_url_attributes($attributes); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>'; |
| 120 | } |
| 121 | |
| 122 | // ------------------------------------------------------------------------ |
| 123 | |
| 124 | /** |
| 125 | * Anchor Link - Pop-up version |
| 126 | * |
| 127 | * Creates an anchor based on the local URL. The link |
| 128 | * opens a new window based on the attributes specified. |
| 129 | * |
| 130 | * @access public |
| 131 | * @param string the URL |
| 132 | * @param string the link title |
| 133 | * @param mixed any attributes |
| 134 | * @return string |
| 135 | */ |
| 136 | function anchor_popup($uri = '', $title = '', $attributes = FALSE) |
| 137 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 138 | $site_url = ( ! preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 139 | |
| 140 | if ($title == '') |
| 141 | { |
| 142 | $title = $site_url; |
| 143 | } |
| 144 | |
| 145 | if ($attributes === FALSE) |
| 146 | { |
| 147 | return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>"; |
| 148 | } |
| 149 | |
| 150 | if ( ! is_array($attributes)) |
| 151 | { |
| 152 | $attributes = array(); |
| 153 | } |
| 154 | |
| 155 | foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val) |
| 156 | { |
| 157 | $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key]; |
| 158 | } |
| 159 | |
| 160 | return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '".parse_url_attributes($atts, TRUE)."');\">".$title."</a>"; |
| 161 | } |
| 162 | |
| 163 | // ------------------------------------------------------------------------ |
| 164 | |
| 165 | /** |
| 166 | * Mailto Link |
| 167 | * |
| 168 | * @access public |
| 169 | * @param string the email address |
| 170 | * @param string the link title |
| 171 | * @param mixed any attributes |
| 172 | * @return string |
| 173 | */ |
| 174 | function mailto($email, $title = '', $attributes = '') |
| 175 | { |
| 176 | if ($title == "") |
| 177 | { |
| 178 | $title = $email; |
| 179 | } |
| 180 | |
| 181 | if (is_array($attributes)) |
| 182 | { |
| 183 | $attributes = parse_url_attributes($attributes); |
| 184 | } |
| 185 | |
| 186 | return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>'; |
| 187 | } |
| 188 | |
| 189 | // ------------------------------------------------------------------------ |
| 190 | |
| 191 | /** |
| 192 | * Encoded Mailto Link |
| 193 | * |
| 194 | * Create a spam-protected mailto link written in Javascript |
| 195 | * |
| 196 | * @access public |
| 197 | * @param string the email address |
| 198 | * @param string the link title |
| 199 | * @param mixed any attributes |
| 200 | * @return string |
| 201 | */ |
| 202 | function safe_mailto($email, $title = '', $attributes = '') |
| 203 | { |
| 204 | if ($title == "") |
| 205 | { |
| 206 | $title = $email; |
| 207 | } |
| 208 | |
| 209 | for ($i = 0; $i < 16; $i++) |
| 210 | { |
| 211 | $x[] = substr('<a href="mailto:', $i, 1); |
| 212 | } |
| 213 | |
| 214 | for ($i = 0; $i < strlen($email); $i++) |
| 215 | { |
| 216 | $x[] = "|".ord(substr($email, $i, 1)); |
| 217 | } |
| 218 | |
| 219 | $x[] = '"'; |
| 220 | |
| 221 | if ($attributes != '') |
| 222 | { |
| 223 | if (is_array($attributes)) |
| 224 | { |
| 225 | foreach ($attributes as $key => $val) |
| 226 | { |
| 227 | $x[] = ' '.$key.'="'; |
| 228 | for ($i = 0; $i < strlen($val); $i++) |
| 229 | { |
| 230 | $x[] = "|".ord(substr($val, $i, 1)); |
| 231 | } |
| 232 | $x[] = '"'; |
| 233 | } |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | for ($i = 0; $i < strlen($attributes); $i++) |
| 238 | { |
| 239 | $x[] = substr($attributes, $i, 1); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | $x[] = '>'; |
| 245 | |
| 246 | $temp = array(); |
| 247 | for ($i = 0; $i < strlen($title); $i++) |
| 248 | { |
| 249 | $ordinal = ord($title[$i]); |
| 250 | |
| 251 | if ($ordinal < 128) |
| 252 | { |
| 253 | $x[] = "|".$ordinal; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | if (count($temp) == 0) |
| 258 | { |
| 259 | $count = ($ordinal < 224) ? 2 : 3; |
| 260 | } |
| 261 | |
| 262 | $temp[] = $ordinal; |
| 263 | if (count($temp) == $count) |
| 264 | { |
| 265 | $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); |
| 266 | $x[] = "|".$number; |
| 267 | $count = 1; |
| 268 | $temp = array(); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; |
| 274 | |
| 275 | $x = array_reverse($x); |
| 276 | ob_start(); |
| 277 | |
| 278 | ?><script type="text/javascript"> |
| 279 | //<![CDATA[ |
| 280 | var l=new Array(); |
| 281 | <?php |
| 282 | $i = 0; |
| 283 | foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?> |
| 284 | |
| 285 | for (var i = l.length-1; i >= 0; i=i-1){ |
| 286 | if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";"); |
| 287 | else document.write(unescape(l[i]));} |
| 288 | //]]> |
| 289 | </script><?php |
| 290 | |
| 291 | $buffer = ob_get_contents(); |
| 292 | ob_end_clean(); |
| 293 | return $buffer; |
| 294 | } |
| 295 | |
| 296 | // ------------------------------------------------------------------------ |
| 297 | |
| 298 | /** |
| 299 | * Auto-linker |
| 300 | * |
| 301 | * Automatically links URL and Email addresses. |
| 302 | * Note: There's a bit of extra code here to deal with |
| 303 | * URLs or emails that end in a period. We'll strip these |
| 304 | * off and add them after the link. |
| 305 | * |
| 306 | * @access public |
| 307 | * @param string the string |
| 308 | * @param string the type: email, url, or both |
| 309 | * @param bool whether to create pop-up links |
| 310 | * @return string |
| 311 | */ |
| 312 | function auto_link($str, $type = 'both', $popup = FALSE) |
| 313 | { |
| 314 | if ($type != 'email') |
| 315 | { |
| 316 | if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)) |
| 317 | { |
| 318 | $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; |
| 319 | |
| 320 | for ($i = 0; $i < sizeof($matches['0']); $i++) |
| 321 | { |
| 322 | $period = ''; |
| 323 | if (preg_match("|\.$|", $matches['6'][$i])) |
| 324 | { |
| 325 | $period = '.'; |
| 326 | $matches['6'][$i] = substr($matches['6'][$i], 0, -1); |
| 327 | } |
| 328 | |
| 329 | $str = str_replace($matches['0'][$i], |
| 330 | $matches['1'][$i].'<a href="http'. |
| 331 | $matches['4'][$i].'://'. |
| 332 | $matches['5'][$i]. |
| 333 | $matches['6'][$i].'"'.$pop.'>http'. |
| 334 | $matches['4'][$i].'://'. |
| 335 | $matches['5'][$i]. |
| 336 | $matches['6'][$i].'</a>'. |
| 337 | $period, $str); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if ($type != 'url') |
| 343 | { |
| 344 | if (preg_match_all("/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches)) |
| 345 | { |
| 346 | for ($i = 0; $i < sizeof($matches['0']); $i++) |
| 347 | { |
| 348 | $period = ''; |
| 349 | if (preg_match("|\.$|", $matches['3'][$i])) |
| 350 | { |
| 351 | $period = '.'; |
| 352 | $matches['3'][$i] = substr($matches['3'][$i], 0, -1); |
| 353 | } |
| 354 | |
| 355 | $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str); |
| 356 | } |
| 357 | |
| 358 | } |
| 359 | } |
| 360 | return $str; |
| 361 | } |
| 362 | |
| 363 | // ------------------------------------------------------------------------ |
| 364 | |
| 365 | /** |
| 366 | * Prep URL |
| 367 | * |
| 368 | * Simply adds the http:// part if missing |
| 369 | * |
| 370 | * @access public |
| 371 | * @param string the URL |
| 372 | * @return string |
| 373 | */ |
| 374 | function prep_url($str = '') |
| 375 | { |
| 376 | if ($str == 'http://' OR $str == '') |
| 377 | { |
| 378 | return ''; |
| 379 | } |
| 380 | |
| 381 | if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') |
| 382 | { |
| 383 | $str = 'http://'.$str; |
| 384 | } |
| 385 | |
| 386 | return $str; |
| 387 | } |
| 388 | |
| 389 | // ------------------------------------------------------------------------ |
| 390 | |
| 391 | /** |
| 392 | * Create URL Title |
| 393 | * |
| 394 | * Takes a "title" string as input and creates a |
| 395 | * human-friendly URL string with either a dash |
| 396 | * or an underscore as the word separator. |
| 397 | * |
| 398 | * @access public |
| 399 | * @param string the string |
| 400 | * @param string the separator: dash, or underscore |
| 401 | * @return string |
| 402 | */ |
| 403 | function url_title($str, $separator = 'dash') |
| 404 | { |
| 405 | if ($separator == 'dash') |
| 406 | { |
| 407 | $search = '_'; |
| 408 | $replace = '-'; |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | $search = '-'; |
| 413 | $replace = '_'; |
| 414 | } |
| 415 | |
| 416 | $trans = array( |
| 417 | $search => $replace, |
| 418 | "\s+" => $replace, |
| 419 | "[^a-z0-9".$replace."]" => '', |
| 420 | $replace."+" => $replace, |
| 421 | $replace."$" => '', |
| 422 | "^".$replace => '' |
| 423 | ); |
| 424 | |
| 425 | $str = strip_tags(strtolower($str)); |
| 426 | |
| 427 | foreach ($trans as $key => $val) |
| 428 | { |
| 429 | $str = preg_replace("#".$key."#", $val, $str); |
| 430 | } |
| 431 | |
| 432 | return trim(stripslashes($str)); |
| 433 | } |
| 434 | |
| 435 | // ------------------------------------------------------------------------ |
| 436 | |
| 437 | /** |
| 438 | * Header Redirect |
| 439 | * |
| 440 | * Header redirect in two flavors |
| 441 | * |
| 442 | * @access public |
| 443 | * @param string the URL |
| 444 | * @param string the method: location or redirect |
| 445 | * @return string |
| 446 | */ |
| 447 | function redirect($uri = '', $method = 'location') |
| 448 | { |
| 449 | switch($method) |
| 450 | { |
| 451 | case 'refresh' : header("Refresh:0;url=".site_url($uri)); |
| 452 | break; |
| 453 | default : header("location:".site_url($uri)); |
| 454 | break; |
| 455 | } |
| 456 | exit; |
| 457 | } |
| 458 | |
| 459 | // ------------------------------------------------------------------------ |
| 460 | |
| 461 | /** |
| 462 | * Parse out the attributes |
| 463 | * |
| 464 | * Some of the functions use this |
| 465 | * |
| 466 | * @access private |
| 467 | * @param array |
| 468 | * @param bool |
| 469 | * @return string |
| 470 | */ |
| 471 | function parse_url_attributes($attributes, $javascript = FALSE) |
| 472 | { |
| 473 | $att = ''; |
| 474 | foreach ($attributes as $key => $val) |
| 475 | { |
| 476 | if ($javascript == TRUE) |
| 477 | { |
| 478 | $att .= $key . '=' . $val . ','; |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | $att .= ' ' . $key . '="' . $val . '"'; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if ($javascript == TRUE) |
| 487 | { |
| 488 | $att = substr($att, 0, -1); |
| 489 | } |
| 490 | |
| 491 | return $att; |
| 492 | } |
| 493 | |
| 494 | ?> |