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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
*
* 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;
/**
* To calculate the page generation time
* Integer
*/
private $start_page;
/**
* Loads in the start_page variable the start time of the page loading
*
*/
public function start_page_stat()
{
$this->start_page = microtime(true);
}
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);
}
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
/**
* 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;
}
}
/**
* Ends the page loading time and displays it
*
*/
public function show_page_stat()
{
$end_page = microtime(true);
$page_total = round($end_page - $this->start_page,3);
if($page_total > 1)
{
$page_seconds = _SECONDS;
}
else
{
$page_seconds = _SECOND;
}
echo _PAGE_GENERATED_IN." <b>".$page_total."</b> ".$page_seconds;
}
/**
* Configures the actual position of the visitor with all query strings to go to the right page after the logging action
*
* @param $index string "index.php?" by default
*/
public function configPosition($index ="index.php?")
{
$querystring = $_SERVER['QUERY_STRING'];
$tab_query = explode("&",$querystring);
$querystring = "";
for($i=0;$i<count($tab_query);$i++)
{
if(substr($tab_query[$i],0,3) <> "css" && substr($tab_query[$i],0,3) <> "CSS")
{
$querystring .= $tab_query[$i]."&";
}
}
$querystring = substr($querystring,0,strlen($querystring)-1);
$_SESSION['position'] = $index.$querystring;
}
/**
* 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))
{
$_SESSION['error'] .= $msg." ".$var . ' ';
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
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))
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
{
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]$/";
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
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
}
}
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
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* 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 '';
}
/**
* Writes an error in pre formating format with header and footer
*
* @param $title string Error title
* @param $message string Error message
* @param $type string If 'title' then displays the title otherwise do not displays it (empty by default)
* @param $img_src string Source of the image to show (empty by default)
*/
public function echo_error($title,$message, $type = '', $img_src = '')
{
if ($type == 'title' || $type <> '')
{
if($img_src <> '')
{
echo '<h1><img src="'.$img_src.'" alt="" />'.$title.'</h1>';
}
else
{
echo "<h1>".$title."</h1>";
}
echo '<div id="inner_content">';
} ?>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
if ($type <> '')
{
echo '</div>';
}
}
/**
* Extracts the user informations from database and puts the result in an array
*
* @param $id integer User identifier
*/
public function infouser($id)
{
require_once "modules" . DIRECTORY_SEPARATOR . "visa" . DIRECTORY_SEPARATOR. "class" . DIRECTORY_SEPARATOR. "class_user_signatures.php";
$us = new UserSignatures();

Florian Azizian
committed
$stmt = $conn->query("SELECT * FROM ".$_SESSION['tablename']['users']." WHERE user_id = ?", array($id));
{
return array("UserId" => "",

Florian Azizian
committed
"department" => "",
"thumbprint" => "",
}
else
{

Florian Azizian
committed
$query = "SELECT path_template FROM docservers WHERE docserver_id = 'TEMPLATES'";

Florian Azizian
committed
$stmt2 = $conn->query($query);
$resDs = $stmt2->fetchObject();
$pathToDs = $resDs->path_template;
$tab_sign = $us->getForUser($line->user_id);
$_SESSION['user']['pathToSignature'] = array();
foreach ($tab_sign as $sign) {
$path = $pathToDs . str_replace(
$sign['signature_path']
)
. $sign['signature_file_name'];
array_push($_SESSION['user']['pathToSignature'], $path);

Florian Azizian
committed
}
return array("UserId" => $line->user_id,
"FirstName" => $line->firstname,
"LastName" => $line->lastname,
"Initials" => $line->initials,

Florian Azizian
committed
"department" => $line->department,
"thumbprint" => $line->thumbprint,
"pathToSignature" => $_SESSION['user']['pathToSignature']
}
}
/**
* 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);
} else if(($databasetype == "MYSQL") && !get_magic_quotes_runtime()) {
$string = addslashes($string);
} else if(($databasetype == "POSTGRESQL") && !get_magic_quotes_runtime()) {

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
* @return Formated string
*/
public static function show_string($string, $replace_CR = false, $chars_to_escape = array(), $databasetype = '', $escape_quote = true)
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
{
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);
}
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
$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;
}
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
/**
* 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);
$last = strtolower($val{strlen($val)-1});
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
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
*/
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;
}
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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) {