Exemplo n.º 1
0
 /**
  *	Parse Ini File
  *	@access public
  *	@param \Touchbase\Filesystem\File $file
  *	@return VOID
  */
 public function parseIniFile(\Touchbase\Filesystem\File $file)
 {
     if (!$file->exists()) {
         throw new \Exception("Config file '{$file->path}' could not be found");
     }
     $this->_iniFileData = parse_ini_file($file->path, true);
     if (!$this->_iniFileData) {
         throw new \Exception("The ini file '{$file->path}' is corrupt or invalid");
     }
     return $this;
 }
Exemplo n.º 2
0
 /** 
  *	Shared
  *	@return \Parse\Control\Client
  */
 public static function shared()
 {
     $instance = StaticStore::shared()->get(self::CLIENT_KEY, false);
     if (!$instance || is_null($instance)) {
         $config = ConfigStore::create();
         try {
             //Load Main Configuration File
             $configurationData = IniConfigProvider::create()->parseIniFile(File::create('../config.ini'));
             $config->addConfig($configurationData->getConfiguration());
         } catch (\Exception $e) {
         }
         //Validation
         if (!$config instanceof ConfigStore) {
             throw new \RuntimeException("No configuration settings found.");
         }
         $instanceName = $config->get("client")->get("provider", "\\Touchbase\\API\\Client\\Provider\\CurlClientProvider");
         $instance = new $instanceName();
         //Validation
         if (!$instance instanceof ClientInterface) {
             throw new \InvalidArgumentException("Client provider must be an instance of \\Touchbase\\API\\Client\\ClientInterface");
         }
         //Configure
         $instance->configure($config);
         //Save
         StaticStore::shared()->set(self::CLIENT_KEY, $instance);
     }
     return $instance;
 }
Exemplo n.º 3
0
 /**
  *	Render With
  *	@param string $templateFile - Template file location
  *	@return string - Parsed template contents
  */
 public function renderWith($templateFile)
 {
     if (is_array($templateFile)) {
         $templateFile = call_user_func_array("Touchbase\\Filesystem\\Filesystem::buildPath", $templateFile);
     }
     foreach ($this->controller->templateSearchPaths() as $path) {
         $templateFileObj = File::create([$path, $templateFile]);
         if ($templateFileObj->exists()) {
             $templateFilePath = $templateFileObj->path;
             break;
         }
     }
     if (!isset($templateFilePath)) {
         return false;
     }
     $this->assign("controller", $this->controller);
     $this->assign("request", $this->controller->request());
     $this->assign("errors", $this->controller->errors);
     $this->assign("messages", $this->controller->messages);
     //TODO: Sort this out!
     if (isset($this->controller) && $this->controller instanceof \Touchbase\Control\WebpageController) {
         $this->assign("assets", $this->controller->response()->assets());
     }
     if ($template = $this->readTemplate($templateFilePath)) {
         /**
          *	Auto CSS / JS include
          */
         if (isset($this->controller) && $this->controller instanceof \Touchbase\Control\WebpageController) {
             //Include Template Styles
             $fileParts = pathinfo($templateFilePath);
             $templateName = strtolower(basename($fileParts['filename'], ".tpl"));
             //Make sure to only add styles for our phtml templates
             if ($fileParts['extension'] === "php") {
                 $this->controller->response()->assets()->includeStyle($templateName . ".css");
                 $this->controller->response()->assets()->includeScript($templateName . ".js");
                 //Include Controller Sytles
                 $controllerName = strtolower($this->controller->controllerName);
                 $this->controller->response()->assets()->includeStyle($controllerName . ".css");
                 $this->controller->response()->assets()->includeScript($controllerName . ".js");
             }
         }
         return $template;
     }
     return false;
 }
