. */ /** * @brief Contains all the various functions of this application. * * @file * @author Claire Figueras * @date $date$ * @version $Revision$ * @ingroup core */ /** * @brief Contains all the various functions of this application. * * * @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); } /** * 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." ".$page_total." ".$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 "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 */ public function add_error($msg,$var) { $msg = trim($msg); if(!empty($msg)) { $_SESSION['error'] .= $msg." ".$var."
"; if(strlen(str_replace(array("
","
"),"",$_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." :
"._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 ""; } 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,4}$/",$w_var)) { 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 ""; } case "phone": 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]$/"; 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 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 = '') { //Fix un little php bug if(strpos($label,"’")!== false) { $label = str_replace("’" , "\'", $label); } $_SESSION['location_bar']['level1']['path'] = "index.php?reinit=true"; $_SESSION['location_bar']['level1']['label'] = $_SESSION['config']['applicationname']; $_SESSION['location_bar']['level1']['id'] = "welcome"; if(!empty($level)) { if($level == 1) { $_SESSION['location_bar']['level2']['path'] = ""; $_SESSION['location_bar']['level2']['label'] = ""; $_SESSION['location_bar']['level2']['id'] = "" ; $_SESSION['location_bar']['level3']['path'] = ""; $_SESSION['location_bar']['level3']['label'] = ""; $_SESSION['location_bar']['level3']['id'] = "" ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } elseif($level == 2) { $_SESSION['location_bar']['level3']['path'] = ""; $_SESSION['location_bar']['level3']['label'] = ""; $_SESSION['location_bar']['level3']['id'] = "" ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } elseif($level == 3) { $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } } else { if(isset($_SESSION['location_bar']['level1']['id']) && trim($id_page) == trim($_SESSION['location_bar']['level1']['id'])) { $_SESSION['location_bar']['level2']['path'] = ""; $_SESSION['location_bar']['level2']['label'] = ""; $_SESSION['location_bar']['level2']['id'] = "" ; $_SESSION['location_bar']['level3']['path'] = ""; $_SESSION['location_bar']['level3']['label'] = ""; $_SESSION['location_bar']['level3']['id'] = "" ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } elseif( isset($_SESSION['location_bar']['level2']['id']) && trim($id_page) == trim($_SESSION['location_bar']['level2']['id'])) { $_SESSION['location_bar']['level3']['path'] = ""; $_SESSION['location_bar']['level3']['label'] = ""; $_SESSION['location_bar']['level3']['id'] = "" ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } elseif(isset($_SESSION['location_bar']['level3']['id']) && trim($id_page) == trim($_SESSION['location_bar']['level3']['id'])) { $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } elseif($init || empty($_SESSION['location_bar']['level2']['id'])) { $_SESSION['location_bar']['level2']['path'] = $path; $_SESSION['location_bar']['level2']['path'] .= "&level=2"; $_SESSION['location_bar']['level2']['label'] = $this->wash_html($label); $_SESSION['location_bar']['level2']['id'] = $id_page ; $_SESSION['location_bar']['level3']['path'] = ""; $_SESSION['location_bar']['level3']['label'] = ""; $_SESSION['location_bar']['level3']['id'] = "" ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } else { if(empty($_SESSION['location_bar']['level3']['path'])) { $_SESSION['location_bar']['level3']['path'] = $path."&level=3"; $_SESSION['location_bar']['level3']['label'] = $this->wash_html($label); $_SESSION['location_bar']['level3']['id'] = $id_page ; $_SESSION['location_bar']['level4']['path'] = ""; $_SESSION['location_bar']['level4']['label'] = ""; $_SESSION['location_bar']['level4']['id'] = "" ; } else { $_SESSION['location_bar']['level4']['path'] = $path."&level=4"; $_SESSION['location_bar']['level4']['label'] = $this->wash_html($label); $_SESSION['location_bar']['level4']['id'] = $id_page ; } } } $this->where_am_i(); } /** * Uses javascript to rewrite the location bar * */ private function where_am_i() { if(empty($_SESSION['location_bar']['level2']['path'])) { ?>"; echo "
";
        print_r($arr);
        echo "
"; echo ""; } /** * 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 '

'.$title.'

'; } else { echo "

".$title."

"; } echo '
'; } ?>

 

 

 

 

 

 

