From 19985dbb8c0aa66dc4bf7905abc1148de909097d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Tue, 11 Jan 2022 12:35:47 +0100 Subject: prvi-commit --- admin/survey/minify/lib/Minify/Controller/Base.php | 81 +++++++++ .../survey/minify/lib/Minify/Controller/Files.php | 71 ++++++++ .../survey/minify/lib/Minify/Controller/Groups.php | 76 ++++++++ .../survey/minify/lib/Minify/Controller/MinApp.php | 196 +++++++++++++++++++++ admin/survey/minify/lib/Minify/Controller/Page.php | 69 ++++++++ 5 files changed, 493 insertions(+) create mode 100644 admin/survey/minify/lib/Minify/Controller/Base.php create mode 100644 admin/survey/minify/lib/Minify/Controller/Files.php create mode 100644 admin/survey/minify/lib/Minify/Controller/Groups.php create mode 100644 admin/survey/minify/lib/Minify/Controller/MinApp.php create mode 100644 admin/survey/minify/lib/Minify/Controller/Page.php (limited to 'admin/survey/minify/lib/Minify/Controller') diff --git a/admin/survey/minify/lib/Minify/Controller/Base.php b/admin/survey/minify/lib/Minify/Controller/Base.php new file mode 100644 index 0000000..557b514 --- /dev/null +++ b/admin/survey/minify/lib/Minify/Controller/Base.php @@ -0,0 +1,81 @@ + + */ +abstract class Minify_Controller_Base implements Minify_ControllerInterface +{ + + /** + * @var Minify_Env + */ + protected $env; + + /** + * @var Minify_Source_Factory + */ + protected $sourceFactory; + + /** + * @var LoggerInterface + */ + protected $logger; + + /** + * @param Minify_Env $env + * @param Minify_Source_Factory $sourceFactory + * @param LoggerInterface $logger + */ + public function __construct(Minify_Env $env, Minify_Source_Factory $sourceFactory, LoggerInterface $logger = null) + { + $this->env = $env; + $this->sourceFactory = $sourceFactory; + if (!$logger) { + $logger = new Logger('minify'); + } + $this->logger = $logger; + } + + /** + * Create controller sources and options for Minify::serve() + * + * @param array $options controller and Minify options + * + * @return Minify_ServeConfiguration + */ + abstract public function createConfiguration(array $options); + + /** + * Send message to the Minify logger + * + * @param string $msg + * + * @return null + * @deprecated use $this->logger + */ + public function log($msg) + { + trigger_error(__METHOD__ . ' is deprecated in Minify 3.0.', E_USER_DEPRECATED); + $this->logger->info($msg); + } + + /** + * {@inheritdoc} + */ + public function getEnv() + { + return $this->env; + } +} diff --git a/admin/survey/minify/lib/Minify/Controller/Files.php b/admin/survey/minify/lib/Minify/Controller/Files.php new file mode 100644 index 0000000..a9bb941 --- /dev/null +++ b/admin/survey/minify/lib/Minify/Controller/Files.php @@ -0,0 +1,71 @@ + + * $options = [ + * 'checkAllowDirs' => false, // allow files to be anywhere + * ]; + * $sourceFactory = new Minify_Source_Factory($env, $options, $cache); + * $controller = new Minify_Controller_Files($env, $sourceFactory); + * $minify->serve($controller, [ + * 'files' => [ + * '//js/jquery.js', + * '//js/plugins.js', + * '/home/username/file.js', + * ], + * ]); + * + * + * @package Minify + * @author Stephen Clay + */ +class Minify_Controller_Files extends Minify_Controller_Base +{ + + /** + * Set up file sources + * + * @param array $options controller and Minify options + * @return Minify_ServeConfiguration + * + * Controller options: + * + * 'files': (required) array of complete file paths, or a single path + */ + public function createConfiguration(array $options) + { + // strip controller options + + $files = $options['files']; + // if $files is a single object, casting will break it + if (is_object($files)) { + $files = array($files); + } elseif (! is_array($files)) { + $files = (array)$files; + } + unset($options['files']); + + $sources = array(); + foreach ($files as $file) { + try { + $sources[] = $this->sourceFactory->makeSource($file); + } catch (Minify_Source_FactoryException $e) { + $this->logger->error($e->getMessage()); + + return new Minify_ServeConfiguration($options); + } + } + + return new Minify_ServeConfiguration($options, $sources); + } +} + diff --git a/admin/survey/minify/lib/Minify/Controller/Groups.php b/admin/survey/minify/lib/Minify/Controller/Groups.php new file mode 100644 index 0000000..6d4e5f4 --- /dev/null +++ b/admin/survey/minify/lib/Minify/Controller/Groups.php @@ -0,0 +1,76 @@ + + * Minify::serve('Groups', array( + * 'groups' => array( + * 'css' => array('//css/type.css', '//css/layout.css') + * ,'js' => array('//js/jquery.js', '//js/site.js') + * ) + * )); + * + * + * If the above code were placed in /serve.php, it would enable the URLs + * /serve.php/js and /serve.php/css + * + * @package Minify + * @author Stephen Clay + */ +class Minify_Controller_Groups extends Minify_Controller_Files +{ + + /** + * Set up groups of files as sources + * + * @param array $options controller and Minify options + * + * 'groups': (required) array mapping PATH_INFO strings to arrays + * of complete file paths. @see Minify_Controller_Groups + * + * @return array Minify options + */ + public function createConfiguration(array $options) + { + // strip controller options + $groups = $options['groups']; + unset($options['groups']); + + $server = $this->env->server(); + + // mod_fcgid places PATH_INFO in ORIG_PATH_INFO + if (isset($server['ORIG_PATH_INFO'])) { + $pathInfo = substr($server['ORIG_PATH_INFO'], 1); + } elseif (isset($server['PATH_INFO'])) { + $pathInfo = substr($server['PATH_INFO'], 1); + } else { + $pathInfo = false; + } + + if (false === $pathInfo || ! isset($groups[$pathInfo])) { + // no PATH_INFO or not a valid group + $this->logger->info("Missing PATH_INFO or no group set for \"$pathInfo\""); + + return new Minify_ServeConfiguration($options); + } + + $files = $groups[$pathInfo]; + // if $files is a single object, casting will break it + if (is_object($files)) { + $files = array($files); + } elseif (! is_array($files)) { + $files = (array)$files; + } + + $options['files'] = $files; + + return parent::createConfiguration($options); + } +} + diff --git a/admin/survey/minify/lib/Minify/Controller/MinApp.php b/admin/survey/minify/lib/Minify/Controller/MinApp.php new file mode 100644 index 0000000..9a6a098 --- /dev/null +++ b/admin/survey/minify/lib/Minify/Controller/MinApp.php @@ -0,0 +1,196 @@ + + */ +class Minify_Controller_MinApp extends Minify_Controller_Base +{ + + /** + * Set up groups of files as sources + * + * @param array $options controller and Minify options + * + * @return array Minify options + */ + public function createConfiguration(array $options) + { + // PHP insecure by default: realpath() and other FS functions can't handle null bytes. + $get = $this->env->get(); + foreach (array('g', 'b', 'f') as $key) { + if (isset($get[$key])) { + $get[$key] = str_replace("\x00", '', (string)$get[$key]); + } + } + + // filter controller options + $defaults = array( + 'groupsOnly' => false, + 'groups' => array(), + 'symlinks' => array(), + ); + $minApp = isset($options['minApp']) ? $options['minApp'] : array(); + $localOptions = array_merge($defaults, $minApp); + + unset($options['minApp']); + + // normalize $symlinks in order to map to target + $symlinks = array(); + foreach ($localOptions['symlinks'] as $link => $target) { + if (0 === strpos($link, '//')) { + $link = rtrim(substr($link, 1), '/') . '/'; + $target = rtrim($target, '/\\'); + $symlinks[$link] = $target; + } + } + + $sources = array(); + $selectionId = ''; + $firstMissing = null; + + if (isset($get['g'])) { + // add group(s) + $selectionId .= 'g=' . $get['g']; + $keys = explode(',', $get['g']); + if ($keys != array_unique($keys)) { + $this->logger->info("Duplicate group key found."); + + return new Minify_ServeConfiguration($options); + } + foreach ($keys as $key) { + if (! isset($localOptions['groups'][$key])) { + $this->logger->info("A group configuration for \"{$key}\" was not found"); + + return new Minify_ServeConfiguration($options); + } + $files = $localOptions['groups'][$key]; + // if $files is a single object, casting will break it + if (is_object($files)) { + $files = array($files); + } elseif (! is_array($files)) { + $files = (array)$files; + } + foreach ($files as $file) { + try { + $source = $this->sourceFactory->makeSource($file); + $sources[] = $source; + } catch (Minify_Source_FactoryException $e) { + $this->logger->error($e->getMessage()); + if (null === $firstMissing) { + $firstMissing = basename($file); + continue; + } else { + $secondMissing = basename($file); + $this->logger->info("More than one file was missing: '$firstMissing', '$secondMissing'"); + + return new Minify_ServeConfiguration($options); + } + } + } + } + } + if (! $localOptions['groupsOnly'] && isset($get['f'])) { + // try user files + // The following restrictions are to limit the URLs that minify will + // respond to. + + // verify at least one file, files are single comma separated, and are all same extension + $validPattern = preg_match('/^[^,]+\\.(css|less|scss|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m); + $hasComment = strpos($get['f'], '//') !== false; + $hasEscape = strpos($get['f'], '\\') !== false; + + if (!$validPattern || $hasComment || $hasEscape) { + $this->logger->info("GET param 'f' was invalid"); + + return new Minify_ServeConfiguration($options); + } + + $ext = ".{$m[1]}"; + $files = explode(',', $get['f']); + if ($files != array_unique($files)) { + $this->logger->info("Duplicate files were specified"); + + return new Minify_ServeConfiguration($options); + } + + if (isset($get['b'])) { + // check for validity + $isValidBase = preg_match('@^[^/]+(?:/[^/]+)*$@', $get['b']); + $hasDots = false !== strpos($get['b'], '..'); + $isDot = $get['b'] === '.'; + + if ($isValidBase && !$hasDots && !$isDot) { + // valid base + $base = "/{$get['b']}/"; + } else { + $this->logger->info("GET param 'b' was invalid"); + + return new Minify_ServeConfiguration($options); + } + } else { + $base = '/'; + } + + $basenames = array(); // just for cache id + foreach ($files as $file) { + $uri = $base . $file; + $path = $this->env->getDocRoot() . $uri; + + // try to rewrite path + foreach ($symlinks as $link => $target) { + if (0 === strpos($uri, $link)) { + $path = $target . DIRECTORY_SEPARATOR . substr($uri, strlen($link)); + break; + } + } + + try { + $source = $this->sourceFactory->makeSource($path); + $sources[] = $source; + $basenames[] = basename($path, $ext); + } catch (Minify_Source_FactoryException $e) { + $this->logger->error($e->getMessage()); + if (null === $firstMissing) { + $firstMissing = $uri; + continue; + } else { + $secondMissing = $uri; + $this->logger->info("More than one file was missing: '$firstMissing', '$secondMissing`'"); + + return new Minify_ServeConfiguration($options); + } + } + } + if ($selectionId) { + $selectionId .= '_f='; + } + $selectionId .= implode(',', $basenames) . $ext; + } + + if (!$sources) { + $this->logger->info("No sources to serve"); + + return new Minify_ServeConfiguration($options); + } + + if (null !== $firstMissing) { + array_unshift($sources, new Minify_Source(array( + 'id' => 'missingFile', + // should not cause cache invalidation + 'lastModified' => 0, + // due to caching, filename is unreliable. + 'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n", + 'minifier' => 'Minify::nullMinifier', + ))); + } + + return new Minify_ServeConfiguration($options, $sources, $selectionId); + } +} diff --git a/admin/survey/minify/lib/Minify/Controller/Page.php b/admin/survey/minify/lib/Minify/Controller/Page.php new file mode 100644 index 0000000..8ca00d5 --- /dev/null +++ b/admin/survey/minify/lib/Minify/Controller/Page.php @@ -0,0 +1,69 @@ + + */ +class Minify_Controller_Page extends Minify_Controller_Base +{ + + /** + * Set up source of HTML content + * + * @param array $options controller and Minify options + * @return array Minify options + * + * Controller options: + * + * 'content': (required) HTML markup + * + * 'id': (required) id of page (string for use in server-side caching) + * + * 'lastModifiedTime': timestamp of when this content changed. This + * is recommended to allow both server and client-side caching. + * + * 'minifyAll': should all CSS and Javascript blocks be individually + * minified? (default false) + */ + public function createConfiguration(array $options) + { + if (isset($options['file'])) { + $sourceSpec = array( + 'filepath' => $options['file'] + ); + $f = $options['file']; + } else { + // strip controller options + $sourceSpec = array( + 'content' => $options['content'], + 'id' => $options['id'], + ); + $f = $options['id']; + unset($options['content'], $options['id']); + } + // something like "builder,index.php" or "directory,file.html" + $selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,'); + + if (isset($options['minifyAll'])) { + // this will be the 2nd argument passed to Minify_HTML::minify() + $sourceSpec['minifyOptions'] = array( + 'cssMinifier' => array('Minify_CSSmin', 'minify'), + 'jsMinifier' => array('JSMin\\JSMin', 'minify'), + ); + unset($options['minifyAll']); + } + + $sourceSpec['contentType'] = Minify::TYPE_HTML; + $sources[] = new Minify_Source($sourceSpec); + + return new Minify_ServeConfiguration($options, $sources, $selectionId); + } +} + -- cgit v1.2.3