/** * @return Application */ public static function getApplication() { if (!isset(self::$Application)) { Factory::getLogger()->emergency('Application have not been set'); } return self::$Application; }
function writeLogs() { foreach ($this->logs as $log) { $Log = new Log(Factory::getDBH(), $log, false); $Log->save(); } }
/** * @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...'); } }
/** * 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); } }
/** * @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; }
/** * @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; }
/** * Set the position in document */ public function setDocumentPosition() { if (!$this->blPositionSet && $this->Position) { Factory::getDocument()->setInPosition($this->Position, $this); $this->blPositionSet = true; } return $this; }
/** * 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'); } }
<?php if (Factory::getDocument()->countByPosition("Component")) { foreach (Factory::getDocument()->getByPosition("Component") as $Content) { echo $Content; } } ?> </div> </div> <?php foreach (Factory::getDocument()->get("StyleSheets") as $CSS) { echo "<link rel='stylesheet' href='{$CSS}' />", PHP_EOL; } foreach (Factory::getDocument()->get("JavaScripts") as $JS) { echo "<script src='{$JS}' ></script>", PHP_EOL; } ?> <script type="text/javascript"> function getURL(URL) { return "<?php echo Factory::getRouter()->getURL(); ?> " + URL; } </script> </body> </html>
public function __destruct() { if ($this->blForDeletion && $this->get('ID') == Factory::getUser()->get('ID')) { throw new PDOException('Cannot Delete User if current session active'); } elseif ($this->blForDeletion) { $this->deleteUserGroupMap(); } parent::__destruct(); }
/** * 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); }
/** * @param $Results * @param $ElementID * @param null $MaxPageSize * @return \Epsilon\Object\Object */ public static function foundationListPaging($Results, $ElementID, $MaxPageSize = null) { $CurrentPage = Input::getVar("CurrentPage", "REQUEST"); if (is_integer($MaxPageSize)) { $PageSize = $MaxPageSize; } else { $PageSize = Config::MAX_PAGE_SIZE; } $Paging = new Object(['HTML' => null, 'Init' => 0, 'Max' => $PageSize, 'Results' => 0, 'PageInit' => 0, 'PageMax' => 0, 'TotalPages' => 0, 'TotalResults' => 0]); if (is_array($Results)) { $TotalResults = count($Results); } else { if (is_numeric($Results)) { $TotalResults = $Results; } else { $TotalResults = 0; } } if ($TotalResults > 0) { $TotalPages = self::getTotalPages($TotalResults, $PageSize); if ($CurrentPage > $TotalPages) { $CurrentPage = $TotalPages; } elseif (!is_numeric($CurrentPage) || !((int) $CurrentPage > 0) || (int) $CurrentPage == 0) { $CurrentPage = 1; } if ($CurrentPage > 1) { $Init = ($CurrentPage - 1) * $PageSize; $Max = $PageSize; $PageInit = ($CurrentPage - 1) * $PageSize + 1; $PageMax = $PageInit + $PageSize - 1; } else { $Init = 0; $Max = $PageSize; $PageInit = 1; $PageMax = $PageInit + $Max - 1; } if ($PageMax >= $TotalResults) { $PageMax = $TotalResults; } $Pages = self::getPages($CurrentPage, $TotalPages); $HTML = ""; $PreviousSet = false; if (is_string($ElementID)) { $ElementID = "\"{$ElementID}\""; } else { $ElementID = "null"; } $eLanguage = Factory::getLanguage(); $PagesCount = 0; foreach ($Pages as $p) { $PagesCount++; if (!$PreviousSet) { $Previus = $CurrentPage - 1; if ($CurrentPage != 1) { $HTML .= "<ul class=\"pagination text-center\" role=\"navigation\" aria-label=\"Pagination\">\n<li class=\"pagination-previous\"><a href='javascript:goToPage({$Previus}, {$ElementID})'><span class=\"show-for-sr\">page</span> " . $eLanguage->_("LIST-RESULT-PREVIOUS") . "</a></li>\n"; } else { $HTML .= "<ul class=\"pagination text-center\" role=\"navigation\" aria-label=\"Pagination\">\n<li class=\"pagination-previous disabled\"><span class=\"show-for-sr\">page</span> " . $eLanguage->_("LIST-RESULT-PREVIOUS") . "</li>\n"; } $PreviousSet = true; } if ($CurrentPage == $p) { $HTML .= "<li class='current'><span class=\"show-for-sr\">You're on page </span > {$p}</li > \n"; } else { $HTML .= " <li><a href = 'javascript:goToPage({$p},{$ElementID})' > {$p}</a ></li > \n"; } if ($PagesCount == count($Pages)) { $Next = $CurrentPage + 1; if ($CurrentPage != $p) { $HTML .= "<li class=\"pagination-next\"><a aria-label=\"Next page\" href='javascript:goToPage({$Next},{$ElementID})'>" . $eLanguage->_("LIST-RESULT-NEXT") . " <span class=\"show-for-sr\">page</span></a></li></ul>"; } else { $HTML .= "<li class=\"pagination-next disabled\"><span class=\"show-for-sr\">page</span>" . $eLanguage->_("LIST-RESULT-NEXT") . "</li>\n</ul>"; } } } $Paging->setProperties(['HTML' => $HTML, 'Init' => $Init, 'Max' => $Max, 'Results' => $PageMax - $PageInit + 1, 'PageInit' => $PageInit, 'PageMax' => $PageMax, 'TotalPages' => $TotalPages, 'TotalResults' => $TotalResults]); } return $Paging; }
/** * 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; }
<?php /** * Project: Epsilon * Date: 11/5/15 * Time: 12:58 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) */ defined('EPSILON_EXEC') or die; use App\Config; use Epsilon\Factory; require_once LIBRARY_PATH . 'Epsilon' . DS . 'autoload.php'; date_default_timezone_set(Config::TIMEZONE); ini_set('default_charset', Config::CHARSET); ini_set('display_errors', Config::APP_DEBUG ? 1 : 0); error_reporting(E_ALL); register_shutdown_function('Epsilon\\Logger\\Logger::shutdown'); set_error_handler('Epsilon\\Logger\\Logger::addPHPError'); set_exception_handler('Epsilon\\Logger\\Logger::uncaughtException'); Factory::setApplication(Config::APPLICATION_ID); Factory::getLanguage()->addFile('Epsilon.xml');
public function Logout() { Factory::getUser()->logOut(); Factory::getApplication()->redirectLogin(); }
<?php /** * Project: Epsilon * Date: 1/5/16 * Time: 5:33 PM * * @link https://github.com/falmar/Epsilon * @author David Lavieri (falmar) <*****@*****.**> * @copyright 2016 David Lavieri * @license http://opensource.org/licenses/MIT The MIT License (MIT) */ defined("EPSILON_EXEC") or die; use Epsilon\Factory; if (Factory::getDocument()->countByPosition("XHRequest")) { foreach (Factory::getDocument()->getByPosition("XHRequest") as $Content) { echo $Content; } }
/** * Redirect the Application * Calls the Router::getURL() method * * @param string $Route * @param array $Parameters * @param null $Fragment */ public function redirect($Route, $Parameters = [], $Fragment = null) { Factory::getEventManager()->dispatch('Application.onRedirect'); header('Location: ' . Factory::getRouter()->getURL($Route, $Parameters, $Fragment)); exit; }
/** * @return array */ public function getAccessLevels() { if (!$this->arAccessLevels) { $dbh = Factory::getDBH(); $stmt = $dbh->prepare('SELECT AccessLevelID, Rules FROM AccessLevel'); try { $stmt->execute(); foreach ($stmt->fetchAll(PDO::FETCH_OBJ) as $accl) { $this->arAccessLevels[$accl->AccessLevelID] = (array) json_decode($accl->Rules); } } catch (PDOException $e) { $dbh->catchException($e, $stmt->queryString); } } return $this->arAccessLevels; }
/** * @param $Element * @return bool */ public static function assignMessages($Element) { if (!isset(self::$arSystemMessagesElement[$Element])) { $dbh = Factory::getDBH(); $stmt = $dbh->prepare("SELECT SystemMessageID,Type,Message FROM SystemMessage WHERE (Element = :Element OR Element = '_system' OR Element = '_DBH') AND (UserID = :UserID OR SessionID = :SessionID) AND Viewed = 0"); try { $stmt->bindValue(':Element', $Element, PDO::PARAM_STR); $stmt->bindValue(':UserID', Factory::getUser()->get('ID'), PDO::PARAM_INT); $stmt->bindValue(':SessionID', Factory::getSession()->getPHP_SessionID()); $stmt->execute(); foreach ($stmt->fetchAll(PDO::FETCH_OBJ) as $Message) { array_push(self::$arSystemMessages, new SystemMessage($dbh, $Message)); } self::$arSystemMessagesElement[$Element] = true; return true; } catch (PDOException $e) { Factory::getDBH()->catchException($e, $stmt->queryString); } } return false; }
protected function setInSession() { if (!Config::APP_DEBUG) { $Language = []; $Language['arImportedFiles'] = $this->arImportedFiles; $Language['arStrings'] = $this->arStrings; Factory::getSession()->set('Language', $Language); } }
<?php defined("EPSILON_EXEC") or die; use Epsilon\Factory; /** @var \Epsilon\MVC\View $this */ $eLanguage = Factory::getLanguage(); $eRouter = Factory::getRouter(); ?> <div style="margin-top: 50px;"></div> <div class="row align-center"> <div class="medium-6 large-5 column"> <div class="callout"> <h3 class="text-center"><?php echo $eLanguage->_("COM_LOGIN-TITLE"); ?> </h3> <form action="<?php echo $eRouter->getURL("Authentication/Authenticate"); ?> " method="post"> <div class="row"> <div class="column"> <label for="fEmail"><?php echo $eLanguage->_("COM_LOGIN-EMAIL"), ' / ', $eLanguage->_("COM_LOGIN-USERNAME"); ?> </label> <input name="Login[Email]" id="fEmail" type="text" value="" required> </div> </div> <div class="row">
/** * @param null $File */ public function setLanguageFile($File = null) { $Files = null; $Language = null; $Properties = $this->defineProperties(); $eLang = Factory::getLanguage(); $Code = $eLang->get('Code'); if (is_string($File) && !empty($File)) { $File = [$File]; } if (isset($Properties['Languages']) && is_array($Properties['Languages'])) { foreach ($Properties['Languages'] as $lg) { if (isset($lg['default']) && (bool) $lg['default'] === true) { $Default = $lg; } elseif (isset($lg['code']) && $lg['code'] == $Code) { $Language = $lg; break; } } if (!$Language && isset($Default)) { $Language = $Default; $Code = isset($Language['code']) ? $Language['code'] : $Code; } if (isset($Language['files']) && is_array($Language['files'])) { $Files = $Language['files']; } } $isArray = is_array($File); if ($Files) { foreach ($Files as $lang) { if ($isArray) { foreach ($File as $f) { if ($f == (string) $lang) { $eLang->addFile((string) $lang, $this->getPath() . 'Language' . DS, $Code); break; } } } else { $eLang->addFile((string) $lang, $this->getPath() . 'Language' . DS, $Code); } } } }
/** * @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; }
/** * @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; }
/** * @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; }
<?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();