1<?php 2/** 3 * Wiki farm manager action plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Etienne MELEARD <etienne.meleard@cru.fr> 7 * @desc Restrict animal usage and display messages depending on animal state. 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/'); 15 16if(!defined('DOKU_FARM_WWW')) define('DOKU_FARM_WWW', 'lib/plugins/farm/'); 17 18if(!defined('DOKU_FARMPLUGINACTIONHANDLER')) define('DOKU_FARMPLUGINACTIONHANDLER', true); 19 20require_once(DOKU_PLUGIN.'action.php'); 21require_once(DOKU_FARM_PLUGIN.'animal.class.php'); 22 23class action_plugin_farm extends DokuWiki_Action_Plugin { 24 private $animal = null; 25 26 /** 27 * Returns infos about the plugin 28 */ 29 function getInfo(){ 30 return array( 31 'author' => 'Etienne Meleard', 32 'email' => 'etienne.meleard@cru.fr', 33 'date' => '2009-12-16', 34 'name' => 'Farm Manager Action Plugin', 35 'desc' => 'Manage Wiki farm'.($this->disabled ? ' (disabled)' : ''), 36 'url' => 'http://wiki.splitbrain.org/plugin:farm', 37 ); 38 } 39 40 /** 41 * Register plugin hooks 42 */ 43 function register(&$controller) { 44 if(!defined('DOKU_IS_FARMER')) return; // don't register if farm not installed 45 46 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'cleanACT'); 47 $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'displayState'); 48 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'processHeaders'); 49 } 50 51 /** 52 * Clean $ACT depending on lockstate 53 * @param &$event dokuwiki event object 54 * @param $args handler arguments 55 */ 56 function cleanACT(&$event, $args) { 57 switch($this->getLockState()) { 58 case 'all' : 59 if( 60 $event->data == 'register' || 61 $event->data == 'login' || 62 $event->data == 'profile' 63 ) $event->data = 'show'; 64 case 'admin' : 65 if($event->data == 'admin') $event->data = 'show'; 66 case 'edits' : 67 if( 68 $event->data == 'edit' || 69 $event->data == 'preview' || 70 $event->data == 'save' || 71 $event->data == 'draft' || 72 $event->data == 'recover' || 73 $event->data == 'draftdel' || 74 $event->data == 'conflict' 75 ) $event->data = 'show'; 76 } 77 } 78 79 /** 80 * Display messages about lockstate / overflow at top of the page 81 * @param &$event dokuwiki event object 82 * @param $args handler arguments 83 */ 84 function displayState(&$event, $args) { 85 $animal = new dokuwiki_farm_animal(DOKU_FARM_ANIMAL, null); 86 87 $farmconf = dokuwiki_farm_animal::getConf(); 88 89 $toobig = false; 90 if((int)$farmconf['animalmaxsize'] > 0) { 91 if($animal->getInfos()->getSize() >= (int)$farmconf['animalmaxsize']) $toobig = true; 92 } 93 94 $farmtoobig = false; 95 if((int)$farmconf['farmmaxsize'] > 0) { 96 if(dokuwiki_farm_animal::farmSize() >= (int)$farmconf['farmmaxsize']) $farmtoobig = true; 97 } 98 99 $ls = $this->getLockState(); 100 101 if(isset($_SERVER['REMOTE_USER']) && !empty($_SERVER['REMOTE_USER'])) if($ls == 'edits' || $ls == 'admin' || $ls == 'all') echo '<div class="info">'.$this->getLang('animal_lockstate_'.$ls.'_message').'</div>'; 102 103 if(isset($_SERVER['REMOTE_USER']) && !empty($_SERVER['REMOTE_USER'])) if($toobig) echo '<div class="error">'.$this->getLang('animal_toobig_message').'</div>'; 104 //if($farmtoobig) echo '<div class="error">'.$this->getLang('animal_farmtoobig_message').'</div>'; 105 } 106 107 /** 108 * Process headers to change paths 109 */ 110 function processHeaders(&$event, $args) { 111 $animal = new dokuwiki_farm_animal(DOKU_FARM_ANIMAL, null); 112 113 $farmconf = dokuwiki_farm_animal::getConf(); 114 115 if($farmconf['virtual'] && ($_SERVER['HTTP_HOST'] != $farmconf['farmerhost'])) { 116 for($i=0; $i<count($event->data['link']); $i++) { 117 if(!isset($event->data['link'][$i]['href'])) continue; 118 $event->data['link'][$i]['href'] = preg_replace('`^/?'.$animal->getName().'`', '', $event->data['link'][$i]['href']); 119 } 120 121 for($i=0; $i<count($event->data['script']); $i++) { 122 if(!isset($event->data['script'][$i]['src'])) continue; 123 $event->data['script'][$i]['src'] = preg_replace('`^/?'.$animal->getName().'`', '', $event->data['script'][$i]['src']); 124 } 125 }elseif(!$farmconf['userewrite']) { 126 for($i=0; $i<count($event->data['link']); $i++) { 127 if(!isset($event->data['link'][$i]['href'])) continue; 128 $event->data['link'][$i]['href'] = $farmconf['farmwebroot'].$farmconf['farmer'].substr($event->data['link'][$i]['href'], strlen(DOKU_BASE)); 129 if(!DOKU_IS_FARMER) $event->data['link'][$i]['href'] .= (strrpos($event->data['link'][$i]['href'], '?') !== false ? '&' : '?').'animal='.$animal->getName(); 130 } 131 132 for($i=0; $i<count($event->data['script']); $i++) { 133 if(!isset($event->data['script'][$i]['src'])) continue; 134 $event->data['script'][$i]['src'] = $farmconf['farmwebroot'].$farmconf['farmer'].substr($event->data['script'][$i]['src'], strlen(DOKU_BASE)); 135 if(!DOKU_IS_FARMER) $event->data['script'][$i]['src'] .= (strrpos($event->data['script'][$i]['src'], '?') !== false ? '&' : '?').'animal='.$animal->getName(); 136 } 137 } 138 } 139 140 /** 141 * Retreive lockstate from animal meta file 142 * @return string or false if no metafile entry 143 */ 144 function getLockState() { 145 foreach(explode("\n", @file_get_contents(DOKU_CONF.'../animal.meta')) as $l) { 146 list($k, $v) = preg_split('`\s+:\s+`', $l, 2); 147 if(trim($k) == 'lockstate') return trim($v); 148 } 149 return false; 150 } 151 152 /** 153 * Localize a string 154 * Customize your lang strings by puting them in <farm_plugin_dir>/customlang/<lang_code>/lang.php 155 * @param $code lang string identifier 156 * @param $data string or array used to replace %s occurences in the localized string 157 * @return localized string 158 */ 159 function getLang($code, $data = null) { 160 global $conf; 161 $l = isset($conf['lang']) ? $conf['lang'] : 'en'; 162 $lang = array(); 163 164 @include DOKU_FARM_PLUGIN.'lang/en/lang.php'; 165 if($l != 'en') @include DOKU_FARM_PLUGIN.'lang/'.$l.'/lang.php'; 166 @include DOKU_FARM_PLUGIN.'customlang/en/lang.php'; 167 if($l != 'en') @include DOKU_FARM_PLUGIN.'customlang/'.$l.'/lang.php'; 168 169 if(!isset($lang[$code])) return '{'.strtoupper($code).'}'; 170 $str = $lang[$code]; 171 if(!is_null($data)) if(!is_array($data)) $data = array($data); 172 if(is_array($data)) foreach($data as $d) $str = preg_replace('`%s`', $d, $str, 1); 173 return $str; 174 } 175} 176