Initial Commit
This commit is contained in:
14
core/Objects/ApiObject.class.php
Normal file
14
core/Objects/ApiObject.class.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Objects;
|
||||
|
||||
abstract class ApiObject implements \JsonSerializable {
|
||||
|
||||
public abstract function jsonSerialize();
|
||||
|
||||
public function __construct() { }
|
||||
public function __toString() { return json_encode($this); }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
46
core/Objects/ConnectionData.class.php
Normal file
46
core/Objects/ConnectionData.class.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Objects;
|
||||
|
||||
class ConnectionData {
|
||||
|
||||
private $host;
|
||||
private $port;
|
||||
private $login;
|
||||
private $password;
|
||||
private $properties;
|
||||
|
||||
public function __construct($host, $port, $login, $password) {
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->login = $login;
|
||||
$this->password = $password;
|
||||
$this->properties = array();
|
||||
}
|
||||
|
||||
public function getProperties() {
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
public function getProperty($key) {
|
||||
if(isset($this->properties[$key]))
|
||||
return $this->properties[$key];
|
||||
else
|
||||
return '';
|
||||
}
|
||||
|
||||
public function setProperty($key, $val) {
|
||||
if(!is_string($val)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->properties[$key] = $val;
|
||||
}
|
||||
|
||||
public function getHost() { return $this->host; }
|
||||
public function getPort() { return $this->port; }
|
||||
public function getLogin() { return $this->login; }
|
||||
public function getPassword() { return $this->password; }
|
||||
}
|
||||
|
||||
?>
|
||||
133
core/Objects/Language.class.php
Normal file
133
core/Objects/Language.class.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Objects;
|
||||
|
||||
class Language extends ApiObject {
|
||||
|
||||
const LANG_CODE_PATTERN = "/^[a-zA-Z]+_[a-zA-Z]+$/";
|
||||
|
||||
private $languageId;
|
||||
private $langCode;
|
||||
private $langName;
|
||||
private $modules;
|
||||
|
||||
protected $entries;
|
||||
|
||||
public function __construct($languageId, $langCode, $langName) {
|
||||
$this->languageId = $languageId;
|
||||
$this->langCode = $langCode;
|
||||
$this->langName = $langName;
|
||||
$this->entries = array();
|
||||
$this->modules = array();
|
||||
}
|
||||
|
||||
public function getId() { return $this->languageId; }
|
||||
public function getCode() { return $this->langCode; }
|
||||
public function getShortCode() { return substr($this->langCode, 0, 2); }
|
||||
public function getName() { return $this->langName; }
|
||||
public function getIconPath() { return "/img/icons/lang/$this->langCode.gif"; }
|
||||
public function getEntries() { return $this->entries; }
|
||||
public function getModules() { return $this->modules; }
|
||||
|
||||
public function loadModule($module) {
|
||||
if(!is_object($module))
|
||||
$module = new $module;
|
||||
|
||||
$aModuleEntries = $module->getEntries($this->langCode);
|
||||
$this->entries = array_merge($this->entries, $aModuleEntries);
|
||||
$this->modules[] = $module;
|
||||
}
|
||||
|
||||
public function translate($key) {
|
||||
if(isset($this->entries[$key]))
|
||||
return $this->entries[$key];
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
public function sendCookie() {
|
||||
setcookie('lang', $this->langCode, 0, "/", "");
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return array(
|
||||
'uid' => $this->languageId,
|
||||
'code' => $this->langCode,
|
||||
'name' => $this->langName,
|
||||
);
|
||||
}
|
||||
|
||||
public static function newInstance($languageId, $langCode, $langName) {
|
||||
|
||||
if(!preg_match(Language::LANG_CODE_PATTERN, $langCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: include dynamically wanted Language
|
||||
return new Language($languageId, $langCode, $langName);
|
||||
|
||||
// $className = $langCode
|
||||
// return new $className($languageId, $langCode);
|
||||
}
|
||||
|
||||
public function load() {
|
||||
global $LANGUAGE;
|
||||
$LANGUAGE = $this;
|
||||
}
|
||||
|
||||
public static function DEFAULT_LANGUAGE() {
|
||||
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
||||
$acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
||||
$aSplit = explode(',',$acceptLanguage);
|
||||
foreach($aSplit as $code) {
|
||||
if(strlen($code) == 2) {
|
||||
$code = $code . '_' . strtoupper($code);
|
||||
}
|
||||
|
||||
$code = str_replace("-", "_", $code);
|
||||
if(strlen($code) != 5)
|
||||
continue;
|
||||
|
||||
$lang = Language::newInstance(0, $code, "");
|
||||
if($lang)
|
||||
return $lang;
|
||||
}
|
||||
}
|
||||
|
||||
return Language::newInstance(1, "en_US", "American English");
|
||||
}
|
||||
};
|
||||
|
||||
function L($key) {
|
||||
if(!array_key_exists('LANGUAGE', $GLOBALS))
|
||||
return $key;
|
||||
|
||||
global $LANGUAGE;
|
||||
return $LANGUAGE->translate($key);
|
||||
}
|
||||
|
||||
function LANG_NAME() {
|
||||
if(!array_key_exists('LANGUAGE', $GLOBALS))
|
||||
return "LANG_NAME";
|
||||
|
||||
global $LANGUAGE;
|
||||
return $LANGUAGE->getName();
|
||||
}
|
||||
|
||||
function LANG_CODE() {
|
||||
if(!array_key_exists('LANGUAGE', $GLOBALS))
|
||||
return "LANG_CODE";
|
||||
|
||||
global $LANGUAGE;
|
||||
return $LANGUAGE->getCode();
|
||||
}
|
||||
|
||||
function SHORT_LANG_CODE() {
|
||||
if(!array_key_exists('LANGUAGE', $GLOBALS))
|
||||
return "SHORT_LANG_CODE";
|
||||
|
||||
global $LANGUAGE;
|
||||
return $LANGUAGE->getShortCode();
|
||||
}
|
||||
|
||||
?>
|
||||
105
core/Objects/Session.class.php
Normal file
105
core/Objects/Session.class.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Objects;
|
||||
|
||||
class Session extends ApiObject {
|
||||
|
||||
const DURATION = 120;
|
||||
|
||||
private $sessionId;
|
||||
private $user;
|
||||
private $expires;
|
||||
private $ipAddress;
|
||||
private $os;
|
||||
private $browser;
|
||||
|
||||
public function __construct($user, $sessionId = NULL) {
|
||||
$this->user = $user;
|
||||
$this->sessionId = $sessionId;
|
||||
}
|
||||
|
||||
private function updateMetaData() {
|
||||
$userAgent = get_browser($_SERVER['HTTP_USER_AGENT'], true);
|
||||
$this->expires = time() + Session::DURATION * 60;
|
||||
$this->ipAddress = $_SERVER['REMOTE_ADDR'];
|
||||
$this->os = $userAgent['platform'];
|
||||
$this->browser = $userAgent['parent'];
|
||||
}
|
||||
|
||||
public function sendCookie() {
|
||||
$this->updateMetaData();
|
||||
$token = array('userId' => $this->user->getId(), 'sessionId' => $this->sessionId);
|
||||
$sessionCookie = JWT::encode($token, getJwtKey());
|
||||
setcookie('session', $sessionCookie, $this->expires, "/", "", true);
|
||||
}
|
||||
|
||||
public function getExpiresTime() {
|
||||
return $this->expires;
|
||||
}
|
||||
|
||||
public function getExpiresSeconds() {
|
||||
return ($this->expires - time());
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return array(
|
||||
'uid' => $this->sessionId,
|
||||
'uidUser' => $this->user->getId(),
|
||||
'expires' => $this->expires,
|
||||
'ipAddress' => $this->ipAddress,
|
||||
'os' => $this->os,
|
||||
'browser' => $this->browser,
|
||||
);
|
||||
}
|
||||
|
||||
public function insert() {
|
||||
$this->updateMetaData();
|
||||
$query = 'INSERT INTO Session (expires, uidUser, ipAddress, os, browser)
|
||||
VALUES (DATE_ADD(NOW(), INTERVAL ? MINUTE),?,?,?,?)';
|
||||
$request = new CExecuteStatement($this->user);
|
||||
|
||||
$success = $request->execute(array(
|
||||
'query' => $query,
|
||||
Session::DURATION,
|
||||
$this->user->getId(),
|
||||
$this->ipAddress,
|
||||
$this->os,
|
||||
$this->browser,
|
||||
));
|
||||
|
||||
if($success) {
|
||||
$this->sessionId = $this->user->getSQL()->getLastInsertId();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function destroy() {
|
||||
$query = 'DELETE FROM Session WHERE Session.uid=? OR Session.expires<=NOW()';
|
||||
$request = new CExecuteStatement($this->user);
|
||||
$success = $request->execute(array('query' => $query, $this->sessionId));
|
||||
return $success;
|
||||
}
|
||||
|
||||
public function update() {
|
||||
$this->updateMetaData();
|
||||
$query = 'UPDATE Session
|
||||
SET Session.expires=DATE_ADD(NOW(), INTERVAL ? MINUTE), Session.ipAddress=?,
|
||||
Session.os=?, Session.browser=?
|
||||
WHERE Session.uid=?';
|
||||
$request = new CExecuteStatement($this->user);
|
||||
$success = $request->execute(array(
|
||||
'query' => $query,
|
||||
Session::DURATION,
|
||||
$this->ipAddress,
|
||||
$this->os,
|
||||
$this->browser,
|
||||
$this->sessionId,
|
||||
));
|
||||
|
||||
return $success;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
189
core/Objects/User.class.php
Normal file
189
core/Objects/User.class.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
namespace Objects;
|
||||
|
||||
class User extends ApiObject {
|
||||
|
||||
private $sql;
|
||||
private $configuration;
|
||||
private $loggedIn;
|
||||
private $session;
|
||||
private $uid;
|
||||
private $username;
|
||||
private $language;
|
||||
|
||||
public function __construct($configuration) {
|
||||
$this->configuration = $configuration;
|
||||
$this->setLangauge(Language::DEFAULT_LANGUAGE());
|
||||
$this->reset();
|
||||
$this->connectDb();
|
||||
$this->parseCookies();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if($this->sql && $this->sql->isConnected()) {
|
||||
$this->sql->close();
|
||||
}
|
||||
}
|
||||
|
||||
private function connectDb() {
|
||||
$databaseConf = $this->configuration->getDatabase();
|
||||
if($databaseConf) {
|
||||
$this->sql = \Driver\SQL::createConnection($databaseConf);
|
||||
}
|
||||
}
|
||||
|
||||
public function setSql($sql) { $this->sql = $sql; }
|
||||
public function getId() { return $this->uid; }
|
||||
public function isLoggedIn() { return $this->loggedIn; }
|
||||
public function getUsername() { return $this->username; }
|
||||
public function getSQL() { return $this->sql; }
|
||||
public function getLanguage() { return $this->language; }
|
||||
public function setLangauge($language) { $this->language = $language; $language->load(); }
|
||||
public function getSession() { return $this->session; }
|
||||
public function getConfiguration() { return $this->configuration; }
|
||||
|
||||
public function __debugInfo() {
|
||||
$debugInfo = array(
|
||||
'loggedIn' => $this->loggedIn,
|
||||
'language' => $this->language->getName(),
|
||||
);
|
||||
|
||||
if($this->loggedIn) {
|
||||
$debugInfo['uid'] = $this->uid;
|
||||
$debugInfo['username'] = $this->username;
|
||||
}
|
||||
|
||||
return $debugInfo;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return array(
|
||||
'uid' => $this->uid,
|
||||
'name' => $this->username,
|
||||
'language' => $this->language,
|
||||
'session' => $this->session,
|
||||
);
|
||||
}
|
||||
|
||||
private function reset() {
|
||||
$this->uid = 0;
|
||||
$this->username = '';
|
||||
$this->loggedIn = false;
|
||||
$this->session = false;
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
if($this->loggedIn) {
|
||||
$this->session->destroy();
|
||||
$this->reset();
|
||||
}
|
||||
}
|
||||
|
||||
public function updateLanguage($lang) {
|
||||
$request = new CSetLanguage($this);
|
||||
return $request->execute(array("langCode" => $lang));
|
||||
}
|
||||
|
||||
public function sendCookies() {
|
||||
if($this->loggedIn) {
|
||||
$this->session->sendCookie();
|
||||
}
|
||||
|
||||
$this->language->sendCookie();
|
||||
}
|
||||
|
||||
public function readData($userId, $sessionId, $sessionUpdate = true) {
|
||||
$query = 'SELECT User.name as userName, Language.uid as langId, Language.code as langCode
|
||||
FROM User
|
||||
INNER JOIN Session ON User.uid=Session.uidUser
|
||||
LEFT JOIN Language ON User.uidLanguage=Language.uid
|
||||
WHERE User.uid=? AND Session.uid=? AND Session.expires>now()';
|
||||
$request = new CExecuteSelect($this);
|
||||
$success = $request->execute(array('query' => $query, $userId, $sessionId));
|
||||
|
||||
if($success) {
|
||||
if(count($request->getResult()['rows']) === 0) {
|
||||
$success = false;
|
||||
} else {
|
||||
$row = $request->getResult()['rows'][0];
|
||||
$this->username = $row['userName'];
|
||||
$this->uid = $userId;
|
||||
$this->session = new CSession($this, $sessionId);
|
||||
if($sessionUpdate) $this->session->update();
|
||||
$this->loggedIn = true;
|
||||
|
||||
if(!is_null($row['langId'])) {
|
||||
$this->setLangauge(CLanguage::newInstance($row['langId'], $row['langCode']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
private function parseCookies() {
|
||||
if(isset($_COOKIE['session'])
|
||||
&& is_string($_COOKIE['session'])
|
||||
&& !empty($_COOKIE['session'])
|
||||
&& ($jwt = $this->configuration->getJWT())) {
|
||||
try {
|
||||
$token = $_COOKIE['session'];
|
||||
$decoded = (array)External\JWT::decode($token, $jwt->getKey());
|
||||
if(!is_null($decoded)) {
|
||||
$userId = (isset($decoded['userId']) ? $decoded['userId'] : NULL);
|
||||
$sessionId = (isset($decoded['sessionId']) ? $decoded['sessionId'] : NULL);
|
||||
if(!is_null($userId) && !is_null($sessionId)) {
|
||||
$this->readData($userId, $sessionId);
|
||||
}
|
||||
}
|
||||
} catch(Exception $e) {
|
||||
echo $e;
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) {
|
||||
$this->updateLanguage($_GET['lang']);
|
||||
} else if(isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) {
|
||||
$this->updateLanguage($_COOKIE['lang']);
|
||||
}
|
||||
}
|
||||
|
||||
public function createSession($userId) {
|
||||
$this->uid = $userId;
|
||||
$this->session = new Session($this);
|
||||
$this->loggedIn = $this->session->insert();
|
||||
return $this->loggedIn;
|
||||
}
|
||||
|
||||
public function authorize($apiKey) {
|
||||
if($this->loggedIn)
|
||||
return true;
|
||||
|
||||
$query = 'SELECT ApiKey.uidUser as uid, User.name as username, Language.uid as langId, Language.code as langCode
|
||||
FROM ApiKey, User
|
||||
LEFT JOIN Language ON User.uidLanguage=Language.uid
|
||||
WHERE api_key=? AND valid_until > now() AND User.uid = ApiKey.uidUser';
|
||||
|
||||
$request = new CExecuteSelect($this);
|
||||
$success = $request->execute(array('query' => $query, $apiKey));
|
||||
|
||||
if($success) {
|
||||
if(count($request->getResult()['rows']) === 0) {
|
||||
$success = false;
|
||||
} else {
|
||||
$row = $request->getResult()['rows'][0];
|
||||
$this->uid = $row['uid'];
|
||||
$this->username = $row['username'];
|
||||
|
||||
if(!is_null($row['langId'])) {
|
||||
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
17
core/Objects/lang/General.php
Normal file
17
core/Objects/lang/General.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
require_once './LanguageModule.php';
|
||||
|
||||
abstract class CLanguageModuleGeneral {
|
||||
|
||||
public static function getEntries($langCode) {
|
||||
switch($langCode) {
|
||||
case "de_DE":
|
||||
$this->entries[""] = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
9
core/Objects/lang/LanguageModule.php
Normal file
9
core/Objects/lang/LanguageModule.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
abstract class CLanguageModule {
|
||||
|
||||
public abstract static function getEntries($langCode);
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user