Move files (again)

This commit is contained in:
2023-11-07 19:41:28 +01:00
parent ab914a391f
commit 97645b3476
31 changed files with 155 additions and 38 deletions

332
lib/Converter.php Normal file
View File

@@ -0,0 +1,332 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\ToolBox;
use \Settings;
/**
* GPX to GeoJSON Converter
*
* To convert a gpx file:
* 1. Add file <file_name>.gpx to geo/ folder
* 2. Assign file to project: UPDATE projects SET codename = '<file_name>' WHERE id_project = <id_project>;
* 3. Load any page
*
* To force gpx rebuild:
* ?a=build_geojson&name=<file_name>
*/
class Converter extends PhpObject {
public function __construct() {
parent::__construct(__CLASS__);
}
public static function convertToGeoJson($sCodeName) {
$oGpx = new Gpx($sCodeName);
$oGeoJson = new GeoJson($sCodeName);
$oGeoJson->buildTracks($oGpx->getTracks());
if($oGeoJson->isSimplicationRequired()) $oGeoJson->buildTracks($oGpx->getTracks(), true);
$oGeoJson->sortOffTracks();
$oGeoJson->saveFile();
return $oGpx->getLog().'<br />'.$oGeoJson->getLog();
}
public static function isGeoJsonValid($sCodeName) {
$bResult = false;
$sGpxFilePath = Gpx::getFilePath($sCodeName);
$sGeoJsonFilePath = GeoJson::getFilePath($sCodeName);
//No need to generate if gpx is missing
if(!file_exists($sGpxFilePath) || file_exists($sGeoJsonFilePath) && filemtime($sGeoJsonFilePath) > filemtime(Gpx::getFilePath($sCodeName))) $bResult = true;
return $bResult;
}
}
class Geo extends PhpObject {
const GEO_FOLDER = '../geo/';
const OPT_SIMPLE = 'simplification';
protected $asTracks;
protected $sFilePath;
public function __construct($sCodeName) {
parent::__construct(get_class($this), Settings::DEBUG, PhpObject::MODE_HTML);
$this->sFilePath = self::getFilePath($sCodeName);
$this->asTracks = array();
}
public static function getFilePath($sCodeName) {
return self::GEO_FOLDER.$sCodeName.static::EXT;
}
public static function getDistFilePath($sCodeName) {
return 'geo/'.$sCodeName.static::EXT;
}
public function getLog() {
return $this->getCleanMessageStack(PhpObject::NOTICE_TAB);
}
}
class Gpx extends Geo {
const EXT = '.gpx';
public function __construct($sCodeName) {
parent::__construct($sCodeName);
$this->parseFile();
}
public function getTracks() {
return $this->asTracks;
}
private function parseFile() {
$this->addNotice('Parsing: '.$this->sFilePath);
if(!file_exists($this->sFilePath)) $this->addError($this->sFilePath.' file missing');
else {
$oXml = simplexml_load_file($this->sFilePath);
//Tracks
$this->addNotice('Converting '.count($oXml->trk).' tracks');
foreach($oXml->trk as $aoTrack) {
$asTrack = array(
'name' => (string) $aoTrack->name,
'desc' => str_replace("\n", '', ToolBox::fixEOL((strip_tags($aoTrack->desc)))),
'cmt' => ToolBox::fixEOL((strip_tags($aoTrack->cmt))),
'color' => (string) $aoTrack->extensions->children('gpxx', true)->TrackExtension->DisplayColor,
'points'=> array()
);
foreach($aoTrack->trkseg as $asSegment) {
foreach($asSegment as $asPoint) {
$asTrack['points'][] = array(
'lon' => (float) $asPoint['lon'],
'lat' => (float) $asPoint['lat'],
'ele' => (int) $asPoint->ele
);
}
}
$this->asTracks[] = $asTrack;
}
//Waypoints
$this->addNotice('Ignoring '.count($oXml->wpt).' waypoints');
}
}
}
class GeoJson extends Geo {
const EXT = '.geojson';
const MAX_FILESIZE = 2; //MB
const MAX_DEVIATION_FLAT = 0.1; //10%
const MAX_DEVIATION_ELEV = 0.1; //10%
public function __construct($sCodeName) {
parent::__construct($sCodeName);
}
public function saveFile() {
$this->addNotice('Saving '.$this->sFilePath);
file_put_contents($this->sFilePath, $this->buildGeoJson());
}
public function isSimplicationRequired() {
//Size in bytes
$iFileSize = strlen($this->buildGeoJson());
//Convert to MB
$iFileSize = round($iFileSize / pow(1024, 2), 2);
//Compare with max allowed size
$bFileTooLarge = ($iFileSize > self::MAX_FILESIZE);
if($bFileTooLarge) $this->addNotice('Output file is too large ('.$iFileSize.'MB > '.self::MAX_FILESIZE.'MB)');
return $bFileTooLarge;
}
public function buildTracks($asTracks, $bSimplify=false) {
$this->addNotice('Creating '.($bSimplify?'Simplified ':'').'GeoJson Tracks');
$iGlobalInvalidPointCount = 0;
$iGlobalPointCount = 0;
$this->asTracks = array();
foreach($asTracks as $asTrackProps) {
$asOptions = $this->parseOptions($asTrackProps['cmt']);
//Color mapping
switch($asTrackProps['color']) {
case 'DarkBlue':
$sType = 'main';
break;
case 'Magenta':
if($bSimplify && $asOptions[self::OPT_SIMPLE]!='keep') {
$this->addNotice('Ignoring Track "'.$asTrackProps['name'].' (off-track)');
continue 2; //discard tracks
}
else {
$sType = 'off-track';
break;
}
case 'Red':
$sType = 'hitchhiking';
break;
default:
$this->addNotice('Ignoring Track "'.$asTrackProps['name'].' (unknown color "'.$asTrackProps['color'].'")');
continue 2; //discard tracks
}
$asTrack = array(
'type' => 'Feature',
'properties' => array(
'name' => $asTrackProps['name'],
'type' => $sType,
'description' => $asTrackProps['desc']
),
'geometry' => array(
'type' => 'LineString',
'coordinates' => array()
)
);
//Track points
$asTrackPoints = $asTrackProps['points'];
$iPointCount = count($asTrackPoints);
$iInvalidPointCount = 0;
$asPrevPoint = array();
foreach($asTrackPoints as $iIndex=>$asPoint) {
$asNextPoint = ($iIndex < ($iPointCount - 1))?$asTrackPoints[$iIndex + 1]:array();
if($bSimplify && !empty($asPrevPoint) && !empty($asNextPoint)) {
if(!$this->isPointValid($asPrevPoint, $asPoint, $asNextPoint)) {
$iInvalidPointCount++;
continue;
}
}
$asTrack['geometry']['coordinates'][] = array_values($asPoint);
$asPrevPoint = $asPoint;
}
$this->asTracks[] = $asTrack;
$iGlobalInvalidPointCount += $iInvalidPointCount;
$iGlobalPointCount += $iPointCount;
if($iInvalidPointCount > 0) $this->addNotice('Removing '.$iInvalidPointCount.'/'.$iPointCount.' points ('.round($iInvalidPointCount / $iPointCount * 100, 1).'%) from '.$asTrackProps['name']);
}
if($bSimplify) $this->addNotice('Total: '.$iGlobalInvalidPointCount.'/'.$iGlobalPointCount.' points removed ('.round($iGlobalInvalidPointCount / $iGlobalPointCount * 100, 1).'%)');
}
public function sortOffTracks() {
$this->addNotice('Sorting off-tracks');
//Find first & last track points
$asTracksEnds = array();
$asTracks = array();
foreach($this->asTracks as $iTrackId=>$asTrack) {
$sTrackId = 't'.$iTrackId;
$asTracksEnds[$sTrackId] = array('first'=>reset($asTrack['geometry']['coordinates']), 'last'=>end($asTrack['geometry']['coordinates']));
$asTracks[$sTrackId] = $asTrack;
}
//Find variants close-by tracks
$asClonedTracks = $asTracks;
foreach($asClonedTracks as $sTrackId=>$asTrack) {
if($asTrack['properties']['type'] != 'off-track') continue;
$iMinDistance = INF;
$sConnectedTrackId = 0;
$iPosition = 0;
//Test all track ending points to find the closest
foreach($asTracksEnds as $sTrackEndId=>$asTrackEnds) {
if($sTrackEndId != $sTrackId) {
//Calculate distance between the last point of the track and every starting point of other tracks
$iDistance = self::getDistance($asTracksEnds[$sTrackId]['last'], $asTrackEnds['first']);
if($iDistance < $iMinDistance) {
$sConnectedTrackId = $sTrackEndId;
$iPosition = 0; //Track before the Connected Track
$iMinDistance = $iDistance;
}
//Calculate distance between the first point of the track and every ending point of other tracks
$iDistance = self::getDistance($asTracksEnds[$sTrackId]['first'], $asTrackEnds['last']);
if($iDistance < $iMinDistance) {
$sConnectedTrackId = $sTrackEndId;
$iPosition = +1; //Track after the Connected Track
$iMinDistance = $iDistance;
}
}
}
//Move track
unset($asTracks[$sTrackId]);
$iOffset = array_search($sConnectedTrackId, array_keys($asTracks)) + $iPosition;
$asTracks = array_slice($asTracks, 0, $iOffset) + array($sTrackId => $asTrack) + array_slice($asTracks, $iOffset);
}
$this->asTracks = array_values($asTracks);
}
private function parseOptions($sComment){
$sComment = strip_tags(html_entity_decode($sComment));
$asOptions = array(self::OPT_SIMPLE=>'');
foreach(explode("\n", $sComment) as $sLine) {
$asOptions[mb_strtolower(trim(mb_strstr($sLine, ':', true)))] = mb_strtolower(trim(mb_substr(mb_strstr($sLine, ':'), 1)));
}
return $asOptions;
}
private function isPointValid($asPointA, $asPointO, $asPointB) {
/* A----O Calculate angle AO^OB
* \ If angle is within [90% Pi ; 110% Pi], O can be discarded
* \ O is valid otherwise
* B
*/
//Path Turn Check -> -> -> ->
//Law of Cosines (vector): angle = arccos(OA.OB / ||OA||.||OB||)
$fVectorOA = array('lon'=>($asPointA['lon'] - $asPointO['lon']), 'lat'=> ($asPointA['lat'] - $asPointO['lat']));
$fVectorOB = array('lon'=>($asPointB['lon'] - $asPointO['lon']), 'lat'=> ($asPointB['lat'] - $asPointO['lat']));
$fLengthOA = sqrt(pow($asPointA['lon'] - $asPointO['lon'], 2) + pow($asPointA['lat'] - $asPointO['lat'], 2));
$fLengthOB = sqrt(pow($asPointO['lon'] - $asPointB['lon'], 2) + pow($asPointO['lat'] - $asPointB['lat'], 2));
$fVectorOAxOB = $fVectorOA['lon'] * $fVectorOB['lon'] + $fVectorOA['lat'] * $fVectorOB['lat'];
$fAngleAOB = ($fLengthOA != 0 && $fLengthOB != 0) ? acos($fVectorOAxOB/($fLengthOA * $fLengthOB)) : 0;
//Elevation Check
//Law of Cosines: angle = arccos((OB² + AO² - AB²) / (2*OB*AO))
$fLengthAB = sqrt(pow($asPointB['ele'] - $asPointA['ele'], 2) + pow($fLengthOA + $fLengthOB, 2));
$fLengthAO = sqrt(pow($asPointO['ele'] - $asPointA['ele'], 2) + pow($fLengthOA, 2));
$fLengthOB = sqrt(pow($asPointB['ele'] - $asPointO['ele'], 2) + pow($fLengthOB, 2));
$fAngleAOBElev = ($fLengthOB != 0 && $fLengthAO != 0) ? (acos((pow($fLengthOB, 2) + pow($fLengthAO, 2) - pow($fLengthAB, 2)) / (2 * $fLengthOB * $fLengthAO))) : 0;
return ($fAngleAOB <= (1 - self::MAX_DEVIATION_FLAT) * M_PI || $fAngleAOB >= (1 + self::MAX_DEVIATION_FLAT) * M_PI ||
$fAngleAOBElev <= (1 - self::MAX_DEVIATION_ELEV) * M_PI || $fAngleAOBElev >= (1 + self::MAX_DEVIATION_ELEV) * M_PI);
}
private function buildGeoJson() {
return json_encode(array('type'=>'FeatureCollection', 'features'=>$this->asTracks));
}
private static function getDistance($asPointA, $asPointB) {
$fLatFrom = $asPointA[1];
$fLonFrom = $asPointA[0];
$fLatTo = $asPointB[1];
$fLonTo = $asPointB[0];
$fRad = M_PI / 180;
//Calculate distance from latitude and longitude
$fTheta = $fLonFrom - $fLonTo;
$fDistance = sin($fLatFrom * $fRad) * sin($fLatTo * $fRad) + cos($fLatFrom * $fRad) * cos($fLatTo * $fRad) * cos($fTheta * $fRad);
return acos($fDistance) / $fRad * 60 * 1.853;
}
}

