41 function relative_href($from,$to)
44 if (!is_string($from) || !is_string($to)) {
50 if (isset( $_SERVER[
'REQUEST_URI']) && preg_match(
'/\/'.SQ_CONF_RECACHE_SUFFIX.
'$/', $_SERVER[
'REQUEST_URI'])) {
54 $from_bits = preg_split(
'/\\/+/', trim($from));
55 $to_bits = preg_split(
'/\\/+/', trim($to));
57 $splitted = preg_split(
'/:\\/\\//', $to);
60 if(isset($splitted[0]) && isset($splitted[1])) {
61 $to_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAssetFromURL($splitted[0], $splitted[1], TRUE, TRUE);
66 if ($recaching && (!is_null($to_asset) && $to_asset->id > 0))
return './?a='.$to_asset->id;
71 if (!preg_match(
'/^[A-Za-z]+:$/', $from_bits[0])) {
72 array_unshift($from_bits,
'http:');
74 if (!preg_match(
'/^[A-Za-z]+:$/', $to_bits[0] )) {
75 array_unshift($to_bits,
'http:');
78 if (strtolower($from_bits[0]) != strtolower($to_bits[0]) || strtolower($from_bits[1]) != strtolower($to_bits[1])) {
80 return implode(
'/', $to_bits);
84 if (empty($from_bits[2]) || empty($to_bits[2]) || $from_bits[2] != $to_bits[2]) {
86 array_splice($to_bits, 0, 2);
87 return '/'.implode(
'/', $to_bits);
92 while (!empty($from_bits[$split_point])
93 && !empty($to_bits[$split_point])
94 && $from_bits[$split_point] == $to_bits[$split_point]) {
98 $href = str_repeat(
'../', count($from_bits) - $split_point);
100 for ($i = $split_point - 1, $max = count($to_bits); $i < $max; $i++) {
101 $href .= $to_bits[$i].
'/';
105 if (substr($to, -1) ==
'/') {
106 $href = preg_replace(
'/\\/+$/',
'/', $href);
108 $href = preg_replace(
'/\\/$/',
'', $href);
113 return str_replace(
'../'.$from_bits[$split_point - 1].
'/',
'', $href);
126 function valid_email($e)
128 if (!is_string($e))
return FALSE;
130 $local =
'0-9a-zA-Z_\+\-'.
'&\'\*\/=\?\^\{\}~';
134 if (preg_match(
'/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<(['.$local.
']+(\.['.$local.
']+)*@(((?:[\da-zA-Z]|[\da-zA-Z][\'-\w]*[\da-zA-Z])\.)+[a-zA-Z]{2,7}))>$/', $e)) {
137 }
else if (preg_match(
'/^(['.$local.
']+(\.['.$local.
']+)*@(((?:[\da-zA-Z]|[\da-zA-Z][\'-\w]*[\da-zA-Z])\.)+[a-zA-Z]{2,7}))$/', $e)) {
156 function valid_url($url, $schemes=Array(
'http',
'https'))
158 return preg_match(
'/^('.implode(
'|', $schemes).
'):\/\/[a-z0-9]+(([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,})?((:[0-9]{1,5})?\/.*)?$/i', $url);
171 function make_raw_post_data($data)
174 if (!is_array($data) || empty($data)) {
180 foreach ($data as $name => $value) {
181 $val_array[] = recursive_make_raw_post_data($name, $value);
185 if (!empty($val_array)) {
186 $result = implode(
'&', $val_array);
203 function recursive_make_raw_post_data($name, $data)
205 if (!is_array($data)) {
206 return $name.
'='.rawurlencode($data);
210 if (empty($data))
return $name.
'=';
212 foreach ($data as $key => $value) {
213 $val_array[] = recursive_make_raw_post_data($name.
'['.$key.
']', $value);
215 return implode(
'&', $val_array);
229 function undo_htmlspecialchars($string)
231 $string = str_replace(
'&',
'&',$string);
232 $string = str_replace(
''',
"'",$string);
233 $string = str_replace(
'"',
'"',$string);
234 $string = str_replace(
'<',
'<',$string);
235 $string = str_replace(
'>',
'>',$string);
259 function make_valid_html_id($id, $replace_char=
'')
261 if (!preg_match(
'/^[A-Za-z0-9:_.-]*$/', $replace_char)) {
264 $id = preg_replace(
'/[^A-Za-z0-9:_.-]/', $replace_char, $id);
267 while (!empty($id) && preg_match(
'/[^A-Za-z]/', $id{0})) {
268 $id = substr($id, 1);
283 function valid_ipaddress($address)
285 $ips = explode(
'.', $address);
287 if (4 != count($ips))
return FALSE;
289 foreach ($ips as $ip) {
290 if (!ctype_digit($ip))
return FALSE;
292 if ((0 > $ip) || (255 < $ip)) {
311 function check_non_live_link($html, $owner_id)
313 $am = $GLOBALS[
'SQ_SYSTEM']->am;
314 $pattern =
'#<a .*?href\s*=\s*(?:\'|")([^\'"]*?)(?:\'|")[^>]*>([^<]*)</a>#msi';
315 $error_link = Array();
316 $error_found = FALSE;
318 preg_match_all($pattern, $html, $match);
319 if (!empty($match)) {
323 $owner = $am->getAsset($owner_id);
325 $parent = $am->getParents($owner_id,
'page', FALSE);
326 if (!empty($parent)) {
327 foreach ($parent as $one_parent_id => $one_parent_type_code) {
328 $parent_id = $one_parent_id;
331 $base_url = $am->getAssetURL($parent_id);
334 $base_url = $am->getAssetURL($owner_id);
337 foreach ($match[1] as $index => $url) {
338 $whole_link = $match[0][$index];
339 $href = $match[1][$index];
340 $link_name = $match[2][$index];
342 if (empty($url))
continue;
344 if (empty($link_name)) $error_message = $href;
346 $pattern =
'#\\?.*a=([\s0-9:v]+).*?#msi';
347 preg_match($pattern, $url, $asset_id);
349 if (!empty($asset_id[0])) {
350 $asset = $am->getAsset(trim($asset_id[1]),
'', TRUE);
353 if (empty($base_url)) {
358 if (parse_url($url, PHP_URL_SCHEME) === NULL) {
359 $url = relative_to_absolute_url($url, $base_url);
366 extract(parse_url($url));
367 $host_path = $host.$path;
368 if ($scheme ===
'http' || $scheme ===
'https') {
369 $root_url = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
370 $is_matrix_url = FALSE;
371 foreach($root_url as $one_root_url) {
372 if (strpos($host_path, $one_root_url) === 0) {
373 $is_matrix_url = TRUE;
378 if ($is_matrix_url) {
382 $rm = $am->getSystemAsset(
'remap_manager');
384 $search =
'#'.$scheme.
'\\://#';
386 $url_with_params = preg_replace($search,
'', $url);
388 $url_without_params = $host_path;
391 $final_url = $rm->getRemapFromURL($scheme, $url_with_params);
392 if ($final_url === FALSE && ($url_with_params !== $url_without_params)) {
394 $final_url = $rm->getRemapFromURL($scheme, $url_without_params);
397 if ($final_url !== FALSE) {
403 extract(parse_url($url));
406 $asset = $am->getAssetFromURL($scheme, $host_path, TRUE, TRUE);
418 if (!empty($asset) && $asset->status != SQ_STATUS_LIVE) {
420 $error_link[] = ($link_name ===
'') ? $error_message : $link_name;
421 $html = str_replace($whole_link, $link_name, $html);
427 $live_link_only_error = translate(
'live_link_only_error');
428 foreach ($error_link as $one_link) {
429 $error_message = $live_link_only_error.
' \''.$one_link.
'\'';
430 trigger_error($error_message, E_USER_WARNING);
453 function relative_to_absolute_url($relative, $base)
456 if (parse_url($relative, PHP_URL_SCHEME) !=
'')
return $relative;
459 if ($relative[0]==
'#' || $relative[0]==
'?')
return $base.$relative;
460 if (strpos($relative,
'./?') === 0)
return $base.substr($relative, 1);
464 extract(parse_url($base));
467 if ($relative[0] ==
'/') $path =
'';
470 $absolute =
"$host$path/$relative";
473 $re = Array(
'#(/\.?/)#',
'#/(?!\.\.)[^/]+/\.\./#');
474 for ($n=1; $n>0; $absolute=preg_replace($re,
'/', $absolute, -1, $n)) {}
477 return $scheme.
'://'.$absolute;