init
This commit is contained in:
235
inc/toolbox.php
Executable file
235
inc/toolbox.php
Executable file
@@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ToolBox - Only static functions missing from php librairy
|
||||
* @author franzz
|
||||
* @version 1.1
|
||||
*/
|
||||
class ToolBox
|
||||
{
|
||||
const MAIL_SUCCESS = 2;
|
||||
|
||||
public static function cleanPost(&$asData)
|
||||
{
|
||||
//get rid of magic quotes
|
||||
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
|
||||
{
|
||||
$asData = self::cleanData($asData, 'stripslashes');
|
||||
}
|
||||
}
|
||||
|
||||
public static function cleanData($oData, $sCleaningFunc)
|
||||
{
|
||||
if(!is_array($oData))
|
||||
{
|
||||
return call_user_func($sCleaningFunc, $oData);
|
||||
}
|
||||
elseif(count($oData)>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);
|
||||
|
||||
//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 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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user