110
lib/Email.php Normal file
View File

@@ -0,0 +1,110 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Mask;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use \Settings;
class Email extends PhpObject {
private $sServName;
private $sTemplateName;
/**
* Email Template
* @var Mask
*/
public $oTemplate;
private $asDests;
public function __construct($sServName, $sTemplateName='') {
parent::__construct(__CLASS__);
$this->sServName = $sServName;
$this->setTemplate($sTemplateName);
$this->asDests = array();
}
public function setTemplate($sTemplateName) {
$this->sTemplateName = $sTemplateName;
$this->oTemplate = new Mask($this->sTemplateName);
$this->oTemplate->setTag('local_server', $this->sServName);
}
/**
* Set Target User Info
* @param array $asDests Contains: id_user, name, email, language, timezone, active
*/
public function setDestInfo($asDests) {
if(array_key_exists('email', $asDests)) $asDests = array($asDests);
$this->asDests = $asDests;
}
public function send() {
$oPHPMailer = new PHPMailer(true);
//Server settings
if(Settings::DEBUG) $oPHPMailer->SMTPDebug = SMTP::DEBUG_SERVER;//Enable verbose debug output
$oPHPMailer->isSMTP(); //Send using SMTP
$oPHPMailer->CharSet = Settings::TEXT_ENC; //Mail Character Set
$oPHPMailer->Encoding = 'base64'; //Base 64 Character Encoding
$oPHPMailer->Host = Settings::MAIL_SERVER; //Set the SMTP server to send through
$oPHPMailer->SMTPAuth = true; //Enable SMTP authentication
$oPHPMailer->Username = Settings::MAIL_USER; //SMTP username
$oPHPMailer->Password = Settings::MAIL_PASS; //SMTP password
$oPHPMailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$oPHPMailer->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$oPHPMailer->setFrom(Settings::MAIL_FROM, 'Spotty');
$oPHPMailer->addReplyTo(Settings::MAIL_FROM, 'Spotty');
foreach($this->asDests as $asDest) {
//Message
$this->oTemplate->setLanguage($asDest['language'], Spot::DEFAULT_LANG);
$this->oTemplate->setTimezone($asDest['timezone']);
//Unsubscribe Link
$sUnsubLink = $this->sServName.'?a=unsubscribe_email&id='.$asDest['id_user'];
$this->oTemplate->setTag('unsubscribe_link', htmlspecialchars($sUnsubLink));
$oPHPMailer->addCustomHeader('List-Unsubscribe','<mailto:'.Settings::MAIL_FROM.'?subject=unsubscribe>, <'.$sUnsubLink.'>');
$oPHPMailer->addCustomHeader('List-Unsubscribe-Post','List-Unsubscribe=One-Click');
//Email Content
$this->oTemplate->setTag('timezone', 'lang:city_time', self::getTimeZoneCity($asDest['timezone']));
$sHtmlMessage = $this->oTemplate->getMask();
$sPlainMessage = strip_tags(str_replace('<br />', "\n", $sHtmlMessage));
//Recipients
try {
$oPHPMailer->addAddress($asDest['email'], $asDest['name']);
} catch (Exception $oError) {
$this->addError('Invalid address skipped: '.$asDest['email'].' ('.$asDest['name'].')');
continue;
}
//Content
$oPHPMailer->isHTML(true);
$oPHPMailer->Subject = $this->oTemplate->getTranslator()->getTranslation($this->sTemplateName.'_subject');
$oPHPMailer->Body = $sHtmlMessage;
$oPHPMailer->AltBody = $sPlainMessage;
$bSuccess = true;
try {
$bSuccess = $bSuccess && $oPHPMailer->send();
}
catch (Exception $oError) {
$this->addError('Message could not be sent to "'.$asDest['email'].'". Mailer Error: '.$oPHPMailer->ErrorInfo);
$oPHPMailer->getSMTPInstance()->reset();
}
$oPHPMailer->clearAddresses();
$oPHPMailer->clearCustomHeaders();
}
return $bSuccess;
}
private static function getTimeZoneCity($sTimeZone) {
return (strpos($sTimeZone, '/')!==false)?str_replace('_', ' ', explode('/', $sTimeZone)[1]):$sTimeZone;
}
}

309
lib/Feed.php Normal file
View File

@@ -0,0 +1,309 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Db;
use Franzz\Objects\Translator;
use \Settings;
/**
* Feed Class
* Also manages spots (devices) & messages
*/
class Feed extends PhpObject {
//Spot feed
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';
const FEED_MAX_REFRESH = 5 * 60; //Seconds
//Weather
const WEATHER_HOOK = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline';
const WEATHER_PARAM = array(
'key' => Settings::WEATHER_TOKEN,
'unitGroup' => 'metric',
'lang' => 'en',
'include' => 'current',
'iconSet' => 'icons2'
);
//Timezone
const TIMEZONE_HOOK = 'http://api.geonames.org/timezoneJSON';
const TIMEZONE_PARAM = array(
'username' => Settings::TIMEZONE_USER
);
//DB Tables
const SPOT_TABLE = 'spots';
const FEED_TABLE = 'feeds';
const MSG_TABLE = 'messages';
//Hide/Display values
const MSG_HIDDEN = 0;
const MSG_DISPLAYED = 1;
/**
* Database Handle
* @var Db
*/
private $oDb;
private $iFeedId;
private $sRefFeedId;
private $iLastUpdate;
public function __construct(Db &$oDb, $iFeedId=0) {
parent::__construct(__CLASS__);
$this->oDb = &$oDb;
if($iFeedId > 0) $this->setFeedId($iFeedId);
}
public function getFeedId() {
return $this->iFeedId;
}
public function getLastUpdate(): int {
return $this->iLastUpdate;
}
public function setFeedId($iFeedId) {
$this->iFeedId = $iFeedId;
$asFeed = $this->getFeed();
$this->sRefFeedId = $asFeed['ref_feed_id'];
$this->iLastUpdate = $asFeed['last_update']=='0000-00-00 00:00:00'?0:strtotime($asFeed['last_update']);
}
public function createFeedId($oProjectId) {
$this->setFeedId($this->oDb->insertRow(self::FEED_TABLE, array(
Db::getId(Project::PROJ_TABLE) => $oProjectId,
'status' => 'INACTIVE'
)));
return $this->getFeedId();
}
public function setRefFeedId($sRefFeedId) {
return $this->updateField('ref_feed_id', $sRefFeedId);
}
public function setSpotId($iSpotId) {
return $this->updateField(Db::getId(self::SPOT_TABLE), $iSpotId);
}
public function setProjectId($iProjectId) {
return $this->updateField(Db::getId(Project::PROJ_TABLE), $iProjectId);
}
public function getSpots() {
$asSpots = $this->oDb->selectRows(array('from'=>self::SPOT_TABLE));
foreach($asSpots as &$asSpot) $asSpot['id'] = $asSpot[Db::getId(self::SPOT_TABLE)];
return $asSpots;
}
public function getFeeds($iFeedId=0) {
$asInfo = array('from'=>self::FEED_TABLE);
if($iFeedId > 0) $asInfo['constraint'] = array(Db::getId(self::FEED_TABLE)=>$iFeedId);
$asFeeds = $this->oDb->selectRows($asInfo);
foreach($asFeeds as &$asFeed) $asFeed['id'] = $asFeed[Db::getId(self::FEED_TABLE)];
return $asFeeds;
}
public function getFeed() {
$asFeeds = $this->getFeeds($this->getFeedId());
return array_shift($asFeeds);
}
public function getMessages($asConstraints=array()) {
$sFeedIdCol = Db::getId(self::FEED_TABLE, true);
$asInfo = array(
'select' => array(
Db::getId(self::MSG_TABLE), 'ref_msg_id', 'type', //ID
'latitude', 'longitude', //Position
'site_time', 'timezone', 'unix_time', //Time
'weather_icon', 'weather_cond', 'weather_temp' //Weather
),
'from' => self::MSG_TABLE,
'join' => array(self::FEED_TABLE => Db::getId(self::FEED_TABLE)),
'constraint'=> array($sFeedIdCol => $this->getFeedId(), 'display' => self::MSG_DISPLAYED),
'constOpe' => array($sFeedIdCol => "=", 'display' => "="),
'orderBy' => array('site_time'=>'ASC')
);
if(!empty($asConstraints)) $asInfo = array_merge($asInfo, $asConstraints);
$asResult = $this->oDb->selectRows($asInfo);
/* Temporary lookup - Start */
$iCount = 0;
foreach($asResult as &$asMsg) {
if($asMsg['weather_icon'] == '' && $iCount < 3) {
$asWeather = $this->getWeather(array($asMsg['latitude'], $asMsg['longitude']), $asMsg['unix_time']);
$asMsg = array_merge($asMsg, $asWeather);
$this->oDb->updateRow(self::MSG_TABLE, $asMsg[Db::getId(self::MSG_TABLE)], $asWeather, false);
$iCount++;
}
}
/* Temporary lookup - End */
return $asResult;
}
public function getLastMessageId($asConstraints=array()) {
$asMessages = $this->getMessages($asConstraints);
return end($asMessages)[Db::getId(self::MSG_TABLE)] ?? 0;
}
public function checkUpdateFeed($sProjectMode) {
$bNewMsg = false;
//Spam Check: no more than 1 API request per 5 minutes
if($sProjectMode == Project::MODE_BLOG) {
$oLastUpdate = new \DateTime('@'.$this->iLastUpdate);
$oNow = new \DateTime('now');
$iSecDiff = $oNow->getTimestamp() - $oLastUpdate->getTimestamp();
if($iSecDiff > self::FEED_MAX_REFRESH) $bNewMsg = $this->updateFeed();
}
return $bNewMsg;
}
private function updateFeed() {
$bNewMsg = false;
$asData = $this->retrieveFeed();
$sNow = date(Db::TIMESTAMP_FORMAT);
if(!isset($asData['response']['errors']) || empty($asData['response']['errors'])) {
$asMsgs = $asData['response']['feedMessageResponse']['messages'];
$asFeed = $asData['response']['feedMessageResponse']['feed'];
//Fix unstable Spot API Structure
if(array_key_exists('message', $asMsgs)) $asMsgs = $asMsgs['message']; //Sometimes adds an extra "message" level
if(!array_key_exists(0, $asMsgs)) $asMsgs = array($asMsgs); //Jumps a level when there is only 1 message
//Update Spot, Feed & Messages
if(!empty($asMsgs) && array_key_exists('messengerId', $asMsgs[0])) {
//Update Spot Info from the first message
$asSpotInfo = array(
'ref_spot_id' => $asMsgs[0]['messengerId'],
'name' => $asMsgs[0]['messengerName'],
'model' => $asMsgs[0]['modelId']
);
$iSpotId = $this->oDb->insertUpdateRow(self::SPOT_TABLE, $asSpotInfo, array('ref_spot_id'));
//Update Feed Info and last update date
$asFeedInfo = array(
'ref_feed_id' => $asFeed['id'],
Db::getId(self::SPOT_TABLE) => $iSpotId,
'name' => $asFeed['name'],
'description' => $asFeed['description'],
'status' => $asFeed['status'],
'last_update' => $sNow
);
$iFeedId = $this->oDb->insertUpdateRow(self::FEED_TABLE, $asFeedInfo, array('ref_feed_id'));
//Update Messages
foreach($asMsgs as $asMsg) {
$asMsg = array(
'ref_msg_id' => $asMsg['id'],
Db::getId(self::FEED_TABLE) => $iFeedId,
'type' => $asMsg['messageType'],
'latitude' => $asMsg['latitude'],
'longitude' => $asMsg['longitude'],
'iso_time' => $asMsg['dateTime'], //ISO 8601 time (backup)
'site_time' => date(Db::TIMESTAMP_FORMAT, $asMsg['unixTime']), //Conversion to Site Time
'timezone' => $this->getTimeZone(array($asMsg['latitude'], $asMsg['longitude']), $asMsg['unixTime']),
'unix_time' => $asMsg['unixTime'], //UNIX Time (backup)
'content' => $asMsg['messageContent'],
'battery_state' => $asMsg['batteryState']
);
$iMsgId = $this->oDb->selectId(self::MSG_TABLE, array('ref_msg_id'=>$asMsg['ref_msg_id']));
if(!$iMsgId) {
//First Catch
$asMsg['posted_on'] = $sNow;
//Weather Data
$asMsg = array_merge($asMsg, $this->getWeather(array($asMsg['latitude'], $asMsg['longitude']), $asMsg['unix_time']));
$this->oDb->insertRow(self::MSG_TABLE, $asMsg);
$bNewMsg = true;
}
else $this->oDb->updateRow(self::MSG_TABLE, $iMsgId, $asMsg);
}
}
}
else $this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array('last_update'=>$sNow));
return $bNewMsg;
}
private function getWeather($asLatLng, $iTimeStamp) {
$sApiUrl = self::WEATHER_HOOK.'/'.$asLatLng[0].','.$asLatLng[1].'/'.$iTimeStamp.'?'.http_build_query(self::WEATHER_PARAM);
$asWeather = json_decode(file_get_contents($sApiUrl), true);
if(array_key_exists('currentConditions', $asWeather)) { //Current conditions
$sWeatherIcon = $asWeather['currentConditions']['icon'];
$sWeatherCond = $asWeather['currentConditions']['conditions'];
$sWeatherTemp = $asWeather['currentConditions']['temp'];
}
elseif($asWeather['days'][0]['icon'] != '') { //Daily Conditions
$sWeatherIcon = $asWeather['days'][0]['icon'];
$sWeatherCond = $asWeather['days'][0]['conditions'];
$sWeatherTemp = $asWeather['days'][0]['temp'];
}
else {
$sWeatherIcon = 'unknown';
}
//Get Condition ID
$sCondKey = (new Translator(self::WEATHER_PARAM['lang']))->getTranslationKey($sWeatherCond);
return array(
'weather_icon' => $sWeatherIcon,
'weather_cond' => $sCondKey,
'weather_temp' => floatval($sWeatherTemp)
);
}
private function getTimeZone($asLatLng, $iTimeStamp) {
$asParams = self::TIMEZONE_PARAM;
$asParams['lat'] = $asLatLng[0];
$asParams['lng'] = $asLatLng[1];
$sApiUrl = self::TIMEZONE_HOOK.'?'.http_build_query($asParams);
$asTimeZone = json_decode(file_get_contents($sApiUrl), true);
return $asTimeZone['timezoneId'] ?? Settings::TIMEZONE;
}
private function retrieveFeed() {
$sContent = '[]';
if($this->sRefFeedId !='') {
$sUrl = self::FEED_HOOK.$this->sRefFeedId.self::FEED_TYPE_JSON;
$sContent = file_get_contents($sUrl);
}
return json_decode($sContent, true);
}
private function updateField($sField, $oValue) {
$bResult = ($this->oDb->updateRow(self::FEED_TABLE, $this->getFeedId(), array($sField=>$oValue)) > 0);
$this->setFeedId($this->getFeedId());
return $bResult;
}
public function delete() {
$asResult = array();
if($this->getFeedId() > 0) {
$asResult = array(
'id' => $this->getFeedId(),
'del' => $this->oDb->deleteRow(self::FEED_TABLE, $this->getFeedId()),
'desc' => $this->oDb->getLastError()
);
}
else $asResult = array('del'=>false, 'desc'=>'Error while setting project: no Feed ID');
return $asResult;
}
}

