page = $args['page']; $this->codes = array('301'=>$langmessage['301 Moved Permanently'],'302'=>$langmessage['302 Moved Temporarily']); \gp\tool\Editing::PrepAutoComplete(); $this->cmds['Save404'] = 'Edit404'; $this->cmds['Edit404'] = ''; $this->cmds['EditRedir'] = ''; $this->cmds['UpdateRedir'] = 'DefaultDisplay'; $this->cmds['SaveRedir'] = 'DefaultDisplay'; $this->cmds['RmRedir'] = 'DefaultDisplay'; } /** * Add instructions for a 301 or 302 redirect * */ public static function AddRedirect($source,$target){ global $dataDir; $datafile = $dataDir.'/data/_site/error_data.php'; $error_data = \gp\tool\Files::Get('_site/error_data'); if( !$error_data ){ $error_data = array(); } $changed = false; //remove redirects from the $target if( isset($error_data['redirects'][$target]) ){ unset($error_data['redirects'][$target]); $changed = true; } //redirect already exists for $source if( !isset($error_data['redirects'][$source]) ){ $error_data['redirects'][$source]['target'] = $target; $error_data['redirects'][$source]['code'] = '301'; $changed = true; } if( $changed ){ \gp\tool\Files::SaveData($datafile,'error_data',$error_data); } } protected function SaveMissingData(){ global $langmessage; if( !\gp\tool\Files::SaveData($this->datafile,'error_data',$this->error_data) ){ message($langmessage['OOPS']); return false; } message($langmessage['SAVED']); return true; } protected function GetCodeLanguage($code){ global $langmessage; switch($code){ case '301': return $langmessage['301 Moved Permanently']; case '302': return $langmessage['302 Moved Temporarily']; } return ''; } /** * Show 404 info and Redirection list * */ public function DefaultDisplay(){ global $langmessage; echo '

'.$langmessage['Link Errors'].'

'; echo '

'.$langmessage['404_Usage'].'

'; //404 Page echo '

'.$langmessage['404_Page'].'

'; echo '
'; echo '

'.$langmessage['About_404_Page'].'

'; echo '

'; echo \gp\tool::Link('Special_Missing',$langmessage['preview'],'','class="gpsubmit"'); echo '   '; echo \gp\tool::Link('Admin/Missing',$langmessage['edit'],'cmd=Edit404','class="gpsubmit"'); echo '

'; echo '
'; //redirection echo '

'.$langmessage['Redirection'].'

'; echo '
'; $this->ShowRedirection(); echo '
'; } protected function Save404(){ $text =& $_POST['gpcontent']; \gp\tool\Files::cleanText($text); $this->error_data['404_TEXT'] = $text; if( $this->SaveMissingData() ){ return true; } $this->Edit404($text); return false; } /** * Display form for editing the 404 page content * */ protected function Edit404($text=null){ global $langmessage; if( is_null($text) ){ if( isset($this->error_data['404_TEXT']) ){ $text = $this->error_data['404_TEXT']; }else{ $text = self::DefaultContent(); } } echo '

'; echo \gp\tool::Link('Admin/Missing',$langmessage['Link Errors']); echo ' » '.$langmessage['404_Page'].'

'; echo '
'; echo ''; \gp\tool\Editing::UseCK($text); echo ''; echo ' '; echo '
'; echo '
'; echo ''; echo ''; echo ''; echo '
'; echo $langmessage['Useful Variables']; echo '
'; echo '{{Similar_Titles}}'; echo ''; echo $langmessage['Similar_Titles']; echo '
'; } /** * Display current redirection settings * */ protected function ShowRedirection(){ global $langmessage, $gp_index, $config; $this->page->head_js[] = '/include/thirdparty/tablesorter/tablesorter.js'; $this->page->jQueryCode .= '$("table.tablesorter").tablesorter({cssHeader:"gp_header",cssAsc:"gp_header_asc",cssDesc:"gp_header_desc"});'; $has_invalid_target = false; echo '

'; echo $langmessage['About_Redirection']; echo '

'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; foreach($this->error_data['redirects'] as $source => $data){ echo ''; } echo ''; $this->AddMissingRow(); echo '
'; echo $langmessage['Source URL']; echo ''; echo $langmessage['Target URL']; echo ''; echo $langmessage['Similarity']; echo ''; echo $langmessage['Method']; echo ''; echo $langmessage['options']; echo '
'; $raw_source = $source; if( !empty($data['raw_source']) ){ $raw_source = $data['raw_source']; } echo \gp\tool::GetUrl(''); echo htmlspecialchars($raw_source); echo ''; $target_show = $data['target']; if( strlen($target_show) > 40 ){ $target_show = substr($target_show,0,15).' ... '.substr($target_show,-15); } $full_target = $this->GetTarget($data['target'],false); $is_gplink = $this->isGPLink($data['target']); $valid_target = $this->ValidTarget($data['target']); if( !$valid_target ){ $has_invalid_target = true; echo '   '; } echo ''.str_replace(' ',' ',htmlspecialchars($target_show)).''; echo ''; if( $is_gplink ){ $lower_source = strtolower($raw_source); $lower_target = strtolower($target_show); similar_text($lower_source,$lower_target,$percent); echo number_format($percent,1).'%'; } echo ' '; echo $this->GetCodeLanguage($data['code']); echo ''; echo \gp\tool::Link('Admin/Missing',$langmessage['edit'],'cmd=EditRedir&source='.urlencode($source),array('data-cmd'=>'gpabox')); echo '   '; echo \gp\tool::Link($source,$langmessage['Test']); echo '   '; $title = sprintf($langmessage['generic_delete_confirm'],$source); echo \gp\tool::Link('Admin/Missing',$langmessage['delete'],'cmd=RmRedir&link='.urlencode($source),array('data-cmd'=>'postlink','title'=>$title,'class'=>'gpconfirm')); echo '
'; echo '
'; echo '
'; if( $has_invalid_target ){ echo '

'; echo '     '; echo $langmessage['Target URL Invalid']; echo ''; echo '

'; } } /** * Return true if the target is a valid url * * @return bool */ public function ValidTarget($target){ global $gp_index; if( empty($target) ){ return true; } if( !$this->isGPLink($target) ){ return true; } if( isset($gp_index[$target]) ){ return true; } $type = \gp\tool::SpecialOrAdmin($target); if( $type == 'admin' ){ return true; } return false; } /** * Add Redirection form for * */ protected function AddMissingRow(){ global $langmessage; $_REQUEST += array('source'=>'','target'=>'','code'=>'','orig_source'=>''); //source echo ''; echo ''; echo \gp\tool::GetUrl(''); echo ''; //target echo ''; echo ''; //code echo ''; echo ''; $this->CodeSelect($_REQUEST['code']); echo ''; echo ''; echo ''; echo ''; } /** * Edit an existing redirection * */ protected function EditRedir(){ global $langmessage; $source = \gp\admin\Tools::PostedSlug( $_REQUEST['source'] ); if( !isset($this->error_data['redirects'][$source]) ){ message($langmessage['OOPS'].' (Invalid Redirect)'); return false; } $args = $this->error_data['redirects'][$source]; $args['cmd'] = 'updateredir'; $args['orig_source'] = $source; $args['source'] = $source; $this->RedirForm($args); } /** * Using inline_box for this one for autocomplete init * */ protected function RedirForm($values=array()){ global $langmessage, $gp_index; $values += array('cmd'=>'saveredir','source'=>'','target'=>'','code'=>'','orig_source'=>''); echo '
'; echo '

'.$langmessage['New Redirection'].'

'; echo '
'; echo ''; echo ''; echo ''; echo ''; //source url echo ''; //method echo ''; //target url echo ''; echo '
'.$langmessage['options'].'
'; echo $langmessage['Source URL']; echo ''; echo \gp\tool::GetUrl(''); echo ''; echo '
'; echo $langmessage['Method']; echo ''; $this->CodeSelect($values['code']); echo '
'; echo $langmessage['Target URL']; echo ''; echo ''; echo '
'; echo '

'; echo ''; echo ' '; echo '

'; echo '
'; echo '
'; } /** * Display select for redirect code * */ protected function CodeSelect($value){ echo ''; } protected function CheckRedir(){ global $langmessage; if( empty($_POST['source']) ){ message($langmessage['OOPS'].' (Empty Source)'); return false; } if( $_POST['source'] == $_POST['target'] ){ message($langmessage['OOPS'].' (Infinite Loop)'); return false; } if( \gp\admin\Tools::PostedSlug($_POST['source']) == \gp\admin\Tools::PostedSlug($_POST['target']) ){ message($langmessage['OOPS'].' (Infinite Loop)'); return false; } if( $_POST['code'] != '302' ){ $_POST['code'] = 301; } return true; } /** * Update the settings for an existing redirection * */ protected function UpdateRedir(){ global $langmessage; if( !$this->CheckRedir() ){ return false; } $orig_source = $_POST['orig_source']; $source = \gp\admin\Tools::PostedSlug( $orig_source ); if( !isset($this->error_data['redirects'][$orig_source]) ){ message($langmessage['OOPS'].' (Entry not found)'); return false; } $data = array(); $data['target'] = $_POST['target']; $data['code'] = $_POST['code']; $data['raw_source'] = $_POST['source']; if( !\gp\tool\Files::ArrayReplace($orig_source,$source,$data,$this->error_data['redirects']) ){ message($langmessage['OOPS']); return false; } return $this->SaveMissingData(); } /** * Save a new redirection * */ protected function SaveRedir(){ global $langmessage, $gp_index; if( !$this->CheckRedir() ){ return false; } $source = \gp\admin\Tools::PostedSlug( $_POST['source'] ); if( isset($this->error_data['redirects'][$source]) ){ message($langmessage['OOPS'].' (Redirect Already Set)'); return false; } $redirect = array( 'target' => $_POST['target'], 'code' => (int)$_POST['code'], 'source' => $_POST['source'], ); $this->error_data['redirects'][$source] = $redirect; return $this->SaveMissingData(); } /** * Remove a redirection * */ protected function RmRedir(){ global $langmessage; $link =& $_POST['link']; if( !isset($this->error_data['redirects'][$link]) ){ message($langmessage['OOPS']); return false; } unset($this->error_data['redirects'][$link]); return $this->SaveMissingData(); } }