server_name = \gp\tool::ServerName(true); //get current rules $this->rule_file_name = self::IIS() ? 'web.config' : '.htaccess'; $this->rule_file = $dataDir.'/'.$this->rule_file_name; if( file_exists($this->rule_file) ){ $this->orig_rules = file_get_contents($this->rule_file); } $this->FileSystem = \gp\tool\FileSystem::init($this->rule_file); $this->WWWAvail(); echo '
'; echo $langmessage['limited_mod_rewrite']; echo '
'; } echo ''; echo str_replace('.htaccess',$this->rule_file_name,$langmessage['manual_htaccess']); echo '
'; //display rewrite code in textarea $lines = explode("\n",$this->new_rules); $len = 70; foreach($lines as $line){ $line_len = strlen($line)+(substr_count($line,"\t")*3); $len = max($len,$line_len); } $len = min(140,$len); echo ''; echo ''; } /** * 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 * * @return boolean */ public function SaveRules(){ global $langmessage, $dirPrefix; if( $this->new_rules === false ){ return false; } $filesystem_base = $this->FileSystem->get_base_dir(); if( $filesystem_base === false ){ return false; } $filesystem_path = $filesystem_base.'/'.$this->rule_file_name; if( !$this->FileSystem->put_contents($filesystem_path,$this->new_rules) ){ return false; } return $this->TestSave($filesystem_path); } /** * Make sure the save hasn't broken the installation * if TestResponse Fails, undo the changes * */ public function TestSave($filesystem_path){ //only need to test if we might needt to undo if( !$this->undo_if_failed ){ return true; } if( !self::TestResponse($this->hide_index) ){ if( is_null($this->orig_rules) ){ $this->FileSystem->unlink($filesystem_path); }else{ $this->FileSystem->put_contents($filesystem_path,$this->orig_rules); } return false; } return true; } /** * Try to fetch a response using RemoteGet to see if we're getting a 500 error * * @access public * @static * @since 1.7 * * @return boolean */ public static function TestResponse($new_rewrite = true){ //get url, force gp_rewrite to $new_gp_rewrite $rewrite_before = $_SERVER['gp_rewrite']; $_SERVER['gp_rewrite'] = $new_rewrite; \gp\tool::SetLinkPrefix(); //without server name, we can't get a valid absoluteUrl if( \gp\tool::ServerName() === false ){ return false; } $abs_url = \gp\tool::AbsoluteUrl('Site_Map','',true,false); //can't be special_site_map, otherwise \gp\tool::IndexToTitle() will be called during install $_SERVER['gp_rewrite'] = $rewrite_before; \gp\tool::SetLinkPrefix(); return self::ConfirmGet($abs_url, false); } /** * Strip rules enclosed by comments * * @access public * @static * @since 1.7 * * @param string $contents .htaccess file contents */ public static function StripRules(&$contents){ //strip code $pos = strpos($contents,'# BEGIN Typesetter'); if( $pos === false ){ return; } $end_comment = '# END Typesetter'; $pos2 = strpos($contents,$end_comment); if( $pos2 > $pos ){ $contents = substr_replace($contents,'',$pos,$pos2-$pos+strlen($end_comment)); }else{ $contents = substr($contents,0,$pos); } $contents = rtrim($contents); } /** * Return the .htaccess code that can be used to hide index.php * add/remove cms rules from $original_contents to get new $contents * */ public static function Rewrite_Rules( $hide_index = true, $home_root, $existing_contents = null, $www = null ){ if( is_null($existing_contents) ){ $existing_contents = ''; } // IIS if( self::IIS() ){ return self::Rewrite_RulesIIS( $hide_index, $existing_contents ); } return self::Rewrite_RulesApache($hide_index, $home_root, $existing_contents, $www); } /** * Generate rewrite rules for the apache server * */ public static function Rewrite_RulesApache( $hide_index, $home_root, $contents, $www ){ // Apache self::StripRules($contents); if( !$hide_index && is_null($www) ){ return $contents; } $home_root = rtrim($home_root,'/').'/'; $new_lines = array(); $server_name = \gp\tool::ServerName(true); // 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 cms 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 Typesetter