コード例 #1
0
 /**
  * Check SSL
  *
  * @return bool If using ssl returns true, otherwise returns false.
  * @deprecated
  */
 function isSsl()
 {
     if (!is_null(self::$isSSL)) {
         return self::$isSSL;
     }
     $url_info = parse_url(Context::getRequestUrl());
     self::$isSSL = $url_info['scheme'] == 'https';
     return self::$isSSL;
 }
コード例 #2
0
 /**
  * Get file information
  *
  * @param string $fileName The file name
  * @param string $targetIe Target IE of file
  * @param string $media Media of file
  * @param bool $forceMinify Whether this file should be minified
  * @return stdClass The file information
  */
 private function getFileInfo($fileName, $targetIe = '', $media = 'all', $forceMinify = false)
 {
     static $existsInfo = array();
     if (self::$minify === null) {
         self::$minify = Context::getDBInfo()->minify_scripts ?: 'common';
     }
     if (isset($existsInfo[$existsKey])) {
         return $existsInfo[$existsKey];
     }
     $pathInfo = pathinfo($fileName);
     $file = new stdClass();
     $file->fileName = $pathInfo['basename'];
     $file->filePath = $this->_getAbsFileUrl($pathInfo['dirname']);
     $file->fileRealPath = FileHandler::getRealPath($pathInfo['dirname']);
     $file->fileExtension = strtolower($pathInfo['extension']);
     if (preg_match('/^(.+)\\.min$/', $pathInfo['filename'], $matches)) {
         $file->fileNameNoExt = $matches[1];
         $file->isMinified = true;
     } else {
         $file->fileNameNoExt = $pathInfo['filename'];
         $file->isMinified = false;
     }
     $file->isExternalURL = preg_match('@^(https?:)?//@i', $file->filePath) ? true : false;
     $file->isCachedScript = !$file->isExternalURL && strpos($file->filePath, 'files/cache/') !== false;
     $file->keyName = $file->fileNameNoExt . '.' . $file->fileExtension;
     $file->cdnPath = $this->_normalizeFilePath($pathInfo['dirname']);
     $originalFilePath = $file->fileRealPath . '/' . $pathInfo['basename'];
     // Fix incorrectly minified URL
     if ($file->isMinified && !$file->isExternalURL && (!file_exists($originalFilePath) || is_link($originalFilePath) || filesize($originalFilePath) < 32 && trim(file_get_contents($originalFilePath)) === $file->keyName)) {
         if (file_exists($file->fileRealPath . '/' . $file->fileNameNoExt . '.' . $file->fileExtension)) {
             $file->fileName = $file->fileNameNoExt . '.' . $file->fileExtension;
             $file->isMinified = false;
             $originalFilePath = $file->fileRealPath . '/' . $file->fileNameNoExt . '.' . $file->fileExtension;
         }
     }
     // Decide whether to minify this file
     if (self::$minify === 'all') {
         $minify_enabled = true;
     } elseif (self::$minify === 'none') {
         $minify_enabled = false;
     } else {
         $minify_enabled = $forceMinify;
     }
     // Minify file
     if ($minify_enabled && !$file->isMinified && !$file->isExternalURL && !$file->isCachedScript && strpos($file->filePath, 'common/js/plugins') === false) {
         if (($file->fileExtension === 'css' || $file->fileExtension === 'js') && file_exists($originalFilePath)) {
             $minifiedFileName = $file->fileNameNoExt . '.min.' . $file->fileExtension;
             $minifiedFileHash = ltrim(str_replace(array('/', '\\'), '.', $pathInfo['dirname']), '.');
             $minifiedFilePath = _XE_PATH_ . 'files/cache/minify/' . $minifiedFileHash . '.' . $minifiedFileName;
             if (!file_exists($minifiedFilePath) || filemtime($minifiedFilePath) < filemtime($originalFilePath)) {
                 if ($file->fileExtension === 'css') {
                     $minifier = new MatthiasMullie\Minify\CSS($originalFilePath);
                     $content = $minifier->execute($minifiedFilePath);
                 } else {
                     $minifier = new MatthiasMullie\Minify\JS($originalFilePath);
                     $content = $minifier->execute($minifiedFilePath);
                 }
                 FileHandler::writeFile($minifiedFilePath, $content);
             }
             $file->fileName = $minifiedFileHash . '.' . $minifiedFileName;
             $file->filePath = $this->_getAbsFileUrl('./files/cache/minify');
             $file->fileRealPath = _XE_PATH_ . 'files/cache/minify';
             $file->keyName = $minifiedFileHash . '.' . $file->fileNameNoExt . '.' . $file->fileExtension;
             $file->cdnPath = $this->_normalizeFilePath('./files/cache/minify');
             $file->isMinified = true;
         }
     }
     // Process targetIe and media attributes
     $file->targetIe = $targetIe;
     if ($file->fileExtension == 'css') {
         $file->media = $media;
         if (!$file->media) {
             $file->media = 'all';
         }
         $file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe . "\t" . $file->media;
     } else {
         if ($file->fileExtension == 'js') {
             $file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe;
         }
     }
     return $file;
 }
