Minor fixes
This commit is contained in:
130
inc/toolbox.php
130
inc/toolbox.php
@@ -9,7 +9,7 @@ class ToolBox
|
||||
{
|
||||
const FILE_EOL = "\n";
|
||||
const MAIL_SUCCESS = 2;
|
||||
|
||||
|
||||
public static function cleanPost(&$asData)
|
||||
{
|
||||
//get rid of magic quotes
|
||||
@@ -18,7 +18,7 @@ class ToolBox
|
||||
$asData = self::cleanData($asData, 'stripslashes');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function cleanData($oData, $sCleaningFunc)
|
||||
{
|
||||
if(!is_array($oData))
|
||||
@@ -40,7 +40,7 @@ class ToolBox
|
||||
if(defined('STDIN')) mb_parse_str(implode('&', array_slice($argv, 1)), $_GET);
|
||||
$_REQUEST = array_merge($_GET, $_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
public static function array_map_encapsulate($oData, $sChar)
|
||||
{
|
||||
if(is_array($oData))
|
||||
@@ -80,9 +80,9 @@ class ToolBox
|
||||
|
||||
return $acText;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param String $sFromName
|
||||
* @param String $sSubject
|
||||
* @param String $sMessage
|
||||
@@ -101,7 +101,7 @@ class ToolBox
|
||||
'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);
|
||||
@@ -110,40 +110,40 @@ class ToolBox
|
||||
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 curl($sUrl, $bHeader=false, $asPostData=array(), $sReturnType='text', $sCookie='', $sCreds='') {
|
||||
$oCurl = curl_init();
|
||||
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
|
||||
curl_setopt($oCurl, CURLOPT_VERBOSE, false);
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
|
||||
|
||||
curl_setopt($oCurl, CURLOPT_HEADER, $bHeader);
|
||||
if($bHeader) curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
|
||||
|
||||
|
||||
if(!empty($asPostData)) {
|
||||
curl_setopt($oCurl, CURLOPT_POST, 1);
|
||||
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $asPostData);
|
||||
}
|
||||
|
||||
|
||||
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
|
||||
|
||||
|
||||
if($sCookie!='') curl_setopt($oCurl, CURLOPT_COOKIE, $sCookie);
|
||||
if($sCreds!='') curl_setopt($oCurl, CURLOPT_USERPWD, $sCreds);
|
||||
|
||||
@@ -155,15 +155,15 @@ class ToolBox
|
||||
$sDesc = '';
|
||||
if(!$bSuccess) $sDesc = ($sCurlErrorId==0)?('HTTP Error Code '.$sHttpCode):($sCurlErrorId.': '.curl_strerror($sCurlErrorId));
|
||||
curl_close($oCurl);
|
||||
|
||||
|
||||
switch($sReturnType) {
|
||||
case 'json': $oContent = json_decode($sContent, true); break;
|
||||
default: $oContent = $sContent;
|
||||
}
|
||||
|
||||
|
||||
return array('result'=>$bSuccess, 'desc'=>$sDesc, 'content'=>$oContent);
|
||||
}
|
||||
|
||||
|
||||
public static function getMimeType($sPath, $bSubTypeOnly=false)
|
||||
{
|
||||
$sMimetype = '';
|
||||
@@ -187,7 +187,7 @@ class ToolBox
|
||||
$sMimetype = curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
|
||||
curl_close($oCurl);
|
||||
}
|
||||
|
||||
|
||||
//Only sub type (after /)
|
||||
if($bSubTypeOnly)
|
||||
{
|
||||
@@ -196,38 +196,38 @@ class ToolBox
|
||||
}
|
||||
return $sMimetype;
|
||||
}
|
||||
|
||||
|
||||
public static function isAnimatedGif($sFilePath)
|
||||
{
|
||||
if(!($oFile = @fopen($sFilePath, 'rb'))) return false;
|
||||
|
||||
|
||||
$iCount = 0;
|
||||
while (!feof($oFile) && $iCount < 2)
|
||||
{
|
||||
|
||||
|
||||
$sChunk = fread($oFile, 1024 * 100); //read 100kb at a time
|
||||
$iCount += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $sChunk, $asMatches);
|
||||
}
|
||||
fclose($oFile);
|
||||
|
||||
|
||||
return $iCount > 1;
|
||||
}
|
||||
|
||||
|
||||
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight=0, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false, $bCopyExif=false)
|
||||
{
|
||||
$asResult = array('error'=>'');
|
||||
|
||||
|
||||
//Look up the file type to choose the image creator
|
||||
$sImageExt = self::getMimeType($sInPath, true);
|
||||
$sImageExt = ($sImageExt=='jpg')?'jpeg':$sImageExt;
|
||||
|
||||
|
||||
//New Destination folder
|
||||
$asInInfo = pathinfo($sInPath);
|
||||
$asOutInfo = pathinfo($sOutPath);
|
||||
if($sOutPath=='') $sOutPath = $sInPath;
|
||||
elseif(mb_substr($sOutPath, -1)=='/') $sOutPath .= mb_strtolower($asInInfo['basename']); //folder only, keep original name
|
||||
elseif(!array_key_exists('extension', $asOutInfo)) $sOutPath .= '.'.$sImageExt; //folder + filename, but getting ext from file info
|
||||
|
||||
|
||||
//New sizes
|
||||
if(!in_array($sImageExt, $asImageExts) && !empty($asImageExts)) $asResult['error'] = 'Wrong file type: '.$sImageExt;
|
||||
elseif($iMaxWidth==0 && $iMaxHeight==0) $asResult['error'] = 'At least one dimension must be resized (width and/or height)';
|
||||
@@ -244,18 +244,18 @@ class ToolBox
|
||||
case 8: $sRotate = 90; break; //Trigo
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
list($iWidth, $iHeight) = getimagesize($sInPath);
|
||||
if(abs($sRotate) == 90) {
|
||||
$iTempWidth = $iWidth;
|
||||
$iWidth = $iHeight;
|
||||
$iHeight = $iTempWidth;
|
||||
}
|
||||
|
||||
|
||||
//Limit on only 1 parameter
|
||||
if($iMaxWidth==0) $iMaxWidth = $iWidth * $iMaxHeight / $iHeight;
|
||||
elseif($iMaxHeight==0) $iMaxHeight = $iHeight * $iMaxWidth / $iWidth;
|
||||
|
||||
|
||||
if($iWidth > $iMaxWidth || $iHeight > $iMaxHeight)
|
||||
{
|
||||
$dResizeDeltaWidth = $iWidth - $iMaxWidth;
|
||||
@@ -263,7 +263,7 @@ class ToolBox
|
||||
$iPosLeft = $iPosTop = 0;
|
||||
$iThumbWidth = $iMaxWidth;
|
||||
$iThumbHeight = $iMaxHeight;
|
||||
|
||||
|
||||
//Max up the lowest value between height and width and crop the other
|
||||
if($bCrop)
|
||||
{
|
||||
@@ -295,13 +295,13 @@ class ToolBox
|
||||
$iThumbWidth = $iResizedWidth;
|
||||
$iThumbHeight = $iResizedHeight;
|
||||
}
|
||||
|
||||
|
||||
if($sImageExt=='gif' && self::isAnimatedGif($sInPath))
|
||||
{
|
||||
$sContent = file_get_contents($sInPath);
|
||||
$sBigGifPath = uniqid().$sOutPath.'.big';
|
||||
$sCoalescePath = uniqid().$sOutPath.'.coalesce';
|
||||
|
||||
|
||||
if(file_put_contents($sBigGifPath, $sContent)===false) $asResult['error'] = 'Unable to create temporary thumbnail : '.$sBigGifPath;
|
||||
system('convert '.$sBigGifPath.' -coalesce '.$sCoalescePath);
|
||||
system('convert -size '.$iWidth.'x'.$iHeight.' '.$sCoalescePath.' -resize '.$iResizedWidth.'x'.$iResizedHeight.' '.$sOutPath);
|
||||
@@ -312,14 +312,14 @@ class ToolBox
|
||||
{
|
||||
//create image from source
|
||||
$oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath);
|
||||
|
||||
|
||||
//Fix rotation
|
||||
if($sRotate) $oSource = imagerotate($oSource, $sRotate, 0);
|
||||
|
||||
if($sRotate) $oSource = imagerotate($oSource, $sRotate, 0);
|
||||
|
||||
//Resize
|
||||
$oThumb = imagecreatetruecolor($iThumbWidth, $iThumbHeight);
|
||||
imagecopyresampled($oThumb, $oSource, $iPosLeft, $iPosTop, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight);
|
||||
|
||||
|
||||
//Save
|
||||
if(file_exists($sOutPath)) unlink($sOutPath);
|
||||
if(!call_user_func_array('image'.$sImageExt, array($oThumb, $sOutPath)))
|
||||
@@ -342,17 +342,17 @@ class ToolBox
|
||||
|
||||
if($bCopyExif && $asResult['error'] == '') self::copyExif($sInPath, $sOutPath);
|
||||
if($bDeleteIn && $asResult['error'] == '' && $sInPath != $sOutPath) unlink($sInPath);
|
||||
|
||||
|
||||
return $asResult;
|
||||
}
|
||||
|
||||
|
||||
public function copyExif($srcfile, $destfile) {
|
||||
// Function transfers EXIF (APP1) and IPTC (APP13) from $srcfile and adds it to $destfile
|
||||
// JPEG file has format 0xFFD8 + [APP0] + [APP1] + ... [APP15] + <image data> where [APPi] are optional
|
||||
// Segment APPi (where i=0x0 to 0xF) has format 0xFFEi + 0xMM + 0xLL + <data> (where 0xMM is
|
||||
// most significant 8 bits of (strlen(<data>) + 2) and 0xLL is the least significant 8 bits
|
||||
// of (strlen(<data>) + 2)
|
||||
|
||||
|
||||
if (file_exists($srcfile) && file_exists($destfile)) {
|
||||
$srcsize = @getimagesize($srcfile, $imageinfo);
|
||||
// Prepare EXIF data bytes from source file
|
||||
@@ -378,7 +378,7 @@ class ToolBox
|
||||
$portiontoadd = chr(0xFF) . chr(0xD8); // Variable accumulates new & original IPTC application segments
|
||||
$exifadded = !$exifdata;
|
||||
$iptcadded = !$iptcdata;
|
||||
|
||||
|
||||
while ((substr($destfilecontent, 0, 2) & 0xFFF0) === 0xFFE0) {
|
||||
$segmentlen = (substr($destfilecontent, 2, 2) & 0xFFFF);
|
||||
$iptcsegmentnumber = (substr($destfilecontent, 1, 1) & 0x0F); // Last 4 bits of second byte is IPTC segment #
|
||||
@@ -408,18 +408,18 @@ class ToolBox
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
@@ -427,54 +427,54 @@ class ToolBox
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
public static function fixEOL($sText)
|
||||
{
|
||||
//Normalize line endings
|
||||
//Convert all line-endings to UNIX format
|
||||
$sText = str_replace("\r\n", "\n", $sText); //Windows
|
||||
$sText = str_replace("\r", "\n", $sText); //Mac
|
||||
|
||||
|
||||
// Don't allow out-of-control blank lines
|
||||
$sText = preg_replace("/\n{2,}/", "\n\n", $sText);
|
||||
return $sText;
|
||||
}
|
||||
|
||||
|
||||
public static function getUserLanguage($available_languages, $sDefaultLang='') {
|
||||
$http_accept_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])?$_SERVER['HTTP_ACCEPT_LANGUAGE']:'';
|
||||
|
||||
|
||||
//Format: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
||||
preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i", $http_accept_language, $hits, PREG_SET_ORDER);
|
||||
|
||||
|
||||
$bestlang = $sDefaultLang;
|
||||
$bestqval = 0;
|
||||
|
||||
|
||||
foreach($hits as $arr) {
|
||||
$langprefix = strtolower($arr[1]);
|
||||
if(!empty($arr[3])) {
|
||||
@@ -482,11 +482,11 @@ class ToolBox
|
||||
$language = $langprefix.'-'.$langrange;
|
||||
}
|
||||
else $language = $langprefix;
|
||||
|
||||
|
||||
//Q Value
|
||||
$qvalue = 1.0;
|
||||
if(!empty($arr[5])) $qvalue = floatval($arr[5]);
|
||||
|
||||
|
||||
//find q-maximal language
|
||||
if(in_array($language, $available_languages) && $qvalue > $bestqval) {
|
||||
$bestlang = $language;
|
||||
@@ -500,7 +500,7 @@ class ToolBox
|
||||
}
|
||||
return $bestlang;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return relative time description
|
||||
* FIXME shitty implementation of i18n
|
||||
@@ -512,7 +512,7 @@ class ToolBox
|
||||
{
|
||||
$iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime);
|
||||
$sCurTimeStamp = time();
|
||||
|
||||
|
||||
switch ($sLang) {
|
||||
case 'en':
|
||||
$asWeekDays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'satursday', 'sunday');
|
||||
@@ -523,12 +523,12 @@ class ToolBox
|
||||
$asMonths = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$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 = '';
|
||||
switch ($sLang) {
|
||||
case 'en':
|
||||
@@ -538,7 +538,7 @@ class ToolBox
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*20) $sDesc = '15 minutes ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*50) $sDesc = 'half an hour ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*2) $sDesc = 'an hour ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'at '.$sHour.' o\'clock';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'at '.date('gA', $iTimeStamp);
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24) $sDesc = 'yesterday';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7 && $sWeek==$sCurWeek) $sDesc = $asWeekDays[$sWeekDay-1];
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7) $sDesc = 'last '.$asWeekDays[$sWeekDay-1];
|
||||
@@ -576,7 +576,7 @@ class ToolBox
|
||||
else $sDesc = 'en '.$sYear;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//return self::mb_ucfirst($sDesc);
|
||||
return $sDesc;
|
||||
}
|
||||
@@ -590,7 +590,7 @@ class ToolBox
|
||||
public static function remove_accents($string)
|
||||
{
|
||||
if(!preg_match('/[\x80-\xff]/', $string)) return $string;
|
||||
|
||||
|
||||
if (true || seems_utf8($string)) {
|
||||
$chars = array(
|
||||
// Decompositions for Latin-1 Supplement
|
||||
@@ -817,4 +817,4 @@ class ToolBox
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user