Skip to content

Commit

Permalink
fix #8035 デフォルトでスマートURLオンの状態に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Mar 28, 2015
1 parent 063e8d0 commit f1c8e92
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 337 deletions.
42 changes: 3 additions & 39 deletions .htaccess
@@ -1,39 +1,3 @@
#
# /.htaccess
#
# 【スマートURLについて】
# 短くスマートなURLを実現する為には、ApacheのRewriteモジュールが必要です。
# スマートURLの設定は、管理画面のシステム設定より行えます。
# (例)
# スマートURLオフ:http://localhost/index.php/contact/index
# スマートURLオン:http://localhost/contact/index
#
# 【スマートURLの設定に失敗した場合】
# /.htaccess と /app/webroot/.htaccess に記述されている
# RewriteBase 設定をサーバー環境に合わせて調整してください。
# マルチドメインの場合は、マルチドメインの設定フォルダを設定値に
# 付け加えるうまくいく事が多いようです。
#
# (例)マルチドメインのフォルダが「test」の場合
# RewriteBase /test
#
# 【スマートURLを手動でオンにする】
# ■ /.htaccess と /app/webroot/.htaccess にRewrite設定を記述する必要があります。
# /.htaccess では次の4行を記述します。
# RewriteEngine on
# RewriteBase /
# RewriteRule ^$ app/webroot/ [L]
# RewriteRule (.*) app/webroot/$1 [L]
# ■ /app/Config/install.php に次の行を追加します。
# Configure::write('App.baseUrl', '');
#
# 【スマートURLを手動でオフにする】
# ■ /.htaccess と、/app/webroot/.htaccess 内の、次の行で始まる行を全て削除します。
# RewriteEngine / RewriteBase / RewriteCond / RewriteRule
# ■ /app/Config/install.php の次の行を削除します。
# Configure::write('App.baseUrl', '');
#
# 【動作確認済レンタルサーバー】
# http://basercms.net/manuals/introductions/2.html
#

RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -45,7 +45,7 @@ before_script:
- bash -c 'if [ "$TRAVIS_PHP_VERSION" != "5.2" ]; then composer install --dev; fi;'
- cd app
- php ./Console/cake.php bc_manager checkenv
- php ./Console/cake.php bc_manager install "http://localhost" "$DB" "admin" "basercms" "webmaster@example.org" --host "localhost" --database "basercms" --login "basercms" --password "basercms" --prefix "mysite_" --smarturl "true" --data "nada-icons.default"
- php ./Console/cake.php bc_manager install "http://localhost" "$DB" "admin" "basercms" "webmaster@example.org" --host "localhost" --database "basercms" --login "basercms" --password "basercms" --prefix "mysite_" --data "nada-icons.default"

script:
- php ./Console/cake.php baser_test baser BcAll
Expand Down
7 changes: 1 addition & 6 deletions app/Config/core.php
Expand Up @@ -110,12 +110,7 @@
* included primarily as a development convenience - and
* thus not recommended for production applications.
*/
// CUSTOMIZE MODIFY 2014/07/03 ryuring
// >>>
////Configure::write('App.baseUrl', env('SCRIPT_NAME'));
// ---
Configure::write('App.baseUrl', $_SERVER['SCRIPT_NAME']);
// <<<
//Configure::write('App.baseUrl', env('SCRIPT_NAME'));

