Example #1
0
 /**
  *	Init
  *	This method must return self
  *	@return self
  */
 public function init()
 {
     parent::init();
     $reflector = new \ReflectionClass($this);
     $this->_applicationNamespace = $reflector->getNamespaceName();
     //Define BASE Asset paths
     $assetConfig = $this->config()->get("assets");
     $assetPath = $assetConfig->get("assets", "assets");
     if (!defined("BASE_ASSETS")) {
         define("BASE_ASSETS", Router::buildPath(SITE_URL, $assetPath));
     }
     if (!defined("BASE_IMAGES")) {
         define("BASE_IMAGES", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("images", "images")));
     }
     if (!defined("BASE_STYLES")) {
         define("BASE_STYLES", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("css", "css")));
     }
     if (!defined("BASE_SCRIPTS")) {
         define("BASE_SCRIPTS", Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("js", "js")));
     }
     if (!defined("BASE_TEMPLATES")) {
         define("BASE_TEMPLATES", Filesystem::buildPath(PROJECT_PATH, $assetConfig->get("templates", "Templates")));
     }
     return $this;
 }
Example #2
0
 /**
  *	__construct
  *	@param object $autoLoader - Autoloader instance
  *	@param string $basePath
  */
 public function __construct($autoLoader = null, $basePath = null)
 {
     $this->_autoLoader = $autoLoader;
     //Base Working Directory
     if (!defined('BASE_PATH')) {
         if (is_null($basePath)) {
             define('BASE_PATH', rtrim(realpath(dirname($_SERVER['DOCUMENT_ROOT'])), DIRECTORY_SEPARATOR));
         } else {
             define('BASE_PATH', rtrim($basePath, DIRECTORY_SEPARATOR));
         }
     }
     //Error Logging
     $error = new \Touchbase\Debug\Error();
     //Configure Touchbase Project
     $this->setConfig($this->_configure(ConfigStore::create()));
     if (!defined('TOUCHBASE_ENV')) {
         define("TOUCHBASE_ENV", $this->config()->get("project")->get("environment", "production"), true);
     }
     //Work out touchbase path
     if ($this->_autoLoader instanceof \Composer\Autoload\ClassLoader) {
         $prefixes = $this->_autoLoader->getPrefixesPsr4();
         foreach ($prefixes as $prefix => $path) {
             if (rtrim($prefix, "\\") == __NAMESPACE__) {
                 $nsBasePath = realpath($path[0]) . DIRECTORY_SEPARATOR;
                 break;
             }
         }
     }
     if (!defined('TOUCHBASE_PATH')) {
         define('TOUCHBASE_PATH', rtrim(realpath($nsBasePath), DIRECTORY_SEPARATOR));
     }
     //Base Working Directory
     if (!defined('WORKING_DIR')) {
         // Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
         $path = realpath($_SERVER['SCRIPT_FILENAME']);
         if (substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) {
             $urlSegmentToRemove = substr($path, strlen(BASE_PATH));
             if (substr($_SERVER['SCRIPT_NAME'], -strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
                 $baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
             }
         }
         define('WORKING_DIR', strtolower(rtrim(isset($baseURL) ? $baseURL : $this->config()->get("project")->get("working_dir", ""), "/")) . "/");
     }
     if (!Router::isCLI()) {
         if (!defined('SITE_PROTOCOL')) {
             $protocol = '';
             if (isset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTOCOL']) == 'https') {
                 $protocol = 'https://';
             } else {
                 if (isset($_SERVER['SSL']) || !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                     $protocol = 'https://';
                 } else {
                     $protocol = 'http://';
                 }
             }
             define('SITE_PROTOCOL', $protocol);
         }
         if (!defined('SITE_URL')) {
             define("SITE_URL", Router::buildPath(SITE_PROTOCOL . htmlentities(filter_var(strtolower(@$_SERVER['HTTP_X_FORWARDED_HOST'] ?: $_SERVER['HTTP_HOST']), FILTER_SANITIZE_URL), ENT_QUOTES, 'UTF-8'), WORKING_DIR), true);
             define("SITE_ROOT", SITE_URL, true);
         }
     }
     // \pre_r(BASE_PATH);
     // \pre_r(TOUCHBASE_PATH);
     // \pre_r(PROJECT_PATH);
     // \pre_r(SITE_URL);
     // \pre_r(TOUCHBASE_ENV);
 }
Example #3
0
 /**
  *	Asset Include Snipit
  *	This will find an asset and import it with the correct html tag
  *	@param string $assetType
  *	@param string $file
  *	@param array $options
  *	@return string - The html snipit
  */
 private function assetIncludeSnipit($assetType, $file, $options)
 {
     foreach ($this->assetSearchPaths($assetType) as $path) {
         if (Router::pathForAssetUrl($filePath = Router::buildPath($path, $file), $assetType)) {
             switch ($assetType) {
                 case self::CSS:
                     return HTML::link()->attr($options)->attr(["rel" => "stylesheet", "type" => "text/css", "href" => $filePath]);
                     break;
                 case self::JS:
                     return HTML::script()->attr($options)->attr(["type" => "text/javascript", "src" => $filePath]);
                     break;
                 case self::IMG:
                     return HTML::img()->attr($options)->attr(["src" => $filePath]);
                     break;
             }
         }
     }
     return NULL;
 }
Example #4
0
 /**
  *	Redirect
  *	Redirect the response
  *	@param string $destinationUrl
  *	@param int $statusCode
  *	@return \Touchbase\Control\HTTPResponse
  */
 public function redirect($destinationUrl, $statusCode = 302)
 {
     if (is_numeric($destinationUrl) && $destinationUrl < 0) {
         $routeHistory = Router::routeHistory();
         $destinationUrl = $routeHistory[count($routeHistory) - abs($destinationUrl)];
     }
     if (Router::isRelativeURL($destinationUrl)) {
         $destinationUrl = Router::buildPath(SITE_URL, $destinationUrl);
     }
     $statusCode = in_array($statusCode, $this->redirectCodes) ? $statusCode : 302;
     $this->setStatusCode($statusCode);
     $this->addHeader('Location', $destinationUrl);
     return $this;
 }
Example #5
0
    /**
     *	__construct
     *	@param \Touchbase\Control\WebpageController $controller
     */
    public function __construct(WebpageController $controller)
    {
        $this->controller = $controller;
        //Set Default Title, if available...
        $this->assets()->pushTitle($controller->config("project")->get("name", null));
        //Add Defualt Meta
        $this->assets()->includeMeta(HTML::meta()->attr('charset', 'UTF-8'));
        $this->assets()->includeMeta(HTML::meta()->attr('http-equiv', 'Content-type')->attr('content', 'text/html; charset=utf-8'));
        $this->assets()->includeMeta('generator', 'Touchbase - http://touchbase.io');
        //Include base url
        $this->assets()->includeExtra(HTML::base()->href(Router::buildPath(SITE_URL, $_SERVER['REQUEST_URI'])));
        //WebApp
        $this->assets()->includeMeta('HandheldFriendly', 'true');
        $this->assets()->includeMeta('MobileOptimized', '320');
        $this->assets()->includeMeta(HTML::meta()->attr('http-equiv', 'cleartype')->attr('content', 'on'));
        $this->assets()->includeMeta('viewport', 'user-scalable=no, initial-scale=1.0, maximum-scale=1.0');
        $this->assets()->includeMeta('apple-mobile-web-app-status-bar-style', 'black-translucent');
        $this->assets()->includeMeta('apple-mobile-web-app-capable', 'yes');
        $this->assets()->includeMeta('apple-mobile-web-app-title', $this->controller->config("project")->get("name", null));
        $this->assets()->includeMeta('mobile-web-app-capable', 'yes');
        //WebAppIcons
        $manifest = File::create([Router::pathForAssetUrl(BASE_IMAGES), 'icons', 'manifest.json']);
        if ($manifest->exists()) {
            $manifestData = json_decode($manifest->read());
            $manifestUrl = Router::urlForPath($manifest->folder->path);
            $this->assets()->includeExtra(HTML::link()->href(Router::buildPath($manifestUrl, $manifest->name))->rel("manifest"));
            //Launch Images
            //Icons
            foreach ($manifestData->icons as $icon) {
                $this->assets()->includeExtra(HTML::link()->href(Router::buildPath($manifestUrl, $icon->src))->sizes($icon->sizes)->rel("apple-touch-icon-precomposed"));
            }
        }
        //Prevent Opening WebApp Links In Mobile Safari!
        $this->assets()->includeScript(HTML::script('(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i,g=a.documentElement;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1);g.className=g.className+" "+c}})(document,window.navigator,"standalone")'), true);
        //Add Base Scripts
        $assetConfig = $controller->config("assets");
        $assetPath = $assetConfig->get("assets", "assets");
        $baseScripts = Router::buildPath(SITE_URL, $assetPath, $assetConfig->get("js", "js"));
        //Include Modernizr
        $this->assets()->includeScript([$baseScripts, 'modernizr.min.js'], true);
        //Include jQuery?
        if ($jqVersion = $controller->config("assets")->get("jquery_version", false)) {
            //Load From Google
            $jqueryPath = Router::buildPath($jqVersion, "jquery.min.js");
            $this->assets()->includeScript(Router::buildPath("//ajax.googleapis.com/ajax/libs/jquery/", $jqueryPath));
            //If That Fails Load Locally
            if (Router::pathForAssetUrl($jqueryUrl = Router::buildPath($baseScripts, "vendor", "jquery", $jqueryPath))) {
                $this->assets()->includeScript(HTML::script('window.jQuery||document.write(\'<script src="' . $jqueryUrl . '"><\\/script>\')'));
            }
        }
        //Include Google Analytics
        $googleAnalyticsCode = $controller->config("project")->get("analytics", null);
        if (isset($googleAnalyticsCode) && Router::isLive()) {
            $this->assets()->includeScript("<script>\n\t\t\t  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n\t\t\t  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n\t\t\t  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n\t\t\t  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n\n\t\t\t  ga('create', '{$googleAnalyticsCode}', 'auto');\n\t\t\t  ga('send', 'pageview');\n\t\t\t</script>");
        }
        if (Router::pathForAssetUrl($fastclickUrl = Router::buildPath($baseScripts, "vendor", "fastclick.js"))) {
            $this->assets()->includeScript($fastclickUrl);
            $this->assets()->includeScript(HTML::script('if ("addEventListener" in document) {
				document.addEventListener("DOMContentLoaded", function() {
					FastClick.attach(document.body);
				}, false);
			}'));
        }
        $this->setLayout($this->layout);
        //HTML
        $this->_htmlTag = HTML::html()->attr("lang", "en")->addClass('no-js');
        $this->_bodyTag = HTML::body();
    }