/** * 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); } } }
/** * 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 */ function SaveRules() { global $gp_filesystem, $langmessage, $dirPrefix; //get current .htaccess $original_contents = false; if (file_exists($this->rule_file)) { $original_contents = file_get_contents($this->rule_file); } //add/remove gpEasy rules from $original_contents to get new $contents $contents = admin_permalinks::Rewrite_Rules($this->changed_to_hide, $dirPrefix, $original_contents); if ($contents === false) { return false; } $filesystem_base = $gp_filesystem->get_base_dir(); if ($filesystem_base === false) { return false; } $filesystem_path = $filesystem_base . '/' . $this->rule_file_name; 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; }
/** * Determine how to save the htaccess file to the server (ftp,direct,manual) and give user the appropriate options * * @return boolean true if the .htaccess file is saved */ function SaveHtaccess() { global $gp_filesystem, $config, $langmessage, $dirPrefix; if (isset($_POST['rewrite_setting']) && $_POST['rewrite_setting'] == 'hide_index') { $this->changed_to_hide = true; } $rules = admin_permalinks::Rewrite_Rules($this->changed_to_hide, $dirPrefix, $config['gpuniq']); //only proceed with hide if we can test the results if (gpRemoteGet::Test()) { if ($gp_filesystem->ConnectOrPrompt('Admin_Permalinks')) { if ($this->SaveRules($this->htaccess_file, $rules)) { message($langmessage['SAVED']); $_SERVER['gp_rewrite'] = $this->changed_to_hide; common::SetLinkPrefix(); echo '<form method="GET" action="' . common::GetUrl('Admin_Permalinks') . '">'; echo '<input type="submit" value="' . $langmessage['continue'] . '" class="gpsubmit" />'; echo '</form>'; return true; } message($langmessage['OOPS']); $gp_filesystem->CompleteForm($_POST, 'Admin_Permalinks'); } } echo '<h3>' . $langmessage['manual_method'] . '</h3>'; echo '<p>'; echo $langmessage['manual_htaccess']; echo '</p>'; echo '<textarea cols="60" rows="7" readonly="readonly" onClick="this.focus();this.select();" class="gptextarea">'; echo htmlspecialchars($rules); echo '</textarea>'; return false; }
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; }
/** * Determine how to save the htaccess file to the server (ftp,direct,manual) and give user the appropriate options * * @return boolean true if the .htaccess file is saved */ public function SaveHtaccess() { global $langmessage, $dirPrefix; //hide index ? if (isset($_POST['rewrite_setting']) && $_POST['rewrite_setting'] == 'hide_index') { $this->hide_index = true; $this->undo_if_failed = true; } // www preference $www = null; if (isset($_POST['www_setting'])) { if ($_POST['www_setting'] === 'with') { $www = true; $this->undo_if_failed = true; } elseif ($_POST['www_setting'] === 'without') { $www = false; $this->undo_if_failed = true; } } $this->new_rules = admin_permalinks::Rewrite_Rules($this->hide_index, $dirPrefix, $this->orig_rules, $www); // only proceed with hide if we can test the results if (!$this->CanTestRules()) { $this->ManualMethod(); return false; } if (!$this->SaveRules()) { $this->FileSystem->CompleteForm($_POST, 'Admin_Permalinks'); $this->ManualMethod(); return false; } msg($langmessage['SAVED']); //redirect to new permalink structure $_SERVER['gp_rewrite'] = $this->hide_index; common::SetLinkPrefix(); $redir = common::GetUrl('Admin_Permalinks'); common::Redirect($redir, 302); return false; }