/
general.php
542 lines (475 loc) · 14.8 KB
/
general.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/*$slash = explode('/', getenv('SCRIPT_NAME'));
if (isset($slash[1])) {
//$app = getenv('DOCUMENT_ROOT') . '/' . $slash[1] . '/';
$app = getenv('DOCUMENT_ROOT') . substr(getenv('SCRIPT_NAME'), 0, (strrpos(getenv('SCRIPT_NAME'), DS) +1));
} else { // this happens when called by make
$app = '';
}*/
require_once(__ROOT__ . 'php'.DS.'inc'.DS.'database.php');
require_once(__ROOT__ . 'local_config'.DS.'config.php');
require_once(__ROOT__ . 'php'.DS.'inc'.DS.'caching.inc.php');
$language = ( (isset($_SESSION['userdata']['language']) and $_SESSION['userdata']['language'] != '') ? $_SESSION['userdata']['language'] : configuration_vars::get_instance()->default_language );
require_once(__ROOT__ . 'local_config'.DS.'lang'.DS. $language . '.php');
require_once(__ROOT__ . 'FirePHPCore/lib/FirePHPCore/FirePHP.class.php');
ob_start(); // Starts FirePHP output buffering
$firephp = FirePHP::getInstance(true);
/**
*
* Returns the user_id of the logged user; wraps a check around this, in order to make sure
* the value is set.
*/
function get_session_user_id(){
if (isset($_SESSION['userdata']['user_id']) && $_SESSION['userdata']['user_id'] > 0 ){
return $_SESSION['userdata']['user_id'];
} else {
throw new Exception("$_Session data user_id is not set!! ");
}
}
/**
*
* returns the uf of the logged user.
*/
function get_session_uf_id(){
if (isset($_SESSION['userdata']['uf_id']) && $_SESSION['userdata']['uf_id'] > 0 ){
return $_SESSION['userdata']['uf_id'];
} else {
throw new Exception("$_Session data uf_id is not set!! ");
}
}
/**
*
* Returns the member_id of the logged user.
*/
function get_session_member_id(){
if (isset($_SESSION['userdata']['member_id']) && $_SESSION['userdata']['member_id'] > 0 ){
return $_SESSION['userdata']['member_id'];
} else {
throw new Exception("$_Session data member_id is not set!! ");
}
}
/**
*
* returns the language for the logged user
*/
function get_session_language(){
if (isset($_SESSION['userdata']['language']) and $_SESSION['userdata']['language'] != '') {
return $_SESSION['userdata']['language'];
} else {
return configuration_vars::get_instance()->default_language;
}
}
/**
*
* returns the theme for the logged user
*/
function get_session_theme(){
if (isset($_SESSION['userdata']['theme']) and $_SESSION['userdata']['theme'] != '') {
return $_SESSION['userdata']['theme'];
} else {
return configuration_vars::get_instance()->default_theme;
}
}
/**
*
* retrieves active role of the logged user
*/
function get_current_role()
{
if (isset($_SESSION['userdata']['current_role']) and $_SESSION['userdata']['current_role'] != '') {
return $_SESSION['userdata']['current_role'];
} else {
return false;
}
}
/**
*
* Provides some basic logic to retrieve values from URL parameters.
* @param str $param_name the name of the parameter passed along
* @param $default a default value. if the parameter is not set, the default value will be used
* @param str $transform basic string transforms applied to the value of the parameter
* @throws Exception
*/
function get_param($param_name, $default=null, $transform = ''){
$value;
global $firephp;
$firephp->log($param_name, 'parameter');
$firephp->log($default, 'default');
if (isset($_REQUEST[$param_name])) {
$value = $_REQUEST[$param_name];
if (($value == '' || $value == 'undefined') && isset($default)){
$value = $default;
} else if (($value == '' || $value == 'undefined') && !isset($default)) {
throw new Exception("get_param: Parameter: {$param_name} has no value and no default value");
}
} else if (isset($default) and $default !== null){
$value= $default;
} else {
throw new Exception("get_param: Missing or wrong parameter name: {$param_name} in URL");
}
//utility hack to retrieve uf_id or user_id from session. e.g. &uf_id=-1
if ($param_name == "uf_id" && $value==-1){
$value = get_session_uf_id();
} else if ($param_name == "user_id" && $value==-1){
$value = get_session_user_id();
} else if ($param_name == "member_id" && $value==-1){
$value = get_session_member_id();
}
switch ($transform){
case 'lowercase':
$value = strtolower($value);
break;
case '':
$value = $value;
break;
case 'crypt':
$value = crypt($value, "ax");
break;
default:
throw new Exception("get_param: transform '{$transform}' on URL parameter not supported. ");
break;
}
return $value;
}
/**
* Execute a stored query
* @param array $args the arguments to be passed to the stored query; possibly empty
* @return the result set
*/
function do_stored_query()
{
$args = func_get_args();
if (is_array($args[0])) {
$args = $args[0];
}
for ($i=1; $i<count($args); ++$i) {
if (is_array($args[$i])) {
$args[$i] = $args[$i][0];
}
}
$sql_func = array_shift($args);
purge_cache($sql_func);
$strSQL = 'CALL ' . $sql_func . '(';
foreach ($args as $arg) {
if (strpos($arg, "'") !== false) {
if (strpos($arg, '"') !== false)
throw new DataException('Cannot use both symbols \' and " in text');
$strSQL .= '"' . $arg . '",';
} else
$strSQL .= "'" . $arg . "',";
}
if (count($args))
$strSQL = rtrim($strSQL, ',');
$strSQL .= ')';
return DBWrap::get_instance()->do_stored_query($strSQL);
}
class output_formatter {
public function rowset_to_jqgrid_XML($rs, $total_entries=0, $page=0, $limit=0, $total_pages=0, $cache_name='')
{
$strXML = '';
if ($rs) {
$strXML .= '<rowset>';
if ($page)
$strXML .= '<page>' . $page . '</page>';
if ($total_pages)
$strXML .= '<total>' . $total_pages . '</total>';
$strXML .= '<records>' . $total_entries . '</records>';
$strXML .= "<rows>";
while ($row = $rs->fetch_assoc())
$strXML .= $this->row_to_XML($row, $cache_name);
$rs->free();
$strXML .= "</rows>";
$strXML .= "</rowset>";
}
return $strXML;
}
public function row_to_XML($row, $cache_name='')
{
// global $firephp;
global $Text;
$strXML = '<row id="' . $row['id'] . '">';
$rowXML = '';
foreach ($row as $field => $value) {
if (isset($Text[$value]))
$value = $Text[$value];
$rowXML
.= '<' . $field . ' f="' . $field
. '"><![CDATA[' . clean_zeros($value) . "]]></$field>";
}
// $firephp->log($rowXML, 'row_xml'));
$strXML .= $rowXML . '</row>';
return $strXML;
}
}
function stored_query_XML() //$queryname, $group_tag, $row_tag, $param)
{
$params = func_get_args();
$strSQL = array_shift($params);
$group_tag = array_shift($params);
$row_tag = array_shift($params);
array_unshift($params, $strSQL);
$cache = new QueryCache($params);
if ($cache->exists())
return $cache->read();
$strXML = "<$group_tag>";
// $rs = ((count($params)>0) ?
// do_stored_query($strSQL, $params)
// : do_stored_query($strSQL));
$rs = do_stored_query($params);
global $Text;
while ($row = $rs->fetch_array()) {
$value = ( ($row_tag == 'description' and isset($Text[$row[1]])) ?
$Text[$row[1]] : $row[1] );
$strXML
.= '<row><id f="id">' . $row[0]
. '</id><' . $row_tag
. ' f="' . $row_tag
. '"><![CDATA[' . clean_zeros($value) . ']]></' . $row_tag
. '></row>';
}
$strXML .= "</$group_tag>";
$cache->write($strXML);
return $strXML;
}
// make variable argument list
function stored_query_XML_fields()
{
$cache = new QueryCache(func_get_args());
if ($cache->exists())
return $cache->read();
$strXML = '<rowset>';
global $Text;
$rs = do_stored_query(func_get_args());
while ($row = $rs->fetch_assoc()) {
$strXML .= '<row';
if (isset($row['id']))
$strXML .= ' id ="' . $row['id'] . '"';
$strXML .= '>';
foreach ($row as $field => $value) {
if ($field == 'description' and isset($Text[$value]))
$value = $Text[$value];
$strXML
.= '<' . $field . ' f="' . $field
. '"><![CDATA[' . clean_zeros($value) . "]]></$field>";
}
$strXML .= '</row>';
}
$strXML .= '</rowset>';
$cache->write($strXML);
return $strXML;
}
function query_XML() //$strSQL, $group_tag, $row_tag, $param1=0, $param2=0)
{
$params = func_get_args();
$strSQL = array_shift($params);
$group_tag = array_shift($params);
$row_tag = array_shift($params);
array_unshift($params, $strSQL);
$cache = new QueryCache($params);
if ($cache->exists())
return $cache->read();
$strXML = "<$group_tag>";
$rs = DBWrap::get_instance()->Execute($params);
while ($row = $rs->fetch_array()) {
$value = ( ($row_tag == 'description' and isset($Text[$row[1]])) ?
$Text[$row[1]] : $row[1] );
$strXML
.= '<row><id f="id">' . $row[0]
. '</id><' . $row_tag
. ' f="' . $row_tag
. '"><![CDATA[' . $value . ']]></' . $row_tag
. '></row>';
}
$strXML .= "</$group_tag>";
$cache->write($strXML);
return $strXML;
}
function query_XML_compact()
{
$params = func_get_args();
$strSQL = array_shift($params);
$group_tag = array_shift($params);
$row_tag = array_shift($params);
array_unshift($params, $strSQL);
$cache = new QueryCache($params);
if ($cache->exists())
return $cache->read();
$strXML = "<$group_tag>";
$rs = DBWrap::get_instance()->Execute($params);
while ($row = $rs->fetch_array()) {
$strXML
.= '<' . $row_tag
. ' f="' . $row_tag
. '"><![CDATA[' . $row[0] . ']]></' . $row_tag
. '>';
}
$strXML .= "</$group_tag>";
$cache->write($strXML);
return $strXML;
}
function query_XML_noparam($queryname)
{
$cache = new QueryCache($queryname);
if ($cache->exists())
return $cache->read();
$strXML = "<$queryname>";
$rs = do_stored_query($queryname);
while ($row = $rs->fetch_assoc()) {
$strXML .= "<row>";
foreach ($row as $field => $value) {
if ($field == 'description' and isset($Text[$value]))
$value = $Text[$value];
$strXML .= "<{$field}>{$value}</{$field}>";
}
$strXML .= "</row>";
}
$strXML .= "</$queryname>";
$cache->write($strXML);
return $strXML;
}
function printXML($str){
$newstr = '<?xml version="1.0" encoding="utf-8"?>';
$newstr .= $str;
header('Content-Type: text/xml');
header('Last-Modified: '.date(DATE_RFC822));
header('Pragma: no-cache');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '. date(DATE_RFC822, time() - 3600));
header('Content-Length: ' . strlen($newstr));
echo $newstr;
}
function HTMLwrite($strHTML, $filename)
{
if(is_writeable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $strHTML) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
function get_config_menu($user_role)
{
$XML = "<navigation>\n";
$mconf = configuration_vars::get_instance()->menu_config;
if (!isset($mconf[$user_role])) {
throw new Exception("Role '" . $user_role . "' not defined in local_config/config.php");
}
foreach ($mconf[$user_role] as $navItem => $status) {
$XML .= '<' . $navItem . '>' . $status . '</' . $navItem . ">\n";
}
return $XML . '</navigation>';
}
function get_field_options_live($table, $field1, $field2)
{
global $Text;
$strXML = '<select>';
$rs = DBWrap::get_instance()->Execute('select :1, :2 from :3', $field1, $field2, $table);
if ($table == 'aixada_uf') {
$strXML .= "<option value=''></option>";
}
while ($row = $rs->fetch_array()) {
$ot = (isset($Text[$row[1]]) ? $Text[$row[1]] : $row[1]);
if ($table == 'aixada_uf')
$ot = //$Text['uf_short'] . ' ' .
$row[0] . ' ' . $ot;
$strXML .= "<option value='{$row[0]}'>{$ot}</option>";
}
return $strXML . '</select>';
}
function get_existing_themes_XML()
{
$exclude_list = array(".", "..", "example.txt");
$folders = array_diff( scandir(__ROOT__ . 'css/ui-themes'), $exclude_list);
$XML = '<themes>';
foreach ($folders as $theme) {
$XML .= "<theme><name>{$theme}</name></theme>";
}
return $XML . '</themes>';
}
function existing_languages()
{
// We require that a line of the form
// $Text['es_es'] = 'Español'
// exists in each language file
$languages = array();
foreach (glob(__ROOT__ . "local_config/lang/*.php") as $lang_file) {
$a = strpos($lang_file, 'lang/');
$lang = substr($lang_file, $a+5, strpos($lang_file, '.')-$a-5);
$handle = @fopen($lang_file, "r");
$line = fgets($handle);
while (strpos($line, "Text['{$lang}']") === false and !feof($handle)) {
$line = fgets($handle);
}
if (feof($handle))
$lang_desc = '';
else {
$tmp = trim(substr($line, strpos($line, '=')));
$lang_desc = trim($tmp, " =;'\"");
}
$languages[$lang] = $lang_desc;
}
return $languages;
}
function existing_languages_XML()
{
// We require that a line of the form
// $Text['es_es'] = 'Español'
// exists in each language file
$XML = '<languages>';
foreach (existing_languages() as $lang => $lang_desc) {
$XML .= "<language><id>{$lang}</id><description>{$lang_desc} ({$lang})</description></language>";
}
return $XML . '</languages>';
}
function get_roles()
{
$XML = '<roles>';
foreach (array_keys(configuration_vars::get_instance()->forbidden_pages) as $role) {
$XML .= "<role><description>{$role}</description></role>";
}
return $XML . '</roles>';
}
function get_commissions()
{
// global $firephp;
$XML = '<rows>';
foreach (array_keys(configuration_vars::get_instance()->forbidden_pages) as $role) {
if (!in_array($role, array('Consumer', 'Checkout', 'Producer'))) {
// $firephp->log($role);
$XML .= "<row><description>{$role}</description></row>";
}
}
return $XML . '</rows>';
}
function purge_cache($sql_func)
{
$cv = configuration_vars::get_instance();
// global $firephp;
// $firephp->log($cv);
// $firephp->log($cv->tables_modified_by);
// exit();
/*
if (array_key_exists($sql_func, $cv->tables_modified_by)) {
$cache_dir = $cv->cache_dir;
foreach($cv->tables_modified_by[$sql_func] as $table) {
foreach(glob($cache_dir . $table . '*') as $filename) {
unlink($filename);
}
}
}
*/
}
function clean_zeros($value)
{
return ((strpos($value, '.') !== false) ?
rtrim(rtrim($value, '0'), '.')
: $value);
}
?>