'') { echo '
'; } } /** * Checks a date and writes the error in session if any * * @param $day string Day * @param $month string Month * @param $year string Year */ public function verif_date($day,$month,$year) { if($month > 12) { $_SESSION['error'] .= _BAD_MONTH_FORMAT.".
"; $_SESSION['monthstart'] = ""; } if($day > 31) { $_SESSION['error'] .= _BAD_DAY_FORMAT.".
"; $_SESSION['daystart'] = ""; } else { if($month == "2" || $month == "02") { if($day > 29) { $_SESSION['error'] .= _BAD_FEBRUARY.".
"; $_SESSION['daystart'] = ""; } } else { if($month == 2 || $month == 4 || $month == 6 || $month == 9 || $month == 11) { if($day > 30) { $_SESSION['error'] .= _BAD_DAY_FORMAT."t.
"; $_SESSION['daystart'] = ""; } } } } } /** * Displays a variable with a label if the var is not empty * * @param $what string The variable to display * @param $label string The label */ public function writeifnotempty($what,$label) { if(!empty($what)) { echo "".$label." : ".$what."
"; } } /** * Extracts the user informations from database and puts the result in an array * * @param $id integer User identifier */ public function infouser($id) { $conn = new dbquery(); $conn->connect(); $conn->query("select * from ".$_SESSION['tablename']['users']." where user_id = '".$id."'"); if($conn->nb_result() == 0) { return array("UserId" => "", "FirstName" => "", "LastName" => "", "Phone" => "", "Mail" => "", "department" => "" ); } else { $line = $conn->fetch_object(); return array("UserId" => $line->user_id, "FirstName" => $this->show_string($line->firstname), "LastName" => $this->show_string($line->lastname), "Phone" => $line->phone, "Mail" => $line->mail , "department" => $this->show_string($line->department) ); } } /** * Tests the exitence of a remote file * * @param $url string Url to connect * @return 1 if the URL host is empty, 2 if Unable to connect to remote host, true if the file exists and false otherwise */ function remote_file_exists ($url) { $head = ""; $url_p = parse_url($url); if (isset ($url_p["host"])) { $host = $url_p["host"]; } else { return 1; } if (isset ($url_p["path"])) { $path = $url_p["path"]; } else { $path = ""; } $fp = fsockopen ($host, 80, $errno, $errstr, 20); if (!$fp) { return 2; } else { $parse = parse_url($url); $host = $parse['host']; fputs($fp, "HEAD ".$url." HTTP/1.1\r\n" ); fputs($fp, "HOST: ".$host."\r\n" ); fputs($fp, "Connection: close\r\n\r\n" ); $headers = ""; while (!feof ($fp)) { $headers .= fgets ($fp, 128); } } fclose ($fp); $arr_headers = explode("\n", $headers); $return = false; if (isset ($arr_headers[0])) { $return = strpos ($arr_headers[0], "404" ) === false; } return $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 */ 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") { if ($withTimeZone) { return $day . "-" . $month . "-" . $year . "   " . $hours; }else{ return $day . "-" . $month . "-" . $year; } } else if ($databasetype == "POSTGRESQL") { if ($_SESSION['config']['lang'] == "fr") { if ($withTimeZone) { return $day . "-" . $month . "-" . $year . "   " . $hours; }else{ return $day . "-" . $month . "-" . $year; } } else { if ($withTimeZone) { 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 */ 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()) { $string = pg_escape_string($string); } 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 function show_string($string, $replace_CR = false, $chars_to_escape = array(), $databasetype = '') { 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); } for($i=0;$iLanguage file missing: '.$file.''; exit(); } $find = false; while (!feof($filelang)) { $string = fgets($filelang); $search="`'".$constant."'`"; preg_match($search,$string,$out); if(isset($out[0])) { $tab_string = explode("'", $string); if(isset($tab_string[5])) { $myresult = $tab_string[5]; $find = true; } else { $find = false; } break; } } if(!$find) { return false; } else { if(!empty($myresult)) { return $myresult; } else { return false; } } } /** * Convert some html entities in normal char (to correct a php bug concerning ’ and else) * * @param $string string The html entity to convert * @return string the converted result */ function convert_smart_quotes($string) { $search = array( '‘', '’', '“', '”', '—' ); $replace = array( "'", "'", '"', '"', '-' ); return str_replace($search, $replace, $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("
","\\n",$var); $var = str_replace("
","\\n",$var); $var = str_replace("
","\\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("
","\\n",$var); $var = str_replace("
","\\n",$var); $var = str_replace("
","\\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); } else { $var = str_replace("
","\\n",$var); $var = str_replace("
","\\n",$var); $var = str_replace("
","\\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; } /** * Restore html string, useful for full_text search * * @param $var string String to convert * @return Cleared string */ public function store_html($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); $var = str_replace("â","?",$var); $var = str_replace("î","?",$var); $var = str_replace("ô","?",$var); $var = str_replace("û","?",$var); return $var; } /** * Returns the next Easter date * * @param $year date (null by default) * @return date The next easter date */ public function WhenEasterCelebrates($year = null) { if (is_null($year)) { $year = (int)date ('Y'); } $iN = $year - 1900; $iA = $iN%19; $iB = floor (((7*$iA)+1)/19); $iC = ((11*$iA)-$iB+4)%29; $iD = floor ($iN/4); $iE = ($iN-$iC+$iD+31)%7; $time = 25-$iC-$iE; if($time > 0) { $WhenEasterCelebrates = strtotime ($year.'/04/'.$time); } else { $WhenEasterCelebrates = strtotime ($year.'/03/'.(31+$time)); } return $WhenEasterCelebrates; } /** * Returns the next open day * * @param $Date date * @param $Delta integer * @return date The next open day */ public function WhenOpenDay($Date, $Delta) { $Date = strtotime ($Date); $Hollidays = array ( '1_1', '1_5', '8_5', '14_7', '15_8', '1_11', '11_11', '25_12' ); if(function_exists ('easter_date')) { $WhenEasterCelebrates = easter_date ((int)date('Y'), $Date); } else { $WhenEasterCelebrates = getEaster ((int)date('Y'), $Date); } $Hollidays[] = date ('j_n', $WhenEasterCelebrates); $Hollidays[] = date ('j_n', $WhenEasterCelebrates + (86400*39)); $Hollidays[] = date ('j_n', $WhenEasterCelebrates + (86400*49)); $iEnd = $Delta * 86400; $i = 0; while ($i < $iEnd) { $i = strtotime ('+1 day', $i); if (in_array (date ('w', $Date+$i),array (0,6) ) || in_array (date ('j_n', $Date+$i), $Hollidays)) { $iEnd = strtotime ('+1 day', $iEnd); $Delta ++; } } return date('d/m/Y', $Date + (86400*$Delta)); } /** * Adds an interval to a date * * @param $Date date Date * @param $num integer Interval * @return date The date + the interval */ public function addDate($Date1, $num) { $Date1 = strtotime($Date1); $result = date ('d-m-Y', $Date1 + (86400*$num)); return $result; } /** * Substracts an interval to a date * * @param $Date date Date * @param $num integer Interval * @return date The date - the interval */ public function removeDate($Date1, $num) { $Date1 = strtotime($Date1); $result = date ('d-m-Y', $Date1 - (86400*$num)); return $result; } /** * Returns the next process day (Open day) after a given interval * * @param $delay integer Interval * @return date The next process day */ public function date_max_treatment($delay) { $result = $this->addDate(strftime("%Y")."-".strftime("%m")."-".strftime("%d"), $delay); $result = $this->WhenOpenDay($result, 1); return $result; } /** * Formats a datetime to a mm/dd/yyyy hh:ii:ss format (timestamp) * * @param $date datetime The date to format * @return datetime The formated date */ public function dateformaten($date) { $tmpArr = explode(" ",$date); $date = $tmpArr[0]; $time = $tmpArr[1]; $dateArr = explode("/",$date); if(preg_match('/\./',$time)) { $tmp = explode('.', $time); $time = $tmp[0]; } if(substr($tmpArr[1],0,2) == "00") { return $dateArr[1]."/".$dateArr[0]."/".$dateArr[2]; } else { return $dateArr[1]."/".$dateArr[0]."/".$dateArr[2]." ".$time; } } /** * 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 */ 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; } /** * Returns array of files in a folder * * @param $dir string The directory to check * @return array list of files */ function getFilesInFolder($dir) { $dir = opendir($dir); $isEmpty = true; $filesArray = array(); while (($entry = readdir($dir)) !== false) { if ($entry !== '.' && $entry !== '..' && $entry !== '.svn' && $entry !== 'backup') { array_push($filesArray, $entry); } } closedir($dir); return $filesArray; } /** * 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, '-_,', '+/')); } /** * Method to generates private and public keys */ public function generatePrivatePublicKey() { $privateKeyPath = $this->getPrivateKeyPath(); $publicKeyPath = $this->getPublicKeyPath(); if(!file_exists($privateKeyPath)) { $inF = fopen($privateKeyPath,"w"); fclose($inF); } if(!file_exists($publicKeyPath)) { $inF = fopen($publicKeyPath,"w"); fclose($inF); } $privateKey = openssl_pkey_new(array( 'private_key_bits' => 1024, 'private_key_type' => OPENSSL_KEYTYPE_RSA, )); $passphrase = ""; openssl_pkey_export_to_file($privateKey, $privateKeyPath, $passphrase); $keyDetails = openssl_pkey_get_details($privateKey); file_put_contents($publicKeyPath, $keyDetails['key']); } /** * 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; } //lgi tests function detectSmartphone() { $mobile_browser = '0'; if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda ','xda-'); if (in_array($mobile_ua,$mobile_agents)) { $mobile_browser++; } if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) { $mobile_browser++; } if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) { $mobile_browser = 0; } if ($mobile_browser > 0) { header('location: loginSmartphone.php'); } else { // nothing to do } } /** * Generate an UUID v4 * @return string UUID */ function gen_uuid() { return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), // 16 bits for "time_mid" mt_rand( 0, 0xffff ), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 mt_rand( 0, 0x0fff ) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 mt_rand( 0, 0x3fff ) | 0x8000, // 48 bits for "node" mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) ); } /** * Return the amount corresponding to the currency * @param $currency string currency of the amount * @param $amount float the amount */ function formatAmount($currency, $amount) { $formattedAmount = ''; if ($currency == 'EUR') { $formattedAmount = '€' . number_format($amount , 2 , ',' , '.' ); } elseif ($currency == 'DOL') { $formattedAmount = '$' . number_format($amount , 2 , ',' , '.' ); } elseif ($currency == 'YEN') { $formattedAmount = '¥' . number_format($amount , 2 , ',' , '.' ); } elseif ($currency == 'POU') { $formattedAmount = '£' . number_format($amount , 2 , ',' , '.' ); } else { $formattedAmount = ' ' . number_format($amount , 2 , ',' , '.' ); } return $formattedAmount; } }