Exemplo n.º 4
0
 /**
  *	_configure
  *	@param \Touchbase\Core\Config\Store
  *	@return Touchbase\Core\Config\Store
  */
 private function _configure(ConfigStore $config)
 {
     $ns = $src = "";
     try {
         //Load Main Configuration File
         $configurationData = IniConfigProvider::create()->parseIniFile(File::create([BASE_PATH, 'config.ini']));
         $config->addConfig($configurationData->getConfiguration());
         $ns = $config->get("project")->get("namespace", "Project");
         $src = $config->get("project")->get("source", "src");
         //Load Extra Configuration Files
         $loadExtraConfig = function ($files, $configFilePath = BASE_PATH) use(&$loadExtraConfig, &$config) {
             if (!empty($files)) {
                 foreach ($files as $condition => $file) {
                     $extraConfigFile = File::create([$configFilePath, $file]);
                     //Not a domain, path or environment - load always
                     if (is_numeric($condition)) {
                         if ($extraConfigFile->exists()) {
                             $configurationData = IniConfigProvider::create()->parseIniFile($extraConfigFile);
                             $config->addConfig($extraConfig = $configurationData->getConfiguration());
                             $loadExtraConfig($extraConfig->get("config")->get("files", ""), File::buildPath($configFilePath, dirname($file)));
                         }
                         //We want to match a certain condition
                     } else {
                         if ((!Router::isCLI() && strpos(@$_SERVER['HTTP_X_FORWARDED_HOST'] ?: $_SERVER['HTTP_HOST'], $condition) !== false || !Router::isCLI() && strpos($_SERVER["REQUEST_URI"], $condition) === 0 || !Router::isCLI() && strpos($_SERVER["SERVER_NAME"], $condition) === 0 || strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN' && $condition == "windows" || defined("TOUCHBASE_ENV") && TOUCHBASE_ENV == $condition) && $extraConfigFile->exists()) {
                             $configurationData = IniConfigProvider::create()->parseIniFile($extraConfigFile);
                             $config->addConfig($extraConfig = $configurationData->getConfiguration());
                             $loadExtraConfig($extraConfig->get("config")->get("files", ""), File::buildPath($configFilePath, dirname($file)));
                         }
                     }
                 }
             }
         };
         $loadExtraConfig($config->get("config")->get("files", ""));
         StaticStore::shared()->set(ConfigStore::CONFIG_KEY, $config);
     } catch (\Exception $e) {
     }
     if (!defined('PROJECT_PATH')) {
         $psr0 = realpath(File::buildPath(BASE_PATH, $src, $ns));
         $psr4 = realpath(File::buildPath(BASE_PATH, $src));
         define('PROJECT_PATH', $psr0 ?: $psr4);
     }
     return $config;
 }
Exemplo n.º 5
0
 /**
  *	Path For Asset Url
  *	@param string assetUrl
  *	@return string
  */
 public static function pathForAssetUrl($assetUrl, $assetType = null)
 {
     if (Router::isSiteUrl($assetUrl)) {
         list($assetMapFragment, $assetUrl) = array_pad(explode("/", Router::relativeUrl($assetUrl), 2), 2, null);
         $file = File::create([BASE_PATH, static::pathForAssetMap($assetMapFragment), $assetUrl]);
         if ($file->exists()) {
             return $file->path;
         }
         // //Is it a folder?
         // $folder = Folder::create([BASE_PATH, static::pathForAssetMap($assetMapFragment), $assetUrl]);
         // if($folder->exists()){
         // 	return $folder->path;
         // }
         return null;
     }
     return $assetUrl;
 }
Exemplo n.º 6
0
 /**
  *	Validate Post Request
  *	This method adds default validation to input fields based on the served HTML.
  *	If any errors are found, the redirect will be made on the response object with the errors.
  *	@param \Touchbase\Control\HTTPRequest $request
  *	@param \Touchbase\Control\HTTPResponse &$response
  *	@param \Touchbase\Utils\Validation &$validation
  *	@return VOID
  */
 protected function validatePostRequest($request, &$response, &$validation)
 {
     if (!$request->isMainRequest()) {
         return;
     }
     $formNameToken = $request->_VAR("tb_form_token");
     $formName = substr($formNameToken, 0, strrpos($formNameToken, "_"));
     if (isset($formNameToken) && ($form = SessionStore::get($formNameToken, false))) {
         SessionStore::consume($formName, $formNameToken);
         $data = $request->_VARS();
         libxml_use_internal_errors(true);
         $dom = new \DOMDocument();
         $dom->loadHtml(gzinflate(base64_decode($form)), LIBXML_NOWARNING | LIBXML_NOERROR);
         if ($dom->documentElement->getAttribute("novalidate")) {
             return;
         }
         $formValidation = Validation::create($formName);
         $privateFields = [];
         foreach (["input", "textarea", "select"] as $tag) {
             foreach ($dom->getElementsByTagName($tag) as $input) {
                 if ($input->hasAttributes() && ($inputName = $input->getAttribute("name"))) {
                     $inputType = $input->getAttribute("type");
                     $inputValidation = Validation::create($inputName);
                     if ($inputType === "password") {
                         $privateFields[] = $inputName;
                     }
                     $inputValidation->type($inputType);
                     if ($inputType === "file" && $input->hasAttribute("accept")) {
                         $validTypes = explode(",", $input->getAttribute("accept"));
                         //File Extentions
                         $fileExt = array_filter($validTypes, function ($value) {
                             return strpos($value, ".") === 0;
                         });
                         if (!empty($fileExt)) {
                             $inputValidation->addRule(function ($value) use($fileExt) {
                                 $name = $value['name'];
                                 if (is_array($name)) {
                                     foreach ($name as $nme) {
                                         if (!in_array(pathinfo($nme, PATHINFO_EXTENSION), $fileExt)) {
                                             return false;
                                         }
                                     }
                                     return true;
                                 }
                                 return in_array(pathinfo($name, PATHINFO_EXTENSION), $fileExt);
                             }, "A file uploaded did not have the correct extension");
                         }
                         //Mime Types
                         $validTypes = array_diff($validTypes, $fileExt);
                         $implicitFileMime = array_filter($validTypes, function ($value) {
                             return strpos($value, "/*") !== false;
                         });
                         if (!empty($implicitFileMime)) {
                             $inputValidation->addRule(function ($value) use($implicitFileMime) {
                                 $tmpName = $value['tmp_name'];
                                 if (is_array($tmpName)) {
                                     foreach ($tmpName as $tmp) {
                                         $mime = strstr(File::create($tmp)->mime(), "/", true) . "/*";
                                         if (!in_array($mime, $implicitFileMime)) {
                                             return false;
                                         }
                                     }
                                     return true;
                                 }
                                 $mime = strstr(File::create($tmpName)->mime(), "/", true) . "/*";
                                 return in_array($mime, $implicitFileMime);
                             }, "A file uploaded did not have the correct mime type");
                         }
                         $validTypes = array_diff($validTypes, $implicitFileMime);
                         $fileMime = array_filter($validTypes, function ($value) {
                             return strpos($value, "/") !== false;
                         });
                         if (!empty($fileMime)) {
                             $inputValidation->addRule(function ($value) use($fileMime) {
                                 $tmpName = $value['tmp_name'];
                                 if (is_array($tmpName)) {
                                     foreach ($tmpName as $tmp) {
                                         if (!in_array(File::create($tmp)->mime(), $fileMime)) {
                                             return false;
                                         }
                                     }
                                     return true;
                                 }
                                 return in_array(File::create($tmpName)->mime(), $fileMime);
                             }, "A file uploaded did not have the correct mime type");
                         }
                     }
                     if ($input->hasAttribute("required")) {
                         $errorMessage = null;
                         if ($placeholder = $input->getAttribute("placeholder")) {
                             $errorMessage = sprintf("Please complete the `%s` field", $placeholder);
                         }
                         $inputValidation->required($errorMessage);
                     }
                     if ($input->hasAttribute("readonly")) {
                         $inputValidation->readonly($input->getAttribute("value"));
                     }
                     if ($input->hasAttribute("disabled")) {
                         $inputValidation->disabled();
                     }
                     if ($minLength = $input->getAttribute("minlength")) {
                         $inputValidation->minLength($minLength);
                     }
                     if ($maxLength = $input->getAttribute("maxlength")) {
                         $inputValidation->maxLength($maxLength);
                     }
                     if (in_array($inputType, ["number", "range", "date", "datetime", "datetime-local", "month", "time", "week"])) {
                         if ($min = $input->getAttribute("min")) {
                             $inputValidation->min($min, $inputType);
                         }
                         if ($max = $input->getAttribute("max")) {
                             $inputValidation->max($max, $inputType);
                         }
                     }
                     if ($pattern = $input->getAttribute("pattern")) {
                         $inputValidation->pattern($pattern);
                     }
                     if (count($inputValidation)) {
                         $validation->addRule($formValidation->addRule($inputValidation));
                     }
                 }
             }
         }
         if (!$validation->validate($data)) {
             $response->withData(array_diff_key($data, array_flip($privateFields)));
             $response->redirect(-1)->withErrors($validation->errors, $formName);
         }
     } else {
         $response->redirect(-1)->withErrors(["Session timed out, please try again."], $formName);
     }
 }
Exemplo n.º 7
0
 /**
  *	Set Layout
  *	@param string $layout
  *	@access public
  *	@return \Touchbase\View\Webpage
  */
 public function setLayout($layout)
 {
     if (is_array($layout)) {
         $layout = call_user_func_array("Touchbase\\Filesystem\\Filesystem::buildPath", $layout);
     }
     $ext = pathinfo($layout)['extension'];
     $filename = $layout . (empty($ext) ? ".tpl.php" : "");
     foreach ($this->controller->templateSearchPaths() as $path) {
         $layoutFile = File::create([$path, $filename]);
         if ($layoutFile->exists()) {
             $this->layout = $layoutFile->path;
             return $this;
         }
     }
     throw new \Exception("Layout Template Doesn't Exist: {$filename}");
 }