OSDN Git Service

Merge branch 'skinnable-master'
[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 <<<<<<< HEAD
29 =======
30 // get skin object
31 $skinid = $member->bookmarklet;
32 if ( !Skin::existsID($skinid) )
33 {
34         $skinid = $CONF['BookmarkletSkin'];
35         if ( !Skin::existsID($skinid) )
36         {
37                 sendContentType('text/html');
38                 echo _ERROR_SKIN;
39                 exit;
40         }
41 }
42 $skin =& $manager->getSkin($skinid, 'AdminActions', 'AdminSkin');
43
44 >>>>>>> skinnable-master
45 // check logged-in or pass through
46 $action = requestVar('action');
47 if ( !$member->isLoggedIn() )
48 {
49 <<<<<<< HEAD
50         bm_loginAndPassThrough($action);
51 =======
52         bm_loginAndPassThrough($skin, $action);
53 >>>>>>> skinnable-master
54         exit;
55 }
56 else if ( $action == 'login')
57 {
58         $action = requestVar('nextaction');
59 }
60
61 $action = strtolower($action);
62
63 if ( $action == 'contextmenucode' )
64 {
65         bm_doContextMenuCode();
66         exit;
67 }
68 else if ( $action == '' )
69 {
70         $action = 'add';
71 }
72
73 <<<<<<< HEAD
74 // send HTTP 1.1 message header for Content-Type
75 sendContentType('text/html', 'bookmarklet-' . $action);
76
77 =======
78 >>>>>>> skinnable-master
79 // check ticket
80 $aActionsNotToCheck = array('login', 'add', 'edit');
81 if ( !in_array($action, $aActionsNotToCheck) )
82 {
83         if ( !$manager->checkTicket() )
84         {
85                 bm_doError($skin, _ERROR_BADTICKET);
86         }
87 }
88
89 // find out what to do
90 switch ( $action )
91 {
92         // adds the item for real
93         case 'additem':
94                 bm_doAddItem($skin);
95                 break;
96         
97         // shows the edit item form
98         case 'edit':
99                 bm_doEditForm($skin);
100                 break;
101         
102         // edits the item for real
103         case 'edititem':
104                 bm_doEditItem($skin);
105                 break;
106         
107         // on login, 'action' gets changed to 'nextaction'
108         case 'login':
109                 bm_doError($skin, 'Something went wrong');
110                 break;
111         
112         // shows the fill in form
113         case 'add':
114         default:
115                 bm_doShowForm($skin);
116                 break;
117 }
118
119 function bm_doAddItem($skin)
120 {
121         global $member, $manager, $CONF;
122         
123         $manager->loadClass('ITEM');
124         $result = Item::createFromRequest();
125         
126         if ( $result['status'] == 'error' )
127         {
128                 bm_doError($skin, $result['message']);
129         }
130         
131         $blogid = getBlogIDFromItemID($result['itemid']);
132         $blog =& $manager->getBlog($blogid);
133         
134         if ( $result['status'] == 'newcategory' )
135         {
136                 $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>';
137                 $extrahead = '';
138         }
139         else
140         {
141                 $message = _ITEM_ADDED;
142                 $extrahead = '';
143         }
144         
145 <<<<<<< HEAD
146         bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
147 =======
148         bm_message($skin, _ITEM_ADDED, $message,$extrahead);
149 >>>>>>> skinnable-master
150         
151         return;
152 }
153
154 function bm_doEditItem($skin)
155 {
156         global $member, $manager, $CONF;
157         
158         $itemid = intRequestVar('itemid');
159         $catid = postVar('catid');
160         
161         // only allow if user is allowed to alter item
162         if ( !$member->canUpdateItem($itemid, $catid) )
163         {
164                 bm_doError($skin, _ERROR_DISALLOWED);
165         }
166         
167         $body = postVar('body');
168         $title = postVar('title');
169         $more = postVar('more');
170         $closed = intPostVar('closed');
171         $actiontype = postVar('actiontype');
172         $draftid = intPostVar('draftid');
173         
174         // redirect to admin area on delete (has delete confirmation)
175         if ( $actiontype == 'delete' )
176         {
177                 redirect('index.php?action=itemdelete&itemid=' . $itemid);
178                 exit;
179         }
180         
181         // create new category if needed (only on edit/changedate)
182         if ( i18n::strpos($catid,'newcat') === 0 )
183         {
184                 // get blogid
185                 list($blogid) = sscanf($catid, "newcat-%d");
186                 
187                 // create
188                 $blog =& $manager->getBlog($blogid);
189                 $catid = $blog->createNewCategory();
190                 
191                 // show error when sth goes wrong
192                 if ( !$catid )
193                 {
194                         bm_doError($skin, 'Could not create new category');
195                 }
196         }
197         
198         // only edit action is allowed for bookmarklet edit
199         switch ( $actiontype )
200         {
201                 case 'changedate':
202                         $publish = 1;
203                         $wasdraft = 0;
204                         $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
205                         break;
206                 case 'edit':
207                         $publish = 1;
208                         $wasdraft = 0;
209                         $timestamp = 0;
210                         break;
211                 case 'backtodrafts':
212                         $publish = 0;
213                         $wasdraft = 0;
214                         $timestamp = 0;
215                         break;
216                 default:
217                         bm_doError($skin, 'Something went wrong');
218         }
219         
220         // update item for real
221         Item::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
222         
223         if ( $draftid > 0 )
224         {
225                 Item::delete($draftid);
226         }
227         
228         if ( $result['status'] == 'newcategory' )
229         {
230                 $href           = "index.php?action=categoryedit&amp;blogid={$blogid}&amp;catid={$result['catid']}";
231                 $onclick        = 'if (event &amp;&amp; event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
232                 $title          = _BOOKMARKLET_NEW_WINDOW;
233                 $aTag           = " <a href=\"{$href}\" onclick=\"{$onclick}\" title=\"{$title}\">";
234                 $message        = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
235         }
236         else
237         {
238                 $message = _ITEM_ADDED;
239         }
240         
241 <<<<<<< HEAD
242         return;
243 }
244
245 function bm_loginAndPassThrough($action='add')
246 =======
247         // show success message
248         bm_message($skin, _ITEM_ADDED, $message, '');
249         return;
250 }
251
252 function bm_loginAndPassThrough($skin, $action='add')
253 >>>>>>> skinnable-master
254 {
255         /*
256          * TODO: これを出力させる
257         $blogid = intRequestVar('blogid');
258         $itemid = intRequestVar('itemid');
259         $log_text = requestVar('logtext');
260         $log_link = requestVar('loglink');
261         $log_linktitle = requestVar('loglinktitle');
262         
263 <<<<<<< HEAD
264         echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
265         echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
266         echo "<head>\n";
267         echo "<title>Nucleus CMS Bookmarklet</title>\n";
268         
269         bm_style();
270         
271         echo "</head>\n";
272         echo "<body>\n";
273         echo '<h1>' . _LOGIN_PLEASE . "</h1>\n";
274         echo "<form method=\"post\" action=\"bookmarklet.php\">\n";
275         echo "<p>\n";
276         echo _LOGINFORM_NAME . "<input type=\"text\" name=\"login\" value=\"\" /><br />\n";
277         echo _LOGINFORM_PWD . "<input type=\"password\" name=\"password\" value=\"\" /><br />\n";
278 =======
279 >>>>>>> skinnable-master
280         echo '<input type="hidden" name="blogid" value="' . Entity::hsc($blogid). '" />' . "\n";
281         echo '<input type="hidden" name="itemid" value="' . Entity::hsc($itemid). '" />' . "\n";
282         echo '<input type="hidden" name="logtext" value="' . Entity::hsc($log_text) . '" />' . "\n";
283         echo '<input type="hidden" name="loglink" value="' . Entity::hsc($log_link) . '" />' . "\n";
284         echo '<input type="hidden" name="loglinktitle" value="' . Entity::hsc($log_linktitle) . '" />' . "\n";
285         echo "<input type=\"hidden\" name=\"nextaction\" value=\"{$action}\" />\n";
286 <<<<<<< HEAD
287         echo '<button type="submit" name="action" value="login">' . _LOGIN . "</button>\n";
288         echo "</p>\n";
289         echo "</form>\n";
290         echo '<p><a href="bookmarklet.php" onclick="window.close();">' . _POPUP_CLOSE . "</a></p>\n";
291         echo "</body>\n";
292         echo "</html>\n";
293 =======
294         */
295         
296         $skin->parse('showlogin');
297 >>>>>>> skinnable-master
298         
299         return;
300 }
301
302 function bm_doShowForm($skin)
303 {
304         global $manager, $member;
305         
306         $blogid                 = intRequestVar('blogid');
307         $log_text               = trim(requestVar('logtext'));
308         $log_link               = requestVar('loglink');
309         $log_linktitle  = requestVar('loglinktitle');
310         
311         if ( !Blog::existsID($blogid) )
312         {
313                 bm_doError($skin, _ERROR_NOSUCHBLOG);
314         }
315 <<<<<<< HEAD
316         else
317         {
318                 $blog =& $manager->getBlog($blogid);
319         }
320         
321         if ( !$member->isTeamMember($blogid) )
322 =======
323         else if ( !$member->isTeamMember($blogid) )
324 >>>>>>> skinnable-master
325         {
326                 bm_doError($skin, _ERROR_NOTONTEAM);
327         }
328         
329         $blog =& $manager->getBlog($blogid);
330         
331         $logje = '';
332         
333         if ( $log_text )
334         {
335                 $logje .= '<blockquote><div>"' . Entity::hsc($log_text) . '"</div></blockquote>' . "\n";
336         }
337         
338         if ( !$log_linktitle )
339         {
340                 $log_linktitle = $log_link;
341         }
342         
343         if ( $log_link )
344         {
345                 $logje .= '<a href="' . Entity::hsc($log_link) . '">' . Entity::hsc($log_linktitle) . '</a>';
346         }
347         
348 <<<<<<< HEAD
349         $variables = array();
350         $variables['body'] = $logje;
351         $variables['title'] = Entity::hsc($log_linktitle);
352         
353         $handler = new PageFactory($blog);
354         $handler->setVariables($variables);
355         
356         $contents = $handler->getTemplateFor('bookmarklet', 'add');
357         $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$blog));
358         
359         $parser = new Parser($handler);
360         $parser->parse($contents);
361         
362 =======
363         $item = array();
364         $item['body'] = $logje;
365         $item['title'] = Entity::hsc($log_linktitle);
366         
367         $data = array(
368                 'blog'          => &$blog,
369                 'item'          => &$item,
370                 'contents'      => &$item
371         );
372         $manager->notify('PreAddItemForm', $data);
373         
374         if ( $blog->convertBreaks() )
375         {
376                 $item['body'] = removeBreaks($item['body']);
377         }
378         
379         Admin::$blog = &$blog;
380         Admin::$contents = &$item;
381         
382         Admin::$action = 'createitem';
383         $skin->parse('createitem');
384         
385 >>>>>>> skinnable-master
386         return;
387 }
388
389 function bm_doEditForm($skin)
390 {
391         global $member, $manager;
392         
393         $itemid = intRequestVar('itemid');
394         
395         if ( !$manager->existsItem($itemid, 0, 0) )
396         {
397                 bm_doError($skin, _ERROR_NOSUCHITEM);
398         }
399         else if ( !$member->canAlterItem($itemid) )
400         {
401                 bm_doError($skin, _ERROR_DISALLOWED);
402         }
403         
404 <<<<<<< HEAD
405         $variables =& $manager->getItem($itemid, 1, 1);
406 =======
407 >>>>>>> skinnable-master
408         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
409         $item =& $manager->getItem($itemid, 1, 1);
410         
411 <<<<<<< HEAD
412         $manager->notify('PrepareItemForEdit', array('item' => &$variables) );
413 =======
414         $data = array(
415                 'blog' => &$blog,
416                 'item' => &$item
417         );
418         $manager->notify('PrepareItemForEdit', $data);
419 >>>>>>> skinnable-master
420         
421         if ( $blog->convertBreaks() )
422         {
423                 $variables['body'] = removeBreaks($variables['body']);
424                 $variables['more'] = removeBreaks($variables['more']);
425         }
426         
427 <<<<<<< HEAD
428         $handler = new PageFactory($blog);
429         $handler->setVariables($variables);
430         
431         $contents = $handler->getTemplateFor('bookmarklet', 'edit');
432         
433         $parser = new Parser($handler);
434         $parser->parse($contents);
435         
436         return;
437 }
438 =======
439         Admin::$blog = &$blog;
440         Admin::$contents = &$item;
441         
442         Admin::$action = 'itemedit';
443         $skin->parse('itemedit');
444         
445         return;}
446 >>>>>>> skinnable-master
447
448 function bm_doError($skin, $msg)
449 {
450         bm_message($skin, _ERRORMSG, $msg);
451         die;
452 }
453
454 function bm_message($skin, $title, $msg, $extrahead = '')
455 {
456 <<<<<<< HEAD
457         echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
458         echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
459         echo "<head>\n";
460         echo "<title>{$title}</title>\n";
461         
462         bm_style();
463         
464         echo $extrahead . "\n";
465         echo "</head>\n";
466         echo "<body>\n";
467         echo "<h1>{$head}</h1>\n";
468         echo "<p>{$msg}</p>\n";
469         echo '<p><a href="bookmarklet.php" onclick="window.close();window.opener.location.reload();">' . _POPUP_CLOSE . "</a></p>\n";
470         echo "</body>\n";
471         echo "</html>\n";
472 =======
473         Admin::$extrahead = $extrahead;
474         Admin::$headMess = $msg;
475         $skin->parse('adminerrorpage');
476 >>>>>>> skinnable-master
477         
478         return;
479 }
480
481 function bm_doContextMenuCode($width=600, $height=500)
482 {
483 <<<<<<< HEAD
484         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/bookmarklet.css\" />\n";
485         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/addedit.css\" />\n";
486         return;
487 }
488
489 function bm_doContextMenuCode($width=600, $height=500)
490 {
491         global $CONF;
492         
493 =======
494         global $CONF;   
495 >>>>>>> skinnable-master
496         $blogid = (integer) intGetVar('blogid');
497         
498         echo "<script type=\"text/javascript\" defer=\"defer\">\n";
499         echo "<![CDATA[\n";
500         echo " doc = external.menuArguments.document;\n";
501         echo " lt = encodeURIComponent(doc.selection.createRange().text);\n";
502         echo " loglink = encodeURIComponent(external.menuArguments.location.href);\n";
503         echo " loglinktitle = encodeURIComponent(doc.title);\n";
504         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";
505         echo " wingm.focus()\n";
506         echo "]]>\n";
507         echo "</script>\n";
508 }