/**
* To configure CakePHP to use a particular domain URL
Expand Down
43 changes: 4 additions & 39 deletions app/webroot/.htaccess
@@ -1,40 +1,5 @@
#
# /app/webroot/.htaccess
#
# 【スマートURLについて】
# 短くスマートなURLを実現する為には、ApacheのRewriteモジュールが必要です。
# スマートURLの設定は、管理画面のシステム設定より行えます。
# (例)
# スマートURLオフ:http://localhost/index.php/contact/index
# スマートURLオン:http://localhost/contact/index
#
# 【スマートURLの設定に失敗した場合】
# /.htaccess と /app/webroot/.htaccess に記述されている
# RewriteBase 設定をサーバー環境に合わせて調整してください。
# マルチドメインの場合は、マルチドメインの設定フォルダを設定値に
# 付け加えるうまくいく事が多いようです。
#
# (例)マルチドメインのフォルダが「test」の場合
# RewriteBase /test/app/webroot
#
# 【スマートURLを手動でオンにする】
# ■ /.htaccess と /app/webroot/.htaccess にRewrite設定を記述する必要があります。
# /app/webroot/.htaccess には次の5行を記述します。
# RewriteEngine on
# RewriteBase /app/webroot
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^(.*)$ index.php [QSA,L]
# ■ /app/Config/install.php に次の行を追加します。
# Configure::write('App.baseUrl', '');
#
# 【スマートURLを手動でオフにする】
# ■ /.htaccess と、/app/webroot/.htaccess 内の、次の行で始まる行を全て削除します。
# RewriteEngine / RewriteBase / RewriteCond / RewriteRule
# ■ /app/Config/install.php の次の行を削除します。
# Configure::write('App.baseUrl', '');
#
# 【動作確認済レンタルサーバー】
# http://basercms.net/manuals/introductions/2.html
#
Allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
13 changes: 2 additions & 11 deletions lib/Baser/Console/Command/BcManagerShell.php
Expand Up @@ -50,9 +50,6 @@ public function getOptionParser() {
))->addOption('port', array(
'help' => 'データベースポート番号',
'default' => ''
))->addOption('smarturl', array(
'help' => 'スマートURLの利用',
'default' => 'true'
))->addOption('baseurl', array(
'help' => 'ベースとなるURL',
'default' => '/'
Expand All @@ -75,7 +72,7 @@ public function startup() {
/**
* インストール
*
* cake bc_manager install "サイト名" "データベースの種類" "管理者アカウント名" "管理者パスワード" "管理者Eメール" -host "DBホスト名" -database "DB名" -login "DBユーザー名" -password "DBパスワード" -prefix "DBプレフィックス" -port "DBポート" -smarturl "スマートURL(true / false)" -baseurl "RewriteBaseに設定するURL"
* cake bc_manager install "サイト名" "データベースの種類" "管理者アカウント名" "管理者パスワード" "管理者Eメール" -host "DBホスト名" -database "DB名" -login "DBユーザー名" -password "DBパスワード" -prefix "DBプレフィックス" -port "DBポート" -baseurl "RewriteBaseに設定するURL"
*/
public function install() {
if (BC_INSTALLED) {
Expand Down Expand Up @@ -233,12 +230,6 @@ protected function _install() {
'email' => $this->args[4],
);

if (isset($this->params['smarturl'])) {
$smartUrl = (boolean)$this->params['smarturl'];
} else {
$smartUrl = false;
}

if (isset($this->params['baseurl'])) {
$baseUrl = $this->params['baseurl'];
} else {
Expand All @@ -251,7 +242,7 @@ protected function _install() {
$dataPattern = 'core.demo';
}

return $this->BcManager->install($siteUrl, $dbConfig, $adminUser, $smartUrl, $baseUrl, $dataPattern);
return $this->BcManager->install($siteUrl, $dbConfig, $adminUser, $baseUrl, $dataPattern);
}

/**
Expand Down
149 changes: 1 addition & 148 deletions lib/Baser/Controller/Component/BcManagerComponent.php
Expand Up @@ -45,7 +45,7 @@ public function startup(Controller $controller) {
* @param type $adminEmail
* @return boolean
*/
public function install($siteUrl, $dbConfig, $adminUser = array(), $smartUrl = false, $baseUrl = '', $dbDataPattern = '') {
public function install($siteUrl, $dbConfig, $adminUser = array(), $baseUrl = '', $dbDataPattern = '') {
if (!$dbDataPattern) {
$dbDataPattern = Configure::read('BcApp.defaultTheme') . '.default';
}
Expand Down Expand Up @@ -139,12 +139,6 @@ public function install($siteUrl, $dbConfig, $adminUser = array(), $smartUrl = f
$result = false;
}

if ($smartUrl) {
if (!$this->setSmartUrl(true, $baseUrl)) {
$this->log('スマートURLの設定に失敗しました。.htaccessの書き込み権限を確認してください。');
}
}

//SiteConfigを再設定
loadSiteConfig();

Expand Down Expand Up @@ -1371,14 +1365,6 @@ public function resetFiles() {
public function reset($dbConfig) {
$result = true;

// スマートURLをオフに設定
if ($this->smartUrl()) {
if (!$this->setSmartUrl(false)) {
$result = false;
$this->log('スマートURLの設定を正常に初期化できませんでした。');
}
}

if (BC_INSTALLED) {
// 設定ファイルを初期化
if (!$this->resetSetting()) {
Expand Down Expand Up @@ -1410,139 +1396,6 @@ public function reset($dbConfig) {
return $result;
}

/**
* スマートURLの設定を取得
*
* @return boolean
* @access public
*/
public function smartUrl() {
if (Configure::read('App.baseUrl')) {
return false;
} else {
return true;
}
}

/**
* スマートURLの設定を行う
*
* @param boolean $smartUrl
* @return boolean
* @access public
*/
public function setSmartUrl($smartUrl, $baseUrl = '') {
/* install.php の編集 */
if ($smartUrl) {
if (!$this->setInstallSetting('App.baseUrl', "''")) {
return false;
}
} else {
if (!$this->setInstallSetting('App.baseUrl', '$_SERVER[\'SCRIPT_NAME\']')) {
return false;
}
}

if (BC_DEPLOY_PATTERN == 2 || BC_DEPLOY_PATTERN == 3) {
$webrootRewriteBase = '/';
} else {
$webrootRewriteBase = '/' . APP_DIR . '/webroot';
}

/* /app/webroot/.htaccess の編集 */
$this->_setSmartUrlToHtaccess(WWW_ROOT . '.htaccess', $smartUrl, 'webroot', $webrootRewriteBase, $baseUrl);

if (BC_DEPLOY_PATTERN == 1) {
/* /.htaccess の編集 */
$this->_setSmartUrlToHtaccess(ROOT . DS . '.htaccess', $smartUrl, 'root', '/', $baseUrl);
}

return true;
}

/**
* .htaccess にスマートURLの設定を書きこむ
*
* @param string $path
* @param array $rewriteSettings
* @return boolean
* @access protected
*/
protected function _setSmartUrlToHtaccess($path, $smartUrl, $type, $rewriteBase = '/', $baseUrl = '') {
//======================================================================
// WindowsのXAMPP環境では、何故か .htaccess を書き込みモード「w」で開けなかったの
// で、追記モード「a」で開くことにした。そのため、実際の書き込み時は、 ftruncate で、
// 内容をリセットし、ファイルポインタを先頭に戻している。
//======================================================================

$rewritePatterns = array(
"/\n[^\n#]*RewriteEngine.+/i",
"/\n[^\n#]*RewriteBase.+/i",
"/\n[^\n#]*RewriteCond.+/i",
"/\n[^\n#]*RewriteRule.+/i"
);
if (!$smartUrl) {
$rewritePatterns[] = "/\n\z/";
}
switch ($type) {
case 'root':
$rewriteSettings = array('RewriteEngine on',
'RewriteBase ' . $this->getRewriteBase($rewriteBase, $baseUrl),
'RewriteRule ^$ ' . APP_DIR . '/webroot/ [L]',
'RewriteRule (.*) ' . APP_DIR . '/webroot/$1 [L]',
'');
break;
case 'webroot':
$rewriteSettings = array('RewriteEngine on',
'RewriteBase ' . $this->getRewriteBase($rewriteBase, $baseUrl),
'RewriteCond %{REQUEST_FILENAME} !-d',
'RewriteCond %{REQUEST_FILENAME} !-f',
'RewriteRule ^(.*)$ index.php [QSA,L]',
'');
break;
}

$file = new File($path);
$file->open('a+');
$data = $file->read();
foreach ($rewritePatterns as $rewritePattern) {
$data = preg_replace($rewritePattern, '', $data);
}
if ($smartUrl) {
$data .= "\n" . implode("\n", $rewriteSettings);
}
ftruncate($file->handle, 0);
if (!$file->write($data)) {
$file->close();
return false;
}
$file->close();
}

/**
* RewriteBase の設定を取得する
*
* @param string $base
* @return string
*/
public function getRewriteBase($url, $baseUrl = null) {
if (!$baseUrl) {
$baseUrl = BC_BASE_URL;
}

if (preg_match("/index\.php/", $baseUrl)) {
$baseUrl = str_replace('/index.php', '', $baseUrl);
}
$baseUrl = preg_replace("/\/$/", '', $baseUrl);
if ($url != '/' || !$baseUrl) {
$url = $baseUrl . $url;
} else {
$url = $baseUrl;
}

return $url;
}

/**
* インストール設定を書き換える
*
Expand Down

0 comments on commit f1c8e92

Please sign in to comment.