Newer
Older
<?php
/*
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
*
* This file is part of Maarch Framework.
*
* Maarch Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maarch Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @brief Contains all the various functions of this application.
*
* @file
* @author Claire Figueras <dev@maarch.org>
* @date $date$
* @version $Revision$
* @ingroup core
*/
/**
* @brief Contains all the various functions of this application.
*
* <ul>
* <li>The toolkit of the Maarch framework</li>
* <li>Management of variables format</li>
* <li>Management of date format</li>
* </ul>
* @ingroup core
*/
class functions
{
/**
*
* @deprecated
*/
private $f_page;
public function normalize ($string)
{
$a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ'
. 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
$b = 'aaaaaaaceeeeiiiidnoooooouuuuy'
. 'bsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
$string = utf8_decode($string);
$string = strtr($string, utf8_decode($a), $b);
$string = strtolower($string);
return utf8_encode($string);
}
/**
* Cuts a string at the maximum number of char to displayed
*
* @param $string string String value
* @param $max integer Maximum character number
*/
public function cut_string($string, $max)
{
if (strlen($string) >= $max)
{
$string = substr($string, 0, $max);
$espace = strrpos($string, " ");
$string = substr($string, 0, $espace)."...";
return $string;
}
else
{
return $string;
}
}
/**
* Adds en error to the errors log
*
* @param $msg string Message to add
* @param $var string Language dependant message
*/
{
$msg = trim($msg);
if(!empty($msg))
{
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
if(strlen(str_replace(array("<br />","<br />"),"",$_SESSION['error'])) < 6)
{
$_SESSION['error'] = "";
}
}
}
/**
* Cleans a variable with multiple possibility
*
* @param $what string Variable to clean
* @param $mask string Mask, "no" by default
* @param $msg_error string Error message, empty by default
* @param $empty string "yes" by default
* @param $min_limit integer Empty by default
* @param $max_limit integer Empty by default
* @return string Cleaned variable or empty string
*/
public function wash($what, $mask = "no", $msg_error = "", $empty = "yes", $min_limit = "", $max_limit = "", $custom_pattern = '', $custom_error_msg = '')
{
//$w_var = addslashes(trim(strip_tags($what)));
$w_var = trim(strip_tags($what));
$test_empty = "ok";
if($empty == "yes")
{
// We use strlen instead of the php's empty function because for a var containing 0 return by a form (in string format)
// the empty function return that the var is empty but it contains à 0
if(strlen($w_var) == 0)
{
$test_empty = "no";
}
else
{
$test_empty = "ok";
}
}
if($test_empty == "no")
{
$this->add_error($msg_error, _IS_EMPTY);
return "";
}
else
{
if($msg_error <> '')
{
if($min_limit <> "")
{
if(strlen($w_var) < $min_limit)
{
if($min_limit > 1)
{
$this->add_error($msg_error, _MUST_MAKE_AT_LEAST." ".$min_limit." "._CHARACTERS);
}
else
{
$this->add_error($msg_error, _MUST_MAKE_AT_LEAST." ".$min_limit." "._CHARACTERS);
}
return "";
}
}
}
if($max_limit <> "")
{
if(strlen($w_var) > $max_limit)
{
if($min_limit > 1)
{
$this->add_error($msg_error, MUST_BE_LESS_THAN." ".$max_limit." "._CHARACTERS);
}
else
{
$this->add_error($msg_error, MUST_BE_LESS_THAN." ".$max_limit." "._CHARACTERS);
}
return "";
}
}
switch ($mask)
{
case "no":
return $w_var;
case "num":
if (preg_match("/^[0-9]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT." :<br/>"._WAITING_INTEGER);
return "";
}
case "float":
if (preg_match("/^[0-9.,]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT." "._WAITING_FLOAT);
return "";
}
case "letter":
if (preg_match("/^[a-zA-Z]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
$this->add_error(_ONLY_ALPHABETIC, '');
return "";
}
case "alphanum":
if (preg_match("/^[a-zA-Z0-9]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error,_WRONG_FORMAT);
$this->add_error(_ONLY_ALPHANUM, '');
return "";
}

Florian Azizian
committed
case "alphanumunderscore":
if (preg_match("/^[a-zA-Z0-9_]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error,_WRONG_FORMAT);
return "";
}
case "nick":
if (preg_match("/^[_a-zA-Z0-9.-]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error,_WRONG_FORMAT);
return "";
}
case "mail":
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,10}$/",$w_var))
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
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
return "";
}
case "url":
if (preg_match("/^[www.]+[_a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
return "";
}
case "file":
if (preg_match("/^[_a-zA-Z0-9.-? é&\/]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
return "";
}
case "name":
if (preg_match("/^[_a-zA-Z0-9.-? \'\/&éea]+$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
return "";
}
if (preg_match("/^[\+0-9\(\)\s\.]*$/",$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT);
return "";
}
case "date":
$date_pattern = "/^[0-3][0-9]-[0-1][0-9]-[1-2][0-9][0-9][0-9]$/";
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
if(preg_match($date_pattern,$w_var))
{
return $w_var;
}
else
{
$this->add_error($msg_error, _WRONG_FORMAT." "._WAITING_DATE);
return "";
}
case "custom":
if(preg_match($custom_pattern,$w_var) == 0)
{
$this->add_error($msg_error, $custom_error_msg.' '.$custom_pattern.' '.$w_var);
return "";
}
else
{
return $w_var;
}
}
}
}
/**
* Returns a variable with personnal formating. It allows you to add formating action when you displays the variable the var
*
* @param $what string Variable to format
* @return string Formated variable
*/
public static function show_str($what)
{
return stripslashes($what);
}
/**
* Manages the location bar in session (4 levels max), then calls the where_am_i() function.
*
* @param $path string Url (empty by default)
* @param $label string Label to show in the location bar (empty by default)
* @param $id_pagestring Page identifier (empty by default)
* @param $init bool If true reinits the location bar (true by default)
* @param $level string Level in the location bar (empty by default)
*/
public function manage_location_bar($path = '', $label = '', $id_page = '', $init = true, $level = '')
{
//INIT LOCATION BAR
if (empty($_SESSION['location_bar_label'])) {
$_SESSION['location_bar_label'][0] = _WELCOME_TITLE;
$_SESSION['location_bar_path'][0] = 'index.php?reinit=true';
} if (!empty($level)) {
//IF USER CLICKED ON LOCATION BAR
$arrLocationLabel = [];
$arrLocationPath = [];
foreach($_SESSION['location_bar_label'] as $key => $value) {
$arrLocationLabel[] = $_SESSION['location_bar_label'][$key];
$arrLocationPath[] = $_SESSION['location_bar_path'][$key];
if($key == $level) {
break;
}
$_SESSION['location_bar_label'] = $arrLocationLabel;
$_SESSION['location_bar_path'] = $arrLocationPath;
} else if (count($_SESSION['location_bar_label'])==4 && $_SESSION['location_bar_label'][count($_SESSION['location_bar_label'])-1] != $label) {
//ERASE BEGIN OF LOCATION BAR IF TOO MUCH ITEMS
array_shift($_SESSION['location_bar_label']);
array_shift($_SESSION['location_bar_path']);
$_SESSION['location_bar_label'][0] = _WELCOME_TITLE;
$_SESSION['location_bar_path'][0] = 'index.php?reinit=true';
//ADD NEW LOCATION
if ($_SESSION['location_bar_label'][count($_SESSION['location_bar_label'])-1] != $label) {
$_SESSION['location_bar_label'][] = $label;
$_SESSION['location_bar_path'][] = $path;
//WRITE LOCATION BAR
foreach($_SESSION['location_bar_label'] as $key => $value) {
?><script type="text/javascript">
writeLocationBar('<?php echo $_SESSION['location_bar_path'][$key]; ?>','<?php echo $value; ?>','<?php echo $key; ?>');
</script><?php
}
}
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
/**
* For debug, displays an array in a more readable way
*
* @param $arr array Array to display
*/
public function show_array($arr)
{
echo "<table width=\"550\"><tr><td align=\"left\">";
echo "<pre>";
print_r($arr);
echo "</pre>";
echo "</td></tr></table>";
}
/**
* Formats a datetime to a dd/mm/yyyy format (date)
*
* @param $date datetime The date to format
* @return datetime The formated date
*/
public function format_date($date)
{
$last_date = '';
if($date <> "")
{
if(strpos($date," "))
{
$date_ex = explode(" ",$date);
$the_date = explode("-",$date_ex[0]);
$last_date = $the_date[2]."-".$the_date[1]."-".$the_date[0];
}
else
{
$the_date = explode("-",$date);
$last_date = $the_date[2]."-".$the_date[1]."-".$the_date[0];
}
}
return $last_date;
}
/**
* Formats a datetime to a dd/mm/yyyy hh:ii:ss format (timestamp)
*
* @param $date datetime The date to format
* @return datetime The formatted date
*/
public function dateformat($realDate, $sep='/')
{
if ($realDate <> '') {
if (preg_match('/ /', $realDate)) {
$hasTime = true;
$tmpArr = explode(" ", $realDate);
$date = $tmpArr[0];
$time = $tmpArr[1];
if (preg_match('/\./', $time)) { // POSTGRES date
$tmp = explode('.', $time);
$time = $tmp[0];
} else if (preg_match('/,/', $time)) { // ORACLE date
$tmp = explode(',', $time);
$time = $tmp[0];
}
} else {
$hasTime = false;
$date = $realDate;
}
if (preg_match('/-/', $date)) {
$dateArr = explode("-", $date);
} else if (preg_match('@\/@', $date)) {
$dateArr = explode("/", $date);
}
if (! $hasTime || substr($tmpArr[1], 0, 2) == "00") {
return $dateArr[2] . $sep . $dateArr[1] . $sep . $dateArr[0];
} else {
return $dateArr[2] . $sep . $dateArr[1] . $sep . $dateArr[0]
. " " . $time;
}
}
return '';
}
/**
* Returns a formated date for SQL queries
*
* @param $date date Date to format
* @param $insert bool If true format the date to insert in the database (true by default)
* @return Formated date or empty string if any error
*/
public static function format_date_db($date, $insert=true, $databasetype= '', $withTimeZone=false)
{
if (isset($_SESSION['config']['databasetype'])
&& ! empty($_SESSION['config']['databasetype'])) {
$databasetype = $_SESSION['config']['databasetype'];
}
if ($date <> "" ) {
$var = explode('-', $date) ;
if (preg_match('/\s/', $var[2])) {
$tmp = explode(' ', $var[2]);
$var[2] = $tmp[0];
$var[3] = substr($tmp[1],0,8);
if (preg_match('/^[0-3][0-9]$/', $var[0])) {
$day = $var[0];
$month = $var[1];
$year = $var[2];
$hours = $var[3];
} else {
$year = $var[0];
$month = $var[1];
$day = substr($var[2], 0, 2);
$hours = $var[3];
}
if ($year <= "1900") {
return '';
} else {
if ($databasetype == "SQLSERVER") {
return $day . "-" . $month . "-" . $year . " " . $hours;
}else{
return $day . "-" . $month . "-" . $year;
}
} else if ($databasetype == "POSTGRESQL") {
if ($_SESSION['config']['lang'] == "fr") {
return $day . "-" . $month . "-" . $year . " " . $hours;
}else{
return $day . "-" . $month . "-" . $year;
}
} else {
return $year . "-" . $month . "-" . $day . " " . $hours;
}else{
return $year . "-" . $month . "-" . $day;
}
}
} else if ($databasetype == "ORACLE") {
return $day . "-" . $month . "-" . $year;
} else if ($databasetype == "MYSQL" && $insert) {
return $year . "-" . $month . "-" . $day;
} else if ($databasetype == "MYSQL" && !$insert) {
return $day . "-" . $month . "-" . $year;
}
}
} else {
return '';
}
}
/**
* Protects string to insert in the database
*
* @param $string string String to format
* @return Formated date
*/

Florian Azizian
committed
public function protect_string_db($string, $databasetype = '', $full='yes')
if (isset($_SESSION['config']['databasetype']) && !empty($_SESSION['config']['databasetype']))
{
$databasetype = $_SESSION['config']['databasetype'];
}
if ($databasetype == "SQLSERVER")
{
$string = str_replace("'", "''", $string);
$string = str_replace("\\", "", $string);
} else if($databasetype == "ORACLE") {
$string = str_replace("'", "''", $string);
$string = str_replace("\\", "", $string);
$string = addslashes($string);

Florian Azizian
committed
$string = str_replace("'", "'", $string);
$string = pg_escape_string($string);

Florian Azizian
committed
if ($full == 'yes') {
$string=str_replace(';', ' ', $string);
$string=str_replace('--', '-', $string);
}
return $string;
}
/**
* Returns a string without the escaping characters
*
* @param $string string String to format
public static function show_string($string, $replace_CR = false, $chars_to_escape = array(), $databasetype = '', $escape_quote = true)
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
{
if(isset($string) && !empty($string) && is_string($string))
{
if(isset($_SESSION['config']['databasetype']) && !empty($_SESSION['config']['databasetype']))
{
$databasetype = $_SESSION['config']['databasetype'];
}
if($databasetype == "SQLSERVER")
{
$string = str_replace("''", "'", $string);
$string = str_replace("\\", "", $string);
}
else if($databasetype == "MYSQL" || $databasetype == "POSTGRESQL" && (ini_get('magic_quotes_gpc') <> true || phpversion() >= 6))
{
$string = stripslashes($string);
$string = str_replace("\\'", "'", $string);
$string = str_replace('\\"', '"', $string);
}
else if($databasetype == "ORACLE")
{
$string = str_replace("''", "'", $string);
$string = str_replace("\\", "", $string);
}
if($replace_CR)
{
$to_del = array("\t", "\n", "�A;", "�D;", "\r");
$string = str_replace($to_del, ' ', $string);
}
if (!empty($chars_to_escape) && is_array($chars_to_escape)) {
for($i=0;$i<count($chars_to_escape);$i++)
{
$string = str_replace($chars_to_escape[$i], '\\'.$chars_to_escape, $string);
}
if ($escape_quote) {
$string = str_replace('"', "'", $string);
}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
$string = trim($string);
}
return $string;
}
/**
* Cleans html string, replacing entities by utf-8 code
*
* @param $var string String to clean
* @return Cleaned string
*/
public function wash_html($var, $mode="UNICODE")
{
if($mode == "UNICODE")
{
$var = str_replace("<br/>","\\n",$var);
$var = str_replace("<br />","\\n",$var);
$var = str_replace("<br/>","\\n",$var);
$var = str_replace(" "," ",$var);
$var = str_replace("é", "\u00e9",$var);
$var = str_replace("è","\u00e8",$var);
$var = str_replace("ê","\00ea",$var);
$var = str_replace("à","\u00e0",$var);
$var = str_replace("â","\u00e2",$var);
$var = str_replace("î","\u00ee",$var);
$var = str_replace("ô","\u00f4",$var);
$var = str_replace("û","\u00fb",$var);
$var = str_replace("´","\u0027",$var);
$var = str_replace("°","\u00b0",$var);
$var = str_replace("’", "\u2019",$var);
}
else if($mode == 'NO_ACCENT')
{
$var = str_replace("<br/>","\\n",$var);
$var = str_replace("<br />","\\n",$var);
$var = str_replace("<br/>","\\n",$var);
$var = str_replace(" "," ",$var);
$var = str_replace("é", "e",$var);
$var = str_replace("è","e",$var);
$var = str_replace("ê","e",$var);
$var = str_replace("à","a",$var);
$var = str_replace("â","a",$var);
$var = str_replace("î","i",$var);
$var = str_replace("ô","o",$var);
$var = str_replace("û","u",$var);
$var = str_replace("´","",$var);
$var = str_replace("°","o",$var);
$var = str_replace("’", "'",$var);
// AT LAST
$var = str_replace("&", " et ",$var);
}
else
{
$var = str_replace("<br/>","\\n",$var);
$var = str_replace("<br />","\\n",$var);
$var = str_replace("<br/>","\\n",$var);
$var = str_replace(" "," ",$var);
$var = str_replace("é", "é",$var);
$var = str_replace("è","è",$var);
$var = str_replace("ê","ê",$var);
$var = str_replace("à","à",$var);
$var = str_replace("â","â",$var);
$var = str_replace("î","î",$var);
$var = str_replace("ô","ô",$var);
$var = str_replace("û","û",$var);
$var = str_replace("´","",$var);
$var = str_replace("°","°",$var);
$var = str_replace("’", "'",$var);
return $var;
}
/**
* Converts a value (from the php.ini) into bytes
*
* @param $val string Value to convert
* @return integer The converted value
*/
public function return_bytes($val)
{
$val = trim($val);
$strlen = strlen($val)-1;
$last = strtolower($val.$strlen);
switch($last) {
// 'G' modifier available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
/**
* Compares to date
*
* @param $date1 date First date
* @param $date2 date Second date
* @return date1 if the first date is the greater, date2 if the second date or "equal" otherwise
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
*/
public function compare_date($date1, $date2)
{
$date1 = strtotime($date1);
$date2 = strtotime($date2);
if($date1 > $date2)
{
$result = "date1";
}
elseif($date1 < $date2)
{
$result = "date2";
}
elseif($date1 = $date2)
{
$result = "equal";
}
return $result;
}
/**
* Compares to date and return dif between 2 dates
*
* @param $date1 date First date
* @param $date2 date Second date
* @return dif between 2 dates in days
*/
public function nbDaysBetween2Dates($date1, $date2)
{
$date1 = strtotime($date1);
$date2 = strtotime($date2);
if($date2 > $date1)
{
$result = round((($date2 - $date1) / (3600)) / 24, 0);
}
elseif($date2 < $date1)
{
$result = round((($date1 - $date2) / (3600)) / 24, 0);
}
else
{
$result = 0;
}
return $result;
}
/**
* Checks if a directory is empty
*
* @param $dir string The directory to check
* @return bool True if empty, False otherwise
*/
function isDirEmpty($dir)
{
$dir = opendir($dir);
$isEmpty = true;
while(($entry = readdir($dir)) !== false)
{
if($entry !== '.' && $entry !== '..' && $entry !== '.svn')
{
$isEmpty = false;
break;
}
}
closedir($dir);
return $isEmpty;
}
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/**
* Convert an object to an array
* @param $object object to convert
*/
public function object2array($object)
{
$return = NULL;
if(is_array($object))
{
foreach($object as $key => $value)
{
$return[$key] = $this->object2array($value);
}
}
else
{
if(is_object($object))
{
$var = get_object_vars($object);
if($var)
{
foreach($var as $key => $value)
{
$return[$key] = ($key && !$value) ? NULL : $this->object2array($value);
}
}
else return $object;
}
else return $object;
}
return $return;
}
/**
* Function to encode an url in base64
*/
function base64UrlEncode($data) {
return strtr(base64_encode($data), '+/', '-_,');
}
/**
* Function to decode an url encoded in base64
*/
function base64UrlDecode($base64) {
return base64_decode(strtr($base64, '-_,', '+/'));
}
/**
* Encrypt a text
* @param $text string to encrypt
*/
public function encrypt($sensitiveData) {
$publicKeyPath = $this->getPublicKeyPath();
if(file_exists($publicKeyPath)) {
$pubKey = openssl_pkey_get_public('file://'.$publicKeyPath);
if(!$pubKey) {
return false;
} else {
$encryptedData = "";
openssl_public_encrypt($sensitiveData, $encryptedData, $pubKey);
//base 64 encode to use it in url
return $this->base64UrlEncode($encryptedData);
}
} else{
return false;
}
}
/**
* Decrypt a text
* @param $text string to decrypt
*/
public function decrypt($encryptedData) {
$privateKeyPath = $this->getPrivateKeyPath();
if(file_exists($privateKeyPath)) {
$passphrase = "";
$privateKey = openssl_pkey_get_private('file://'.$privateKeyPath, $passphrase);
if(!$privateKey) {
return false;
} else {
$decryptedData = "";
openssl_private_decrypt($this->base64UrlDecode($encryptedData), $decryptedData, $privateKey);
return $decryptedData;
}
} else {
return false;
}
}
/**
* return the path of the private key path
*/
public function getPrivateKeyPath() {
if(file_exists($_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml')) {
$path = $_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
} else {
$path = 'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
}
$xmlconfig = simplexml_load_file($path);
$CRYPT = $xmlconfig->CRYPT;
return (string) $CRYPT->pathtoprivatekey;
}
/**
* return the path of the public key path
*/
public function getPublicKeyPath() {
if(file_exists($_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml')) {
$path = $_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
} else {
$path = 'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
}
$xmlconfig = simplexml_load_file($path);
$CRYPT = $xmlconfig->CRYPT;
return $CRYPT->pathtopublickey;
}
public function isEncrypted()
{
if(file_exists($_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml')) {
$path = $_SESSION['config']['corepath'].'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
} else {
$path = 'apps'.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id'].DIRECTORY_SEPARATOR.'xml'.DIRECTORY_SEPARATOR.'config.xml';
}
$xmlconfig = simplexml_load_file($path);
$CRYPT = $xmlconfig->CRYPT;
return $CRYPT->encrypt;
}
/**
* Return the file's extention of a file
* @param $sFullPath string path of the file
*/
function extractFileExt($sFullPath)
{
$sName = $sFullPath;
if (strpos($sName, ".") == 0) {
$extractFileExt = "";
} else {
$extractFileExt = explode(".", $sName);
}
if ($extractFileExt <> '') {
return $extractFileExt[count($extractFileExt) - 1];
}
return '';
}
/**
* Browse each file and folder in the folder and return true if the folder is not empty
* @param $folder path string of the folder
*/
function isDirNotEmpty($folder)
{
$foundDoc = false;
$classScan = dir($folder);
while (($fileScan = $classScan->read()) != false) {
if($fileScan == '.' || $fileScan == '..' || $fileScan == '.svn') {
continue;
} else {
$foundDoc = true;break;
}
}
return $foundDoc;
}
/**
* xss mitigation functions
* Return protected chars
* @param $data to encode
* @param $encoding ut8 by default
*/
static function xssafe($data, $encoding='UTF-8')
{

Florian Azizian
committed
return htmlspecialchars($data, ENT_QUOTES | ENT_HTML401, $encoding);
}
}
/**
* xss mitigation functions
* Return protected chars
* @param $data to encode
*/
static function xecho($data)
{
echo functions::xssafe($data);
}
/*************************************************************************
* Returns an empty list for SELECT X WHERE Y IN (------)
*
* Return
* (string) Empty list
*
*************************************************************************/
public function empty_list()
{
switch($_SESSION['config']['databasetype']) {
case 'MYSQL' : return "''";
case 'POSTGRESQL' : return "''";
case 'SQLSERVER' : return "''''";