lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/ ; if($from->format('Y') === $to->format('Y')){ if($from->format('Ym') === $to->format('Ym')){ if($from->format('Ymd') === $to->format('Ymd')){ $period = format_datetime($from, 'd MMM yyyy'); }else{ $period = sprintf('%s - %s', format_datetime($from, 'd'), format_datetime($to, 'd MMM yyyy')); } }else{ $period = sprintf('%s - %s', format_datetime($from, 'd MMM'), format_datetime($to, 'd MMM yyyy')); } }else{ $period = sprintf('%s - %s', format_datetime($from, 'd MMM yyyy'), format_datetime($to, 'd MMM yyyy')); } return $period; } /* -- -----------------*/ function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => _('år'), 'm' => _('månader'), 'w' => _('veckor'), 'd' => _('dagar'), 'h' => _('timmar'), 'i' => _('minuter'), 's' => _('sekunder'), ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v/* . ($diff->$k > 1 ? 's' : '')*/; } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 2); return $string ? implode(' och ', $string) . _(' sedan') : _('just nu'); } /* -- -----------------*/ function CreateDistanceTable(){ global $dbh; try { $TempName = "_competition_distance_" . rand(); $query = " CREATE TEMPORARY TABLE `{$TempName}` ( `DistId` smallint(3) NOT NULL, `DistName` varchar(75) COLLATE utf8_unicode_ci NOT NULL, `DistPool` tinyint(2) NOT NULL, `Distance` smallint(6) NOT NULL DEFAULT '0', `Relay` tinyint(1) NOT NULL DEFAULT '0', `Stroke` tinyint(1) NOT NULL DEFAULT '0', UNIQUE KEY `DistId` (`DistId`), KEY `DistPool` (`DistPool`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "; $dbh->query($query); $query = " INSERT INTO `{$TempName}` (`DistId`, `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke`) (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke` FROM `competition_distance` WHERE `DistRemoved` = 1) UNION (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, 4 AS `DistPool`, 0 `Distance`, 0 `Relay`, 0 `Stroke` FROM `competition_distance_custom` WHERE `DistRemoved` = 1) ORDER BY `DistId` "; $pst = $dbh->query($query); if($pst->rowCount() === 0) return false; return $TempName; } catch(PDOException $e){ echo $e->getMessage(); exit; } catch(Exception $e){ echo $e->getMessage(); exit; } return false; } /* -- -----------------*/ function UserResults($UserId){ global $dbh; $TempName = sprintf("_result%d", rand(1111,9999)); $query = " CREATE TEMPORARY TABLE `{$TempName}` SELECT `ResDate` `Date`, MIN(`ResTime`) `Time`, `DistId`, `DistName` `Name`, `DistPool` `Pool`, CONCAT(`Distance`, `Relay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filename.".{$Ext}"); $zip->close(); //Make sure the zip file created and output it. if(is_file($zipPath)){ if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header('Content-Description: File Transfer'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename="'.$Filename.'.zip"'); header('Content-Length: ' . filesize($zipPath)); readfile($zipPath); @unlink($Path.$Filename.".{$Ext}"); @unlink($Path.$Filename.".zip"); exit; } } $zip = null; } /* restrict to zip -- -----------------*/ function ZipRestriction(){ return file_exists(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH)) && is_file(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH)); } /* is valid zip? -- -----------------*/ function IsValidZip($Zipcode = ""){ if(false === $XML = simplexml_load_file(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH))) return true; foreach($XML->zipcodes->zipcode as $Zip){ if((int)$Zip === (int)$Zipcode) return true; } return false; } /* LocalAPI request -- -----------------*/ function cURLLocalAPI($Url, $Data = array(), $AsArray = false){ $Ch = curl_init(); curl_setopt($Ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($Ch, CURLOPT_USERPWD, sprintf("%s:%s", KO_LOCAL_API_USER, KO_LOCAL_API_PASS)); //Your credentials goes here curl_setopt($Ch, CURLOPT_USERAGENT, "KanslietAPIClient"); if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) curl_setopt($Ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); /* post -- ------*/ // $Data = array_merge($Data, array("Checksum" => md5(KANSLIETONLINE_CUSTOMER_NO.KO_LOCAL_API_PASS))); $Data['Checksum'] = md5(KANSLIETONLINE_CUSTOMER_NO.KO_LOCAL_API_PASS); // var_dump($Data); curl_setopt($Ch, CURLOPT_POST, 1); curl_setopt($Ch, CURLOPT_POSTFIELDS, http_build_query($Data)); /* get -- ------*/ // $Url = sprintf("%s?%s", $Url, http_build_query($Data)); curl_setopt($Ch, CURLOPT_URL, sprintf("%s%s%s", ABSURL, ADMPATH, $Url)); curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($Ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($Ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt ($Ch, CURLOPT_HTTPHEADER, array( // 'content-type: application/json', sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', KO_LOCAL_API_USER,KO_LOCAL_API_PASS))) )); $Res = curl_exec($Ch); $Info = curl_getinfo($Ch); // return $Res; /* Log file -- ------------*/ if(DEBUG === true){ $__Log = fopen(sprintf("%s%slogs/cURL.log", ROOTPATH, ADMPATH), "a"); fwrite($__Log, sprintf("%s\t%s\t%s\t%s\n", date('Y-m-d H:i:s'), $Info['http_code'], $Url, $Res)); @fopen($__Log); } if($Info['http_code'] !== 200 || curl_error($Ch)){ error_log(sprintf("cURLLocalAPI error %s: %s (http %d) Message: %s", curl_errno($Ch), curl_error($Ch), $Info['http_code'], $Res)); return false; } curl_close ($Ch); return json_decode($Res, $AsArray); } /** * Returns a GUIDv4 string * * Uses the best cryptographically secure method * for all supported pltforms with fallback to an older, * less secure version. * * @param bool $trim * @return string */ function GUIDv4($trim = true) { // Windows if (function_exists('com_create_guid') === true) { if ($trim === true) return trim(com_create_guid(), '{}'); else return com_create_guid(); } // OSX/Linux if (function_exists('openssl_random_pseudo_bytes') === true) { $data = openssl_random_pseudo_bytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } // Fallback (PHP 4.2+) mt_srand((double)microtime() * 10000); $charid = strtolower(md5(uniqid(rand(), true))); $hyphen = chr(45); // "-" $lbrace = $trim ? "" : chr(123); // "{" $rbrace = $trim ? "" : chr(125); // "}" $guidv4 = $lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/ lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/ lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/ lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/ iption: File Transfer'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename="'.$Filename.'.zip"'); header('Content-Length: ' . filesize($zipPath)); readfile($zipPath); @unlink($Path.$Filename.".{$Ext}"); @unlink($Path.$Filename.".zip"); exit; } } $zip = null; } /* restrict to zip -- -----------------*/ function ZipRestriction(){ return file_exists(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH)) && is_file(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH)); } /* is valid zip? -- -----------------*/ function IsValidZip($Zipcode = ""){ if(false === $XML = simplexml_load_file(sprintf("%s%szipcodes.xml", ROOTPATH, SKINPATH))) return true; foreach($XML->zipcodes->zipcode as $Zip){ if((int)$Zip === (int)$Zipcode) return true; } return false; } /* LocalAPI request -- -----------------*/ function cURLLocalAPI($Url, $Data = array(), $AsArray = false){ $Ch = curl_init(); curl_setopt($Ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($Ch, CURLOPT_USERPWD, sprintf("%s:%s", KO_LOCAL_API_USER, KO_LOCAL_API_PASS)); //Your credentials goes here curl_setopt($Ch, CURLOPT_USERAGENT, "KanslietAPIClient"); if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) curl_setopt($Ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); /* post -- ------*/ // $Data = array_merge($Data, array("Checksum" => md5(KANSLIETONLINE_CUSTOMER_NO.KO_LOCAL_API_PASS))); $Data['Checksum'] = md5(KANSLIETONLINE_CUSTOMER_NO.KO_LOCAL_API_PASS); // var_dump($Data); curl_setopt($Ch, CURLOPT_POST, 1); curl_setopt($Ch, CURLOPT_POSTFIELDS, http_build_query($Data)); /* get -- ------*/ // $Url = sprintf("%s?%s", $Url, http_build_query($Data)); curl_setopt($Ch, CURLOPT_URL, sprintf("%s%s%s", ABSURL, ADMPATH, $Url)); curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($Ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($Ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt ($Ch, CURLOPT_HTTPHEADER, array( // 'content-type: application/json', sprintf('Authorization: Basic %s', base64_encode(sprintf('%s:%s', KO_LOCAL_API_USER,KO_LOCAL_API_PASS))) )); $Res = curl_exec($Ch); $Info = curl_getinfo($Ch); // return $Res; /* Log file -- ------------*/ if(DEBUG === true){ $__Log = fopen(sprintf("%s%slogs/cURL.log", ROOTPATH, ADMPATH), "a"); fwrite($__Log, sprintf("%s\t%s\t%s\t%s\n", date('Y-m-d H:i:s'), $Info['http_code'], $Url, $Res)); @fopen($__Log); } if($Info['http_code'] !== 200 || curl_error($Ch)){ error_log(sprintf("cURLLocalAPI error %s: %s (http %d) Message: %s", curl_errno($Ch), curl_error($Ch), $Info['http_code'], $Res)); return false; } curl_close ($Ch); return json_decode($Res, $AsArray); } /** * Returns a GUIDv4 string * * Uses the best cryptographically secure method * for all supported pltforms with fallback to an older, * less secure version. * * @param bool $trim * @return string */ function GUIDv4($trim = true) { // Windows if (function_exists('com_create_guid') === true) { if ($trim === true) return trim(com_create_guid(), '{}'); else return com_create_guid(); } // OSX/Linux if (function_exists('openssl_random_pseudo_bytes') === true) { $data = openssl_random_pseudo_bytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } // Fallback (PHP 4.2+) mt_srand((double)microtime() * 10000); $charid = strtolower(md5(uniqid(rand(), true))); $hyphen = chr(45); // "-" $lbrace = $trim ? "" : chr(123); // "{" $rbrace = $trim ? "" : chr(125); // "}" $guidv4 = $lbrace. substr($charid, 0, 8).$hyphen. substr($charid, 8, 4).$hyphen. substr($charid, 12, 4).$hyphen. substr($charid, 16, 4).$hyphen. substr($charid, 20, 12). $rbrace; return $guidv4; } /* -- -----------------*/ /* -- -----------------*/