comparison common-functions.inc.php @ 134:b6b4a58c7625

Using .inc.php rather than just .inc for include files.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Sun, 22 Jan 2023 19:22:00 +0100
parents common-functions.inc@6b882fb6ea46
children
comparison
equal deleted inserted replaced
133:00255ca89459 134:b6b4a58c7625
1 <?php
2 /**
3 * @file
4 * Functionality which doesn't belong anywhere else
5 */
6 include_once 'ScriptIncludeCache.inc.php';
7
8 /// @cond
9 $baseDir = dirname(__FILE__);
10
11 $cache = ScriptIncludeCache::instance(__FILE__);
12 $cache->includeOnce('StatusCodes.inc.php', $baseDir);
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 */
23 function repMapString($map)
24 {
25 $opt = 'array(';
26 $start = True;
27
28 foreach($map as $param => $value) {
29 if ($start) {
30 $start = False;
31 $opt .= "\"${param}\" => \"${value}\"";
32 }
33 else {
34 $opt .= ", \"${param}\" => \"${value}\"";
35 }
36 }
37 $opt .= ')';
38 return $opt;
39 }
40
41 /**
42 * Get the location on the server where the top level script is
43 * located
44 *
45 * @return directory
46 */
47 function basePath()
48 {
49 $l = strrpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME']);
50 return substr($_SERVER['SCRIPT_FILENAME'], 0, $l);
51 }
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 */
64 function loadFile($sFilename, $sCharset = 'UTF-8')
65 {
66 if (floatval(phpversion()) >= 4.3) {
67 if (!file_exists($sFilename))
68 return -3;
69 $sData = file_get_contents($sFilename);
70 }
71 else {
72 if (!file_exists($sFilename))
73 return -3;
74 $rHandle = fopen($sFilename, 'r');
75 if (!$rHandle)
76 return -2;
77
78 $sData = '';
79 while(!feof($rHandle))
80 $sData .= fread($rHandle, filesize($sFilename));
81 fclose($rHandle);
82 }
83 if ($sEncoding = mb_detect_encoding($sData, 'auto', true) != $sCharset) {
84 if ($sEncoding != 1) {
85 $sData = mb_convert_encoding($sData, $sCharset, $sEncoding);
86 }
87 }
88 return $sData;
89 }
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 */
97 function errorPage($errorText, $errorCode = 403)
98 {
99 header(StatusCodes::httpHeaderFor($errorCode));
100 print '<?xml version="1.0" encoding="UTF-8"?>';
101 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
102 <html xmlns="http://www.w3.org/1999/xhtml"><head>';
103
104 print '<title>' . StatusCodes::httpHeaderFor($errorCode) . '</title>';
105 print '</head><body>';
106 print "<div id=\"page\"><h1>${errorText}</h1></div>";
107 print '</body></html>';
108
109 exit;
110 }
111
112 /**
113 * Generates an URL for the specified parameters
114 *
115 * @param $urlParams an associative array of the values already set
116 * @param $keys a set of values to override $urlParams
117 *
118 * @param $nonQueryParams a list of keys, where values should be in
119 * the URL, rather than the query part, note that the order is
120 * important
121 */
122 function genUrl($urlParams, $keys = array(), $nonQueryParams = array()) {
123 $out = '';
124 $first = 1;
125 $new_params = $urlParams;
126 foreach($keys as $param => $val) {
127 $new_params[$param] = $val;
128 }
129
130 $numEncParams=0;
131 foreach($nonQueryParams as $nqp) {
132 if (array_key_exists($nqp, $new_params)) {
133 ++$numEncParams;
134 $val = $new_params[$nqp];
135 if ($val)
136 $out .= "/${val}";
137 unset($new_params[$nqp]);
138 }
139 }
140 if ($numEncParams<count($nonQueryParams)) {
141 $out .= '/';
142 }
143
144 foreach($new_params as $param => $val) {
145 if ($val) {
146 if($first) {
147 $first = 0;
148 $out .= "?";
149 }
150 else
151 $out .= "&amp;";
152 $out .= urlencode($param) . '=' . urlencode($val);
153 }
154 }
155
156 return $out;
157 }
158
159 /**
160 * Retrieves a single subelement
161 * @throw Exception if number of elements are different from 1
162 *
163 * @todo Throw more specific exception
164 *
165 * @param $obj the xml element to search in
166 * @param $name the name of the element to search for
167 */
168 function getElementByTagName($obj, $name) {
169 $elems = $obj->getElementsByTagName($name);
170 if ($elems->length != 1) {
171 throw new UnexpectedValueException("More than one tag with name \"${name}\"");
172 }
173 $elem = $elems->item(0);
174 return $elem;
175 }
176
177 /**
178 * Checks if one string start 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 */
185 function startswith($haystack, $needle)
186 {
187 return strpos($haystack, $needle) === 0;
188 }
189
190 /**
191 * Checks if one string ends with another string
192 *
193 * @param $haystack the string to search
194 * @param $needle the string to search for
195 *
196 * @return bool if match
197 */
198 function endsWith($haystack, $needle)
199 {
200 $l = strlen($haystack) - strlen($needle);
201 return strrpos($haystack, $needle) === $l;
202 }
203
204 /**
205 * Generates the query part of an URI
206 *
207 * @param $opts an associative array of options
208 * @return a string that can be used for the query part of an URI
209 */
210 function opttostring($opts)
211 {
212 $str = '';
213 foreach (array_keys($opts) as $key) {
214 $value = $opts[$key];
215 if ($str) {
216 $str .= "&${key}=${value}";
217 }
218 else {
219 $str = "?${key}=${value}";
220 }
221 }
222 return $str;
223 }
224 ?>