45
lib/Map.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Db;
use \Settings;
class Map extends PhpObject {
const MAP_TABLE = 'maps';
const MAPPING_TABLE = 'mappings';
private Db $oDb;
private $asMaps;
public function __construct(Db &$oDb) {
parent::__construct(__CLASS__);
$this->oDb = &$oDb;
$this->setMaps();
}
private function setMaps() {
$asMaps = $this->oDb->selectRows(array('from'=>self::MAP_TABLE));
foreach($asMaps as $asMap) $this->asMaps[$asMap['codename']] = $asMap;
}
public function getProjectMaps($iProjectId) {
$asMappings = $this->oDb->getArrayQuery("SELECT id_map FROM mappings WHERE id_project = ".$iProjectId." OR id_project IS NULL", true);
return array_filter($this->asMaps, function($asMap) use($asMappings) {return in_array($asMap['id_map'], $asMappings);});
}
public function getMapUrl($sCodeName, $asParams) {
$asParams['token'] = $this->asMaps[$sCodeName]['token'];
return self::populateParams($this->asMaps[$sCodeName]['pattern'], $asParams);
}
private static function populateParams($sUrl, $asParams) {
foreach($asParams as $sParam=>$sValue) {
$sUrl = str_replace('{'.$sParam.'}', $sValue, $sUrl);
}
return $sUrl;
}
}

339
lib/Media.php Normal file
View File

