Adding to SP3 : fixing images and others

This commit is contained in:
2014-11-30 17:24:49 +01:00
parent b6ebf33a02
commit c1f4308089
9 changed files with 159 additions and 115 deletions

View File

@@ -512,28 +512,38 @@ class Databap extends PhpObject
$sRegEx = '(https?://|www\\.)(.*)'.$sCat.'(.*)\\.[a-zA-Z]{2,4}';
break;
}
$asResult = $this->oMySql->selectRows(array('select'=>array('id_message', 'nickname', 'message', 'led'), 'from'=>'messages', 'constraint'=>array('message'=>$sRegEx), 'constOpe'=>array('message'=>' REGEXP ')));
$sMessageIdCol = MySqlManager::getId(self::MSG_TABLE);
$sChanIdCol = MySqlManager::getId(self::CHAN_TABLE);
$asResult = $this->oMySql->selectRows(array('select'=>array($sMessageIdCol, 'nickname', $sChanIdCol, 'message', 'led'),
'from'=>self::MSG_TABLE,
'constraint'=>array('message'=>$sRegEx),
'constOpe'=>array('message'=>' REGEXP ')));
$asChans = $this->oMySql->selectRows(array('select'=>array($sChanIdCol, 'safe_name'), 'from'=>self::CHAN_TABLE), true, $sChanIdCol);
$sPattern = '/(https?\:\/\/|www\.)[\S]+\.[a-zA-Z]{2,4}([\S]*)/ui';
foreach($asResult as $iLinkId=>$asRow)
{
//get link out of message
preg_match($sPattern, $asRow['message'], $asMatches);
$asRow['link'] = (substr($asMatches[0], 0, 4)=='http')?$asMatches[0]:'http://'.$asMatches[0];
//add item
$sChatlink = $this->getInternalLink('chat', $asRow['id_message']);
$asItem = array
(
'title'=>'Lien'.($sCat==''?'':' - '.$sCat).' #'.($iLinkId+1),
'category'=>$sCat,
'description'=>'Lien post&eacute; par '.self::getNickNameFormat($asRow['nickname']).' &agrave; '.self::getDateFormat($asRow['led']).' : <a href="'.$asRow['link'].'" target="_blank">'.$asRow['message'].'</a>',
'author'=>$asRow['nickname'],
'link'=>$sChatlink,
'pub_date'=>$asRow['led'],
'guid'=>$sChatlink
);
$oFeed->addItem($asItem);
//filter on authorized chans
if($this->checkChanAuth($asChans[$asRow[$sChanIdCol]], $this->getUserId()))
{
//get link out of message
preg_match($sPattern, $asRow['message'], $asMatches);
$asRow['link'] = (substr($asMatches[0], 0, 4)=='http')?$asMatches[0]:'http://'.$asMatches[0];
//add item
$sChatlink = $this->getInternalLink('chat', $asRow['id_message']);
$asItem = array
(
'title'=>'Lien'.($sCat==''?'':' - '.$sCat).' #'.($iLinkId+1),
'category'=>$sCat,
'description'=>'Lien post&eacute; par '.self::getNickNameFormat($asRow['nickname']).' &agrave; '.self::getDateFormat($asRow['led']).' : <a href="'.$asRow['link'].'" target="_blank">'.$asRow['message'].'</a>',
'author'=>$asRow['nickname'],
'link'=>$sChatlink,
'pub_date'=>$asRow['led'],
'guid'=>$sChatlink
);
$oFeed->addItem($asItem);
}
}
}
@@ -944,32 +954,38 @@ class Databap extends PhpObject
//Add record in Search Table
$this->oSearchEngine->buildIndex($iDbDocId, self::DOC_TYPE);
return self::jsonExport(array('result'=>'success','doc_id'=>$iDbDocId));
//TODO add error handling
return $this->getJsonPostResult(self::SUCCESS, '', array('doc_id'=>$iDbDocId));
}
public function getDoc($iDocId)
{
//Extract doc data
$asDoc = $this->oMySql->selectRow(self::DOC_TABLE, $iDocId);
$asDoc['description'] = self::getDescriptionFormat($asDoc['description']);
$asDoc['title'] = self::getDescriptionFormat($asDoc['title']);
$asDoc['led'] = self::getDateFormat($asDoc['led']);
//Extract extra data
$asUser = $this->getUserInfo($asDoc[MySqlManager::getId(self::USER_TABLE)]);
$asDoc['name'] = $asUser['name'];
$asDoc['company'] = $asUser['company'];
//Extract doc files
$sFileIdCol = MySqlManager::getId(self::FILE_TABLE);
$asFiles = $this->oMySql->selectRows(array('from'=>self::FILE_TABLE, 'constraint'=>array('id_item'=>$iDocId, 'type'=>self::DOC_TYPE)));
foreach($asFiles as $asFile)
$bSuccess = !empty($asDoc);
$sDesc = '';
if(!$bSuccess) $sDesc = self::NOT_FOUND;
else
{
$asDoc['files'][$asFile[$sFileIdCol]] = array( 'description' => self::getDescriptionFormat($asFile['description']),
'ext' => $asFile['extension']);
$asDoc['description'] = self::getDescriptionFormat($asDoc['description']);
$asDoc['title'] = self::getDescriptionFormat($asDoc['title']);
$asDoc['led'] = self::getDateFormat($asDoc['led']);
//Extract extra data
$asUser = $this->getUserInfo($asDoc[MySqlManager::getId(self::USER_TABLE)]);
$asDoc['name'] = $asUser['name'];
$asDoc['company'] = $asUser['company'];
//Extract doc files
$sFileIdCol = MySqlManager::getId(self::FILE_TABLE);
$asFiles = $this->oMySql->selectRows(array('from'=>self::FILE_TABLE, 'constraint'=>array('id_item'=>$iDocId, 'type'=>self::DOC_TYPE)));
foreach($asFiles as $asFile)
{
$asDoc['files'][$asFile[$sFileIdCol]] = array( 'description' => self::getDescriptionFormat($asFile['description']),
'ext' => $asFile['extension']);
}
}
return self::jsonExport($asDoc);
return $this->getJsonPostResult($bSuccess, $sDesc, $asDoc);
}
public function addTable($sSystem, $sTitle, $sDescription, $sKeyWords, $iPrevTableId, $bSimul=false)
@@ -1102,19 +1118,16 @@ class Databap extends PhpObject
return $sResult;
}
public function uploadImage()
public function uploadFile($sFileType)
{
switch($sFileType)
{
case 'image': $asAuthorizedTypes = self::$UPLOAD_IMG_EXTS; break;
case 'doc' : $asAuthorizedTypes = self::$UPLOAD_DOC_EXTS; break;
default : $asAuthorizedTypes = array();
}
$this->oClassManagement->incClass('fileuploader');
$oFileUploader = new fileUploader(Procedure::IMAGE_FOLDER_TMP, self::$UPLOAD_IMG_EXTS);
$asResult = $oFileUploader->handleUpload();
return $this->jsonConvert($asResult);
}
public function uploadDoc()
{
$this->oClassManagement->incClass('fileuploader');
$oFileUploader = new fileUploader(self::DOC_TMP_FOLDER, self::$UPLOAD_DOC_EXTS);
$oFileUploader = new fileUploader(self::DOC_TMP_FOLDER, $asAuthorizedTypes);
$asResult = $oFileUploader->handleUpload();
return $this->jsonConvert($asResult);
@@ -1179,8 +1192,7 @@ class Databap extends PhpObject
private function getTableInfo($iTableId, $bFormatting=true)
{
$asTable = $this->oMySql->selectRow(self::TABL_TABLE, $iTableId);
if(!$asTable) $asTable = array(); //return false on empty
else
if(!empty($asTable))
{
$asTable['timestamp'] = strtotime($asTable['led']);
if($bFormatting)
@@ -1852,10 +1864,8 @@ class Databap extends PhpObject
private function downloadToTmp($sUrl)
{
$sFileInfo = pathinfo($sUrl);
$sImageExt = mb_strtolower($sFileInfo['extension']);
$sFilePath = self::DOC_TMP_FOLDER.uniqid().'.'.$sImageExt;
return ToolBox::createThumbnail($sUrl, self::CHAT_IMG_MAX_WIDTH, self::CHAT_IMG_MAX_HEIGHT, $sFilePath, false, Databap::$UPLOAD_IMG_EXTS);
$sFilePath = self::DOC_TMP_FOLDER.uniqid();
return ToolBox::createThumbnail($sUrl, self::CHAT_IMG_MAX_WIDTH, self::CHAT_IMG_MAX_HEIGHT, $sFilePath, false, self::$UPLOAD_IMG_EXTS);
}
private function sendPM($iUserIdTo, $sMessage)
@@ -2344,11 +2354,6 @@ class Databap extends PhpObject
return $oMask->getMask();
}
public static function checkNoAuthCookieResetAction($sAction)
{
return in_array($sAction, array('messages', 'upload_image', 'user_info', 'rss'));
}
public function logMeIn($sToken, $sAction)
{
$iUserId = 0;
@@ -2384,7 +2389,7 @@ class Databap extends PhpObject
$asUserInfo = $this->oMySql->selectRow(self::USER_TABLE, $asConstraints, array($sUserTableId, 'led'));
$iUserId = $asUserInfo[$sUserTableId];
//Check pass reset necessity
//Reset pass once a day
$bResetPass = (mb_substr($asUserInfo['led'], 0, 10) != date(Databap::DATE_SQL_FORMAT));
}
}
@@ -2398,10 +2403,7 @@ class Databap extends PhpObject
if($iUserId>0)
{
$this->setUserId($iUserId);
if(!self::checkNoAuthCookieResetAction($sAction) && $bResetPass)
{
$this->resetAuthCookie();
}
if($bResetPass) $this->resetAuthCookie();
//Post-Redirect-Get if user manually logging from logon page
if($sNameToken!='')

View File

@@ -516,21 +516,22 @@ class MySqlManager extends PhpObject
public function selectRow($sTableName, $asConstraints=array(), $sColumnName='*')
{
if(!is_array($asConstraints))
{
$asConstraints = array($this->getId($sTableName)=>$asConstraints);
}
$asResult = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints));
$iCountNb = count($asResult);
//Table ID directly
if(!is_array($asConstraints)) $asConstraints = array($this->getId($sTableName)=>$asConstraints);
$asRows = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints));
$iCountNb = count($asRows);
switch($iCountNb)
{
case 0 :
return false;
$asResult = array();
break;
case $iCountNb > 1 :
$this->addError('Trop de r&eacute;sultats pour un selectRow() : '.$iCountNb.' lignes. Table: '.$sTableName.', contrainte: '.self::implodeAll($asConstraints, '=', ' ').', colonne: '.$sColumnName);
break;
default:
$asResult = array_shift($asRows);
}
return array_shift($asResult);
return $asResult;
}
public function selectValue($sTableName, $sColumnName, $oConstraints=array())

View File

@@ -130,20 +130,48 @@ class ToolBox
return json_encode($asData);
}
public static function getMimeType($sPath, $bSubTypeOnly=false)
{
//Retrieving file Content Type
if(file_exists($sPath)) //Local
{
$oFileInfo = new finfo(FILEINFO_MIME);
$sMimetype = $oFileInfo->file($sPath);
}
else //Remote
{
$oCurl = curl_init($sPath);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($oCurl, CURLOPT_HEADER, true);
curl_setopt($oCurl, CURLOPT_NOBODY, true);
curl_exec($oCurl);
$sMimetype = curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
}
//Only sub type (after /)
if($bSubTypeOnly)
{
preg_match('`\/(?P<type>\w+)(\;|$)`ui', mb_strtolower($sMimetype), $asMatch);
$sMimetype = $asMatch['type'];
}
return $sMimetype;
}
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false)
{
$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']);
//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 .= $sInName;
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))