OSDN Git Service

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