@@ -0,0 +1,339 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Db;
use Franzz\Objects\ToolBox;
use \Settings;
class Media extends PhpObject {
//DB Tables
const MEDIA_TABLE = 'medias';
//Media folders
const MEDIA_FOLDER = '../files/';
const THUMB_FOLDER = self::MEDIA_FOLDER.'thumbs/';
const THUMB_MAX_WIDTH = 400;
/**
* Database Handle
* @var Db
*/
private $oDb;
/**
* Media Project
* @var Project
*/
private $oProject;
private $asMedia;
private $asMedias;
private $sSystemType;
private $iMediaId;
public function __construct(Db &$oDb, &$oProject, $iMediaId=0) {
parent::__construct(__CLASS__);
$this->oDb = &$oDb;
$this->oProject = &$oProject;
$this->asMedia = array();
$this->asMedias = array();
$this->sSystemType = (substr(php_uname(), 0, 7) == "Windows")?'win':'unix';
$this->setMediaId($iMediaId);
}
public function setMediaId($iMediaId) {
$this->iMediaId = $iMediaId;
}
public function getMediaId() {
return $this->iMediaId;
}
public function getProjectCodeName() {
return $this->oProject->getProjectCodeName();
}
public function setComment($sComment) {
$sError = '';
$asData = array();
if($this->iMediaId > 0) {
$bResult = $this->oDb->updateRow(self::MEDIA_TABLE, $this->iMediaId, array('comment'=>$sComment));
if(!$bResult) $sError = 'error_commit_db';
else $asData = $this->getInfo();
}
else $sError = 'media_no_id';
return Spot::getResult(($sError==''), $sError, $asData);
}
public function getMediasInfo($oMediaIds=null) {
$bOwnMedia = is_numeric($oMediaIds); //1 value
$bConstraintArray = is_array($oMediaIds); //Custom Constraints
if($bOwnMedia && empty($this->asMedia) || !$bOwnMedia && empty($this->asMedias) || $bConstraintArray) {
if($this->oProject->getProjectId()) {
$asParams = array(
'select' => array(Db::getId(self::MEDIA_TABLE), 'filename', 'taken_on', 'posted_on', 'timezone', 'latitude', 'longitude', 'altitude', 'width', 'height', 'rotate', 'type AS subtype', 'comment'),
'from' => self::MEDIA_TABLE,
'constraint'=> array(Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId())
);
if($bOwnMedia) $asParams['constraint'][Db::getId(self::MEDIA_TABLE)] = $oMediaIds;
if($bConstraintArray) $asParams = array_merge($asParams, $oMediaIds);
$asMedias = $this->oDb->selectRows($asParams);
foreach($asMedias as &$asMedia) {
$asMedia['media_path'] = self::getMediaPath($asMedia['filename']);
$asMedia['thumb_path'] = $this->getMediaThumbnail($asMedia['filename']);
}
if(!empty($asMedias) && !$bConstraintArray) {
if($bOwnMedia) $this->asMedia = array_shift($asMedias);
else $this->asMedias = $asMedias;
}
}
}
return $bOwnMedia?$this->asMedia:$asMedias;
}
public function getInfo() {
return $this->getMediasInfo($this->iMediaId);
}
public function isProjectEditable() {
return $this->oProject->isEditable();
}
public function addMedia($sMediaName, $sMethod='upload') {
$sError = '';
$asParams = array();
if(!$this->isProjectEditable() && $sMethod!='sync') {
$sError = 'upload_mode_archived';
$asParams[] = $this->oProject->getProjectCodeName();
}
elseif($this->oDb->pingValue(self::MEDIA_TABLE, array('filename'=>$sMediaName)) && $sMethod!='sync') {
$sError = 'upload_media_exist';
$asParams[] = $sMediaName;
}
else {
$asMediaInfo = $this->getMediaInfoFromFile($sMediaName);
//Converting times to Site Time Zone, by using date()
//Media Timezone is kept in a separate field for later conversion to Local Time
$asDbInfo = array(
Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId(),
'filename' => $sMediaName,
'taken_on' => date(Db::TIMESTAMP_FORMAT, ($asMediaInfo['taken_ts'] > 0)?$asMediaInfo['taken_ts']:$asMediaInfo['file_ts']),
'posted_on' => date(Db::TIMESTAMP_FORMAT, $asMediaInfo['file_ts']),
'timezone' => $asMediaInfo['timezone'],
'latitude' => is_null($asMediaInfo['latitude'])?'NULL':$asMediaInfo['latitude'],
'longitude' => is_null($asMediaInfo['longitude'])?'NULL':$asMediaInfo['longitude'],
'altitude' => is_null($asMediaInfo['altitude'])?'NULL':$asMediaInfo['altitude'],
'width' => $asMediaInfo['width'],
'height' => $asMediaInfo['height'],
'rotate' => $asMediaInfo['rotate'],
'type' => $asMediaInfo['type']
);
if($sMethod=='sync') $iMediaId = $this->oDb->insertUpdateRow(self::MEDIA_TABLE, $asDbInfo, array('filename'));
else $iMediaId = $this->oDb->insertRow(self::MEDIA_TABLE, $asDbInfo);
if(!$iMediaId) $sError = 'error_commit_db';
else {
$this->setMediaId($iMediaId);
$asParams = $this->getInfo(); //Creates thumbnail
}
}
return Spot::getResult(($sError==''), $sError, $asParams);
}
private function getMediaInfoFromFile($sMediaName)
{
$sMediaPath = self::getMediaPath($sMediaName);
$sType = self::getMediaType($sMediaName);
$iPostedOn = filemtime($sMediaPath);
$sTimeZone = date_default_timezone_get();
$iWidth = 0;
$iHeight = 0;
$sRotate = '0';
$sTakenOn = '';
$fLat = null;
$fLng = null;
$iAlt = null;
switch($sType) {
case 'video':
$asResult = array();
$sParams = implode(' ', array(
'-loglevel error', //Remove comments
'-select_streams v:0', //First video channel
'-show_entries '. //filter tags : Width, Height, Creation Time, Location & Rotation
'format_tags=creation_time,com.apple.quicktime.creationdate,com.apple.quicktime.location.ISO6709:'.
'stream_tags=rotate,creation_time:'.
'stream=width,height',
'-print_format json', //output format: json
'-i' //input file
));
exec('ffprobe '.$sParams.' "'.$sMediaPath.'"', $asResult);
$asExif = json_decode(implode('', $asResult), true);
//Taken On
if(isset($asExif['format']['tags']['com.apple.quicktime.creationdate'])) {
$sTakenOn = $asExif['format']['tags']['com.apple.quicktime.creationdate']; //contains Time Zone
$sTimeZone = Spot::getTimeZoneFromDate($sTakenOn) ?? $sTimeZone;
}
else $sTakenOn = $asExif['format']['tags']['creation_time'] ?? $asExif['streams'][0]['tags']['creation_time'];
//Location
if(isset($asExif['format']['tags']['com.apple.quicktime.location.ISO6709'])) {
list($fLat, $fLng, $iAlt) = self::getLatLngAltFromISO6709($asExif['format']['tags']['com.apple.quicktime.location.ISO6709']);
}
//Width & Height
$iWidth = $asExif['streams'][0]['width'];
$iHeight = $asExif['streams'][0]['height'];
//Orientation
if(isset($asExif['streams'][0]['tags']['rotate'])) $sRotate = $asExif['streams'][0]['tags']['rotate'];
break;
case 'image':
$asExif = @exif_read_data($sMediaPath, 0, true);
if($asExif === false) $asExif = array();
list($iWidth, $iHeight) = getimagesize($sMediaPath);
//Posted On
if(array_key_exists('FILE', $asExif) && array_key_exists('FileDateTime', $asExif['FILE'])) $iPostedOn = $asExif['FILE']['FileDateTime'];
//Taken On & Timezone
if(array_key_exists('EXIF', $asExif)) {
if(array_key_exists('DateTimeOriginal', $asExif['EXIF'])) $sTakenOn = $asExif['EXIF']['DateTimeOriginal'];
/* Priorities:
* 1. OffsetTimeOriginal: timezone for DateTimeOriginal (exif version >= 2.31)
* 2. 0x9011: same as above, but unidentified
* 3. Timezone extracted from DateTimeOriginal
* 4. Uploader Browser Time Zone (PHP Default Time Zone)
*/
$sTimeZone = $asExif['EXIF']['OffsetTimeOriginal'] ?? $asExif['EXIF']['UndefinedTag:0x9011'] ?? Spot::getTimeZoneFromDate($sTakenOn) ?? $sTimeZone;
}
//Location
if(array_key_exists('GPS', $asExif)) {
$asGps = $asExif['GPS'];
$fLat = self::getLatLngFromExif($asGps['GPSLatitudeRef'] ?? $asGps['UndefinedTag:0x0001'], $asGps['GPSLatitude'] ?? $asGps['UndefinedTag:0x0002']);
$fLng = self::getLatLngFromExif($asGps['GPSLongitudeRef'] ?? $asGps['UndefinedTag:0x0003'], $asGps['GPSLongitude'] ?? $$asGps['UndefinedTag:0x0004']);
$iAlt = (($asGps['GPSAltitudeRef'] ?? $asGps['UndefinedTag:0x0005'] ?? '0') == '1'?-1:1) * ($asGps['GPSAltitude'] ?? $asGps['UndefinedTag:0x0006'] ?? 0);
}
//Orientation
if(array_key_exists('IFD0', $asExif) && array_key_exists('Orientation', $asExif['IFD0'])) {
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
}
}
break;
}
//Assign the correct Time Zone to $sTakenOn if it is not already contained in it. Then get Unix Timestamp
//Time Zone (2nd parameter) will be ignored if already contained in $sTakenOn
$iTakenOn = 0;
if($sTakenOn != '') {
$oTakenOn = new \DateTime($sTakenOn, new \DateTimeZone($sTimeZone));
$iTakenOn = $oTakenOn->format('U');
}
return array(
'timezone' => $sTimeZone,
'latitude' => $fLat,
'longitude' => $fLng,
'altitude' => $iAlt,
'taken_ts' => $iTakenOn,
'file_ts' => $iPostedOn,
'width' => $iWidth,
'height' => $iHeight,
'rotate' => $sRotate,
'type' => $sType
);
}
private function getMediaThumbnail($sMediaName)
{
$sMediaPath = self::getMediaPath($sMediaName);
$sThumbPath = self::getMediaPath($sMediaName, 'thumbnail');
if(!file_exists($sThumbPath)) {
$sType = self::getMediaType($sMediaName);
switch($sType) {
case 'image':
$asThumbInfo = ToolBox::createThumbnail($sMediaPath, self::THUMB_MAX_WIDTH, 0, $sThumbPath);
break;
case 'video':
//Get a screenshot of the video 1 second in
$sTempPath = self::getMediaPath(uniqid('temp_').'.png');
$asResult = array();
$sParams = implode(' ', array(
'-i "'.$sMediaPath.'"', //input file
'-ss 00:00:01.000', //Image taken after x seconds
'-vframes 1', //number of video frames to output
'"'.$sTempPath.'"', //output file
));
exec('ffmpeg '.$sParams, $asResult);
//Resize
$asThumbInfo = ToolBox::createThumbnail($sTempPath, self::THUMB_MAX_WIDTH, 0, $sThumbPath, true);
break;
}
}
else $asThumbInfo = array('error'=>'', 'out'=>$sThumbPath);
return ($asThumbInfo['error']=='')?$asThumbInfo['out']:$sMediaPath;
}
private static function getMediaPath($sMediaName, $sFileType='media') {
if($sFileType=='thumbnail') return self::THUMB_FOLDER.$sMediaName.(strtolower(substr($sMediaName, -3))=='mov'?'.png':'');
else return self::MEDIA_FOLDER.$sMediaName;
}
private static function getMediaType($sMediaName) {
$sMediaPath = self::getMediaPath($sMediaName);
$sMediaMime = mime_content_type($sMediaPath);
switch($sMediaMime) {
case 'video/quicktime': $sType = 'video'; break;
default: $sType = 'image'; break;
}
return $sType;
}
private static function getLatLngFromExif($sDirection, $asCoords) {
$fCoord = 0;
foreach($asCoords as $iIndex=>$sCoord) {
$fValue = 0;
$asCoordParts = explode('/', $sCoord);
switch(count($asCoordParts)) {
case 1:
$fValue = $asCoordParts[0];
break;
case 2:
$fValue = floatval($asCoordParts[0]) / floatval($asCoordParts[1]);
break;
}
$fCoord += $fValue / pow(60, $iIndex);
}
return $fCoord * (($sDirection == 'W' || $sDirection == 'S')?-1:1);
}
private static function getLatLngAltFromISO6709($sIso6709) {
preg_match('/^(?P<lat>[\+\-][0,1]?\d{2}\.\d+)(?P<lng>[\+\-][0,1]?\d{2}\.\d+)(?P<alt>[\+\-]\d+)?/', $sIso6709, $asMatches);
return array(floatval($asMatches['lat']), floatval($asMatches['lng']), floatval($asMatches['alt'] ?? 0));
}
}

226
lib/Project.php Normal file
View File

@@ -0,0 +1,226 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Db;
use \Settings;
class Project extends PhpObject {
//Spot Mode
const MODE_PREVIZ = 'P';
const MODE_BLOG = 'B';
const MODE_HISTO = 'H';
const MODES = array('previz'=>self::MODE_PREVIZ, 'blog'=>self::MODE_BLOG, 'histo'=>self::MODE_HISTO);
//DB Tables
const PROJ_TABLE = 'projects';
/**
* Database Handle
* @var Db
*/
private $oDb;
private $iProjectId;
private $sName;
private $sCodeName;
private $sMode;
private $asActive;
private $asGeo;
public function __construct(Db &$oDb, $iProjectId=0) {
parent::__construct(__CLASS__);
$this->oDb = &$oDb;
if($iProjectId > 0) $this->setProjectId($iProjectId);
}
public function getProjectId() {
return $this->iProjectId;
}
public function setProjectId($iProjectId=0) {
if($iProjectId > 0) {
$this->iProjectId = $iProjectId;
}
else {
/**
* Project 1 [-----------------]
* Project 2 [---------------------------]
* Project 3 [-----------]
* Selected Project [-------Project 1-------][------------Project 2-------------][---------------Project 3------------------
* Mode --P--][--------B--------][--P--][-----------B---------------][---P---][-----B-----][---------H----------
*/
$sQuery =
"SELECT MAX(id_project) ".
"FROM projects ".
"WHERE active_to = (".
"SELECT MIN(active_to) ". //Select closest project in the future
"FROM projects ".
"WHERE active_to > NOW() ". //Select Next project
"OR active_to = (". //In case there is no next project, select the last one
"SELECT MAX(active_to) ".
"FROM projects".
")".
")";
$asResult = $this->oDb->getArrayQuery($sQuery, true);
$this->iProjectId = array_shift($asResult);
}
$this->setProjectInfo();
}
public function createProjectId() {
$this->setProjectId($this->oDb->insertRow(self::PROJ_TABLE, array('codename'=>'')));
return $this->getProjectId();
}
public function getMode() {
return $this->sMode;
}
public function getProjectName() {
return $this->sName;
}
public function setProjectName($sName) {
return $this->updateField('name', $sName);
}
public function getProjectCodeName() {
return $this->sCodeName;
}
public function setProjectCodeName($sCodeName) {
return $this->updateField('codename', $sCodeName);
}
public function getActivePeriod($sFromTo='') {
return ($sFromTo=='')?$this->asActive:$this->asActive[$sFromTo];
}
public function setActivePeriod($oValue, $sFromTo='') {
if($sFromTo=='') {
$this->updateField('active_from', $oValue['from']);
return $this->updateField('active_to', $oValue['to']);
}
else {
return $this->updateField('active_'.$sFromTo, $oValue);
}
}
public function getFeedIds() {
return $this->oDb->selectColumn(
Feed::FEED_TABLE,
Db::getId(Feed::FEED_TABLE),
array(Db::getId(self::PROJ_TABLE) => $this->getProjectId())
);
}
public function getProjects($iProjectId=0) {
$bSpecificProj = ($iProjectId > 0);
$asInfo = array(
'select'=> array(
Db::getId(self::PROJ_TABLE)." AS id",
'codename',
'name',
'active_from',
'active_to',
"IF(NOW() BETWEEN active_from AND active_to, 1, IF(NOW() < active_from, 0, 2)) AS mode"
),
'from' => self::PROJ_TABLE
);
if($bSpecificProj) $asInfo['constraint'] = array(Db::getId(self::PROJ_TABLE)=>$iProjectId);
$asProjects = $this->oDb->selectRows($asInfo, 'codename');
foreach($asProjects as $sCodeName=>&$asProject) {
switch($asProject['mode']) {
case 0: $asProject['mode'] = self::MODE_PREVIZ; break;
case 1: $asProject['mode'] = self::MODE_BLOG; break;
case 2: $asProject['mode'] = self::MODE_HISTO; break;
}
$asProject['editable'] = $this->isModeEditable($asProject['mode']);
if($sCodeName != '' && !Converter::isGeoJsonValid($sCodeName)) Converter::convertToGeoJson($sCodeName);
$asProject['geofilepath'] = Spot::addTimestampToFilePath(GeoJson::getDistFilePath($sCodeName));
$asProject['gpxfilepath'] = Spot::addTimestampToFilePath(Gpx::getDistFilePath($sCodeName));
$asProject['codename'] = $sCodeName;
}
return $bSpecificProj?$asProject:$asProjects;
}
public function getProject() {
return $this->getProjects($this->getProjectId());
}
public function getLastUpdate(): int {
$iLastUpdate = INF;
$asFeedIds = $this->getFeedIds();
foreach($asFeedIds as $iFeedId) {
$iLastUpdate = min($iLastUpdate, (new Feed($this->oDb, $iFeedId))->getLastUpdate());
}
return $iLastUpdate;
}
public function getLastMessageId($asConstraints=array()): int {
$iLastMsg = 0;
$asFeedIds = $this->getFeedIds();
foreach($asFeedIds as $iFeedId) {
$iLastMsg = max($iLastMsg, (new Feed($this->oDb, $iFeedId))->getLastMessageId($asConstraints));
}
return $iLastMsg;
}
private function setProjectInfo() {
if($this->getProjectId() > 0) {
$asProject = $this->getProject();
$this->sName = $asProject['name'];
$this->sCodeName = $asProject['codename'];
$this->sMode = $asProject['mode'];
$this->asActive = array('from'=>$asProject['active_from'], 'to'=>$asProject['active_to']);
$this->asGeo = array('geofile'=>$asProject['geofilepath'], 'gpxfile'=>$asProject['gpxfilepath']);
}
else $this->addError('Error while setting project: no project ID');
}
private function updateField($sField, $oValue) {
$bResult = ($this->oDb->updateRow(self::PROJ_TABLE, $this->getProjectId(), array($sField=>$oValue)) > 0);
$this->setProjectInfo();
return $bResult;
}
public function delete() {
$asResult = array();
if($this->getProjectId() > 0) {
$asFeedIds = $this->getFeedIds();
foreach($asFeedIds as $iFeedId) {
$asResult['feed'][] = (new Feed($this->oDb, $iFeedId))->delete();
}
$asResult['project'][] = array(
'id' => $this->getProjectId(),
'del' => $this->oDb->deleteRow(self::PROJ_TABLE, $this->getProjectId()),
'desc' => $this->oDb->getLastError()
);
}
else $asResult['project'][] = array('del'=>false, 'desc'=>'Error while setting project: no project ID');
return $asResult;
}
public function isEditable() {
return self::isModeEditable($this->getMode());
}
static public function isModeEditable($sMode) {
return ($sMode != self::MODE_HISTO);
}
}

