comparison common-functions.inc @ 66:74f7b64bdb78

Lots of documentation fixes, and removal of unused function getXmlContent
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 22:11:33 +0200
parents 13d899b748b7
children 4dfa3f6a2dc1
comparison
equal deleted inserted replaced
65:7b87ec8b0842 66:74f7b64bdb78
3 * @file 3 * @file
4 * Functionality which doesn't belong anywhere else 4 * Functionality which doesn't belong anywhere else
5 */ 5 */
6 include_once 'CacheTimeCheck.inc'; 6 include_once 'CacheTimeCheck.inc';
7 7
8 /// @cond
8 $baseDir = dirname(__FILE__); 9 $baseDir = dirname(__FILE__);
9 10
10 $cache = CacheTimeCheck::instance(__FILE__); 11 $cache = CacheTimeCheck::instance(__FILE__);
11 $cache->includeOnce('StatusCodes.inc', $baseDir); 12 $cache->includeOnce('StatusCodes.inc', $baseDir);
12 13 /// @endcond
14
15 /**
16 * Generates a representation for an array of key => value pairs
17 *
18 * @note Behaviour is undefined if value is a composite structure.
19 *
20 * @param $map the input array
21 * @return a string representation that may be eval'ed
22 */
13 function repMapString($map) 23 function repMapString($map)
14 { 24 {
15 $opt = 'array('; 25 $opt = 'array(';
16 $start = True; 26 $start = True;
17 27
26 } 36 }
27 $opt .= ')'; 37 $opt .= ')';
28 return $opt; 38 return $opt;
29 } 39 }
30 40
41 /**
42 * Get the location on the server where the top level script is
43 * located
44 *
45 * @return directory
46 */
31 function basePath() 47 function basePath()
32 { 48 {
33 $l = strrpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME']); 49 $l = strrpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME']);
34 return substr($_SERVER['SCRIPT_FILENAME'], 0, $l); 50 return substr($_SERVER['SCRIPT_FILENAME'], 0, $l);
35 } 51 }
36 52
53 /**
54 * Loads a file
55 *
56 * @param $sFilename name of the file to load
57 * @param $sCharset the character encoding of the file
58 *
59 * @todo make this function throw instead of returning codes
60 *
61 * @return the contents of the file, or a status code (-3 if file does
62 * not exists, if file could not be opened -2)
63 */
37 function loadFile($sFilename, $sCharset = 'UTF-8') 64 function loadFile($sFilename, $sCharset = 'UTF-8')
38 { 65 {
39 if (floatval(phpversion()) >= 4.3) { 66 if (floatval(phpversion()) >= 4.3) {
40 if (!file_exists($sFilename)) 67 if (!file_exists($sFilename))
41 return -3; 68 return -3;
59 } 86 }
60 } 87 }
61 return $sData; 88 return $sData;
62 } 89 }
63 90
91 /**
92 * Generate a status page and exit
93 *
94 * @param $errorText the text to be displayed in the body
95 * @param $errorCode the status code to be served
96 */
64 function errorPage($errorText, $errorCode = 403) 97 function errorPage($errorText, $errorCode = 403)
65 { 98 {
66 header(StatusCodes::httpHeaderFor($errorCode)); 99 header(StatusCodes::httpHeaderFor($errorCode));
67 print "<div id=\"page\"><h1>${errorText}</h1></div>"; 100 print "<div id=\"page\"><h1>${errorText}</h1></div>";
68 exit; 101 exit;
69 } 102 }
70 103
104 /**
105 * Generates an URL for the specified parameters
106 *
107 * @param $urlParams an associative array of the values already set
108 * @param $keys a set of values to override $urlParams
109 *
110 * @param $nonQueryParams a list of keys, where values should be in
111 * the URL, rather than the query part, note that the order is
112 * important
113 */
71 function genUrl($urlParams, $keys = array(), $nonQueryParams = array()) { 114 function genUrl($urlParams, $keys = array(), $nonQueryParams = array()) {
72 $out = ''; 115 $out = '';
73 $first = 1; 116 $first = 1;
74 $new_params = $urlParams; 117 $new_params = $urlParams;
75 foreach($keys as $param => $val) { 118 foreach($keys as $param => $val) {
98 } 141 }
99 142
100 return $out; 143 return $out;
101 } 144 }
102 145
146 /**
147 * Retrieves a single subelement
148 * @throw Exception if number of elements are different from 1
149 *
150 * @todo Throw more specific exception
151 *
152 * @param $obj the xml element to search in
153 * @param $name the name of the element to search for
154 */
103 function getElementByTagName($obj, $name) { 155 function getElementByTagName($obj, $name) {
104 $elems = $obj->getElementsByTagName($name); 156 $elems = $obj->getElementsByTagName($name);
105 if ($elems->length != 1) { 157 if ($elems->length != 1) {
106 throw new Exception("More than one tag with name \"${name}\""); 158 throw new Exception("More than one tag with name \"${name}\"");
107 } 159 }
108 $elem = $elems->item(0); 160 $elem = $elems->item(0);
109 return $elem; 161 return $elem;
110 } 162 }
111 163
112 function getXmlContent($node) 164 /**
113 { 165 * Checks if one string start with another string
114 $text = $node->ownerDocument->saveXml($node); 166 *
115 $pattern = "/^<" . $node->tagName."[^>]*>/is"; 167 * @param $haystack the string to search
116 $text = preg_replace($pattern, '' , $text); 168 * @param $needle the string to search for
117 $pattern = '/<\/' . $node->tagName . '[^>]*>$/is'; 169 *
118 $text = preg_replace($pattern, '' , $text); 170 * @return bool if match
119 171 */
120 return $text;
121 }
122
123 function startswith($haystack, $needle) 172 function startswith($haystack, $needle)
124 { 173 {
125 return strpos($haystack, $needle) === 0; 174 return strpos($haystack, $needle) === 0;
126 } 175 }
127 176
177 /**
178 * Checks if one string ends with another string
179 *
180 * @param $haystack the string to search
181 * @param $needle the string to search for
182 *
183 * @return bool if match
184 */
128 function endsWith($haystack, $needle) 185 function endsWith($haystack, $needle)
129 { 186 {
130 $l = strlen($haystack) - strlen($needle); 187 $l = strlen($haystack) - strlen($needle);
131 return strrpos($haystack, $needle) === $l; 188 return strrpos($haystack, $needle) === $l;
132 } 189 }
133 190
191 /**
192 * Queries a URL for headers
193 *
194 * @param $url the url to query
195 * @return an associative array of all headers returned
196 */
134 function getHeaders($url) 197 function getHeaders($url)
135 { 198 {
136 $response = http_head($url, array("timeout"=>1), $info); 199 $response = http_head($url, array("timeout" => 1), $info);
137 $headers = array(); 200 $headers = array();
138 $str = explode("\n", $response); 201 $str = explode("\n", $response);
139 foreach($str as $kv) { 202 foreach($str as $kv) {
140 $p = strpos($kv, ":"); 203 $p = strpos($kv, ":");
141 if ($p) { 204 if ($p) {
145 } 208 }
146 } 209 }
147 return $headers; 210 return $headers;
148 } 211 }
149 212
213 /**
214 * Generates the query part of an URI
215 *
216 * @param $opts an associative array of options
217 * @return a string that can be used for the query part of an URI
218 */
150 function opttostring($opts) 219 function opttostring($opts)
151 { 220 {
152 $str = ''; 221 $str = '';
153 foreach (array_keys($opts) as $key) { 222 foreach (array_keys($opts) as $key) {
154 $value = $opts[$key]; 223 $value = $opts[$key];