OSDN Git Service

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