814
lib/Spot.php Executable file
View File

@@ -0,0 +1,814 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\Db;
use Franzz\Objects\Main;
use Franzz\Objects\Translator;
use Franzz\Objects\ToolBox;
use Franzz\Objects\Mask;
use \Settings;
/* Timezones
* ---------
* Site Time: Timestamp converted to the Timezone from which the user is viewing the Site (default PHP/SQL Timezone)
* Local Time: Timestamp converted to the Timezone from which the content (media/post/message) has been sent (Local Timezone stored in timezone field)
*
* - Feeds (table `feeds`):
* - last_update: timestamp in Site Time
* - Spot Messages (table `messages`):
* - unix_time: UNIX (int) in UTC
* - site_time: timestamp in Site Time
* - iso_time: raw ISO 8601 in UTC or Local Time (spot messages are unreliable, timezone is then calculated from GPS coordinates)
* - posted_on: timestamp in Site Time
* - timezone: Local Timezone
* - Medias (table `medias`):
* - posted_on: timestamp in Site Time
* - taken_on: timestamp in Site Time
* - timezone: Local Timezone
* - Posts (table `posts`):
* - site_time: timestamp in Site Time
* - timezone: Local Timezone
*/
class Spot extends Main
{
//Database
const POST_TABLE = 'posts';
const FEED_CHUNK_SIZE = 15;
const MAIL_CHUNK_SIZE = 5;
const DEFAULT_LANG = 'en';
const MAIN_PAGE = 'index';
private Project $oProject;
private Media $oMedia;
private User $oUser;
private Map $oMap;
public function __construct($sProcessPage, $sTimezone)
{
parent::__construct($sProcessPage, true, $sTimezone);
$this->oUser = new User($this->oDb);
$this->oLang = new Translator('', self::DEFAULT_LANG);
$this->oProject = new Project($this->oDb);
$this->oMedia = new Media($this->oDb, $this->oProject);
$this->oMap = new Map($this->oDb);
}
protected function install()
{
//Install DB
$this->oDb->install();
//Add first user
$iUserId = $this->oDb->insertRow(User::USER_TABLE, array(
'name' => 'Admin',
'email' => 'admin@admin.com',
'language' => self::DEFAULT_LANG,
'timezone' => date_default_timezone_get(),
'active' => User::USER_ACTIVE,
'clearance' => User::CLEARANCE_ADMIN
));
$this->oUser->setUserId($iUserId);
}
protected function getSqlOptions()
{
return array
(
'tables' => array
(
Feed::MSG_TABLE => array('ref_msg_id', Db::getId(Feed::FEED_TABLE), 'type', 'latitude', 'longitude', 'iso_time', 'site_time', 'timezone', 'unix_time', 'content', 'battery_state', 'posted_on', 'weather_icon', 'weather_cond', 'weather_temp', 'display'),
Feed::FEED_TABLE => array('ref_feed_id', Db::getId(Feed::SPOT_TABLE), Db::getId(Project::PROJ_TABLE), 'name', 'description', 'status', 'last_update'),
Feed::SPOT_TABLE => array('ref_spot_id', 'name', 'model'),
Project::PROJ_TABLE => array('name', 'codename', 'active_from', 'active_to'),
self::POST_TABLE => array(Db::getId(Project::PROJ_TABLE), Db::getId(User::USER_TABLE), 'name', 'content', 'site_time', 'timezone'),
Media::MEDIA_TABLE => array(Db::getId(Project::PROJ_TABLE), 'filename', 'type', 'taken_on', 'posted_on', 'timezone', 'latitude', 'longitude', 'altitude', 'width', 'height', 'rotate', 'comment'),
User::USER_TABLE => array('name', 'email', 'gravatar', 'language', 'timezone', 'active', 'clearance'),
Map::MAP_TABLE => array('codename', 'pattern', 'token', 'tile_size', 'min_zoom', 'max_zoom', 'attribution'),
Map::MAPPING_TABLE => array(Db::getId(Map::MAP_TABLE) , Db::getId(Project::PROJ_TABLE))
),
'types' => array
(
'active' => "BOOLEAN DEFAULT ".User::USER_INACTIVE,
'clearance' => "TINYINT(1) DEFAULT ".User::CLEARANCE_USER,
'active_from' => "TIMESTAMP DEFAULT 0",
'active_to' => "TIMESTAMP DEFAULT 0",
'battery_state' => "VARCHAR(10)",
'codename' => "VARCHAR(100)",
'content' => "LONGTEXT",
'comment' => "LONGTEXT",
'description' => "VARCHAR(100)",
'email' => "VARCHAR(320) NOT NULL",
'filename' => "VARCHAR(100) NOT NULL",
'iso_time' => "VARCHAR(24)",
'language' => "VARCHAR(2)",
'last_update' => "TIMESTAMP DEFAULT 0",
'latitude' => "DECIMAL(7,5)",
'longitude' => "DECIMAL(8,5)",
'altitude' => "SMALLINT",
'model' => "VARCHAR(20)",
'name' => "VARCHAR(100)",
'pattern' => "VARCHAR(200) NOT NULL",
'posted_on' => "TIMESTAMP DEFAULT 0",
'ref_feed_id' => "VARCHAR(40)",
'ref_msg_id' => "VARCHAR(15)",
'ref_spot_id' => "VARCHAR(10)",
'rotate' => "SMALLINT",
'site_time' => "TIMESTAMP DEFAULT 0", //DEFAULT 0 removes auto-set to current time
'status' => "VARCHAR(10)",
'taken_on' => "TIMESTAMP DEFAULT 0",
'timezone' => "CHAR(64) NOT NULL", //see mysql.time_zone_name
'token' => "VARCHAR(4096)",
'type' => "VARCHAR(20)",
'unix_time' => "INT",
'min_zoom' => "TINYINT UNSIGNED",
'max_zoom' => "TINYINT UNSIGNED",
'attribution' => "VARCHAR(100)",
'gravatar' => "LONGTEXT",
'weather_icon' => "VARCHAR(30)",
'weather_cond' => "VARCHAR(30)",
'weather_temp' => "DECIMAL(3,1)",
'tile_size' => "SMALLINT UNSIGNED DEFAULT 256",
'width' => "INT",
'height' => "INT",
'display' => "BOOLEAN DEFAULT ".Feed::MSG_DISPLAYED
),
'constraints' => array
(
Feed::MSG_TABLE => array("UNIQUE KEY `uni_ref_msg_id` (`ref_msg_id`)", "INDEX(`ref_msg_id`)"),
Feed::FEED_TABLE => array("UNIQUE KEY `uni_ref_feed_id` (`ref_feed_id`)", "INDEX(`ref_feed_id`)"),
Feed::SPOT_TABLE => array("UNIQUE KEY `uni_ref_spot_id` (`ref_spot_id`)", "INDEX(`ref_spot_id`)"),
Project::PROJ_TABLE => "UNIQUE KEY `uni_proj_name` (`codename`)",
Media::MEDIA_TABLE => "UNIQUE KEY `uni_file_name` (`filename`)",
User::USER_TABLE => "UNIQUE KEY `uni_email` (`email`)",
Map::MAP_TABLE => "UNIQUE KEY `uni_map_name` (`codename`)"
),
'cascading_delete' => array
(
Feed::SPOT_TABLE => array(Feed::FEED_TABLE),
Feed::FEED_TABLE => array(Feed::MSG_TABLE),
Project::PROJ_TABLE => array(Feed::FEED_TABLE, Media::MEDIA_TABLE, self::POST_TABLE, Map::MAPPING_TABLE),
Map::MAP_TABLE => array(Map::MAPPING_TABLE)
)
);
}
public function getAppParams() {
//Cache Page List
$asPages = array_diff($this->asMasks, array('email_update', 'email_conf'));
if(!$this->oUser->checkUserClearance(User::CLEARANCE_ADMIN)) {
$asPages = array_diff($asPages, array('admin', 'upload'));
}
$asGlobalVars = array(
'vars' => array(
'chunk_size' => self::FEED_CHUNK_SIZE,
'default_project_codename' => $this->oProject->getProjectCodeName(),
'projects' => $this->oProject->getProjects(),
'user' => $this->oUser->getUserInfo()
),
'consts' => array(
'server' => $this->asContext['serv_name'],
'modes' => Project::MODES,
'clearances' => User::CLEARANCES,
'default_timezone' => Settings::TIMEZONE
)
);
return self::getJsonResult(true, '', parent::getParams($asGlobalVars, self::MAIN_PAGE, $asPages));
}
public function getAppMainPage()
{
return parent::getMainPage(
self::MAIN_PAGE,
array(
'language' => $this->oLang->getLanguage(),
'host_url' => $this->asContext['serv_name'],
'filepath_css' => self::addTimestampToFilePath('spot.css'),
'filepath_js' => self::addTimestampToFilePath('../dist/app.js')
)
);
}
public function checkUserClearance($iClearance) {
return $this->oUser->checkUserClearance($iClearance);
}
/* Managing projects */
public function setProjectId($iProjectId=0) {
$this->oProject->setProjectId($iProjectId);
}
public function updateProject() {
$bNewMsg = false;
$bSuccess = true;
$sDesc = '';
//Update all feeds belonging to the project
$asFeeds = $this->oProject->getFeedIds();
foreach($asFeeds as $iFeedId) {
$oFeed = new Feed($this->oDb, $iFeedId);
$bNewMsg = $bNewMsg || $oFeed->checkUpdateFeed($this->oProject->getMode());
}
//Send Update Email
if($bNewMsg) {
$oEmail = new Email($this->asContext['serv_name'], 'email_update');
$oEmail->setDestInfo($this->oUser->getActiveUsersInfo());
//Add Position
$asLastMessage = array_shift($this->getSpotMessages(array($this->oProject->getLastMessageId($this->getFeedConstraints(Feed::MSG_TABLE)))));
$oEmail->oTemplate->setTags($asLastMessage);
$oEmail->oTemplate->setTag('date_time', 'time:'.$asLastMessage['unix_time'], 'd/m/Y, H:i');
//Add latest news feed
$asNews = $this->getNextFeed(0, true);
$iPostCount = 0;
foreach($asNews as $asPost) {
if($asPost['type'] != 'message') {
$oEmail->oTemplate->newInstance('news');
$oEmail->oTemplate->setInstanceTags('news', array(
'local_server' => $this->asContext['serv_name'],
'project' => $this->oProject->getProjectCodeName(),
'type' => $asPost['type'],
'id' => $asPost['id_'.$asPost['type']])
);
$oEmail->oTemplate->addInstance($asPost['type'], $asPost);
$oEmail->oTemplate->setInstanceTag($asPost['type'], 'local_server', $this->asContext['serv_name']);
$iPostCount++;
}
if($iPostCount == self::MAIL_CHUNK_SIZE) break;
}
$bSuccess = $oEmail->send();
$sDesc = $bSuccess?'mail_sent':'mail_failure';
}
else $sDesc = 'no_new_msg';
return self::getJsonResult($bSuccess, $sDesc);
}
public function genCronFile() {
//$bSuccess = (file_put_contents('spot_cron.sh', '#!/bin/bash'."\n".'cd '.dirname($_SERVER['SCRIPT_FILENAME'])."\n".'php -f index.php a=update_feed')!==false);
$sFileName = 'spot_cron.sh';
$sContent =
'#!/bin/bash'."\n".
'wget -qO- '.$this->asContext['serv_name'].'index.php?a=update_project > /dev/null'."\n".
'#Crontab job: 0 * * * * . '.dirname($_SERVER['SCRIPT_FILENAME']).'/'.$sFileName.' > /dev/null'."\n";
$bSuccess = (file_put_contents($sFileName, $sContent)!==false);
return self::getJsonResult($bSuccess, '');
}
public function getMarkers($asMessageIds=array(), $asMediaIds=array(), $bInternal=false)
{
$asMessages = $this->getSpotMessages($asMessageIds);
$asGeoMedias = array();
usort($asMessages, function($a, $b){return $a['unix_time'] > $b['unix_time'];});
$bHasMsg = !empty($asMessages);
//Add medias
$asMedias = $this->getMedias('taken_on', $asMediaIds);
usort($asMedias, function($a, $b){return $a['unix_time'] > $b['unix_time'];});
//Assign medias to closest message
$iIndex = 0;
$iMaxIndex = count($asMessages) - 1;
foreach($asMedias as $asMedia) {
if($asMedia['latitude']!='' && $asMedia['longitude']!='') $asGeoMedias[] = $asMedia;
elseif($bHasMsg) {
while($iIndex <= $iMaxIndex && $asMedia['unix_time'] > $asMessages[$iIndex]['unix_time']) $iIndex++;
//All medias before first message or after last message are assigned to first/last message respectively
if($iIndex == 0) $iMsgIndex = $iIndex;
elseif($iIndex > $iMaxIndex) $iMsgIndex = $iMaxIndex;
else {
$iHalfWayPoint = ($asMessages[$iIndex - 1]['unix_time'] + $asMessages[$iIndex]['unix_time'])/2;
$iMsgIndex = ($asMedia['unix_time'] >= $iHalfWayPoint)?$iIndex:($iIndex - 1);
}
$asMessages[$iMsgIndex]['medias'][] = $asMedia;
}
}
//Spot Last Update
$asLastUpdate = array();
$this->addTimeStamp($asLastUpdate, $this->oProject->getLastUpdate());
$asResult = array(
'messages' => $asMessages,
'medias' => $asGeoMedias,
'maps' => $this->oMap->getProjectMaps($this->oProject->getProjectId()),
'last_update' => $asLastUpdate
);
return $bInternal?$asResult:self::getJsonResult(true, '', $asResult);
}
public function subscribe($sEmail, $sNickName) {
$asResult = $this->oUser->addUser($sEmail, $this->oLang->getLanguage(), date_default_timezone_get(), $sNickName);
$asUserInfo = $this->oUser->getUserInfo();
//Send Confirmation Email
if($asResult['result'] && $asResult['desc']=='lang:nl_subscribed') {
$oConfEmail = new Email($this->asContext['serv_name'], 'email_conf');
$oConfEmail->setDestInfo($asUserInfo);
$oConfEmail->send();
}
return self::getJsonResult($asResult['result'], $asResult['desc'], $asUserInfo);
}
public function unsubscribe() {
$asResult = $this->oUser->removeUser();
return self::getJsonResult($asResult['result'], $asResult['desc'], $asResult['data']);
}
public function unsubscribeFromEmail($iUserId) {
$this->oUser->setUserId($iUserId);
$this->oLang->setLanguage($this->oUser->getLang(), self::DEFAULT_LANG);
$asResult = $this->oUser->removeUser();
$sDesc = explode(':', $asResult['desc'])[1];
return $this->oLang->getTranslation($sDesc);
}
private function getSpotMessages($asMsgIds=array())
{
$asConstraints = $this->getFeedConstraints(Feed::MSG_TABLE);
if(!empty($asMsgIds)) {
$asConstraints['constraint'][Db::getId(Feed::MSG_TABLE)] = $asMsgIds;
$asConstraints['constOpe'][Db::getId(Feed::MSG_TABLE)] = 'IN';
}
$asCombinedMessages = array();
//Get messages from all feeds belonging to the project
$asFeeds = $this->oProject->getFeedIds();
foreach($asFeeds as $iFeedId) {
$oFeed = new Feed($this->oDb, $iFeedId);
$asMessages = $oFeed->getMessages($asConstraints);
foreach($asMessages as $asMessage)
{
$asMessage['latitude'] = floatval($asMessage['latitude']);
$asMessage['longitude'] = floatval($asMessage['longitude']);
$asMessage['lat_dms'] = self::decToDms($asMessage['latitude'], 'lat');
$asMessage['lon_dms'] = self::decToDms($asMessage['longitude'], 'lon');
$asMessage['displayed_id'] = $asMessage[Db::getId(Feed::MSG_TABLE)];
$asMessage['static_img_url'] = $this->oMap->getMapUrl('static', array('x'=>$asMessage['longitude'], 'y'=>$asMessage['latitude']));
$asMessage['marker_img_url'] = $this->oMap->getMapUrl('static_marker', array('x'=>$asMessage['longitude'], 'y'=>$asMessage['latitude']));
$this->addTimeStamp($asMessage, $asMessage['unix_time'], $asMessage['timezone']);
$asCombinedMessages[] = $asMessage;
}
}
return $asCombinedMessages;
}
/**
* Get valid medias based on $sTimeRefField:
* - taken_on: Date/time on which the media was taken
* - posted_on: Date/time on which the media was uploaded
* @param String $sTimeRefField Field to calculate relative times: 'taken_on' or 'posted_on'
* @return Array Medias info
*/
private function getMedias($sTimeRefField, $asMediaIds=array())
{
//Constraints
$asConstraints = $this->getFeedConstraints(Media::MEDIA_TABLE, $sTimeRefField);
if(!empty($asMediaIds)) {
$asConstraints['constraint'][Db::getId(Media::MEDIA_TABLE)] = $asMediaIds;
$asConstraints['constOpe'][Db::getId(Media::MEDIA_TABLE)] = 'IN';
}
$asMedias = $this->oMedia->getMediasInfo($asConstraints);
foreach($asMedias as &$asMedia) {
$iTimeStampTakenOn = strtotime($asMedia['taken_on']);
$iTimeStampPostedOn = strtotime($asMedia['posted_on']);
$asMedia['taken_on_formatted'] = $this->getTimeFormat($iTimeStampTakenOn);
$asMedia['taken_on_formatted_local'] = $this->getTimeFormat($iTimeStampTakenOn, $asMedia['timezone']);
$asMedia['posted_on_formatted'] = $this->getTimeFormat($iTimeStampPostedOn);
$asMedia['posted_on_formatted_local'] = $this->getTimeFormat($iTimeStampPostedOn, $asMedia['timezone']);
$asMedia['displayed_id'] = $asMedia[Db::getId(Media::MEDIA_TABLE)];
$this->addTimeStamp($asMedia, strtotime($asMedia[$sTimeRefField]), $asMedia['timezone']);
}
return $asMedias;
}
private function getPosts($asPostIds=array())
{
$asInfo = array(
'select' => array(Db::getFullColumnName(self::POST_TABLE, '*'), 'gravatar'),
'from' => self::POST_TABLE,
'join' => array(User::USER_TABLE => Db::getId(User::USER_TABLE))
);
$asInfo = array_merge($asInfo, $this->getFeedConstraints(self::POST_TABLE));
if(!empty($asPostIds)) {
$asInfo['constraint'][Db::getId(self::POST_TABLE)] = $asPostIds;
$asInfo['constOpe'][Db::getId(self::POST_TABLE)] = 'IN';
}
$asPosts = $this->oDb->selectRows($asInfo);
foreach($asPosts as &$asPost) {
$iUnixTimeStamp = strtotime($asPost['site_time']); //assumes site timezone
$asPost['formatted_name'] = Toolbox::mb_ucwords($asPost['name']);
unset($asPost[Db::getId(User::USER_TABLE)]);
$this->addTimeStamp($asPost, $iUnixTimeStamp, $asPost['timezone']);
}
return $asPosts;
}
private function addTimeStamp(&$asData, $iTime, $sTimeZone='') {
$asData['unix_time'] = (int) $iTime;
$asData['relative_time'] = Toolbox::getDateTimeDesc($iTime, $this->oLang->getLanguage());
$asData['formatted_time'] = $this->getTimeFormat($iTime);
if($sTimeZone != '') {
$asData['formatted_time_local'] = $this->getTimeFormat($iTime, $sTimeZone);
$asData['day_offset'] = self::getTimeZoneDayOffset($iTime, $sTimeZone);
}
}
private function getFeedConstraints($sType, $sTimeField='site_time', $sReturnFormat='array') {
$asConsArray = array();
$sConsSql = "";
$asActPeriod = $this->oProject->getActivePeriod();
//Filter on Project ID
$sConsSql = "WHERE ".Db::getId(Project::PROJ_TABLE)." = ".$this->oProject->getProjectId();
$asConsArray = array(
'constraint'=> array(Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId()),
'constOpe' => array(Db::getId(Project::PROJ_TABLE) => "=")
);
//Time Filter
switch($sType) {
case Feed::MSG_TABLE:
$asConsArray['constraint'][$sTimeField] = $asActPeriod;
$asConsArray['constOpe'][$sTimeField] = "BETWEEN";
$asConsArray['constraint']['display'] = Feed::MSG_DISPLAYED;
$asConsArray['constOpe']['display'] = "=";
$sConsSql .= " AND ".$sTimeField." BETWEEN '".$asActPeriod['from']."' AND '".$asActPeriod['to']."' AND display = ".Feed::MSG_DISPLAYED;
break;
case Media::MEDIA_TABLE:
$asConsArray['constraint'][$sTimeField] = $asActPeriod['to'];
$asConsArray['constOpe'][$sTimeField] = "<=";
$sConsSql .= " AND ".$sTimeField." <= '".$asActPeriod['to']."'";
break;
case self::POST_TABLE:
$asConsArray['constraint'][$sTimeField] = $asActPeriod['to'];
$asConsArray['constOpe'][$sTimeField] = "<=";
$sConsSql .= " AND ".$sTimeField." <= '".$asActPeriod['to']."'";
break;
}
return ($sReturnFormat=='array')?$asConsArray:$sConsSql;
}
public function getNewFeed($iRefIdFirst) {
$asResult = array();
$sDesc = '';
if($this->oProject->isEditable()) {
$asMessageIds = $asMediaIds = array();
//New Feed Items
$asResult = $this->getFeed($iRefIdFirst, ">", "DESC");
foreach($asResult['feed'] as $asItem) {
switch($asItem['type']) {
case 'message':
$asMessageIds[] = $asItem['id'];
break;
case 'media':
$asMediaIds[] = $asItem['id'];
break;
}
}
//New Markers
$asMarkers = $this->getMarkers(
empty($asMessageIds)?array(0):$asMessageIds,
empty($asMediaIds)?array(0):$asMediaIds,
true
);
$asResult = array_merge($asResult, $asMarkers);
}
else $sDesc = 'mode_histo';
return self::getJsonResult(true, $sDesc, $asResult);
}
public function getNextFeed($iRefIdLast=0, $bInternal=false) {
if($this->oProject->getMode() == Project::MODE_HISTO) {
$sDirection = ">";
$sSort = "ASC";
}
else {
$sDirection = "<";
$sSort = "DESC";
}
$asResult = $this->getFeed($iRefIdLast, $sDirection, $sSort);
return $bInternal?$asResult['feed']:self::getJsonResult(true, '', $asResult);
}
public function getFeed($iRefId=0, $sDirection, $sSort) {
$this->oDb->cleanSql($iRefId);
$this->oDb->cleanSql($sDirection);
$this->oDb->cleanSql($sSort);
$sMediaRefField = 'posted_on';
$sProjectIdField = Db::getId(Project::PROJ_TABLE);
$sMsgIdField = Db::getId(Feed::MSG_TABLE);
$sMediaIdField = Db::getId(Media::MEDIA_TABLE);
$sPostIdField = Db::getId(self::POST_TABLE);
$sFeedIdField = Db::getId(Feed::FEED_TABLE);
$sQuery = implode(" ", array(
"SELECT type, id, ref",
"FROM (",
"SELECT {$sProjectIdField}, {$sMsgIdField} AS id, 'message' AS type, CONCAT(UNIX_TIMESTAMP(site_time), '.0', {$sMsgIdField}) AS ref",
"FROM ".Feed::MSG_TABLE,
"INNER JOIN ".Feed::FEED_TABLE." USING({$sFeedIdField})",
$this->getFeedConstraints(Feed::MSG_TABLE, 'site_time', 'sql'),
"UNION",
"SELECT {$sProjectIdField}, {$sMediaIdField} AS id, 'media' AS type, CONCAT(UNIX_TIMESTAMP({$sMediaRefField}), '.1', {$sMediaIdField}) AS ref",
"FROM ".Media::MEDIA_TABLE,
$this->getFeedConstraints(Media::MEDIA_TABLE, $sMediaRefField, 'sql'),
"UNION",
"SELECT {$sProjectIdField}, {$sPostIdField} AS id, 'post' AS type, CONCAT(UNIX_TIMESTAMP(site_time), '.2', {$sPostIdField}) AS ref",
"FROM ".self::POST_TABLE,
$this->getFeedConstraints(self::POST_TABLE, 'site_time', 'sql'),
") AS items",
($iRefId > 0)?("WHERE ref ".$sDirection." ".$iRefId):"",
"ORDER BY ref ".$sSort,
"LIMIT ".self::FEED_CHUNK_SIZE
));
//Get new chunk
$asItems = $this->oDb->getArrayQuery($sQuery, true);
//Update Reference Point with latest/earliest value
$iRefIdFirst = $iRefIdLast = 0;
if(!empty($asItems)) {
$iRefIdLast = end($asItems)['ref'];
$iRefIdFirst = reset($asItems)['ref'];
}
//Sort Table IDs by type & Get attributes
$asFeedIds = array('message'=>array(), 'media'=>array(), 'message'=>array());
foreach($asItems as $asItem) {
$asFeedIds[$asItem['type']][$asItem['id']] = $asItem;
}
$asFeedAttrs = array(
'message' => empty($asFeedIds['message'])?array():$this->getSpotMessages(array_keys($asFeedIds['message'])),
'media' => empty($asFeedIds['media'])?array():$this->getMedias($sMediaRefField, array_keys($asFeedIds['media'])),
'post' => empty($asFeedIds['post'])?array():$this->getPosts(array_keys($asFeedIds['post']))
);
//Replace Array Key with Item ID
foreach($asFeedAttrs as $sType=>$asFeedAttr) {
foreach($asFeedAttr as $asFeed) {
$asFeeds[$sType][$asFeed['id_'.$sType]] = $asFeed;
}
}
//Assign
foreach($asItems as &$asItem) {
$asItem = array_merge($asFeeds[$asItem['type']][$asItem['id']], $asItem);
}
return array('ref_id_last'=>$iRefIdLast, 'ref_id_first'=>$iRefIdFirst, 'sort'=>$sSort, 'feed'=>$asItems);
}
public function addPost($sName, $sPost)
{
$iPostId = 0;
$sDesc = '';
if($this->oProject->isEditable()) {
$asData = array(
Db::getId(Project::PROJ_TABLE) => $this->oProject->getProjectId(),
'name' => mb_strtolower(trim($sName)),
'content' => trim($sPost),
'site_time' => date(Db::TIMESTAMP_FORMAT), //Now in Site Time
'timezone' => date_default_timezone_get() //Site Time Zone
);
if($this->oUser->getUserId() > 0) $asData[Db::getId(User::USER_TABLE)] = $this->oUser->getUserId();
$iPostId = $this->oDb->insertRow(self::POST_TABLE, $asData);
$this->oUser->updateNickname($sName);
}
else $sDesc = 'mode_histo';
return self::getJsonResult(($iPostId > 0), $sDesc);
}
public function upload()
{
$oUploader = new Uploader($this->oMedia, $this->oLang);
return $oUploader->sBody;
}
public function addComment($iMediaId, $sComment) {
$oMedia = new Media($this->oDb, $this->oProject, $iMediaId);
$asResult = $oMedia->setComment($sComment);
return self::getJsonResult($asResult['result'], $asResult['desc'], $asResult['data']);
}
public function getAdminSettings($sType='') {
$oFeed = new Feed($this->oDb);
$asData = array(
'project' => $this->oProject->getProjects(),
'feed' => $oFeed->getFeeds(),
'spot' => $oFeed->getSpots(),
'user' => $this->oUser->getActiveUsersInfo()
);
foreach($asData['project'] as &$asProject) {
$asProject['active_from'] = substr($asProject['active_from'], 0, 10);
$asProject['active_to'] = substr($asProject['active_to'], 0, 10);
}
return self::getJsonResult(true, '', $asData);
}
public function setAdminSettings($sType, $iId, $sField, $sValue) {
$bSuccess = false;
$sDesc = '';
$asResult = array();
switch($sType) {
case 'project':
$oProject = new Project($this->oDb, $iId);
switch($sField) {
case 'name':
$bSuccess = $oProject->setProjectName($sValue);
break;
case 'codename':
$bSuccess = $oProject->setProjectCodeName($sValue);
break;
case 'active_from':
$bSuccess = $oProject->setActivePeriod($sValue.' 00:00:00', 'from');
break;
case 'active_to':
$bSuccess = $oProject->setActivePeriod($sValue.' 23:59:59', 'to');
break;
default:
$sDesc = $this->oLang->getTranslation('unknown_field', $sField);
}
$asResult = $oProject->getProject();
$asResult['active_from'] = substr($asResult['active_from'], 0, 10);
$asResult['active_to'] = substr($asResult['active_to'], 0, 10);
break;
case 'feed':
$oFeed = new Feed($this->oDb, $iId);
switch($sField) {
case 'ref_feed_id':
$bSuccess = $oFeed->setRefFeedId($sValue);
break;
case 'id_spot':
$bSuccess = $oFeed->setSpotId($sValue);
break;
case 'id_project':
$bSuccess = $oFeed->setProjectId($sValue);
break;
default:
$sDesc = $this->oLang->getTranslation('unknown_field', $sField);
}
$asResult = $oFeed->getFeed();
break;
case 'user':
switch($sField) {
case 'clearance':
$asReturnCode = $this->oUser->setUserClearance($iId, $sValue);
$bSuccess = $asReturnCode['result'];
$sDesc = $asReturnCode['desc'];
break;
default:
$sDesc = $this->oLang->getTranslation('unknown_field', $sField);
}
$asResult = $this->oUser->getActiveUserInfo($iId);
break;
}
if(!$bSuccess && $sDesc=='') $sDesc = Mask::LANG_PREFIX.'error_commit_db';
return self::getJsonResult($bSuccess, $sDesc, array($sType=>array($asResult)));
}
public function delAdminSettings($sType, $iId) {
$bSuccess = false;
$sDesc = '';
switch($sType) {
case 'project':
$oProject = new Project($this->oDb, $iId);
$asResult = $oProject->delete();
$sDesc = $asResult['project'][0]['desc'];
break;
case 'feed':
$oFeed = new Feed($this->oDb, $iId);
$asResult = array('feed'=>array($oFeed->delete()));
$sDesc = $asResult['feed'][0]['desc'];
break;
}
$bSuccess = ($sDesc=='');
return self::getJsonResult($bSuccess, $sDesc, $asResult);
}
public function createProject() {
$oProject = new Project($this->oDb);
$iNewProjectId = $oProject->createProjectId();
$oFeed = new Feed($this->oDb);
$oFeed->createFeedId($iNewProjectId);
return self::getJsonResult($iNewProjectId>0, '', array(
'project' => array($oProject->getProject()),
'feed' => array($oFeed->getFeed())
));
}
public static function decToDms($dValue, $sType) {
if($sType=='lat') $sDirection = ($dValue >= 0)?'N':'S'; //Latitude
else $sDirection = ($dValue >= 0)?'E':'W'; //Longitude
$dLeft = abs($dValue);
//Degrees
$iDegree = floor($dLeft);
$dLeft -= $iDegree;
//Minutes
$iMinute = floor($dLeft * 60);
$dLeft -= $iMinute / 60;
//Seconds
$fSecond = round($dLeft * 3600, 1);
return
$iDegree.'°'.
self::getNumberWithLeadingZeros($iMinute, 2, 0)."'".
self::getNumberWithLeadingZeros($fSecond, 2, 1).'"'.
$sDirection;
}
public static function getNumberWithLeadingZeros($fValue, $iNbLeadingZeros, $iNbDigits){
$sDecimalSeparator = ".";
if($iNbDigits > 0) $iNbLeadingZeros += mb_strlen($sDecimalSeparator) + $iNbDigits;
$sPattern = '%0'.$iNbLeadingZeros.$sDecimalSeparator.$iNbDigits.'f';
return sprintf($sPattern, $fValue);
}
public function getTimeFormat($iTime, $sTimeZone='') {
if($sTimeZone == '') $sTimeZone = date_default_timezone_get();
$oDate = new \DateTime('@'.$iTime);
$oDate->setTimezone(new \DateTimeZone($sTimeZone));
$sDate = $oDate->format('d/m/Y');
$sTime = $oDate->format('H:i');
return $this->oLang->getTranslation('date_time', array($sDate, $sTime));
}
public static function getTimeZoneDayOffset($iTime, $sLocalTimeZone) {
$sSiteTimeZone = date_default_timezone_get();
$iLocalDate = (int) (new \DateTime('@'.$iTime))->setTimezone(new \DateTimeZone($sLocalTimeZone))->format('Ymd');
$iSiteDate = (int) (new \DateTime('@'.$iTime))->setTimezone(new \DateTimeZone($sSiteTimeZone ))->format('Ymd');
return ($iLocalDate == $iSiteDate)?'0':(($iLocalDate > $iSiteDate)?'+1':'-1');
}
public static function getTimeZoneFromDate($sDate) {
$sTimeZone = null;
preg_match('/(?<timezone>(\+|\-)\d{2}:?(\d{2}|))$/', $sDate, $asMatch);
if(array_key_exists('timezone', $asMatch)) {
$sTimeZone = $asMatch['timezone'];
//Complete short form: +12 => +1200
if(strlen($sTimeZone) == 3) $sTimeZone .= '00';
//Add colon: +1200 => +12:00
if(!strpos($sTimeZone, ':')) $sTimeZone = substr_replace($sTimeZone, ':', 3, 0);
}
return $sTimeZone;
}
}

