[ Index ]

PHP Cross Reference of Nucleus CMS v3.51 code documentation

title

Body

[close]

/nucleus/ -> bookmarklet.php (source)

   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: bookmarklet.php 1388 2009-07-18 06:31:28Z shizuki $

  19   */
  20  
  21  // bookmarklet is part of admin area (might need XML-RPC)

  22  $CONF = array();
  23  $CONF['UsingAdminArea'] = 1;
  24  
  25  // include all classes and config data

  26  include ('../config.php');
  27  
  28  $action = requestVar('action');
  29  
  30  if ($action == 'contextmenucode') {
  31      bm_doContextMenuCode();
  32      exit;
  33  }
  34  
  35  if (!$member->isLoggedIn() ) {
  36      bm_loginAndPassThrough();
  37      exit;
  38  }
  39  
  40  // on successfull login

  41  if ( ($action == 'login') && ($member->isLoggedIn() ) ) {
  42      $action = requestVar('nextaction');
  43  }
  44  
  45  if ($action == '') {
  46      $action = 'add';
  47  }
  48  
  49  sendContentType('text/html', 'bookmarklet-' . $action);
  50  
  51  // check ticket

  52  $action = strtolower($action);
  53  $aActionsNotToCheck = array('login', 'add', 'edit');
  54  
  55  if (!in_array($action, $aActionsNotToCheck) ) {
  56  
  57      if (!$manager->checkTicket() ) {
  58          bm_doError(_ERROR_BADTICKET);
  59      }
  60  
  61  }
  62  
  63  // find out what to do

  64  switch ($action) {
  65      // adds the item for real

  66      case 'additem':
  67          bm_doAddItem();
  68          break;
  69  
  70      // shows the edit item form

  71      case 'edit':
  72          bm_doEditForm();
  73          break;
  74  
  75      // edits the item for real

  76      case 'edititem':
  77          bm_doEditItem();
  78          break;
  79  
  80      // on login, 'action' gets changed to 'nextaction'

  81      case 'login':
  82          bm_doError('Something went wrong');
  83          break;
  84  
  85      // shows the fill in form

  86      case 'add':
  87      default:
  88          bm_doShowForm();
  89          break;
  90  }
  91  
  92  function bm_doAddItem() {
  93      global $member, $manager, $CONF;
  94  
  95      $manager->loadClass('ITEM');
  96      $result = ITEM::createFromRequest();
  97  
  98      if ($result['status'] == 'error') {
  99          bm_doError($result['message']);
 100      }
 101  
 102      $blogid = getBlogIDFromItemID($result['itemid']);
 103      $blog =& $manager->getBlog($blogid);
 104  
 105      if ($result['status'] == 'newcategory') {
 106          $message = 'Item was added, and a new category was created. <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">Click here to edit the name and description of the category.</a>';
 107          $extrahead = '';
 108      } else {
 109          $message = _ITEM_ADDED;
 110          $extrahead = '';
 111      }
 112  
 113      bm_message(_ITEM_ADDED, _ITEM_ADDED, $message,$extrahead);
 114  }
 115  
 116  function bm_doEditItem() {
 117      global $member, $manager, $CONF;
 118  
 119      $itemid = intRequestVar('itemid');
 120      $catid = postVar('catid');
 121  
 122      // only allow if user is allowed to alter item

 123      if (!$member->canUpdateItem($itemid, $catid) ) {
 124          bm_doError(_ERROR_DISALLOWED);
 125      }
 126  
 127      $body = postVar('body');
 128      $title = postVar('title');
 129      $more = postVar('more');
 130      $closed = intPostVar('closed');
 131      $actiontype = postVar('actiontype');
 132      $draftid = intPostVar('draftid');
 133  
 134      // redirect to admin area on delete (has delete confirmation)

 135      if ($actiontype == 'delete') {
 136          redirect('index.php?action=itemdelete&itemid=' . $itemid);
 137          exit;
 138      }
 139  
 140      // create new category if needed (only on edit/changedate)

 141      if (strstr($catid,'newcat') ) {
 142          // get blogid

 143          list($blogid) = sscanf($catid, "newcat-%d");
 144  
 145          // create

 146          $blog =& $manager->getBlog($blogid);
 147          $catid = $blog->createNewCategory();
 148  
 149          // show error when sth goes wrong

 150          if (!$catid) {
 151              bm_doError('Could not create new category');
 152          }
 153      }
 154  
 155      // only edit action is allowed for bookmarklet edit

 156      switch ($actiontype) {
 157          case 'changedate':
 158              $publish = 1;
 159              $wasdraft = 0;
 160              $timestamp = mktime(intPostVar('hour'), intPostVar('minutes'), 0, intPostVar('month'), intPostVar('day'), intPostVar('year') );
 161              break;
 162          case 'edit':
 163              $publish = 1;
 164              $wasdraft = 0;
 165              $timestamp = 0;
 166              break;
 167          case 'backtodrafts':
 168              $publish = 0;
 169              $wasdraft = 0;
 170              $timestamp = 0;
 171              break;
 172          default:
 173              bm_doError('Something went wrong');
 174      }
 175  
 176      // update item for real

 177      ITEM::update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp);
 178  
 179      if ($draftid > 0) {
 180          ITEM::delete($draftid);
 181      }
 182  
 183      // show success message

 184      if ($catid != intPostVar('catid') ) {
 185          bm_message(_ITEM_UPDATED, _ITEM_UPDATED, 'Item was added, and a new category was created. <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">Click here to edit the name and description of the category.</a>', '');
 186      } else {
 187          bm_message(_ITEM_UPDATED, _ITEM_UPDATED, _ITEM_UPDATED, '');
 188      }
 189  }
 190  
 191  function bm_loginAndPassThrough() {
 192  
 193      $blogid = intRequestVar('blogid');
 194      $log_text = requestVar('logtext');
 195      $log_link = requestVar('loglink');
 196      $log_linktitle = requestVar('loglinktitle');
 197  
 198      ?>
 199  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 200      <html xmlns="http://www.w3.org/1999/xhtml">
 201      <head>
 202          <title>Nucleus</title>
 203          <?php bm_style(); ?>
 204      </head>
 205      <body>
 206      <h1><?php echo _LOGIN_PLEASE ?></h1>
 207  
 208      <form method="post" action="bookmarklet.php">
 209      <p>
 210          <input name="action" value="login" type="hidden" />
 211          <input name="blogid" value="<?php echo htmlspecialchars($blogid); ?>" type="hidden" />
 212          <input name="logtext" value="<?php echo htmlspecialchars($log_text); ?>" type="hidden" />
 213          <input name="loglink" value="<?php echo htmlspecialchars($log_link); ?>" type="hidden" />
 214          <input name="loglinktitle" value="<?php echo htmlspecialchars($log_linktitle); ?>" type="hidden" />
 215          <?php echo _LOGINFORM_NAME ?>:
 216          <br /><input name="login" />
 217          <br /><?php echo _LOGINFORM_PWD ?>:
 218          <br /><input name="password" type="password" />
 219          <br /><br />
 220          <br /><input type="submit" value="<?php echo _LOGIN ?>" />
 221      </p>
 222      </form>
 223      <p><a href="bookmarklet.php" onclick="window.close();"><?php echo _POPUP_CLOSE ?></a></p>
 224      </body>
 225      </html>
 226      <?php
 227  }
 228  
 229  function bm_doShowForm() {
 230      global $member;
 231  
 232      $blogid = intRequestVar('blogid');
 233      $log_text = trim(requestVar('logtext'));
 234      $log_link = requestVar('loglink');
 235      $log_linktitle = requestVar('loglinktitle');
 236  
 237      if (!BLOG::existsID($blogid) ) {
 238          bm_doError(_ERROR_NOSUCHBLOG);
 239      }
 240  
 241      if (!$member->isTeamMember($blogid) ) {
 242          bm_doError(_ERROR_NOTONTEAM);
 243      }
 244  
 245      $logje = '';
 246  
 247      if ($log_text) {
 248          $logje .= '<blockquote><div>"' . htmlspecialchars($log_text) . '"</div></blockquote>' . "\n";
 249      }
 250  
 251      if (!$log_linktitle) {
 252          $log_linktitle = $log_link;
 253      }
 254  
 255      if ($log_link) {
 256          $logje .= '<a href="' . htmlspecialchars($log_link) . '">' . htmlspecialchars($log_linktitle) . '</a>';
 257      }
 258  
 259      $item['body'] = $logje;
 260      $item['title'] = htmlspecialchars($log_linktitle);
 261  
 262      $factory = new PAGEFACTORY($blogid);
 263      $factory->createAddForm('bookmarklet', $item);
 264  }
 265  
 266  function bm_doEditForm() {
 267      global $member, $manager;
 268  
 269      $itemid = intRequestVar('itemid');
 270  
 271      if (!$manager->existsItem($itemid, 0, 0) ) {
 272          bm_doError(_ERROR_NOSUCHITEM);
 273      }
 274  
 275      if (!$member->canAlterItem($itemid) ) {
 276          bm_doError(_ERROR_DISALLOWED);
 277      }
 278  
 279      $item =& $manager->getItem($itemid, 1, 1);
 280      $blog =& $manager->getBlog(getBlogIDFromItemID($itemid) );
 281  
 282      $manager->notify('PrepareItemForEdit', array('item' => &$item) );
 283  
 284      if ($blog->convertBreaks() ) {
 285          $item['body'] = removeBreaks($item['body']);
 286          $item['more'] = removeBreaks($item['more']);
 287      }
 288  
 289      $formfactory = new PAGEFACTORY($blog->getID() );
 290      $formfactory->createEditForm('bookmarklet', $item);
 291  }
 292  
 293  function bm_doError($msg) {
 294      bm_message(_ERROR, _ERRORMSG, $msg);
 295      die;
 296  }
 297  
 298  function bm_message($title, $head, $msg, $extrahead = '') {
 299      ?>
 300  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 301      <html xmlns="http://www.w3.org/1999/xhtml">
 302      <head>
 303          <title><?php echo $title ?></title>
 304          <?php bm_style(); ?>
 305          <?php echo $extrahead; ?>
 306      </head>
 307      <body>
 308      <h1><?php echo $head; ?></h1>
 309      <p><?php echo $msg; ?></p>
 310      <p><a href="bookmarklet.php" onclick="window.close();window.opener.location.reload();"><?php echo _POPUP_CLOSE ?></a></p>
 311      </body>
 312      </html>
 313  
 314      <?php
 315  }
 316  
 317  function bm_style() {
 318      echo '<link rel="stylesheet" type="text/css" href="styles/bookmarklet.css" />';
 319      echo '<link rel="stylesheet" type="text/css" href="styles/addedit.css" />';
 320  }
 321  
 322  function bm_doContextMenuCode() {
 323      global $CONF;
 324      ?>
 325  <script type="text/javascript" defer="defer">
 326  doc = external.menuArguments.document;
 327  lt = escape(doc.selection.createRange().text);
 328  loglink = escape(external.menuArguments.location.href);
 329  loglinktitle = escape(doc.title);
 330  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');
 331  wingm.focus();
 332  </script>
 333      <?php
 334  }
 335  
 336  ?>


Generated: Sun Aug 1 03:56:06 2010
Open Source related documentation for developers.