コード例 #3
0
 public function testHandler()
 {
     Context::setUseCdn('Y');
     Context::setRequestUrl('http://test.com');
     $handler = new FrontEndFileHandler();
     // js(head)
     $handler->loadFile(array('./common/js/jquery.js'));
     $handler->loadFile(array('./common/js/js_app.js'));
     $handler->loadFile(array('./common/js/common.js'));
     $handler->loadFile(array('./common/js/xml_handler.js'));
     $handler->loadFile(array('./common/js/xml_js_filter.js'));
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => '');
     $this->assertEquals($handler->getJsFileList(), $expected);
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     // js(body)
     $handler->loadFile(array('./common/js/jquery.js', 'body'));
     $handler->loadFile(array('./common/js/js_app.js', 'body'));
     $handler->loadFile(array('./common/js/common.js', 'body'));
     $handler->loadFile(array('./common/js/xml_handler.js', 'body'));
     $handler->loadFile(array('./common/js/xml_js_filter.js', 'body'));
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => '');
     $this->assertEquals($handler->getJsFileList('body'), $expected);
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     // css
     $handler->loadFile(array('./common/css/xe.css'));
     $handler->loadFile(array('./common/css/common.css'));
     $expected[] = array('file' => '/xe/common/css/xe.css', 'media' => 'all', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     // order (duplicate)
     $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => '');
     $this->assertEquals($handler->getJsFileList(), $expected);
     // order (redefine)
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/jquery.js', 'head', '', 1));
     $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => '');
     $this->assertEquals($handler->getJsFileList(), $expected);
     // unload
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/js/jquery.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/js_app.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/common.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_handler.js', 'head', '', -100000));
     $handler->loadFile(array('./common/js/xml_js_filter.js', 'head', '', -100000));
     $handler->unloadFile('./common/js/jquery.js', '', 'all');
     $expected[] = array('file' => '/xe/common/js/js_app.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/common.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_handler.js', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/js/xml_js_filter.js', 'targetie' => '');
     $this->assertEquals($handler->getJsFileList(), $expected);
     // target IE(js)
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie6'));
     $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie7'));
     $handler->loadFile(array('./common/js/jquery.js', 'head', 'ie8'));
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie6');
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie7');
     $expected[] = array('file' => '/xe/common/js/jquery.js', 'targetie' => 'ie8');
     $this->assertEquals($handler->getJsFileList(), $expected);
     // target IE(css)
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/css/common.css', null, 'ie6'));
     $handler->loadFile(array('./common/css/common.css', null, 'ie7'));
     $handler->loadFile(array('./common/css/common.css', null, 'ie8'));
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => 'ie6');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => 'ie7');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => 'ie8');
     $this->assertEquals($handler->getCssFileList(), $expected);
     // media
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/css/common.css', 'all'));
     $handler->loadFile(array('./common/css/common.css', 'screen'));
     $handler->loadFile(array('./common/css/common.css', 'handled'));
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'screen', 'targetie' => '');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'handled', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
     // CDN
     unset($handler);
     unset($expected);
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v');
     $expected[] = array('file' => 'http://static.xpressengine.com/core/v/common/css/common.css', 'media' => 'all', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
     // CDN(no cdn setting)
     unset($handler);
     unset($expected);
     Context::setUseCdn('N');
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
     // CDN(use ssl)
     unset($handler);
     unset($expected);
     Context::setUseCdn('Y');
     Context::setRequestUrl('https://test.com');
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('./common/css/common.css'), true, 'http://static.xpressengine.com/core/', 'v');
     $expected[] = array('file' => '/xe/common/css/common.css', 'media' => 'all', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
     // external file
     unset($handler);
     unset($expected);
     Context::setUseCdn('Y');
     Context::setRequestUrl('http://test.com');
     $handler = new FrontEndFileHandler();
     $handler->loadFile(array('http://external.com/css/style2.css'));
     $handler->loadFile(array('http://external.com/css/style.css'), true, 'http://static.xpressengine.com/core/', 'v');
     $expected[] = array('file' => 'http://external.com/css/style2.css', 'media' => 'all', 'targetie' => '');
     $expected[] = array('file' => 'http://external.com/css/style.css', 'media' => 'all', 'targetie' => '');
     $this->assertEquals($handler->getCssFileList(), $expected);
 }