68
lib/Uploader.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\UploadHandler;
use Franzz\Objects\Translator;
class Uploader extends UploadHandler
{
/**
* Medias Management
* @var Media
*/
private $oMedia;
/**
* Languages
* @var Translator
*/
private $oLang;
public $sBody;
function __construct(Media &$oMedia, Translator &$oLang)
{
$this->oMedia = &$oMedia;
$this->oLang = &$oLang;
$this->sBody = '';
parent::__construct(array('image_versions'=>array(), 'accept_file_types'=>'/\.(gif|jpe?g|png|mov|mp4)$/i'));
}
protected function validate($uploaded_file, $file, $error, $index, $content_range) {
$bResult = parent::validate($uploaded_file, $file, $error, $index, $content_range);
//Check project mode
if(!$this->oMedia->isProjectEditable()) {
$file->error = $this->get_error_message('upload_mode_archived', array($this->oMedia->getProjectCodeName()));
$bResult = false;
}
return $bResult;
}
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
$file = parent::handle_file_upload($uploaded_file, $name, $size, $type, $error, $index, $content_range);
if(empty($file->error)) {
$asResult = $this->oMedia->addMedia($file->name);
if(!$asResult['result']) $file->error = $this->get_error_message($asResult['desc'], $asResult['data']);
else {
$file->id = $this->oMedia->getMediaId();
$file->thumbnail = $asResult['data']['thumb_path'];
}
}
return $file;
}
protected function body($sBodyPart) {
$this->sBody .= $sBodyPart;
}
protected function get_error_message($sError, $asParams=array()) {
$sTranslatedError = $this->oLang->getTranslation($sError, $asParams);
if($sTranslatedError) return $sTranslatedError;
elseif(array_key_exists($sError, $this->error_messages)) return $this->error_messages[$sError];
else return $sError;
}
}

