OSDN Git Service

6c2bee14526dc56165da7e336f3a3070bde7dcb2
[nucleus-jp/nucleus-next.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: 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 // get skin object
29 $skinid = $CONF['BookmarkletSkin'];
30 if ( !Skin::existsID($skinid) )
31 {
32         echo _ERROR_SKIN;
33         exit;
34 }
35 $skin = new Skin($skinid, 'AdminActions', 'AdminSkin');
36
37 // check logged-in or pass through
38 $action = requestVar('action');
39 if ( !$member->isLoggedIn() )
40 {
41         bm_loginAndPassThrough($skin, $action);
42         exit;
43 }
44 else if ( $action == 'login')
45 {
46         $action = requestVar('nextaction');
47 }
48
49 $action = strtolower($action);
50
51 if ( $action == 'contextmenucode' )
52 {
53         bm_doContextMenuCode();
54         exit;
55 }
56 else if ( $action == '' )
57 {
58         $action = 'add';
59 }
60
61 // check ticket
62 $aActionsNotToCheck = array('login', 'add', 'edit');
63 if ( !in_array($action, $aActionsNotToCheck) )
64 {
65         if ( !$manager->checkTicket() )
66         {
67                 bm_doError($skin, _ERROR_BADTICKET);
68         }
69 }
70
71 // find out what to do
72 switch ( $action )
73 {
74         // adds the item for real
75         case 'additem':
76                 bm_doAddItem($skin);
77                 break;
78         
79         // shows the edit item form
80         case 'edit':
81                 bm_doEditForm($skin);
82                 break;
83         
84         // edits the item for real
85         case 'edititem':
86                 bm_doEditItem($skin);
87                 break;
88         
89         // on login, 'action' gets changed to 'nextaction'
90         case 'login':
91                 bm_doError($skin, 'Something went wrong');
92                 break;
93         
94         // shows the fill in form
95         case 'add':
96         default:
97                 bm_doShowForm($skin);
98                 break;
99 }
100
101 function bm_doAddItem($skin)
102 {
103         global $member, $manager, $CONF;
104         
105         $manager->loadClass('ITEM');
106         $result = Item::createFromRequest();
107         
108         if ( $result['status'] == 'error' )
109         {
110                 bm_doError($skin, $result['message']);
111         }
112         
113         $blogid = getBlogIDFromItemID($result['itemid']);
114         $blog =& $manager->getBlog($blogid);
115         
116         if ( $result['status'] == 'newcategory' )
117         {
118                 $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>';
119                 $extrahead = '';
120         }
121         else
122         {
123                 $message = _ITEM_ADDED;
124                 $extrahead = '';
125         }
126         
127         bm_message($skin, _ITEM_ADDED, $message,$extrahead);
128         
129         return;
130 }
131
132 function bm_doEditItem($skin)
133 {
134         global $member, $manager, $CONF;
135         
136         $itemid = intRequestVar('itemid');
137         $catid = postVar('catid');
138         
139         // only allow if user is allowed to alter item
140         if ( !$member->canUpdateItem($itemid, $catid) )
141         {
142                 bm_doError($skin, _ERROR_DISALLOWED);
143         }
144         
145         $body = postVar('body');
146         $title = postVar('title');
147         $more = postVar('more');
148         $closed = intPostVar('closed');
149         $actiontype = postVar('actiontype');
150         $draftid = intPostVar('draftid');
151         
152         // redirect to admin area on delete (has delete confirmation)
153         if ( $actiontype == 'delete' )
154         {
155                 redirect('index.php?action=itemdelete&itemid=' . $itemid);
156                 exit;
157         }
158         
159         // create new category if needed (only on edit/changedate)
160         if ( i18n::strpos($catid,'newcat') === 0 )
161         {
162                 // get blogid
163                 list($blogid) = sscanf($catid, "newcat-%d");
164                 
165                 // create
166                 $blog =& $manager->getBlog($blogid);
167                 $catid = $blog->createNewCategory();
168                 
169                 // show error when sth goes wrong
170                 if ( !$catid )
171                 {
172                         bm_doError($skin, 'Could not create new category');
173                 }
174         }
175         
176         // only edit action is allowed for bookmarklet edit
177         switch ( $actiontype )
178         {
179                 case 'changedate':
180                         $publish = 1;
181                         $wasdraft = 0;
182                         $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
183                         break;
184                 case 'edit':
185                         $publish = 1;
186                         $wasdraft = 0;
187                         $timestamp = 0;
188                         break;
189                 case 'backtodrafts':
190                         $publish = 0;
191                         $wasdraft = 0;
192                         $timestamp = 0;
193                         break;
194                 default:
195                         bm_doError($skin, 'Something went wrong');
196         }
197         
198         // update item for real
199         Item::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
200         
201         if ( $draftid > 0 )
202         {
203                 Item::delete($draftid);
204         }
205         
206         if ( $result['status'] == 'newcategory' )
207         {
208                 $href           = "index.php?action=categoryedit&amp;blogid={$blogid}&amp;catid={$result['catid']}";
209                 $onclick        = 'if (event &amp;&amp; event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
210                 $title          = _BOOKMARKLET_NEW_WINDOW;
211                 $aTag           = " <a href=\"{$href}\" onclick=\"{$onclick}\" title=\"{$title}\">";
212                 $message        = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
213         }
214         else
215         {
216                 $message = _ITEM_ADDED;
217         }
218         
219         // show success message
220         bm_message($skin, _ITEM_ADDED, $message, '');
221         return;
222 }
223
224 function bm_loginAndPassThrough($skin, $action='add')
225 {
226         /*
227          * TODO: これを出力させる
228         $blogid = intRequestVar('blogid');
229         $itemid = intRequestVar('itemid');
230         $log_text = requestVar('logtext');
231         $log_link = requestVar('loglink');
232         $log_linktitle = requestVar('loglinktitle');
233         
234         echo '<input type="hidden" name="blogid" value="' . Entity::hsc($blogid). '" />' . "\n";
235         echo '<input type="hidden" name="itemid" value="' . Entity::hsc($itemid). '" />' . "\n";
236         echo '<input type="hidden" name="logtext" value="' . Entity::hsc($log_text) . '" />' . "\n";
237         echo '<input type="hidden" name="loglink" value="' . Entity::hsc($log_link) . '" />' . "\n";
238         echo '<input type="hidden" name="loglinktitle" value="' . Entity::hsc($log_linktitle) . '" />' . "\n";
239         echo "<input type=\"hidden\" name=\"nextaction\" value=\"{$action}\" />\n";
240         */
241         
242         $skin->parse('showlogin');
243         
244         return;
245 }
246
247 function bm_doShowForm($skin)
248 {
249         global $manager, $member;
250         
251         $blogid                 = intRequestVar('blogid');
252         $log_text               = trim(requestVar('logtext'));
253         $log_link               = requestVar('loglink');
254         $log_linktitle  = requestVar('loglinktitle');
255         
256         if ( !Blog::existsID($blogid) )
257         {
258                 bm_doError($skin, _ERROR_NOSUCHBLOG);
259         }
260         else if ( !$member->isTeamMember($blogid) )
261         {
262                 bm_doError($skin, _ERROR_NOTONTEAM);
263         }
264         
265         $blog =& $manager->getBlog($blogid);
266         
267         $logje = '';
268         
269         if ( $log_text )
270         {
271                 $logje .= '<blockquote><div>"' . Entity::hsc($log_text) . '"</div></blockquote>' . "\n";
272         }
273         
274         if ( !$log_linktitle )
275         {
276                 $log_linktitle = $log_link;
277         }
278         
279         if ( $log_link )
280         {
281                 $logje .= '<a href="' . Entity::hsc($log_link) . '">' . Entity::hsc($log_linktitle) . '</a>';
282         }
283         
284         $item = array();
285         $item['body'] = $logje;
286         $item['title'] = Entity::hsc($log_linktitle);
287         
288         $data = array(
289                 'blog'          => &$blog,
290                 'item'          => &$item,
291                 'contents'      => &$item
292         );
293         $manager->notify('PreAddItemForm', $data);
294         
295         if ( $blog->convertBreaks() )
296         {
297                 $item['body'] = removeBreaks($item['body']);
298         }
299         
300         Admin::$blog = &$blog;
301         Admin::$contents = &$item;
302         
303         Admin::setAdminAction('createitem');
304         $skin->parse('createitem');
305         
306         return;
307 }
308
309 function bm_doEditForm($skin)
310 {
311         global $member, $manager;
312         
313         $itemid = intRequestVar('itemid');
314         
315         if ( !$manager->existsItem($itemid, 0, 0) )
316         {
317                 bm_doError($skin, _ERROR_NOSUCHITEM);
318         }
319         else if ( !$member->canAlterItem($itemid) )
320         {
321                 bm_doError($skin, _ERROR_DISALLOWED);
322         }
323         
324         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
325         $item =& $manager->getItem($itemid, 1, 1);
326         
327         $data = array(
328                 'blog' => &$blog,
329                 'item' => &$item
330         );
331         $manager->notify('PrepareItemForEdit', $data);
332         
333         if ( $blog->convertBreaks() )
334         {
335                 $item['body'] = removeBreaks($item['body']);
336                 $item['more'] = removeBreaks($item['more']);
337         }
338         
339         Admin::$blog = &$blog;
340         Admin::$contents = &$item;
341         
342         Admin::setAdminAction('itemedit');
343         $skin->parse('itemedit');
344         
345         return;}
346
347 function bm_doError($skin, $msg)
348 {
349         bm_message($skin, _ERRORMSG, $msg);
350         die;
351 }
352
353 function bm_message($skin, $title, $msg, $extrahead = '')
354 {
355         Admin::$extrahead = $extrahead;
356         Admin::$headMess = $msg;
357         $skin->parse('adminerrorpage');
358         
359         return;
360 }
361
362 function bm_doContextMenuCode($width=600, $height=500)
363 {
364         global $CONF;   
365         $blogid = (integer) intGetVar('blogid');
366         
367         echo "<script type=\"text/javascript\" defer=\"defer\">\n";
368         echo "<![CDATA[\n";
369         echo " doc = external.menuArguments.document;\n";
370         echo " lt = encodeURIComponent(doc.selection.createRange().text);\n";
371         echo " loglink = encodeURIComponent(external.menuArguments.location.href);\n";
372         echo " loglinktitle = encodeURIComponent(doc.title);\n";
373         echo " wingm = window.open('{$CONF['AdminURL']}bookmarklet.php?blogid={$blogid}&logtext=' + lt + '&loglink=' + loglink + '&loglinktitle=' + loglinktitle, 'nucleusbm', 'scrollbars=yes,width={$width},height={$height},left=10,top=10,status=yes,resizable=yes')\n";
374         echo " wingm.focus()\n";
375         echo "]]>\n";
376         echo "</script>\n";
377 }