Minor fixes

This commit is contained in:
2021-05-24 21:47:32 +02:00
parent 3b3066c83b
commit cc58284478
3 changed files with 138 additions and 140 deletions

View File

@@ -12,7 +12,7 @@ class Db extends PhpObject
const DB_NO_DATA = 'ERR_2'; const DB_NO_DATA = 'ERR_2';
const DB_NO_TABLE = 'ERR_3'; const DB_NO_TABLE = 'ERR_3';
const ID_TAG = 'id_'; const ID_TAG = 'id_';
//Database formats //Database formats
const MONTH_FORMAT = 'Ym'; const MONTH_FORMAT = 'Ym';
const DATE_FORMAT = 'Y-m-d'; const DATE_FORMAT = 'Y-m-d';
@@ -20,7 +20,7 @@ class Db extends PhpObject
public $sDbState; public $sDbState;
private $bTrace; private $bTrace;
/** /**
* SQL connection Handle * SQL connection Handle
* @var mysqli * @var mysqli
@@ -28,7 +28,7 @@ class Db extends PhpObject
private $oConnection; private $oConnection;
private $sDatabase; private $sDatabase;
private $asOptions; private $asOptions;
/** /**
* Tables & fields descriptions * Tables & fields descriptions
* array( 'tables'=>array('table_name1'=>array('table_field1', 'table_field2', ...), 'table_name2'=>array(...)), * array( 'tables'=>array('table_name1'=>array('table_field1', 'table_field2', ...), 'table_name2'=>array(...)),
@@ -93,7 +93,7 @@ class Db extends PhpObject
$this->bTrace = $bTrace; $this->bTrace = $bTrace;
//if($bTrace) $this->setDebug(true); //if($bTrace) $this->setDebug(true);
} }
public function getTrace() public function getTrace()
{ {
return $this->bTrace; return $this->bTrace;
@@ -114,10 +114,10 @@ class Db extends PhpObject
//Create tables //Create tables
@array_walk($this->getInstallQueries(), array($this, 'setQuery')); @array_walk($this->getInstallQueries(), array($this, 'setQuery'));
} }
public function getBackup() { public function getBackup() {
$sBackupFile = uniqid('backup_').'.sql'; $sBackupFile = uniqid('backup_').'.sql';
$sAppPath = ''; $sAppPath = '';
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $sAppPath = 'C:\ProgramData\xampp\mysql\bin\\'; if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $sAppPath = 'C:\ProgramData\xampp\mysql\bin\\';
exec($sAppPath.'mysqldump --user='.Settings::DB_LOGIN.' --password='.Settings::DB_PASS.' '.Settings::DB_NAME.' --add-drop-table --result-file='.$sBackupFile); exec($sAppPath.'mysqldump --user='.Settings::DB_LOGIN.' --password='.Settings::DB_PASS.' '.Settings::DB_NAME.' --add-drop-table --result-file='.$sBackupFile);
@@ -128,7 +128,7 @@ class Db extends PhpObject
} }
else return false; else return false;
} }
public function restoreBackup($sBackupFile) { public function restoreBackup($sBackupFile) {
$sAppPath = ''; $sAppPath = '';
if(file_exists($sBackupFile)) { if(file_exists($sBackupFile)) {
@@ -137,11 +137,11 @@ class Db extends PhpObject
} }
else return false; else return false;
} }
public function loadFile($sFilePath) { public function loadFile($sFilePath) {
set_time_limit(0); set_time_limit(0);
$bResult = false; $bResult = false;
if(file_exists($sFilePath)) if(file_exists($sFilePath))
{ {
$sContent = file_get_contents($sFilePath); $sContent = file_get_contents($sFilePath);
@@ -162,7 +162,7 @@ class Db extends PhpObject
$bResult = false; $bResult = false;
break; break;
} }
$bResult = true; $bResult = true;
$sSql = ''; $sSql = '';
} }
@@ -172,7 +172,7 @@ class Db extends PhpObject
else $this->addError('File is empty: '.basename($sFilePath)); else $this->addError('File is empty: '.basename($sFilePath));
} }
else $this->addError('File not found: '.$sFilePath); else $this->addError('File not found: '.$sFilePath);
return $bResult; return $bResult;
} }
@@ -234,7 +234,7 @@ class Db extends PhpObject
} }
return $oResult; return $oResult;
} }
public function getLastError() public function getLastError()
{ {
return $this->oConnection->error; return $this->oConnection->error;
@@ -302,12 +302,12 @@ class Db extends PhpObject
$asTableIds = array_map(array('self', 'getId'), $asTables); $asTableIds = array_map(array('self', 'getId'), $asTables);
return in_array($sColumnName, $asTableIds); return in_array($sColumnName, $asTableIds);
} }
private function isField($sTableFieldName) private function isField($sTableFieldName)
{ {
$asPath = explode('.', str_replace('`', '', $sTableFieldName)); $asPath = explode('.', str_replace('`', '', $sTableFieldName));
return ( return (
is_array($asPath) is_array($asPath)
&& count($asPath)==2 && count($asPath)==2
&& $this->isColumnInTable($asPath[0], $asPath[1]) && $this->isColumnInTable($asPath[0], $asPath[1])
); );
@@ -328,17 +328,17 @@ class Db extends PhpObject
public function getTablecolumns($sTableName, $bTypes=true) public function getTablecolumns($sTableName, $bTypes=true)
{ {
if(!array_key_exists($sTableName, $this->asOptions['tables'])) return false; if(!array_key_exists($sTableName, $this->asOptions['tables'])) return false;
$asTableColumns = array(self::getId($sTableName)); $asTableColumns = array(self::getId($sTableName));
foreach($this->asOptions['tables'][$sTableName] as $sFieldName) $asTableColumns[] = $sFieldName; foreach($this->asOptions['tables'][$sTableName] as $sFieldName) $asTableColumns[] = $sFieldName;
$asTableColumns[] = 'led'; $asTableColumns[] = 'led';
if(!$bTypes) return $asTableColumns; if(!$bTypes) return $asTableColumns;
$asTableName = array_fill(0, count($asTableColumns), $sTableName); $asTableName = array_fill(0, count($asTableColumns), $sTableName);
return array_combine($asTableColumns, array_map(array('self', 'getColumnType'), $asTableColumns, $asTableName)); return array_combine($asTableColumns, array_map(array('self', 'getColumnType'), $asTableColumns, $asTableName));
} }
public function isColumnInTable($sTableName, $sColName) { public function isColumnInTable($sTableName, $sColName) {
$asCols = $this->getTablecolumns($sTableName, false); $asCols = $this->getTablecolumns($sTableName, false);
return ($asCols && in_array($sColName, $asCols)); return ($asCols && in_array($sColName, $asCols));
@@ -383,7 +383,7 @@ class Db extends PhpObject
private function addQuotes($oData) private function addQuotes($oData)
{ {
//TODO remake //TODO remake
$asTrustedFunc = array('CURDATE()', 'NOW()'); $asTrustedFunc = array('CURDATE()', 'NOW()', 'NULL');
$sChar = "'"; $sChar = "'";
if(is_array($oData)) if(is_array($oData))
{ {
@@ -401,7 +401,7 @@ class Db extends PhpObject
{ {
return $this->oConnection->insert_id; return $this->oConnection->insert_id;
} }
private function getLastImpact() private function getLastImpact()
{ {
return ($this->oConnection->affected_rows > 0); return ($this->oConnection->affected_rows > 0);
@@ -428,7 +428,7 @@ class Db extends PhpObject
{ {
$asConstraints = array($this->getId($sTableName)=>$asConstraints); $asConstraints = array($this->getId($sTableName)=>$asConstraints);
} }
//Cleaning values //Cleaning values
$this->cleanSql($sTableName); $this->cleanSql($sTableName);
$this->cleanSql($asData); $this->cleanSql($asData);
@@ -437,7 +437,7 @@ class Db extends PhpObject
$asConstraintsValues = $this->addQuotes($asConstraints); $asConstraintsValues = $this->addQuotes($asConstraints);
$this->addColumnSelectors($asQueryValues); $this->addColumnSelectors($asQueryValues);
$this->addColumnSelectors($asConstraintsValues); $this->addColumnSelectors($asConstraintsValues);
//Building query //Building query
if(!$bLedUpdate) $asQueryValues['led'] = 'led'; if(!$bLedUpdate) $asQueryValues['led'] = 'led';
$sLimit = $iLimit>0?" LIMIT $iLimit":""; $sLimit = $iLimit>0?" LIMIT $iLimit":"";
@@ -494,7 +494,7 @@ class Db extends PhpObject
$this->cleanSql($iTableId); $this->cleanSql($iTableId);
$bSuccess = true; $bSuccess = true;
//linked tables //linked tables
switch($sTableName) switch($sTableName)
{ {
@@ -516,13 +516,13 @@ class Db extends PhpObject
} }
return $bSuccess; return $bSuccess;
} }
public function emptyTable($sTableName) public function emptyTable($sTableName)
{ {
$this->cleanSql($sTableName); $this->cleanSql($sTableName);
return $this->setQuery("TRUNCATE ".$sTableName); return $this->setQuery("TRUNCATE ".$sTableName);
} }
public function selectList($sTableName, $sColumnName='', $asConstraints=array()) public function selectList($sTableName, $sColumnName='', $asConstraints=array())
{ {
$sColumnName = $sColumnName==''?self::getText($sTableName):$sColumnName; $sColumnName = $sColumnName==''?self::getText($sTableName):$sColumnName;
@@ -533,7 +533,7 @@ class Db extends PhpObject
true, true,
$sIdColumnName); $sIdColumnName);
} }
public function selectRows($asInfo, $sGroupBy='', $bStringOnly=true) public function selectRows($asInfo, $sGroupBy='', $bStringOnly=true)
{ {
$asAttributes = array('select'=>"SELECT", 'from'=>"FROM", 'join'=>"LEFT JOIN", 'joinOn'=>"LEFT JOIN", 'constraint'=>"WHERE", 'groupBy'=>"GROUP BY", 'orderBy'=>"ORDER BY", 'limit'=>'LIMIT'); $asAttributes = array('select'=>"SELECT", 'from'=>"FROM", 'join'=>"LEFT JOIN", 'joinOn'=>"LEFT JOIN", 'constraint'=>"WHERE", 'groupBy'=>"GROUP BY", 'orderBy'=>"ORDER BY", 'limit'=>'LIMIT');
@@ -543,16 +543,16 @@ class Db extends PhpObject
//Simple selectRows //Simple selectRows
if(!is_array($asInfo)) $asInfo = array('from'=>$asInfo); if(!is_array($asInfo)) $asInfo = array('from'=>$asInfo);
//Get table by key //Get table by key
if($sGroupBy===true) if($sGroupBy===true)
{ {
$sGroupBy = self::getId($asInfo['from']); $sGroupBy = self::getId($asInfo['from']);
//Add id to selection //Add id to selection
if(isset($asInfo['select']) && $asInfo['select'][0]!="*") $asInfo['select'][] = $sGroupBy; if(isset($asInfo['select']) && $asInfo['select'][0]!="*") $asInfo['select'][] = $sGroupBy;
} }
$sQuery = ""; $sQuery = "";
foreach($asAttributes as $sStatement => $sKeyWord) foreach($asAttributes as $sStatement => $sKeyWord)
{ {
@@ -574,12 +574,12 @@ class Db extends PhpObject
{ {
if(is_array($asConstraints)) if(is_array($asConstraints))
{ {
if(array_key_exists($sField, $asInfo['constOpe']) && $asInfo['constOpe'][$sField]=='BETWEEN') { if(array_key_exists('constOpe', $asInfo) && array_key_exists($sField, $asInfo['constOpe']) && $asInfo['constOpe'][$sField]=='BETWEEN') {
//Between //Between
$asSelection[$sField] = $asConstraints['from'].' AND '.$asConstraints['to']; $asSelection[$sField] = $asConstraints['from'].' AND '.$asConstraints['to'];
$asInfo['constOpe'][$sField] = " BETWEEN "; $asInfo['constOpe'][$sField] = " BETWEEN ";
} }
else { else {
//Multiple values (IN) //Multiple values (IN)
$asSelection[$sField] = "(".implode(', ', $asConstraints).")"; $asSelection[$sField] = "(".implode(', ', $asConstraints).")";
$asInfo['constOpe'][$sField] = " IN "; $asInfo['constOpe'][$sField] = " IN ";
@@ -622,7 +622,7 @@ class Db extends PhpObject
$sQuery .= " ".$sKeyWord." * "; $sQuery .= " ".$sKeyWord." * ";
} }
} }
return $this->getArrayQuery(trim($sQuery), $bStringOnly, $sGroupBy); return $this->getArrayQuery(trim($sQuery), $bStringOnly, $sGroupBy);
} }
@@ -646,7 +646,7 @@ class Db extends PhpObject
{ {
//Table ID directly //Table ID directly
if(!is_array($asConstraints)) $asConstraints = array($this->getId($sTableName)=>$asConstraints); if(!is_array($asConstraints)) $asConstraints = array($this->getId($sTableName)=>$asConstraints);
$asRows = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints)); $asRows = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints));
$iCountNb = count($asRows); $iCountNb = count($asRows);
switch($iCountNb) switch($iCountNb)
@@ -661,13 +661,13 @@ class Db extends PhpObject
} }
return $asResult; return $asResult;
} }
public function selectColumn($sTableName, $asColumnNames, $asConstraints) public function selectColumn($sTableName, $asColumnNames, $asConstraints)
{ {
$sGroupBy = ''; $sGroupBy = '';
if(!is_array($asColumnNames)) $asColumnNames = array($asColumnNames); if(!is_array($asColumnNames)) $asColumnNames = array($asColumnNames);
else $sGroupBy = $asColumnNames[0]; else $sGroupBy = $asColumnNames[0];
return $this->selectRows( return $this->selectRows(
array( array(
'select' => $asColumnNames, 'select' => $asColumnNames,
@@ -687,7 +687,7 @@ class Db extends PhpObject
$oResult = $this->selectRow($sTableName, $oConstraints, $sColumnName); $oResult = $this->selectRow($sTableName, $oConstraints, $sColumnName);
return empty($oResult)?false:$oResult; return empty($oResult)?false:$oResult;
} }
public function selectId($sTableName, $oConstraints) public function selectId($sTableName, $oConstraints)
{ {
return $this->selectValue($sTableName, self::getId($sTableName), $oConstraints); return $this->selectValue($sTableName, self::getId($sTableName), $oConstraints);
@@ -750,23 +750,23 @@ class Db extends PhpObject
return array_combine($asKeys, $asValues); return array_combine($asKeys, $asValues);
} }
} }
/* /*
public function select($asFields='*') public function select($asFields='*')
{ {
$oSql = new Sql($this, array('select'=>$asFields)); $oSql = new Sql($this, array('select'=>$asFields));
return $oSql; return $oSql;
} }
public function from($sTable) public function from($sTable)
{ {
$oSql = new Sql($this, array('from'=>$sTable)); $oSql = new Sql($this, array('from'=>$sTable));
return $oSql; return $oSql;
} }
public function getConnection() public function getConnection()
{ {
return $this->oConnection; return $this->oConnection;
} }
*/ */
} }

