-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcls_phpconfig.php
501 lines (433 loc) · 14 KB
/
cls_phpconfig.php
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?PHP
/**
*
* Asterisk configuration file interface
* Used as an aid in viewing and editing Asterisk confiuration
* files.
*
*
*
* phpconfig: v 1.0 2011/07/26 edit by [email protected]
* ported from trixbox to freepbx module ( name configedit-1.0.0.0.tgz )
*
* phpconfig:,v 1.0 2003/07/03 17:19:37
* Authors: Dave Packham <[email protected]>
* Rob Birkinshaw <[email protected]>
*/
class Open_Conf
{
// for application specific defaults
// use the phpconfig_init.php file //
//// DEFAULTS ////
// remark symbol in conf file
var $_OC_remark = ";";
// temporary directory
var $_OC_temp_dir = "/tmp";
// temporary file prefix
var $_OC_temp_prefix = "conf-";
// executable to read conf files
var $_OC_reset_cmd = "/usr/sbin/asterisk -rx \"module reload\"";
var $_OC_access_file = "/etc/asterisk/manager.conf";
// directory of configuration files
var $_OC_conf_dir = "/etc/asterisk";
// expression defining valid configuration files
var $_OC_conf_filter = "/.conf\$/";
// values expression
var $_OC_value_exp = "/^[^=;]*=>?\s*(\d[^\,\;]*)\s*[\,\;]*.*[\r\n]\$/";
// values expression replacement string
var $_OC_value_exp_replace = "\$1";
// assignment operators in conf files (ex. = or =>)
var $_OC_assignment_oper = "/^(=>?)|,*\$/";
//// SHOULD NOT CHANGE THESE ////
// fully qualified confFile
var $_OC_conf_file = "";
// md5 hash of the conf
var $_OC_md5 = "";
// valid configuration directories
var $_OC_conf_dirs = array();
// holds each line of conf file
var $_OC_the_file = array();
// stores each section, firstline and offset
var $_OC_the_sections = array();
// stores values (right side of "=" or "=>")
var $_OC_the_values = array();
//////////////////////////////////////////////////////////
/**
* Access functions
* Set Open Conf Global variables
*
* author: Rob Birkinshaw [email protected]
* param: $newvalue
* return:
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////
function OC_setRemark($newvalue) { $this->_OC_remark=$newvalue; }
function OC_setTempDir($newvalue) { $this->_OC_temp_dir=$newvalue; }
function OC_setTempPrefix($newvalue) { $this->_OC_temp_prefix=$newvalue; }
function OC_setConfDir($newvalue,$confdirs)
{
// check that new conf direcotry exists in the
// valid list of configuration directories
foreach($confdirs as $thedir)
{
if ($newvalue == $thedir)
{
$this->_OC_conf_dir=$newvalue;
return true;
}
}
return false;
}
function OC_setConfFilter($newvalue) { $this->_OC_conf_filter=$newvalue; }
function OC_setValueExp($newvalue) { $this->_OC_value_exp=$newvalue; }
function OC_setValueExp_replace($newvalue) { $this->_OC_value_exp_replace=$newvalue; }
function OC_setAssignmentOper($newvalue) { $this->_OC_assignment_oper=$newvalue; }
function OC_setAccessFile($newvalue) { $this->_OC_access_file=$newvalue; }
function OC_setResetCmd($newvalue) { $this->_OC_reset_cmd=$newvalue; }
function OC_setConfFile($newvalue) { $this->_OC_conf_file=$newvalue; }
function OC_setConfDirectories($newvalue)
{
// build directory name => link menu list
foreach ($newvalue as $item)
{
$this->_OC_conf_dirs[$item] = $PHP_SELF . '?type=tool&display=configedit&dir=' . $item;
}
}
//////////////////////////////////////////////////////////////
/**
* OC_readConfFile
* Reads conf file into array $_OC_the_file
*
* author: Rob Birkinshaw [email protected]
* param: $confFile string - Name of configuration file to read
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_readConfFile($confFile)
{
$this->OC_setConfFile($this->_OC_conf_dir . '/' . basename($confFile));
$this->_OC_the_file = array();
if (! is_file($this->_OC_conf_file))
{
return(false);
}
$file = fopen($this->_OC_conf_file, "r");
while (!feof($file))
{
$this->_OC_the_file[] = fgets($file);
}
fclose($file);
// generate MD5 hash of conf file to be used
// with update operation
$this->_OC_md5 = md5_file($this->_OC_conf_file);
// build array with each section found in conf file
if (! $this->_OC_readConfSections())
{
return(false);
}
return(true);
}
//////////////////////////////////////////////////////////////
/**
* _OC_readConfSections - Private function
* Finds sections in the filearray and its line boundries.
* Stored in $_OC_the_sections[]=array(section,firstline,offset)
*
* author: Rob Birkinshaw [email protected]
* param: none
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function _OC_readConfSections()
{
// first element of array is the entire conf file from line zero to EOF
$this->_OC_the_sections[] = array(basename($this->_OC_conf_file),0,count($this->_OC_the_file));
$linenumber == 0;
foreach($this->_OC_the_file as $line)
{
// look for sections
if(preg_match("/^\s*\[([^\]]*)\].*[\r\n]\$/", $line))
{
// parse for section text and add to section array
$section = preg_replace("/^\s*\[([^\]]*)\].*[\r\n]\$/","\$1",$line);
$this->_OC_the_sections[] = array(trim($section),$linenumber);
}
elseif ($linenumber == 0) // is file header (comments before first section)
{
$this->_OC_the_sections[] = array('Header',0);
}
$linenumber ++;
} // foreach line in file
// find firstline and offset for each section
$sectionCount = count($this->_OC_the_sections);
if ($sectionCount == 0)
{
return(false);
}
for ($i = 1; $i < $sectionCount; $i++)
{
if ($i == $sectionCount -1) //last section
{
$this->_OC_the_sections[$i][2] = count($this->_OC_the_file) - $this->_OC_the_sections[$i][1] -1;
}
else
{
$this->_OC_the_sections[$i][2] = $this->_OC_the_sections[$i+1][1] - $this->_OC_the_sections[$i][1];
}
}
return(true);
}
//////////////////////////////////////////////////////////////
/**
* OC_getConfSectionItems
* Reads supplied section into an array
*
* author: Rob Birkinshaw [email protected]
* param: $inSection string, name of section to read
* return: array or false
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_getConfSectionItems($inSection)
{
foreach($this->_OC_the_sections as $record)
{
list($name,$firstLine, $offset) = $record;
if ($name == $inSection)
{
$result = array_slice ($this->_OC_the_file, $firstLine,$offset);
return $result;
}
} // foreach section
return(false);
}
//////////////////////////////////////////////////////////////
/**
* OC_writeConfSection
* Writes supplied section out to tempfile then to original file
*
* author: Rob Birkinshaw [email protected]
* param: $originalMD5 string, md5 hash
* $updateSection string, section to update
* $section_text string, text to update section with
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_writeConfSection($originalMD5, $updateSection, $section_text)
{
// check if user has privs. to write
$access_result = $this->OC_checkAccess($_SESSION['valid_user']);
if (!$access_result)
{
return false;
}
// grab current hash for file to update
// and compare to old hash, abort if different
if ($originalMD5 != $this->_OC_md5)
{
die ('The file has changed between read and write.<br>Operation failed.<br>');
}
// parse textarea text back into an array
$newSection = preg_split("/\r\n/",$section_text);
// add linefeeds to each row and clean array
for ($i = 0; $i < count($newSection); $i++)
{
$newSection[$i] = $newSection[$i] . "\n";
}
// clean up extra line from html textarea control
if (strlen($newSection[count($newSection)-1]) == 1)
{
array_pop($newSection);
}
// splice in new section
foreach($this->_OC_the_sections as $record)
{
list($name,$lineStart, $offset) = $record;
if($name == $updateSection)
{
array_splice ($this->_OC_the_file, $lineStart, $offset, $newSection);
break;
}
}
// create and open temp file
$tempfile = tempnam($this->_OC_temp_dir, $this->_OC_temp_prefix);
if(! $tempfile)
{
return(false);
}
$transient = fopen($tempfile, "w");
// write file array out to tempfile
foreach ($this->_OC_the_file as $line)
{
fputs($transient, str_replace("\r\n", "\n", $line));
}
fclose($transient);
// copy tempfile to origional file and remove tempfile
$writing = copy($tempfile, $this->_OC_conf_file);
unlink($tempfile);
return($writing);
}
//////////////////////////////////////////////////////////////
/**
* OC_getConfFiles
* Creates an array of configuration files for the current
* configuration directory.
*
* author: Rob Birkinshaw [email protected]
* param: $inSection string, name of section to read
* return: array of configuration files or false
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_getConfFiles()
{
if (! $dir = @opendir($this->_OC_conf_dir)) return (false);
while (($file = readdir($dir)) !== false)
{
// ignore hidden files
if(!preg_match("/^\./", $file))
{
// ignore directories
if(! is_dir($this->_OC_conf_dir . '/' . $file))
{
// Match extensions
if(preg_match($this->_OC_conf_filter, $file))
{
$files[] = $file;
}
} // ignore dirs
} // ignore hidden
} //while read dir
closedir($dir);
if(is_array($files))
{
sort($files);
}
else
{
return(false);
}
return $files;
}
//////////////////////////////////////////////////////////////
/**
* OC_readConfValues
* Finds values (value is defined by _OC_value_exp) in the filearray.
*
* author: Rob Birkinshaw [email protected]
* param: $theItems array, list of item values
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_readConfValues($theItems)
{
$atemp = array();
foreach($theItems as $line)
{
// filer on "value" defined by _OC_value_exp
if(preg_match($this->_OC_value_exp, $line))
{
$atemp[] = preg_replace($this->_OC_value_exp,$this->_OC_value_exp_replace,$line);
}
}
if (count($atemp) == 0)
{
return (false);
}
$this->_OC_the_values = array_unique($atemp);
sort($this->_OC_the_values,SORT_STRING);
reset($this->_OC_the_values);
return(true);
}
//////////////////////////////////////////////////////////////
/**
* OC_checkAccess
* Reads access file $_OC_access_file and checks if user exists
*
* author: Rob Birkinshaw [email protected]
* param: $theItems array, list of item values
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_checkAccess($user)
{
$accessFile = array();
if (! is_file($this->_OC_access_file))
{
echo 'Access file: ' .$this->_OC_access_file . 'not found!<br>';
return(false);
}
$file = fopen($this->_OC_access_file, "r");
while (!feof($file))
{
$accessFile[] = fgetc($file);
}
fclose($file);
foreach ($accessFile as $line)
{
{
return(true);
}
}
echo 'User: ' . $user . 'does not have access to this feature.<br>';
return(false);
}
//////////////////////////////////////////////////////////////
/**
* OC_checkValidUser
* Checks is user has a valid session
*
* author: Rob Birkinshaw [email protected]
* param:
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_checkValidUser()
{
if (!isset($_SESSION['valid_user']))
{
die( 'You are not logged in.<br>');
}
return(true);
}
//////////////////////////////////////////////////////////////
/**
* OC_checkIfAdministrator
* Not used yet
*
* author: Rob Birkinshaw [email protected]
* param:
* return: boolean
* date: 2003-07-03
*
*/
//////////////////////////////////////////////////////////////
function OC_checkIfAdministrator()
{
if ( $_SESSION['is_admin'] )
{
return(true);
}
else
{
return(false);
}
}
}
?>