| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2007 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | function wiki_pagename() |
| 23 | { |
| 24 | if (!Get::v('n')) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | $words = explode('/', trim(Get::v('n'), '/')); |
| 29 | if (count($words) == 2) { |
| 30 | return join('.', $words); |
| 31 | } |
| 32 | |
| 33 | array_unshift($words, $words[0]); |
| 34 | $b = array_pop($words); |
| 35 | $a = array_pop($words); |
| 36 | |
| 37 | pl_redirect($a.'/'.$b); |
| 38 | } |
| 39 | |
| 40 | function wiki_filename($s) |
| 41 | { |
| 42 | if (@iconv('utf-8', 'utf-8', $s) == $s) { |
| 43 | return $s; |
| 44 | } |
| 45 | return utf8_encode($s); |
| 46 | } |
| 47 | |
| 48 | function wiki_work_dir() |
| 49 | { |
| 50 | global $globals; |
| 51 | return $globals->spoolroot.'/spool/wiki.d'; |
| 52 | } |
| 53 | |
| 54 | function wiki_clear_all_cache() |
| 55 | { |
| 56 | system('rm -f '.wiki_work_dir().'/cache_*'); |
| 57 | } |
| 58 | |
| 59 | function wiki_perms_options() { |
| 60 | return array('public' => 'Public', 'logged' => 'Connecté', |
| 61 | 'mdp' => 'Authentifié', 'admin' => 'Admin'); |
| 62 | } |
| 63 | |
| 64 | function wiki_get_perms($n) |
| 65 | { |
| 66 | $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n)); |
| 67 | $lines = explode("\n", @file_get_contents($file)); |
| 68 | foreach ($lines as $line) { |
| 69 | @list($k, $v) = explode('=', $line, 2); |
| 70 | if ($k == 'platal_perms') { |
| 71 | return explode(':', $v); |
| 72 | } |
| 73 | } |
| 74 | return array('logged', 'admin'); |
| 75 | } |
| 76 | |
| 77 | function wiki_putfile($f, $s) |
| 78 | { |
| 79 | $fp = fopen($f, 'w'); |
| 80 | fputs($fp, $s); |
| 81 | fclose($fp); |
| 82 | } |
| 83 | |
| 84 | function wiki_set_perms($n, $pr, $pw) |
| 85 | { |
| 86 | $file = wiki_work_dir().'/'.wiki_filename(str_replace('/', '.', $n)); |
| 87 | if (!file_exists($file)) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | $p = $pr . ':' . $pw; |
| 92 | |
| 93 | $lines = explode("\n", file_get_contents($file)); |
| 94 | foreach ($lines as $i => $line) { |
| 95 | list($k, $v) = explode('=', $line, 2); |
| 96 | if ($k == 'platal_perms') { |
| 97 | $lines[$i] = 'platal_perms='.$p; |
| 98 | wiki_putfile($file, join("\n", $lines)); |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | array_splice($lines, 1, 0, array('platal_perms='.$p)); |
| 104 | wiki_putfile($file, join("\n", $lines)); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | function wiki_may_have_perms($perm) { |
| 109 | switch ($perm) { |
| 110 | case 'public': return true; |
| 111 | case 'logged': return S::logged(); |
| 112 | case 'mdp': return S::logged(); |
| 113 | default: return S::has_perms(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | function wiki_apply_feed_perms($perm) |
| 118 | { |
| 119 | if ($perm == 'public') { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | require_once 'rss.inc.php'; |
| 124 | $uid = init_rss(null, Env::v('user'), Env::v('hash')); |
| 125 | if (!$uid) { |
| 126 | exit; |
| 127 | } |
| 128 | $res = XDB::query('SELECT user_id, IF (nom_usage <> \'\', nom_usage, nom) AS nom, prenom, perms |
| 129 | FROM auth_user_md5 |
| 130 | WHERE user_id = {?}', $uid); |
| 131 | if (!$res->numRows()) { |
| 132 | exit; |
| 133 | } |
| 134 | $table = $res->fetchOneAssoc(); |
| 135 | $_SESSION = array_merge($_SESSION, $table, array('forlife' => Env::v('user'))); |
| 136 | if ($perm == 'logged' || S::has_perms()) { |
| 137 | return; |
| 138 | } |
| 139 | exit; |
| 140 | } |
| 141 | |
| 142 | function wiki_apply_perms($perm) { |
| 143 | global $page, $platal, $globals; |
| 144 | |
| 145 | switch ($perm) { |
| 146 | case 'public': |
| 147 | return; |
| 148 | |
| 149 | case 'logged': |
| 150 | if (!call_user_func(array($globals->session, 'doAuthCookie'))) { |
| 151 | $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet(); |
| 152 | $platal->force_login($page); |
| 153 | } |
| 154 | return; |
| 155 | |
| 156 | default: |
| 157 | if (!call_user_func(array($globals->session, 'doAuth'))) { |
| 158 | $platal = empty($GLOBALS['IS_XNET_SITE']) ? new Platal() : new Xnet(); |
| 159 | $platal->force_login($page); |
| 160 | } |
| 161 | if ($perm == 'admin') { |
| 162 | check_perms(); |
| 163 | } |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | function wiki_require_page($pagename) |
| 169 | { |
| 170 | global $globals; |
| 171 | $pagename_slashes = str_replace('.','/',$pagename); |
| 172 | $pagename_dots = str_replace('/','.',$pagename); |
| 173 | if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) return; |
| 174 | system('wget '.$globals->baseurl.'/'.$pagename_slashes.' -O /dev/null'); |
| 175 | } |
| 176 | |
| 177 | function wiki_delete_page($pagename) |
| 178 | { |
| 179 | $pagename_dots = str_replace('/','.',$pagename); |
| 180 | if (!strpos($pagename_dots, '.')) { |
| 181 | return false; |
| 182 | } |
| 183 | $file = wiki_work_dir().'/'.wiki_filename($pagename_dots); |
| 184 | $cachefile = wiki_work_dir().'/cache_'.$pagename_dots.'.tpl'; |
| 185 | if (is_file($cachefile)) { |
| 186 | unlink($cachefile); |
| 187 | } |
| 188 | if (!is_file($file)) { |
| 189 | return false; |
| 190 | } |
| 191 | unlink($file); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | function wiki_links_in_line($line, $groupname) |
| 196 | { |
| 197 | $links = array(); |
| 198 | if (preg_match_all('@\[\[([^~][^\]\|\?#]*)((\?|#)[^\]\|]+)?(\\|[^\]]+)?\]\]@', $line, $matches, PREG_OFFSET_CAPTURE)) { |
| 199 | foreach ($matches[1] as $j => $link) if (!preg_match('@http://@', $link[0])) { |
| 200 | $mylink = str_replace('/','.',trim($link[0])); |
| 201 | $sup = trim(substr($matches[2][$j][0],1)); |
| 202 | $alt = trim(substr($matches[4][$j][0],1)); |
| 203 | $newlink = str_replace(' ','',ucwords($mylink)); |
| 204 | if (strpos($newlink,'.') === false) { |
| 205 | $newlink = $groupname.'.'.$newlink; |
| 206 | } |
| 207 | if (!$alt && $mylink != $newlink) { |
| 208 | $alt = trim($link[0]); |
| 209 | } |
| 210 | $links[] = array( |
| 211 | 'pos' => $matches[0][$j][1], |
| 212 | 'size' => strlen($matches[0][$j][0]), |
| 213 | 'href' => $newlink, |
| 214 | 'sup' => $sup, |
| 215 | 'alt' => $alt, |
| 216 | 'group' => substr($mylink, 0, strpos($mylink, '.'))); |
| 217 | } |
| 218 | } |
| 219 | return $links; |
| 220 | } |
| 221 | |
| 222 | function wiki_rename_page($pagename, $newname, $changeLinks = true) |
| 223 | { |
| 224 | $pagename_dots = str_replace('/','.',$pagename); |
| 225 | $newname_dots = str_replace('/','.',$newname); |
| 226 | if (!strpos($pagename_dots, '.') || !strpos($newname_dots, '.')) { |
| 227 | return false; |
| 228 | } |
| 229 | $groupname = substr($pagename_dots, 0, strpos($pagename_dots,'.')); |
| 230 | $newgroupname = substr($newname_dots, 0, strpos($pagename_dots,'.')); |
| 231 | |
| 232 | $file = wiki_work_dir().'/'.wiki_filename($pagename_dots); |
| 233 | $newfile = wiki_work_dir().'/'.wiki_filename($newname_dots); |
| 234 | if (!is_file($file)) { |
| 235 | // old page doesn't exist |
| 236 | return false; |
| 237 | } |
| 238 | if (!rename($file, $newfile)) { |
| 239 | // impossible to renama page |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | if (!$changeLinks) { |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | $changedLinks = 0; |
| 248 | // change name inside this folder and ingroup links if changing group |
| 249 | $lines = explode("\n", file_get_contents($newfile)); |
| 250 | $changed = false; |
| 251 | foreach ($lines as $i => $line) { |
| 252 | list($k, $v) = explode('=', $line, 2); |
| 253 | if ($k == 'name' && $v == $pagename_dots) { |
| 254 | $lines[$i] = 'name='.$newname_dots; |
| 255 | $changed = true; |
| 256 | } else if ($groupname != $newgroupname) { |
| 257 | $links = wiki_links_in_line($line, $groupname); |
| 258 | $newline = ''; $last = 0; |
| 259 | foreach ($links as $link) if ($link['group'] == $groupname) { |
| 260 | $newline .= substr($line, $last, $link['pos']); |
| 261 | $newline .= '[['.$link['href'].$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]'; |
| 262 | $last = $link['pos']+$link['size']; |
| 263 | $changedLinks++; |
| 264 | } |
| 265 | if ($last != 0) { |
| 266 | $newline .= substr($line, $last); |
| 267 | $lines[$i] = $newline; |
| 268 | $changed = true; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | wiki_putfile($newfile, join("\n", $lines)); |
| 273 | |
| 274 | // change wiki links in all wiki pages |
| 275 | $endname = substr($pagename_dots, strpos($pagename_dots,'.')+1); |
| 276 | $pages = array(); |
| 277 | exec("grep ".$endname." ".wiki_work_dir()."/* -sc", $pages); |
| 278 | foreach($pages as $line) { |
| 279 | if (preg_match('%/([^/:]+):([0-9]+)$%', $line, $vals) && $vals[2] > 0) { |
| 280 | $inpage = $vals[1]; |
| 281 | $lines = explode("\n", file_get_contents(wiki_work_dir().'/'.$inpage)); |
| 282 | $changed = false; |
| 283 | // find all wiki links in page and change if linked to this page |
| 284 | foreach ($lines as $i => $line) { |
| 285 | $links = wiki_links_in_line($line, substr($inpage, 0, strpos($inpage, '.'))); |
| 286 | $newline = ''; $last = 0; |
| 287 | foreach ($links as $link) { |
| 288 | if ($link['href'] == $pagename_dots) { |
| 289 | $newline .= substr($line, $last, $link['pos']); |
| 290 | $newline .= '[['.$newname_dots.$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]'; |
| 291 | $last = $link['pos']+$link['size']; |
| 292 | $changedLinks++; |
| 293 | } |
| 294 | } |
| 295 | if ($last != 0) { |
| 296 | $newline .= substr($line, $last); |
| 297 | $lines[$i] = $newline; |
| 298 | $changed = true; |
| 299 | } |
| 300 | } |
| 301 | if ($changed) |
| 302 | { |
| 303 | wiki_putfile(wiki_work_dir().'/'.$inpage, join("\n", $lines)); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | if ($changedLinks > 0) { |
| 308 | return $changedLinks; |
| 309 | } |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | function wiki_rename_folder($pagename, $newname, $changeLinks = true) |
| 314 | { |
| 315 | } |
| 316 | |
| 317 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 318 | ?> |