Exemplo n.º 1
0
 /**
  * Set a cookie
  *
  * @param string   $Key
  * @param mixed    $Value
  * @param int|NULL $Lifespan seconds
  */
 function set($Key, $Value, $Lifespan = null)
 {
     if ($Value === null) {
         $Lifespan = time() - 1;
     }
     $this->newCookies[$Key] = ['value' => base64_encode($Value), 'lifespan' => $Lifespan];
     $Lifespan = time() + (is_int($Lifespan) ? $Lifespan : $this->Lifespan);
     if (!Factory::getApplication()->isCLI()) {
         setcookie($Key, base64_encode($Value), $Lifespan, $this->Path, $this->Domain);
     }
 }
Exemplo n.º 2
0
 /**
  * Logs with an arbitrary level.
  *
  * @param mixed  $level
  * @param string $message
  * @param array  $context
  * @return null
  */
 public function log($level, $message, array $context = [])
 {
     $message = $this->interpolateMessage($message, $context);
     if (Config::APP_DEBUG) {
         echo $message, PHP_EOL;
     }
     if ($level == LogLevel::EMERGENCY || $level == LogLevel::ALERT || !Factory::getDBH()) {
         if (is_writeable('error_log')) {
             $handle = fopen('error_log', 'a');
             fwrite($handle, $message . PHP_EOL);
             fclose($handle);
         }
         exit;
     } else {
         array_push($this->logs, ['UserID' => Factory::getUser()->get('ID'), 'SessionID' => Factory::getSession()->getPHP_SessionID(), 'ApplicationID' => Factory::getApplication()->getApplicationID(), 'Level' => $level, 'ErrorString' => $message, 'RegisteredDate' => Utility::getDateForDB()]);
     }
 }
Exemplo n.º 3
0
 /**
  * @return URI
  */
 public static function getInstance()
 {
     if (!isset(self::$Instance)) {
         if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
             $https = 'https://';
         } else {
             $https = 'http://';
         }
         if (!Factory::getApplication()->isCLI()) {
             if ($_SERVER['PHP_SELF'] && isset($_SERVER['REQUEST_URI'])) {
                 $uri = $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             } else {
                 $uri = $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
                 if (isset($_SERVER['QUERY_STRING']) && !$_SERVER['QUERY_STRING']) {
                     $uri .= $_SERVER['QUERY_STRING'];
                 }
             }
         } else {
             $uri = null;
         }
         self::$Instance = new URI($uri);
     }
     return self::$Instance;
 }
Exemplo n.º 4
0
 /**
  * @return mixed
  */
 public function getComponent()
 {
     if (!isset($this->Component)) {
         $dbh = Factory::getDBH();
         $_Component = Factory::getRouter()->getRoute('Component');
         $_Controller = Factory::getRouter()->getRoute('Controller');
         $Action = Factory::getRouter()->getRoute('Action');
         $ID = Factory::getRouter()->getRoute('ID');
         try {
             $stmt = $dbh->prepare('SELECT * FROM Component WHERE ApplicationID = :AppID AND blStatus = 1 AND Component = :Component;');
             try {
                 $stmt->bindValue(':AppID', $this->ApplicationID, PDO::PARAM_STR);
                 $stmt->bindValue(':Component', (string) ucfirst($_Component), PDO::PARAM_STR);
                 $stmt->execute();
                 $Component = new Object($stmt->fetch(PDO::FETCH_OBJ));
             } catch (PDOException $e) {
                 $dbh->catchException($e, $stmt->queryString);
                 throw new Exception('EpsilonCMS cannot Load Component DB');
             }
             if ($Component->get('ComponentID')) {
                 $AccessLevels = Factory::getUser()->getAuthorizedLevels();
                 /** Verify if the current user has access to the component */
                 if (!in_array($Component->get('AccessLevelID'), $AccessLevels)) {
                     if (Factory::getUser()->isGuest()) {
                         if (Factory::getApplication()->isCLI()) {
                             Factory::getLogger()->alert(Factory::getLanguage()->_('NOT_AUTHORIZED'));
                         } else {
                             Factory::getApplication()->redirectLogin();
                         }
                     } else {
                         Factory::getApplication()->redirectHome();
                     }
                 }
                 /** Creates the Class|Controller Namespace */
                 $Namespace = '\\Components\\' . $_Component . '\\Controllers\\';
                 /**
                  * If the route contains a controller use that controller
                  * else
                  * use the component name as default controller
                  */
                 if ($_Controller) {
                     $Controller = $_Controller;
                 } else {
                     $Controller = $_Component;
                 }
                 $Class = $Namespace . $Controller;
                 if (!class_exists($Class)) {
                     throw new \Exception("Controller does not exist {$Controller}->{$Action}({$ID})");
                 }
                 $Component = new $Class($dbh, $Component);
                 /** Verify if the method (Action) exist */
                 if (is_callable([$Component, $Action])) {
                     $Component->{$Action}($ID);
                 } else {
                     throw new \Exception("Controller method does not exist {$Controller}->{$Action}({$ID})");
                 }
                 $this->Component = $Component;
             } else {
                 throw new \Exception('Component {' . $_Component . '} does not exist in Database');
             }
         } catch (\Exception $e) {
             Factory::getLogger()->alert('ComponentManagerException: {Message} {File} {Line}', ['Message' => $e->getMessage(), 'File' => $e->getFile(), 'Line' => $e->getLine()]);
         }
     }
     return $this->Component;
 }
