1<?php 2/** 3 * Redirect2 - DokuWiki Redirect Manager 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Satoshi Sahara <sahara.satoshi@gmail.com> 7 */ 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 12require_once(DOKU_PLUGIN.'admin.php'); 13 14class admin_plugin_redirect2 extends DokuWiki_Admin_Plugin { 15 16 protected $LogFile; 17 18 function __construct() { 19 global $conf; 20 $this->LogFile = $conf['cachedir'].'/redirection.log'; 21 } 22 23 /** 24 * Access for managers allowed 25 */ 26 function forAdminOnly(){ return false; } 27 28 /** 29 * return sort order for position in admin menu 30 */ 31 function getMenuSort() { return 140; } 32 33 /** 34 * return prompt for admin menu 35 */ 36 function getMenuText($language) { return $this->getLang('name'); } 37 38 /** 39 * handle user request 40 */ 41 function handle() { 42 global $INPUT; 43 $map = plugin_load('helper', $this->getPluginName()); 44 45 if ($INPUT->post->str('redirdata') && checkSecurityToken()) { 46 if (io_saveFile($map->ConfFile, cleanText($INPUT->str('redirdata')))) { 47 msg($this->getLang('saved'), 1); 48 } 49 } 50 } 51 52 /** 53 * output appropriate html 54 */ 55 function html() { 56 global $lang; 57 $map = plugin_load('helper', $this->getPluginName()); 58 59 echo $this->locale_xhtml('intro'); 60 echo '<form action="" method="post" >'; 61 echo '<input type="hidden" name="do" value="admin" />'; 62 echo '<input type="hidden" name="page" value="'.$this->getPluginName().'" />'; 63 echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'; 64 echo '<textarea class="edit" rows="15" cols="80" style="height: 300px" name="redirdata">'; 65 echo formtext(io_readFile($map->ConfFile)); 66 echo '</textarea><br />'; 67 echo '<input type="submit" value="'.$lang['btn_save'].'" class="button" />'; 68 echo '<br />'; 69 echo '</form>'; 70 71 if (!$this->getConf('logging')) return; 72 $logData = $this->getLogData(); 73 74 echo '<br />'; 75 echo $this->locale_xhtml('loginfo'); 76 echo '<table class="'.$this->getPluginName().'">'; 77 echo '<thead><tr>'; 78 echo '<th>Count</th>'; 79 echo '<th>Status</th>'; 80 echo '<th>Page/Media</th>'; 81 echo '<th>Redirect to | Referer</th>'; 82 echo '<th>Recent redirection</th>'; 83 echo '</tr></thead>'; 84 echo '<tbody>'; 85 foreach ($logData as $id => $data) { 86 echo '<tr>'; 87 echo '<td>'.$data['count'].'</td>'; 88 echo '<td>'.$data['status'].'</td>'; 89 echo '<td>'.$this->html_atag($data['caller'], $id).'</td>'; 90 echo '<td>'.$this->html_atag($data['caller'], $data['dest']).'</td>'; 91 echo '<td>'.$data['last'].'</td>'; 92 echo '</tr>'; 93 } 94 echo '</tbody>'; 95 echo '</table>'; 96 97 } 98 99 100 /** 101 * Load redierction data from log file 102 */ 103 protected function getLogData() { 104 $logData = array(); 105 if (!file_exists($this->LogFile)) return $logData; 106 107 $logfile = new SplFileObject($this->LogFile); 108 $logfile->setFlags(SplFileObject::READ_CSV); 109 $logfile->setCsvControl("\t"); // tsv 110 111 foreach ($logfile as $line) { 112 if ($line[0] == NULL) continue; 113 list($datetime, $caller, $status, $orig, $dest) = $line; 114 if (!isset($logData[$orig])) { 115 $logData[$orig] = array( 116 'count' => 1, 117 'caller' => $caller, 118 'status' => $status, 119 'dest' => $dest, 120 'last' => $datetime, 121 ); 122 } else { 123 $logData[$orig]['count'] ++; 124 if ($datetime > $logData[$orig]['last']) { 125 $logData[$orig]['last'] = $datetime; 126 } 127 } 128 } 129 unset($logfile); 130 uasort($logData, array($this, 'compareCounts')); 131 return $logData; 132 } 133 134 protected function compareCounts($a, $b) { 135 return $b['count'] - $a['count']; 136 } 137 138 private function html_atag($caller, $id) { 139 140 if (preg_match('@^(https?://|/)@', $id)) { 141 $linkType = 'external'; 142 } elseif ($caller == 'redirectMedia') { 143 $linkType = 'media'; 144 } else { 145 $linkType = 'page'; 146 } 147 $format = 'xhtml'; 148 switch ($linkType) { 149 case 'media': 150 $link = '{{:'.$id.'?linkonly|'.$id.'}}'; 151 $html = strip_tags(p_render($format, p_get_instructions($link), $info), '<a>'); 152 break; 153 case 'page': 154 $link = '[[:'.$id.'|'.$id.']]'; 155 $html = strip_tags(p_render($format, p_get_instructions($link), $info), '<a>'); 156 break; 157 default: 158 $html = hsc($id); 159 } 160 return $html; 161 } 162 163} 164//Setup VIM: ex: et ts=4 enc=utf-8 :