3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2006 The Nucleus Group
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)
13 * This script allows adding items to Nucleus through bookmarklets. The member must be logged in
14 * in order to use this.
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.7 2006-07-17 20:01:39 kimitake Exp $
19 * $NucleusJP: bookmarklet.php,v 1.6 2005/08/13 07:33:58 kimitake Exp $
22 // bookmarklet is part of admin area (might need XML-RPC)
24 $CONF['UsingAdminArea'] = 1;
26 // include all classes and config data
27 include('../config.php');
29 $action = requestVar('action');
31 if ($action == 'contextmenucode') {
32 bm_doContextMenuCode();
36 if (!$member->isLoggedIn()) {
37 bm_loginAndPassThrough();
41 // on successfull login
42 if (($action == 'login') && ($member->isLoggedIn()))
43 $action = requestVar('nextaction');
47 sendContentType('application/xhtml+xml', 'bookmarklet-'.$action);
50 $action = strtolower($action);
51 $aActionsNotToCheck = array('login', 'add', 'edit');
52 if (!in_array($action, $aActionsNotToCheck))
54 if (!$manager->checkTicket())
55 bm_doError(_ERROR_BADTICKET);
59 // find out what to do
62 bm_doAddItem(); // adds the item for real
65 bm_doEditForm(); // shows the edit item form
67 case 'edititem': // edits the item for real
70 case 'login': // on login, 'action' gets changed to 'nextaction'
71 bm_doError('Something went wrong');
75 bm_doShowForm(); // shows the fill in form
79 function bm_doAddItem() {
80 global $member, $manager, $CONF;
82 $manager->loadClass('ITEM');
83 $result = ITEM::createFromRequest();
85 if ($result['status'] == 'error')
86 bm_doError($result['message']);
88 $blogid = getBlogIDFromItemID($result['itemid']);
89 $blog =& $manager->getBlog($blogid);
91 if ($result['status'] == 'newcategory') {
92 $message = 'Item was added, and a new category was created. <a href="index.php?action=categoryedit&blogid='.$blogid.'&catid='.$result['catid'].'" onclick="if (event && 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>';
94 } elseif ((postVar('actiontype') == 'addnow') && $blog->pingUserland()) {
95 $message = 'Item was added successfully. Now pinging weblogs.com. Please hold on... (can take a while)';
96 $pingUrl = $manager->addTicketToUrl($CONF['AdminURL'] . 'index.php?action=sendping&blogid=' . intval($blogid));
97 $extrahead = '<meta http-equiv="refresh" content="1; url=' . htmlspecialchars($pingUrl). '" />';
99 $message = _ITEM_ADDED;
103 bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
106 function bm_doEditItem() {
107 global $member, $manager, $CONF;
109 $itemid = intRequestVar('itemid');
110 $catid = postVar('catid');
112 // only allow if user is allowed to alter item
113 if (!$member->canUpdateItem($itemid, $catid))
114 bm_doError(_ERROR_DISALLOWED);
116 $body = postVar('body');
117 $title = postVar('title');
118 $more = postVar('more');
119 $closed = intPostVar('closed');
120 $actiontype = postVar('actiontype');
122 $draftid = intPostVar('draftid');
124 // redirect to admin area on delete (has delete confirmation)
125 if ($actiontype == 'delete') {
126 redirect('index.php?action=itemdelete&itemid='.$itemid);
130 // create new category if needed (only on edit/changedate)
131 if (strstr($catid,'newcat')) {
133 list($blogid) = sscanf($catid,"newcat-%d");
136 $blog =& $manager->getBlog($blogid);
137 $catid = $blog->createNewCategory();
139 // show error when sth goes wrong
141 bm_doError('Could not create new category');
144 // only edit action is allowed for bookmarklet edit
145 switch ($actiontype) {
149 $timestamp = mktime(postVar('hour'), postVar('minutes'), 0, postVar('month'), postVar('day'), postVar('year'));
157 bm_doError('Something went wrong');
160 // update item for real
161 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
164 ITEM::delete($draftid);
167 // show success message
168 if ($catid != intPostVar('catid'))
169 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, 'Item was added, and a new category was created. <a href="index.php?action=categoryedit&blogid='.$blog->getID().'&catid='.$catid.'" onclick="if (event && 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>', '');
171 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
174 function bm_loginAndPassThrough() {
176 $blogid = intRequestVar('blogid');
177 $log_text = requestVar('logtext');
178 $log_link = requestVar('loglink');
179 $log_linktitle = requestVar('loglinktitle');
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">
185 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
186 <title>Nucleus</title>
190 <h1><?php echo _LOGIN_PLEASE?></h1>
192 <form method="post" action="bookmarklet.php">
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" />
204 <br /><input type="submit" value="<?php echo _LOGIN?>" />
207 <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
213 function bm_doShowForm() {
216 $blogid = intRequestVar('blogid');
217 $log_text = trim(requestVar('logtext'));
218 $log_link = requestVar('loglink');
219 $log_linktitle = requestVar('loglinktitle');
221 $log_text = uniDecode($log_text,_CHARSET);
222 $log_linktitle = uniDecode($log_linktitle,_CHARSET);
224 if (!BLOG::existsID($blogid))
225 bm_doError(_ERROR_NOSUCHBLOG);
227 if (!$member->isTeamMember($blogid))
228 bm_doError(_ERROR_NOTONTEAM);
232 $logje .= '<blockquote><div>"' . htmlspecialchars($log_text) .'"</div></blockquote>' . "\n";
234 $log_linktitle = $log_link;
236 $logje .= '<a href="'. htmlspecialchars($log_link) . '">'. htmlspecialchars($log_linktitle).'</a>';
239 $item['body'] = $logje;
240 $item['title'] = htmlspecialchars($log_linktitle);
242 $factory = new PAGEFACTORY($blogid);
243 $factory->createAddForm('bookmarklet',$item);
246 function bm_doEditForm() {
247 global $member, $manager;
249 $itemid = intRequestVar('itemid');
251 if (!$manager->existsItem($itemid, 0, 0))
252 bm_doError(_ERROR_NOSUCHITEM);
254 if (!$member->canAlterItem($itemid))
255 bm_doError(_ERROR_DISALLOWED);
257 $item =& $manager->getItem($itemid,1,1);
258 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
260 $manager->notify('PrepareItemForEdit', array('item' => &$item));
262 if ($blog->convertBreaks()) {
263 $item['body'] = removeBreaks($item['body']);
264 $item['more'] = removeBreaks($item['more']);
267 $formfactory = new PAGEFACTORY($blog->getID());
268 $formfactory->createEditForm('bookmarklet',$item);
272 function bm_doError($msg) {
273 bm_message(_ERROR,_ERRORMSG,$msg);
277 function bm_message($title, $head, $msg, $extrahead = '') {
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">
282 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
283 <title><?php echo $title ?></title>
285 <?php echo $extrahead?>
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>
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" />';
301 function bm_doContextMenuCode() {
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');
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');
319 function toUtf8($ar){
320 foreach($ar as $val){
321 $val = intval(substr($val,2),16);
322 if($val < 0x7F){ // 0000-007F
324 }elseif($val < 0x800) { // 0080-0800
325 $c .= chr(0xC0 | ($val / 64));
326 $c .= chr(0x80 | ($val % 64));
328 $c .= chr(0xE0 | (($val / 64) / 64));
329 $c .= chr(0x80 | (($val / 64) % 64));
330 $c .= chr(0x80 | ($val % 64));