/halogy/helpers/url_helper.php
PHP | 593 lines | 334 code | 75 blank | 184 comment | 77 complexity | 760c93dcaba9477d50370490d7b9a808 MD5 | raw file
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2/** 3 * CodeIgniter 4 * 5 * An open source application development framework for PHP 4.3.2 or newer 6 * 7 * @package CodeIgniter 8 * @author ExpressionEngine Dev Team 9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc. 10 * @license http://codeigniter.com/user_guide/license.html 11 * @link http://codeigniter.com 12 * @since Version 1.0 13 * @filesource 14 */ 15 16// ------------------------------------------------------------------------ 17 18/** 19 * CodeIgniter URL Helpers 20 * 21 * @package CodeIgniter 22 * @subpackage Helpers 23 * @category Helpers 24 * @author ExpressionEngine Dev Team 25 * @link http://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 */ 40if ( ! function_exists('site_url')) 41{ 42 function site_url($uri = '') 43 { 44 $CI =& get_instance(); 45 return $CI->config->site_url($uri); 46 } 47} 48 49// ------------------------------------------------------------------------ 50 51/** 52 * Base URL 53 * 54 * Returns the "base_url" item from your config file 55 * 56 * @access public 57 * @return string 58 */ 59if ( ! function_exists('base_url')) 60{ 61 function base_url() 62 { 63 $CI =& get_instance(); 64 return $CI->config->slash_item('base_url'); 65 } 66} 67 68// ------------------------------------------------------------------------ 69 70/** 71 * Current URL 72 * 73 * Returns the full URL (including segments) of the page where this 74 * function is placed 75 * 76 * @access public 77 * @return string 78 */ 79if ( ! function_exists('current_url')) 80{ 81 function current_url() 82 { 83 $CI =& get_instance(); 84 return $CI->config->site_url($CI->uri->uri_string()); 85 } 86} 87 88// ------------------------------------------------------------------------ 89/** 90 * URL String 91 * 92 * Returns the URI segments. 93 * 94 * @access public 95 * @return string 96 */ 97if ( ! function_exists('uri_string')) 98{ 99 function uri_string() 100 { 101 $CI =& get_instance(); 102 return $CI->uri->uri_string(); 103 } 104} 105 106// ------------------------------------------------------------------------ 107 108/** 109 * Index page 110 * 111 * Returns the "index_page" from your config file 112 * 113 * @access public 114 * @return string 115 */ 116if ( ! function_exists('index_page')) 117{ 118 function index_page() 119 { 120 $CI =& get_instance(); 121 return $CI->config->item('index_page'); 122 } 123} 124 125// ------------------------------------------------------------------------ 126 127/** 128 * Anchor Link 129 * 130 * Creates an anchor based on the local URL. 131 * 132 * @access public 133 * @param string the URL 134 * @param string the link title 135 * @param mixed any attributes 136 * @return string 137 */ 138if ( ! function_exists('anchor')) 139{ 140 function anchor($uri = '', $title = '', $attributes = '') 141 { 142 $title = (string) $title; 143 144 if ( ! is_array($uri)) 145 { 146 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri; 147 } 148 else 149 { 150 $site_url = site_url($uri); 151 } 152 153 if ($title == '') 154 { 155 $title = $site_url; 156 } 157 158 if ($attributes != '') 159 { 160 $attributes = _parse_attributes($attributes); 161 } 162 163 return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>'; 164 } 165} 166 167// ------------------------------------------------------------------------ 168 169/** 170 * Anchor Link - Pop-up version 171 * 172 * Creates an anchor based on the local URL. The link 173 * opens a new window based on the attributes specified. 174 * 175 * @access public 176 * @param string the URL 177 * @param string the link title 178 * @param mixed any attributes 179 * @return string 180 */ 181if ( ! function_exists('anchor_popup')) 182{ 183 function anchor_popup($uri = '', $title = '', $attributes = FALSE) 184 { 185 $title = (string) $title; 186 187 $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri; 188 189 if ($title == '') 190 { 191 $title = $site_url; 192 } 193 194 if ($attributes === FALSE) 195 { 196 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank');\">".$title."</a>"; 197 } 198 199 if ( ! is_array($attributes)) 200 { 201 $attributes = array(); 202 } 203 204 foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val) 205 { 206 $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key]; 207 unset($attributes[$key]); 208 } 209 210 if ($attributes != '') 211 { 212 $attributes = _parse_attributes($attributes); 213 } 214 215 return "<a href='javascript:void(0);' onclick=\"window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');\"$attributes>".$title."</a>"; 216 } 217} 218 219// ------------------------------------------------------------------------ 220 221/** 222 * Mailto Link 223 * 224 * @access public 225 * @param string the email address 226 * @param string the link title 227 * @param mixed any attributes 228 * @return string 229 */ 230if ( ! function_exists('mailto')) 231{ 232 function mailto($email, $title = '', $attributes = '') 233 { 234 $title = (string) $title; 235 236 if ($title == "") 237 { 238 $title = $email; 239 } 240 241 $attributes = _parse_attributes($attributes); 242 243 return '<a href="mailto:'.$email.'"'.$attributes.'>'.$title.'</a>'; 244 } 245} 246 247// ------------------------------------------------------------------------ 248 249/** 250 * Encoded Mailto Link 251 * 252 * Create a spam-protected mailto link written in Javascript 253 * 254 * @access public 255 * @param string the email address 256 * @param string the link title 257 * @param mixed any attributes 258 * @return string 259 */ 260if ( ! function_exists('safe_mailto')) 261{ 262 function safe_mailto($email, $title = '', $attributes = '') 263 { 264 $title = (string) $title; 265 266 if ($title == "") 267 { 268 $title = $email; 269 } 270 271 for ($i = 0; $i < 16; $i++) 272 { 273 $x[] = substr('<a href="mailto:', $i, 1); 274 } 275 276 for ($i = 0; $i < strlen($email); $i++) 277 { 278 $x[] = "|".ord(substr($email, $i, 1)); 279 } 280 281 $x[] = '"'; 282 283 if ($attributes != '') 284 { 285 if (is_array($attributes)) 286 { 287 foreach ($attributes as $key => $val) 288 { 289 $x[] = ' '.$key.'="'; 290 for ($i = 0; $i < strlen($val); $i++) 291 { 292 $x[] = "|".ord(substr($val, $i, 1)); 293 } 294 $x[] = '"'; 295 } 296 } 297 else 298 { 299 for ($i = 0; $i < strlen($attributes); $i++) 300 { 301 $x[] = substr($attributes, $i, 1); 302 } 303 } 304 } 305 306 $x[] = '>'; 307 308 $temp = array(); 309 for ($i = 0; $i < strlen($title); $i++) 310 { 311 $ordinal = ord($title[$i]); 312 313 if ($ordinal < 128) 314 { 315 $x[] = "|".$ordinal; 316 } 317 else 318 { 319 if (count($temp) == 0) 320 { 321 $count = ($ordinal < 224) ? 2 : 3; 322 } 323 324 $temp[] = $ordinal; 325 if (count($temp) == $count) 326 { 327 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); 328 $x[] = "|".$number; 329 $count = 1; 330 $temp = array(); 331 } 332 } 333 } 334 335 $x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>'; 336 337 $x = array_reverse($x); 338 ob_start(); 339 340 ?><script type="text/javascript"> 341 //<![CDATA[ 342 var l=new Array(); 343 <?php 344 $i = 0; 345 foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?> 346 347 for (var i = l.length-1; i >= 0; i=i-1){ 348 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";"); 349 else document.write(unescape(l[i]));} 350 //]]> 351 </script><?php 352 353 $buffer = ob_get_contents(); 354 ob_end_clean(); 355 return $buffer; 356 } 357} 358 359// ------------------------------------------------------------------------ 360 361/** 362 * Auto-linker 363 * 364 * Automatically links URL and Email addresses. 365 * Note: There's a bit of extra code here to deal with 366 * URLs or emails that end in a period. We'll strip these 367 * off and add them after the link. 368 * 369 * @access public 370 * @param string the string 371 * @param string the type: email, url, or both 372 * @param bool whether to create pop-up links 373 * @return string 374 */ 375if ( ! function_exists('auto_link')) 376{ 377 function auto_link($str, $type = 'both', $popup = FALSE) 378 { 379 if ($type != 'email') 380 { 381 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)) 382 { 383 $pop = ($popup == TRUE) ? " target=\"_blank\" " : ""; 384 385 for ($i = 0; $i < count($matches['0']); $i++) 386 { 387 $period = ''; 388 if (preg_match("|\.$|", $matches['6'][$i])) 389 { 390 $period = '.'; 391 $matches['6'][$i] = substr($matches['6'][$i], 0, -1); 392 } 393 394 $str = str_replace($matches['0'][$i], 395 $matches['1'][$i].'<a href="http'. 396 $matches['4'][$i].'://'. 397 $matches['5'][$i]. 398 $matches['6'][$i].'"'.$pop.'>http'. 399 $matches['4'][$i].'://'. 400 $matches['5'][$i]. 401 $matches['6'][$i].'</a>'. 402 $period, $str); 403 } 404 } 405 } 406 407 if ($type != 'url') 408 { 409 if (preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $str, $matches)) 410 { 411 for ($i = 0; $i < count($matches['0']); $i++) 412 { 413 $period = ''; 414 if (preg_match("|\.$|", $matches['3'][$i])) 415 { 416 $period = '.'; 417 $matches['3'][$i] = substr($matches['3'][$i], 0, -1); 418 } 419 420 $str = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period, $str); 421 } 422 } 423 } 424 425 return $str; 426 } 427} 428 429// ------------------------------------------------------------------------ 430 431/** 432 * Prep URL 433 * 434 * Simply adds the http:// part if missing 435 * 436 * @access public 437 * @param string the URL 438 * @return string 439 */ 440if ( ! function_exists('prep_url')) 441{ 442 function prep_url($str = '') 443 { 444 if ($str == 'http://' OR $str == '') 445 { 446 return ''; 447 } 448 449 if (substr($str, 0, 7) != 'http://' && substr($str, 0, 8) != 'https://') 450 { 451 $str = 'http://'.$str; 452 } 453 454 return $str; 455 } 456} 457 458// ------------------------------------------------------------------------ 459 460/** 461 * Create URL Title 462 * 463 * Takes a "title" string as input and creates a 464 * human-friendly URL string with either a dash 465 * or an underscore as the word separator. 466 * 467 * @access public 468 * @param string the string 469 * @param string the separator: dash, or underscore 470 * @return string 471 */ 472if ( ! function_exists('url_title')) 473{ 474 function url_title($str, $separator = 'dash', $lowercase = FALSE) 475 { 476 if ($separator == 'dash') 477 { 478 $search = '_'; 479 $replace = '-'; 480 } 481 else 482 { 483 $search = '-'; 484 $replace = '_'; 485 } 486 487 $trans = array( 488 '&\#\d+?;' => '', 489 '&\S+?;' => '', 490 '\s+' => $replace, 491 '[^a-z0-9\-\._]' => '', 492 $replace.'+' => $replace, 493 $replace.'$' => $replace, 494 '^'.$replace => $replace, 495 '\.+$' => '' 496 ); 497 498 $str = strip_tags($str); 499 500 foreach ($trans as $key => $val) 501 { 502 $str = preg_replace("#".$key."#i", $val, $str); 503 } 504 505 if ($lowercase === TRUE) 506 { 507 $str = strtolower($str); 508 } 509 510 return trim(stripslashes($str)); 511 } 512} 513 514// ------------------------------------------------------------------------ 515 516/** 517 * Header Redirect 518 * 519 * Header redirect in two flavors 520 * For very fine grained control over headers, you could use the Output 521 * Library's set_header() function. 522 * 523 * @access public 524 * @param string the URL 525 * @param string the method: location or redirect 526 * @return string 527 */ 528if ( ! function_exists('redirect')) 529{ 530 function redirect($uri = '', $method = 'location', $http_response_code = 302) 531 { 532 if ( ! preg_match('#^https?://#i', $uri)) 533 { 534 $uri = site_url($uri); 535 } 536 537 switch($method) 538 { 539 case 'refresh' : header("Refresh:0;url=".$uri); 540 break; 541 default : header("Location: ".$uri, TRUE, $http_response_code); 542 break; 543 } 544 exit; 545 } 546} 547 548// ------------------------------------------------------------------------ 549 550/** 551 * Parse out the attributes 552 * 553 * Some of the functions use this 554 * 555 * @access private 556 * @param array 557 * @param bool 558 * @return string 559 */ 560if ( ! function_exists('_parse_attributes')) 561{ 562 function _parse_attributes($attributes, $javascript = FALSE) 563 { 564 if (is_string($attributes)) 565 { 566 return ($attributes != '') ? ' '.$attributes : ''; 567 } 568 569 $att = ''; 570 foreach ($attributes as $key => $val) 571 { 572 if ($javascript == TRUE) 573 { 574 $att .= $key . '=' . $val . ','; 575 } 576 else 577 { 578 $att .= ' ' . $key . '="' . $val . '"'; 579 } 580 } 581 582 if ($javascript == TRUE AND $att != '') 583 { 584 $att = substr($att, 0, -1); 585 } 586 587 return $att; 588 } 589} 590 591 592/* End of file url_helper.php */ 593/* Location: ./system/helpers/url_helper.php */