-180) && ($value < 180)) ? 1 : 0; } } return $result; } /** * Valid value * * Inputs: * - value value * - digitsb digits before point * - digitsa digits after point * - Empty_valid An empty number also valid? * * Return: 1(OK)/0(Error) */ function valid_value($value, $empty_valid = 1, $digitsb = 10, $digitsa = 2) { $before = ($digitsb - $digitsa); if ($empty_valid) { $result = ((preg_match("/^[0-9]{0,$before}(\.[0-9]{0,$digitsa})?$/", $value)) || (!strlen($value))); } else { $result = (preg_match("/^[0-9]{0,$before}(\.[0-9]{0,$digitsa})?$/", $value)); } return $result; } /** * Convert date * * Inputs: * - date Input date/ * - from_unix Convert from (0) or to (1) unix timestamp * * Return: unix date */ function convert_date($date, $from_unix = 0) { if (strlen($date)) { if ($from_unix) { $result = date('Y-m-d', $date); } else { $array = explode("-", $date); $result = mktime(0,0,0,$array[1],$array[2],$array[0]); } } else { $result = ""; } return $result; } /** * Convert datetime * * Inputs: * - date Input date/timestamp * - from_unix Convert from (0) or to (1) unix timestamp * * Return: unix timestamp/date */ function convert_datetime($datetime, $from_unix = 0) { // Initial values $result = ""; if (strlen($datetime)) { if ($from_unix) { // Y2K+38 BUG!! if ($datetime < 2145913200) { $result = date('Y-m-d H:i:s', $datetime); } else { DBG("Y2K+38 BUG!"); } } else { $array = explode(" ", $datetime); $date = ((count($array) > 0) && strlen($array[0])) ? explode("-", $array[0]) : array("0","0","0"); $time = ((count($array) > 1) && strlen($array[1])) ? explode(":", $array[1]) : array("0","0","0"); $result = mktime(intval($time[0]),intval($time[1]),intval($time[2]),intval($date[1]),intval($date[2]),intval($date[0])); } } return $result; } /** * Convert date/time to ISO 8601 date/time string * * Inputs: * - date Input date/timestamp * - flags ISO8601_xxx flags (orred) * * Return: ISO 8601 formatted string */ define("ISO8601_DEFAULT", 0); define("ISO8601_WITH_DATE", 0); // placeholder define("ISO8601_WITH_TIME", 0); // placeholder define("ISO8601_NO_DATE", 0x0001); define("ISO8601_NO_TIME", 0x0002); define("ISO8601_WITH_TZ", 0x0010); function iso8601($datetime, $flags = ISO8601_DEFAULT) { if( !is_numeric($datetime) ) $datetime = convert_datetime($datetime, FALSE); // Build format string $fmt = ""; if( !($flags & ISO8601_NO_DATE) ) $fmt .= "%Y%m%d"; if( !($flags & (ISO8601_NO_DATE | ISO8601_NO_TIME)) ) $fmt .= "T"; if( !($flags & ISO8601_NO_TIME) ) $fmt .= "%H%M%S"; if( ($flags & ISO8601_WITH_TZ) ) $fmt .= "%z"; return strftime($fmt, $datetime); } /** * Valid start/end date * * Inputs: * - start Start date * - end End date * * Return: 1(OK)/0(Error) */ function valid_start_end_date($start, $end) { $result = 0; if ((strlen($start)) && (strlen($end))) { $start_stamp = convert_date($start); $end_stamp = convert_date($end); // Check if end date is not before start date if ($start_stamp < $end_stamp) { $result = 1; } } else { $result = 1; } return $result; } /** * Valid gps timestamp * * Inputs: * - timestamp * * Return: 1(OK)/0(Error) */ function valid_gps_timestamp($timestamp) { // Initial values $result = 0; if (strlen($timestamp)) { $array = explode(" ", $timestamp); $date = explode("-", $array[0]); // No gps date => 1 jan 1970 $result = ($date[0] == 1970) ? 0 : 1; if ($result) { // 1 day after now? $result = (convert_datetime($timestamp) >= convert_datetime(date('Y-m-d H:i:s')) + 86400) ? 0 : $result; } } return $result; } /** * Valid start/end datetime * * Inputs: * - start_date Start date * - start_time Start time * - end_date End date * - end_time End time * * Return: 1(OK)/0(Error) */ function valid_start_end_datetime($start_date, $start_time, $end_date, $end_time) { $result = 0; if ((strlen($start_date)) && (strlen($end_date))) { $start_stamp = convert_datetime($start_date . " " . $start_time); $end_stamp = convert_datetime($end_date . " " . $end_time); // Check if end date is not before start date if ($start_stamp < $end_stamp) { $result = 1; } } else { $result = 1; } return $result; } /** * Valid timestamp * * Inputs: * - begin Begin date/datetime * - eind Eind date/datetime * - datetime (1) datetime/ (0) date * * Return: 1(OK)/0(Error) */ function valid_timestamp($begin, $eind, $datetime = 0) { $result = 1; // retrieve current timestamp $current = ($datetime) ? convert_datetime(date('Y-m-d H:i:s')) : convert_date(date('Y-m-d')); // Convert database begin/end stamp $begin = ($datetime) ? convert_datetime($begin) : convert_date($begin); $end = ($datetime) ? convert_datetime($eind) : convert_date($eind); if ((strlen($begin)) && (strlen($end))) { if (($current < $begin) || ($current > $end)) { $result = 0; } } else if (strlen($begin)) { if ($current < $begin) { $result = 0; } } else if (strlen($end)) { if ($current > $end) { $result = 0; } } return $result; } /** * convert timeperiod => valid timeperiod * * Inputs: * - begin Begin date/datetime * - eind Eind date/datetime * - begin_period Begin period * - end_period End period * * Return: array containing begin/end */ function convert_timeperiod($begin, $end, $begin_period, $end_period) { $result = ""; // Convert begin/end stamps $begin_period = (strlen($begin_period)) ? convert_datetime($begin_period) : ""; $end_period = (strlen($end_period)) ? convert_datetime($end_period) : ""; $begin = (strlen($begin)) ? convert_datetime($begin) : ""; $end = (strlen($end)) ? convert_datetime($end) : ""; // valid begin/end if ((strlen($begin)) && (strlen($end))) { if ((strlen($begin_period)) && (strlen($end_period))) { // Valid? if (($begin <= $end_period) && ($end >= $begin_period)) { // Correct start time? if ($begin < $begin_period) { $begin = $begin_period; } // Correct end time? if ($end > $end_period) { $end = $end_period; } $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($begin_period)) { // Valid? if ($end >= $begin_period) { // Correct start time? if ($begin < $begin_period) { $begin = $begin_period; } $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($end_period)) { // Valid? if ($begin <= $end_period) { // Correct end time? if ($end > $end_period) { $end = $end_period; } $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($begin)) { if ((strlen($begin_period)) && (strlen($end_period))) { if (($begin >= $begin_period) && ($begin <= $end_period)) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($begin_period)) { if ($begin >= $begin_period) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($end_period)) { if ($begin <= $end_period) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($end)) { if ((strlen($begin_period)) && (strlen($end_period))) { if (($end >= $begin_period) && ($end <= $end_period)) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($begin_period)) { if ($end >= $begin_period) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else if (strlen($end_period)) { if ($end >= $end_period) { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } else { $result = array(convert_datetime($begin,1), convert_datetime($end,1)); } } return $result; } /** * Check action * * Inputs: * * Return: 1(OK)/0(Error) */ function is_valid_action() { GLOBAL $_PAGE_INFO; // Initial result value $result = 0; // Retrieve number of arguments $numargs = func_num_args(); if ($numargs) { // Retrieve arguments $arg_list = func_get_args(); // Check arguments for ($i = 0; $i < $numargs; $i++) { if ((isset($_SESSION[$_PAGE_INFO['id']]['action'])) && ($_SESSION[$_PAGE_INFO['id']]['action'] == $arg_list[$i])) { // Result OK $result = 1; } } } else { $result = ((isset($_SESSION[$_PAGE_INFO['id']]['action'])) && (strlen($_SESSION[$_PAGE_INFO['id']]['action']))) ? 1 : 0; } return $result; } /** * Check recall * * Inputs: * * Return: 1(OK)/0(Error) */ function is_valid_recall() { GLOBAL $_PAGE_INFO; // Initial result value $result = 0; // Retrieve number of arguments $numargs = func_num_args(); if (($numargs) && (isset($_SESSION[$_PAGE_INFO['id']]['recall']))) { // Retrieve arguments $arg_list = func_get_args(); // Split recall string $rec = split(",", $_SESSION[$_PAGE_INFO['id']]['recall']); // Check arguments for ($i = 0; $i < $numargs; $i++) { if (in_array($arg_list[$i], $rec)) { // Result OK $result = 1; } } } // Valid recall? else if (isset($_SESSION[$_PAGE_INFO['id']]['recall'])) { $result = 1; } return $result; } /** * Store recall */ function store_recall($array) { GLOBAL $_PAGE_INFO; // Clear old recall action unset($_SESSION[$_PAGE_INFO['id']]['recall']); // Find new recall action if (is_array($array)) { foreach ($array as $key => $value) { $pos = strpos($key, "recall_"); if ($pos !== FALSE) { if (isset($_SESSION[$_PAGE_INFO['id']]['recall'])) { $_SESSION[$_PAGE_INFO['id']]['recall'] = $_SESSION[$_PAGE_INFO['id']]['recall'] . "," . substr($key, ($pos + strlen("recall_"))); } else { $_SESSION[$_PAGE_INFO['id']]['recall'] = substr($key, ($pos + strlen("recall_"))); } } } } } /** * Strip time from date_time value * * Inputs: * - date_time Date time value */ function strip_time($date_time) { $array = explode(" ", $date_time); return $array[0]; } /** * Strip date from date_time value * * Inputs: * - date_time Date time value */ function strip_date($date_time, $skip_seconds = 1) { $array = explode(" ", $date_time); if ($skip_seconds) { if (isset($array[1])) { $pos1 = strpos($array[1], ":"); $start = (is_integer($pos1)) ? ($pos1 + 1) : 0; $pos2 = strpos($array[1], ":", $start); $result = substr($array[1], 0, $pos2); } else { $result = ""; } } else { $result = $array[1]; } return $result; } /** * Is Read only? * * Inputs: * - $ro_array Read only array * * Return: not ro(0)/ro(1) */ function is_ro($ro_array) { $result = 0; if (is_array($ro_array)) { foreach ($ro_array as $ro) { if (is_valid_action($ro)) { $result = 1; } } } return $result; } /** * Store href history * */ function store_history($href) { GLOBAL $_PAGE_INFO; // Store new value $_SESSION[$_PAGE_INFO['id']]['href_history_prev'] = $_SESSION[$_PAGE_INFO['id']]['href_history']; $_SESSION[$_PAGE_INFO['id']]['href_history'] = $href; } /** * Detect browser * * Return: String containing browser */ function browser() { $browser_user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? strtolower($_SERVER['HTTP_USER_AGENT']) : ''; if(stristr($browser_user_agent, "Opera")) { return "OPERA"; } else if(stristr($browser_user_agent, "Msie")) { return "IE"; } else if(stristr($browser_user_agent, "Netscape")) { return "NETSCAPE"; } else if(stristr($browser_user_agent, "Firefox")) { return "FIREFOX"; } else if(stristr($browser_user_agent, "SeaMonkey")) { return "SEAMONKEY"; } else if(stristr($browser_user_agent, "Chrome")) { return "CHROME"; } else if(stristr($browser_user_agent, "Safari")) { return "SAFARI"; } else if(stristr($browser_user_agent, "Gecko")) { return "MOZILLA"; } else { return "UNKNOWN"; } } /** * Detect browser version * * Return: version */ function browser_version() { $browser_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']); // you can add different browsers with the same way .. if(preg_match('/(chromium)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'chromium'; } else if (preg_match('/(chrome)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'chrome'; } else if (preg_match('/(safari)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'safari'; } else if (preg_match('/(opera)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'opera'; } else if (preg_match('/(msie)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'msie'; } else if (preg_match('/(mozilla)[ \/]([\w.]+)/', $browser_user_agent)) { $browser = 'mozilla'; } preg_match('/('.$browser.')[ \/]([\w]+)/', $browser_user_agent, $version); return $version[2]; } /** * create a random code * * Return: String containing random code (number-char-number-etc.) */ function createRandomCode($nmr) { $chars = "abcdefghijkmnpqrstuvwxyz"; $nums = "023456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i < $nmr) { if ($i % 2) { $num = rand() % 24; $tmp = substr($chars, $num, 1); } else { $num = rand() % 9; $tmp = substr($nums, $num, 1); } $pass = $pass . $tmp; $i++; } return $pass; } /** * create a random code * * Return: String containing random code (only numbers) */ function createRandomCodeNumberOnly($nmr) { $nums = "0123456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i < $nmr) { $num = rand() % 9; $tmp = substr($nums, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } /** * Check device * * Return: Ok (1)/error (0) */ function is_dev() { GLOBAL $_PAGE_INFO; // Initial result value $result = 0; // Retrieve number of arguments $numargs = func_num_args(); if ($numargs) { // Retrieve arguments $arg_list = func_get_args(); // Check arguments for ($i = 0; $i < $numargs; $i++) { if (strtolower($_PAGE_INFO['MTinfo_device']) == strtolower($arg_list[$i])) { // Result OK $result = 1; } } } return $result; } /** * Get microtime */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } /** * Sort multidimesional araay */ function array_sort($array, $keyname, $type = SORT_STRING, $asc_desc = SORT_ASC) { $sort_array = array(); // Retrieve data if (is_array($array)) { foreach($array as $key => $data) { array_push($sort_array, strtolower($data[$keyname])); } array_multisort($sort_array, $type, $asc_desc, $array); } return $array; } /** * Remove duplicated keys, also for multidimesional arrays */ function arrayUnique($array, $preserveKeys = false) { // Unique Array for return $arrayRewrite = array(); // Array with the md5 hashes $arrayHashes = array(); foreach($array as $key => $item) { // Serialize the current element and create a md5 hash $hash = md5(serialize($item)); // If the md5 didn't come up yet, add the element to // to arrayRewrite, otherwise drop it if (!isset($arrayHashes[$hash])) { // Save the current element hash $arrayHashes[$hash] = $hash; // Add element to the unique Array if ($preserveKeys) { $arrayRewrite[$key] = $item; } else { $arrayRewrite[] = $item; } } } return $arrayRewrite; } /** * Remove extension */ function RemoveExtension($strName) { $ext = strrchr($strName, '.'); if($ext !== false) { $strName = substr($strName, 0, -strlen($ext)); } return $strName; } /** * Get extension */ function GetExtension($strName) { $ext = strrchr($strName, '.'); return $ext; } /** * Retrieve all timezones */ function retrieve_timezones() { // Initial value $result = ""; if (file_exists(TIME_ZONE_FILE)) { // open file which contain timezones $handle = fopen(TIME_ZONE_FILE, 'r'); while(!feof($handle)) { // Read line $line = fgets($handle); if ($line[0] != "#") { // Parse into result if (!is_array($result)) { $result = array(); } // Split by tabs $line_array = split("\t", $line); if ((strlen($line_array[2])) && (strlen($line_array[0]))) { array_push($result, array(timezone => str_replace("\n","",$line_array[2]), code => $line_array[0])); } } } // Add UTC if (is_array($result)) { array_push($result, array(timezone => "UTC")); } // Close file fclose($handle); } return array_sort($result, "timezone"); } /** * Get random image */ function get_random_image($dir, $allowed_ext = array("jpg", "png", "gif", "bmp", "ico")) { // Create file array $files = array(); // Existing dir? if (is_dir($dir)) { // Open handle if ($handle = opendir($dir)) { // Read all files from dir while($file = readdir($handle)) { // Get extension $ext = strtolower(substr(strrchr($file, "."), 1)); // Allowed extension? if ((!is_array($allowed_ext)) || (in_array($ext, $allowed_ext))) { if (($file != ".") && ($file != "..") && !is_dir($file)) { array_push($files, $dir . $file); } } } // Close handle closedir($handle); } } // Select (random) image if (empty($files)) { $result = ""; } else { srand((float) microtime() * 10000000); $result = $files[array_rand($files, 1)]; } // Return image return $result; } /** * Get all files */ function get_all_files($dir, $allowed_ext = "") { // Create file array $files = array(); // Existing dir? if (is_dir($dir)) { // Open handle if ($handle = opendir($dir)) { // Read all files from dir while($file = readdir($handle)) { // Get extension $ext = strtolower(substr(strrchr($file, "."), 1)); // Allowed extension? if ((!is_array($allowed_ext)) || (in_array($ext, $allowed_ext))) { if (($file != ".") && ($file != "..") && !is_dir($file)) { array_push($files, $dir . $file); } } } // Close handle closedir($handle); } } // Sort array sort($files); // Return files return $files; } /** * Get all directories */ function get_all_dirs($dir) { // Create dir array $dirs = array(); // Existing dir? if ($handle = opendir($dir)) { if ($handle !== FALSE) { do { $dir = readdir($handle); // Skip '.'/'..'/CVS if (($dir !== FALSE) && ($dir != ".") && ($dir != "..") && ($dir != "CVS")) { array_push($dirs, $dir); } } while($dir !== FALSE); } } // Sort array sort($dirs); // Return dir return $dirs; } /** * Define number of lines and return array containing lines */ function nmr_lines($text, $chars_on_line) { // Initial value $result = array(); $finished = 0; $index = 0; do { // Initial values $skip = 0; $length = 0; // Find carriage return $pos_cr = strpos($text, "\n"); // Found carriage return? if (($pos_cr !== FALSE) && ($pos_cr <= $chars_on_line)) { // Split up line $length = $pos_cr; // Skip cr char $skip = 1; } // EOL else if (strlen($text) <= $chars_on_line) { $length = strlen($text); } else { // Find space $pos_space = strpos($text, " "); // Found space if (($pos_space !== FALSE) && ($pos_space <= $chars_on_line)) { // Skip space char $skip = 1; // Initial counter value $counter = $pos_space; do { // Find another space $pos_space = strpos($text, " ", $counter + 1); // Find one within chars_on_line? if (($pos_space !== FALSE) && ($pos_space <= $chars_on_line)) { // increment offset $counter = $pos_space; } else { // Split up line $length = $counter; } } while (!$length); } else { // Split up line after chars_on_line $length = (strlen($text) >= $chars_on_line) ? $chars_on_line : strlen($text); } } // Parse result $result[$index++] = substr($text, 0, $length); // Adapt text $text = substr($text, ($length + $skip)); // Finished? $finished = (!strlen($text)) ? 1 : 0; } while(!$finished); return $result; } /** * Shorten text */ define("SHORTEN_NONE", 0); define("SHORTEN_CAPS", 0x01); define("SHORTEN_NARROW", 0x02); define("SHORTEN_ALL", 0x03); // flags define("SHORTEN_HTML", 0x10); define("SHORTEN_NO_SUFFIX", 0x20); // don't add an ellipsis after the string function shorten_text($value, $max_size = 25, $adjust = SHORTEN_NONE, $html = TRUE) { // Initial value $result = array(); // override 'html' flag if( ($adjust & SHORTEN_HTML) ) $html = TRUE; if( $html ) { // count html character entities as one character $value_wo_html = preg_replace("/&.+;/U", "x", $value); $max_size += strlen($value) - strlen($value_wo_html); } else $value_wo_html = $value; // Determine value length for( $i = 0, $n = 0; $i < strlen($value); $i++ ) { // copy the first 'max_size' characters, minding HTML characters like "&" if( $html && substr($value, $i, 1) == "&" ) { $char = substr($value, $i, 1); do { $char .= substr($value, ++$i, 1); } while( substr($value, $i, 1) != ";" && $i < strlen($value) ); } else { $char = substr($value, $i, 1); // Adjust for wide and narrow characters? if( $adjust && $n < $max_size ) { // Clip the name of an item in the menu structure (minding nearower letters) // Capitals and lower case 'm' and 'w' are wide if( ($adjust & SHORTEN_CAPS) ) { $max_size -= preg_match_all("/([A-Zmw])/", $char, $dummy) * 1.2; $max_size -= preg_match_all("/([MW])/", $char, $dummy) * 1.2; } // Compensate for narrow letters if( ($adjust & SHORTEN_NARROW) ) { $max_size += preg_match_all("/([.,;:il])/", $char, $dummy) / 1.2; } } } $result[$n++] = $char; } if( count($result) > $max_size ) { if( ($adjust & SHORTEN_NO_SUFFIX) ) { $result = array_splice($result, 0, $max_size); } else { $result = array_splice($result, 0, $max_size - 2); $result[] = ($html ? "…" : "..."); } } return implode("", $result); } /** * Tablet? */ function is_tablet() { return ((!isset($_COOKIE['screen_width'])) || (!strlen($_COOKIE['screen_width'])) || ($_COOKIE['screen_width'] >= 1024)) ? 0 : 1; } /** * Write cookie */ function WriteCookie($key, $value, $remove = 0, $days = 100) { if (!$remove) { $datetime = time() + $days * (24 * 60 * 60); } else { $datetime = time(); } // Set secured/http only (only for 5.2.0 or higher) cookie if (PHPValid(5,2,0)) { setcookie($key, $value, $datetime, "/", "", TRUE, TRUE); } else { setcookie($key, $value, $datetime, "/", "", TRUE); } } /** * Update globals */ function UpdateGlobals() { GLOBAL $_PAGE_INFO; // First all root items $items = array("i18n","base","base_path","skin_name","skin","logged_on"); foreach($items as $item) { if (isset($_SESSION[$_PAGE_INFO['id']][$item])) { $_PAGE_INFO[$item] = $_SESSION[$_PAGE_INFO['id']][$item]; } } // Next all login items $items = array("user","customer","project"); foreach($items as $item) { if (isset($_SESSION[$_PAGE_INFO['id']]['login'][$item]['id'])) { $_PAGE_INFO['login'][$item]['id'] = $_SESSION[$_PAGE_INFO['id']]['login'][$item]['id']; } } } /** * Send RS3000 message */ function SendMessageRS3000($params = "") { GLOBAL $_PAGE_INFO; // Initial values $message = ""; $split_char = ","; // Send session id $message = "id='" . $_PAGE_INFO['id'] ."'"; // Send i18n if (isset($_PAGE_INFO['i18n'])) { $message .= $split_char . "i18n='" . $_PAGE_INFO['i18n'] . "'"; } // Send project id if (isset($_PAGE_INFO['login']['project']['id'])) { $message .= $split_char . "page_project='" . $_PAGE_INFO['login']['project']['id'] . "'"; } // motd? $motd = db_fetch_system_maint_mes($_PAGE_INFO['i18n']); if (is_array($motd)) { if (strlen($motd['onderhoud'])) { $message .= $split_char . "motd='" . str_replace($split_char, " ", $motd['onderhoud']) . "'"; } } // Send params if (is_array($params)) { $message .= $split_char . implode($split_char, $params); } if (strlen($message)) { if (LOG_RS) { if ((!empty($_SESSION[$_PAGE_INFO['id']]['login']['imei'])) || (!empty($_SESSION[$_PAGE_INFO['id']]['login']['user']['id']))) { $user_info = (!empty($_SESSION[$_PAGE_INFO['id']]['login']['user']['id'])) ? "user: " . $_SESSION[$_PAGE_INFO['id']]['login']['user']['id'] : ""; $imei_info = (!empty($_SESSION[$_PAGE_INFO['id']]['login']['imei'])) ? "imei: " . $_SESSION[$_PAGE_INFO['id']]['login']['imei'] : ""; $separator = ((!empty($_SESSION[$_PAGE_INFO['id']]['login']['imei'])) && (!empty($_SESSION[$_PAGE_INFO['id']]['login']['user']['id']))) ? ", " : ""; DBG("RS3000 (" . $user_info . $separator . $imei_info . "): " . $message); } else { DBG("RS3000: " . $message); } } echo $message; } } /* * Debug function */ function DBG($value, $email = "") { // Get IP $remote_addr = (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) ? " (" . $_SERVER['HTTP_X_FORWARDED_FOR'] . ")" : ((isset($_SERVER['REMOTE_ADDR'])) ? " (" . $_SERVER['REMOTE_ADDR'] . ")" : ""); if (is_array($value)) { $count = 0; foreach($value as $key => $item) { syslog(LOG_INFO, "MTinfo debug" . $remote_addr . ": " . (sprintf("%-15s",$key)) . ": " . (sprintf("%-50s",$item)) . "(" . ++$count . "/" . sizeof($value) . ")"); if (strlen($email)) { if (valid_email($email, FALSE)) { send_mail($email, "", "", "debug@dualinventive.com", "MTinfo debug", $value[$i] . "(" . $i . "/" . sizeof($value) . ")"); } } } } else if (strlen($value)) { syslog(LOG_INFO, "MTinfo debug" . $remote_addr . ": " . $value); if (strlen($email)) { if (valid_email($email, FALSE)) { send_mail($email, "", "", "debug@dualinventive.com", "MTinfo debug", $value); } } } } /** * Is this release candidate? * * Return: Release candidate (TRUE)/Release (FALSE) */ function is_ReleaseCandidate() { GLOBAL $_RELEASE; // Initial result value $result = FALSE; // Does the script name contain release candidate directory? $result = (!strlen($_RELEASE['rc']['dir']) || stristr(realpath($_SERVER['SCRIPT_FILENAME']), $_RELEASE['rc']['dir']) === FALSE) ? FALSE : TRUE; return $result; } /** * Calculate duration * * Return: String containing duration */ function CalculateDuration($start, $end) { $duration = convert_datetime($end) - convert_datetime($start); $hours = (int)($duration / 3600); $duration = (int)$duration - ($hours * 3600); $minutes = (int)($duration / 60); $duration = (int)$duration - ($minutes * 60); $seconds = (int)($duration); return sprintf("%02u:%02u:%02u", $hours, $minutes, $seconds); } /** * Next service date * * Return: String containing next service date */ function NextService($service, $sw3000) { // Get next switch memory service date $sw3000_service = strtotime(date("Y-m-d", strtotime($sw3000)) . " +4 years"); if (strlen($sw3000)) { if ((!strlen($service)) || (convert_datetime($service) > $sw3000_service)) { $service = convert_datetime($sw3000_service, TRUE); } } return $service; } /** * Hash a password * * See "CheckPassword()" below for notes and the history of this function. */ function HashPassword($input, $uid) { // Salt which will be added to password if( $uid ) { // "random" salt as recommened by security audit (v1.0 of 20131023) // the 'bcrypt' algorithm is not available, so we will resort to SHA-1 // with a "random" salt based on the user-id (which is random for any // third party, so there is no reason not to...) $uid = intval($uid); $salt_prefix = "\x50\x31"; $salt_suffix = "\x53\x84"; for( $i = 0; $i <= 30; $i += 6 ) { $salt_prefix .= chr(($uid >> $i) & 0xFF); $salt_suffix .= chr(($uid >> (30 - $i)) & 0xFF); } return sha1($salt_prefix . strtolower($input) . $salt_suffix); } else { $salt = "/$DI$/SALT&/"; return md5($salt . strtolower($input) . $salt); } } /** * Check password * * Input: * - input User input (plain text) * - password Encrypted/hashed password in the database to match * - uid User identifier * Returns: * - FALSE or one of the defines below (which will evaluate to "TRUE" where needed) * * Notes and history: * - The first version only matched MD5 hashes, but this was considered * inadequate after the first security audit. A "salt" was added. * - After the second security audit, a fixed salt was no longer adequate * so the salt is now "randomized" (i.e. the user-id is added to the * "salt" as this is hidden information and therefore sufficiently random * as the purpose of the salt is to prevent that an external party can * "guess" the passwords using a rainbow table or whichever other technique) * - The recommended "bcrypt()" function is not documented (and the site * "php.net" is down because theire HTTPS certificate has been revoked???). * The function "crypt()", which does have an implementation of the blowfish * algorithm, does not seem to return valid data on both 'arcfs12' and the * 'rootnet' servers. The output should be used as 'salt' to check the * user's password, but this does not work nor does it produce the same * output as the examples??? Moreover, when the (obviously) incorrect output * was used, it simply returned the "salt" as its output in a second run, * regardless of the data (password) to hash. * - An extra effort will be made to make sure that the encrypted passwords * are not visible, so it will be impossible to match them against a * "rainbow table". Note that the encrypted password were only "visible" in * the HTML source of the "change user" (and the similar "change password") * page and only when a user was logged in. * - The fact that MD5 or SHA1 are "too quick" is nonsense * disputable; see the comments made on the page below: * "http://us3.php.net/manual/en/faq.passwords.php#faq.passwords.fasthash" * Besides that, this only applies when an attacker gets hold of the database * data (or hashed passwords in general) and this should be avoided at all * cost as this would render any encryption void. Security starts at the * front door. */ define("PASSWD_MD5", 1); define("PASSWD_SALTED", 2); define("PASSWD_RND_SALT", 3); define("PASSWD_BCRYPT", 4); // perhaps some time in the future // the next define is used during the login process to automatically upgrade // the user's password to the recommended encryption; older encryptions // (MD5, salted MD5) may be discontinued in the future and any user that // hasn't logged on since this automatic upgrade has been effectuated, will // not be able to do so when the old encryption methods are no longer // supported // NOTE / TO DO: the automatic upgrading of passwords must be activated // when this functionality is in the live tree; NOT BEFORE! define("PASSWD_RECOMMENDED_ENCRYPTION", 0); // TO DO TO ACTIVATE AUTOMATIC UPGRADES: define("PASSWD_RECOMMENDED_ENCRYPTION", PASSWD_RND_SALT); // display text for password; control characters would have been nice (because // the user cannot enter those) but the browser (or HTTP RFC) filters those // out, so no such luck there. define("PASSWD_DISPLAY_TEXT", "sterretjes"); function CheckPassword($input, $password, $uid) { if( !$uid ) return FALSE; if( md5(strtolower($input)) == $password ) return PASSWD_MD5; else if( HashPassword($input, NULL) == $password ) return PASSWD_SALTED; else if( HashPassword($input, $uid) == $password ) return PASSWD_RND_SALT; else return FALSE; } /** * Check password complexity * * Input * - plain text password * * Returns: boolean (TRUE if the password is sufficiently complex, FALSE if not) * * Note: there are no rules at the moment, so this function only checks that * the password is not empty. */ function CheckPasswordComplexity($input) { return strlen($input) > 0; } /** * Check directory */ function CheckDirectory($allowedDir, $actualFile) { // Initial values $result = FALSE; $actualFile = realpath(dirname($actualFile)); if (is_array($allowedDir)) { foreach($allowedDir as $dir) { $dir = realpath($dir); if (($dir !== FALSE) && ($actualFile !== FALSE)) { if ($dir == $actualFile) { $result = TRUE; } } } } else { $allowedDir = realpath($allowedDir); if (($allowedDir !== FALSE) && ($actualFile !== FALSE)) { if ($allowedDir == $actualFile) { $result = TRUE; } } } return $result; } /** * Calculate Short circuit quality */ function CalcShortCircuitQuality($b_a_limit, $b_a_current) { $val = (100 * (1 - (pow(($b_a_current - 0.125),4)/pow(($b_a_limit - 0.125),4)))); return ($val < 0) ? 0 : $val; } /** * Check for active branch */ function ActiveBranch() { global $_DEFAULT, $_HOSTNAME; return in_array(file_get_contents($_DEFAULT['active_branch']), $_HOSTNAME); } /** * Object to array conversion */ function object_to_array($data) { if(is_array($data) || is_object($data)) { $result = array(); foreach($data as $key => $value) { $result[$key] = object_to_array($value); } return $result; } return $data; } /** * Encrypt data */ function encrypt($data, $password) { return base64_encode($password . $data); } /** * Decrypt data */ function decrypt($data, $password) { return substr(base64_decode($data), strlen($password)); } /** * Check if current php version is valid (or higher of course) */ function PHPValid($major, $minor, $release) { // Initial value $result = TRUE; // Get php version $version = phpversion(); // Difference between php versions if (!is_array($version)) { $version = explode('.', $version); } if ($major > $version[0]) { $result = FALSE; } else if ($major == $version[0]) { if ($minor > $version[1]) { $result = FALSE; } else if ($minor == $version[1]) { if ($release > $version[2]) { $result = FALSE; } } } return $result; } /** * Check if a string is serialized * @param string $string */ function is_serial($string) { return (@unserialize($string) !== false || $string == 'b:0;'); } /** * encode characters (mostly to eliminate html tags) */ function specialchars($str) { //global $_PAGE_INFO; //return htmlentities($str, ENT_QUOTES, $_PAGE_INFO['charset']); return addslashes($str); } /** * htmlspecialchars for array (recursive) * @param string $string */ function htmlspecialchars_array(&$variable) { foreach ($variable as &$value) { if (!is_array($value)) { $value = specialchars($value); } else { htmlspecialchars_array($value); } } } /** * htmlspecialchars_decode for array (recursive) * @param string $string */ function htmlspecialchars_decode_array(&$variable) { foreach ($variable as &$value) { if (!is_array($value)) { $value = htmlspecialchars_decode($value, ENT_QUOTES); } else { htmlspecialchars_decode_array($value); } } } /** * Support for error tracing: get the call trace as a string * * Inputs: * - convert Convert output (string parameter), currently only * "html" and anything else is plain text * - skip_frame Skip the first frames; by default only frame #0 * is skipped (the function 'backtrace()' itself) */ function calltrace($convert = FALSE, $skip_frame = 0) { $backtrace = ""; $frame = 0; $call_trace = debug_backtrace(); foreach( $call_trace as $call ) { if( $frame > $skip_frame ) { $backtrace .= "#" . ($frame++ - $skip_frame - 1) . " "; if( $call['class'] ) $backtrace .= $call['class'] . " "; if( $call['object'] ) $backtrace .= serialize($call['object']); $backtrace .= $call['type'] . $call['function'] . "("; $sep = ""; if( $call['args'] ) foreach( $call['args'] as $arg ) { $backtrace .= $sep; $sep = ","; if( is_null($arg) ) $backtrace .= "NULL"; else if( is_string($arg) ) $backtrace .= "\"" . $arg . "\""; else if( is_bool($arg) ) $backtrace .= ($arg ? "TRUE" : "FALSE"); else $backtrace .= $arg; } $backtrace .= ") [" . $call['file'] . ":" . $call['line'] . "]\n"; } else { // no use showing the "db_report_slow_query" frame $frame++; } } // convert output? if( $convert ) switch( strtolower($convert) ) { case "html": case "text/html": // mime type :-) $backtrace = nl2br(specialchars($backtrace)); break; // default: text/plain } return $backtrace; } ?>