Author: Stephen Clay (steve@mrclay.org)
Example #1
0
 public function action_index()
 {
     $group = $this->request->param('group');
     if (!empty($group)) {
         $_GET['g'] = $group;
     }
     $config = Kohana::$config->load('minify');
     Minify::$uploaderHoursBehind = $config['uploaderHoursBehind'];
     Minify::setCache(isset($config['cachePath']) ? $config['cachePath'] : '', $config['cacheFileLocking']);
     if ($config['documentRoot']) {
         $_SERVER['DOCUMENT_ROOT'] = $config['documentRoot'];
         Minify::$isDocRootSet = TRUE;
     }
     $serve_options = $config['serveOptions'];
     $serve_options['minifierOptions']['text/css']['symlinks'] = $config['symlinks'];
     foreach ($config['symlinks'] as $uri => $target) {
         $serve_options['minApp']['allowDirs'][] = $target;
     }
     if ($config['allowDebugFlag']) {
         $serve_options['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
     }
     if ($config['errorLogger']) {
         if (TRUE === $config['errorLogger']) {
             Minify_Logger::setLogger(FirePHP::getInstance(true));
         } else {
             Minify_Logger::setLogger($config['errorLogger']);
         }
     }
     // Check for URI versioning
     if (preg_match('/&\\d/', Arr::get($_SERVER, 'QUERY_STRING'))) {
         $serve_options['maxAge'] = 31536000;
     }
     if (isset($_GET['g'])) {
         // well need groups config
         $serve_options['minApp']['groups'] = $config['groupsConfig'];
     }
     if (isset($_GET['f']) or isset($_GET['g'])) {
         $response = Minify::serve(new Minify_Controller_MinApp(), array('quiet' => TRUE) + $serve_options);
         $this->response->headers($response['headers'])->status($response['statusCode'])->body($response['content']);
     }
 }
Example #2
0
 public function action_index()
 {
     extract(Kohana::$config->load('minify')->as_array(), EXTR_SKIP);
     if ($this->request->param('group')) {
         $_GET['g'] = $this->request->param('group');
     }
     Minify::$uploaderHoursBehind = $uploaderHoursBehind;
     Minify::setCache(isset($cachePath) ? $cachePath : '', $cacheFileLocking);
     if ($documentRoot) {
         $_SERVER['DOCUMENT_ROOT'] = $documentRoot;
         Minify::$isDocRootSet = TRUE;
     }
     $serveOptions['minifierOptions']['text/css']['symlinks'] = $symlinks;
     $serveOptions['minApp']['allowDirs'] += array_values($symlinks);
     if ($allowDebugFlag) {
         $serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
     }
     if ($errorLogger) {
         if (TRUE === $errorLogger) {
             $errorLogger = FirePHP::getInstance(TRUE);
         }
         Minify_Logger::setLogger($errorLogger);
     }
     // Check for URI versioning
     if (preg_match('/&\\d/', $_SERVER['QUERY_STRING'])) {
         $serveOptions['maxAge'] = 31536000;
     }
     if (isset($_GET['g'])) {
         // Well need groups config
         $serveOptions['minApp']['groups'] = $groupsConfig;
     }
     if (isset($_GET['f']) or isset($_GET['g'])) {
         set_time_limit(0);
         // Serve!
         $response = Minify::serve(new Minify_Controller_MinApp(), array('quiet' => TRUE) + $serveOptions);
         $this->response->headers($response['headers'])->body($response['content'])->status($response['statusCode']);
     }
 }
Example #3
0
}
require "{$min_libPath}/Minify/Loader.php";
Minify_Loader::register();
Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
Minify::setCache(isset($min_cachePath) ? $min_cachePath : '', $min_cacheFileLocking);
if ($min_documentRoot) {
    $_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
    Minify::$isDocRootSet = true;
}
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
// auto-add targets to allowDirs
foreach ($min_symlinks as $uri => $target) {
    $min_serveOptions['minApp']['allowDirs'][] = $target;
}
if ($min_allowDebugFlag) {
    $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
}
if ($min_errorLogger) {
    if (true === $min_errorLogger) {
        $min_errorLogger = FirePHP::getInstance(true);
    }
    Minify_Logger::setLogger($min_errorLogger);
}
// check for URI versioning
if (preg_match('/&\\d/', $_SERVER['QUERY_STRING'])) {
    $min_serveOptions['maxAge'] = 31536000;
}
if (isset($_GET['g'])) {
    // well need groups config
    $min_serveOptions['minApp']['groups'] = (require MINIFY_MIN_DIR . '/groupsConfig.php');
}
Example #4
0
// use an environment object to encapsulate all input
$server = $_SERVER;
if ($min_documentRoot) {
    $server['DOCUMENT_ROOT'] = $min_documentRoot;
}
$env = new Minify_Env(array('server' => $server));
// TODO probably should do this elsewhere...
$min_serveOptions['minifierOptions']['text/css']['docRoot'] = $env->getDocRoot();
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
// auto-add targets to allowDirs
foreach ($min_symlinks as $uri => $target) {
    $min_serveOptions['minApp']['allowDirs'][] = $target;
}
if ($min_allowDebugFlag) {
    // TODO get rid of static stuff
    $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
}
if (!empty($min_concatOnly)) {
    $min_serveOptions['concatOnly'] = true;
}
if ($min_errorLogger) {
    if (true === $min_errorLogger) {
        $min_errorLogger = FirePHP::getInstance(true);
    }
    // TODO get rid of global state
    Minify_Logger::setLogger($min_errorLogger);
}
// check for URI versioning
if (null !== $env->get('v') || preg_match('/&\\d/', $env->server('QUERY_STRING'))) {
    $min_serveOptions['maxAge'] = 31536000;
}
 /**
  * Serves friendly minify url with minified contents
  *
  * This actually does a remote get from Minify's dir with appropriate `?f`
  * query variable built from persistently stored detected enqueued files.
  * All minified contents as well as headers should be preserved.
  *
  * Serving friendly minify urls like this cause a lot of overheads, but is
  * safer to use than server rewrite rules.
  *
  * @uses wp_remote_get()
  * @return void
  */
 public function serve($query)
 {
     $group_handle = isset($_GET['min_group']) ? $this->_sanitize_request($_GET['min_group']) : '';
     $type = isset($_GET['min_type']) && in_array($_GET['min_type'], array('js', 'css')) ? $_GET['min_type'] : '';
     $bid = isset($_GET['blog']) ? (int) $_GET['blog'] : 0;
     if (empty($group_handle) || empty($type) || empty($bid)) {
         // if missing any important data, do not proceed
         return;
     }
     global $blog_id;
     if ($bid != $blog_id) {
         // if not on the correct blog, do not proceed
         return;
     }
     $group = $this->_detector->get_group($group_handle);
     if (false == $group) {
         // this should not happen, but just in case it does
         echo sprintf(__('Minify group %s not found.', $this->_domain), $group_handle);
         exit;
     }
     // load Minify class based on default or custom Minify directory
     $min_dir = $this->_main->get_min_dir();
     if (empty($min_dir) || !file_exists($min_dir . 'config.php')) {
         // if Minify directory is not valid or Minify library is not found
         // we can not continue
         _e('Invalid Minify directory, please check Minify settings.', $this->_domain);
         exit;
     }
     // prepare input for Minify to handle
     $_GET['f'] = $group['string'];
     if ($this->_options['enable_debug'] == 'yes') {
         // add debug flag if needed
         $_GET['debug'] = 1;
     } else {
         // minified contents are often served compressed so it's best to turn off
         // error reporting to avoid content encoding error, unless debug is
         // enabled. This is useful when Minify doesn't catch all the notices
         // (for example when the cache directory is not writable).
         error_reporting(0);
     }
     // load Minify classes
     define('MINIFY_MIN_DIR', $min_dir);
     require_once MINIFY_MIN_DIR . '/config.php';
     require_once $min_libPath . '/Minify/Loader.php';
     Minify_Loader::register();
     // set some optional for the Minify application based on current settings
     Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
     // set cache file name (id) and cache path if needed
     Minify::setCacheId('minify-b' . $blog_id . '-' . $group_handle . '.' . $type);
     Minify::setCache(isset($min_cachePath) ? $min_cachePath : '', $min_cacheFileLocking);
     if ($min_documentRoot) {
         $_SERVER['DOCUMENT_ROOT'] = $min_documentRoot;
         Minify::$isDocRootSet = true;
     }
     // set serve options for each file type if needed
     $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
     foreach ($min_symlinks as $uri => $target) {
         $min_serveOptions['minApp']['allowDirs'][] = $target;
     }
     if ($min_allowDebugFlag) {
         // allow debugging Minify application
         $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
     }
     if ($min_errorLogger) {
         // log Minify error if allowed
         if (true === $min_errorLogger) {
             $min_errorLogger = FirePHP::getInstance(true);
         }
         Minify_Logger::setLogger($min_errorLogger);
     }
     // serve minified contents, on the fly or from cache
     $min_serveController = new Minify_Controller_MinApp();
     Minify::serve($min_serveController, $min_serveOptions);
     // for a proper response headers
     exit;
 }
