New version submitted by TomB
[carbonphp.git] / Source / carbon / utilities / url_utility.php
blob82c4b7bb7b2eb0c95584d1518e3b85daede05560
1 <?php
2 /*------------------------------------------------------------
3 * CarbonPHP framework (C) Tom Bell
4 * http://tombell.org.uk
5 *------------------------------------------------------------*/
7 if (!defined('CARBON_PATH'))
9 exit('Direct script access is not allowed.');
12 if (!function_exists('site_url'))
14 function site_url($uri = '')
16 $carbon =& get_instance();
17 return $carbon->config->get_site_url($uri);
21 if (!function_exists('base_url'))
23 function base_url()
25 $carbon =& get_instance();
26 return $carbon->config->slash_item('base_url');
30 if (!function_exists('index_page'))
32 function index_page()
34 $carbon =& get_instance();
35 return $carbon->config->get_item_value('index_page');
39 if (!function_exists('anchor'))
41 function anchor($uri = '', $title = '', $attributes = '')
43 $title = (string) $title;
45 if (!is_array($uri))
47 $site_url = (!preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
49 else
51 $site_url = site_url($uri);
54 if ($title == '')
56 $title = $site_url;
59 if ($attributes == '')
61 $attributes = ' title="' . $title . '"';
63 else
65 $attributes = parse_attributes($attributes);
68 return '<a href="' . $site_url . '"' . $attributes . '>' . $title . '</a>';
72 if (!function_exists('anchor_popup'))
74 function anchor_popup($uri = '', $title = '', $attributes = false)
76 $title = (string) $title;
77 $site_url = (!preg_match('!^\w+://!i', $uri)) ? site_url($uri) : $uri;
79 if ($title == '')
81 $title = $site_url;
84 if ($attributes === false)
86 return "<a href='javascript:void(0);' onclick=\"window.open('" . $site_url . "', '_blank');\">" . $title . "</a>";
89 if (!is_array($attributes))
91 $attributes = array();
94 $default = array(
95 'width' => '800',
96 'height' => '600',
97 'scrollbars' => 'yes',
98 'status' => 'yes',
99 'resizable' => 'yes',
100 'screenx' => '0',
101 'screeny' => '0'
104 foreach ($default as $key => $val)
106 $attr[$key] = (!isset($attributes[$key])) ? $val : $attributes[$key];
109 return "<a href='javascript(0);' onclick=\"window.open('" . $site_url . "', '_blank', '" . parse_attributes($attr, true) . "');\">" . $title . "</a>";
113 if (!function_exists('mailto'))
115 function mailto($email, $title = '', $attributes = '')
117 $title = (string) $title;
119 if ($title == '')
121 $title = $email;
124 $attributes = parse_attributes($attributes);
126 return '<a href="mailto:' . $email . '"' . $attributes . '>' . $title . '</a>';
130 if (!function_exists('safe_mailto'))
132 function safe_mailto($email, $title = '', $attributes = '')
134 $title = (string) $title;
136 if ($title == '')
138 $title = $email;
141 for ($i = 0; $i < 16; $i++)
143 $x[] = substr('<a href="mailto:', $i, 1);
146 for ($i = 0; $i < strlen($email); $i++)
148 $x[] = '|' . ord(substr($email, $i, 1));
151 $x[] = '"';
153 if ($attributes != '')
155 if (is_array($attributes))
157 foreach ($attributes as $key => $val)
159 $x[] = ' ' . $key . '="';
161 for ($i = 0; $i < strlen($val); $i++)
163 $x[] = '|' . ord(substr($val, $i, 1));
166 $x[] = '"';
169 else
171 for ($i = 0; $i < strlen($attributes); $i++)
173 $x[] = substr($attributes, $i, 1);
178 $x[] = '>';
180 $temp = array();
182 for ($i = 0; $i < strlen($title); $i++)
184 $ordinal = ord($title[$i]);
186 if ($ordinal < 128)
188 $x[] = '|' . $ordinal;
190 else
192 if (count($temp) == 0)
194 $count = ($ordinal < 224) ? 2 : 3;
197 $temp[] = $ordinal;
199 if (count($temp) == $count)
201 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
202 $x[] = '|' . $number;
203 $count = 1;
204 $temp = array();
209 $x[] = '<';
210 $x[] = '/';
211 $x[] = 'a';
212 $x[] = '>';
214 $x = array_reverse($x);
216 ob_start();
219 <script type="text/javascript">
220 //<![CDATA[
221 var l = new Array();
223 <?php
224 $i = 0;
225 foreach ($x as $val)
228 l[<?php echo $i++;?>] = '<?php echo $val;?>';
229 <?php
233 for (var i = l.length - 1; i >= 0; i = i - 1)
235 if (l[i].substring(0, 1) == '|')
237 document.write("&#" + unescape(l[i].substring(1)) + ";");
239 else
241 document.write(unescape(l[i]));
244 //]]>
245 </script>
247 <?php
248 $buffer = ob_get_contents();
249 ob_end_clean();
250 return $buffer;
254 if (!function_exists('auto_link'))
256 function auto_link($string, $type = 'both', $popup = false)
258 if ($type != 'email')
260 if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $string, $matches))
262 $pop = ($popup == true) ? ' target="_blank" ' : '';
264 for ($i = 0; $i < sizeof($matches['0']); $i++)
266 $period = '';
268 if (preg_match("|\.$|", $matches['6'][$i]))
270 $peroid = '.';
271 $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
274 $string = str_replace($matches['0'][$i],
275 $matches['1'][$i] . '<a href="http' .
276 $matches['4'][$i] . '://' .
277 $matches['5'][$i] .
278 $matches['6'][$i] . '"' . $pop . '>http' .
279 $matches['4'][$i] . '://' .
280 $matches['5'][$i] .
281 $matches['6'][$i] . '</a>' .
282 $period, $string);
287 if ($type != 'url')
289 if (preg_match_all("/([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i", $string, $matches))
291 for ($i = 0; $i < sizeof($matches['0']); $i++)
293 $period = '.';
294 $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
297 $string = str_replace($matches['0'][$i], safe_mailto($matches['1'][$i] . '@' . $matches['2'][$i] . '.' . $matches['3'][$i]) . $period, $string);
301 return $string;
305 if (!function_exists('prep_url'))
307 function prep_url($string = '')
309 if ($string == 'http://' || $string == '')
311 return '';
314 if (substr($string, 0, 7) != 'http://' && substr($string, 0, 8) != 'https://')
316 $string = 'http://' . $string;
319 return $string;
323 if (!function_exists('url_title'))
325 function url_title($string, $separator = 'dash')
327 if ($separator == 'dash')
329 $search = '_';
330 $replace = '-';
332 else
334 $search = '-';
335 $replace = '_';
338 $trans = array(
339 $search => $replace,
340 "\s+" => $replace,
341 "[^a-z0-9]" . $replace . "]" => '',
342 $replace . "+" => $replace,
343 $replace . "$" => '',
344 "^" . $replace => ''
347 $string = strip_tags(strtolower($string));
349 foreach ($trans as $key => $val)
351 $string = preg_replace("#" . $key . "#", $val, $string);
354 return trim(stripslashes($string));
358 if (!function_exists('redirect'))
360 function redirect($uri = '', $method = 'location')
362 switch ($method)
364 case 'refresh':
365 header('Refresh:0; url= ' . site_url($uri));
366 break;
368 default:
369 header('Location: ' . site_url($uri));
370 break;
373 exit();
377 if (!function_exists('parse_attributes'))
379 function parse_attributes($attributes, $javascript = false)
381 if (is_string($attributes))
383 return ($attributes != '') ? ' ' . $attributes : '';
386 $attr = '';
388 foreach ($attributes as $key => $val)
390 if ($javascript == true)
392 $attr .= $key . '=' . $val . ',';
394 else
396 $attr .= ' ' . $key . '="' . $val . '"';
400 if ($javascript == true && $attr != '')
402 $attr = substr($attr, 0, -1);
405 return $attr;