OSDN Git Service

b7d39e199aa81a9b80e0c5ca32f827be4ad02418
[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(_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, _ITEM_ADDED, $message,$extrahead);
128         
129         return;
130         
131         return;
132 }
133
134 function bm_doEditItem($skin)
135 {
136         global $member, $manager, $CONF;
137         
138         $itemid = intRequestVar('itemid');
139         $catid = postVar('catid');
140         
141         // only allow if user is allowed to alter item
142         if ( !$member->canUpdateItem($itemid, $catid) )
143         {
144                 bm_doError(_ERROR_DISALLOWED);
145         }
146         
147         $body = postVar('body');
148         $title = postVar('title');
149         $more = postVar('more');
150         $closed = intPostVar('closed');
151         $actiontype = postVar('actiontype');
152         $draftid = intPostVar('draftid');
153         
154         // redirect to admin area on delete (has delete confirmation)
155         if ( $actiontype == 'delete' )
156         {
157                 redirect('index.php?action=itemdelete&itemid=' . $itemid);
158                 exit;
159         }
160         
161         // create new category if needed (only on edit/changedate)
162         if ( i18n::strpos($catid,'newcat') === 0 )
163         {
164                 // get blogid
165                 list($blogid) = sscanf($catid, "newcat-%d");
166                 
167                 // create
168                 $blog =& $manager->getBlog($blogid);
169                 $catid = $blog->createNewCategory();
170                 
171                 // show error when sth goes wrong
172                 if ( !$catid )
173                 {
174                         bm_doError('Could not create new category');
175                 }
176         }
177         
178         // only edit action is allowed for bookmarklet edit
179         switch ( $actiontype )
180         {
181                 case 'changedate':
182                         $publish = 1;
183                         $wasdraft = 0;
184                         $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
185                         break;
186                 case 'edit':
187                         $publish = 1;
188                         $wasdraft = 0;
189                         $timestamp = 0;
190                         break;
191                 case 'backtodrafts':
192                         $publish = 0;
193                         $wasdraft = 0;
194                         $timestamp = 0;
195                         break;
196                 default:
197                         bm_doError('Something went wrong');
198         }
199         
200         // update item for real
201         Item::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
202         
203         if ( $draftid > 0 )
204         {
205                 Item::delete($draftid);
206         }
207         
208         // show success message
209         if ( $catid != intPostVar('catid') )
210         {
211                 bm_message($skin, _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>', '');
212         }
213         else
214         {
215                 bm_message($skin, _ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
216         }
217         
218         return;
219 }
220
221 function bm_loginAndPassThrough($skin, $action='add')
222 {
223         /*
224          * TODO: これを出力させる
225         $blogid = intRequestVar('blogid');
226         $itemid = intRequestVar('itemid');
227         $log_text = requestVar('logtext');
228         $log_link = requestVar('loglink');
229         $log_linktitle = requestVar('loglinktitle');
230         
231         echo '<input type="hidden" name="blogid" value="' . Entity::hsc($blogid). '" />' . "\n";
232         echo '<input type="hidden" name="itemid" value="' . Entity::hsc($itemid). '" />' . "\n";
233         echo '<input type="hidden" name="logtext" value="' . Entity::hsc($log_text) . '" />' . "\n";
234         echo '<input type="hidden" name="loglink" value="' . Entity::hsc($log_link) . '" />' . "\n";
235         echo '<input type="hidden" name="loglinktitle" value="' . Entity::hsc($log_linktitle) . '" />' . "\n";
236         echo "<input type=\"hidden\" name=\"nextaction\" value=\"{$action}\" />\n";
237         */
238         
239         $skin->parse('showlogin');
240         
241         return;
242 }
243
244 function bm_doShowForm($skin)
245 {
246         global $manager, $member;
247         
248         $blogid                 = intRequestVar('blogid');
249         $log_text               = trim(requestVar('logtext'));
250         $log_link               = requestVar('loglink');
251         $log_linktitle  = requestVar('loglinktitle');
252         
253         if ( !Blog::existsID($blogid) )
254         {
255                 bm_doError(_ERROR_NOSUCHBLOG);
256         }
257         else if ( !$member->isTeamMember($blogid) )
258         {
259                 bm_doError(_ERROR_NOTONTEAM);
260         }
261         
262         $blog =& $manager->getBlog($blogid);
263         
264         $logje = '';
265         
266         if ( $log_text )
267         {
268                 $logje .= '<blockquote><div>"' . Entity::hsc($log_text) . '"</div></blockquote>' . "\n";
269         }
270         
271         if ( !$log_linktitle )
272         {
273                 $log_linktitle = $log_link;
274         }
275         
276         if ( $log_link )
277         {
278                 $logje .= '<a href="' . Entity::hsc($log_link) . '">' . Entity::hsc($log_linktitle) . '</a>';
279         }
280         
281         $item = array();
282         $item['body'] = $logje;
283         $item['title'] = Entity::hsc($log_linktitle);
284         
285         $data = array(
286                 'blog'          => &$blog,
287                 'item'          => &$item,
288                 'contents'      => &$item
289         );
290         $manager->notify('PreAddItemForm', $data);
291         
292         if ( $blog->convertBreaks() )
293         {
294                 $item['body'] = removeBreaks($item['body']);
295         }
296         
297         Admin::$blog = &$blog;
298         Admin::$item = &$item;
299         
300         Admin::setAdminAction('createitem');
301         $skin->parse('createitem');
302         
303         return;
304 }
305
306 function bm_doEditForm($skin)
307 {
308         global $member, $manager;
309         
310         $itemid = intRequestVar('itemid');
311         
312         if ( !$manager->existsItem($itemid, 0, 0) )
313         {
314                 bm_doError(_ERROR_NOSUCHITEM);
315         }
316         else if ( !$member->canAlterItem($itemid) )
317         {
318                 bm_doError(_ERROR_DISALLOWED);
319         }
320         
321         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
322         $item =& $manager->getItem($itemid, 1, 1);
323         
324         $data = array(
325                 'blog' => &$blog,
326                 'item' => &$item
327         );
328         $manager->notify('PrepareItemForEdit', $data);
329         
330         if ( $blog->convertBreaks() )
331         {
332                 $item['body'] = removeBreaks($item['body']);
333                 $item['more'] = removeBreaks($item['more']);
334         }
335         
336         Admin::$blog = &$blog;
337         Admin::$item = &$item;
338         
339         Admin::setAdminAction('itemedit');
340         $skin->parse('itemedit');
341         
342         return;}
343
344 function bm_doError($skin, $msg)
345 {
346         bm_message($skin, _ERROR, _ERRORMSG, $msg);
347         die;
348 }
349
350 function bm_message($skin, $title, $head, $msg, $extrahead = '')
351 {
352         /* TODO: $title, $head, $msg, $extraheadを渡す */
353         $skin->parse('adminerrorpage');
354         
355         return;
356 }
357
358 function bm_doContextMenuCode($width=600, $height=500)
359 {
360         global $CONF;   
361         $blogid = (integer) intGetVar('blogid');
362         
363         echo "<script type=\"text/javascript\" defer=\"defer\">\n";
364         echo "<![CDATA[\n";
365         echo " doc = external.menuArguments.document;\n";
366         echo " lt = encodeURIComponent(doc.selection.createRange().text);\n";
367         echo " loglink = encodeURIComponent(external.menuArguments.location.href);\n";
368         echo " loglinktitle = encodeURIComponent(doc.title);\n";
369         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";
370         echo " wingm.focus()\n";
371         echo "]]>\n";
372         echo "</script>\n";
373 }