Esempio n. 1
0
 /**
  * @return Application
  */
 public static function getApplication()
 {
     if (!isset(self::$Application)) {
         Factory::getLogger()->emergency('Application have not been set');
     }
     return self::$Application;
 }
Esempio n. 2
0
 /**
  * @param string $strDSN
  * @param string $User
  * @param string $Password
  * @param array  $Options
  */
 public function __construct($strDSN, $User, $Password, $Options = [])
 {
     $this->PreparedQueries = 0;
     try {
         parent::__construct($strDSN, $User, $Password, $Options);
     } catch (PDOException $e) {
         Factory::getLogger()->emergency('DatabaseHandler cannot connect exiting...');
     }
 }
Esempio n. 3
0
 /**
  * @param PDOException $exception
  * @param null|string  $SSQL
  * @return bool
  */
 public static function catchException(PDOException $exception, $SSQL = null)
 {
     if (count($exception->errorInfo) == 3) {
         $DriverCode = $exception->errorInfo[1];
         $DriverMessage = htmlentities($exception->errorInfo[2]);
     } else {
         $DriverCode = 'PDO_ENGINE';
         $DriverMessage = htmlentities($exception->getMessage());
     }
     Factory::getLogger()->warning('DBH Exception: DriverCode: {DriverCode} Message: {Message} File: {File} ({Line})', ['DriverCode' => $DriverCode, 'Message' => $DriverMessage, 'Line' => $exception->getLine(), 'File' => $exception->getFile()]);
     if ($SSQL) {
         array_push(self::$arErrorDebugSSQL, $SSQL);
     }
     if (self::inDebug()) {
         SystemMessage::addMessage('_DBH', 'alert', $DriverMessage, false);
     }
     return true;
 }
Esempio n. 4
0
 /**
  * Initialize the Application
  * Initialize the Document
  * Load the Component and execute it
  * Load the Modules if there's not an XML HTTP Request
  */
 public function initialize()
 {
     Factory::getDocument()->initialize();
     if ($this->isCLI()) {
         if ($this->getCLIOption('user') && $this->getCLIOption('password')) {
             if (!Factory::getUser()->authenticate($this->getCLIOption('user'), $this->getCLIOption('password'), true)) {
                 Factory::getLogger()->emergency('Wrong username or password');
             }
         }
     }
     $this->Component = $this->getComponentManager()->getComponent();
     if (!$this->XHRequest) {
         $this->Modules = $this->getModuleManager()->getModules();
     }
 }
Esempio n. 5
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;
 }
Esempio n. 6
0
 /**
  * @param      $Name
  * @param null $Component
  * @return object
  */
 protected function getModel($Name, $Component = null)
 {
     if (!isset($this->Models[$Name])) {
         try {
             if ($Component != $this->get($this->ControllerType) && !is_null($Component)) {
                 $ClassName = $Name;
             } else {
                 $Component = $this->get($this->ControllerType);
                 $ClassName = null;
                 $Properties = $this->defineProperties();
                 if (isset($Properties['Models']) && is_array($Properties['Models'])) {
                     foreach ($Properties['Models'] as $Model) {
                         if ($Model == $Name) {
                             $ClassName = $Name;
                             break;
                         }
                     }
                 }
                 if ($ClassName === null) {
                     throw new Exception('Model {' . $Name . '} does not exist in ' . $this->ControllerType . ' {' . $this->get($this->ControllerType) . '}');
                 }
             }
             $class = '\\' . $this->ControllerType . 's\\' . $Component . '\\Models\\' . $ClassName;
             $this->Models[$Name] = new $class();
         } catch (Exception $e) {
             Factory::getLogger()->alert('ComponentException: {Message}', ['Message' => $e->getMessage()]);
         }
     }
     return $this->Models[$Name];
 }