Example #6
0
 /**
  * Constructor
  *
  * @param string $dir Directory containing config files
  */
 public function __construct($dir)
 {
     $that = $this;
     $this->dir = rtrim($dir, '/\\');
     $this->cache = function (App $app) use($that) {
         $config = $app->config;
         if ($config->cachePath instanceof \Minify_CacheInterface) {
             return $config->cachePath;
         }
         if (!$config->cachePath || is_string($config->cachePath)) {
             return new \Minify_Cache_File($config->cachePath, $config->cacheFileLocking, $app->logger);
         }
         $type = $that->typeOf($config->cachePath);
         throw new \RuntimeException('$min_cachePath must be a path or implement Minify_CacheInterface.' . " Given {$type}");
     };
     $this->config = function (App $app) {
         $config = (require $app->configPath);
         if ($config instanceof \Minify\Config) {
             return $config;
         }
         // copy from vars into properties
         $config = new \Minify\Config();
         $propNames = array_keys(get_object_vars($config));
         $prefixer = function ($name) {
             return "min_{$name}";
         };
         $varNames = array_map($prefixer, $propNames);
         $vars = compact($varNames);
         foreach ($varNames as $varName) {
             if (isset($vars[$varName])) {
                 $config->{substr($varName, 4)} = $vars[$varName];
             }
         }
         return $config;
     };
     $this->configPath = "{$this->dir}/config.php";
     $this->controller = function (App $app) use($that) {
         $config = $app->config;
         if (empty($config->factories['controller'])) {
             $ctrl = new \Minify_Controller_MinApp($app->env, $app->sourceFactory, $app->logger);
         } else {
             $ctrl = call_user_func($config->factories['controller'], $app);
         }
         if ($ctrl instanceof \Minify_ControllerInterface) {
             return $ctrl;
         }
         $type = $that->typeOf($ctrl);
         throw new \RuntimeException('$min_factories["controller"] callable must return an implementation' . " of Minify_CacheInterface. Returned {$type}");
     };
     $this->docRoot = function (App $app) {
         $config = $app->config;
         if (empty($config->documentRoot)) {
             return $app->env->getDocRoot();
         }
         return $app->env->normalizePath($config->documentRoot);
     };
     $this->env = function (App $app) {
         $config = $app->config;
         $envArgs = empty($config->envArgs) ? array() : $config->envArgs;
         return new \Minify_Env($envArgs);
     };
     $this->errorLogHandler = function (App $app) {
         $format = "%channel%.%level_name%: %message% %context% %extra%";
         $handler = new \Monolog\Handler\ErrorLogHandler();
         $handler->setFormatter(new \Monolog\Formatter\LineFormatter($format));
         return $handler;
     };
     $this->groupsConfig = function (App $app) {
         return require $app->groupsConfigPath;
     };
     $this->groupsConfigPath = "{$this->dir}/groupsConfig.php";
     $this->logger = function (App $app) use($that) {
         $value = $app->config->errorLogger;
         if ($value instanceof \Psr\Log\LoggerInterface) {
             return $value;
         }
         $logger = new \Monolog\Logger('minify');
         if (!$value) {
             return $logger;
         }
         if ($value === true || $value instanceof \FirePHP) {
             $logger->pushHandler($app->errorLogHandler);
             $logger->pushHandler(new \Monolog\Handler\FirePHPHandler());
             return $logger;
         }
         if ($value instanceof \Monolog\Handler\HandlerInterface) {
             $logger->pushHandler($value);
             return $logger;
         }
         // BC
         if (is_object($value) && is_callable(array($value, 'log'))) {
             $handler = new \Minify\Logger\LegacyHandler($value);
             $logger->pushHandler($handler);
             return $logger;
         }
         $type = $that->typeOf($value);
         throw new \RuntimeException('If set, $min_errorLogger must be a PSR-3 logger or a Monolog handler.' . " Given {$type}");
     };
     $this->minify = function (App $app) use($that) {
         $config = $app->config;
         if (empty($config->factories['minify'])) {
             return new \Minify($app->cache, $app->logger);
         }
         $minify = call_user_func($config->factories['minify'], $app);
         if ($minify instanceof \Minify) {
             return $minify;
         }
         $type = $that->typeOf($minify);
         throw new \RuntimeException('$min_factories["minify"] callable must return a Minify object.' . " Returned {$type}");
     };
     $this->serveOptions = function (App $app) {
         $config = $app->config;
         $env = $app->env;
         $ret = $config->serveOptions;
         $ret['minifierOptions']['text/css']['docRoot'] = $app->docRoot;
         $ret['minifierOptions']['text/css']['symlinks'] = $config->symlinks;
         $ret['minApp']['symlinks'] = $config->symlinks;
         // auto-add targets to allowDirs
         foreach ($config->symlinks as $uri => $target) {
             $ret['minApp']['allowDirs'][] = $target;
         }
         if ($config->allowDebugFlag) {
             $ret['debug'] = \Minify_DebugDetector::shouldDebugRequest($env);
         }
         if ($config->concatOnly) {
             $ret['concatOnly'] = true;
         }
         // check for URI versioning
         if ($env->get('v') !== null || preg_match('/&\\d/', $app->env->server('QUERY_STRING'))) {
             $ret['maxAge'] = 31536000;
         }
         // need groups config?
         if ($env->get('g') !== null) {
             $ret['minApp']['groups'] = $app->groupsConfig;
         }
         return $ret;
     };
     $this->sourceFactory = function (App $app) {
         return new \Minify_Source_Factory($app->env, $app->sourceFactoryOptions, $app->cache);
     };
     $this->sourceFactoryOptions = function (App $app) {
         $serveOptions = $app->serveOptions;
         $ret = array();
         // translate legacy setting to option for source factory
         if (isset($serveOptions['minApp']['noMinPattern'])) {
             $ret['noMinPattern'] = $serveOptions['minApp']['noMinPattern'];
         }
         if (isset($serveOptions['minApp']['allowDirs'])) {
             $ret['allowDirs'] = $serveOptions['minApp']['allowDirs'];
         }
         if (isset($serveOptions['checkAllowDirs'])) {
             $ret['checkAllowDirs'] = $serveOptions['checkAllowDirs'];
         }
         if (is_numeric($app->config->uploaderHoursBehind)) {
             $ret['uploaderHoursBehind'] = $app->config->uploaderHoursBehind;
         }
         return $ret;
     };
 }