202
lib/User.php Normal file
View File

@@ -0,0 +1,202 @@
<?php
namespace Franzz\Spot;
use Franzz\Objects\PhpObject;
use Franzz\Objects\Db;
use \Settings;
class User extends PhpObject {
//DB Tables
const USER_TABLE = 'users';
//Clearance Levels
const USER_ACTIVE = 1;
const USER_INACTIVE = 0;
const CLEARANCE_USER = 0;
const CLEARANCE_ADMIN = 9;
const CLEARANCES = array('user'=>self::CLEARANCE_USER, 'admin'=>self::CLEARANCE_ADMIN);
//Cookie
const COOKIE_ID_USER = 'subscriber';
const COOKIE_DURATION = 60 * 60 * 24 * 365; //1 year
/**
* Database Handle
* @var Db
*/
private $oDb;
//User Info
private $iUserId;
private $asUserInfo;
public function __construct(Db &$oDb) {
parent::__construct(__CLASS__);
$this->oDb = &$oDb;
$this->iUserId = 0;
$this->asUserInfo = array(
'id' => 0,
Db::getId(self::USER_TABLE) => 0,
'name' => '',
'email' => '',
'language' => '',
'timezone' => '',
'active' => self::USER_INACTIVE,
'clearance' => self::CLEARANCE_USER
);
$this->checkUserCookie();
}
public function getLang() {
return $this->asUserInfo['language'];
}
public function addUser($sEmail, $sLang, $sTimezone, $sNickName='') {
$bSuccess = false;
$sDesc = '';
$sEmail = trim($sEmail);
//Check Email availability
$iUserId = $this->oDb->selectValue(self::USER_TABLE, Db::getId(self::USER_TABLE), array('email'=>$sEmail, 'active'=>self::USER_ACTIVE));
if($iUserId > 0) {
//Just log user in
$sDesc = 'lang:nl_email_exists';
$bSuccess = true;
}
else {
//Add/Reactivate user
$iUserId = $this->oDb->insertUpdateRow(
self::USER_TABLE,
array('email'=>$sEmail, 'language'=>$sLang, 'timezone'=>$sTimezone, 'active'=>self::USER_ACTIVE),
array('email')
);
if($iUserId==0) $sDesc = 'lang:error_commit_db';
else {
$sDesc = 'lang:nl_subscribed';
$bSuccess = true;
}
}
if($bSuccess) {
$this->setUserId($iUserId);
//Set Cookie (valid 1 year)
$this->updateCookie(self::COOKIE_DURATION);
//Update Nickname if user has already posted
$this->updateNickname($sNickName);
//Retrieve Gravatar image
$this->updateGravatar($iUserId, $sEmail);
}
return Spot::getResult($bSuccess, $sDesc);
}
public function removeUser() {
$bSuccess = false;
$sDesc = '';
if($this->iUserId > 0) {
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $this->getUserId(), array('active'=>self::USER_INACTIVE));
if($iUserId==0) $sDesc = 'lang:error_commit_db';
else {
$sDesc = 'lang:nl_unsubscribed';
$this->updateCookie(-60 * 60); //Set Cookie in the past, deleting it
$bSuccess = true;
}
}
else $sDesc = 'lang:nl_unknown_email';
return Spot::getResult($bSuccess, $sDesc);
}
public function updateNickname($sNickname) {
if($this->getUserId() > 0 && $sNickname!='') $this->oDb->updateRow(self::USER_TABLE, $this->getUserId(), array('name'=>$sNickname));
}
private function updateGravatar($iUserId, $sEmail) {
$sImage = ($sEmail != '')?@file_get_contents('https://www.gravatar.com/avatar/'.md5($sEmail).'.png?d=404&s=24'):'';
$this->oDb->updateRow(self::USER_TABLE, $iUserId, array('gravatar' => base64_encode($sImage)));
}
private function checkUserCookie() {
if(isset($_COOKIE[self::COOKIE_ID_USER])){
$this->setUserId($_COOKIE[self::COOKIE_ID_USER]);
//Extend cookie life
if($this->getUserId() > 0) $this->updateCookie(self::COOKIE_DURATION);
}
}
public function getUserId() {
return $this->iUserId;
}
public function setUserId($iUserId) {
$this->iUserId = 0;
$asUser = $this->getActiveUserInfo($iUserId);
if(!empty($asUser)) {
$this->iUserId = $iUserId;
$this->asUserInfo = $asUser;
}
}
public function getUserInfo() {
return $this->asUserInfo;
}
public function getActiveUserInfo($iUserId) {
$asUsersInfo = array();
if($iUserId > 0) $asUsersInfo = $this->getActiveUsersInfo($iUserId);
return empty($asUsersInfo)?array():array_shift($asUsersInfo);
}
public function getActiveUsersInfo($iUserId=-1) {
//Mapping between user fields and DB fields
$asSelect = array_keys($this->asUserInfo);
$asSelect[array_search('id', $asSelect)] = Db::getId(self::USER_TABLE)." AS id";
//Non-admin cannot access clearance info
if(!$this->checkUserClearance(self::CLEARANCE_ADMIN)) unset($asSelect['clearance']);
$asInfo = array(
'select' => $asSelect,
'from' => self::USER_TABLE,
'constraint'=> array('active'=>self::USER_ACTIVE)
);
if($iUserId != -1) $asInfo['constraint'][Db::getId(self::USER_TABLE)] = $iUserId;
return $this->oDb->selectRows($asInfo);
}
public function checkUserClearance($iClearance)
{
return ($this->asUserInfo['clearance'] >= $iClearance);
}
public function setUserClearance($iUserId, $iClearance) {
$bSuccess = false;
$sDesc = '';
if(!$this->checkUserClearance(self::CLEARANCE_ADMIN)) $sDesc = 'unauthorized';
else {
if(!in_array($iClearance, self::CLEARANCES)) $sDesc = 'Setting wrong clearance "'.$iClearance.'" to user ID "'.$iUserId.'"';
else {
$iUserId = $this->oDb->updateRow(self::USER_TABLE, $iUserId, array('clearance'=>$iClearance));
if(!$iUserId) $sDesc = 'lang:error_commit_db';
else $bSuccess = true;
}
}
return Spot::getResult($bSuccess, $sDesc);
}
private function updateCookie($iDeltaTime) {
setcookie(self::COOKIE_ID_USER, ($iDeltaTime < 0)?'':$this->getUserId(), array('samesite' => 'Lax', 'expires' => time() + $iDeltaTime));
}
}

