OSDN Git Service

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