/** * attempt to create an htaccess file * .htaccess creation only works for base_installations because of the $dirPrefix variable * This is for the rewrite_rule and TestResponse() which uses AbsoluteUrl() * * @access public * @static * @since 1.7 * * @param string $destination The root path of the installation * @param array $config Current installation configuration */ static function InstallHtaccess($destination, $config) { global $install_ftp_connection, $dirPrefix; includeFile('admin/admin_permalinks.php'); //only proceed with save if we can test the results if (!gpRemoteGet::Test()) { return; } $GLOBALS['config']['homepath'] = false; //to prevent a warning from absoluteUrl() $file = $destination . '/.htaccess'; $original_contents = false; if (file_exists($file)) { $original_contents = file_get_contents($file); } $contents = admin_permalinks::Rewrite_Rules(true, $dirPrefix, $original_contents); if (!isset($config['useftp'])) { //echo 'not using ftp'; $fp = @fopen($file, 'wb'); if (!$fp) { return; } @fwrite($fp, $contents); fclose($fp); @chmod($file, 0666); //return .htaccess to original state if (!admin_permalinks::TestResponse()) { if ($original_contents === false) { unlink($file); } else { $fp = @fopen($file, 'wb'); if ($fp) { @fwrite($fp, $original_contents); fclose($fp); } } } return; } //using ftp $file = $config['ftp_root'] . '/.htaccess'; $temp = tmpfile(); if (!$temp) { return false; } fwrite($temp, $contents); fseek($temp, 0); //Skip back to the start of the file being written to @ftp_fput($install_ftp_connection, $file, $temp, FTP_ASCII); fclose($temp); //return .htaccess to original state if (!admin_permalinks::TestResponse()) { if ($original_contents === false) { @ftp_delete($install_ftp_connection, $file); } else { $temp = tmpfile(); fwrite($temp, $original_contents); fseek($temp, 0); @ftp_fput($install_ftp_connection, $file, $temp, FTP_ASCII); fclose($temp); } } }
/** * Return the .htaccess code that can be used to hide index.php * */ static function Rewrite_Rules($hide_index = true, $home_root, $existing_contents = '') { if (!$existing_contents) { $existing_contents = ''; } // IIS if (self::IIS()) { return self::Rewrite_RulesIIS($hide_index, $existing_contents); } // Apache admin_permalinks::StripRules($existing_contents); if (!$hide_index) { return $existing_contents; } $home_root = rtrim($home_root, '/') . '/'; return $existing_contents . "\n\n" . '# BEGIN gpEasy <IfModule mod_rewrite.c> RewriteEngine On RewriteBase "' . $home_root . '" # Don\'t rewrite multiple times RewriteCond %{QUERY_STRING} gp_rewrite RewriteRule .* - [L] # Redirect away from requests with index.php RewriteRule index\\.php(.*) $1 [R=302,L] # Add gp_rewrite to root requests RewriteRule ^$ "' . $home_root . 'index.php?gp_rewrite" [qsa,L] # Don\'t rewrite for static files RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_URI} \\.(js|css|jpe?g|jpe|gif|png|ico)$ [NC] RewriteRule .* - [L] # Send all other requests to index.php # Append the gp_rewrite argument to tell gpEasy not to use index.php and to prevent multiple rewrites RewriteRule /?(.*) "' . $home_root . 'index.php?gp_rewrite=$1" [qsa,L] </IfModule> # END gpEasy'; }
/** * Save the htaccess rule to the server using $filesystem and test to make sure we aren't getting 500 errors * * @access public * @since 1.7 * * @param string $path The path to the local .htaccess file * @param string $rules The rules to be added to the .htaccess file * @return boolean */ function SaveRules($path, $rules) { global $gp_filesystem, $langmessage; //force a 500 error for testing //$rules .= "\n</IfModule>"; //get current .htaccess $contents = ''; $original_contents = false; if (file_exists($path)) { $original_contents = $contents = file_get_contents($path); } // new gpeasy rules admin_permalinks::StripRules($contents); $contents .= $rules; $filesystem_base = $gp_filesystem->get_base_dir(); if ($filesystem_base === false) { return false; } $filesystem_path = $filesystem_base . '/.htaccess'; if (!$gp_filesystem->put_contents($filesystem_path, $contents)) { return false; } //if TestResponse Fails, undo the changes //only need to test for hiding if ($this->changed_to_hide && !admin_permalinks::TestResponse()) { if ($original_contents === false) { $gp_filesystem->unlink($filesystem_path); } else { $gp_filesystem->put_contents($filesystem_path, $original_contents); } return false; } return true; }
function Options_htaccess($site) { global $langmessage; includeFile('admin/admin_permalinks.php'); if (!gpRemoteGet::Test()) { return; } $site_info = $this->siteData['sites'][$site]; $site_url = $site_info['url']; $site_uniq = false; if (isset($site_info['gpuniq'])) { $site_uniq = $site_info['gpuniq']; } $file_path = $site . '/.htaccess'; if (file_exists($file_path)) { $original_contents = $contents = file_get_contents($file_path); } if (!isset($_POST['hide_index'])) { $to_hide_index = false; unset($this->siteData['sites'][$site]['hide_index']); $prefix = ''; } else { if (empty($site_url)) { message('A valid site url is required to hide index.php'); return false; } $array = @parse_url($site_url); $prefix =& $array['path']; $to_hide_index = $this->siteData['sites'][$site]['hide_index'] = true; } //add the gpeasy rules admin_permalinks::StripRules($contents); $contents .= admin_permalinks::Rewrite_Rules($to_hide_index, $prefix, $site_uniq); if (!gpFiles::Save($file_path, $contents)) { message($langmessage['OOPS'] . ' (Couldn\'t save .htaccess)'); return false; } //check for valid response when hiding index.php if ($to_hide_index) { $check_url = $site_url . '/Special_Site_Map'; $result = gpRemoteGet::Get_Successful($check_url); if (!$result) { message('Did not recieve valid response when fetching url without index.php: ' . htmlspecialchars($check_url)); gpFiles::Save($file_path, $original_contents); return false; } } return true; }
/** * Generate rewrite rules for the apache server * */ public static function Rewrite_RulesApache($hide_index, $home_root, $contents, $www) { // Apache admin_permalinks::StripRules($contents); if (!$hide_index && is_null($www)) { return $contents; } $home_root = rtrim($home_root, '/') . '/'; $new_lines = array(); $server_name = gpsession::ServerName(); // with www if ($www) { $new_lines[] = '# with www'; $new_lines[] = 'RewriteCond %{HTTPS} off'; $new_lines[] = 'RewriteCond %{HTTP_HOST} "^' . $server_name . '"'; $new_lines[] = 'RewriteRule (.*) "http://www.' . $server_name . '/$1" [R=301,L]'; $new_lines[] = ''; $new_lines[] = '# with www and https'; $new_lines[] = 'RewriteCond %{HTTPS} on'; $new_lines[] = 'RewriteCond %{HTTP_HOST} "^' . $server_name . '"'; $new_lines[] = 'RewriteRule (.*) "https://www.' . $server_name . '/$1" [R=301,L]'; // without www } elseif ($www === false) { $new_lines[] = '# without www'; $new_lines[] = 'RewriteCond %{HTTPS} off'; $new_lines[] = 'RewriteCond %{HTTP_HOST} "^www.' . $server_name . '"'; $new_lines[] = 'RewriteRule (.*) "http://' . $server_name . '/$1" [R=301,L]'; $new_lines[] = ''; $new_lines[] = '# without www and https'; $new_lines[] = 'RewriteCond %{HTTPS} on'; $new_lines[] = 'RewriteCond %{HTTP_HOST} "^www.' . $server_name . '"'; $new_lines[] = 'RewriteRule (.*) "https://' . $server_name . '/$1" [R=301,L]'; } $new_lines[] = "\n"; // hide index.php if ($hide_index) { $new_lines[] = 'RewriteBase "' . $home_root . '"'; $new_lines[] = ''; $new_lines[] = '# Don\'t rewrite multiple times'; $new_lines[] = 'RewriteCond %{QUERY_STRING} gp_rewrite'; $new_lines[] = 'RewriteRule .* - [L]'; $new_lines[] = ''; $new_lines[] = '# Redirect away from requests with index.php'; $new_lines[] = 'RewriteRule index\\.php(.*) "' . rtrim($home_root, '/') . '$1" [R=302,L]'; $new_lines[] = ''; $new_lines[] = '# Add gp_rewrite to root requests'; $new_lines[] = 'RewriteRule ^$ "' . $home_root . 'index.php?gp_rewrite" [qsa,L]'; $new_lines[] = ''; $new_lines[] = '# Don\'t rewrite for static files'; $new_lines[] = 'RewriteCond %{REQUEST_FILENAME} -f [OR]'; $new_lines[] = 'RewriteCond %{REQUEST_FILENAME} -d [OR]'; $new_lines[] = 'RewriteCond %{REQUEST_URI} \\.(js|css|jpe?g|jpe|gif|png|ico)$ [NC]'; $new_lines[] = 'RewriteRule .* - [L]'; $new_lines[] = ''; $new_lines[] = '# Send all other requests to index.php'; $new_lines[] = '# Append the gp_rewrite argument to tell gpEasy not to use index.php and to prevent multiple rewrites'; $new_lines[] = 'RewriteRule /?(.*) "' . $home_root . 'index.php?gp_rewrite=$1" [qsa,L]'; $new_lines[] = ''; } return $contents . ' # BEGIN gpEasy <IfModule mod_rewrite.c> RewriteEngine On ' . implode("\n\t", $new_lines) . ' </IfModule> # END gpEasy'; }