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