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