Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
www.inc
1 <?php
41 function relative_href($from,$to)
42 {
43  // type verification
44  if (!is_string($from) || !is_string($to)) {
45  return '';
46  }
47 
48  // check to see if we are indeed recaching
49  $recaching = FALSE;
50  if (isset( $_SERVER['REQUEST_URI']) && preg_match('/\/'.SQ_CONF_RECACHE_SUFFIX.'$/', $_SERVER['REQUEST_URI'])) {
51  $recaching = TRUE;
52  }
53 
54  $from_bits = preg_split('/\\/+/', trim($from));
55  $to_bits = preg_split('/\\/+/', trim($to));
56 
57  $splitted = preg_split('/:\\/\\//', $to);
58 
59  // check to see if we have the whole array to play with, else just continue with regular stuff
60  if(isset($splitted[0]) && isset($splitted[1])) {
61  $to_asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL($splitted[0], $splitted[1], TRUE, TRUE);
62  // bug fix #3740 asset_lnk variable in parse file not returning correct URL
63  // if we are recaching let not send keyword replacement like ../../whatever
64  // the ../../ will break the URL when initially they are using _recache and
65  // later if the try it without the _recache
66  if ($recaching && (!is_null($to_asset) && $to_asset->id > 0)) return './?a='.$to_asset->id;
67  }
68 
69 
70  // Ensure the first element is the protocol
71  if (!preg_match('/^[A-Za-z]+:$/', $from_bits[0])) {
72  array_unshift($from_bits, 'http:');
73  }
74  if (!preg_match('/^[A-Za-z]+:$/', $to_bits[0] )) {
75  array_unshift($to_bits, 'http:');
76  }
77  // Different protocols or domains? ABSOLUTE HREF!
78  if (strtolower($from_bits[0]) != strtolower($to_bits[0]) || strtolower($from_bits[1]) != strtolower($to_bits[1])) {
79  $to_bits[0] .= '/';
80  return implode('/', $to_bits);
81  }
82 
83  // Different first directories? Root path!
84  if (empty($from_bits[2]) || empty($to_bits[2]) || $from_bits[2] != $to_bits[2]) {
85  // Shift off protocol and domain
86  array_splice($to_bits, 0, 2);
87  return '/'.implode('/', $to_bits);
88  }
89 
90  // Start from the second directory and find the place where the urls start to vary
91  $split_point = 3;
92  while (!empty($from_bits[$split_point])
93  && !empty($to_bits[$split_point])
94  && $from_bits[$split_point] == $to_bits[$split_point]) {
95  $split_point++;
96  }
97 
98  $href = str_repeat('../', count($from_bits) - $split_point);
99  // Forward to the destination
100  for ($i = $split_point - 1, $max = count($to_bits); $i < $max; $i++) {
101  $href .= $to_bits[$i].'/';
102  }
103 
104  // if they wanted a trailing slash make sure there is one, else remove it
105  if (substr($to, -1) == '/') {
106  $href = preg_replace('/\\/+$/', '/', $href);
107  } else {
108  $href = preg_replace('/\\/$/', '', $href);
109  }
110 
111  // No remove any bit where we go back a directory then forward again to the
112  // same last common directory
113  return str_replace('../'.$from_bits[$split_point - 1].'/', '', $href);
114 
115 }//end relative_href()
116 
117 
126 function valid_email($e)
127 {
128  if (!is_string($e)) return FALSE;
129 
130  $local = '0-9a-zA-Z_\+\-'.'&\'\*\/=\?\^\{\}~';
131 
132  // dot character cannot be the first or last character in the local-part
133  // and it cannot appear two or more times consecutively
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)) {
135  // matches email with display name, for example, 'Someone <some.one@squiz.net>'
136  return TRUE;
137  } else if (preg_match('/^(['.$local.']+(\.['.$local.']+)*@(((?:[\da-zA-Z]|[\da-zA-Z][\'-\w]*[\da-zA-Z])\.)+[a-zA-Z]{2,7}))$/', $e)) {
138  // matches normal email address
139  return TRUE;
140  } else {
141  return FALSE;
142  }
143 
144 }//end valid_email()
145 
146 
156 function valid_url($url, $schemes=Array('http','https'))
157 {
158  return preg_match('/^('.implode('|', $schemes).'):\/\/[a-z0-9]+(([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,})?((:[0-9]{1,5})?\/.*)?$/i', $url);
159 
160 }//end valid_url()
161 
162 
171 function make_raw_post_data($data)
172 {
173  // type verification
174  if (!is_array($data) || empty($data)) {
175  return '';
176  }
177 
178  $val_array = NULL;
179 
180  foreach ($data as $name => $value) {
181  $val_array[] = recursive_make_raw_post_data($name, $value);
182  }
183 
184  $result = '';
185  if (!empty($val_array)) {
186  $result = implode('&', $val_array);
187  }
188 
189  return $result;
190 
191 }//end make_raw_post_data()
192 
193 
203 function recursive_make_raw_post_data($name, $data)
204 {
205  if (!is_array($data)) {
206  return $name.'='.rawurlencode($data);
207  } else {
208  $val_array=Array();
209 
210  if (empty($data)) return $name.'=';
211 
212  foreach ($data as $key => $value) {
213  $val_array[] = recursive_make_raw_post_data($name.'['.$key.']', $value);
214  }
215  return implode('&', $val_array);
216  }
217 
218 }//end recursive_make_raw_post_data()
219 
220 
229 function undo_htmlspecialchars($string)
230 {
231  $string = str_replace('&amp;','&',$string);
232  $string = str_replace('&#039;',"'",$string);
233  $string = str_replace('&quot;','"',$string);
234  $string = str_replace('&lt;','<',$string);
235  $string = str_replace('&gt;','>',$string);
236 
237  return $string;
238 
239 }//end undo_htmlspecialchars()
240 
241 
259 function make_valid_html_id($id, $replace_char='')
260 {
261  if (!preg_match('/^[A-Za-z0-9:_.-]*$/', $replace_char)) {
262  return FALSE;
263  }
264  $id = preg_replace('/[^A-Za-z0-9:_.-]/', $replace_char, $id);
265 
266  // keep dropping first character until we get a valid one
267  while (!empty($id) && preg_match('/[^A-Za-z]/', $id{0})) {
268  $id = substr($id, 1);
269  }
270  return $id;
271 
272 }//end make_valid_html_id()
273 
274 
283 function valid_ipaddress($address)
284 {
285  $ips = explode('.', $address);
286 
287  if (4 != count($ips)) return FALSE;
288 
289  foreach ($ips as $ip) {
290  if (!ctype_digit($ip)) return FALSE;
291  $val = intval($ip);
292  if ((0 > $ip) || (255 < $ip)) {
293  return FALSE;
294  }
295  }
296  return TRUE;
297 
298 }//end valid_ipaddress()
299 
300 
311 function check_non_live_link($html, $owner_id)
312 {
313  $am = $GLOBALS['SQ_SYSTEM']->am;
314  $pattern = '#<a .*?href\s*=\s*(?:\'|")([^\'"]*?)(?:\'|")[^>]*>([^<]*)</a>#msi';
315  $error_link = Array();
316  $error_found = FALSE;
317 
318  preg_match_all($pattern, $html, $match);
319  if (!empty($match)) {
320  /*
321  * find base url from the owner or its parents
322  */
323  $owner = $am->getAsset($owner_id);
324  if ($owner instanceof Content_Type) {
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;
329  break 1;
330  }
331  $base_url = $am->getAssetURL($parent_id);
332  }
333  } else {
334  $base_url = $am->getAssetURL($owner_id);
335  }
336 
337  foreach ($match[1] as $index => $url) {
338  $whole_link = $match[0][$index];
339  $href = $match[1][$index];
340  $link_name = $match[2][$index];
341 
342  if (empty($url)) continue;
343 
344  if (empty($link_name)) $error_message = $href;
345 
346  $pattern = '#\\?.*a=([\s0-9:v]+).*?#msi';
347  preg_match($pattern, $url, $asset_id);
348  // if url is like './?a=250:v1'
349  if (!empty($asset_id[0])) {
350  $asset = $am->getAsset(trim($asset_id[1]), '', TRUE);
351  } else {
352 
353  if (empty($base_url)) {
354  continue;
355  }
356 
357  // if there is no protocol, the url must be relative.
358  if (parse_url($url, PHP_URL_SCHEME) === NULL) {
359  $url = relative_to_absolute_url($url, $base_url);
360  }
361 
362  $scheme = '';
363  $host = '';
364  $path = '';
365  // parse base URL and convert to local variables: $scheme, $host, $path
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;
374  break 1;
375  }
376  }
377 
378  if ($is_matrix_url) {
379  /*
380  * find out whether url is remapped
381  */
382  $rm = $am->getSystemAsset('remap_manager');
383 
384  $search = '#'.$scheme.'\\://#';
385  // This URL may contain parameters
386  $url_with_params = preg_replace($search, '', $url);
387  // This URL is without parameters
388  $url_without_params = $host_path;
389 
390  // Firstly, look for an exact match in the database
391  $final_url = $rm->getRemapFromURL($scheme, $url_with_params);
392  if ($final_url === FALSE && ($url_with_params !== $url_without_params)) {
393  // Fall back to the URL without parameters if there is no a match
394  $final_url = $rm->getRemapFromURL($scheme, $url_without_params);
395  }
396 
397  if ($final_url !== FALSE) {
398  $url = $final_url;
399  $scheme = '';
400  $host = '';
401  $path = '';
402  // parse base URL again and convert to local variables: $scheme, $host, $path
403  extract(parse_url($url));
404  }
405 
406  $asset = $am->getAssetFromURL($scheme, $host_path, TRUE, TRUE);
407  } else {
408  // don't worry about non-Matrix url or incorrect url
409  continue;
410  } // end if ($is_matrix_url)
411 
412  } else {
413  // don't worry about other protocol or incorrect url
414  continue;
415  }// end if ($scheme === 'http' || $scheme === 'https')
416  }
417 
418  if (!empty($asset) && $asset->status != SQ_STATUS_LIVE) {
419  $error_found = TRUE;
420  $error_link[] = ($link_name === '') ? $error_message : $link_name;
421  $html = str_replace($whole_link, $link_name, $html);
422  }
423  }// end checking each url
424  }
425 
426  if ($error_found) {
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);
431  }
432  }
433  return $html;
434 
435 }//end check_non_live_link()
436 
437 
453 function relative_to_absolute_url($relative, $base)
454 {
455  /* return if already absolute URL */
456  if (parse_url($relative, PHP_URL_SCHEME) != '') return $relative;
457 
458  /* queries and anchors */
459  if ($relative[0]=='#' || $relative[0]=='?') return $base.$relative;
460  if (strpos($relative, './?') === 0) return $base.substr($relative, 1);
461 
462  /* parse base URL and convert to local variables:
463  $scheme, $host, $path */
464  extract(parse_url($base));
465 
466  /* destroy path if relative url points to root */
467  if ($relative[0] == '/') $path = '';
468 
469  /* dirty absolute URL */
470  $absolute = "$host$path/$relative";
471 
472  /* replace '//' or '/./' or '/foo/../' with '/' */
473  $re = Array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
474  for ($n=1; $n>0; $absolute=preg_replace($re, '/', $absolute, -1, $n)) {}
475 
476  /* absolute URL is ready! */
477  return $scheme.'://'.$absolute;
478 
479 }
480 
481 ?>