OSDN Git Service

Wikiメインウィジェット更新。
[magic3/magic3.git] / widgets / wiki_main / include / plugin / edit.inc.php
1 <?php
2 /**
3  * editプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2017 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id$
14  * @link       http://www.magic3.org
15  */
16 // PukiWiki - Yet another WikiWikiWeb clone.
17 // $Id: edit.inc.php 3474 2010-08-13 10:36:48Z fishbone $
18 // Copyright (C) 2001-2006 PukiWiki Developers Team
19 // License: GPL v2 or (at your option) any later version
20
21 define('PLUGIN_EDIT_FREEZE_REGEX', '/^(?:#freeze(?!\w)\s*)+/im');
22
23 function plugin_edit_action()
24 {
25         global $_title_edit;
26         global $script, $_title_cannotedit, $_msg_unfreeze, $_title_invalid_pagename, $_msg_invalid_pagename;
27         global $gEnvManager;
28
29         // ### パスワード認証フォーム表示 ###
30         // 認証されている場合はスルーして関数以降を実行
31         $retStatus = password_form();
32         if (!empty($retStatus)) return $retStatus;
33         
34         $page = WikiParam::getPage();
35
36         // 「:」で始まるシステム用ページは作成不可
37         if (strncmp($page, ':', 1) == 0){
38                 $msg  = $_title_invalid_pagename;
39                 $body = sprintf($_msg_invalid_pagename, $page);
40                 return array('msg' => $msg, 'body' => $body);
41         }
42                 
43         if (!check_editable($page, true, true)){                // 編集不可のとき
44                 $body = $title = str_replace('$1', make_pagelink($page), $_title_cannotedit);
45                 if (is_freeze($page) && WikiConfig::isUserWithFreezeAuth()){                    // 解凍・凍結権限ありの場合
46                         $body .= '(<a href="' . $script . WikiParam::convQuery('?cmd=unfreeze&amp;page=' . rawurlencode($page)) . '">' . $_msg_unfreeze . '</a>)';
47                 }
48                 return array('msg' => $title, 'body' => $body);
49         }
50
51         if (WikiParam::getVar('preview') != '' || WikiParam::getVar('template') != '') {
52                 return plugin_edit_preview();
53         } else if (WikiParam::getVar('write') != '') {
54                 return plugin_edit_write();
55         } else if (WikiParam::getVar('cancel') != '') { // キャンセルボタンが押されたとき
56                 return plugin_edit_cancel();
57         }
58
59         $postdata = get_source($page, true);
60         if ($postdata == '') $postdata = auto_template($page);
61         return array('msg'=>$_title_edit, 'body'=>edit_form($page, $postdata));
62 }
63
64 // Preview
65 function plugin_edit_preview()
66 {
67         global $_title_preview, $_msg_preview, $_msg_preview_delete;
68
69         $page = WikiParam::getPage();
70
71         // Loading template
72         $templatePage = WikiParam::getVar('template_page');
73         if ($templatePage != '' && WikiPage::isPage($templatePage)) {
74 /*              $vars['msg'] = join('', get_source($vars['template_page']));
75                 // Cut fixed anchors
76                 $vars['msg'] = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $vars['msg']);*/
77                 
78                 $msg = get_source($templatePage, true);
79
80                 // Cut fixed anchors
81                 $msg = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m', '$1$2', $msg);
82 //              WikiParam::setMsg($msg);                // tmp
83         } else {
84                 $msg = WikiParam::getMsg();
85         }
86
87         /*$vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
88         $postdata = $vars['msg'];*/
89 //      $msg = WikiParam::getMsg();                     // tmp
90         //$msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $msg);              // not use freeze expression removed by magic3
91 //      WikiParam::setMsg($msg);                // tmp
92         $postdata = $msg;
93         
94         if (WikiParam::getVar('add')) {
95                 if (WikiParam::getVar('add_top')) {
96                         //$postdata  = $postdata . "\n\n" . @join('', get_source($page));
97                         $postdata  = $postdata . "\n\n" . get_source($page, true);
98                 } else {
99                         //$postdata  = @join('', get_source($page)) . "\n\n" . $postdata;
100                         $postdata  = get_source($page, true) . "\n\n" . $postdata;
101                 }
102         }
103
104         $body = $_msg_preview . '<br />' . "\n";
105         if ($postdata == '')
106                 $body .= '<strong>' . $_msg_preview_delete . '</strong>';
107         $body .= '<br />' . "\n";
108
109         if ($postdata) {
110                 $postdata = make_str_rules($postdata);
111                 $postdata = explode("\n", $postdata);
112                 $postdata = drop_submit(convert_html($postdata));
113                 $body .= '<div id="preview">' . $postdata . '</div>' . "\n";
114         }
115         //$body .= edit_form($page, $vars['msg'], $vars['digest'], FALSE);
116         $body .= edit_form($page, $msg, WikiParam::getVar('digest'), FALSE);
117
118         return array('msg'=>$_title_preview, 'body'=>$body);
119 }
120
121 // Inline: Show edit (or unfreeze text) link
122 function plugin_edit_inline()
123 {
124         static $usage = '&edit(pagename#anchor[[,noicon],nolabel])[{label}];';
125         global $script, $fixed_heading_anchor_edit;
126
127         // Arguments
128         $args = func_get_args();
129
130         // {label}. Strip anchor tags only
131         $s_label = strip_htmltag(array_pop($args), FALSE);
132
133         $page    = array_shift($args);
134         if ($page == NULL) $page = '';
135         $_noicon = $_nolabel = FALSE;
136         foreach($args as $arg){
137                 switch(strtolower($arg)){
138                 case ''       :                   break;
139                 case 'nolabel': $_nolabel = TRUE; break;
140                 case 'noicon' : $_noicon  = TRUE; break;
141                 default       : return $usage;
142                 }
143         }
144
145         // Separate a page-name and a fixed anchor
146         list($s_page, $id, $editable) = anchor_explode($page, TRUE);
147
148         // Default: This one
149         if ($s_page == '') $s_page = WikiParam::getPage();
150
151         // $s_page fixed
152         $isfreeze = is_freeze($s_page);
153         $ispage   = WikiPage::isPage($s_page);
154
155         // Paragraph edit enabled or not
156         $short = htmlspecialchars('Edit');
157         if ($fixed_heading_anchor_edit && $editable && $ispage && ! $isfreeze) {
158                 // Paragraph editing
159                 $id    = rawurlencode($id);
160                 $title = htmlspecialchars(sprintf('Edit %s', $page));
161                 $icon = '<img src="' . IMAGE_DIR . 'paraedit.png' .
162                         '" width="9" height="9" alt="' .
163                         $short . '" title="' . $title . '" /> ';
164                 $class = ' class="anchor_super"';
165         } else {
166                 // Normal editing / unfreeze
167                 $id    = '';
168                 if ($isfreeze) {
169                         $title = 'Unfreeze %s';
170                         $icon  = 'unfreeze.png';
171                 } else {
172                         $title = 'Edit %s';
173                         $icon  = 'edit.png';
174                 }
175                 $title = htmlspecialchars(sprintf($title, $s_page));
176                 $icon = '<img src="' . IMAGE_DIR . $icon .
177                         '" width="20" height="20" alt="' .
178                         $short . '" title="' . $title . '" />';
179                 $class = '';
180         }
181         if ($_noicon) $icon = ''; // No more icon
182         if ($_nolabel) {
183                 if (!$_noicon) {
184                         $s_label = '';     // No label with an icon
185                 } else {
186                         $s_label = $short; // Short label without an icon
187                 }
188         } else {
189                 if ($s_label == '') $s_label = $title; // Rich label with an icon
190         }
191
192         // URL
193         // modified for Magic3 by naoki on 2008/10/6
194         if ($isfreeze) {
195                 //$url   = $script . '?cmd=unfreeze&amp;page=' . rawurlencode($s_page);
196                 $url   = $script . WikiParam::convQuery('?cmd=unfreeze&amp;page=' . rawurlencode($s_page));
197         } else {
198                 $s_id = ($id == '') ? '' : '&amp;id=' . $id;
199                 //$url  = $script . '?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id;
200                 $url  = $script . WikiParam::convQuery('?cmd=edit&amp;page=' . rawurlencode($s_page) . $s_id);
201         }
202         $atag  = '<a' . $class . ' href="' . $url . '" title="' . $title . '">';
203         static $atags = '</a>';
204
205         if ($ispage) {
206                 // Normal edit link
207                 return $atag . $icon . $s_label . $atags;
208         } else {
209                 // Dangling edit link
210                 return '<span class="noexists">' . $atag . $icon . $atags .
211                         $s_label . $atag . '?' . $atags . '</span>';
212         }
213 }
214
215 // Write, add, or insert new comment
216 function plugin_edit_write()
217 {
218         global $_title_collided, $_msg_collided_auto, $_msg_collided, $_title_deleted;
219         global $notimeupdate, $_msg_invalidpass, $do_update_diff_table;
220         global $gPageManager;
221
222         $page   = WikiParam::getPage();
223         $add    = WikiParam::getVar('add');
224         $digest = WikiParam::getVar('digest');          // POST値を取得
225         
226         /*$vars['msg'] = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', $vars['msg']);
227         $msg = $vars['msg']; // Reference*/
228         //$msg = preg_replace(PLUGIN_EDIT_FREEZE_REGEX, '', WikiParam::getMsg());               // removed for magic3
229         //WikiParam::setMsg($msg);
230         $msg = WikiParam::getMsg();                     // 編集データ
231         
232         $retvars = array();
233
234         // Collision Detection
235         //$oldpagesrc = join('', get_source($page));
236         // 変更データの元データが保存されているデータと同じかどうかのチェック
237         $oldpagesrc = get_source($page, true);
238         $oldpagemd5 = md5($oldpagesrc);
239         if ($digest != $oldpagemd5){            // 元データが異なるとき
240                 //$vars['digest'] = $oldpagemd5; // Reset
241                 WikiParam::setDigest($oldpagemd5); // Reset
242
243                 $original = WikiParam::getOriginal();           // 編集前の元データ
244                 list($postdata_input, $auto) = do_update_diff($oldpagesrc, $msg, $original);
245
246                 $retvars['msg' ] = $_title_collided;
247                 $retvars['body'] = ($auto ? $_msg_collided_auto : $_msg_collided) . "\n";
248                 $retvars['body'] .= $do_update_diff_table;
249                 $retvars['body'] .= edit_form($page, $postdata_input, $oldpagemd5, FALSE);
250                 return $retvars;
251         }
252
253         // Action?
254         if ($add) {
255                 // Add
256                 if (WikiParam::getVar('add_top')) {
257                         //$postdata  = $msg . "\n\n" . @join('', get_source($page));
258                         $postdata  = $msg . "\n\n" . get_source($page, true);
259                 } else {
260                         //$postdata  = @join('', get_source($page)) . "\n\n" . $msg;
261                         $postdata  = get_source($page, true) . "\n\n" . $msg;
262                 }
263         } else {
264                 // Edit or Remove
265                 //$postdata = $msg; // Reference
266                 $postdata = $msg;
267         }
268
269         // NULL POSTING, OR removing existing page
270         //if ($postdata == '') {
271         // スペースや改行のみのデータのときは、空データとしてページを削除
272         if (trim($postdata) == ''){
273                 //page_write($page, $postdata);
274                 page_write($page, '');
275                 $retvars['msg' ] = $_title_deleted;
276                 $retvars['body'] = str_replace('$1', htmlspecialchars($page), $_title_deleted);
277
278 //              if ($trackback) tb_delete($page);
279
280                 return $retvars;
281         }
282
283         // $notimeupdate: Checkbox 'Do not change timestamp'
284         //$notimestamp = isset($vars['notimestamp']) && $vars['notimestamp'] != '';
285         $notimestamp = WikiParam::getVar('notimestamp') != '';
286         //if ($notimeupdate > 1 && $notimestamp && ! pkwk_login($vars['pass'])) {
287         if ($notimeupdate > 1 && $notimestamp && ! pkwk_login(WikiParam::getVar('pass'))) {
288                 // Enable only administrator & password error
289                 $retvars['body']  = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
290                 $retvars['body'] .= edit_form($page, $msg, $digest, FALSE);
291                 return $retvars;
292         }
293
294         page_write($page, $postdata, $notimeupdate != 0 && $notimestamp);
295 //      pkwk_headers_sent();
296         // modified for Magic3 by naoki on 2008/10/6
297         //header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
298 //      header('Location: ' . get_script_uri() . WikiParam::convQuery('?' . rawurlencode($page), false));
299 //      exit;
300         // 編集権限がある場合はプレビューモードで画面を表示
301         $urlOption = '';
302         if (WikiConfig::isUserWithEditAuth()) $urlOption = '&' . M3_REQUEST_PARAM_OPERATION_COMMAND . '=' . M3_REQUEST_CMD_PREVIEW;
303                 
304         $gPageManager->redirect(get_script_uri() . WikiParam::convQuery('?' . rawurlencode($page), false) . $urlOption);
305 }
306
307 /**
308  * キャンセルボタンが押されたときの処理
309  *
310  * @return                              なし
311  */
312 function plugin_edit_cancel()
313 {
314         global $gPageManager;
315
316         $page = WikiParam::getPage();
317         
318         // リファラがある場合はリファラのページへ遷移
319         $refer = WikiParam::getRefer();
320         if (!empty($refer)) $page = $refer;
321         
322         // 編集権限がある場合はプレビューモードで画面を表示
323         $urlOption = '';
324         if (WikiConfig::isUserWithEditAuth()) $urlOption = '&' . M3_REQUEST_PARAM_OPERATION_COMMAND . '=' . M3_REQUEST_CMD_PREVIEW;
325         
326         $gPageManager->redirect(get_script_uri() . WikiParam::convQuery('?' . rawurlencode($page), false) . $urlOption);
327 }
328 ?>