OSDN Git Service

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