From d0058eff5c615b41c22826ee006905619ee98351 Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 18 Dec 2019 10:43:07 +0900 Subject: [PATCH 01/67] feat: send cache-control header when not logging in --- Controller/NetCommonsAppController.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 72f2ab0d..5847a3bb 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -271,7 +271,15 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - parent::afterFilter(); + if (Current::isLogin()) { + // ログインしている場合はキャッシュしない + $this->response->header('Pragma', 'no-cache'); + } else { + // CDN(Proxy)で使われるキャッシュヘッダーをセットする + $this->response->header('Cache-Control', 's-maxage=60, public'); + } + + parent::afterFilter(); } /** From fc1f39c6bce6c6709215299cc6cb49b0bcd01e91 Mon Sep 17 00:00:00 2001 From: watura Date: Tue, 17 Dec 2019 17:14:15 +0900 Subject: [PATCH 02/67] feat: application.local.yml replaces application.yml --- Config/bootstrap.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Config/bootstrap.php b/Config/bootstrap.php index 532436e5..0e4f7ddd 100644 --- a/Config/bootstrap.php +++ b/Config/bootstrap.php @@ -27,13 +27,13 @@ // Load application configurations $conf = array(); -$files = array('application.yml', 'application.local.yml'); +$files = array('application.yml', 'application.local.yml', env('HTTP_HOST') . '.yml', env('HTTP_X_FORWARDED_HOST') . '.yml'); foreach ($files as $file) { - if (file_exists(APP . 'Config' . DS . $file)) { - $conf = array_merge($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); - Configure::write($conf); - } + if (file_exists(APP . 'Config' . DS . $file)) { + $conf = array_replace_recursive($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); + } } +Configure::write($conf); // Load all plugins $plugins = App::objects('plugins'); From fcc8b1ca87b295768efc75ca21f213451315b55f Mon Sep 17 00:00:00 2001 From: Wataru Nishimoto Date: Thu, 19 Dec 2019 09:53:46 +0900 Subject: [PATCH 03/67] feat: add prefix for application..yml Co-Authored-By: Sakamoto, Kazunori --- Config/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Config/bootstrap.php b/Config/bootstrap.php index 0e4f7ddd..afe272d4 100644 --- a/Config/bootstrap.php +++ b/Config/bootstrap.php @@ -27,7 +27,7 @@ // Load application configurations $conf = array(); -$files = array('application.yml', 'application.local.yml', env('HTTP_HOST') . '.yml', env('HTTP_X_FORWARDED_HOST') . '.yml'); +$files = array('application.yml', 'application.local.yml', 'application.' . env('HTTP_HOST') . '.yml', 'application.' . env('HTTP_X_FORWARDED_HOST') . '.yml'); foreach ($files as $file) { if (file_exists(APP . 'Config' . DS . $file)) { $conf = array_replace_recursive($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); From 733944fc882bc3c0c6b67477faf9545ce2243012 Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 19 Dec 2019 10:01:05 +0900 Subject: [PATCH 04/67] style: use tab --- Config/bootstrap.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Config/bootstrap.php b/Config/bootstrap.php index afe272d4..cc3babab 100644 --- a/Config/bootstrap.php +++ b/Config/bootstrap.php @@ -29,9 +29,9 @@ $conf = array(); $files = array('application.yml', 'application.local.yml', 'application.' . env('HTTP_HOST') . '.yml', 'application.' . env('HTTP_X_FORWARDED_HOST') . '.yml'); foreach ($files as $file) { - if (file_exists(APP . 'Config' . DS . $file)) { - $conf = array_replace_recursive($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); - } + if (file_exists(APP . 'Config' . DS . $file)) { + $conf = array_replace_recursive($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); + } } Configure::write($conf); From d2bf861981460f0438cbfa2b5aa07acc884f844b Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 19 Dec 2019 10:05:16 +0900 Subject: [PATCH 05/67] style: use tab --- Controller/NetCommonsAppController.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 5847a3bb..2d8b1a4e 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -271,15 +271,15 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin()) { - // ログインしている場合はキャッシュしない - $this->response->header('Pragma', 'no-cache'); - } else { - // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $this->response->header('Cache-Control', 's-maxage=60, public'); - } - - parent::afterFilter(); + if (Current::isLogin()) { + // ログインしている場合はキャッシュしない + $this->response->header('Pragma', 'no-cache'); + } else { + // CDN(Proxy)で使われるキャッシュヘッダーをセットする + $this->response->header('Cache-Control', 's-maxage=60, public'); + } + + parent::afterFilter(); } /** From 38db020fcde2a7f8e0a393775076e6a48b34178c Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 20 Dec 2019 07:22:02 +0900 Subject: [PATCH 06/67] feat: use App.Cache.sMaxAge for s-maxage header --- Controller/NetCommonsAppController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 2d8b1a4e..cd9d4aab 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -276,7 +276,11 @@ public function afterFilter() { $this->response->header('Pragma', 'no-cache'); } else { // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $this->response->header('Cache-Control', 's-maxage=60, public'); + $maxage = Configure::read('App.Cache.sMaxAge'); + if (!isset($maxage)) { + $maxage = 60; + } + $this->response->header('Cache-Control', 's-maxage=' . $maxage . ', public'); } parent::afterFilter(); From 5356f9344b202b6abbaa156325aa2184d22c007c Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 20 Dec 2019 13:46:59 +0900 Subject: [PATCH 07/67] feat(CDN): Add NetCommonsCDNCache - clear cache --- Utility/NetCommonsCDNCache.php | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Utility/NetCommonsCDNCache.php diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php new file mode 100644 index 00000000..c723c5d5 --- /dev/null +++ b/Utility/NetCommonsCDNCache.php @@ -0,0 +1,42 @@ + + * @author Shohei Nakajima + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + * @copyright Copyright 2014, NetCommons Project + */ + +/** + * NetCommons用CDNキャッシュ Utility + * + * @author Shohei Nakajima + * @package NetCommons\NetCommons\Utility + */ +class NetCommonsCDNCache +{ + public function clear() + { + $data = array( + "Site" => array( + "Domain" => Configure::read('App.fullBaseUrl') + ) + ); + + $curl = curl_init(); + $accessToken = Configure::read('CDN.accessToken'); + $accessTokenSecret = Configure::read('CDN.accessTokenSecret'); + + curl_setopt($curl, CURLOPT_URL, Configure::read('CDN.apiUrl') . 'deleteallcache'); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_USERPWD, "$accessToken:$accessTokenSecret"); + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); + + curl_exec($curl); + curl_close($curl); + } +} From 85912a16ac071059184fc2c43bf50ba95e4071cc Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 20 Dec 2019 14:46:03 +0900 Subject: [PATCH 08/67] chore: ignore config files of JetBrains IDE (#563) --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bd743c4d..4a354409 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ app/tmp/* app/[Cc]onfig/core.php app/[Cc]onfig/database.php !empty + +.idea/ From 5d6d9b8d725cd71b53c1985f66f3a5054af87f47 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 20 Dec 2019 14:46:22 +0900 Subject: [PATCH 09/67] style: add .editorconfig to standardize coding style (#562) --- .editorconfig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..0b708ada --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{js,yml}] +indent_size = 2 +indent_style = space + +[*.json] +indent_size = 4 +indent_style = space + +[*.{css,ctp,php}] +indent_style = tab + +[*.md] +max_line_length = off +trim_trailing_whitespace = false From e98ee21c3f500a777dfdba1398592d918c87b78d Mon Sep 17 00:00:00 2001 From: watura Date: Mon, 23 Dec 2019 08:40:15 +0900 Subject: [PATCH 10/67] style: fix 'line exceeds maximum limit' --- Config/bootstrap.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Config/bootstrap.php b/Config/bootstrap.php index cc3babab..f6d6f8ba 100644 --- a/Config/bootstrap.php +++ b/Config/bootstrap.php @@ -27,7 +27,10 @@ // Load application configurations $conf = array(); -$files = array('application.yml', 'application.local.yml', 'application.' . env('HTTP_HOST') . '.yml', 'application.' . env('HTTP_X_FORWARDED_HOST') . '.yml'); +$files = array('application.yml', + 'application.local.yml', + 'application.' . env('HTTP_HOST') . '.yml', + 'application.' . env('HTTP_X_FORWARDED_HOST') . '.yml'); foreach ($files as $file) { if (file_exists(APP . 'Config' . DS . $file)) { $conf = array_replace_recursive($conf, Spyc::YAMLLoad(APP . 'Config' . DS . $file)); From 16f372b9ed2e89ff73d1d2bfe7030663c54f929d Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 20 Dec 2019 13:48:49 +0900 Subject: [PATCH 11/67] feat(CDN): Clear cache when NetCommonsCache clears cache --- Utility/NetCommonsCDNCache.php | 14 +++++++++----- Utility/NetCommonsCache.php | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index c723c5d5..f553843a 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -15,10 +15,14 @@ * @author Shohei Nakajima * @package NetCommons\NetCommons\Utility */ -class NetCommonsCDNCache -{ - public function clear() - { +class NetCommonsCDNCache { + +/** + * Clear CDN Cache + * + * @return void + */ + public function clear() { $data = array( "Site" => array( "Domain" => Configure::read('App.fullBaseUrl') @@ -33,7 +37,7 @@ public function clear() curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_USERPWD, "$accessToken:$accessTokenSecret"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_exec($curl); diff --git a/Utility/NetCommonsCache.php b/Utility/NetCommonsCache.php index a961ec7e..c86eda4b 100644 --- a/Utility/NetCommonsCache.php +++ b/Utility/NetCommonsCache.php @@ -10,6 +10,7 @@ */ App::uses('Cache', 'Cache'); +App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); /** * Configure the cache used for general framework caching. Path information, @@ -201,6 +202,7 @@ public function clear() { //if ($engine && $engine->key($this->__cacheName)) { // $success = Cache::delete($this->__cacheName, $this->__cacheType); Cache::delete($this->__cacheName, $this->__cacheType); + (new NetCommonsCDNCache())->clear(); //} else { // $success = true; //} From d56ccbf99bf8bbae3688ac2f64b7df5f01933159 Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 25 Dec 2019 06:57:30 +0900 Subject: [PATCH 12/67] feat(CDN): invalidate CDN cache when model committed --- Model/NetCommonsAppModel.php | 14 ++++++++++++++ composer.json | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Model/NetCommonsAppModel.php b/Model/NetCommonsAppModel.php index bd8c0906..08df0e83 100644 --- a/Model/NetCommonsAppModel.php +++ b/Model/NetCommonsAppModel.php @@ -11,6 +11,7 @@ */ App::uses('Model', 'Model'); +App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); App::uses('ValidateMerge', 'NetCommons.Utility'); /** @@ -97,6 +98,14 @@ class NetCommonsAppModel extends Model { */ public $contentKey = null; +/** + * invalidateCDN cache + * DB 保存/削除時に CDN のキャッシュを invalidate するか + * + * @var boolean + */ + public $invalidateCDN = true; + /** * Constructor. DataSourceの選択 * @@ -319,6 +328,11 @@ public function begin() { public function commit() { $dataSource = $this->getDataSource(); $dataSource->commit(); + + if ($this->invalidateCDN) { + $cdnCache = new NetCommonsCDNCache(); + $cdnCache->invalidate(); + } } /** diff --git a/composer.json b/composer.json index 080bce68..5dfadb80 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "netcommons/mails": "@dev", "netcommons/topics": "@dev", "netcommons/menus": "@dev", - "netcommons/clean-up": "@dev" + "netcommons/clean-up": "@dev", + "ext-curl": "*" }, "config": { "vendor-dir": "vendors", From ce47d7c878a41d08f722e0de2d85972ca7ccad0d Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 25 Dec 2019 07:44:40 +0900 Subject: [PATCH 13/67] feat(CDN): remove cdn cache invalidate from NetCommonsCache --- Utility/NetCommonsCache.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Utility/NetCommonsCache.php b/Utility/NetCommonsCache.php index c86eda4b..a961ec7e 100644 --- a/Utility/NetCommonsCache.php +++ b/Utility/NetCommonsCache.php @@ -10,7 +10,6 @@ */ App::uses('Cache', 'Cache'); -App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); /** * Configure the cache used for general framework caching. Path information, @@ -202,7 +201,6 @@ public function clear() { //if ($engine && $engine->key($this->__cacheName)) { // $success = Cache::delete($this->__cacheName, $this->__cacheType); Cache::delete($this->__cacheName, $this->__cacheType); - (new NetCommonsCDNCache())->clear(); //} else { // $success = true; //} From d38a637c49f90812d9272daf8f9beae65f7e97ed Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 25 Dec 2019 07:46:35 +0900 Subject: [PATCH 14/67] style(CDN): rename clear to invalidate --- Utility/NetCommonsCDNCache.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index f553843a..fe118723 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -13,16 +13,18 @@ * NetCommons用CDNキャッシュ Utility * * @author Shohei Nakajima + * @author Wataru Nishimoto + * @author Kazunori Sakamoto * @package NetCommons\NetCommons\Utility */ class NetCommonsCDNCache { /** - * Clear CDN Cache + * Invalidate CDN Cache * * @return void */ - public function clear() { + public function invalidate() { $data = array( "Site" => array( "Domain" => Configure::read('App.fullBaseUrl') From 72b5cf43234f3813ae96d0f66e7fe8a7eae63cc9 Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 25 Dec 2019 09:07:59 +0900 Subject: [PATCH 15/67] test: fix phpdoc comment --- Model/NetCommonsAppModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/NetCommonsAppModel.php b/Model/NetCommonsAppModel.php index 08df0e83..b105edff 100644 --- a/Model/NetCommonsAppModel.php +++ b/Model/NetCommonsAppModel.php @@ -102,7 +102,7 @@ class NetCommonsAppModel extends Model { * invalidateCDN cache * DB 保存/削除時に CDN のキャッシュを invalidate するか * - * @var boolean + * @var bool */ public $invalidateCDN = true; From b9799db1d939b55815acf468ff55cf280746f240 Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 10 Jan 2020 16:28:08 +0900 Subject: [PATCH 16/67] feat: use environmnet value instead of applicaiton.yml --- Utility/NetCommonsCDNCache.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index fe118723..0179dd79 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -27,15 +27,19 @@ class NetCommonsCDNCache { public function invalidate() { $data = array( "Site" => array( - "Domain" => Configure::read('App.fullBaseUrl') + "Domain" => Configure::read('App.cacheDomain') ) ); $curl = curl_init(); - $accessToken = Configure::read('CDN.accessToken'); - $accessTokenSecret = Configure::read('CDN.accessTokenSecret'); + $accessToken = env('CDN_ACCESS_TOKEN'); + $accessTokenSecret = env('CDN_ACCESS_TOKEN_SECRET'); + if (!(isset($accessToken) && isset($accessTokenSecret))) { + return; + } - curl_setopt($curl, CURLOPT_URL, Configure::read('CDN.apiUrl') . 'deleteallcache'); + curl_setopt($curl, CURLOPT_URL, + 'https://secure.sakura.ad.jp/cloud/zone/is1a/api/webaccel/1.0/deleteallcache'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_USERPWD, "$accessToken:$accessTokenSecret"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); From af6dfbc29060d4221bcc14c6017f467d062dc5b2 Mon Sep 17 00:00:00 2001 From: watura Date: Tue, 14 Jan 2020 12:48:27 +0900 Subject: [PATCH 17/67] feat: redirect to member url when needed --- Controller/NetCommonsAppController.php | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index eff4c494..c29ace5f 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -186,6 +186,42 @@ private function __prepare() { if (in_array($this->params['action'], ['emptyRender', 'throwBadRequest', 'emptyFrame'])) { $this->params['pass'] = array(); } + + if ($this->__updateFullBaseUrl()) { + $this->redirect($this->request->here); + } + } + +/** + * set and return member or non member url if redirect needed + * + * @return bool + */ + private function __updateFullBaseUrl() { + // 以下の redirect 処理は未ログイン時のみ実施 + if (Current::isLogin()) { + return false; + } + + $memberUrl = Configure::read('App.memberUrl'); + + if (!isset($memberUrl)) { + return false; + } + + $nonMemberUrl = str_replace("member-", "", $memberUrl); + $authControllers = array('auth', 'auth_general'); + $isAuthController = in_array($this->request->controller, $authControllers); + // Auth 関連の URL の場合は memberUrl を fullBaseUrl にセットし、 + // Auth 関連以外の URL の場合は nonMemberUrl を fullBaseUrl にセットする + if ($isAuthController && Router::fullBaseUrl() != $memberUrl) { + Router::fullBaseUrl($memberUrl); + return true; + } elseif (!$isAuthController && Router::fullBaseUrl() == $memberUrl) { + Router::fullBaseUrl($nonMemberUrl); + return true; + } + return false; } /** From 2d61b08107dfd60306eb8d56ca9f12365719ee97 Mon Sep 17 00:00:00 2001 From: watura Date: Wed, 15 Jan 2020 09:56:04 +0900 Subject: [PATCH 18/67] style: SuppressWarnings PHPMD.CouplingBetweenObject --- Controller/NetCommonsAppController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index c29ace5f..6144d7ca 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -23,6 +23,7 @@ * @author Shohei Nakajima * @package NetCommons\NetCommons\Controller * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class NetCommonsAppController extends Controller { From a1a022d23a056168ca34edb50be2636c2def16bd Mon Sep 17 00:00:00 2001 From: watura Date: Mon, 3 Feb 2020 14:56:06 +0900 Subject: [PATCH 19/67] feat: set s-max-age to 86400 --- Controller/NetCommonsAppController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 6144d7ca..8c3a44fd 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -325,11 +325,7 @@ public function afterFilter() { $this->response->header('Pragma', 'no-cache'); } else { // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $maxage = Configure::read('App.Cache.sMaxAge'); - if (!isset($maxage)) { - $maxage = 60; - } - $this->response->header('Cache-Control', 's-maxage=' . $maxage . ', public'); + $this->response->header('Cache-Control', 's-maxage=86400, public'); } parent::afterFilter(); From 50f3c6059d24d6f73aa5e72a9afb930c10db7f7b Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 6 Feb 2020 16:16:42 +0900 Subject: [PATCH 20/67] feat: use application.yml instead of env --- Utility/NetCommonsCDNCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index 0179dd79..3b965872 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -32,8 +32,8 @@ public function invalidate() { ); $curl = curl_init(); - $accessToken = env('CDN_ACCESS_TOKEN'); - $accessTokenSecret = env('CDN_ACCESS_TOKEN_SECRET'); + $accessToken = Configure::read('Cdn.AccessToken'); + $accessTokenSecret = Configure::read('Cdn.Secret'); if (!(isset($accessToken) && isset($accessTokenSecret))) { return; } From 5599fbabb13c2340e09e69f6fc9dc89f717302de Mon Sep 17 00:00:00 2001 From: watura Date: Mon, 3 Feb 2020 14:56:06 +0900 Subject: [PATCH 21/67] feat: set s-max-age to 86400 --- Controller/NetCommonsAppController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 6144d7ca..8c3a44fd 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -325,11 +325,7 @@ public function afterFilter() { $this->response->header('Pragma', 'no-cache'); } else { // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $maxage = Configure::read('App.Cache.sMaxAge'); - if (!isset($maxage)) { - $maxage = 60; - } - $this->response->header('Cache-Control', 's-maxage=' . $maxage . ', public'); + $this->response->header('Cache-Control', 's-maxage=86400, public'); } parent::afterFilter(); From d74b0b119873ad36b11a634742bef986f72c77a8 Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 6 Feb 2020 16:16:42 +0900 Subject: [PATCH 22/67] feat: use application.yml instead of env --- Utility/NetCommonsCDNCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index 0179dd79..3b965872 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -32,8 +32,8 @@ public function invalidate() { ); $curl = curl_init(); - $accessToken = env('CDN_ACCESS_TOKEN'); - $accessTokenSecret = env('CDN_ACCESS_TOKEN_SECRET'); + $accessToken = Configure::read('Cdn.AccessToken'); + $accessTokenSecret = Configure::read('Cdn.Secret'); if (!(isset($accessToken) && isset($accessTokenSecret))) { return; } From a542d82663693a59f0bcc87dc1a3f91accfc468b Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 14 Feb 2020 23:57:51 +0900 Subject: [PATCH 23/67] fix: use stricter comparisons --- Controller/NetCommonsAppController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8c3a44fd..aaa3a71f 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -212,13 +212,13 @@ private function __updateFullBaseUrl() { $nonMemberUrl = str_replace("member-", "", $memberUrl); $authControllers = array('auth', 'auth_general'); - $isAuthController = in_array($this->request->controller, $authControllers); + $isAuthController = in_array($this->request->controller, $authControllers, true); // Auth 関連の URL の場合は memberUrl を fullBaseUrl にセットし、 // Auth 関連以外の URL の場合は nonMemberUrl を fullBaseUrl にセットする - if ($isAuthController && Router::fullBaseUrl() != $memberUrl) { + if ($isAuthController && Router::fullBaseUrl() !== $memberUrl) { Router::fullBaseUrl($memberUrl); return true; - } elseif (!$isAuthController && Router::fullBaseUrl() == $memberUrl) { + } elseif (!$isAuthController && Router::fullBaseUrl() === $memberUrl) { Router::fullBaseUrl($nonMemberUrl); return true; } From b3ff38839ba4b72663329a38c7d91512874a92ac Mon Sep 17 00:00:00 2001 From: watura Date: Tue, 18 Feb 2020 06:54:05 +0900 Subject: [PATCH 24/67] revert: set s-maxage --- Controller/NetCommonsAppController.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8f5fce4a..8c982711 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -324,9 +324,6 @@ public function afterFilter() { if (Current::isLogin()) { // ログインしている場合はキャッシュしない $this->response->header('Pragma', 'no-cache'); - } else { - // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $this->response->header('Cache-Control', 's-maxage=86400, public'); } parent::afterFilter(); From f9544de139b074d1c0cec224c4a0ccc6f93b30d9 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 20 Feb 2020 12:27:58 +0900 Subject: [PATCH 25/67] feat: don't invalidate CDN cache frequently --- Model/NetCommonsAppModel.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Model/NetCommonsAppModel.php b/Model/NetCommonsAppModel.php index b105edff..d70d9d8a 100644 --- a/Model/NetCommonsAppModel.php +++ b/Model/NetCommonsAppModel.php @@ -11,6 +11,7 @@ */ App::uses('Model', 'Model'); +App::uses('NetCommonsCache', 'NetCommons.Utility'); App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); App::uses('ValidateMerge', 'NetCommons.Utility'); @@ -36,6 +37,13 @@ */ class NetCommonsAppModel extends Model { +/** + * 高頻度なキャッシュ無効化を防ぐために、無効化のリクエストを無視する期間(秒) + * + * @var float + */ + const NO_CACHE_INVALIDATION_DURATION_SEC = 1.0; + /** * use behaviors * @@ -330,8 +338,14 @@ public function commit() { $dataSource->commit(); if ($this->invalidateCDN) { - $cdnCache = new NetCommonsCDNCache(); - $cdnCache->invalidate(); + $ncCache = new NetCommonsCache('cache_invalidated_at', false, 'netcommons_core'); + $lastTime = floatval($ncCache->read()); + $now = microtime(true); + if ($now - $lastTime > self::NO_CACHE_INVALIDATION_DURATION_SEC) { + $ncCache->write($now); + $cdnCache = new NetCommonsCDNCache(); + $cdnCache->invalidate(); + } } } From 63516033ceea5f2e2c310cdc9afea76da414699b Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 20 Feb 2020 14:10:33 +0900 Subject: [PATCH 26/67] fix: disable cache in GET requests via Ajax (#582) --- webroot/js/base.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index 017155f3..45c9062f 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -8,10 +8,15 @@ var NetCommonsApp = angular.module('NetCommonsApp', ['ngAnimate', 'ui.bootstrap' //CakePHPがX-Requested-Withで判断しているため NetCommonsApp.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; - $httpProvider.defaults.headers.common['If-Modified-Since'] = - new Date() . toUTCString(); -}]); + // Disable cache in GET requests via Ajax + if (!$httpProvider.defaults.headers.get) { + $httpProvider.defaults.headers.get = {}; + } + $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; + $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; + $httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; +}]); /** * ncHtmlContent filter From d0a650094176bd883feb569385178d7d259dd730 Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 20 Feb 2020 15:08:05 +0900 Subject: [PATCH 27/67] feat: add endpoint for invalidate cdn cache --- Config/routes.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Config/routes.php b/Config/routes.php index 24590e11..4fb7f647 100644 --- a/Config/routes.php +++ b/Config/routes.php @@ -11,6 +11,12 @@ App::uses('SlugRoute', 'Pages.Routing/Route'); App::uses('Current', 'NetCommons.Utility'); +Router::connect( + '/invalidate', + array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'invalidate'), + array('routeClass' => 'SlugRoute') +); + Router::connect( '/' . Current::SETTING_MODE_WORD . '/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'index'), From 3d94cae5ea4658732f502ae3072e44b16b10ca39 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 5 Feb 2020 18:38:04 +0900 Subject: [PATCH 28/67] =?UTF-8?q?style:=20phpcs=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20https://github.com/researchmap/RmNetCommon?= =?UTF-8?q?s3/issues/1640?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/Component/NetCommonsComponent.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Controller/Component/NetCommonsComponent.php b/Controller/Component/NetCommonsComponent.php index cc238593..009c32f4 100644 --- a/Controller/Component/NetCommonsComponent.php +++ b/Controller/Component/NetCommonsComponent.php @@ -57,7 +57,10 @@ public function renderJson($results = [], $name = 'OK', $status = 200) { $results = NetCommonsAppController::camelizeKeyRecursive($results); $this->controller->set(compact('results')); $this->controller->set('_serialize', 'results'); - $this->controller->set('_jsonOptions', JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); + $this->controller->set( + '_jsonOptions', + JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT + ); } /** From 62b6c2b0d0def721417efbabb64a0a80ba984de3 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 5 Feb 2020 18:39:39 +0900 Subject: [PATCH 29/67] =?UTF-8?q?fix:=20AuthComponent=E3=81=8C=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=95=E3=82=8C=E3=81=A6=E3=81=84=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=A8=E3=80=81Fatal=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=97=E3=80=81=E6=AD=A3=E3=81=97?= =?UTF-8?q?=E3=81=84=E3=82=A8=E3=83=A9=E3=83=BC=E3=82=92=E8=BF=94=E3=81=95?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3=20https:?= =?UTF-8?q?//github.com/researchmap/RmNetCommons3/issues/1640?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Error/NetCommonsExceptionRenderer.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Error/NetCommonsExceptionRenderer.php b/Error/NetCommonsExceptionRenderer.php index 16c601eb..4cdd6b57 100644 --- a/Error/NetCommonsExceptionRenderer.php +++ b/Error/NetCommonsExceptionRenderer.php @@ -151,7 +151,8 @@ public function error400($error) { } elseif ($this->_is403And404($error)) { list($redirect, $redirectUrl) = $this->_get403And404Redirect(); - if (! $this->controller->request->is('ajax')) { + if (! $this->controller->request->is('ajax') && + $this->__loadedAuthComponent()) { $this->controller->Auth->redirectUrl($redirectUrl); } @@ -187,7 +188,7 @@ protected function _get400Message($error) { 'net_commons', 'Under maintenance. Nobody is allowed to login except for administrators.' ); } elseif ($message === 'Forbidden') { - if ($this->controller->Auth->user()) { + if ($this->__isLoggedIn()) { $message = __d('net_commons', 'Permission denied. Bad account.'); } else { $message = __d('net_commons', 'Permission denied. You must be logged.'); @@ -217,7 +218,7 @@ protected function _is403And404($error) { * @return array array($redirect, $redirectUrl) */ protected function _get403And404Redirect() { - if ($this->controller->Auth->user()) { + if ($this->__isLoggedIn()) { $referer = Router::parse($this->controller->request->referer(true)); $here = Router::parse($this->controller->request->here(false)); @@ -251,6 +252,24 @@ protected function _get403And404Redirect() { return array($redirect, $redirectUrl); } +/** + * Authコンポーネントがロードされているか否か + * + * @return bool + */ + private function __loadedAuthComponent() { + return ! empty($this->controller->Auth); + } + +/** + * ログインしているかいない + * + * @return bool + */ + private function __isLoggedIn() { + return $this->__loadedAuthComponent() && $this->controller->Auth->user(); + } + /** * Convenience method to display a 500 page. * From 0645051c5206c526fe6a7ed99a0927132cc04304 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 5 Feb 2020 19:12:34 +0900 Subject: [PATCH 30/67] =?UTF-8?q?style:=20phpmd=E5=9B=9E=E9=81=BF=20https:?= =?UTF-8?q?//github.com/researchmap/RmNetCommons3/issues/1640?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 1 + Error/NetCommonsExceptionRenderer.php | 1 + 2 files changed, 2 insertions(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8c3a44fd..9ab1d54a 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -294,6 +294,7 @@ public function beforeFilter() { * @param bool $local If true, restrict referring URLs to local server * @return string Referring URL * @link https://book.cakephp.org/2.0/en/controllers.html#Controller::referer + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ public function referer($default = null, $local = true) { return parent::referer($default, $local); diff --git a/Error/NetCommonsExceptionRenderer.php b/Error/NetCommonsExceptionRenderer.php index 4cdd6b57..a357c78e 100644 --- a/Error/NetCommonsExceptionRenderer.php +++ b/Error/NetCommonsExceptionRenderer.php @@ -126,6 +126,7 @@ protected function _getController($exception) { * * @param Exception $error Exception * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function error400($error) { $message = $error->getMessage(); From 5aed50e2ba2dd7c0a84b3bfa4b544cb7d156b07e Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 14 Feb 2020 23:57:51 +0900 Subject: [PATCH 31/67] fix: use stricter comparisons --- Controller/NetCommonsAppController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 9ab1d54a..8f5fce4a 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -212,13 +212,13 @@ private function __updateFullBaseUrl() { $nonMemberUrl = str_replace("member-", "", $memberUrl); $authControllers = array('auth', 'auth_general'); - $isAuthController = in_array($this->request->controller, $authControllers); + $isAuthController = in_array($this->request->controller, $authControllers, true); // Auth 関連の URL の場合は memberUrl を fullBaseUrl にセットし、 // Auth 関連以外の URL の場合は nonMemberUrl を fullBaseUrl にセットする - if ($isAuthController && Router::fullBaseUrl() != $memberUrl) { + if ($isAuthController && Router::fullBaseUrl() !== $memberUrl) { Router::fullBaseUrl($memberUrl); return true; - } elseif (!$isAuthController && Router::fullBaseUrl() == $memberUrl) { + } elseif (!$isAuthController && Router::fullBaseUrl() === $memberUrl) { Router::fullBaseUrl($nonMemberUrl); return true; } From 4687c5e3ba96a19e4516b8f6d1176366f03cd3fa Mon Sep 17 00:00:00 2001 From: watura Date: Tue, 18 Feb 2020 06:54:05 +0900 Subject: [PATCH 32/67] revert: set s-maxage --- Controller/NetCommonsAppController.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8f5fce4a..8c982711 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -324,9 +324,6 @@ public function afterFilter() { if (Current::isLogin()) { // ログインしている場合はキャッシュしない $this->response->header('Pragma', 'no-cache'); - } else { - // CDN(Proxy)で使われるキャッシュヘッダーをセットする - $this->response->header('Cache-Control', 's-maxage=86400, public'); } parent::afterFilter(); From 7ae0658621aeef532f8bc3227f1b3fe055ab89ed Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 20 Feb 2020 12:27:58 +0900 Subject: [PATCH 33/67] feat: don't invalidate CDN cache frequently --- Model/NetCommonsAppModel.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Model/NetCommonsAppModel.php b/Model/NetCommonsAppModel.php index b105edff..d70d9d8a 100644 --- a/Model/NetCommonsAppModel.php +++ b/Model/NetCommonsAppModel.php @@ -11,6 +11,7 @@ */ App::uses('Model', 'Model'); +App::uses('NetCommonsCache', 'NetCommons.Utility'); App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); App::uses('ValidateMerge', 'NetCommons.Utility'); @@ -36,6 +37,13 @@ */ class NetCommonsAppModel extends Model { +/** + * 高頻度なキャッシュ無効化を防ぐために、無効化のリクエストを無視する期間(秒) + * + * @var float + */ + const NO_CACHE_INVALIDATION_DURATION_SEC = 1.0; + /** * use behaviors * @@ -330,8 +338,14 @@ public function commit() { $dataSource->commit(); if ($this->invalidateCDN) { - $cdnCache = new NetCommonsCDNCache(); - $cdnCache->invalidate(); + $ncCache = new NetCommonsCache('cache_invalidated_at', false, 'netcommons_core'); + $lastTime = floatval($ncCache->read()); + $now = microtime(true); + if ($now - $lastTime > self::NO_CACHE_INVALIDATION_DURATION_SEC) { + $ncCache->write($now); + $cdnCache = new NetCommonsCDNCache(); + $cdnCache->invalidate(); + } } } From b6828f6f9832adadf2870250d6c6aa37f681a5a9 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 20 Feb 2020 14:10:33 +0900 Subject: [PATCH 34/67] fix: disable cache in GET requests via Ajax (#582) --- webroot/js/base.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index 017155f3..45c9062f 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -8,10 +8,15 @@ var NetCommonsApp = angular.module('NetCommonsApp', ['ngAnimate', 'ui.bootstrap' //CakePHPがX-Requested-Withで判断しているため NetCommonsApp.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; - $httpProvider.defaults.headers.common['If-Modified-Since'] = - new Date() . toUTCString(); -}]); + // Disable cache in GET requests via Ajax + if (!$httpProvider.defaults.headers.get) { + $httpProvider.defaults.headers.get = {}; + } + $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; + $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; + $httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; +}]); /** * ncHtmlContent filter From 657b32ed7de609beef80da082d6f242017a27fcb Mon Sep 17 00:00:00 2001 From: watura Date: Thu, 20 Feb 2020 15:08:05 +0900 Subject: [PATCH 35/67] feat: add endpoint for invalidate cdn cache --- Config/routes.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Config/routes.php b/Config/routes.php index 24590e11..4fb7f647 100644 --- a/Config/routes.php +++ b/Config/routes.php @@ -11,6 +11,12 @@ App::uses('SlugRoute', 'Pages.Routing/Route'); App::uses('Current', 'NetCommons.Utility'); +Router::connect( + '/invalidate', + array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'invalidate'), + array('routeClass' => 'SlugRoute') +); + Router::connect( '/' . Current::SETTING_MODE_WORD . '/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'index'), From e9dddcab91513d961d2df2cc3c893287498102cc Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 21 Feb 2020 09:35:56 +0900 Subject: [PATCH 36/67] fix: redirect when using auth, auth_general plugin --- Controller/NetCommonsAppController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8c982711..a0e1cc6a 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -211,14 +211,14 @@ private function __updateFullBaseUrl() { } $nonMemberUrl = str_replace("member-", "", $memberUrl); - $authControllers = array('auth', 'auth_general'); - $isAuthController = in_array($this->request->controller, $authControllers, true); - // Auth 関連の URL の場合は memberUrl を fullBaseUrl にセットし、 - // Auth 関連以外の URL の場合は nonMemberUrl を fullBaseUrl にセットする - if ($isAuthController && Router::fullBaseUrl() !== $memberUrl) { + $authPlugins = array('auth', 'auth_general'); + $isAuthPlugin = in_array($this->request->plugin, $authPlugins, true); + // Auth 関連の Plugin の場合は memberUrl を fullBaseUrl にセットし、 + // Auth 関連以外の Plugin の場合は nonMemberUrl を fullBaseUrl にセットする + if ($isAuthPlugin && Router::fullBaseUrl() !== $memberUrl) { Router::fullBaseUrl($memberUrl); return true; - } elseif (!$isAuthController && Router::fullBaseUrl() === $memberUrl) { + } elseif (!$isAuthPlugin && Router::fullBaseUrl() === $memberUrl) { Router::fullBaseUrl($nonMemberUrl); return true; } From 396cb32169656bcb9088da645c435e760289bd7c Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 21 Feb 2020 14:00:14 +0900 Subject: [PATCH 37/67] fix: disable CDN cache for Ajax requests --- Controller/NetCommonsAppController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index a0e1cc6a..02c3861d 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -321,7 +321,7 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin()) { + if (Current::isLogin() || $this->request->is('ajax')) { // ログインしている場合はキャッシュしない $this->response->header('Pragma', 'no-cache'); } From 3a0d010edcfa1605725ed60a493e47e3e7d4a311 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 21 Feb 2020 18:32:13 +0900 Subject: [PATCH 38/67] feat: add updateTokens function --- webroot/js/base.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index 45c9062f..5126e408 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -1,6 +1,7 @@ /** * @fileoverview NetCommonsApp Javascript * @author nakajimashouhei@gmail.com (Shohei Nakajima) + * @author exkazuu@willbooster.com (Kazunori Sakamoto) */ var NetCommonsApp = angular.module('NetCommonsApp', ['ngAnimate', 'ui.bootstrap']); @@ -167,7 +168,7 @@ NetCommonsApp.factory('ajaxSendPost', ['$http', '$q', 'NC3_URL', function($http, * base controller */ NetCommonsApp.controller('NetCommons.base', - ['$scope', '$location', '$window', 'NC3_URL', function($scope, $location, $window, NC3_URL) { + ['$scope', '$location', '$window', '$http', 'NC3_URL', function($scope, $location, $window, $http, NC3_URL) { /** * Base URL @@ -274,4 +275,23 @@ NetCommonsApp.controller('NetCommons.base', } }).trigger('hashchange'); }; + + /** + * updateTokens + * + * @return {void} + */ + $scope.updateTokens = function() { + var $token = $('input[name="data[_Token][key]"]'); + var $submit = $token.closest('form').find(':submit'); + $submit.attr('disabled', 'disabled'); + $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') + .then(function(response) { + var token = response.data; + $token.val(token.data._Token.key); + $submit.removeAttr('disabled'); + }, + function() { + }); + }; }]); From c1fbb4c4e1981ae6eb7f87a741a5efcdf4831db2 Mon Sep 17 00:00:00 2001 From: watura Date: Fri, 21 Feb 2020 17:30:30 +0900 Subject: [PATCH 39/67] fix: redirect when using net_commons plugin --- Controller/NetCommonsAppController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 02c3861d..7b938026 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -206,7 +206,7 @@ private function __updateFullBaseUrl() { $memberUrl = Configure::read('App.memberUrl'); - if (!isset($memberUrl)) { + if (!isset($memberUrl) || $this->request->plugin == 'net_commons') { return false; } From c332fcd88aab14895765046ab05ac5f9b92d98fb Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 21 Feb 2020 20:57:04 +0900 Subject: [PATCH 40/67] feat: import changes from NetCommons3 (#4) --- Controller/NetCommonsAppController.php | 16 ++++++++-------- webroot/js/base.js | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 8c982711..7b938026 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -206,19 +206,19 @@ private function __updateFullBaseUrl() { $memberUrl = Configure::read('App.memberUrl'); - if (!isset($memberUrl)) { + if (!isset($memberUrl) || $this->request->plugin == 'net_commons') { return false; } $nonMemberUrl = str_replace("member-", "", $memberUrl); - $authControllers = array('auth', 'auth_general'); - $isAuthController = in_array($this->request->controller, $authControllers, true); - // Auth 関連の URL の場合は memberUrl を fullBaseUrl にセットし、 - // Auth 関連以外の URL の場合は nonMemberUrl を fullBaseUrl にセットする - if ($isAuthController && Router::fullBaseUrl() !== $memberUrl) { + $authPlugins = array('auth', 'auth_general'); + $isAuthPlugin = in_array($this->request->plugin, $authPlugins, true); + // Auth 関連の Plugin の場合は memberUrl を fullBaseUrl にセットし、 + // Auth 関連以外の Plugin の場合は nonMemberUrl を fullBaseUrl にセットする + if ($isAuthPlugin && Router::fullBaseUrl() !== $memberUrl) { Router::fullBaseUrl($memberUrl); return true; - } elseif (!$isAuthController && Router::fullBaseUrl() === $memberUrl) { + } elseif (!$isAuthPlugin && Router::fullBaseUrl() === $memberUrl) { Router::fullBaseUrl($nonMemberUrl); return true; } @@ -321,7 +321,7 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin()) { + if (Current::isLogin() || $this->request->is('ajax')) { // ログインしている場合はキャッシュしない $this->response->header('Pragma', 'no-cache'); } diff --git a/webroot/js/base.js b/webroot/js/base.js index 45c9062f..5126e408 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -1,6 +1,7 @@ /** * @fileoverview NetCommonsApp Javascript * @author nakajimashouhei@gmail.com (Shohei Nakajima) + * @author exkazuu@willbooster.com (Kazunori Sakamoto) */ var NetCommonsApp = angular.module('NetCommonsApp', ['ngAnimate', 'ui.bootstrap']); @@ -167,7 +168,7 @@ NetCommonsApp.factory('ajaxSendPost', ['$http', '$q', 'NC3_URL', function($http, * base controller */ NetCommonsApp.controller('NetCommons.base', - ['$scope', '$location', '$window', 'NC3_URL', function($scope, $location, $window, NC3_URL) { + ['$scope', '$location', '$window', '$http', 'NC3_URL', function($scope, $location, $window, $http, NC3_URL) { /** * Base URL @@ -274,4 +275,23 @@ NetCommonsApp.controller('NetCommons.base', } }).trigger('hashchange'); }; + + /** + * updateTokens + * + * @return {void} + */ + $scope.updateTokens = function() { + var $token = $('input[name="data[_Token][key]"]'); + var $submit = $token.closest('form').find(':submit'); + $submit.attr('disabled', 'disabled'); + $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') + .then(function(response) { + var token = response.data; + $token.val(token.data._Token.key); + $submit.removeAttr('disabled'); + }, + function() { + }); + }; }]); From c4b9ffd8abbc8f09d2e08094b7bd5abdb2dc1082 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Tue, 25 Feb 2020 20:17:29 +0900 Subject: [PATCH 41/67] fix: don't use cahce if no-cache get param is given --- Controller/NetCommonsAppController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 7b938026..88cbcf13 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -321,8 +321,8 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin() || $this->request->is('ajax')) { - // ログインしている場合はキャッシュしない + if (Current::isLogin() || $this->request->is('ajax') || + $this->request->query('no-cache')) { $this->response->header('Pragma', 'no-cache'); } From c69ac5fe2dec2d1e60bcca5fb5a45c0342e80b94 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Tue, 25 Feb 2020 21:10:59 +0900 Subject: [PATCH 42/67] refactor: no-cache and redirection code --- Controller/NetCommonsAppController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 88cbcf13..2f332c14 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -199,14 +199,18 @@ private function __prepare() { * @return bool */ private function __updateFullBaseUrl() { - // 以下の redirect 処理は未ログイン時のみ実施 + // ログイン済みの場合は、memberにリダイレクトしない if (Current::isLogin()) { return false; } - $memberUrl = Configure::read('App.memberUrl'); + // NetCommonsプラグインの処理では、memberにリダイレクトしない + if ($this->request->plugin == 'net_commons') { + return false; + } - if (!isset($memberUrl) || $this->request->plugin == 'net_commons') { + $memberUrl = Configure::read('App.memberUrl'); + if (!isset($memberUrl)) { return false; } @@ -233,6 +237,10 @@ private function __updateFullBaseUrl() { public function beforeFilter() { parent::beforeFilter(); + if ($this->request->is('ajax') || $this->request->query('no-cache')) { + $this->response->header('Pragma', 'no-cache'); + } + if (empty($this->request->params['requested'])) { $this->request->allowMethod($this->_allowMethods); } @@ -321,8 +329,7 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin() || $this->request->is('ajax') || - $this->request->query('no-cache')) { + if (Current::isLogin()) { $this->response->header('Pragma', 'no-cache'); } From 5fc5c492a92ee5c3920bd6b488ada4541566f777 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 26 Feb 2020 13:05:51 +0900 Subject: [PATCH 43/67] Import changes from NetCommons3/NetCommons (#5) --- Controller/NetCommonsAppController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 7b938026..2f332c14 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -199,14 +199,18 @@ private function __prepare() { * @return bool */ private function __updateFullBaseUrl() { - // 以下の redirect 処理は未ログイン時のみ実施 + // ログイン済みの場合は、memberにリダイレクトしない if (Current::isLogin()) { return false; } - $memberUrl = Configure::read('App.memberUrl'); + // NetCommonsプラグインの処理では、memberにリダイレクトしない + if ($this->request->plugin == 'net_commons') { + return false; + } - if (!isset($memberUrl) || $this->request->plugin == 'net_commons') { + $memberUrl = Configure::read('App.memberUrl'); + if (!isset($memberUrl)) { return false; } @@ -233,6 +237,10 @@ private function __updateFullBaseUrl() { public function beforeFilter() { parent::beforeFilter(); + if ($this->request->is('ajax') || $this->request->query('no-cache')) { + $this->response->header('Pragma', 'no-cache'); + } + if (empty($this->request->params['requested'])) { $this->request->allowMethod($this->_allowMethods); } @@ -321,8 +329,7 @@ public function afterFilter() { $this->CurrentLib->terminate($this); } - if (Current::isLogin() || $this->request->is('ajax')) { - // ログインしている場合はキャッシュしない + if (Current::isLogin()) { $this->response->header('Pragma', 'no-cache'); } From 0d7cc1629bd0dd42489acc8d0c61eb51beda09bb Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 26 Feb 2020 20:47:52 +0900 Subject: [PATCH 44/67] fix: don't create cache when using flash message --- Controller/Component/NetCommonsComponent.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Controller/Component/NetCommonsComponent.php b/Controller/Component/NetCommonsComponent.php index 009c32f4..b82e1e82 100644 --- a/Controller/Component/NetCommonsComponent.php +++ b/Controller/Component/NetCommonsComponent.php @@ -106,6 +106,8 @@ public function handleValidationError($errors, $message = null) { * @return void */ public function setFlashNotification($message, $params = array(), $status = 200) { + $this->controller->response->header('Pragma', 'no-cache'); + if (is_string($params)) { $params = array('class' => $params); } From 5dc8f090ef59f64f9d61e45b8492b731e5827e15 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 27 Feb 2020 15:08:21 +0900 Subject: [PATCH 45/67] feat: add CDNCacheHelper --- Controller/NetCommonsAppController.php | 5 ++++- View/Helper/CDNCacheHelper.php | 28 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 View/Helper/CDNCacheHelper.php diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 2f332c14..3de4e7ed 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -3,6 +3,7 @@ * NetCommonsApp Controller * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project @@ -21,6 +22,7 @@ * NetCommonsApp Controller * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @package NetCommons\NetCommons\Controller * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -104,8 +106,9 @@ class NetCommonsAppController extends Controller { 'M17n.M17n', 'NetCommons.BackTo', 'NetCommons.Button', - 'NetCommons.LinkButton', + 'NetCommons.CDNCache', 'NetCommons.Date', + 'NetCommons.LinkButton', 'NetCommons.MessageFlash', 'NetCommons.NetCommonsForm', 'NetCommons.NetCommonsHtml', diff --git a/View/Helper/CDNCacheHelper.php b/View/Helper/CDNCacheHelper.php new file mode 100644 index 00000000..9f5657f0 --- /dev/null +++ b/View/Helper/CDNCacheHelper.php @@ -0,0 +1,28 @@ + + * @author Kazunori Sakamoto + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + * @copyright Copyright 2020, NetCommons Project + */ + +/** + * CDNCache Helper + * + */ +class CDNCacheHelper extends AppHelper { + +/** + * Return a boolean value whether the page is cacheable or not. + * + * @return bool a boolean value whether the page is cacheable or not. + */ + public function isCacheable() { + $nonCacheable = $this->_View->response->header()['Pragma'] === 'no-cache' || + strncmp('origin-', $_SERVER['SERVER_NAME'], 7) !== 0; + return ! $nonCacheable; + } +} From 96e25b17e17c76c024ed8b5ce1f6e9584cead788 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 27 Feb 2020 15:20:44 +0900 Subject: [PATCH 46/67] refactor: cdn-cache-related code (#586) --- Model/NetCommonsAppModel.php | 22 +++++------------ Utility/NetCommonsCDNCache.php | 45 ++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/Model/NetCommonsAppModel.php b/Model/NetCommonsAppModel.php index d70d9d8a..67165aee 100644 --- a/Model/NetCommonsAppModel.php +++ b/Model/NetCommonsAppModel.php @@ -5,13 +5,14 @@ * @author Shohei Nakajima * @author Jun Nishikawa * @author Takako Miyagawa + * @author Wataru Nishimoto + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project */ App::uses('Model', 'Model'); -App::uses('NetCommonsCache', 'NetCommons.Utility'); App::uses('NetCommonsCDNCache', 'NetCommons.Utility'); App::uses('ValidateMerge', 'NetCommons.Utility'); @@ -31,19 +32,14 @@ * @author Shohei Nakajima * @author Jun Nishikawa * @author Takako Miyagawa + * @author Wataru Nishimoto + * @author Kazunori Sakamoto * @package NetCommons\NetCommons\Model * @SuppressWarnings(PHPMD.NumberOfChildren) * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class NetCommonsAppModel extends Model { -/** - * 高頻度なキャッシュ無効化を防ぐために、無効化のリクエストを無視する期間(秒) - * - * @var float - */ - const NO_CACHE_INVALIDATION_DURATION_SEC = 1.0; - /** * use behaviors * @@ -338,14 +334,8 @@ public function commit() { $dataSource->commit(); if ($this->invalidateCDN) { - $ncCache = new NetCommonsCache('cache_invalidated_at', false, 'netcommons_core'); - $lastTime = floatval($ncCache->read()); - $now = microtime(true); - if ($now - $lastTime > self::NO_CACHE_INVALIDATION_DURATION_SEC) { - $ncCache->write($now); - $cdnCache = new NetCommonsCDNCache(); - $cdnCache->invalidate(); - } + $cdnCache = new NetCommonsCDNCache(); + $cdnCache->invalidate(); } } diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index 3b965872..06a38791 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -4,11 +4,15 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Wataru Nishimoto + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project */ +App::uses('NetCommonsCache', 'NetCommons.Utility'); + /** * NetCommons用CDNキャッシュ Utility * @@ -20,30 +24,57 @@ class NetCommonsCDNCache { /** - * Invalidate CDN Cache + * 高頻度なキャッシュ無効化を防ぐために、無効化のリクエストを無視する期間(秒) + * + * @var float + */ + const NO_CACHE_INVALIDATION_DURATION_SEC = 1.0; + +/** + * Invalidate CDN Cache checking the invalidation frequency * * @return void */ public function invalidate() { - $data = array( - "Site" => array( - "Domain" => Configure::read('App.cacheDomain') - ) - ); + $ncCache = new NetCommonsCache('cdn_cache_invalidated_at', false, 'netcommons_core'); + $lastTime = floatval($ncCache->read()); + $now = microtime(true); + if ($now - $lastTime > self::NO_CACHE_INVALIDATION_DURATION_SEC) { + $ncCache->write($now); + $this->postInvalidationRequest(); + } + } + + /** + * Invalidate CDN Cache + * + * @return void + */ + private function postInvalidationRequest() { + $cacheDomain = Configure::read('App.cacheDomain'); + if (!isset($cacheDomain)) { + return; + } - $curl = curl_init(); $accessToken = Configure::read('Cdn.AccessToken'); $accessTokenSecret = Configure::read('Cdn.Secret'); if (!(isset($accessToken) && isset($accessTokenSecret))) { return; } + $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://secure.sakura.ad.jp/cloud/zone/is1a/api/webaccel/1.0/deleteallcache'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_USERPWD, "$accessToken:$accessTokenSecret"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + $data = array( + "Site" => array( + "Domain" => $cacheDomain + ) + ); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_exec($curl); From dbc4895516f99846e6e807df18c5297eff6b3d35 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Sun, 1 Mar 2020 11:40:48 +0900 Subject: [PATCH 47/67] fix: Undefined index: Pragma --- View/Helper/CDNCacheHelper.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/View/Helper/CDNCacheHelper.php b/View/Helper/CDNCacheHelper.php index 9f5657f0..836efdcc 100644 --- a/View/Helper/CDNCacheHelper.php +++ b/View/Helper/CDNCacheHelper.php @@ -21,8 +21,9 @@ class CDNCacheHelper extends AppHelper { * @return bool a boolean value whether the page is cacheable or not. */ public function isCacheable() { - $nonCacheable = $this->_View->response->header()['Pragma'] === 'no-cache' || - strncmp('origin-', $_SERVER['SERVER_NAME'], 7) !== 0; - return ! $nonCacheable; + $nonCacheable = (isset($this->_View->response->header()['Pragma']) && + $this->_View->response->header()['Pragma'] === 'no-cache') || + strncmp('origin-', $_SERVER['SERVER_NAME'], 7) !== 0; + return !$nonCacheable; } } From 4fa1720d0176af559dec3a62b66d1d44878c2aab Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Tue, 3 Mar 2020 09:46:38 +0900 Subject: [PATCH 48/67] feat: serve static files from CDN (#587) --- View/Helper/CDNCacheHelper.php | 2 +- View/Helper/NetCommonsHtmlHelper.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/View/Helper/CDNCacheHelper.php b/View/Helper/CDNCacheHelper.php index 836efdcc..afa24237 100644 --- a/View/Helper/CDNCacheHelper.php +++ b/View/Helper/CDNCacheHelper.php @@ -23,7 +23,7 @@ class CDNCacheHelper extends AppHelper { public function isCacheable() { $nonCacheable = (isset($this->_View->response->header()['Pragma']) && $this->_View->response->header()['Pragma'] === 'no-cache') || - strncmp('origin-', $_SERVER['SERVER_NAME'], 7) !== 0; + strncmp('origin-', $_SERVER['HTTP_HOST'], 7) !== 0; return !$nonCacheable; } } diff --git a/View/Helper/NetCommonsHtmlHelper.php b/View/Helper/NetCommonsHtmlHelper.php index 6a6400a9..e267c1a8 100644 --- a/View/Helper/NetCommonsHtmlHelper.php +++ b/View/Helper/NetCommonsHtmlHelper.php @@ -120,6 +120,15 @@ private function __convertWebrootPath($paths) { self::$__convertPaths[$path] = $convUrl; $covPaths[] = $convUrl; } + if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) === 0) { + foreach ($covPaths as &$covPath) { + if (strncmp($covPath, '/', 1) === 0 && + $covPath !== '/components/bootstrap/dist/css/bootstrap.min.css') { + $covPath = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $covPath; + } + } + unset($covPath); + } return $covPaths; } From 85a30134428c01e5a37b22f3f6c3a9478951a0b9 Mon Sep 17 00:00:00 2001 From: Shohei Nakajima Date: Sun, 8 Mar 2020 14:36:01 +0900 Subject: [PATCH 49/67] =?UTF-8?q?fix:=20=E7=94=BB=E5=83=8F=E3=81=8C?= =?UTF-8?q?=E3=82=A2=E3=83=83=E3=83=97=E3=83=AD=E3=83=BC=E3=83=89=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=81=AA=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F=E3=83=90?= =?UTF-8?q?=E3=82=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- View/Helper/NetCommonsHtmlHelper.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/View/Helper/NetCommonsHtmlHelper.php b/View/Helper/NetCommonsHtmlHelper.php index e267c1a8..579abb20 100644 --- a/View/Helper/NetCommonsHtmlHelper.php +++ b/View/Helper/NetCommonsHtmlHelper.php @@ -120,15 +120,15 @@ private function __convertWebrootPath($paths) { self::$__convertPaths[$path] = $convUrl; $covPaths[] = $convUrl; } - if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) === 0) { - foreach ($covPaths as &$covPath) { - if (strncmp($covPath, '/', 1) === 0 && - $covPath !== '/components/bootstrap/dist/css/bootstrap.min.css') { - $covPath = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $covPath; - } - } - unset($covPath); - } + //if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) === 0) { + // foreach ($covPaths as &$covPath) { + // if (strncmp($covPath, '/', 1) === 0 && + // $covPath !== '/components/bootstrap/dist/css/bootstrap.min.css') { + // $covPath = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $covPath; + // } + // } + // unset($covPath); + //} return $covPaths; } From dc86d0ee7ceaa47b09f987493a2918ff14caa0b4 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Sat, 14 Mar 2020 20:18:27 +0900 Subject: [PATCH 50/67] feat: serve some static files via CDN --- View/Helper/NetCommonsHtmlHelper.php | 45 ++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/View/Helper/NetCommonsHtmlHelper.php b/View/Helper/NetCommonsHtmlHelper.php index 579abb20..2d8f5639 100644 --- a/View/Helper/NetCommonsHtmlHelper.php +++ b/View/Helper/NetCommonsHtmlHelper.php @@ -120,15 +120,6 @@ private function __convertWebrootPath($paths) { self::$__convertPaths[$path] = $convUrl; $covPaths[] = $convUrl; } - //if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) === 0) { - // foreach ($covPaths as &$covPath) { - // if (strncmp($covPath, '/', 1) === 0 && - // $covPath !== '/components/bootstrap/dist/css/bootstrap.min.css') { - // $covPath = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $covPath; - // } - // } - // unset($covPath); - //} return $covPaths; } @@ -149,6 +140,7 @@ public function script($url, $options = array()) { ); $url = $this->__convertWebrootPath($url); + $url = $this->__convertCDNUrls($url); return $this->Html->script($url, array_merge($defaultOptions, $options)); } @@ -168,6 +160,7 @@ public function css($path, $options = array()) { ); $path = $this->__convertWebrootPath($path); + $path = $this->__convertCDNUrls($path); return $this->Html->css($path, array_merge($defaultOptions, $options)); } @@ -246,6 +239,7 @@ public function image($path, $options = array()) { $paths = $this->__convertWebrootPath($path); $path = $paths[0]; } + $path = $this->__convertCDNUrls($path); $path = $this->__getUrl($path, $options); $output = $this->Html->image($path, $options); return $output; @@ -356,6 +350,39 @@ private function __parseLink($title, $url, &$options) { return ['title' => $title, 'url' => $url]; } +/** + * CDNを介するようなURLに変換 + * + * @param string|array $urls 変換するURL + * @return string + */ + private function __convertCDNUrls($urls) { + if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) !== 0) { + return $urls; + } + if (is_string($urls)) { + return $this->__convertCDNUrl($urls); + } + foreach ($urls as &$url) { + $url = $this->__convertCDNUrl($url); + } + return $urls; + } + +/** + * CDNを介するようなURLに変換 + * + * @param string $url 変換するURL + * @return string + */ + private function __convertCDNUrl($url) { + if (strncmp($url, '/', 1) === 0 && strpos($url, 'bootstrap.min.css') == false + && strpos($url, 'tinymce.min.js') == false) { + $url = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $url; + } + return $url; + } + /** * ``タグの出力 * From c8ad902213670a871d83f3981f8a2df45c539252 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 19 Mar 2020 02:08:21 +0900 Subject: [PATCH 51/67] feat: update all like counts as well as csrf tokens --- webroot/js/base.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/webroot/js/base.js b/webroot/js/base.js index 5126e408..c5bc33f0 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -276,6 +276,61 @@ NetCommonsApp.controller('NetCommons.base', }).trigger('hashchange'); }; + /** + * updateLikes + * + * @return {void} + */ + $scope.updateLikes = function() { + var $buttons = $('span.like-button'); + var condsStrsObj = {}; + $buttons.each(function() { condsStrsObj[this.className.split(' ')[0]] = 0; }); + var condsStrs = Object.keys(condsStrsObj); + if (!condsStrs.length) return; + + var iCondsStrs = 0; + var condsStrsList = []; + do { + // Since the maximum length of a URL is about 2000 + var list = []; + var strLength = 0; + for (; iCondsStrs < condsStrs.length && strLength < 1900; iCondsStrs++) { + list.push(condsStrs[iCondsStrs]); + strLength += condsStrs[iCondsStrs].length; + } + condsStrsList.push(list); + } while (iCondsStrs < condsStrs.length); + + var promise = Promise.resolve(); + for (var iList = 0; iList < condsStrsList.length; iList++) { + (function(iList) { + var condsStrs = condsStrsList[iList]; + promise = promise.then(function() { + var params = '?like_conds_strs=' + condsStrs.join(','); + return $http.get(NC3_URL + '/likes/likes/load.json' + params).then( + function(response) { + var likes = response.data.likes; + for (var i = 0; i < condsStrs.length; i++) { + var condsStr = condsStrs[i]; + var like = likes[condsStr] || { + like_count: 0, + unlike_count: 0, + disabled: false, + }; + var queryPrefix = '.' + condsStr; + $(queryPrefix + ' .like-count').text(like.like_count); + $(queryPrefix + ' .unlike-count').text(like.unlike_count); + $(queryPrefix + ' > a').css('display', like.disabled ? 'none' : ''); + $(queryPrefix + ' > .text-muted').css('display', like.disabled ? '' : 'none'); + } + }, + function() { + }); + }); + }(iList)); + } + }; + /** * updateTokens * From 76a66398b3ab72eb29a60b7cd4c34fe9879ccfd5 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 24 Apr 2020 17:05:46 +0900 Subject: [PATCH 52/67] fix: fade settings of javascript_alert --- webroot/js/base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index c5bc33f0..860c9640 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -216,7 +216,7 @@ NetCommonsApp.controller('NetCommons.base', }; $('#nc-flash-message').removeClass('hidden'); if (interval > 0) { - $('#nc-flash-message').fadeIn(500).fadeTo(500, 1).fadeOut(interval); + $('#nc-flash-message').fadeIn(500).fadeTo(interval, 1).fadeOut(2000); } else { $('#nc-flash-message').fadeIn(500); } From 058b57e32f58ab58d13870ca346b3495fa048121 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 24 Apr 2020 17:05:46 +0900 Subject: [PATCH 53/67] fix: fade settings of javascript_alert --- webroot/js/base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index c5bc33f0..860c9640 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -216,7 +216,7 @@ NetCommonsApp.controller('NetCommons.base', }; $('#nc-flash-message').removeClass('hidden'); if (interval > 0) { - $('#nc-flash-message').fadeIn(500).fadeTo(500, 1).fadeOut(interval); + $('#nc-flash-message').fadeIn(500).fadeTo(interval, 1).fadeOut(2000); } else { $('#nc-flash-message').fadeIn(500); } From ec2f738f3664220592e77cf7c850eb5c93f4b592 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 27 May 2020 10:38:53 +0900 Subject: [PATCH 54/67] feat: link member's login screen directly (#12) --- View/Elements/common_header.ctp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/View/Elements/common_header.ctp b/View/Elements/common_header.ctp index e7b47f57..0d63a8ae 100644 --- a/View/Elements/common_header.ctp +++ b/View/Elements/common_header.ctp @@ -123,7 +123,7 @@ if (empty($navbarStyle)) {
  • - NetCommonsHtml->link(__d('net_commons', 'Login'), '/auth/login'); ?> + NetCommonsHtml->link(__d('net_commons', 'Login'), Configure::read('App.memberUrl'). '/auth/login'); ?>
  • From 45b9990fb94701ff29fef40a37893b0e5ce06362 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 8 Jul 2020 17:02:52 +0900 Subject: [PATCH 55/67] =?UTF-8?q?fix:=20Ajax=E3=81=A7=E6=8A=95=E3=81=92?= =?UTF-8?q?=E3=82=8BURL=E3=82=92=E3=83=96=E3=83=A9=E3=82=A6=E3=82=B6?= =?UTF-8?q?=E3=81=A7=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B=E3=81=A8=E3=82=AD?= =?UTF-8?q?=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5=E3=81=8C=E5=8F=96=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=97=E3=81=BE=E3=81=86=E3=83=90=E3=82=B0=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=20https://github.com/edumap-jp/Edumap2/issues/95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 3de4e7ed..9d71aa64 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -240,6 +240,11 @@ private function __updateFullBaseUrl() { public function beforeFilter() { parent::beforeFilter(); + if (!empty($this->request->params['ext']) && + $this->request->params['ext'] === 'json') { + $this->request->addDetector('ajax', ['param' => 'ext', 'value' => 'json']); + } + if ($this->request->is('ajax') || $this->request->query('no-cache')) { $this->response->header('Pragma', 'no-cache'); } From d8dc8eab426c90ea37c320553feb9b947dfb4d60 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 31 Jul 2020 19:45:02 +0900 Subject: [PATCH 56/67] =?UTF-8?q?fix:=20test:=20Notice=E3=81=8C=E7=99=BA?= =?UTF-8?q?=E7=94=9F=E3=81=99=E3=82=8B=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- View/Helper/NetCommonsHtmlHelper.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/View/Helper/NetCommonsHtmlHelper.php b/View/Helper/NetCommonsHtmlHelper.php index 2d8f5639..946574b0 100644 --- a/View/Helper/NetCommonsHtmlHelper.php +++ b/View/Helper/NetCommonsHtmlHelper.php @@ -357,7 +357,8 @@ private function __parseLink($title, $url, &$options) { * @return string */ private function __convertCDNUrls($urls) { - if (strncmp($_SERVER['HTTP_HOST'], 'member-', 7) !== 0) { + $httpHost = $_SERVER['HTTP_HOST'] ?? ''; + if (strncmp($httpHost, 'member-', 7) !== 0) { return $urls; } if (is_string($urls)) { @@ -378,7 +379,8 @@ private function __convertCDNUrls($urls) { private function __convertCDNUrl($url) { if (strncmp($url, '/', 1) === 0 && strpos($url, 'bootstrap.min.css') == false && strpos($url, 'tinymce.min.js') == false) { - $url = 'https://' . substr($_SERVER['HTTP_HOST'], 7) . $url; + $httpHost = $_SERVER['HTTP_HOST'] ?? ''; + $url = 'https://' . substr($httpHost, 7) . $url; } return $url; } From a337bad572ca7996d6c1f356a1cc55ccb9fc9a03 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 31 Jul 2020 21:14:34 +0900 Subject: [PATCH 57/67] =?UTF-8?q?style:=20test:=20phpcs,phpmd,gjslint?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Config/routes.php | 6 +- Controller/NetCommonsAppController.php | 1 + Utility/NetCommonsCDNCache.php | 14 +- View/Elements/common_header.ctp | 3 +- webroot/js/base.js | 347 +++++++++++++------------ 5 files changed, 188 insertions(+), 183 deletions(-) diff --git a/Config/routes.php b/Config/routes.php index 4fb7f647..295f8648 100644 --- a/Config/routes.php +++ b/Config/routes.php @@ -12,9 +12,9 @@ App::uses('Current', 'NetCommons.Utility'); Router::connect( - '/invalidate', - array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'invalidate'), - array('routeClass' => 'SlugRoute') + '/invalidate', + array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'invalidate'), + array('routeClass' => 'SlugRoute') ); Router::connect( diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index a0a05971..e89a3ef0 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -236,6 +236,7 @@ private function __updateFullBaseUrl() { * beforeFilter * * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function beforeFilter() { parent::beforeFilter(); diff --git a/Utility/NetCommonsCDNCache.php b/Utility/NetCommonsCDNCache.php index 06a38791..7f00ba36 100644 --- a/Utility/NetCommonsCDNCache.php +++ b/Utility/NetCommonsCDNCache.php @@ -41,16 +41,16 @@ public function invalidate() { $now = microtime(true); if ($now - $lastTime > self::NO_CACHE_INVALIDATION_DURATION_SEC) { $ncCache->write($now); - $this->postInvalidationRequest(); + $this->__postInvalidationRequest(); } } - /** - * Invalidate CDN Cache - * - * @return void - */ - private function postInvalidationRequest() { +/** + * Invalidate CDN Cache + * + * @return void + */ + private function __postInvalidationRequest() { $cacheDomain = Configure::read('App.cacheDomain'); if (!isset($cacheDomain)) { return; diff --git a/View/Elements/common_header.ctp b/View/Elements/common_header.ctp index 0d63a8ae..32c5bb68 100644 --- a/View/Elements/common_header.ctp +++ b/View/Elements/common_header.ctp @@ -123,7 +123,8 @@ if (empty($navbarStyle)) {
  • - NetCommonsHtml->link(__d('net_commons', 'Login'), Configure::read('App.memberUrl'). '/auth/login'); ?> + NetCommonsHtml->link(__d('net_commons', 'Login'), + Configure::read('App.memberUrl') . '/auth/login'); ?>
  • diff --git a/webroot/js/base.js b/webroot/js/base.js index 860c9640..2ff3c444 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -19,6 +19,7 @@ NetCommonsApp.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; }]); + /** * ncHtmlContent filter * @@ -168,185 +169,187 @@ NetCommonsApp.factory('ajaxSendPost', ['$http', '$q', 'NC3_URL', function($http, * base controller */ NetCommonsApp.controller('NetCommons.base', - ['$scope', '$location', '$window', '$http', 'NC3_URL', function($scope, $location, $window, $http, NC3_URL) { - - /** - * Base URL - * - * @type {string} - */ - $scope.baseUrl = NC3_URL; - - /** - * sending - * - * @type {boolean} - */ - $scope.sending = false; - - /** - * messages - * - * @type {Object} - */ - $scope.messages = {}; - - /** - * top - * - * @type {function} - */ - $scope.top = function() { - $location.hash('nc-modal-top'); - $anchorScroll(); - }; + ['$scope', '$location', '$window', '$http', 'NC3_URL', + function($scope, $location, $window, $http, NC3_URL) { + + /** + * Base URL + * + * @type {string} + */ + $scope.baseUrl = NC3_URL; + + /** + * sending + * + * @type {boolean} + */ + $scope.sending = false; + + /** + * messages + * + * @type {Object} + */ + $scope.messages = {}; + + /** + * top + * + * @type {function} + */ + $scope.top = function() { + $location.hash('nc-modal-top'); + $anchorScroll(); + }; - /** - * flash message method - * - * @param {string} message - * @param {string} messageClass - * @param {int} interval - * @return {void} - */ - $scope.flashMessage = function(message, messageClass, interval) { - $scope.flash = { - message: message, - class: messageClass + /** + * flash message method + * + * @param {string} message + * @param {string} messageClass + * @param {int} interval + * @return {void} + */ + $scope.flashMessage = function(message, messageClass, interval) { + $scope.flash = { + message: message, + class: messageClass + }; + $('#nc-flash-message').removeClass('hidden'); + if (interval > 0) { + $('#nc-flash-message').fadeIn(500).fadeTo(interval, 1).fadeOut(2000); + } else { + $('#nc-flash-message').fadeIn(500); + } }; - $('#nc-flash-message').removeClass('hidden'); - if (interval > 0) { - $('#nc-flash-message').fadeIn(500).fadeTo(interval, 1).fadeOut(2000); - } else { - $('#nc-flash-message').fadeIn(500); - } - }; - /** - * submit - * - * @type {function} - */ - $scope.submit = function($event) { - if ($scope.sending) { - $event.preventDefault(); - } - $scope.sending = true; - }; + /** + * submit + * + * @type {function} + */ + $scope.submit = function($event) { + if ($scope.sending) { + $event.preventDefault(); + } + $scope.sending = true; + }; - /** - * cancel - * - * @type {function} - */ - $scope.cancel = function(url) { - $scope.sending = true; - if ($window.location.href === url) { - $window.location.reload(); - } else { - $window.location.href = url; - } - }; + /** + * cancel + * + * @type {function} + */ + $scope.cancel = function(url) { + $scope.sending = true; + if ($window.location.href === url) { + $window.location.reload(); + } else { + $window.location.href = url; + } + }; - /** - * hashChange - * - * @return {void} - */ - $scope.hashChange = function() { - $($window).bind('hashchange', function() { - var hash = $location.hash(); - var frameId = $window.location.href.match('frame_id=([0-9]+)'); - var element = null; - try { - if (hash) { - if (hash.substr(0, 1) === '/') { - element = $('#' + hash.substr(1)); - } else { - element = $('#' + hash); + /** + * hashChange + * + * @return {void} + */ + $scope.hashChange = function() { + $($window).bind('hashchange', function() { + var hash = $location.hash(); + var frameId = $window.location.href.match('frame_id=([0-9]+)'); + var element = null; + try { + if (hash) { + if (hash.substr(0, 1) === '/') { + element = $('#' + hash.substr(1)); + } else { + element = $('#' + hash); + } + } else if (frameId) { + element = $('#frame-' + frameId[1]); } - } else if (frameId) { - element = $('#frame-' + frameId[1]); + var pos = element.offset().top; + $window.scrollTo(0, pos - 100); + } catch (err) { + $window.scrollTo(0, 0); } - var pos = element.offset().top; - $window.scrollTo(0, pos - 100); - } catch (err) { - $window.scrollTo(0, 0); - } - }).trigger('hashchange'); - }; + }).trigger('hashchange'); + }; - /** - * updateLikes - * - * @return {void} - */ - $scope.updateLikes = function() { - var $buttons = $('span.like-button'); - var condsStrsObj = {}; - $buttons.each(function() { condsStrsObj[this.className.split(' ')[0]] = 0; }); - var condsStrs = Object.keys(condsStrsObj); - if (!condsStrs.length) return; - - var iCondsStrs = 0; - var condsStrsList = []; - do { - // Since the maximum length of a URL is about 2000 - var list = []; - var strLength = 0; - for (; iCondsStrs < condsStrs.length && strLength < 1900; iCondsStrs++) { - list.push(condsStrs[iCondsStrs]); - strLength += condsStrs[iCondsStrs].length; + /** + * updateLikes + * + * @return {void} + */ + $scope.updateLikes = function() { + var $buttons = $('span.like-button'); + var condsStrsObj = {}; + $buttons.each(function() { condsStrsObj[this.className.split(' ')[0]] = 0; }); + var condsStrs = Object.keys(condsStrsObj); + if (!condsStrs.length) return; + + var iCondsStrs = 0; + var condsStrsList = []; + do { + // Since the maximum length of a URL is about 2000 + var list = []; + var strLength = 0; + for (; iCondsStrs < condsStrs.length && strLength < 1900; iCondsStrs++) { + list.push(condsStrs[iCondsStrs]); + strLength += condsStrs[iCondsStrs].length; + } + condsStrsList.push(list); + } while (iCondsStrs < condsStrs.length); + + var promise = Promise.resolve(); + for (var iList = 0; iList < condsStrsList.length; iList++) { + (function(iList) { + var condsStrs = condsStrsList[iList]; + promise = promise.then(function() { + var params = '?like_conds_strs=' + condsStrs.join(','); + return $http.get(NC3_URL + '/likes/likes/load.json' + params).then( + function(response) { + var likes = response.data.likes; + for (var i = 0; i < condsStrs.length; i++) { + var condsStr = condsStrs[i]; + var like = likes[condsStr] || { + like_count: 0, + unlike_count: 0, + disabled: false, + }; + var queryPrefix = '.' + condsStr; + $(queryPrefix + ' .like-count').text(like.like_count); + $(queryPrefix + ' .unlike-count').text(like.unlike_count); + $(queryPrefix + ' > a').css('display', like.disabled ? 'none' : ''); + $(queryPrefix + ' > .text-muted') + .css('display', like.disabled ? '' : 'none'); + } + }, + function() { + }); + }); + }(iList)); } - condsStrsList.push(list); - } while (iCondsStrs < condsStrs.length); - - var promise = Promise.resolve(); - for (var iList = 0; iList < condsStrsList.length; iList++) { - (function(iList) { - var condsStrs = condsStrsList[iList]; - promise = promise.then(function() { - var params = '?like_conds_strs=' + condsStrs.join(','); - return $http.get(NC3_URL + '/likes/likes/load.json' + params).then( - function(response) { - var likes = response.data.likes; - for (var i = 0; i < condsStrs.length; i++) { - var condsStr = condsStrs[i]; - var like = likes[condsStr] || { - like_count: 0, - unlike_count: 0, - disabled: false, - }; - var queryPrefix = '.' + condsStr; - $(queryPrefix + ' .like-count').text(like.like_count); - $(queryPrefix + ' .unlike-count').text(like.unlike_count); - $(queryPrefix + ' > a').css('display', like.disabled ? 'none' : ''); - $(queryPrefix + ' > .text-muted').css('display', like.disabled ? '' : 'none'); - } - }, - function() { - }); - }); - }(iList)); - } - }; + }; - /** - * updateTokens - * - * @return {void} - */ - $scope.updateTokens = function() { - var $token = $('input[name="data[_Token][key]"]'); - var $submit = $token.closest('form').find(':submit'); - $submit.attr('disabled', 'disabled'); - $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') - .then(function(response) { - var token = response.data; - $token.val(token.data._Token.key); - $submit.removeAttr('disabled'); - }, - function() { - }); - }; - }]); + /** + * updateTokens + * + * @return {void} + */ + $scope.updateTokens = function() { + var $token = $('input[name="data[_Token][key]"]'); + var $submit = $token.closest('form').find(':submit'); + $submit.attr('disabled', 'disabled'); + $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') + .then(function(response) { + var token = response.data; + $token.val(token.data._Token.key); + $submit.removeAttr('disabled'); + }, + function() { + }); + }; + }]); From 0dd7f44f6e21c36b52c8519ea39615b38b05dd24 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 31 Jul 2020 21:37:52 +0900 Subject: [PATCH 58/67] =?UTF-8?q?style:=20test:=20gjslint=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webroot/js/base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index 2ff3c444..6304796f 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -344,7 +344,7 @@ NetCommonsApp.controller('NetCommons.base', var $submit = $token.closest('form').find(':submit'); $submit.attr('disabled', 'disabled'); $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') - .then(function(response) { + .then(function(response) { var token = response.data; $token.val(token.data._Token.key); $submit.removeAttr('disabled'); From bb48a100ef1f0bcd0992c0b668a46755d00d1c30 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Mon, 3 Aug 2020 23:34:36 +0900 Subject: [PATCH 59/67] =?UTF-8?q?revert:=20no-cache=E3=81=8C=E3=81=AA?= =?UTF-8?q?=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=97=E3=81=BE=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index e89a3ef0..9aaa2bd1 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -246,6 +246,10 @@ public function beforeFilter() { $this->request->addDetector('ajax', ['param' => 'ext', 'value' => 'json']); } + if ($this->request->is('ajax') || $this->request->query('no-cache')) { + $this->response->header('Pragma', 'no-cache'); + } + if (empty($this->request->params['requested'])) { $this->request->allowMethod($this->_allowMethods); } From 1198a9d0db6e1578c9c061a6eb1453e6cf5bf8d0 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Tue, 4 Aug 2020 06:06:02 +0900 Subject: [PATCH 60/67] =?UTF-8?q?style:=20test:=20phpmd=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E5=9B=9E=E9=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 9aaa2bd1..106448b5 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -237,6 +237,7 @@ private function __updateFullBaseUrl() { * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ public function beforeFilter() { parent::beforeFilter(); From 6308ddacd7ad2d54be914a5b2e0d7356e2165e39 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Mon, 26 Oct 2020 18:59:04 +0900 Subject: [PATCH 61/67] Revert "Merge branch 'availability' into availability" This reverts commit 5b507bf24deccbfd0e98cbec876601bf43feaddd, reversing changes made to d0a650094176bd883feb569385178d7d259dd730. --- View/Elements/common_footer.ctp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/View/Elements/common_footer.ctp b/View/Elements/common_footer.ctp index 03234090..26355500 100644 --- a/View/Elements/common_footer.ctp +++ b/View/Elements/common_footer.ctp @@ -10,12 +10,7 @@ ?>
    From 4ab2b6185e79dee82d0708555fea885896d75a11 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 29 Oct 2020 17:26:36 +0900 Subject: [PATCH 62/67] fix: support dot for member and origin (#601) --- Controller/NetCommonsAppController.php | 2 +- View/Helper/CDNCacheHelper.php | 4 +++- View/Helper/NetCommonsHtmlHelper.php | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index 106448b5..cb06ee0f 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -217,7 +217,7 @@ private function __updateFullBaseUrl() { return false; } - $nonMemberUrl = str_replace("member-", "", $memberUrl); + $nonMemberUrl = preg_replace("/^member[.-]/", "", $memberUrl); $authPlugins = array('auth', 'auth_general'); $isAuthPlugin = in_array($this->request->plugin, $authPlugins, true); // Auth 関連の Plugin の場合は memberUrl を fullBaseUrl にセットし、 diff --git a/View/Helper/CDNCacheHelper.php b/View/Helper/CDNCacheHelper.php index afa24237..663537c7 100644 --- a/View/Helper/CDNCacheHelper.php +++ b/View/Helper/CDNCacheHelper.php @@ -21,9 +21,11 @@ class CDNCacheHelper extends AppHelper { * @return bool a boolean value whether the page is cacheable or not. */ public function isCacheable() { + $nonOrigin = strncmp('origin-', $_SERVER['HTTP_HOST'], 7) !== 0 && + strncmp('origin.', $_SERVER['HTTP_HOST'], 7) !== 0; $nonCacheable = (isset($this->_View->response->header()['Pragma']) && $this->_View->response->header()['Pragma'] === 'no-cache') || - strncmp('origin-', $_SERVER['HTTP_HOST'], 7) !== 0; + $nonOrigin; return !$nonCacheable; } } diff --git a/View/Helper/NetCommonsHtmlHelper.php b/View/Helper/NetCommonsHtmlHelper.php index 946574b0..ed902571 100644 --- a/View/Helper/NetCommonsHtmlHelper.php +++ b/View/Helper/NetCommonsHtmlHelper.php @@ -358,7 +358,9 @@ private function __parseLink($title, $url, &$options) { */ private function __convertCDNUrls($urls) { $httpHost = $_SERVER['HTTP_HOST'] ?? ''; - if (strncmp($httpHost, 'member-', 7) !== 0) { + $nonMember = strncmp($httpHost, 'member-', 7) !== 0 && + strncmp($httpHost, 'member.', 7) !== 0; + if ($nonMember) { return $urls; } if (is_string($urls)) { From 1a83db0315ae8cdb6b2c313e5a085a37bdabbe3d Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Sat, 31 Oct 2020 09:08:22 +0900 Subject: [PATCH 63/67] =?UTF-8?q?fix:=20=E6=AD=A3=E8=A6=8F=E8=A1=A8?= =?UTF-8?q?=E7=8F=BE=E3=81=ABschema=E3=81=8C=E3=81=AA=E3=81=84=E3=81=9F?= =?UTF-8?q?=E3=82=81=E7=84=A1=E9=99=90=E3=83=AB=E3=83=BC=E3=83=97=E3=81=AB?= =?UTF-8?q?=E9=99=A5=E3=82=8B=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index cb06ee0f..273dfdf1 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -217,7 +217,7 @@ private function __updateFullBaseUrl() { return false; } - $nonMemberUrl = preg_replace("/^member[.-]/", "", $memberUrl); + $nonMemberUrl = preg_replace("#^(http(s)?://)member[.-]#", "$1", $memberUrl); $authPlugins = array('auth', 'auth_general'); $isAuthPlugin = in_array($this->request->plugin, $authPlugins, true); // Auth 関連の Plugin の場合は memberUrl を fullBaseUrl にセットし、 From e56b820617a9d43115fec9092f10836cbc5d0b64 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 8 Jan 2021 19:27:43 +0900 Subject: [PATCH 64/67] =?UTF-8?q?fix:=20test:=20phpmd=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controller/NetCommonsAppController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/NetCommonsAppController.php b/Controller/NetCommonsAppController.php index ccfad2ab..ddee2d7e 100644 --- a/Controller/NetCommonsAppController.php +++ b/Controller/NetCommonsAppController.php @@ -169,6 +169,7 @@ public function __construct($request = null, $response = null) { * 事前準備 * * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function __prepare() { if (Current::read('Block') && From c935bdf3a8b0bde660953d278ffeed6c64476056 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 27 Jan 2021 19:36:10 +0900 Subject: [PATCH 65/67] fix: don't use native Promise --- webroot/js/base.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index bbfe21b6..adb2da55 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -169,9 +169,8 @@ NetCommonsApp.factory('ajaxSendPost', ['$http', '$q', 'NC3_URL', function($http, * base controller */ NetCommonsApp.controller('NetCommons.base', - ['$scope', '$location', '$window', '$http', 'NC3_URL', - function($scope, $location, $window, $http, NC3_URL) { - + ['$scope', '$location', '$window', '$http', 'NC3_URL', '$q', + function($scope, $location, $window, $http, NC3_URL, $q) { /** * Base URL * @@ -293,9 +292,9 @@ NetCommonsApp.controller('NetCommons.base', var iCondsStrs = 0; var condsStrsList = []; do { - // Since the maximum length of a URL is about 2000 var list = []; var strLength = 0; + // Since the maximum length of a URL is about 2000 for (; iCondsStrs < condsStrs.length && strLength < 1900; iCondsStrs++) { list.push(condsStrs[iCondsStrs]); strLength += condsStrs[iCondsStrs].length; @@ -303,11 +302,15 @@ NetCommonsApp.controller('NetCommons.base', condsStrsList.push(list); } while (iCondsStrs < condsStrs.length); - var promise = Promise.resolve(); + var deferred = $q.defer(); + var promise = deferred.promise; for (var iList = 0; iList < condsStrsList.length; iList++) { + // Pass iList to the following anonymous function to keep the current value (function(iList) { var condsStrs = condsStrsList[iList]; + // Chain promise.then() to call API sequentially promise = promise.then(function() { + console.log(condsStrs); var params = '?like_conds_strs=' + condsStrs.join(','); return $http.get(NC3_URL + '/likes/likes/load.json' + params).then( function(response) { @@ -326,12 +329,11 @@ NetCommonsApp.controller('NetCommons.base', $(queryPrefix + ' > .text-muted') .css('display', like.disabled ? '' : 'none'); } - }, - function() { }); }); }(iList)); } + deferred.resolve(); }; /** From e71ebb3d128d3f0e4b9be2a175aed1f6d0a6d760 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Wed, 27 Jan 2021 19:49:48 +0900 Subject: [PATCH 66/67] fix: remove debug code --- webroot/js/base.js | 1 - 1 file changed, 1 deletion(-) diff --git a/webroot/js/base.js b/webroot/js/base.js index adb2da55..d71d4b23 100644 --- a/webroot/js/base.js +++ b/webroot/js/base.js @@ -310,7 +310,6 @@ NetCommonsApp.controller('NetCommons.base', var condsStrs = condsStrsList[iList]; // Chain promise.then() to call API sequentially promise = promise.then(function() { - console.log(condsStrs); var params = '?like_conds_strs=' + condsStrs.join(','); return $http.get(NC3_URL + '/likes/likes/load.json' + params).then( function(response) { From 3b7a5276605e4400cf2371c21a7e22861e5703fa Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Fri, 22 Apr 2022 23:49:12 +0900 Subject: [PATCH 67/67] =?UTF-8?q?test:=20github=20actions=E3=81=AE?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 95015662..d6b4be4a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -38,7 +38,7 @@ jobs: NC3_BUILD_DIR: "/opt/nc3" NC3_DOCKER_DIR: "/opt/docker" NC3_GIT_URL: "git://github.com/NetCommons3/NetCommons3.git" - NC3_GIT_BRANCH: "master" + NC3_GIT_BRANCH: "availability" PLUGIN_BUILD_DIR: ${{ github.workspace }} PHP_VERSION: ${{ matrix.php }} MYSQL_VERSION: ${{ matrix.mysql }}