3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 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-2009 The Nucleus Group
19 * $NucleusJP: bookmarklet.php,v 1.9.2.1 2007/09/07 07:16:39 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');
50 sendContentType('text/html', 'bookmarklet-' . $action);
53 $action = strtolower($action);
54 $aActionsNotToCheck = array('login', 'add', 'edit');
56 if (!in_array($action, $aActionsNotToCheck) ) {
58 if (!$manager->checkTicket() ) {
59 bm_doError(_ERROR_BADTICKET);
64 // find out what to do
66 // adds the item for real
71 // shows the edit item form
76 // edits the item for real
81 // on login, 'action' gets changed to 'nextaction'
83 bm_doError('Something went wrong');
86 // shows the fill in form
93 function bm_doAddItem() {
94 global $member, $manager, $CONF;
96 $manager->loadClass('ITEM');
97 $result = ITEM::createFromRequest();
99 if ($result['status'] == 'error') {
100 bm_doError($result['message']);
103 $blogid = getBlogIDFromItemID($result['itemid']);
104 $blog =& $manager->getBlog($blogid);
106 if ($result['status'] == 'newcategory') {
107 $href = 'index.php?action=categoryedit&blogid=' . $blogid . '&catid=' . $result['catid'];
108 $onclick = 'if (event && event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
109 $title = _BOOKMARKLET_NEW_WINDOW;
110 $aTag = ' <a href="' . $href . '" onclick="' . $onclick . '" title="' . $title . '">';
111 $message = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
113 } elseif ( (postVar('actiontype') == 'addnow') && $blog->sendPing() ) {
114 $message = _BOOKMARKLET_SEND_PING;
115 $pingUrl = $manager->addTicketToUrl($CONF['AdminURL'] . 'index.php?action=sendping&blogid=' . intval($blogid) );
116 $extrahead = '<meta http-equiv="refresh" content="1; url=' . htmlspecialchars($pingUrl) . '" />';
118 $message = _ITEM_ADDED;
122 bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
125 function bm_doEditItem() {
126 global $member, $manager, $CONF;
128 $itemid = intRequestVar('itemid');
129 $catid = postVar('catid');
131 // only allow if user is allowed to alter item
132 if (!$member->canUpdateItem($itemid, $catid) ) {
133 bm_doError(_ERROR_DISALLOWED);
136 $body = postVar('body');
137 $title = postVar('title');
138 $more = postVar('more');
139 $closed = intPostVar('closed');
140 $actiontype = postVar('actiontype');
141 $draftid = intPostVar('draftid');
143 // redirect to admin area on delete (has delete confirmation)
144 if ($actiontype == 'delete') {
145 redirect('index.php?action=itemdelete&itemid=' . $itemid);
149 // create new category if needed (only on edit/changedate)
150 if (strstr($catid,'newcat') ) {
152 list($blogid) = sscanf($catid, "newcat-%d");
155 $blog =& $manager->getBlog($blogid);
156 $catid = $blog->createNewCategory();
158 // show error when sth goes wrong
160 bm_doError('Could not create new category');
164 // only edit action is allowed for bookmarklet edit
165 switch ($actiontype) {
169 $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
182 bm_doError('Something went wrong');
185 // update item for real
186 ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
189 ITEM::delete($draftid);
192 // show success message
193 if ($catid != intPostVar('catid') ) {
194 $href = 'index.php?action=categoryedit&blogid=' . $blog->getID() . '&catid=' . $catid;
195 $onclick = 'if (event && event.preventDefault) event.preventDefault(); window.open(this.href); return false;';
196 $title = _BOOKMARKLET_NEW_WINDOW;
197 $aTag = ' <a href="' . $href . '" onclick="' . $onclick . '" title="' . $title . '">';
198 $message = _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>';
199 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _BOOKMARKLET_NEW_CATEGORY . $aTag . _BOOKMARKLET_NEW_CATEGORY_EDIT . '</a>', '');
201 bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
205 function bm_loginAndPassThrough() {
207 $blogid = intRequestVar('blogid');
208 $log_text = requestVar('logtext');
209 $log_link = requestVar('loglink');
210 $log_linktitle = requestVar('loglinktitle');
213 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
214 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
216 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
217 <title>Nucleus</title>
221 <h1><?php echo _LOGIN_PLEASE ?></h1>
223 <form method="post" action="bookmarklet.php">
225 <input name="action" value="login" type="hidden" />
226 <input name="blogid" value="<?php echo htmlspecialchars($blogid); ?>" type="hidden" />
227 <input name="logtext" value="<?php echo htmlspecialchars($log_text); ?>" type="hidden" />
228 <input name="loglink" value="<?php echo htmlspecialchars($log_link); ?>" type="hidden" />
229 <input name="loglinktitle" value="<?php echo htmlspecialchars($log_linktitle); ?>" type="hidden" />
230 <?php echo _LOGINFORM_NAME ?>:
231 <br /><input name="login" />
232 <br /><?php echo _LOGINFORM_PWD ?>:
233 <br /><input name="password" type="password" />
235 <br /><input type="submit" value="<?php echo _LOGIN ?>" />
238 <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE ?></a></p>
244 function bm_doShowForm() {
247 $blogid = intRequestVar('blogid');
248 $log_text = trim(requestVar('logtext'));
249 $log_link = requestVar('loglink');
250 $log_linktitle = requestVar('loglinktitle');
252 $log_text = uniDecode($log_text,_CHARSET);
253 $log_linktitle = uniDecode($log_linktitle,_CHARSET);
255 if (!BLOG::existsID($blogid) ) {
256 bm_doError(_ERROR_NOSUCHBLOG);
259 if (!$member->isTeamMember($blogid) ) {
260 bm_doError(_ERROR_NOTONTEAM);
266 $logje .= '<blockquote><div>"' . htmlspecialchars($log_text) . '"</div></blockquote>' . "\n";
269 if (!$log_linktitle) {
270 $log_linktitle = $log_link;
274 $logje .= '<a href="' . htmlspecialchars($log_link) . '">' . htmlspecialchars($log_linktitle) . '</a>';
277 $item['body'] = $logje;
278 $item['title'] = htmlspecialchars($log_linktitle);
280 $factory = new PAGEFACTORY($blogid);
281 $factory->createAddForm('bookmarklet', $item);
284 function bm_doEditForm() {
285 global $member, $manager;
287 $itemid = intRequestVar('itemid');
289 if (!$manager->existsItem($itemid, 0, 0) ) {
290 bm_doError(_ERROR_NOSUCHITEM);
293 if (!$member->canAlterItem($itemid) ) {
294 bm_doError(_ERROR_DISALLOWED);
297 $item =& $manager->getItem($itemid, 1, 1);
298 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
300 $manager->notify('PrepareItemForEdit', array('item' => &$item) );
302 if ($blog->convertBreaks() ) {
303 $item['body'] = removeBreaks($item['body']);
304 $item['more'] = removeBreaks($item['more']);
307 $formfactory = new PAGEFACTORY($blog->getID() );
308 $formfactory->createEditForm('bookmarklet', $item);
311 function bm_doError($msg) {
312 bm_message(_ERROR, _ERRORMSG, $msg);
316 function bm_message($title, $head, $msg, $extrahead = '') {
318 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
319 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
321 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
322 <title><?php echo $title ?></title>
324 <?php echo $extrahead; ?>
327 <h1><?php echo $head; ?></h1>
328 <p><?php echo $msg; ?></p>
329 <p><a href="bookmarklet.php" onclick="window.close();window.opener.location.reload();"><?php echo _POPUP_CLOSE ?></a></p>
336 function bm_style() {
337 echo '<link rel="stylesheet" type="text/css" href="styles/bookmarklet.css" />';
338 echo '<link rel="stylesheet" type="text/css" href="styles/addedit.css" />';
341 function bm_doContextMenuCode() {
344 <script type="text/javascript" defer="defer">
345 doc = external.menuArguments.document;
346 lt = escape(doc.selection.createRange().text);
347 loglink = escape(external.menuArguments.location.href);
348 loglinktitle = escape(doc.title);
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');
355 function uniDecode($str,$charcode){
356 $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/", 'toUtf8', $str);
357 return mb_convert_encoding($text, $charcode, 'UTF-8');
359 function toUtf8($ar){
360 foreach($ar as $val){
361 $val = intval(substr($val,2),16);
362 if($val < 0x7F){ // 0000-007F
364 }elseif($val < 0x800) { // 0080-0800
365 $c .= chr(0xC0 | ($val / 64));
366 $c .= chr(0x80 | ($val % 64));
368 $c .= chr(0xE0 | (($val / 64) / 64));
369 $c .= chr(0x80 | (($val / 64) % 64));
370 $c .= chr(0x80 | ($val % 64));