Exemplo n.º 5
0
 /**
  * Load the template
  * set available positions of the template to the document
  * set the Javascript and Cascade Style Sheets if there's not an XML HTTP Request
  */
 public function setTemplate()
 {
     $eApplication = Factory::getApplication();
     $Template = Factory::getTemplate();
     $ContentType = $eApplication->get('ContentType');
     $XHRequest = $eApplication->get('XHRequest');
     if ($ContentType == 'text/html' && !$XHRequest) {
         foreach ($Template->getPositions() as $p) {
             $this->setPosition($p);
         }
         foreach ($Template->getJavaScripts() as $v) {
             $this->setJavaScript($v);
         }
         foreach ($Template->getStyleSheets() as $v) {
             $this->setStyleSheet($v);
         }
     } else {
         $this->setPosition('XHRequest');
     }
 }
Exemplo n.º 6
0
 /**
  * Shortcut method for Factory::getApplication()->redirect()
  *
  * @param string      $Route
  * @param array       $Parameters
  * @param null|string $Fragment
  */
 protected function redirect($Route, $Parameters = [], $Fragment = null)
 {
     Factory::getApplication()->redirect($Route, $Parameters, $Fragment);
 }
Exemplo n.º 7
0
    /**
     * TODO: rewrite method
     *
     * @return mixed
     */
    public function getCurrentMenuID()
    {
        if (!isset($this->CurrentMenuID)) {
            $dbh = Factory::getDBH();
            $App = Factory::getApplication();
            $ComponentID = $App->get('Component')->get('ID');
            $ApplicationID = $App->getApplicationID();
            $URL = $this->getRouteString();
            $ssql = 'SELECT m.MenuID AS MenuID FROM Menu m
					INNER JOIN MenuBundle mb ON mb.MenuBundleID = m.MenuBundleID
					WHERE (mb.ApplicationID = :AppID AND m.URL LIKE :URL) OR m.ComponentID = :ComponentID';
            $stmt = $dbh->prepare($ssql);
            try {
                $this->bindMenuValues($stmt, $ApplicationID, $ComponentID, $URL, $MenuID);
                $stmt->execute();
                $stmt->fetch();
                $sections = count(array_filter(explode('/', $URL)));
                if ($sections == 5 && !$stmt->rowCount()) {
                    $URL = explode('/', $URL);
                    array_pop($URL);
                    $sections--;
                    $URL = implode('/', $URL) . '/';
                    $stmt = $dbh->prepare($ssql);
                    $this->bindMenuValues($stmt, $ApplicationID, $ComponentID, $URL, $MenuID);
                    $stmt->execute();
                }
                if ($sections == 4 && !$stmt->rowCount()) {
                    $URL = explode('/', $URL);
                    array_pop($URL);
                    $URL = implode('/', $URL) . '/';
                    $stmt = $dbh->prepare($ssql);
                    $this->bindMenuValues($stmt, $ApplicationID, $ComponentID, $URL, $MenuID);
                    $stmt->execute();
                }
                if ($stmt->rowCount() == 1) {
                    $stmt->fetch();
                    $this->CurrentMenuID = $MenuID;
                }
            } catch (PDOException $e) {
                $dbh->catchException($e, $stmt->queryString);
            }
        }
        return $this->CurrentMenuID;
    }
