wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.URIUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.URIUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * @class URIUtil
22  * @ingroup Util
23  * @brief URIUtil provides support for uri manipulation.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 class URIUtil
28 {
29  /**
30  * Convert an absolute URI to a relative
31  * code from http://www.webmasterworld.com/forum88/334.htm
32  * @param abs_uri Absolute URI to convert
33  * @param base Base URI
34  */
35  function makeRelative($abs_uri, $base)
36  {
37  $abs_uri = preg_replace("{^[^:]+://[^/]+}", '', $abs_uri);
38  $base = preg_replace("{^[^:]+://[^/]+}", '', $base);
39 
40  $abs_array = explode('/', $abs_uri);
41  $base_array = explode('/', $base);
42 
43  // remove trailing file names
44  $fileName = '';
45  if (strrpos($abs_uri, '/') !== strlen($abs_uri))
46  $fileName = array_pop($abs_array);
47  if (strrpos($base, '/') !== strlen($base))
48  array_pop($base_array);
49 
50  // ignore common path
51  while ($abs_array[0] == $base_array[0] && sizeof($abs_array) > 0)
52  {
53  array_shift($abs_array);
54  array_shift($base_array);
55  }
56 
57  // construct connecting path
58  $rel_uri = str_repeat('../', sizeof($base_array)).join('/', $abs_array).'/'.$fileName;
59  return $rel_uri;
60  }
61  /**
62  * Convert a relative URI to an absolute
63  * code from http://www.webmasterworld.com/forum88/334.htm
64  * @param rel_uri Relative URI to convert
65  * @param base Base URI
66  * @param REMOVE_LEADING_DOTS True/False wether to remove leading dots or not [default: true]
67  */
68  function makeAbsolute($rel_uri, $base, $REMOVE_LEADING_DOTS = true)
69  {
70  preg_match("'^([^:]+://[^/]+)/'", $base, $m);
71  $base_start = $m[1];
72  if (preg_match("'^/'", $rel_uri))
73  return $base_start.$rel_uri;
74 
75  $base = preg_replace("{[^/]+$}", '', $base);
76  $base .= $rel_uri;
77  $base = preg_replace("{^[^:]+://[^/]+}", '', $base);
78  $base_array = explode('/', $base);
79  if (count($base_array) and !strlen($base_array[0]))
80  array_shift($base_array);
81 
82  $i = 1;
83  while ($i < count($base_array))
84  {
85  if ($base_array[$i - 1] == ".")
86  {
87  array_splice($base_array, $i - 1, 1);
88  if ($i > 1) $i--;
89  }
90  elseif ($base_array[$i] == ".." and $base_array[$i - 1]!= "..")
91  {
92  array_splice($base_array, $i - 1, 2);
93  if ($i > 1)
94  {
95  $i--;
96  if ($i == count($base_array))
97  array_push($base_array, "");
98  }
99  }
100  else
101  {
102  $i++;
103  }
104  }
105 
106  if (count($base_array) and $base_array[-1] == ".")
107  $base_array[-1] = "";
108 
109  /* How do we treat the case where there are still some leading ../
110  segments left? According to RFC2396 we are free to handle that
111  any way we want. The default is to remove them.
112  #
113  "If the resulting buffer string still begins with one or more
114  complete path segments of "..", then the reference is considered
115  to be in error. Implementations may handle this error by
116  retaining these components in the resolved path (i.e., treating
117  them as part of the final URI), by removing them from the
118  resolved path (i.e., discarding relative levels above the root),
119  or by avoiding traversal of the reference."
120  #
121  http://www.faqs.org/rfcs/rfc2396.html 5.2.6.g
122  */
123 
124  if ($REMOVE_LEADING_DOTS)
125  {
126  while (count($base_array) and preg_match("/^\.\.?$/", $base_array[0]))
127  array_shift($base_array);
128  }
129  return($base_start . '/' . implode("/", $base_array));
130  }
131  /**
132  * Translate a relative URI from one location to the script location.
133  * For example if a file path is stored relative to location A and should be
134  * translated to the script URI (location B), use
135  * URIUtil::translate($filepathAsSeenFromA, $pathFromBtoA)
136  * @param rel_uri Relative URI to translate as seen from base
137  * @param base Base URI
138  * @return An associtative array with keys 'absolute' and 'relative'
139  * and the absolute and relative URI (as seen from the executed script) as values
140  */
141  function translate($rel_uri, $base)
142  {
143  $self = UriUtil::getProtocolStr().$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
144  $path = dirname($self).'/';
145  $absUrl = URIUtil::makeAbsolute($rel_uri, $path.$base);
146  $relUrl = URIUtil::makeRelative($absUrl, $self);
147 
148  return array('absolute' => $absUrl, 'relative' => $relUrl);
149  }
150  /**
151  * Check if an url is available (HTTP-Code: 200)
152  * code from http://de.php.net/fsockopen
153  * @param url The url to check
154  * @param timeout The timeout in seconds (default: 10)
155  * @return True/False wether the url is available
156  */
157  function validateUrl($url, $timeout=10)
158  {
159  $url_parts = @parse_url($url);
160  if (empty($url_parts["host"]))
161  {
162  // check local relative url
163  $fh = @fopen($url, "r");
164  if ($fh === false)
165  return(false);
166  else
167  return(true);
168  }
169 
170  if (!empty($url_parts["path"]))
171  $documentpath = $url_parts["path"];
172  else
173  $documentpath = "/";
174 
175  if (!empty($url_parts["query"]))
176  $documentpath .= "?" . $url_parts["query"];
177 
178  $host = $url_parts["host"];
179  $port = $url_parts["port"];
180 
181  // Now (HTTP-)GET $documentpath at $host";
182  if (empty($port))
183  $port = "80";
184 
185  $socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
186  if (!$socket)
187  return(false);
188  else
189  {
190  fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
191  $http_response = fgets($socket, 22);
192  preg_match('/.+ ([0-9]{3}) .+/', $http_response, $matches);
193  if (intval($matches[1]) < 400)
194  {
195  return(true);
196  fclose($socket);
197  }
198  else
199  {
200  if (Log::isDebugEnabled(__CLASS__))
201  Log::debug("$url: HTTP-Response: $http_response", __CLASS__);
202  return(false);
203  }
204  }
205  }
206  /*
207  * Get the protocol string (http:// or https://)
208  * @return The protocol string
209  */
210  function getProtocolStr()
211  {
212  if (strlen($_SERVER['HTTPS']) > 0 && $_SERVER['HTTPS'] != 'off')
213  return 'https://';
214  else
215  return 'http://';
216  }
217  /**
218  * Get the current page url
219  * @return The url of the page
220  */
221  function getPageURL()
222  {
223  $pageURL = URIUtil::getProtocolStr();
224  if ($_SERVER["SERVER_PORT"] != "80") {
225  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
226  }
227  else {
228  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
229  }
230  return $pageURL;
231  }
232 }
233 ?>
debug($message, $category)
Definition: class.Log.php:39
makeRelative($abs_uri, $base)
makeAbsolute($rel_uri, $base, $REMOVE_LEADING_DOTS=true)
isDebugEnabled($category)
Definition: class.Log.php:89
URIUtil provides support for uri manipulation.
validateUrl($url, $timeout=10)
translate($rel_uri, $base)