OSDN Git Service

add specialSkinParts setting
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / bookmarklet.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 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-2007 The Nucleus Group
18  * @version $Id: bookmarklet.php,v 1.9 2007-02-04 06:28:45 kimitake Exp $
19  * $NucleusJP: bookmarklet.php,v 1.8 2007/02/03 05:41:29 kimitake Exp $
20  */
21
22 // bookmarklet is part of admin area (might need XML-RPC)
23 $CONF = array();
24 $CONF['UsingAdminArea'] = 1;
25
26 // include all classes and config data
27 include('../config.php');
28
29 $action = requestVar('action');
30
31 if ($action == 'contextmenucode') {
32         bm_doContextMenuCode();
33         exit;
34 }
35
36 if (!$member->isLoggedIn() ) {
37         bm_loginAndPassThrough();
38         exit;
39 }
40
41 // on successfull login
42 if ( ($action == 'login') && ($member->isLoggedIn() ) ) {
43         $action = requestVar('nextaction');
44 }
45
46 if ($action == '') {
47         $action = 'add';
48 }
49
50 sendContentType('application/xhtml+xml', 'bookmarklet-' . $action);
51
52 // check ticket
53 $action = strtolower($action);
54 $aActionsNotToCheck = array('login', 'add', 'edit');
55
56 if (!in_array($action, $aActionsNotToCheck) ) {
57
58         if (!$manager->checkTicket() ) {
59                 bm_doError(_ERROR_BADTICKET);
60         }
61
62 }
63
64 // find out what to do
65 switch ($action) {
66         // adds the item for real
67         case 'additem':
68                 bm_doAddItem();
69                 break;
70
71         // shows the edit item form
72         case 'edit':
73                 bm_doEditForm();
74                 break;
75
76         // edits the item for real
77         case 'edititem':
78                 bm_doEditItem();
79                 break;
80
81         // on login, 'action' gets changed to 'nextaction'
82         case 'login':
83                 bm_doError('Something went wrong');
84                 break;
85
86         // shows the fill in form
87         case 'add':
88         default:
89                 bm_doShowForm();
90                 break;
91 }
92
93 function bm_doAddItem() {
94         global $member, $manager, $CONF;
95
96         $manager->loadClass('ITEM');
97         $result = ITEM::createFromRequest();
98
99         if ($result['status'] == 'error') {
100                 bm_doError($result['message']);
101         }
102
103         $blogid = getBlogIDFromItemID($result['itemid']);
104         $blog =& $manager->getBlog($blogid);
105
106         if ($result['status'] == 'newcategory') {
107                 $message = 'アイテムは追加され、新しいカテゴリーが作成されました。 <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">ここをクリックしてカテゴリーの名前と説明を編集してください。</a>';
108                 $extrahead = '';
109         } elseif ((postVar('actiontype') == 'addnow') && $blog->pingUserland()) {
110                 $message = 'アイテムの追加に成功しました。現在weblogs.comにpingを送っています。しばらくの間お待ちください...';
111                 $pingUrl = $manager->addTicketToUrl($CONF['AdminURL'] . 'index.php?action=sendping&blogid=' . intval($blogid));
112                 $extrahead = '<meta http-equiv="refresh" content="1; url=' . htmlspecialchars($pingUrl). '" />';
113         } else {
114                 $message = _ITEM_ADDED;
115                 $extrahead = '';
116         }
117
118         bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
119 }
120
121 function bm_doEditItem() {
122         global $member, $manager, $CONF;
123
124         $itemid = intRequestVar('itemid');
125         $catid = postVar('catid');
126
127         // only allow if user is allowed to alter item
128         if (!$member->canUpdateItem($itemid, $catid) ) {
129                 bm_doError(_ERROR_DISALLOWED);
130         }
131
132         $body = postVar('body');
133         $title = postVar('title');
134         $more = postVar('more');
135         $closed = intPostVar('closed');
136         $actiontype = postVar('actiontype');
137         $draftid = intPostVar('draftid');
138
139         // redirect to admin area on delete (has delete confirmation)
140         if ($actiontype == 'delete') {
141                 redirect('index.php?action=itemdelete&itemid=' . $itemid);
142                 exit;
143         }
144
145         // create new category if needed (only on edit/changedate)
146         if (strstr($catid,'newcat') ) {
147                 // get blogid
148                 list($blogid) = sscanf($catid, "newcat-%d");
149
150                 // create
151                 $blog =& $manager->getBlog($blogid);
152                 $catid = $blog->createNewCategory();
153
154                 // show error when sth goes wrong
155                 if (!$catid) {
156                         bm_doError('Could not create new category');
157                 }
158         }
159
160         // only edit action is allowed for bookmarklet edit
161         switch ($actiontype) {
162                 case 'changedate':
163                         $publish = 1;
164                         $wasdraft = 0;
165                         $timestamp = mktime(postVar('hour'), postVar('minutes'), 0, postVar('month'), postVar('day'), postVar('year') );
166                         break;
167                 case 'edit':
168                         $publish = 1;
169                         $wasdraft = 0;
170                         $timestamp = 0;
171                         break;
172                 default:
173                         bm_doError('Something went wrong');
174         }
175
176         // update item for real
177         ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
178
179         if ($draftid > 0) {
180                 ITEM::delete($draftid);
181         }
182
183         // show success message
184         if ($catid != intPostVar('catid') ) {
185                 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, 'アイテムは追加され、新しいカテゴリーが作成されました。<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">ここをクリックしてカテゴリーの名前と説明を編集してください。</a>', '');
186         } else {
187                 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
188         }
189 }
190
191 function bm_loginAndPassThrough() {
192
193         $blogid = intRequestVar('blogid');
194         $log_text = requestVar('logtext');
195         $log_link = requestVar('loglink');
196         $log_linktitle = requestVar('loglinktitle');
197
198         ?>
199 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
200         <html xmlns="http://www.w3.org/1999/xhtml">
201         <head>
202                 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
203                 <title>Nucleus</title>
204                 <?php bm_style(); ?>
205         </head>
206         <body>
207         <h1><?php echo _LOGIN_PLEASE ?></h1>
208
209         <form method="post" action="bookmarklet.php">
210         <p>
211                 <input name="action" value="login" type="hidden" />
212                 <input name="blogid" value="<?php echo htmlspecialchars($blogid); ?>" type="hidden" />
213                 <input name="logtext" value="<?php echo htmlspecialchars($log_text); ?>" type="hidden" />
214                 <input name="loglink" value="<?php echo htmlspecialchars($log_link); ?>" type="hidden" />
215                 <input name="loglinktitle" value="<?php echo htmlspecialchars($log_linktitle); ?>" type="hidden" />
216                 <?php echo _LOGINFORM_NAME ?>:
217                 <br /><input name="login" />
218                 <br /><?php echo _LOGINFORM_PWD ?>:
219                 <br /><input name="password" type="password" />
220                 <br /><br />
221                 <br /><input type="submit" value="<?php echo _LOGIN ?>" />
222         </p>
223         </form>
224         <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE ?></a></p>
225         </body>
226         </html>
227         <?php
228 }
229
230 function bm_doShowForm() {
231         global $member;
232
233         $blogid = intRequestVar('blogid');
234         $log_text = trim(requestVar('logtext'));
235         $log_link = requestVar('loglink');
236         $log_linktitle = requestVar('loglinktitle');
237
238         $log_text = uniDecode($log_text,_CHARSET);
239         $log_linktitle = uniDecode($log_linktitle,_CHARSET);
240         
241         if (!BLOG::existsID($blogid))
242                 bm_doError(_ERROR_NOSUCHBLOG);
243
244         if (!$member->isTeamMember($blogid) ) {
245                 bm_doError(_ERROR_NOTONTEAM);
246         }
247
248         $logje = '';
249
250         if ($log_text) {
251                 $logje .= '<blockquote><div>"' . htmlspecialchars($log_text) . '"</div></blockquote>' . "\n";
252         }
253
254         if (!$log_linktitle) {
255                 $log_linktitle = $log_link;
256         }
257
258         if ($log_link) {
259                 $logje .= '<a href="' . htmlspecialchars($log_link) . '">' . htmlspecialchars($log_linktitle) . '</a>';
260         }
261
262         $item['body'] = $logje;
263         $item['title'] = htmlspecialchars($log_linktitle);
264
265         $factory = new PAGEFACTORY($blogid);
266         $factory->createAddForm('bookmarklet', $item);
267 }
268
269 function bm_doEditForm() {
270         global $member, $manager;
271
272         $itemid = intRequestVar('itemid');
273
274         if (!$manager->existsItem($itemid, 0, 0) ) {
275                 bm_doError(_ERROR_NOSUCHITEM);
276         }
277
278         if (!$member->canAlterItem($itemid) ) {
279                 bm_doError(_ERROR_DISALLOWED);
280         }
281
282         $item =& $manager->getItem($itemid, 1, 1);
283         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
284
285         $manager->notify('PrepareItemForEdit', array('item' => &$item) );
286
287         if ($blog->convertBreaks() ) {
288                 $item['body'] = removeBreaks($item['body']);
289                 $item['more'] = removeBreaks($item['more']);
290         }
291
292         $formfactory = new PAGEFACTORY($blog->getID() );
293         $formfactory->createEditForm('bookmarklet', $item);
294 }
295
296 function bm_doError($msg) {
297         bm_message(_ERROR, _ERRORMSG, $msg);
298         die;
299 }
300
301 function bm_message($title, $head, $msg, $extrahead = '') {
302         ?>
303 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
304         <html xmlns="http://www.w3.org/1999/xhtml">
305         <head>
306                 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
307                 <title><?php echo  $title ?></title>
308                 <?php bm_style(); ?>
309                 <?php echo $extrahead; ?>
310         </head>
311         <body>
312         <h1><?php echo $head; ?></h1>
313         <p><?php echo $msg; ?></p>
314         <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE ?></a></p>
315         </body>
316         </html>
317
318         <?php
319 }
320
321 function bm_style() {
322         echo '<link rel="stylesheet" type="text/css" href="styles/bookmarklet.css" />';
323         echo '<link rel="stylesheet" type="text/css" href="styles/addedit.css" />';
324 }
325
326 function bm_doContextMenuCode() {
327         global $CONF;
328         ?>
329 <script type="text/javascript" defer="defer">
330 doc = external.menuArguments.document;
331 lt = escape(doc.selection.createRange().text);
332 loglink = escape(external.menuArguments.location.href);
333 loglinktitle = escape(doc.title);
334 wingm = window.open('<?php echo $CONF['AdminURL']?>bookmarklet.php?blogid=<?php echo intGetVar('blogid')?>&logtext=' + lt + '&loglink=' + loglink + '&loglinktitle=' + loglinktitle, 'nucleusbm', 'scrollbars=yes,width=600,height=500,left=10,top=10,status=yes,resizable=yes');
335 wingm.focus();
336 </script>
337         <?php
338 }
339
340 function uniDecode($str,$charcode){
341   $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8,$str);
342   return mb_convert_encoding($text, $charcode, 'UTF-8');
343 }
344 function toUtf8($ar){
345   foreach($ar as $val){
346     $val = intval(substr($val,2),16);
347     if($val < 0x7F){        // 0000-007F
348         $c .= chr($val);
349     }elseif($val < 0x800) { // 0080-0800
350         $c .= chr(0xC0 | ($val / 64));
351         $c .= chr(0x80 | ($val % 64));
352     }else{                // 0800-FFFF
353         $c .= chr(0xE0 | (($val / 64) / 64));
354         $c .= chr(0x80 | (($val / 64) % 64));
355         $c .= chr(0x80 | ($val % 64));
356     }
357   }
358   return $c;
359 }
360
361 ?>