Exemplo n.º 8
0
 /**
  * @return bool
  */
 public function writeVariables()
 {
     if ($this->blWritten || Factory::getApplication()->isCLI()) {
         return false;
     }
     try {
         if (is_array($this->newSessionVariables)) {
             foreach ($this->newSessionVariables as $k => $v) {
                 try {
                     if (is_null(unserialize($v['Value']))) {
                         $stmt = $this->objPDO->prepare('DELETE FROM SessionVariable WHERE AsciiSessionID = :Ascii_ID AND VariableName = :VariableName');
                     } else {
                         if ($this->checkVar($k, $v['PHP_SessionID'])) {
                             $stmt = $this->objPDO->prepare('INSERT INTO SessionVariable (AsciiSessionID, VariableName, VariableValue, Lifespan) VALUES (:Ascii_ID,:VariableName,:VariableValue,IF(:Lifespan>0,DATE_ADD(:now, INTERVAL :Lifespan SECOND),NULL))');
                         } else {
                             $stmt = $this->objPDO->prepare('UPDATE SessionVariable SET VariableValue = :VariableValue, Lifespan = IF(:Lifespan>0,DATE_ADD(:now,INTERVAL :Lifespan SECOND),NULL) WHERE VariableName = :VariableName AND AsciiSessionID = :Ascii_ID');
                         }
                         $stmt->bindValue(':VariableValue', $v['Value'], PDO::PARAM_LOB);
                         $stmt->bindValue(':now', $this->getDateNOW(), PDO::PARAM_STR);
                         $stmt->bindValue(':Lifespan', $v['Lifespan'], PDO::PARAM_INT);
                     }
                     $stmt->bindValue(':VariableName', $k, PDO::PARAM_STR);
                     $stmt->bindValue(':Ascii_ID', $v['PHP_SessionID']);
                     $stmt->execute();
                     $v['Written'] = true;
                 } catch (PDOException $e) {
                 }
             }
             $this->blWritten = true;
             return true;
         }
     } catch (PDOException $e) {
         Factory::getDBH()->catchException($e);
     }
     return false;
 }
