272 lines
9.1 KiB
PHP
Executable File
272 lines
9.1 KiB
PHP
Executable File
<?php
|
|
|
|
class Spot extends Main
|
|
{
|
|
//Spot Mode
|
|
const MODE_HISTO = 'histo';
|
|
const MODE_BLOG = 'blog';
|
|
|
|
//Spot feed
|
|
const FEED_ID = '0Y5LrvigElWeAieBGnFol0KBEuOTkFJmm';
|
|
const FEED_HOOK = 'https://api.findmespot.com/spot-main-web/consumer/rest-api/2.0/public/feed/';
|
|
const FEED_TYPE_XML = '/message.xml';
|
|
const FEED_TYPE_JSON = '/message.json';
|
|
|
|
//Database
|
|
const MSG_TABLE = 'messages';
|
|
const FEED_TABLE = 'feeds';
|
|
const SPOT_TABLE = 'spots';
|
|
const POST_TABLE = 'posts';
|
|
|
|
const FORMAT_TIME = 'd/m/Y à H:i';
|
|
|
|
const FEED_CHUNK_SIZE = 15;
|
|
|
|
public function __construct($oClassManagement, $sProcessPage)
|
|
{
|
|
parent::__construct($oClassManagement, $sProcessPage);
|
|
}
|
|
|
|
protected function install()
|
|
{
|
|
//Install DB
|
|
$this->oMySql->install();
|
|
|
|
//Add feed
|
|
//$this->oMySql->insertRow(self::FEED_TABLE, array('ref_feed_id'=>self::FEED_ID));
|
|
}
|
|
|
|
protected function getSqlOptions()
|
|
{
|
|
return array
|
|
(
|
|
'tables' => array
|
|
(
|
|
self::MSG_TABLE => array('ref_msg_id', MySqlManager::getId(self::FEED_TABLE), 'type', 'latitude', 'longitude', 'timestamp', 'unix_timestamp', 'content', 'battery_state'),
|
|
self::FEED_TABLE => array('ref_feed_id', MySqlManager::getId(self::SPOT_TABLE), 'name', 'description', 'status'),
|
|
self::SPOT_TABLE => array('ref_spot_id', 'name', 'model'),
|
|
self::POST_TABLE => array('name', 'content')
|
|
),
|
|
'types' => array
|
|
(
|
|
'ref_msg_id' => "INT",
|
|
'type' => "VARCHAR(20)",
|
|
'latitude' => "DECIMAL(7,5)",
|
|
'longitude' => "DECIMAL(8,5)",
|
|
'timestamp' => "DATETIME",
|
|
'unix_timestamp'=> "INT",
|
|
'content' => "LONGTEXT",
|
|
'battery_state' => "VARCHAR(10)",
|
|
'ref_spot_id' => "VARCHAR(10)",
|
|
'name' => "VARCHAR(100)",
|
|
'model' => "VARCHAR(20)",
|
|
'ref_feed_id' => "VARCHAR(40)",
|
|
'description' => "VARCHAR(100)",
|
|
'status' => "VARCHAR(10)",
|
|
),
|
|
'constraints' => array
|
|
(
|
|
self::MSG_TABLE => "UNIQUE KEY `uni_ref_msg_id` (`ref_msg_id`)",
|
|
self::FEED_TABLE => "UNIQUE KEY `uni_ref_feed_id` (`ref_feed_id`)",
|
|
self::SPOT_TABLE => "UNIQUE KEY `uni_ref_spot_id` (`ref_spot_id`)",
|
|
self::MSG_TABLE => "INDEX(`ref_msg_id`)",
|
|
self::FEED_TABLE => "INDEX(`ref_feed_id`)",
|
|
self::SPOT_TABLE => "INDEX(`ref_spot_id`)",
|
|
),
|
|
'cascading_delete' => array
|
|
(
|
|
self::SPOT_TABLE=>array(self::MSG_TABLE)
|
|
)
|
|
);
|
|
}
|
|
|
|
public function getMainPage()
|
|
{
|
|
return parent::getMainPage(array('vars'=>array( 'feed_id' => self::FEED_ID,
|
|
'chunk_size'=> self::FEED_CHUNK_SIZE,
|
|
'mode' => Settings::MODE)));
|
|
}
|
|
|
|
/* Getting & Storing messages */
|
|
|
|
private function getFeed($sRefFeedId=self::FEED_ID)
|
|
{
|
|
$sUrl = self::FEED_HOOK.$sRefFeedId.self::FEED_TYPE_JSON;
|
|
$sContent = file_get_contents($sUrl);
|
|
return json_decode($sContent, true);
|
|
}
|
|
|
|
private function updateFeed($sRefFeedId=self::FEED_ID)
|
|
{
|
|
$asData = $this->getFeed($sRefFeedId);
|
|
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
|
|
$asFeed = $asData['response']['feedMessageResponse']['feed'];
|
|
|
|
if(!empty($asMsgs))
|
|
{
|
|
//Update Spot Info
|
|
$asFirstMsg = array_values($asMsgs)[0];
|
|
$asSpotInfo = array('ref_spot_id'=>$asFirstMsg['messengerId'], 'name'=>$asFirstMsg['messengerName'], 'model'=>$asFirstMsg['modelId']);
|
|
$iSpotId = $this->oMySql->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
|
|
|
|
//Update Feed Info
|
|
$asFeedInfo = array('ref_feed_id' => $sRefFeedId,
|
|
MySqlManager::getId(self::SPOT_TABLE) => $iSpotId,
|
|
'name' => $asFeed['name'],
|
|
'description' => $asFeed['description'],
|
|
'status' => $asFeed['status'],
|
|
'led' => date(MySqlManager::MYSQL_TIMESTAMP)); //update with current time
|
|
$iFeedId = $this->oMySql->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
|
|
|
|
//Update Messages
|
|
foreach($asMsgs as $asMsg)
|
|
{
|
|
$asMsg = array( 'ref_msg_id' => $asMsg['id'],
|
|
MySqlManager::getId(self::FEED_TABLE) => $iFeedId,
|
|
'type' => $asMsg['messageType'],
|
|
'latitude' => $asMsg['latitude'],
|
|
'longitude' => $asMsg['longitude'],
|
|
'timestamp' => date(MySqlManager::MYSQL_TIMESTAMP, strtotime($asMsg['dateTime'])), //Stored in Local Time
|
|
'unix_timestamp' => $asMsg['unixTime'], //Stored in UNIX time
|
|
'content' => $asMsg['messageContent'],
|
|
'battery_state' => $asMsg['batteryState']);
|
|
$this->oMySql->insertUpdateRow(self::MSG_TABLE, $asMsg, array('ref_msg_id'));
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getMessages($sRefFeedId=self::FEED_ID)
|
|
{
|
|
//Adding another point to test
|
|
/*
|
|
$test = $this->oMySql->selectRow(self::MSG_TABLE, 1);
|
|
unset($test['id_message']);
|
|
$test['ref_msg_id'] = $test['ref_msg_id'] + 1;
|
|
$test['latitude'] = '-41.4395';
|
|
$test['longitude'] = '172.1936';
|
|
$this->oMySql->insertUpdateRow(self::MSG_TABLE, $test, array('ref_msg_id'));
|
|
*/
|
|
|
|
//Check last message & update feed if necessary (max once a day)
|
|
$sLastMsg = $this->oMySql->selectValue(self::FEED_TABLE, 'led', array('ref_feed_id'=>$sRefFeedId));
|
|
if(Settings::MODE!=self::MODE_HISTO && mb_substr($sLastMsg, 0, 10) != date('Y-m-d')) $this->updateFeed($sRefFeedId);
|
|
|
|
//Extract messages
|
|
$asMessages = $this->getSpotMessages();
|
|
$bSuccess = !empty($asMessages);
|
|
$sDesc = $bSuccess?'':self::NO_DATA;
|
|
|
|
return self::getJsonResult($bSuccess, $sDesc, $asMessages);
|
|
}
|
|
|
|
private function getSpotMessages()
|
|
{
|
|
$asMessages = $this->oMySql->selectRows(array('from'=>self::MSG_TABLE, 'orderBy'=>array('timestamp'=>'ASC')));
|
|
foreach($asMessages as $iKey=>$asMessage)
|
|
{
|
|
$iUnixTimeStamp = strtotime($asMessage['timestamp']);
|
|
$asMessages[$iKey]['relative_time'] = Toolbox::getDateTimeDesc($iUnixTimeStamp);
|
|
$asMessages[$iKey]['formatted_time'] = date(self::FORMAT_TIME, $iUnixTimeStamp);
|
|
}
|
|
return $asMessages;
|
|
}
|
|
|
|
public function getNewsFeed($iChunk=0)
|
|
{
|
|
$asFeed = array();
|
|
|
|
//Messages
|
|
$asMessages = $this->getSpotMessages();
|
|
foreach($asMessages as $asMessage)
|
|
{
|
|
$iId = ($asMessage['unix_timestamp']*-1).'.0'.$asMessage[MySqlManager::getId(self::MSG_TABLE)];
|
|
$asFeed[$iId] = $asMessage;// array('type'=>'message', 'time'=>$asMessage['relative_time'], 'text'=>$asMessage['relative_time']);
|
|
$asFeed[$iId]['type'] = 'message';
|
|
$asFeed[$iId]['id'] = $asMessage[MySqlManager::getId(self::MSG_TABLE)];
|
|
}
|
|
|
|
//Pictures
|
|
$asPicPaths = glob('files/*.*');
|
|
foreach($asPicPaths as $iKey=>$sPicPath)
|
|
{
|
|
//Finding picture timestamp
|
|
$asPicInfo = self::getPicInfo($sPicPath);
|
|
$iPicTimeStamp = $asPicInfo['timestamp'];
|
|
|
|
//Preparing pictures sorting key and related info
|
|
$asPics[($iPicTimeStamp*-1).'.1'.$iKey] = array( 'type' => 'picture',
|
|
'path' => $sPicPath,
|
|
'rotate' => $asPicInfo['rotate'],
|
|
'formatted_time'=> date(self::FORMAT_TIME, $iPicTimeStamp),
|
|
'relative_time' => Toolbox::getDateTimeDesc($iPicTimeStamp));
|
|
}
|
|
ksort($asPics);
|
|
$iCount = count($asPics);
|
|
foreach($asPics as $iKey=>$asPic)
|
|
{
|
|
$asFeed[$iKey] = $asPic;
|
|
$asFeed[$iKey]['id'] = $iCount--;
|
|
}
|
|
|
|
//Post
|
|
$asPosts = $this->oMySql->selectRows(array('from'=>self::POST_TABLE));
|
|
foreach($asPosts as $asPost)
|
|
{
|
|
$iUnixTimeStamp = strtotime($asPost['led']);
|
|
$iId = ($iUnixTimeStamp*-1).'.2'.$asPost[MySqlManager::getId(self::POST_TABLE)];
|
|
$asFeed[$iId] = array('name'=>Toolbox::mb_ucwords($asPost['name']), 'post'=>$asPost['content'], 'type'=>'post');;
|
|
$asFeed[$iId]['relative_time'] = Toolbox::getDateTimeDesc($iUnixTimeStamp);
|
|
$asFeed[$iId]['formatted_time'] = date(self::FORMAT_TIME, $iUnixTimeStamp);
|
|
//$asFeed[$iId]['id'] = $asPost[MySqlManager::getId(self::POST_TABLE)];
|
|
}
|
|
|
|
ksort($asFeed);
|
|
|
|
//split chunks
|
|
$asFeed = array_slice($asFeed, $iChunk*self::FEED_CHUNK_SIZE, self::FEED_CHUNK_SIZE);
|
|
|
|
return self::getJsonResult(true, '', $asFeed);
|
|
}
|
|
|
|
public function addPost($sName, $sPost)
|
|
{
|
|
$asData = array('name'=>mb_strtolower(trim($sName)), 'content'=>trim($sPost));
|
|
$iPostId = $this->oMySql->insertRow(self::POST_TABLE, $asData);
|
|
return self::getJsonResult(($iPostId > 0), '');
|
|
}
|
|
|
|
public function upload()
|
|
{
|
|
$this->oClassManagement->incClass('uploader', true);
|
|
$oUploader = new Uploader(array('image_versions'=>array(), 'accept_file_types'=>'/\.(gif|jpe?g|png)$/i'));
|
|
$oUploader->init();
|
|
return $oUploader->getBody();
|
|
}
|
|
|
|
public static function getPicInfo($sPicPath)
|
|
{
|
|
$iPicTimeStamp = 0;
|
|
$asExif = @exif_read_data($sPicPath, 0, true);
|
|
|
|
//Timestamp
|
|
if(array_key_exists('DateTimeOriginal', $asExif['EXIF'])) $iPicTimeStamp = strtotime($asExif['EXIF']['DateTimeOriginal']);
|
|
else $iPicTimeStamp = $asExif['FILE']['FileDateTime'];
|
|
|
|
//Time Zone
|
|
//if($iPicTimeStamp >= self::HONEYMOON_DATE_BEG && $iPicTimeStamp <= self::HONEYMOON_DATE_END) $iPicTimeStamp -= 60*60*(12+1); //timezone + daylight saving
|
|
|
|
//Orientation
|
|
switch($asExif['IFD0']['Orientation'])
|
|
{
|
|
case 1: $sRotate = '0'; break; //None
|
|
case 3: $sRotate = '180'; break; //Flip over
|
|
case 6: $sRotate = '90'; break; //Clockwise
|
|
case 8: $sRotate = '-90'; break; //Trigo
|
|
default: $sRotate = $asExif['IFD0']['Orientation'];
|
|
}
|
|
|
|
return array('timestamp'=>$iPicTimeStamp, 'rotate'=>$sRotate);
|
|
}
|
|
}
|
|
|
|
?>
|