Example #7
0
 public function indexAction()
 {
     include APP_PATH . '/library/Thirdpart/Minify/bootstrap.php';
     // set config path defaults
     $min_configPaths = array('base' => CONF_PATH . '/minify-config.php', 'test' => CONF_PATH . '/minify-config-test.php');
     // check for custom config paths
     if (!empty($min_customConfigPaths) && is_array($min_customConfigPaths)) {
         $min_configPaths = array_merge($min_configPaths, $min_customConfigPaths);
     }
     // load config
     require $min_configPaths['base'];
     if (isset($_GET['test'])) {
         include $min_configPaths['test'];
     }
     // setup factories
     $defaultFactories = array('minify' => function (Minify_CacheInterface $cache) {
         return new Minify($cache);
     }, 'controller' => function (Minify_Env $env, Minify_Source_Factory $sourceFactory) {
         return new Minify_Controller_MinApp($env, $sourceFactory);
     });
     if (!isset($min_factories)) {
         $min_factories = array();
     }
     $min_factories = array_merge($defaultFactories, $min_factories);
     // use an environment object to encapsulate all input
     $server = $_SERVER;
     if ($min_documentRoot) {
         $server['DOCUMENT_ROOT'] = $min_documentRoot;
     }
     $env = new Minify_Env(array('server' => $server));
     // TODO probably should do this elsewhere...
     $min_serveOptions['minifierOptions']['text/css']['docRoot'] = $env->getDocRoot();
     $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
     // auto-add targets to allowDirs
     foreach ($min_symlinks as $uri => $target) {
         $min_serveOptions['minApp']['allowDirs'][] = $target;
     }
     if ($min_allowDebugFlag) {
         // TODO get rid of static stuff
         $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
     }
     if (!empty($min_concatOnly)) {
         $min_serveOptions['concatOnly'] = true;
     }
     if ($min_errorLogger) {
         if (true === $min_errorLogger) {
             $min_errorLogger = FirePHP::getInstance(true);
         }
         // TODO get rid of global state
         Minify_Logger::setLogger($min_errorLogger);
     }
     // check for URI versioning
     if (null !== $env->get('v') || preg_match('/&\\d/', $env->server('QUERY_STRING'))) {
         $min_serveOptions['maxAge'] = 31536000;
     }
     // need groups config?
     if (null !== $env->get('g')) {
         // we need groups config
         $min_serveOptions['minApp']['groups'] = \Model\Minify::loadGroupConfig();
     }
     // cache defaults
     if (!isset($min_cachePath)) {
         $min_cachePath = '';
     }
     if (!isset($min_cacheFileLocking)) {
         $min_cacheFileLocking = true;
     }
     if (is_string($min_cachePath)) {
         $cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
     } else {
         // assume it meets interface.
         $cache = $min_cachePath;
     }
     /* @var Minify_CacheInterface $cache */
     $minify = call_user_func($min_factories['minify'], $cache);
     /* @var Minify $minify */
     if (!$env->get('f') && $env->get('g') === null) {
         // no spec given
         $msg = '<p>No "f" or "g" parameters were detected.</p>';
         $url = 'https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#long-url-parameters-are-ignored';
         $defaults = $minify->getDefaultOptions();
         $minify->errorExit($defaults['badRequestHeader'], $url, $msg);
     }
     $sourceFactoryOptions = array();
     // translate legacy setting to option for source factory
     if (isset($min_serveOptions['minApp']['noMinPattern'])) {
         $sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
     }
     $sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions, $cache);
     $controller = call_user_func($min_factories['controller'], $env, $sourceFactory);
     /* @var Minify_ControllerInterface $controller */
     $minify->serve($controller, $min_serveOptions);
     return false;
 }