OSDN Git Service

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