OSDN Git Service

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