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