View File

@@ -9,10 +9,10 @@ class Feed extends PhpObject
{ {
const CHANNEL_TAGS = array('title', 'link', 'copyright', 'description', 'language', 'lastBuildDate', 'generator', 'webMaster'); const CHANNEL_TAGS = array('title', 'link', 'copyright', 'description', 'language', 'lastBuildDate', 'generator', 'webMaster');
const ITEM_TAGS = array('title', 'author', 'link', 'category', 'description', 'pubDate', 'guid'); const ITEM_TAGS = array('title', 'author', 'link', 'category', 'description', 'pubDate', 'guid');
private $asChannel; private $asChannel;
private $asItems; private $asItems;
/** /**
* Constructor * Constructor
* @param array $asChannel Description of the feed: fields 'title', 'link' (optional), 'copyright', 'description', 'language', 'webMaster' * @param array $asChannel Description of the feed: fields 'title', 'link' (optional), 'copyright', 'description', 'language', 'webMaster'
@@ -26,12 +26,12 @@ class Feed extends PhpObject
$asChannel['link'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'/rss'; $asChannel['link'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'/rss';
} }
$this->asChannel = $asChannel; $this->asChannel = $asChannel;
//Items //Items
$this->asItems = array(); $this->asItems = array();
array_walk($asItems, array($this, 'addItem')); array_walk($asItems, array($this, 'addItem'));
} }
public function loadRss($sUrl) public function loadRss($sUrl)
{ {
$oCurl = curl_init(); $oCurl = curl_init();
@@ -46,10 +46,10 @@ class Feed extends PhpObject
//curl_setopt($oCurl, CURLOPT_ENCODING, 'gzip'); //curl_setopt($oCurl, CURLOPT_ENCODING, 'gzip');
$sRssContent = curl_exec($oCurl); $sRssContent = curl_exec($oCurl);
curl_close($oCurl); curl_close($oCurl);
//Parse document encoding (useless) //Parse document encoding (useless)
$sEncoding = $this->getPregMatch("'encoding=[\'\"](.*?)[\'\"]'si", $sRssContent); $sEncoding = $this->getPregMatch("'encoding=[\'\"](.*?)[\'\"]'si", $sRssContent);
//Parse Channel info //Parse Channel info
if(preg_match("'<channel.*?>(.*?)</channel>'si", $sRssContent, $sChannelContent)) { if(preg_match("'<channel.*?>(.*?)</channel>'si", $sRssContent, $sChannelContent)) {
foreach(self::CHANNEL_TAGS as $sChannelTag) foreach(self::CHANNEL_TAGS as $sChannelTag)
@@ -66,7 +66,7 @@ class Feed extends PhpObject
if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
} }
}*/ }*/
//Parse Image info //Parse Image info
/*preg_match("'<image.*?>(.*?)</image>'si", $sRssContent, $out_imageinfo); /*preg_match("'<image.*?>(.*?)</image>'si", $sRssContent, $out_imageinfo);
if (isset($out_imageinfo[1])) { if (isset($out_imageinfo[1])) {
@@ -75,7 +75,7 @@ class Feed extends PhpObject
if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
} }
}*/ }*/
//Parse Items //Parse Items
preg_match_all("'<item(| .*?)>(.*?)</item>'si", $sRssContent, $asItems); preg_match_all("'<item(| .*?)>(.*?)</item>'si", $sRssContent, $asItems);
//$i = 0; //$i = 0;
@@ -91,43 +91,43 @@ class Feed extends PhpObject
//if($this->stripHTML && $this->asItems[$i]['description']) $this->asItems[$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['description']))); //if($this->stripHTML && $this->asItems[$i]['description']) $this->asItems[$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['description'])));
//Strip HTML tags and other bullshit from TITLE //Strip HTML tags and other bullshit from TITLE
//if($this->stripHTML && $this->asItems[$i]['title']) $this->asItems[$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['title']))); //if($this->stripHTML && $this->asItems[$i]['title']) $this->asItems[$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($this->asItems[$i]['title'])));
//Fix for author //Fix for author
if(!array_key_exists('author', $asItemTags)) if(!array_key_exists('author', $asItemTags))
{ {
$sTagContent = $this->getPregMatch("'<dc\:creator.*?>(.*?)</dc\:creator>'si", $asItem); $sTagContent = $this->getPregMatch("'<dc\:creator.*?>(.*?)</dc\:creator>'si", $asItem);
if($sTagContent != '') $asItemTags['author'] = $sTagContent; if($sTagContent != '') $asItemTags['author'] = $sTagContent;
} }
$this->addItem($asItemTags); $this->addItem($asItemTags);
} }
} }
public function addItem($asItem) public function addItem($asItem)
{ {
$this->asItems[] = $asItem; $this->asItems[] = $asItem;
return count($this->asItems) - 1; return count($this->asItems) - 1;
} }
public function removeItem($iItemId) public function removeItem($iItemId)
{ {
$bExist = array_key_exists($iItemId, $this->asItems); $bExist = array_key_exists($iItemId, $this->asItems);
if($bExist) unset($this->asItems[$iItemId]); if($bExist) unset($this->asItems[$iItemId]);
return $bExist; return $bExist;
} }
public function filterItems($sField, $sRegex) public function filterItems($sField, $sRegex)
{ {
$this->asItems = array_filter($this->asItems, function($asItem) use ($sField, $sRegex) { $this->asItems = array_filter($this->asItems, function($asItem) use ($sField, $sRegex) {
return preg_match($sRegex, $asItem[$sField]); return preg_match($sRegex, $asItem[$sField]);
}); });
} }
public function getItems() public function getItems()
{ {
return $this->asItems; return $this->asItems;
} }
private function getGlobalPubDate() private function getGlobalPubDate()
{ {
$iGlobalPubDate = 0; $iGlobalPubDate = 0;
@@ -141,7 +141,7 @@ class Feed extends PhpObject
} }
return self::cleanRss(self::getDate($iGlobalPubDate)); return self::cleanRss(self::getDate($iGlobalPubDate));
} }
public function getFeed($bSetMime=true) public function getFeed($bSetMime=true)
{ {
//Feed Channel //Feed Channel
@@ -154,20 +154,20 @@ class Feed extends PhpObject
$sRssChannel .= self::getHtml($this->getGlobalPubDate(), 'lastBuildDate'); $sRssChannel .= self::getHtml($this->getGlobalPubDate(), 'lastBuildDate');
$sRssChannel .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator'); $sRssChannel .= self::getHtml('Lutran.fr RSS Feed Generator', 'generator');
$sRssChannel .= array_key_exists('webMaster', $this->asChannel)?self::getHtml($this->asChannel['webMaster'].' (Webmaster)', 'webMaster'):''; $sRssChannel .= array_key_exists('webMaster', $this->asChannel)?self::getHtml($this->asChannel['webMaster'].' (Webmaster)', 'webMaster'):'';
//Feed Items //Feed Items
$asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pubDate'); $asSortedItems = $this->rSortTimeMatrix($this->asItems, 'pubDate');
$sItems = implode("\n", array_map(array($this, 'buildItem'), $asSortedItems)); $sItems = implode("\n", array_map(array($this, 'buildItem'), $asSortedItems));
//Global Feed //Global Feed
$sFeed = '<?xml version="1.0" encoding="'.Settings::TEXT_ENC.'" ?>'; $sFeed = '<?xml version="1.0" encoding="'.Settings::TEXT_ENC.'" ?>';
$sFeed .= self::getHtml(self::getHtml($sRssChannel.$sItems, 'channel'), 'rss', '', '', array('version'=>'2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom')); $sFeed .= self::getHtml(self::getHtml($sRssChannel.$sItems, 'channel'), 'rss', '', '', array('version'=>'2.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom'));
if($bSetMime) header('Content-type: application/rss+xml'); if($bSetMime) header('Content-type: application/rss+xml');
return $sFeed; return $sFeed;
} }
private static function getDate($sDate) private static function getDate($sDate)
{ {
if(!is_numeric($sDate)) if(!is_numeric($sDate))
@@ -176,7 +176,7 @@ class Feed extends PhpObject
} }
return date('r', $sDate); return date('r', $sDate);
} }
private function buildItem($asItem) private function buildItem($asItem)
{ {
$sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title'); $sRssItem = self::getHtml(self::cleanRss($asItem['title']), 'title');
@@ -188,22 +188,22 @@ class Feed extends PhpObject
$sRssItem .= array_key_exists('guid', $asItem)?self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')):''; $sRssItem .= array_key_exists('guid', $asItem)?self::getHtml($asItem['guid'], 'guid', '', '', array('isPermaLink'=>'true')):'';
return self::getHtml($sRssItem, 'item'); return self::getHtml($sRssItem, 'item');
} }
private function getPregMatch($pattern, $subject) private function getPregMatch($pattern, $subject)
{ {
preg_match($pattern, $subject, $out); preg_match($pattern, $subject, $out);
//if there is some result... process it and return it //if there is some result... process it and return it
if(isset($out[1])) if(isset($out[1]))
{ {
//If code page is set convert character encoding to required //If code page is set convert character encoding to required
//if ($this->cp != '') $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]); //if ($this->cp != '') $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
return str_replace(array('<![CDATA[', ']]>'), '', trim($out[1])); return str_replace(array('<![CDATA[', ']]>'), '', trim($out[1]));
} }
else return ''; else return '';
} }
private static function getHtml($oText, $sTag, $sClass='', $sStyle='', $asExtraAttr=array(), $bAutoClose=false, $sInter='') private static function getHtml($oText, $sTag, $sClass='', $sStyle='', $asExtraAttr=array(), $bAutoClose=false, $sInter='')
{ {
$sHtmlAttr = ''; $sHtmlAttr = '';
@@ -229,19 +229,17 @@ class Feed extends PhpObject
} }
return $sHtml; return $sHtml;
} }
private static function cleanRss($oText) private static function cleanRss($oText)
{ {
$asForbiddenChars = array('&', '<', '>', '"', "'");
$asReplacementCode = array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;');
if(!is_array($oText)) if(!is_array($oText))
{ {
return str_replace($asForbiddenChars, $asReplacementCode, $oText); return htmlspecialchars($oText, ENT_QUOTES);
} }
elseif(count($oText)>0) elseif(count($oText)>0)
{ {
$oTextKeys = array_map(array($this, 'cleanRss'), array_keys($oText)); $oTextKeys = array_map(array(self, 'cleanRss'), array_keys($oText));
$oTextValues = array_map(array($this, 'cleanRss'), array_values($oText)); $oTextValues = array_map(array(self, 'cleanRss'), array_values($oText));
return array_combine($oTextKeys, $oTextValues); return array_combine($oTextKeys, $oTextValues);
} }
else else
@@ -249,7 +247,7 @@ class Feed extends PhpObject
return $oText; return $oText;
} }
} }
private static function encapsulate($oText, $sPre='', $sPost=false, $sInter='') private static function encapsulate($oText, $sPre='', $sPost=false, $sInter='')
{ {
if($sPost===false) if($sPost===false)
@@ -262,7 +260,7 @@ class Feed extends PhpObject
} }
return $sPre.$oText.$sPost; return $sPre.$oText.$sPost;
} }
private static function rSortTimeMatrix($asMatrix, $sTimeCol) private static function rSortTimeMatrix($asMatrix, $sTimeCol)
{ {
$asResult = array(); $asResult = array();
@@ -271,4 +269,4 @@ class Feed extends PhpObject
foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId]; foreach($asKeys as $iRowId=>$iTimeStamp) $asResult[$iRowId] = $asMatrix[$iRowId];
return $asResult; return $asResult;
} }
} }

View File

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