. */ /** * Functions defined here are not directly accessing the database. */ // Require database layer functions if (isset($kga['server_conn'])) require("db_layer_".$kga['server_conn'].".php"); /** * Prepare all values of the array so it's save to put them into an sql query. * The conversion to utf8 is done here as well, if configured. * * @param array $data Array which values are being prepared. * @return array The same array, except all values are being escaped correctly. */ function clean_data($data) { global $kga; foreach ($data as $key => $value) { if ($key != "pw") { $return[$key] = urldecode(strip_tags($data[$key])); $return[$key] = str_replace('"','_',$data[$key]); $return[$key] = str_replace("'",'_',$data[$key]); $return[$key] = str_replace('\\','',$data[$key]); } else { $return[$key] = $data[$key]; } if ($kga['utf8']) $return[$key] = utf8_decode($return[$key]); } return $return; } /** * Kill the current users session by redirecting him to the logout page. */ function kickUser() { die(""); } /** * returns formatted time string -> h:mm * input: number of seconds * * @param integer $sek seconds to extract the time from * @return string * @author th * @deprecated use formatDuration instead */ function intervallApos($sek) { if (is_array($sek)) { $arr = array(); foreach ($sek as $key=>$value) { $arr[$key] = intervallApos($value); } return $arr; } else return sprintf('%d:%02d', $sek / 3600, $sek / 60 % 60); } /** * returns formatted time string -> h:mm:ss * input: number of seconds * * @param integer $sek seconds to extract the time from * @return string * @author th * @deprecated use formatDuration instead */ function intervallColon($sek) { return sprintf('%d:%02d:%02d', $sek / 3600, $sek / 60 % 60, $sek % 60); } /** * Format a duration given in seconds according to the global setting. Either * seconds are shown or not. * * @param integer|array one value in seconds or an array of values in seconds * @return integer|array depending on the $sek param which contains the formatted duration * @author sl */ function formatDuration($sek) { global $kga; if (is_array($sek)) { // Convert all values of the array. $arr = array(); foreach ($sek as $key=>$value) { $arr[$key] = formatDuration($value); } return $arr; } else { // Format accordingly. if ($kga['conf']['durationWithSeconds'] == 0) return sprintf('%d:%02d', $sek / 3600, $sek / 60 % 60); else return sprintf('%d:%02d:%02d', $sek / 3600, $sek / 60 % 60, $sek % 60); } } /** * Format a currency or an array of currencies accordingly. * * @param integer|array one value or an array of decimal numbers * @return integer|array formatted string(s) * @author sl */ function formatCurrency($number,$htmlNoWrap = true) { global $kga; if (is_array($number)) { // Convert all values of the array. $arr = array(); foreach ($number as $key=>$value) { $arr[$key] = formatCurrency($value); } return $arr; } else { $value = str_replace(".", $kga['conf']['decimalSeparator'], sprintf("%01.2f",$number) ); if ($kga['conf']['currency_first']) $value = $kga['currency_sign']." ".$value; else $value = $value." ".$kga['currency_sign']; if ($htmlNoWrap) return "$value"; else return $value; } } /** * Format the annotations and only include data which the user wants to see. * The array which is passed to the method will be modified. * * @param $ann array the annotation array (userid => (time, costs) ) */ function formatAnnotations(&$ann) { $type = usr_get_preference('ui.sublistAnnotations'); $userIds = array_keys($ann); if ($type == null) $type = 0; switch ($type) { case 0: // just time foreach ($userIds as $userId) { $ann[$userId] = formatDuration($ann[$userId]['time']); } break; case 1: // just costs foreach ($userIds as $userId) { $ann[$userId] = formatCurrency($ann[$userId]['costs']); } break; case 2: default: // both foreach ($userIds as $userId) { $time = formatDuration($ann[$userId]['time']); $costs = formatCurrency($ann[$userId]['costs']); $ann[$userId] = "$time | $costs"; } } } /** * returns hours, minutes and seconds as array * input: number of seconds * * @param integer $sek seconds to extract the time from * @return array * @author th */ function hourminsec($sek) { $i['h'] = $sek / 3600 % 24; $i['i'] = $sek / 60 % 60; $i['s'] = $sek % 60; return $i; } /** * returns array of subdirectorys - needed for skin selector * * @param string $dir Directory to list subdirectorys from * @return array * @author unknown */ function ls($dir){ $arr_subfolders = array(); $i=0; $handle = opendir($dir); while (false !== ($readdir = readdir($handle))) { if ($readdir != '.' && $readdir != '..' && substr($readdir,0,1) != '.' && is_dir("${dir}/${readdir}") ) { $arr_subfolders[$i] = $readdir; $i++; } } return isset($arr_subfolders)?$arr_subfolders:false; closedir($handle); } /** * returns array of language files * * @param none * @return array * @author unknown/th */ function langs(){ $arr_files = array(); $arr_files[] = ""; $handle = opendir(WEBROOT.'/language/'); while (false !== ($readdir = readdir($handle))) { if ($readdir != '.' && $readdir != '..' && substr($readdir,0,1) != '.' && endsWith($readdir,'.php') ) { $arr_files[] = str_replace(".php", "", $readdir); } } closedir($handle); sort($arr_files); return $arr_files; } /** * Get a list of available time zones. This is directly taken from PHP. * * @return array of timezone names */ function timezoneList() { return DateTimeZone::listIdentifiers(); } /** * Returns array for smarty's html_options funtion. * *
* returns: * [0] -> pct/evt names * [1] -> values as IDs ** * @param string either 'pct', 'evt', 'knd', 'grp' * @return array * @author th, sl, kp */ function makeSelectBox($subject,$user,$selection=null){ global $kga; $sel = array(); $sel[0] = array(); $sel[1] = array(); switch ($subject) { case 'pct': $arr_pct = get_arr_pct($user); $i=0; foreach ($arr_pct as $pct) { if ($pct['pct_visible']) { if ($kga['conf']['flip_pct_display']) { $sel[0][$i] = $pct['knd_name'] . ": " . $pct['pct_name']; if ($kga['conf']['pct_comment_flag']) { $sel[0][$i] .= "(" . $pct['pct_comment'] .")" ; } } else { $sel[0][$i] = $pct['pct_name'] . " (" . $pct['knd_name'] . ")"; if ($kga['conf']['pct_comment_flag']) { $sel[0][$i] .= "(" . $pct['pct_comment'] .")"; } } $sel[1][$i] = $pct['pct_ID']; $i++; } } break; case 'evt': $arr_evt = get_arr_evt($user); $i=0; foreach ($arr_evt as $evt) { if ($evt['evt_visible']) { $sel[0][$i] = $evt['evt_name']; $sel[1][$i] = $evt['evt_ID']; $i++; } } break; case 'tpl': $arr_tpl = get_arr_tpl($user); $i=0; foreach ($arr_tpl as $tpl) { $sel[0][$i] = $tpl['tpl_name']; $sel[1][$i] = $tpl['tpl_ID']; $i++; } break; case 'knd': $arr_knd = get_arr_knd($user); $i=0; $selectionFound = false; foreach ($arr_knd as $knd) { if ($knd['knd_visible']) { $sel[0][$i] = $knd['knd_name']; $sel[1][$i] = $knd['knd_ID']; $i++; if ($selection == $knd['knd_ID']) $selectionFound = true; } } if ($selection != null && !$selectionFound) { $data = knd_get_data($selection); $sel[0][$i] = $data['knd_name']; $sel[1][$i] = $data['knd_ID']; } break; case 'grp': $arr_grp = get_arr_grp(); $i=0; foreach ($arr_grp as $grp) { if (!$grp['grp_trash']) { $sel[0][$i] = $grp['grp_name']; $sel[1][$i] = $grp['grp_ID']; $i++; } } break; default: // TODO leave default options empty ??? break; } return $sel; } /** * returns list of projects and their time summary within zef_entry timespace as array * * OLD VERSION THAT MERGES TWO QUERYS - bad bad stuff ... * TODO: [tom] revise with join query! * * @param integer $group ID of group in table grp * @param integer $user ID of user in table usr * @param integer $in start time in unix seconds * @param integer $out end time in unix seconds * @global array $kga kimai-global-array * @return array * @author th */ function get_arr_pct_with_time($group,$user,$in,$out) { global $kga; //TODO: [tom] Functions results with 1 query $arr_pcts = get_arr_pct($group); $arr_time = get_arr_time_pct($user,$in,$out); //TODO END $arr = array(); $i=0; foreach ($arr_pcts as $pct) { $arr[$i]['pct_ID'] = $pct['pct_ID']; $arr[$i]['knd_ID'] = $pct['knd_ID']; $arr[$i]['pct_name'] = $pct['pct_name']; $arr[$i]['pct_comment'] = $pct['pct_comment']; $arr[$i]['knd_name'] = $pct['knd_name']; $arr[$i]['pct_visible'] = $pct['pct_visible']; $arr[$i]['zeit'] = @formatDuration($arr_time[$pct['pct_ID']]); $i++; } return $arr; } /** * returns a random code with given length * * @global integer $length length of the code * @return array * @author th */ function random_code($length) { $code = ""; $string="ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz0123456789"; mt_srand((double)microtime()*1000000); for ($i=1; $i <= $length; $i++) { $code .= substr($string, mt_rand(0,strlen($string)-1), 1); } return $code; } /** * returns a random number with X digits * * @global integer $length digit count of number * @return array * @author th */ function random_number($length) { $number = ""; $string="0123456789"; mt_srand((double)microtime()*1000000); for ($i=1; $i <= $length; $i++) { $number .= substr($string, mt_rand(0,strlen($string)-1), 1); } return $number; } /** * checks if the database structure needs to be updated for new Kimai version. * if yes the function redirects to /admin/updater.php * * @param string $path path to admin dir relative to the document that calls this function (usually "." or "..") * @global array $kga kimai-global-array * @return array * @author th */ function checkDBversion($path) { global $kga; // check for versions before 0.7.13r96 $installedVersion = get_DBversion(); $checkVersion = $installedVersion[0]; $checkVersion = "$checkVersion"; if ($checkVersion != $kga['version']) { header("Location: $path/updater.php"); exit; } // the check for revision is much simpler ... if ( (int)$installedVersion[1] < (int)$kga['revision']) { header("Location: $path/updater.php"); exit; } } /** * returns browser name * * returns: "Opera", "msie", "Safari", "Mozilla" or "?" * * TODO: [togi] check if this still is really needed anywhere * Smarty has a browser value by itself!!! * * * @return string * @author th */ function get_agent() { @$agent=$_SERVER["HTTP_USER_AGENT"]; if(strpos($agent,"opera") !== false) $browser = "Opera"; else if(strpos($agent,"msie") !== false) $browser = "msie"; else if(strpos($agent,"Safari") !== false) $browser = "Safari"; else if(strpos($agent,"mozilla") !== false) $browser = "Mozilla"; else $browser = "?"; return $browser; } /** * writes errors during install or update to the logfile stored in temporary * * @param string $value message * @param string $path relative path to temporary directory * @param boolean $success * @author th */ function logfile($value) { $value = str_replace("\n", "", $value); $value = str_replace(" ", " ", $value); $value = str_replace(" ", " ", $value); $logdatei=fopen(WEBROOT."temporary/logfile.txt","a"); fputs($logdatei, date("[d.m.Y H:i:s] ",time()) . $value ."\n"); fclose($logdatei); } /** * preprocess shortcut for date entries * * allowed shortcut formats are shown in the dialogue for edit timesheet entries (click the "?") * * @param string $date shortcut date * @return string * @author th */ function expand_date_shortcut($date) { $date = str_replace(" ","",$date); // empty string can't be a time value if (strlen($date)==0) return false; // get the parts $parts = preg_split("/\./",$date); if (count($parts) == 0 || count($parts) > 3) return false; // check day if (strlen($parts[0]) == 1) $parts[0] = "0".$parts[0]; // check month if (!isset($parts[1])) $parts[1] = date("m"); else if (strlen($parts[1]) == 1) $parts[1] = "0".$parts[1]; // check year if (!isset($parts[2])) $parts[2] = date("Y"); else if (strlen($parts[2]) == 2) { if ($parts[2] > 70) $parts[2] = "19".$parts[2]; else { if ($parts[2] < 10) $parts[2] = "200".$parts[2]; else $parts[2] = "20".$parts[2]; } } $return = implode(".",$parts); if (!preg_match("/([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2,4})/",$return)) $return = false; return $return; } /** * preprocess shortcut for time entries * * allowed shortcut formats are shown in the dialogue for edit timesheet entries (click the "?") * * @param string $date shortcut time * @return string * @author th */ function expand_time_shortcut($time) { $time = str_replace(" ","",$time); // empty string can't be a time value if (strlen($time)==0) return false; // get the parts $parts = preg_split("/:|\./",$time); for ($i=0;$i
* returns: * [0] -> in * [1] -> out ** * @param string $user ID of user * @global array $kga kimai-global-array * @return array * @author th */ // checked function get_timespace() { global $kga, $conn; $timespace = array(null,null); if (isset($kga['usr'])) { $timespace[0] = $kga['usr']['timespace_in']; $timespace[1] = $kga['usr']['timespace_out']; } /* database has no entries? */ $mon = date("n"); $day = date("j"); $Y = date("Y"); if (!$timespace[0]) { $timespace[0] = mktime(0,0,0,$mon,1,$Y); } if (!$timespace[1]) { $timespace[1] = mktime(23,59,59,$mon,$day,$Y); } return $timespace; } function endsWith($haystack,$needle) { return strcmp(substr($haystack, strlen($haystack)-strlen($needle)),$needle)===0; } ?>