public static function getInstance() { if (static::$oResolver === null) { $driver = Runtime::$SETTINGS->getString("ROUTER_DRIVER", "imphp"); switch ($driver) { case "imphp": static::$oResolver = new IMPHPResolver(); break; default: /* * Let modules add namespace hooks for missing drivers */ $class = "driver\\Router\\" . $driver . "\\Resolver"; if (Runtime::loadClassFile($class)) { static::$oResolver = (new ReflectionClass($class))->newInstance(); } } } return static::$oResolver; }
/** * Create a new encrypter instance * * @api * @see api\libraries\Crypt\Encrypter * * @param string $driver * The encrypter driver to use * * @param string $cipher * Cipher to use (needs to be supported by the driver) * * @param string $hash * Hash to use * * @param string $mode * Block mode to use (needs to be supported by the driver) * * @param string $twoStep * Enable 2-step authentication (needs to be supported by the driver) * * @return Encrypter * The new encrypter */ public static function newInstance(string $driver = null, string $cipher = null, string $hash = null, string $mode = null, bool $twoStep = true) { $encrypter = null; if ($driver === null) { $driver = "dummy"; } switch ($driver) { case "openssl": $encrypter = new OpensslEncrypter($cipher, $hash, $mode, $twoStep); break; case "mcrypt": $encrypter = new McryptEncrypter($cipher, $hash, $mode, $twoStep); break; case "dummy": $encrypter = new DummyEncrypter($cipher, $hash, $mode, $twoStep); break; default: /* * Let modules add namespace hooks for missing drivers */ $class = "driver\\Crypt\\" . $driver . "\\Encrypter"; if (Runtime::loadClassFile($class)) { $encrypter = (new ReflectionClass($class))->newInstance($cipher, $hash, $mode, $twoStep); } } return $encrypter; }
/** * Create a new Connection instance * * @api * @see api\libraries\Database\Connection * * @param string $protocol * Connection protocol * * @return Connection|null * The new Connection */ public static function newInstance(string $protocol) { $driver = substr($protocol, 0, strpos($protocol, ":")); $connection = null; switch ($driver) { case "sqlite3": $connection = new ConnectionSqlite3($protocol); break; case "mysqli": $connection = new ConnectionMySqli($protocol); break; default: /* * Let modules add namespace hooks for missing drivers */ $class = "driver\\Database\\" . $driver . "\\Connection"; if (Runtime::loadClassFile($class)) { $connection = (new ReflectionClass($class))->newInstance($protocol); } } return $connection; }
/** @ignore */ public function readBack() { if (!$this->mSessReady && !in_array(Runtime::$SYSTEM["REQUEST_CLIENT"], ["terminal", "crawler"])) { $driver = Runtime::$SETTINGS->getString("SESSION_DRIVER", "file"); $maxlife = Runtime::$SETTINGS->getInt("SESSION_EXPIRES", 24 * 60 * 60); switch ($driver) { case "file": $this->mSessHandler = new FileHandler($this->mSessId, $maxlife); break; case "database": $this->mSessHandler = new DBHandler($this->mSessId, $maxlife); break; case "cache": $this->mSessHandler = new CacheHandler($this->mSessId, $maxlife); break; default: $class = "driver\\Session\\" . $driver . "\\Handler"; if (Runtime::loadClassFile($class)) { $this->mSessHandler = (new ReflectionClass($class))->newInstance($this->mSessId, $maxlife); } } if ($this->mSessHandler === null) { throw new Exception("Could find the Session Driver '" . $driver . "'"); } if (mt_rand(0, 100) == 1) { $this->mSessHandler->gc(); } $data = $this->mSessHandler->read(); $this->mData = !empty($data) ? @unserialize($data) : []; if (!is_array($this->mData)) { if (Runtime::$SETTINGS->getBoolean("SESSION_ENCRYPT_DATA")) { $cryptKey = Runtime::$SETTINGS->getString("SECURITY_PASSWD"); if (!empty($cryptKey)) { $this->mData = @unserialize(Crypt::decrypt($data, $cryptKey, true)); if (!is_array($this->mData)) { $this->mData = []; } } } else { $this->mData = []; } } } $this->mSessReady = true; }
public function request(string $request) { if ($this->mRequestLocked) { $this->mPendingRequest = $request; return; } $routes = Runtime::$SETTINGS->getArray("ROUTER"); $controller = null; $request = rtrim($request, "/"); if (empty($request)) { $request = "/"; } /* * Pages should be allowed to issue a special page defined in settings as ROUTER_[num]. * These however should not be available from te URI */ $special = preg_match("#^ROUTER_[0-9]+\$#", $request) && strcmp($request, Runtime::$SYSTEM->getString("URI_LOCATION", "/")) !== 0; if ($routes !== null && !$special) { $controller = $routes[$request] ?? null; if (empty($controller)) { foreach ($routes as $key => $value) { $key = str_replace([":alpha:", ":all:", ":num:"], ["[A-Za-z0-9\\-_]+", ".+", "[0-9]+"], $key); if (preg_match('#^' . $key . '$#', $request)) { if (!empty($value)) { if (substr($value, 0, 1) == ":") { $value = substr($value, 1); if (strpos($val, "\$") !== false) { $value = preg_replace("#^" . $key . "\$#", $value, $request); } $this->request($value); return; } else { $controller = $value; } break; } } } } elseif (substr($controller, 0, 1) == ":") { $this->request(substr($controller, 1)); return; } } elseif ($special) { $controller = Runtime::$SETTINGS->getString($request); } $request = trim($request, "/"); $controller = "page\\" . trim($controller, "\\"); if (!Runtime::loadClassFile($controller)) { $controller = "page\\" . trim(Runtime::$SETTINGS->getString("ROUTER_404"), "\\"); if (!Runtime::loadClassFile($controller)) { /* * Do not parse output through any controller if one was selected previously */ $this->mController = null; header(Runtime::$SERVER["SERVER_PROTOCOL"] . " 404 Not Found", 404, true); Runtime::quit(["404 Not Found", "The requested file does not exist"], 1); } } $this->mSegments = empty($request) ? [] : explode("/", $request); $this->mRequest = "/" . $request; /* * Let's say we redirect from with a controller * * 1. Controller1::__construct invokes a new assignment to $this->mController by calling $this->request() * However Controller1::__construct does not finish until $this->request() returns, so it stalls between * property assignments. * * 2. Controller2::__construct does a lot of page loading * * 3. Controller1::__construct finishes it's construct and is now assigned to $this->mController, overwriting Controller2 * * 4. Shutdown now executes, but uses Controller1 instead of Controller2 * * We need to finish the first controller before starting a new. * To do this, we add a pending feature. */ $this->mRequestLocked = true; $this->mController = new $controller(); $this->mRequestLocked = false; if ($this->mPendingRequest !== null) { $pending = $this->mPendingRequest; $this->mPendingRequest = null; $this->request($pending); } }
public static function newInstance(string $driver, $protocol) { $oStorage = null; switch ($driver) { case "file": $oStorage = new FileStorage($protocol); break; case "memcached": $oStorage = new MemStorage($protocol); break; case "database": $oStorage = new DBStorage($protocol); break; default: /* * Let modules add namespace hooks for missing drivers */ $class = "driver\\Cache\\" . $driver . "\\Storage"; if (Runtime::loadClassFile($class)) { $oStorage = (new ReflectionClass($class))->newInstance($protocol); } } return $oStorage; }