107
lib/index.php Executable file
View File

@@ -0,0 +1,107 @@
<?php
/* Requests Handler */
//Start buffering
ob_start();
//Run from /dist/
$oLoader = require __DIR__.'/../vendor/autoload.php';
use Franzz\Objects\ToolBox;
use Franzz\Objects\Main;
use Franzz\Spot\Spot;
use Franzz\Spot\User;
ToolBox::fixGlobalVars($argv ?? array());
//Available variables
$sAction = $_REQUEST['a'] ?? '';
$sTimezone = $_REQUEST['t'] ?? '';
$sName = $_GET['name'] ?? '';
$sContent = $_GET['content'] ?? '';
$iProjectId = $_REQUEST['id_project'] ?? 0 ;
$sField = $_REQUEST['field'] ?? '';
$oValue = $_REQUEST['value'] ?? '';
$iId = $_REQUEST['id'] ?? 0 ;
$sType = $_REQUEST['type'] ?? '';
$sEmail = $_REQUEST['email'] ?? '';
//Initiate class
$oSpot = new Spot(__FILE__, $sTimezone);
$oSpot->setProjectId($iProjectId);
$sResult = '';
if($sAction!='')
{
switch($sAction)
{
case 'params':
$sResult = $oSpot->getAppParams();
break;
case 'markers':
$sResult = $oSpot->getMarkers();
break;
case 'next_feed':
$sResult = $oSpot->getNextFeed($iId);
break;
case 'new_feed':
$sResult = $oSpot->getNewFeed($iId);
break;
case 'add_post':
$sResult = $oSpot->addPost($sName, $sContent);
break;
case 'subscribe':
$sResult = $oSpot->subscribe($sEmail, $sName);
break;
case 'unsubscribe':
$sResult = $oSpot->unsubscribe();
break;
case 'unsubscribe_email':
$sResult = $oSpot->unsubscribeFromEmail($iId);
break;
case 'update_project':
$sResult = $oSpot->updateProject();
break;
default:
if($oSpot->checkUserClearance(User::CLEARANCE_ADMIN))
{
switch($sAction)
{
case 'upload':
$sResult = $oSpot->upload();
break;
case 'add_comment':
$sResult = $oSpot->addComment($iId, $sContent);
break;
case 'admin_new':
$sResult = $oSpot->createProject();
break;
case 'admin_get':
$sResult = $oSpot->getAdminSettings();
break;
case 'admin_set':
$sResult = $oSpot->setAdminSettings($sType, $iId, $sField, $oValue);
break;
case 'admin_del':
$sResult = $oSpot->delAdminSettings($sType, $iId);
break;
case 'generate_cron':
$sResult = $oSpot->genCronFile();
break;
case 'sql':
$sResult = $oSpot->getDbBuildScript();
break;
default:
$sResult = Main::getJsonResult(false, Main::NOT_FOUND);
}
}
else $sResult = Main::getJsonResult(false, Main::NOT_FOUND);
}
}
else $sResult = $oSpot->getAppMainPage();
$sDebug = ob_get_clean();
if(Settings::DEBUG && $sDebug!='') $oSpot->addUncaughtError($sDebug);
echo $sResult;