-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileutils.php
More file actions
executable file
·294 lines (260 loc) · 6.56 KB
/
Copy pathfileutils.php
File metadata and controls
executable file
·294 lines (260 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
require_once("config.php");
function CheckPermissions()
{
$filesToCheck=array('nextnodeid.txt','nextchangesetid.txt','nextwayid.txt','db.lock');
foreach($filesToCheck as $f)
if(!is_writable($f))
{
header('HTTP/1.1 500 Internal Server Error');
echo $f.' is not writable';
exit();
}
}
function GetServerRequestMethod()
{
global $PROG_ARG_LONG;
$options = getopt(PROG_ARG_STRING, $PROG_ARG_LONG);
$out = "GET"; //The default
if(isset($options["m"]))
$out = $options["m"];
if(isset($_SERVER['REQUEST_METHOD']))
$out = $_SERVER['REQUEST_METHOD'];
if(isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']))
$out = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
return $out;
}
function RequireMethod($reqMethod)
{
if(strcmp(GetServerRequestMethod(),$reqMethod)!=0 and !DEBUG_MODE)
{
header('HTTP/1.1 405 Method Not Allowed');
echo "Only method ".$reqMethod." is supported on this URI";
exit;
}
}
function getDirectory( $path = '.', $level = 0 )
{
$out=array();
// Directories to ignore when listing output.
$ignore = array( '.', '..' );
// Open the directory to the handle $dh
$dh = @opendir( $path );
// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) )
{
// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) )
{
// Show directories only
if(is_dir( "$path/$file" ) )
{
//echo $file."\n";
array_push($out,$file);
}
}
}
// Close the directory handle
closedir( $dh );
return $out;
}
function ReadAndIncrementFileNum($filename)
{
chdir(dirname(realpath (__FILE__)));
//This needs to be thread safe
$fp = fopen($filename, "r+t");
while (1)
{
$wouldblock = null;
$ret = flock($fp, LOCK_EX, $wouldblock);// do an exclusive lock
if($ret == false) {throw new Exception('Lock failed.');}
$out = (int)fread($fp,1024);
if($out==0) $out = 1; //Disallow changeset to be zero
fseek($fp,0);
ftruncate($fp, 0); // truncate file
fwrite($fp, $out+1);
flock($fp, LOCK_UN); // release the lock
fclose($fp);
return $out;
}
return null;
}
function ReadFileNum($filename)
{
chdir(dirname(realpath (__FILE__)));
//This needs to be thread safe
$fp = fopen($filename, "r+t");
while (1)
{
$wouldblock = null;
$ret = flock($fp, LOCK_EX, $wouldblock);// do an exclusive lock
if($ret == false) {throw new Exception('Lock failed.');}
$out = (int)fread($fp,1024);
if($out==0) $out = 1; //Disallow changeset to be zero
flock($fp, LOCK_UN); // release the lock
fclose($fp);
return $out;
}
return null;
}
function SetFileNum($filename, $id)
{
chdir(dirname(realpath (__FILE__)));
//This needs to be thread safe
$fp = fopen($filename, "w+t");
$wouldblock = null;
$ret = flock($fp, LOCK_EX, $wouldblock);// do an exclusive lock
if($ret == false) {throw new Exception('Lock failed.');}
ftruncate($fp, 0); // truncate file
fwrite($fp, $id);
flock($fp, LOCK_UN); // release the lock
fclose($fp);
}
//http://www.codewalkers.com/c/a/File-Manipulation-Code/Recursive-Delete-Function/
function RecursiveDeleteFolder($dirname)
{ // recursive function to delete
// all subdirectories and contents:
if(is_dir($dirname))$dir_handle=opendir($dirname);
while($file=readdir($dir_handle))
{
if($file!="." && $file!="..")
{
if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file);
else RecursiveDeleteFolder($dirname."/".$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
class Lock
{
var $fp = null;
var $lockObj = null;
function __construct($write=0)
{
//echo "Getting Lock...\n";
if(!$write)
{
chdir(dirname(realpath (__FILE__)));
//To unlock, let the returned object go out of scope
$this->fp = fopen("db.lock", "w");
$this->lockObj = flock($this->fp, LOCK_SH);
}
else
{
chdir(dirname(realpath (__FILE__)));
//To unlock, let the returned object go out of scope
$this->fp = fopen("db.lock", "w");
$this->lockObj = flock($this->fp, LOCK_EX);
}
}
function __destruct()
{
//echo "Releasing Lock...\n";
flock($this->fp, LOCK_UN);
}
}
function GetReadDatabaseLock()
{
return new Lock(0);
}
function GetWriteDatabaseLock()
{
return new Lock(1);
}
function isValidEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function ValidateBbox($bbox)
{
dprint( "ValidateBbox:",$bbox);
if(!is_array($bbox) or count($bbox) != 4) return "invalid-bbox";
if($bbox[0] > $bbox[2]){
// swap em!
$tmp = $bbox[0];
$bbox[2]=$bbox[0];
$bbox[0]=$tmp;
}
if($bbox[1] > $bbox[3])
{
// swap em!
$tmp = $bbox[1];
$bbox[3]=$bbox[1];
$bbox[1]=$tmp;
}
if($bbox[0] < -180.0 or $bbox[0] > 180.0) return "invalid-bbox";
if($bbox[2] < -180.0 or $bbox[2] > 180.0) return "invalid-bbox";
if($bbox[1] < -90.0 or $bbox[1] > 90.0) return "invalid-bbox";
if($bbox[3] < -90.0 or $bbox[3] > 90.0) return "invalid-bbox";
$area = abs((float)$bbox[2] - (float)$bbox[0]) * ((float)$bbox[3] - (float)$bbox[1]);
global $PROG_ARG_LONG;
$options = getopt(PROG_ARG_STRING, $PROG_ARG_LONG);
if($area > MAX_QUERY_AREA and !isset($options['big-query']))
{
return "bbox-too-large";
}
return $bbox;
}
function UpdateBbox(&$original,$new)
{
//Expand bbox to contain new area
if(!is_array($new)) return;
if(!is_array($original)) {$original = $new; return;}
if($new[0] < $original[0]) $original[0] = $new[0];
if($new[1] < $original[1]) $original[1] = $new[1];
if($new[2] > $original[2]) $original[2] = $new[2];
if($new[3] > $original[3]) $original[3] = $new[3];
}
function GetRequestPath()
{
//Convert path to internally usable format
if(isset($_SERVER['PATH_INFO']))
{
$pathInfo = $_SERVER['PATH_INFO'];
}
if(!isset($pathInfo) and isset($_SERVER['REDIRECT_URL']))
{
$pathInfo = $_SERVER['REDIRECT_URL'];
$pathInfoExp = explode("/",$pathInfo);
//TODO is there a better way then using INSTALL_FOLDER_DEPTH?
$pathInfo = "/".implode("/",array_slice($pathInfoExp,INSTALL_FOLDER_DEPTH));
}
global $PROG_ARG_LONG;
$options = getopt(PROG_ARG_STRING, $PROG_ARG_LONG);
if(!isset($pathInfo))
{
$pathInfo= $options["p"];
}
if(!isset($pathInfo))
{
//print_r($_SERVER);
die("Could not determine URL path, no -p option on the command line.\n" );
}
return $pathInfo;
}
function dprint($name,$value)
{
// debug_print_backtrace();
error_log("DPRINT:". $name. ":". print_r($value,true) . "\n<",0);
}
//Allow GET args to be set by command line
function CommandLineOptionsSetVar($opts, $existVars)
{
if($existVars === Null) $out = array();
else $out = $existVars;
if($opts !== Null)
{
if(!is_array($opts)) $opts = array($opts);
foreach ($opts as &$value)
{
$kv = explode("=",$value);
if (isset($kv[1]))
$out[$kv[0]]=$kv[1];
else
$out[$kv[0]]="";
}
}
return $out;
}
?>