Esempio n. 7
0
 /**
  * Creates the route to according to the URI
  */
 public function route()
 {
     if (!isset($this->Route)) {
         $strRoute = $this->getRouteString();
         $Route = $this->getDefaultRouteMap();
         $Params = [];
         if (strlen($strRoute) >= 3) {
             if ($Rules = $this->getRules()) {
                 foreach ($Rules as $rKey => $rV) {
                     if (preg_match($this->getRuleRegex($rKey), $strRoute)) {
                         $Params = $this->getMapFromRule($rKey, $strRoute);
                         $strRoute = $rV;
                         break;
                     }
                 }
             }
             foreach ($this->getRouteMaps() as $Map) {
                 if (substr_count($Map, '/') === substr_count($strRoute, '/')) {
                     $Route = [$Map => $strRoute];
                 }
             }
         }
         $arMap = explode('/', Input::cleanVar(array_keys($Route)[0]));
         $arPath = explode('/', Input::cleanVar(array_values($Route)[0]));
         $cMap = count($arMap);
         for ($f = 0; $f < $cMap; $f++) {
             $this->Route[$arMap[$f]] = $arPath[$f];
         }
         foreach ($Params as $pKey => $pValue) {
             $this->Route[$pKey] = $pValue;
         }
         if (!$this->Route) {
             Factory::getLogger()->emergency('Can\'t Route Application exiting...');
         }
     }
 }
Esempio n. 8
0
 /**
  * @return mixed
  */
 public static function getInstance()
 {
     if (!isset(self::$Instance)) {
         $dbh = Factory::getDBH();
         if (Input::getVar('TemplateID', 'REQUEST')) {
             $TemplateID = Input::getVar('TemplateID', 'REQUEST');
         } elseif (Factory::getCookie()->get('TemplateID')) {
             $TemplateID = Factory::getCookie()->get('TemplateID');
         } else {
             $TemplateID = null;
         }
         if ($TemplateID) {
             $stmt = $dbh->prepare('SELECT * FROM Template WHERE TemplateID = :TemplateID AND ApplicationID = :AppID');
             try {
                 $stmt->bindValue(':AppID', Factory::getApplication()->getApplicationID(), PDO::PARAM_STR);
                 $stmt->bindValue(':TemplateID', $TemplateID, PDO::PARAM_INT);
                 $stmt->execute();
                 $rst = $stmt->fetch(PDO::FETCH_OBJ);
                 if (is_object($rst)) {
                     $Class = $rst->Template;
                     self::$Instance = new $Class($dbh, $rst);
                 } else {
                     unset($rst);
                 }
             } catch (PDOException $e) {
                 $dbh->catchException($e, $stmt->queryString);
             }
         }
         if (!self::$Instance instanceof Template) {
             $stmt = $dbh->prepare('SELECT * FROM Template WHERE ApplicationID = :AppID AND Root = 1');
             $stmt->bindValue(':AppID', Factory::getApplication()->getApplicationID(), PDO::PARAM_STR);
             try {
                 $stmt->execute();
                 $rst = $stmt->fetch(PDO::FETCH_OBJ);
                 if (is_object($rst)) {
                     $Class = "Templates\\{$rst->Template}\\{$rst->Template}";
                     self::$Instance = new $Class($dbh, $rst);
                 }
             } catch (PDOException $e) {
                 $dbh->catchException($e, $stmt->queryString);
             }
             if (!self::$Instance) {
                 Factory::getLogger()->emergency('No Template found in Database exiting...');
             }
         }
     }
     return self::$Instance;
 }
Esempio n. 9
0
 /**
  * @param mixed  $FileName
  * @param string $Path
  * @param string $DefaultCode
  * @return bool
  */
 public function addFile($FileName, $Path = null, $DefaultCode = null)
 {
     if (!isset($this->arImportedFiles[$Path . $FileName])) {
         if (!$DefaultCode) {
             $DefaultCode = $this->get('Code');
         }
         if (!$Path) {
             $Path = $this->getPath();
         }
         $File = $Path . $DefaultCode . DS . $DefaultCode . '.' . $FileName;
         if (is_readable($File)) {
             if (strtolower(pathinfo($FileName, PATHINFO_EXTENSION)) == 'xml') {
                 $XML_Language = simplexml_load_file($File);
                 if (isset($XML_Language->Strings)) {
                     $this->arImportedFiles[$Path . $FileName] = 1;
                     foreach ($XML_Language->Strings->String as $String) {
                         $this->arStrings[(string) $String['key']] = (string) $String;
                     }
                 }
             }
             if (isset($this->arImportedFiles[$Path . $FileName])) {
                 $this->setInSession();
             }
         } else {
             Factory::getLogger()->warning('LanguageException: cannot read Language: {File} ', ['File' => $File]);
         }
     }
     if (isset($this->arImportedFiles[$Path . $FileName])) {
         return true;
     } else {
         return false;
     }
 }