/** * Função para definir uso de cache na Query * * @access public * @return \MySQLTableCached */ public function Cache($iTimeout = 3600) { Storage::Set("cachedb.timeout", $iTimeout); Events::Set("BeforeQuery", function ($sSQL, $fCallback) { if (Storage::Get("cachedb.enabled", false)) { $mCache = @CacheDB::Get(sha1($sSQL)); if ($mCache !== false && !is_null($mCache)) { if ($fCallback) { $fCallback(json_decode($mCache, true), null); } return true; } else { return false; } } else { return false; } }); Events::Set("AfterQuery", function ($sSQL, $aResult) { if (Storage::Get("cachedb.enabled", false)) { @CacheDB::Set(sha1($sSQL), json_encode($aResult), Storage::Get("cachedb.timeout", 3600)); } }); return $this; }
/** * Function to start the module * * @access public * @return void */ public function Start() { if (file_exists($this->sModuleDiretory . SP . "status.txt")) { $iStatus = intval(file_get_contents($this->sModuleDiretory . SP . "status.txt")); $bStatus = $iStatus == 1; Storage::Set("app." . $this->sModuleName . ".enabled", $bStatus); } else { $bStatus = true; file_put_contents($this->sModuleDiretory . SP . "status.txt", "1"); Storage::Set("app." . $this->sModuleName . ".enabled", true); } if ($bStatus) { Storage::SetArray("class.list", "module." . $this->sModuleName, $this->sModuleDiretory . "core" . SP); //Diretory Paths Storage::Set("app." . $this->sModuleName, $this->sModuleDiretory); Storage::Set("app." . $this->sModuleName . ".shell.css", $this->sModuleDiretory . "shell" . SP . "css" . SP); Storage::Set("app." . $this->sModuleName . ".shell.tpl", $this->sModuleDiretory . "shell" . SP . "tpl" . SP); Storage::Set("app." . $this->sModuleName . ".shell.js", $this->sModuleDiretory . "shell" . SP . "js" . SP); Storage::Set("app." . $this->sModuleName . ".shell.img", $this->sModuleDiretory . "shell" . SP . "img" . SP); //Web Paths Storage::Set("virtual." . $this->sModuleName . ".shell.img", Storage::Join("route.root", "app/" . $this->sModuleName . "/shell/img/")); Storage::Set("virtual." . $this->sModuleName . ".shell.tpl", Storage::Join("route.root", "app/" . $this->sModuleName . "/shell/tpl/")); Storage::Set("virtual." . $this->sModuleName . ".shell.js", Storage::Join("route.root", "app/" . $this->sModuleName . "/shell/js/")); Storage::Set("virtual." . $this->sModuleName . ".shell.css", Storage::Join("route.root", "app/" . $this->sModuleName . "/shell/css/")); //Loading submodules if (is_dir($this->sModuleDiretory . "modules")) { $aModulesDirectories = glob($this->sModuleDiretory . "modules" . SP . "*", GLOB_ONLYDIR); foreach ($aModulesDirectories as $sModuleDiretory) { if (file_exists($sModuleDiretory . SP . "status.txt")) { $bStatus = intval(file_get_contents($sModuleDiretory . SP . "status.txt")) == 1; } else { $bStatus = false; } if ($bStatus) { if (file_exists($sModuleDiretory . SP . "settings.php") && $bStatus) { require_once $sModuleDiretory . SP . "settings.php"; } if (file_exists($sModuleDiretory . SP . "include.php") && $bStatus) { require_once $sModuleDiretory . SP . "include.php"; } if (file_exists($sModuleDiretory . SP . "routes.php") && $bStatus) { require_once $sModuleDiretory . SP . "routes.php"; } if (file_exists($sModuleDiretory . SP . "events.php") && $bStatus) { require_once $sModuleDiretory . SP . "events.php"; } } } } } }
/** * Starting the application * * @static * @access public * @return void */ public static function Start() { $oThis = self::CreateInstanceIfNotExists(); //setting autoload spl_autoload_register(array($oThis, "AutoLoad")); //Configuring basic directories Storage::Set("dir.root", __DIR__ . SP); Storage::Set("dir.public", __DIR__ . SP . "public" . SP); Storage::Set("dir.public.assets", __DIR__ . SP . "public" . SP . "assets" . SP); Storage::Set("dir.public.compiles", __DIR__ . SP . "public" . SP . "compiles" . SP); Storage::Set("dir.public.configs", __DIR__ . SP . "public" . SP . "configs" . SP); Storage::Set("dir.app", __DIR__ . SP . "app" . SP); //Configuring default route Storage::Set("route.root", "//" . $_SERVER["SERVER_NAME"] . str_replace(array("index.php", " "), array("", "%20"), $_SERVER["SCRIPT_NAME"])); //Bugfix $oThis->LoadApp(); }
/** * Function to return parameters passed by PUT or DELETE methods * * @static * @access public * @param boolean $bReturn * @return mixed */ public static function RestParams($bReturn = false) { $sBuffer = file_get_contents("php://input"); $aParams = explode("&", $sBuffer); $aReturn = array(); foreach ($aParams as $sParam) { @(list($mKey, $mValue) = @explode("=", $sParam)); $mValue = urldecode($mValue); if (!empty($mKey) && !empty($mValue)) { Storage::Set(strtolower(Routes::Restful()) . "." . $mKey, $mValue); if ($bReturn) { $aReturn[strtolower(Routes::Restful()) . "." . $mKey] = $mValue; } } } if ($bReturn) { return $aReturn; } }
/** * Função para iniciar serviço de armazenamento de cache em memória * * @access public * @param string $sHostname Hostname do servidor de cache * @param integer $iPort Porta do servidor (Padrão: 11211) * @return boolean */ public static function Start($sHostname, $iPort = 11211) { $oThis = self::CreateInstanceIfNotExists(); if (class_exists("memcached")) { if (!empty($sHostname) && is_int($iPort)) { $oThis->oMemCache = new memcached(array("servers" => array($sHostname . ":" . $iPort), "debug" => false, "compress_threshold" => 10240, "persistant" => true)); if (is_object($oThis->oMemCache)) { Storage::Set("cachedb.enabled", true); return true; } else { Storage::Set("cachedb.enabled", false); return false; } } else { Storage::Set("cachedb.enabled", false); return false; } } else { Storage::Set("cachedb.enabled", false); return false; } }
<?php /** * Settings */ //Default Settings Storage::Set("app.charset", "UTF-8"); Storage::Set("debug", true); Storage::Set("app.minified", false); //Smarty Settings Storage::Set("smarty.dir.compile", __DIR__ . SP . "public" . SP . "compiles" . SP); Storage::Set("smarty.dir.config", __DIR__ . SP . "public" . SP . "configs" . SP);
/** * Function to perform the login session * * @static * @access public * @param mixed $mId User ID * @param string $sUsername Username * @param string $sName Name * @param integer $iTimeout Maximum time of permanence in the session * @param boolean $bRoot Defines the user as root * @return void */ public static function Login($mId, $sUsername, $sName, $iTimeout = 3600, $bRoot = false) { $oThis = self::CreateInstanceIfNotExists(); if ($oThis->bStarted) { $iTimeout = time() + $iTimeout; $oThis->aAuth = array("id" => $mId, "name" => $sName, "username" => $sUsername, "timeout" => $iTimeout, "root" => $bRoot); $_SESSION[$oThis->sName]["authentication"] = $oThis->aAuth; if (!empty($oThis->aAuth)) { Storage::Set("user.id", $oThis->aAuth["id"]); Storage::Set("user.name", $oThis->aAuth["name"]); Storage::Set("user.username", $oThis->aAuth["username"]); Storage::Set("user.root", $oThis->aAuth["root"]); Storage::Set("session.timeout.login", $iTimeout); } } }
/** * Function to create the Javascript file cache * * @access public * @param boolean $bForce Forcing creation * @return void */ public static function CreateCacheJS() { $oThis = self::CreateInstanceIfNotExists(); if (count($oThis->aJS) > 0) { $sCacheFilename = Storage::Join("dir.public.assets", strtolower($oThis->sNamespace) . ".js"); Storage::Set("assets.js", Storage::Join("route.root", "public/assets/" . strtolower($oThis->sNamespace) . ".js")); if (!file_exists($sCacheFilename) || Storage::Get("debug", false)) { $sBuffer = ""; foreach ($oThis->aJS as $sAppendBuffer) { $sBuffer .= $sAppendBuffer; } file_put_contents($sCacheFilename, $sBuffer); } } }
<?php /** * Settings * * @package MagicPHP Hello World * @author André Ferreira <*****@*****.**> * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ Storage::Set("app.title", "MagicPHP - Hello World");