/** * Initialize nyro */ private static function init() { if (!self::$cfg) { factory::init(); if (DEV) { debug::timer('nyro'); debug::timer('nyroProcess'); } request::init(); self::$cfg = new config(factory::loadCfg(__CLASS__)); file::init(); session::initFlash(); } }
/** * Add an include file * * @param array $prm Same parameter as addCss or addJs, with adding: * - string type File type (js or css) (required) * @return bool True if addedor already added, False if not found * @throws nExecption if type or file not provided * @see addJs, addCss */ public function add(array $prm) { if (config::initTab($prm, array( 'type'=>null, 'file'=>null, 'dir'=>'nyro', 'media'=>'screen', 'condIE'=>false, 'verifExists'=>true ))) { $ret = false; $firstFile = $prm['file']; if (strpos($firstFile, 'jquery') === 0 && $firstFile != 'jquery') $this->addJS('jquery'); foreach($this->getDepend($prm['file'], $prm['type']) as $d) { if (is_array($d)) $this->add(array_merge($prm, $d)); else $this->add(array_merge($prm, array('file'=>$d))); } foreach($this->cfg->getInArray($prm['type'], 'alias') as $k=>$a) { if (strtolower($prm['file']) == strtolower($k)) $prm['file'] = $a; } $prmType = $this->cfg->get($prm['type']); $locDir = $prm['dir']; if (!array_key_exists($locDir, $this->incFiles[$prm['type']])) $locDir = 'nyro'; $fileExt = $prm['file'].'.'.$prm['type']; if ($prm['verifExists']) $fileExists = $locDir == 'web' ? file::webExists($prmType['dirWeb'].DS.$fileExt) : file::nyroExists(array( 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$prm['type'].'_'.$prm['file'], 'type'=>'tpl', 'tplExt'=>$prm['type'] )); else $fileExists = true; if ($fileExists) { if (!isset($this->incFiles[$prm['type']][$locDir][$prm['media']])) $this->incFiles[$prm['type']][$locDir][$prm['media']] = array(); $this->incFiles[$prm['type']][$locDir][$prm['media']][$prm['file']] = $prm; if ($prm['type'] == 'css') { $c = file::read($fileExists); preg_match_all('`@import (url\()?"(.+).css"\)?;`', $c, $matches); if (!empty($matches[2])) { $prefix = substr($prm['file'], 0, strpos($prm['file'], '_')+1); foreach($matches[2 ] as $m) $this->add(array_merge($prm, array('file'=>$prefix.$m))); } } $ret = true; } foreach($this->getDepend($firstFile, $prm['type'], true) as $d) { if (is_array($d)) $this->add(array_merge($prm, $d)); else $this->add(array_merge($prm, array('file'=>$d))); } return $ret; } else throw new nException('reponse::add: parameters file and/or type not provied'); }
<?php /** * @author Cédric Nirousset <*****@*****.**> * @version 0.2 * @package nyroFwk */ require('../bootstrap.inc.php'); nyro::main();
/** * Return the configuration for a className * * @param string $className ClassName to load its configuration * @param bool $searchParent Indicate if the parent and implements class configuration should be searched * @return array Cfg Array parameter */ public static function loadCfg($className, $searchParent = true) { if (!array_key_exists($className, self::$loadedCfg)) { self::$loadedCfg[$className] = array(); if ($searchParent) { $ref = new nReflection($className); // Load the parent class configuration if ($parent = $ref->getParentClass()) { self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($parent->getName())); } // Load the implements class configuration if (count($implements = $ref->getInterfaces()) > 0) { foreach ($implements as $imp) { self::mergeCfg(self::$loadedCfg[$className], self::loadCfg($imp->getName())); } } } $listCfg = file::nyroExists(array('name' => $className, 'type' => 'cfg', 'rtl' => false, 'list' => true)); if (!empty($listCfg)) { foreach ($listCfg as $lc) { include $lc; if (isset($cfg)) { self::mergeCfg(self::$loadedCfg[$className], $cfg, $className); } $cfg = null; } } self::mergeCfg(self::$loadedCfg[$className], nyro::getGlobalCfg($className)); self::removeKeepUnique(self::$loadedCfg[$className]); } return self::$loadedCfg[$className]; }
/** * Compress the file requested, using MoxieCompressor library * * @param string $type File type (css or js) * @param array $prm Files to compress */ protected function compress($type, $prm) { $resp = response::getInstance(); if ($type == 'js') { $conf = $this->cfg->all; factory::mergeCfg($conf, $this->cfg->js); } else if ($type == 'css') { $conf = $this->cfg->all; factory::mergeCfg($conf, $this->cfg->css); } $key = $type.'--'.md5(serialize($prm)).'--'.md5(serialize($conf)); $supportsGzip = false; if ($conf['compress']) { $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))) : array(); if ($conf['gzip_compress'] && (in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('gzencode') && !ini_get('zlib.output_compression')) { $enc = in_array('x-gzip', $encodings) ? 'x-gzip' : 'gzip'; $supportsGzip = true; $key = 'gzip-'.$key; } } $content = null; $cache = cache::getInstance($this->cfg->cache); $cacheDate = $cache->get($content, array('id'=>$key)); if (!$conf['disk_cache'] || !$cacheDate) { foreach($prm as $file) { $f = file::nyroExists(array( 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$type.'_'.$file, 'type'=>'tpl', 'tplExt'=>$type )); if ($f) { if ($conf['php']) $content.= file::fetch($f); else $content.= file::read($f); } } if ($conf['compress']) { if ($type == 'js') { lib::load('jsMin'); $content = JSMin::minify($content); } else if ($type == 'css') { lib::load('cssMin'); $content = CssMin::minify($content, $conf['filters'], $conf['plugins']); } if ($supportsGzip) $content = gzencode($content, 9, FORCE_GZIP); } $cache->save(); } else if ($cacheDate) { $resp->addHeader('Age', time() - $cacheDate); } /* @var $resp response_http */ if ($conf['compress']) { $resp->setCompress(false); $resp->addHeader('Vary', 'Accept-Encoding'); // Handle proxies if ($conf['etags'] || preg_match('/MSIE/i', $_SERVER['HTTP_USER_AGENT'])) { // We need to use etags on IE since it will otherwise always load the contents $resp->addHeader('ETag', md5($content)); } $parseTime = $this->_parseTime($conf['expires_offset']); $resp->addHeader('Expires', gmdate('D, d M Y H:i:s', time() + $parseTime).' GMT'); $resp->addHeader('Cache-Control', 'public, max-age='.$parseTime); if ($type == 'js') { // Output explorer workaround or compressed file if (!isset($_GET['gz']) && $supportsGzip && $conf['patch_ie'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { // Build request URL $url = $_SERVER['REQUEST_URI']; if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) $url.= '?'.$_SERVER['QUERY_STRING'].'&gz=1'; else $url.= '?gz=1'; // This script will ensure that the gzipped script gets loaded on IE versions with the Gzip request chunk bug echo 'var gz;try {gz = new XMLHttpRequest();} catch(gz) { try {gz = new ActiveXObject("Microsoft.XMLHTTP");}'; echo 'catch (gz) {gz = new ActiveXObject("Msxml2.XMLHTTP");}}'; echo 'gz.open("GET", "'.$url.'", false);gz.send(null);eval(gz.responseText);'; die(); } } if ($supportsGzip) $resp->addHeader('Content-Encoding', $enc); } $resp->sendText($content); }