From c9ccb22951ddc2ccf31cc93ad85a69c77cc6b10e Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 11 Nov 2018 17:38:32 +0000 Subject: [PATCH] make handle_static its own extension --- .gitignore | 2 - .scrutinizer.yml | 2 +- core/_install.php | 2 +- core/page.php | 6 +-- core/sys_config.php | 2 +- ext/browser_search/main.php | 2 +- ext/handle_404/main.php | 31 +++--------- ext/handle_404/test.php | 3 -- ext/handle_ico/test.php | 2 +- ext/handle_static/main.php | 47 ++++++++++++++++++ .../handle_static}/modernizr-3.3.1.custom.js | 0 lib/shimmie.js => ext/handle_static/script.js | 0 {lib => ext/handle_static}/static/README.txt | 0 .../static/apple-touch-icon.png | Bin {lib => ext/handle_static}/static/favicon.ico | Bin {lib => ext/handle_static}/static/favicon.png | Bin {lib => ext/handle_static}/static/favicon.svg | 0 .../handle_static}/static/favicon_64.png | Bin {lib => ext/handle_static}/static/grey.gif | Bin {lib => ext/handle_static}/static/robots.txt | 0 .../handle_static/style.css | 0 ext/handle_static/test.php | 8 +++ index.php | 2 +- 23 files changed, 70 insertions(+), 39 deletions(-) create mode 100644 ext/handle_static/main.php rename {lib => ext/handle_static}/modernizr-3.3.1.custom.js (100%) rename lib/shimmie.js => ext/handle_static/script.js (100%) rename {lib => ext/handle_static}/static/README.txt (100%) rename {lib => ext/handle_static}/static/apple-touch-icon.png (100%) rename {lib => ext/handle_static}/static/favicon.ico (100%) rename {lib => ext/handle_static}/static/favicon.png (100%) rename {lib => ext/handle_static}/static/favicon.svg (100%) rename {lib => ext/handle_static}/static/favicon_64.png (100%) rename {lib => ext/handle_static}/static/grey.gif (100%) rename {lib => ext/handle_static}/static/robots.txt (100%) rename lib/shimmie.css => ext/handle_static/style.css (100%) create mode 100644 ext/handle_static/test.php diff --git a/.gitignore b/.gitignore index 4002f868..98c90ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,10 +2,8 @@ backup data images thumbs -!lib/images *.phar *.sqlite -/lib/vendor/ #Composer composer.phar diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 3dba09a6..f672bc78 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -3,7 +3,7 @@ imports: - php filter: - excluded_paths: [lib/*,ext/*/lib/*,ext/tagger/script.js,ext/chatbox/*] + excluded_paths: [ext/*/lib/*,ext/tagger/script.js,ext/chatbox/*] tools: external_code_coverage: true diff --git a/core/_install.php b/core/_install.php index 2b0a4eb3..3a876a7d 100644 --- a/core/_install.php +++ b/core/_install.php @@ -25,7 +25,7 @@ date_default_timezone_set('UTC'); Shimmie Installation - + diff --git a/core/page.php b/core/page.php index e16194a7..33dd7918 100644 --- a/core/page.php +++ b/core/page.php @@ -322,7 +322,7 @@ class Page { $this->add_html_header("", 40); - # 404/static handler will map these to themes/foo/bar.ico or lib/static/bar.ico + # static handler will map these to themes/foo/static/bar.ico or ext/handle_static/static/bar.ico $this->add_html_header("", 41); $this->add_html_header("", 42); @@ -335,7 +335,6 @@ class Page { /*** Generate CSS cache files ***/ $css_latest = $config_latest; $css_files = array_merge( - zglob("lib/shimmie.css"), zglob("ext/{".ENABLED_EXTS."}/style.css"), zglob("themes/$theme_name/style.css") ); @@ -365,9 +364,8 @@ class Page { "vendor/bower-asset/jquery-timeago/jquery.timeago.js", "vendor/bower-asset/tablesorter/jquery.tablesorter.min.js", "vendor/bower-asset/js-cookie/src/js.cookie.js", - "lib/modernizr-3.3.1.custom.js", + "ext/handle_static/modernizr-3.3.1.custom.js", ], - zglob("lib/shimmie.js"), zglob("ext/{".ENABLED_EXTS."}/script.js"), zglob("themes/$theme_name/script.js") ); diff --git a/core/sys_config.php b/core/sys_config.php index 891840db..296885a8 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -37,7 +37,7 @@ _d("SEARCH_ACCEL", false); // boolean use search accelerator _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse _d("VERSION", '2.7-beta'); // string shimmie version _d("TIMEZONE", null); // string timezone -_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,comment,tag_list,index,tag_edit,alias_editor"); // extensions to always enable +_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor"); // extensions to always enable _d("EXTRA_EXTS", ""); // string optional extra extensions _d("BASE_URL", null); // string force a specific base URL (default is auto-detect) _d("MIN_PHP_VERSION", '7.0');// string minimum supported PHP version diff --git a/ext/browser_search/main.php b/ext/browser_search/main.php index 719dddfc..653d4d2b 100644 --- a/ext/browser_search/main.php +++ b/ext/browser_search/main.php @@ -34,7 +34,7 @@ class BrowserSearch extends Extension { $search_title = $config->get_string('title'); $search_form_url = make_link('post/list/{searchTerms}'); $suggenton_url = make_link('browser_search/')."{searchTerms}"; - $icon_b64 = base64_encode(file_get_contents("lib/static/favicon.ico")); + $icon_b64 = base64_encode(file_get_contents("ext/handle_static/static/favicon.ico")); // Now for the XML $xml = " diff --git a/ext/handle_404/main.php b/ext/handle_404/main.php index 4c3eafdf..5da9dfc2 100644 --- a/ext/handle_404/main.php +++ b/ext/handle_404/main.php @@ -5,7 +5,7 @@ * Link: http://code.shishnet.org/shimmie2/ * License: GPLv2 * Visibility: admin - * Description: If Shimmie can't handle a request, check static files; if that fails, show a 404 + * Description: If no other extension puts anything onto the page, show 404 */ class Handle404 extends Extension { @@ -14,29 +14,12 @@ class Handle404 extends Extension { // hax. if($page->mode == "page" && (!isset($page->blocks) || $this->count_main($page->blocks) == 0)) { $h_pagename = html_escape(implode('/', $event->args)); - $f_pagename = preg_replace("/[^a-z_\-\.]+/", "_", $h_pagename); - $theme_name = $config->get_string("theme", "default"); - - if(file_exists("themes/$theme_name/$f_pagename") || file_exists("lib/static/$f_pagename")) { - $filename = file_exists("themes/$theme_name/$f_pagename") ? - "themes/$theme_name/$f_pagename" : "lib/static/$f_pagename"; - - $page->add_http_header("Cache-control: public, max-age=600"); - $page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT'); - $page->set_mode("data"); - $page->set_data(file_get_contents($filename)); - if(endsWith($filename, ".ico")) $page->set_type("image/x-icon"); - if(endsWith($filename, ".png")) $page->set_type("image/png"); - if(endsWith($filename, ".txt")) $page->set_type("text/plain"); - } - else { - log_debug("handle_404", "Hit 404: $h_pagename"); - $page->set_code(404); - $page->set_title("404"); - $page->set_heading("404 - No Handler Found"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Explanation", "No handler could be found for the page '$h_pagename'")); - } + log_debug("handle_404", "Hit 404: $h_pagename"); + $page->set_code(404); + $page->set_title("404"); + $page->set_heading("404 - No Handler Found"); + $page->add_block(new NavBlock()); + $page->add_block(new Block("Explanation", "No handler could be found for the page '$h_pagename'")); } } diff --git a/ext/handle_404/test.php b/ext/handle_404/test.php index 2d7c9f73..f02548a9 100644 --- a/ext/handle_404/test.php +++ b/ext/handle_404/test.php @@ -6,9 +6,6 @@ class Handle404Test extends ShimmiePHPUnitTestCase { $this->assert_text("No handler could be found for the page 'not/a/page'"); $this->assert_title('404'); $this->assert_response(404); - - $this->get_page('favicon.ico'); - $this->assert_response(200); } } diff --git a/ext/handle_ico/test.php b/ext/handle_ico/test.php index fa130100..2d6946eb 100644 --- a/ext/handle_ico/test.php +++ b/ext/handle_ico/test.php @@ -2,7 +2,7 @@ class IcoHandlerTest extends ShimmiePHPUnitTestCase { public function testIcoHander() { $this->log_in_as_user(); - $image_id = $this->post_image("lib/static/favicon.ico", "shimmie favicon"); + $image_id = $this->post_image("ext/handle_static/static/favicon.ico", "shimmie favicon"); $this->get_page("post/view/$image_id"); // test for no crash # FIXME: test that the thumb works diff --git a/ext/handle_static/main.php b/ext/handle_static/main.php new file mode 100644 index 00000000..0d0b8360 --- /dev/null +++ b/ext/handle_static/main.php @@ -0,0 +1,47 @@ + + * Link: http://code.shishnet.org/shimmie2/ + * License: GPLv2 + * Visibility: admin + * Description: If Shimmie can't handle a request, check static files ($theme/static/$filename, then ext/handle_static/static/$filename) + */ + +class HandleStatic extends Extension { + public function onPageRequest(PageRequestEvent $event) { + global $config, $page; + // hax. + if($page->mode == "page" && (!isset($page->blocks) || $this->count_main($page->blocks) == 0)) { + $h_pagename = html_escape(implode('/', $event->args)); + $f_pagename = preg_replace("/[^a-z_\-\.]+/", "_", $h_pagename); + $theme_name = $config->get_string("theme", "default"); + + $theme_file = "themes/$theme_name/static/$f_pagename"; + $static_file = "ext/handle_static/static/$f_pagename"; + + if(file_exists($theme_file) || file_exists($static_file)) { + $filename = file_exists($theme_file) ? $theme_file : $static_file; + + $page->add_http_header("Cache-control: public, max-age=600"); + $page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT'); + $page->set_mode("data"); + $page->set_data(file_get_contents($filename)); + if(endsWith($filename, ".ico")) $page->set_type("image/x-icon"); + if(endsWith($filename, ".png")) $page->set_type("image/png"); + if(endsWith($filename, ".txt")) $page->set_type("text/plain"); + } + } + } + + private function count_main($blocks) { + $n = 0; + foreach($blocks as $block) { + if($block->section == "main" && $block->is_content) $n++; // more hax. + } + return $n; + } + + public function get_priority(): int {return 98;} // before 404 +} + diff --git a/lib/modernizr-3.3.1.custom.js b/ext/handle_static/modernizr-3.3.1.custom.js similarity index 100% rename from lib/modernizr-3.3.1.custom.js rename to ext/handle_static/modernizr-3.3.1.custom.js diff --git a/lib/shimmie.js b/ext/handle_static/script.js similarity index 100% rename from lib/shimmie.js rename to ext/handle_static/script.js diff --git a/lib/static/README.txt b/ext/handle_static/static/README.txt similarity index 100% rename from lib/static/README.txt rename to ext/handle_static/static/README.txt diff --git a/lib/static/apple-touch-icon.png b/ext/handle_static/static/apple-touch-icon.png similarity index 100% rename from lib/static/apple-touch-icon.png rename to ext/handle_static/static/apple-touch-icon.png diff --git a/lib/static/favicon.ico b/ext/handle_static/static/favicon.ico similarity index 100% rename from lib/static/favicon.ico rename to ext/handle_static/static/favicon.ico diff --git a/lib/static/favicon.png b/ext/handle_static/static/favicon.png similarity index 100% rename from lib/static/favicon.png rename to ext/handle_static/static/favicon.png diff --git a/lib/static/favicon.svg b/ext/handle_static/static/favicon.svg similarity index 100% rename from lib/static/favicon.svg rename to ext/handle_static/static/favicon.svg diff --git a/lib/static/favicon_64.png b/ext/handle_static/static/favicon_64.png similarity index 100% rename from lib/static/favicon_64.png rename to ext/handle_static/static/favicon_64.png diff --git a/lib/static/grey.gif b/ext/handle_static/static/grey.gif similarity index 100% rename from lib/static/grey.gif rename to ext/handle_static/static/grey.gif diff --git a/lib/static/robots.txt b/ext/handle_static/static/robots.txt similarity index 100% rename from lib/static/robots.txt rename to ext/handle_static/static/robots.txt diff --git a/lib/shimmie.css b/ext/handle_static/style.css similarity index 100% rename from lib/shimmie.css rename to ext/handle_static/style.css diff --git a/ext/handle_static/test.php b/ext/handle_static/test.php new file mode 100644 index 00000000..20a2c88d --- /dev/null +++ b/ext/handle_static/test.php @@ -0,0 +1,8 @@ +get_page('favicon.ico'); + $this->assert_response(200); + } +} + diff --git a/index.php b/index.php index 4aedff09..e748c06b 100644 --- a/index.php +++ b/index.php @@ -59,7 +59,7 @@ if(!file_exists("vendor/")) { Shimmie Error - +