1<?php 2/** 3 * Access Counter and Popularity Plugin -- Data Transferer 4 * Transfers this plugin's data to new places (cache directory and meta directory) 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author HokkaidoPerson <dosankomali@yahoo.co.jp> 8 */ 9 10if(!defined('DOKU_INC')) die(); 11 12 13class action_plugin_accscounter extends DokuWiki_Action_Plugin { 14 15 public function register(Doku_Event_Handler $controller) { 16 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'allow_my_action'); 17 $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'my_action'); 18 } 19 20 public function allow_my_action(Doku_Event $event, $param) { 21 global $INFO; 22 if($event->data != 'accscounter_datatransfer') { 23 if (auth_ismanager() and file_exists(DOKU_PLUGIN . 'accscounter/log/')) msg($this->getLang('datanotice'), 2); 24 if (auth_ismanager() and file_exists(metaFN($INFO['id'], '.accscounternm'))) msg($this->getLang('datanotice2'), 2); 25 return; 26 } 27 $event->preventDefault(); 28 } 29 30 public function my_action(Doku_Event $event, $param) { 31 if($event->data != 'accscounter_datatransfer') return; 32 $event->preventDefault(); 33 global $conf; 34 $achelper = plugin_load('helper','accscounter'); 35 36 if (!auth_ismanager()) { 37 echo 'Permission Denied'; 38 return; 39 } 40 41 if ($_GET['mode'] == 'move') { 42 $old = DOKU_PLUGIN . 'accscounter/log/iplogs/'; 43 $new = $conf['cachedir'] . '/accscounterlog/'; 44 $moved = FALSE; 45 46 if (file_exists($old)) { 47 $moved = TRUE; 48 echo $this->getLang('moveiplog') . ': '; 49 if ($this->dir_copy($old, $new)) echo $this->getLang('success') . '<br><br>'; else echo $this->getLang('failiplog') . '<br><br>'; 50 } 51 52 echo $this->getLang('movenmlog') . ':<br>'; 53 $aryret = array(); 54 $dir = DOKU_PLUGIN . 'accscounter/log/'; 55 $ext = '.count'; 56 $pattern = '/^([-_.a-zA-Z0-9%]+)' . preg_quote($ext, '/') . '$/'; 57 $dir2 = $conf['metadir']; 58 $ext2 = '.accscounternm'; 59 $pattern2 = '/^([-_.a-zA-Z0-9%]+)' . preg_quote($ext2, '/') . '$/'; 60 if (file_exists($dir)) { 61 $dp = @opendir($dir); 62 if ($dp) { 63 $matches = array(); 64 while (($file = readdir($dp)) !== FALSE) { 65 if (preg_match($pattern, $file, $matches)) { 66 $aryret[$file] = urldecode($matches[1]); 67 } 68 } 69 closedir($dp); 70 71 foreach ($aryret as $file=>$page) { 72 echo $page . ' → '; 73 $data = file_get_contents($dir . $file); 74 if ($data === FALSE) { 75 echo $this->getLang('failnmload') . '<br>'; 76 continue; 77 } 78 if (!io_saveFile($achelper->counterFN($page, '.number'), $data)) { 79 echo $this->getLang('failnmsave') . '<br>'; 80 continue; 81 } 82 if (!unlink($dir . $file)) { 83 echo $this->getLang('failnmdel') . '<br>'; 84 continue; 85 } 86 echo $this->getLang('success') . '<br>'; 87 } 88 unlink($dir . '.htaccess'); 89 if (rmdir($dir)) echo '<br>' . $this->getLang('complete') . '<br><br>'; else echo '<br>' . $this->getLang('remaining') . '<br><br>'; 90 91 } else echo $this->getLang('failnmdir') . '<br><br>'; 92 } 93 94 $metadir = $this->getFiles($conf['metadir']); 95 $length = utf8_strlen($conf['metadir'] . '/'); 96 foreach ($metadir as $filename) { 97 $islogfile = utf8_substr($filename, $length); 98 $islogfile = utf8_decodeFN($islogfile); 99 $islogfile = str_replace('/', ':', $islogfile); 100 if (utf8_substr($islogfile, mb_strrpos($islogfile, '.') + 1) != 'accscounternm') continue; 101 $moved = TRUE; 102 $page = utf8_substr($islogfile, 0, utf8_strlen($islogfile) - utf8_strlen('.accscounternm')); 103 echo $page . ' → '; 104 $data = file_get_contents($filename); 105 if ($data === FALSE) { 106 echo $this->getLang('failnmload2') . '<br>'; 107 continue; 108 } 109 if (!io_saveFile($achelper->counterFN($page, '.number'), $data)) { 110 echo $this->getLang('failnmsave') . '<br>'; 111 continue; 112 } 113 if (!unlink($filename)) { 114 echo $this->getLang('failnmdel2') . '<br>'; 115 continue; 116 } 117 echo $this->getLang('success') . '<br>'; 118 } 119 if ($moved) echo $this->getLang('allloaded') . '<br><br>'; else echo $this->getLang('nothing') . '<br><br>'; 120 echo $this->getLang('funcend'); 121 return; 122 } 123 124 if ($_GET['mode'] == 'delete') { 125 $deleted = FALSE; 126 127 if (file_exists(DOKU_PLUGIN . 'accscounter/log/')) { 128 $deleted = TRUE; 129 if ($this->delTree(DOKU_PLUGIN . 'accscounter/log/')) echo $this->getLang('successdel') . '<br>'; else echo $this->getLang('faildel') . '<br>'; 130 } 131 132 $metadir = $this->getFiles($conf['metadir']); 133 $length = utf8_strlen($conf['metadir'] . '/'); 134 foreach ($metadir as $filename) { 135 $islogfile = utf8_substr($filename, $length); 136 $show = utf8_decodeFN($islogfile); 137 if (utf8_substr($islogfile, mb_strrpos($islogfile, '.') + 1) != 'accscounternm') continue; 138 $deleted = TRUE; 139 echo $show . ' → '; 140 if (!unlink($filename)) { 141 echo $this->getLang('faildellog') . '<br>'; 142 continue; 143 } 144 echo $this->getLang('successdellog') . '<br>'; 145 } 146 if ($deleted) echo $this->getLang('alldeleted') . '<br><br>'; else echo $this->getLang('nothingtodelete') . '<br><br>'; 147 148 echo '<br>' . $this->getLang('funcend'); 149 return; 150 } 151 152 echo $this->locale_xhtml('movedirection'); 153 return; 154 } 155 156 protected function delTree($dir) { 157 $files = array_diff(scandir($dir), array('.','..')); 158 foreach ($files as $file) { 159 (is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file"); 160 } 161 return rmdir($dir); 162 } 163 164 protected function dir_copy($dir_name, $new_dir) { 165 if (!is_dir($new_dir)) { 166 mkdir($new_dir); 167 } 168 169 if (is_dir($dir_name)) { 170 if ($dh = opendir($dir_name)) { 171 while (($file = readdir($dh)) !== false) { 172 if ($file == "." || $file == "..") { 173 continue; 174 } 175 if (is_dir($dir_name . "/" . $file)) { 176 $this->dir_copy($dir_name . "/" . $file, $new_dir . "/" . $file); 177 rmdir($dir_name . "/" . $file); 178 } 179 else { 180 copy($dir_name . "/" . $file, $new_dir . "/" . $file); 181 unlink($dir_name . "/" . $file); 182 } 183 } 184 closedir($dh); 185 if (!rmdir($dir_name)) return false; 186 } 187 } 188 return true; 189 } 190 191 protected function getFiles($path) { 192 $result = array(); 193 194 foreach(glob($path . "/*") as $file) { 195 if (is_dir($file)) { 196 $result = array_merge($result, $this->getFiles($file)); 197 } 198 199 $result[] = $file; 200 } 201 202 return $result; 203 } 204 205} 206