OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / bookmarklet.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2012 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  *
12  * This script allows adding items to Nucleus through bookmarklets. The member must be logged in
13  * in order to use this.
14  */
15
16 // bookmarklet is part of admin area (might need XML-RPC)
17 $CONF = array();
18 $CONF['UsingAdminArea'] = 1;
19
20 // include all classes and config data
21 include('../config.php');
22
23 $action = requestVar('action');
24
25 if ($action == 'contextmenucode') {
26         bm_doContextMenuCode();
27         exit;
28 }
29
30 if (!$member->isLoggedIn() ) {
31         bm_loginAndPassThrough();
32         exit;
33 }
34
35 // on successfull login
36 if ( ($action == 'login') && ($member->isLoggedIn() ) ) {
37         $action = requestVar('nextaction');
38 }
39
40 if ($action == '') {
41         $action = 'add';
42 }
43
44 sendContentType('text/html', 'bookmarklet-' . $action);
45
46 // check ticket
47 $action = strtolower($action);
48 $aActionsNotToCheck = array('login', 'add', 'edit');
49
50 if (!in_array($action, $aActionsNotToCheck) ) {
51
52         if (!$manager->checkTicket() ) {
53                 bm_doError(_ERROR_BADTICKET);
54         }
55
56 }
57
58 // find out what to do
59 switch ($action) {
60         // adds the item for real
61         case 'additem':
62                 bm_doAddItem();
63                 break;
64
65                 // shows the edit item form
66         case 'edit':
67                 bm_doEditForm();
68                 break;
69
70                 // edits the item for real
71         case 'edititem':
72                 bm_doEditItem();
73                 break;
74
75                 // on login, 'action' gets changed to 'nextaction'
76         case 'login':
77                 bm_doError(_BOOKMARKLET_ERROR_SOMETHINGWRONG);
78                 break;
79
80                 // shows the fill in form
81         case 'add':
82         default:
83                 bm_doShowForm();
84                 break;
85 }
86
87 function bm_doAddItem() {
88         global $member, $manager, $CONF;
89
90         $manager->loadClass('ITEM');
91         $result = ITEM::createFromRequest();
92
93         if ($result['status'] == 'error') {
94                 bm_doError($result['message']);
95         }
96
97         $blogid = getBlogIDFromItemID($result['itemid']);
98         $blog =& $manager->getBlog($blogid);
99
100         if ($result['status'] == 'newcategory') {
101                 $href      = 'index.php?action=categoryedit&amp;blogid=' . $blogid . '&amp;catid=' . $result['catid'];
102                 $onclick   = 'if (event &amp;&amp; event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
103                 $title     = _BOOKMARKLET_NEW_WINDOW;
104                 $aTag      = ' <a href="' . $href . '" onclick="' . $onclick . '" title="' . $title . '">';
105                 $message   = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
106                 $extrahead = '';
107         } else {
108                 $message = _ITEM_ADDED;
109                 $extrahead = '';
110         }
111
112         bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
113 }
114
115 function bm_doEditItem() {
116         global $member, $manager, $CONF;
117
118         $itemid = intRequestVar('itemid');
119         $catid = postVar('catid');
120
121         // only allow if user is allowed to alter item
122         if (!$member->canUpdateItem($itemid, $catid) ) {
123                 bm_doError(_ERROR_DISALLOWED);
124         }
125
126         $body = postVar('body');
127         $title = postVar('title');
128         $more = postVar('more');
129         $closed = intPostVar('closed');
130         $actiontype = postVar('actiontype');
131         $draftid = intPostVar('draftid');
132
133         // redirect to admin area on delete (has delete confirmation)
134         if ($actiontype == 'delete') {
135                 redirect('index.php?action=itemdelete&itemid=' . $itemid);
136                 exit;
137         }
138
139         // create new category if needed (only on edit/changedate)
140         if (strstr($catid,'newcat') ) {
141                 // get blogid
142                 list($blogid) = sscanf($catid, "newcat-%d");
143
144                 // create
145                 $blog =& $manager->getBlog($blogid);
146                 $catid = $blog->createNewCategory();
147
148                 // show error when sth goes wrong
149                 if (!$catid) {
150                         bm_doError(_BOOKMARKLET_ERROR_COULDNTNEWCAT);
151                 }
152         }
153
154         // only edit action is allowed for bookmarklet edit
155         switch ($actiontype) {
156                 case 'changedate':
157                         $publish = 1;
158                         $wasdraft = 0;
159                         $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
160                         break;
161                 case 'edit':
162                         $publish = 1;
163                         $wasdraft = 0;
164                         $timestamp = 0;
165                         break;
166                 case 'backtodrafts':
167                         $publish = 0;
168                         $wasdraft = 0;
169                         $timestamp = 0;
170                         break;
171                 default:
172                         bm_doError(_BOOKMARKLET_ERROR_SOMETHINGWRONG);
173         }
174
175         // update item for real
176         ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
177
178         if ($draftid > 0) {
179                 ITEM::delete($draftid);
180         }
181
182         // show success message
183         if ($catid != intPostVar('catid') ) {
184                 $href      = 'index.php?action=categoryedit&amp;blogid=' . $blog->getID() . '&amp;catid=' . $catid;
185                 $onclick   = 'if (event &amp;&amp; event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
186                 $title     = _BOOKMARKLET_NEW_WINDOW;
187                 $aTag      = ' <a href="' . $href . '" onclick="' . $onclick . '" title="' . $title . '">';
188                 $message   = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
189                 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>', '');
190         } else {
191                 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
192         }
193 }
194
195 function bm_loginAndPassThrough() {
196
197         $blogid = intRequestVar('blogid');
198         $log_text = requestVar('logtext');
199         $log_link = requestVar('loglink');
200         $log_linktitle = requestVar('loglinktitle');
201
202         ?>
203 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
204 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
205 <head>
206 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
207 <title>Nucleus</title>
208         <?php bm_style(); ?>
209 </head>
210 <body>
211 <h1><?php echo _LOGIN_PLEASE ?></h1>
212
213 <form method="post" action="bookmarklet.php">
214         <p>
215                 <input name="action" value="login" type="hidden" />
216                 <input name="blogid" value="<?php echo htmlspecialchars($blogid); ?>" type="hidden" />
217                 <input name="logtext" value="<?php echo htmlspecialchars($log_text); ?>" type="hidden" />
218                 <input name="loglink" value="<?php echo htmlspecialchars($log_link); ?>" type="hidden" />
219                 <input name="loglinktitle" value="<?php echo htmlspecialchars($log_linktitle); ?>" type="hidden" />
220                 <?php echo _LOGINFORM_NAME ?>
221                 <br /><input name="login" />
222                 <br /><?php echo _LOGINFORM_PWD ?>
223                 <br /><input name="password" type="password" />
224                 <br /><br />
225                 <br /><input type="submit" value="<?php echo _LOGIN ?>" />
226         </p>
227 </form>
228 <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE ?></a></p>
229 </body>
230 </html>
231         <?php
232 }
233
234 function bm_doShowForm() {
235         global $member;
236
237         $blogid = intRequestVar('blogid');
238         $log_text = trim(requestVar('logtext'));
239         $log_link = requestVar('loglink');
240         $log_linktitle = requestVar('loglinktitle');
241
242         $log_text = uniDecode($log_text,_CHARSET);
243         $log_linktitle = uniDecode($log_linktitle,_CHARSET);
244
245         if (!BLOG::existsID($blogid) ) {
246                 bm_doError(_ERROR_NOSUCHBLOG);
247         }
248
249         if (!$member->isTeamMember($blogid) ) {
250                 bm_doError(_ERROR_NOTONTEAM);
251         }
252
253         $logje = '';
254
255         if ($log_text) {
256                 $logje .= '<blockquote><div>"' . htmlspecialchars($log_text) . '"</div></blockquote>' . "\n";
257         }
258
259         if (!$log_linktitle) {
260                 $log_linktitle = $log_link;
261         }
262
263         if ($log_link) {
264                 $logje .= '<a href="' . htmlspecialchars($log_link) . '">' . htmlspecialchars($log_linktitle) . '</a>';
265         }
266
267         $item['body'] = $logje;
268         $item['title'] = htmlspecialchars($log_linktitle);
269
270         $factory = new PAGEFACTORY($blogid);
271         $factory->createAddForm('bookmarklet', $item);
272 }
273
274 function bm_doEditForm() {
275         global $member, $manager;
276
277         $itemid = intRequestVar('itemid');
278
279         if (!$manager->existsItem($itemid, 0, 0) ) {
280                 bm_doError(_ERROR_NOSUCHITEM);
281         }
282
283         if (!$member->canAlterItem($itemid) ) {
284                 bm_doError(_ERROR_DISALLOWED);
285         }
286
287         $item =& $manager->getItem($itemid, 1, 1);
288         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
289
290         $param = array('item' => &$item);
291         $manager->notify('PrepareItemForEdit', $param);
292
293         if ($blog->convertBreaks() ) {
294                 $item['body'] = removeBreaks($item['body']);
295                 $item['more'] = removeBreaks($item['more']);
296         }
297
298         $formfactory = new PAGEFACTORY($blog->getID() );
299         $formfactory->createEditForm('bookmarklet', $item);
300 }
301
302 function bm_doError($msg) {
303         bm_message(_ERROR, _ERRORMSG, $msg);
304         die;
305 }
306
307 function bm_message($title, $head, $msg, $extrahead = '') {
308         ?>
309 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
310 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
311 <head>
312 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
313 <title><?php echo $title ?></title>
314         <?php bm_style(); ?>
315         <?php echo $extrahead; ?>
316 </head>
317 <body>
318 <h1><?php echo $head; ?></h1>
319 <p><?php echo $msg; ?></p>
320 <p><a href="bookmarklet.php" onclick="window.close();window.opener.location.reload();"><?php echo _POPUP_CLOSE ?></a></p>
321 </body>
322 </html>
323
324         <?php
325 }
326
327 function bm_style() {
328         echo '<link rel="stylesheet" type="text/css" href="styles/bookmarklet.css" />';
329         echo '<link rel="stylesheet" type="text/css" href="styles/addedit.css" />';
330 }
331
332 function bm_doContextMenuCode() {
333         global $CONF;
334         ?>
335 <script type="text/javascript" defer="defer">
336 doc = external.menuArguments.document;
337 lt = escape(doc.selection.createRange().text);
338 loglink = escape(external.menuArguments.location.href);
339 loglinktitle = escape(doc.title);
340 wingm = window.open('<?php echo $CONF['AdminURL']?>bookmarklet.php?blogid=<?php echo intGetVar('blogid')?>&logtext=' + lt + '&loglink=' + loglink + '&loglinktitle=' + loglinktitle, 'nucleusbm', 'scrollbars=yes,width=710,height=500,left=10,top=10,status=yes,resizable=yes');
341 wingm.focus();
342 </script>
343         <?php
344 }
345
346 function uniDecode($str,$charcode){
347         $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/", 'toUtf8', $str);
348         return mb_convert_encoding($text, $charcode, 'UTF-8');
349 }
350 function toUtf8($ar){
351         foreach($ar as $val){
352                 $val = intval(substr($val,2),16);
353                 if($val < 0x7F){        // 0000-007F
354                         $c .= chr($val);
355                 }elseif($val < 0x800) { // 0080-0800
356                         $c .= chr(0xC0 | ($val / 64));
357                         $c .= chr(0x80 | ($val % 64));
358                 }else{                // 0800-FFFF
359                         $c .= chr(0xE0 | (($val / 64) / 64));
360                         $c .= chr(0x80 | (($val / 64) % 64));
361                         $c .= chr(0x80 | ($val % 64));
362                 }
363         }
364         return $c;
365 }
366
367 ?>