Exemplo n.º 9
0
    /**
     * @return int MainMenuID
     */
    protected function getMainMenuID()
    {
        if (!$this->MainMenuID) {
            $dbh = Factory::getDBH();
            $stmt = $dbh->prepare('SELECT m.MenuID AS MenuID FROM Menu m
					INNER JOIN MenuBundle mb ON mb.MenuBundleID = m.MenuBundleID
					WHERE mb.ApplicationID = :AppID AND m.Root = 1 AND blStatus = 1');
            try {
                $stmt->bindValue(':AppID', Factory::getApplication()->getApplicationID(), PDO::PARAM_STR);
                $stmt->bindColumn('MenuID', $MenuID, PDO::PARAM_INT);
                $stmt->execute();
                $stmt->fetch();
                $this->MainMenuID = $MenuID;
            } catch (PDOException $e) {
                $dbh->catchException($e, $stmt->queryString);
            }
        }
        return $this->MainMenuID;
    }
Exemplo n.º 10
0
 /**
  * @param string $Asset
  * @param bool   $Recursive
  * @return Rules
  */
 protected function getAssetRules($Asset, $Recursive = false)
 {
     $select = $Recursive ? 'b.Rules' : 'a.Rules';
     $group = $Recursive ? 'GROUP BY b.AssetID, b.Rules, b.lft' : 'a.AssetID, a.Rules, a.lft';
     $where = !is_string($Asset) ? 'a.AssetID = :AssetID' : 'a.Asset = :AssetID';
     $where .= ' AND (a.ApplicationID = :AppID AND b.ApplicationID = :AppID)';
     if ($Recursive) {
         $join = 'LEFT JOIN Asset AS b ON b.lft <= a.lft AND b.rgt >= a.rgt';
         $order = 'ORDER BY b.lft';
     } else {
         $join = null;
         $order = null;
     }
     $dbh = Factory::getDBH();
     $stmt = $dbh->prepare("SELECT {$select} FROM Asset as a {$join} WHERE {$where} {$group} {$order}");
     $stmt->bindValue(':AssetID', $Asset, PDO::PARAM_STR);
     $stmt->bindValue(':AppID', Factory::getApplication()->getApplicationID(), PDO::PARAM_STR);
     $arRules = [];
     try {
         $stmt->execute();
         foreach ($stmt->fetchAll(PDO::FETCH_OBJ) as $Rule) {
             array_push($arRules, $Rule->Rules);
         }
     } catch (PDOException $e) {
         $dbh->catchException($e, $stmt->queryString);
     }
     return new Rules($arRules);
 }
Exemplo n.º 11
0
 public function display()
 {
     if (!$this->Rendered) {
         $eApplication = Factory::getApplication();
         $ContentType = $eApplication->get('ContentType');
         $XHRequest = $eApplication->get('XHRequest');
         $ContentType = strtolower($ContentType);
         if ($ContentType === 'text/html') {
             header('Content-type: text/html;');
             if ($XHRequest) {
                 $View = $this->get('DefaultXHRTemplate');
             } else {
                 $View = $this->get('DefaultTemplate');
             }
             echo new View($this->getViewPath(), $View, null, ['SystemMessages' => SystemMessage::getMessages()]);
         }
         $this->Rendered = true;
     }
 }
Exemplo n.º 12
0
 /**
  * @return Language
  */
 public static function getInstance()
 {
     if (!isset(self::$Instance)) {
         $dbh = Factory::getDBH();
         $ApplicationID = Factory::getApplication()->getApplicationID();
         $Session = Factory::getSession();
         $LanguageID = null;
         if (Input::getVar('LanguageID', 'REQUEST')) {
             $LanguageID = Input::getVar('LanguageID', 'REQUEST');
         } elseif ($Session->get('LanguageID')) {
             $LanguageID = $Session->get('LanguageID');
         }
         if ($LanguageID) {
             $stmt = $dbh->prepare('SELECT * FROM Language WHERE ApplicationID = :AppID AND LanguageID = :LangID');
             try {
                 $stmt->bindValue(':AppID', $ApplicationID, PDO::PARAM_STR);
                 $stmt->bindValue(':LangID', $LanguageID, PDO::PARAM_INT);
                 $stmt->execute();
                 $rst = $stmt->fetch(PDO::FETCH_OBJ);
                 if (is_object($rst)) {
                     self::$Instance = new Language($dbh, $rst);
                 } else {
                     unset($rst);
                 }
             } catch (PDOException $e) {
                 $dbh->catchException($e, $stmt->queryString);
             }
         }
         if (!self::$Instance instanceof Language) {
             $stmt = $dbh->prepare('SELECT * FROM Language WHERE ApplicationID = :AppID AND Root = 1');
             try {
                 $stmt->bindValue(':AppID', $ApplicationID, PDO::PARAM_STR);
                 $stmt->execute();
                 $rst = $stmt->fetch(PDO::FETCH_OBJ);
                 if (is_object($rst)) {
                     self::$Instance = new Language($dbh, $rst);
                 } else {
                     Factory::getLogger()->emergency('No Language found in Database exiting...');
                 }
             } catch (PDOException $e) {
                 $dbh->catchException($e, $stmt->queryString);
             }
         }
         if (self::$Instance instanceof Language && !$Session->get('LanguageID') || self::$Instance->get('ID') != $Session->get('LanguageID')) {
             $Session->set('LanguageID', self::$Instance->get('ID'));
             Factory::getSession()->set('Language', null);
         } else {
             $Language = Factory::getSession()->get('Language');
             if (is_array($Language)) {
                 if (isset($Language['arImportedFiles'])) {
                     self::$Instance->set('arImportedFiles', $Language['arImportedFiles']);
                 }
                 if (isset($Language['arStrings'])) {
                     self::$Instance->set('arStrings', $Language['arStrings']);
                 }
             }
         }
     }
     return self::$Instance;
 }
Exemplo n.º 13
0
 public function Logout()
 {
     Factory::getUser()->logOut();
     Factory::getApplication()->redirectLogin();
 }
Exemplo n.º 14
0
<?php

/**
 * Project: Epsilon
 * Date: 11/5/15
 * Time: 12:55 PM
 *
 * @link      https://github.com/falmar/Epsilon
 * @author    David Lavieri (falmar) <*****@*****.**>
 * @copyright 2015 David Lavieri
 * @license   http://opensource.org/licenses/MIT The MIT License (MIT)
 */
define('EPSILON_EXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('EPSILON_PATH', __DIR__ . DS);
require_once 'App' . DS . 'DefinePath.php';
require_once 'App' . DS . 'DefineVariables.php';
use Epsilon\Factory;
$App = Factory::getApplication();
$App->initialize();
$App->render();