0) { $asCleaningFunc = array_fill(1, count($oData), $sCleaningFunc); $asKeys = array_map(array('self', 'cleanData'), array_keys($oData), $asCleaningFunc); $asValues = array_map(array('self', 'cleanData'), $oData, $asCleaningFunc); return array_combine($asKeys, $asValues); } } public static function fixGlobalVars($argv) { //Add CLI arguments if(defined('STDIN')) mb_parse_str(implode('&', array_slice($argv, 1)), $_GET); $_REQUEST = array_merge($_GET, $_REQUEST); //Add Server Name $sServerName = array_key_exists('SERVER_NAME', $_SERVER)?$_SERVER['SERVER_NAME']:$_SERVER['PWD']; $sAppPath = 'http://'.str_replace('http://', '', $sServerName.dirname($_SERVER['SCRIPT_NAME'])); $_GET['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':''); } public static function array_map_encapsulate($oData, $sChar) { if(is_array($oData)) { $asChar = array_fill(1, count($oData), $sChar); return array_combine(array_keys($oData), array_map(array('self', 'array_map_encapsulate'), $oData, $asChar)); } else { return $sChar.$oData.$sChar; } } public static function capitalizeWords($acText, $sCharList = '') { //Use ucwords if no delimiters are given if($sCharList=='') { return Toolbox::mb_ucwords($acText); } // Go through all characters $capitalizeNext = true; $max = mb_strlen($acText); for ($i = 0; $i < $max; $i++) { if(mb_strpos($sCharList, $acText[$i]) !== false) { $capitalizeNext = true; } elseif($capitalizeNext) { $capitalizeNext = false; $acText[$i] = mb_strtoupper($acText[$i]); } } return $acText; } /** * * @param String $sFromName * @param String $sSubject * @param String $sMessage * @param String $sTo * @param Array $asCc array(name => email) * @param Boolean $bSelfMail * @return mixed */ public function sendMail($sFromName, $sSubject, $sMessage, $sTo, $asCc=array(), $bSelfMail=true) { $asForm = array('api_key'=>Settings::MAIL_API_KEY, 'app'=>'Wedding', 'from_name'=>$sFromName, 'subject'=>$sSubject, 'msg'=>$sMessage, 'to_email'=>$sTo, 'cc_email'=>self::jsonConvert($asCc), 'self'=>$bSelfMail); $oCurl = curl_init(); curl_setopt($oCurl, CURLOPT_URL, Settings::MAIL_SCRIPT); curl_setopt($oCurl, CURLOPT_POST, true); curl_setopt($oCurl, CURLOPT_HEADER, false); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true); curl_setopt($oCurl, CURLOPT_POSTFIELDS, $asForm); $iResult = curl_exec($oCurl); curl_close($oCurl); return $iResult; } public static function jsonExport($asData) { header('Content-type: application/json'); //return htmlspecialchars(json_encode($asData), ENT_NOQUOTES); return self::jsonConvert($asData); } public static function jsonConvert($asData) { return json_encode($asData); } public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png')) { $asResult = array('error'=>''); //Look up the extension to choose the image creator //TODO use MIME types $sInInfo = pathinfo($sInPath); $sInName = mb_strtolower($sInInfo['basename']); $sImageExt = mb_strtolower($sInInfo['extension']); $sImageExt = ($sImageExt=='jpg')?'jpeg':$sImageExt; //New Destination folder if($sOutPath=='') $sOutPath = $sInPath; elseif(mb_substr($sOutPath, -1)=='/') $sOutPath .= $sInName; //New sizes if(in_array($sImageExt, $asImageExts)) { list($iWidth, $iHeight) = getimagesize($sInPath); if($iWidth > $iMaxWidth || $iHeight > $iMaxHeight) { $dResizeDeltaWidth = $iWidth - $iMaxWidth; $dResizeDeltaHeight = $iHeight - $iMaxHeight; if($dResizeDeltaWidth > $dResizeDeltaHeight) { $iResizedWidth = $iMaxWidth; $iResizedHeight = ($iResizedWidth / $iWidth) * $iHeight; } else { $iResizedHeight = $iMaxHeight; $iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth; } //create image from source $oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath); //Resize $oThumb = imagecreatetruecolor($iResizedWidth, $iResizedHeight); imagecopyresized($oThumb, $oSource, 0, 0, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight); //Save if(file_exists($sOutPath)) unlink($sOutPath); if(!call_user_func_array('image'.$sImageExt, array($oThumb, $sOutPath))) { $asResult['error'] = 'Unable to create thumbnail : '.$sOutPath; } } elseif($sInPath != $sOutPath) { $iResizedWidth = $iWidth; $iResizedHeight = $iHeight; if(!copy($sInPath, $sOutPath)) $asResult['error'] = 'Copy failed from '.$sInPath.' to '.$sOutPath; } $asResult['width'] = $iResizedWidth; $asResult['height'] = $iResizedHeight; $asResult['out'] = $sOutPath; } else $asResult['error'] = 'Wrong file type'; if($bDeleteIn && $asResult['error']=='' && $sInPath != $sOutPath) unlink($sInPath); return $asResult; } public static function utf8_compliant($sText) { if(strlen($sText) == 0) return true; return (preg_match('/^.{1}/us', $sText, $ar) == 1); } public static function mb_ucwords($sText) { return mb_convert_case($sText, MB_CASE_TITLE, "UTF-8"); } public static function mb_ucfirst($sText) { $sLength = mb_strlen($sText); $sFirstChar = mb_substr($sText, 0, 1); $sThen = mb_substr($sText, 1, $sLength - 1); return mb_strtoupper($sFirstChar).$sThen; } public static function file_get_contents_utf8($oFile) { $sContent = file_get_contents($oFile); return mb_convert_encoding($sContent, 'UTF-8', mb_detect_encoding($sContent, 'UTF-8, ISO-8859-1', true)); } public static function rgbToHex($R, $G, $B) { $R = dechex($R); if(strlen($R)<2) $R='0'.$R; $G = dechex($G); if(strlen($G)<2) $G='0'.$G; $B = dechex($B); if(strlen($B)<2) $B='0'.$B; return $R.$G.$B; } public static function setCookie($sCookieName, $sCookieValue, $iDays) { $iTimeLimit = time()+60*60*24*$iDays; setcookie($sCookieName, $sCookieValue, $iTimeLimit); } //TODO implement link pattern public static function findReplaceLinks($sText, $sLinkPattern='') { $sServerPath = substr(str_replace(array('http://', 'https://'), '', $_GET['serv_name']), 0, -1); //Phase 1 : Identify internal links : merge cases /#code-1 and code 1, destroy link. $asPatterns = array(); foreach(Databap::$HASH_TO_PAGE as $sAlt=>$sPage) { $asPatterns['`(^|\ )(https?://|)'.$sServerPath.'/?\#'.$sAlt.'\-([\d]+)`sui'] = '$1$4'.$sPage.' $3'; } $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); //Phase 2 : Identify remaining links (external) $asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`sui', '`((?$1 ', '$1'); $sText = preg_replace($asPatterns, $asLinks, $sText); //Phase 3: rebuild link $asPatterns = array(); foreach(Databap::$HASH_TO_PAGE as $sAlt=>$sPage) { if(mb_strlen($sAlt)>1) { $sTitle = Databap::$PAGE_TITLES[$sPage]; $asPatterns['`(^|\ )'.$sAlt.'\ ([\d]+)(\ |$)`sui'] = '$1'.$sTitle.' n°$2$3'; if($sPage=='table') $asPatterns['`(^|\ )'.$sAlt.'\ ([_A-Z0-9]+)(\ |$)`su'] = '$1'.$sTitle.' $2$3'; } } $sText = preg_replace(array_keys($asPatterns), array_values($asPatterns), $sText); //@file_put_contents('log.html', "\n\n".date('r')."\n".$test, FILE_APPEND); return $sText; } public static function getDateTimeDesc($oTime) { $iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime); $sCurTimeStamp = time(); $asWeekDays = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'); $asMonths = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'); $sSep = '|'; $sFormat = 'Y'.$sSep.'n'.$sSep.'W'.$sSep.'N'.$sSep.'j'.$sSep.'G'; list($sYear, $sMonth, $sWeek, $sWeekDay, $sDay, $sHour) = explode($sSep, date($sFormat, $iTimeStamp)); list($sCurYear, $sCurMonth, $sCurWeek, $sCurWeekDay, $sCurDay, $sCurHour) = explode($sSep, date($sFormat, $sCurTimeStamp)); $sDesc = ''; if($iTimeStamp>$sCurTimeStamp) $sDesc = 'dans le futur'; elseif($sCurTimeStamp-$iTimeStamp<60) $sDesc = 'il y a quelques secondes'; elseif($sCurTimeStamp-$iTimeStamp<60*10) $sDesc = 'il y a quelques minutes'; elseif($sCurTimeStamp-$iTimeStamp<60*20) $sDesc = 'il y a un quart d\'heure'; elseif($sCurTimeStamp-$iTimeStamp<60*50) $sDesc = 'il y a une demi-heure'; elseif($sCurTimeStamp-$iTimeStamp<60*60*2) $sDesc = 'il y a une heure'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'à '.$sHour.'h'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24) $sDesc = 'hier'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7 && $sWeek==$sCurWeek) $sDesc = $asWeekDays[$sWeekDay-1]; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7) $sDesc = $asWeekDays[$sWeekDay-1].' dernier'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*9) $sDesc = 'il y a une semaine'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*12) $sDesc = 'il y a 10 jours'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*16) $sDesc = 'il y a 2 semaines'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*23) $sDesc = 'il y a 3 semaines'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*31 && $sMonth==$sCurMonth) $sDesc = 'le '.$sDay.' '.$asMonths[$sMonth-1]; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*30*2 && $sMonth==($sCurMonth-1)) $sDesc = 'le mois dernier'; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365 && $sYear==$sCurYear) $sDesc = 'en '.$asMonths[$sMonth-1]; elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365) $sDesc = 'en '.$asMonths[$sMonth-1].' '.$sYear; elseif($sYear==($sCurYear-1)) $sDesc = 'l\'année dernière'; else $sDesc = 'en '.$sYear; //return self::mb_ucfirst($sDesc); return $sDesc; } } ?>