[ Index ]

PHP Cross Reference of Textpattern 4.0.8

title

Body

[close]

/textpattern/include/ -> txp_plugin.php (source)

   1  <?php
   2  
   3  /*
   4      This is Textpattern
   5  
   6      Copyright 2005 by Dean Allen
   7      www.textpattern.com
   8      All rights reserved
   9  
  10      Use of this software indicates acceptance of the Textpattern license agreement
  11  
  12  $HeadURL: https://textpattern.googlecode.com/svn/releases/4.0.8/source/textpattern/include/txp_plugin.php $
  13  $LastChangedRevision: 3052 $
  14  
  15  */
  16  
  17      if (!defined('txpinterface')) die('txpinterface is undefined.');
  18  
  19      if ($event == 'plugin') {
  20          require_privs('plugin');
  21  
  22          if(!$step or !in_array($step, array('plugin_edit','plugin_help','plugin_list','plugin_install','plugin_save','plugin_verify','switch_status','plugin_multi_edit'))){
  23              plugin_list();
  24          } else $step();
  25      }
  26  
  27  // -------------------------------------------------------------
  28  
  29  	function plugin_list($message = '')
  30      {
  31          pagetop(gTxt('edit_plugins'), $message);
  32  
  33          echo n.n.startTable('edit').
  34              tr(
  35                  tda(
  36                      plugin_form()
  37                  ,' colspan="8" style="height: 30px; border: none;"')
  38              ).
  39          endTable();
  40  
  41          extract(gpsa(array('sort', 'dir')));
  42  
  43          $dir = ($dir == 'desc') ? 'desc' : 'asc';
  44  
  45          if (!in_array($sort, array('name', 'status', 'author', 'version', 'modified', 'load_order'))) $sort = 'name';
  46  
  47          $sort_sql = $sort.' '.$dir;
  48  
  49          $switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
  50  
  51          $rs = safe_rows_start('name, status, author, author_uri, version, description, length(help) as help, abs(strcmp(md5(code),code_md5)) as modified, load_order',
  52              'txp_plugin', '1 order by '.$sort_sql);
  53  
  54          if ($rs and numRows($rs) > 0)
  55          {
  56              echo '<form action="index.php" method="post" name="longform" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">'.
  57  
  58              startTable('list').
  59  
  60              tr(
  61                  column_head('plugin', 'name', 'plugin', true, $switch_dir, '', '', ('name' == $sort) ? $dir : '').
  62                  column_head('author', 'author', 'plugin', true, $switch_dir, '', '', ('author' == $sort) ? $dir : '').
  63                  column_head('version', 'version', 'plugin', true, $switch_dir, '', '', ('version' == $sort) ? $dir : '').
  64                  column_head('plugin_modified', 'modified', 'plugin', true, $switch_dir, '', '', ('modified' == $sort) ? $dir : '').
  65                  hCell(gTxt('description')).
  66                  column_head('active', 'status', 'plugin', true, $switch_dir, '', '', ('status' == $sort) ? $dir : '').
  67                  column_head('order', 'load_order', 'plugin', true, $switch_dir, '', '', ('load_order' == $sort) ? $dir : '').
  68                  hCell(gTxt('help')).
  69                  hCell().
  70                  hCell()
  71              );
  72  
  73              while ($a = nextRow($rs))
  74              {
  75                  foreach ($a as $key => $value) {
  76                      $$key = htmlspecialchars($value);
  77                  }
  78                  // Fix up the description for clean cases
  79                  $description = preg_replace(array('#&lt;br /&gt;#',
  80                                                    '#&lt;(/?(a|b|i|em|strong))&gt;#',
  81                                                    '#&lt;a href=&quot;(https?|\.|\/|ftp)([A-Za-z0-9:/?.=_]+?)&quot;&gt;#'),
  82                                              array('<br />','<$1>','<a href="$1$2">'),
  83                                              $description);
  84  
  85                  $help = !empty($help) ?
  86                      '<a href="?event=plugin'.a.'step=plugin_help'.a.'name='.$name.'">'.gTxt('view').'</a>' :
  87                      gTxt('none');
  88  
  89                  echo tr(
  90  
  91                      n.td($name).
  92  
  93                      td(
  94                          href($author, $author_uri)
  95                      ).
  96  
  97                      td($version, 10).
  98                      td($modified ? gTxt('yes') : '').
  99                      td($description, 260).
 100  
 101                      td(
 102                          status_link($status, $name, yes_no($status))
 103                      ,30).
 104  
 105                      td($load_order).
 106                      td($help).
 107  
 108                      td(
 109                          eLink('plugin', 'plugin_edit', 'name', $name, gTxt('edit'))
 110                      ).
 111  
 112                      td(
 113                          fInput('checkbox', 'selected[]', $name)
 114                      ,30)
 115                  );
 116  
 117                  unset($name, $page, $deletelink);
 118              }
 119  
 120              echo tr(
 121                  tda(
 122                      select_buttons().
 123                      plugin_multiedit_form('', $sort, $dir, '', '')
 124                  , ' colspan="10" style="text-align: right; border: none;"')
 125              ).
 126  
 127              n.endTable().
 128              n.'</form>';
 129          }
 130      }
 131  
 132  // -------------------------------------------------------------
 133  
 134  	function switch_status()
 135      {
 136          extract(gpsa(array('name', 'status')));
 137  
 138          $change = ($status) ? 0 : 1;
 139  
 140          safe_update('txp_plugin', "status = $change", "name = '".doSlash($name)."'");
 141  
 142          $message = gTxt('plugin_updated', array('{name}' => $name));
 143  
 144          plugin_list($message);
 145      }
 146  
 147  // -------------------------------------------------------------
 148    function plugin_edit()
 149    {
 150          $name = gps('name');
 151          pagetop(gTxt('edit_plugins'));
 152          echo plugin_edit_form($name);
 153    }
 154  
 155  
 156  // -------------------------------------------------------------
 157  	function plugin_help()
 158      {
 159          $name = gps('name');
 160          pagetop(gTxt('plugin_help'));
 161          $help = ($name) ? safe_field('help','txp_plugin',"name = '".doSlash($name)."'") : '';
 162          echo
 163          startTable('edit')
 164          .    tr(tda($help,' width="600"'))
 165          .    endTable();
 166  
 167      }
 168  
 169  // -------------------------------------------------------------
 170  	function plugin_edit_form($name='')
 171      {
 172          $sub = fInput('submit','',gTxt('save'),'publish');
 173          $code = ($name) ? fetch('code','txp_plugin','name',$name) : '';
 174          $thing = ($code)
 175          ?    $code
 176          :    '';
 177          $textarea = '<textarea id="plugin-code" class="code" name="code" rows="28" cols="90">'.htmlspecialchars($thing).'</textarea>';
 178  
 179          return
 180          form(startTable('edit')
 181          .    tr(td($textarea))
 182          .    tr(td($sub))
 183  #        .    tr(td($help))
 184          .    endTable().sInput('plugin_save').eInput('plugin').hInput('name',$name)).
 185          n.'<script type="text/javascript">'.
 186          n.'if(jQuery.browser.mozilla){$("#plugin-code").attr("spellcheck", false);}'.
 187          n.'</script>';
 188          ;
 189      }
 190  
 191  // -------------------------------------------------------------
 192  
 193  	function plugin_save()
 194      {
 195          extract(doSlash(gpsa(array('name', 'code'))));
 196  
 197          safe_update('txp_plugin', "code = '$code'", "name = '$name'");
 198  
 199          $message = gTxt('plugin_saved', array('{name}' => $name));
 200  
 201          plugin_list($message);
 202      }
 203  
 204  // -------------------------------------------------------------
 205  
 206  	function status_link($status,$name,$linktext)
 207      {
 208          $out = '<a href="index.php?';
 209          $out .= 'event=plugin&#38;step=switch_status&#38;status='.
 210              $status.'&#38;name='.urlencode($name).'"';
 211          $out .= '>'.$linktext.'</a>';
 212          return $out;
 213      }
 214  
 215  // -------------------------------------------------------------
 216  	function plugin_verify()
 217      {
 218  
 219          if (ps('txt_plugin')) {
 220              $plugin = join("\n", file($_FILES['theplugin']['tmp_name']));
 221          } else {
 222              $plugin = ps('plugin');
 223          }
 224  
 225          $plugin = preg_replace('@.*\$plugin=\'([\w=+/]+)\'.*@s', '$1', $plugin);
 226          $plugin = preg_replace('/^#.*$/m', '', $plugin);
 227  
 228          if(isset($plugin)) {
 229              $plugin_encoded = $plugin;
 230              $plugin = base64_decode($plugin);
 231  
 232              if (strncmp($plugin, "\x1F\x8B", 2) === 0)
 233              {
 234                  if (function_exists('gzinflate'))
 235                  {
 236                      $plugin = gzinflate(substr($plugin, 10));
 237                  }
 238  
 239                  else
 240                  {
 241                      plugin_list(gTxt('plugin_compression_unsupported'));
 242                      return;
 243                  }
 244              }
 245  
 246              if ($plugin = @unserialize($plugin))
 247              {
 248                  if(is_array($plugin)){
 249                      extract($plugin);
 250                      $source = '';
 251                      if (isset($help_raw) && empty($plugin['allow_html_help'])) {
 252                          include_once txpath.'/lib/classTextile.php';
 253                          $textile = new Textile();
 254                          $help_source = $textile->TextileRestricted($help_raw, 0, 0);
 255                      } else {
 256                          $help_source= highlight_string($help, true);
 257                      }
 258                      $source.= highlight_string('<?php'.$plugin['code'].'?>', true);
 259                      $sub = fInput('submit','',gTxt('install'),'publish');
 260  
 261                      pagetop(gTxt('edit_plugins'));
 262                      echo
 263                      form(
 264                          hed(gTxt('previewing_plugin'), 3).
 265                          tag($source, 'div', ' id="preview-plugin" class="code"').
 266                          hed(gTxt('plugin_help').':', 3).
 267                          tag($help_source, 'div', ' id="preview-help" class="code"').
 268                          $sub.
 269                          sInput('plugin_install').
 270                          eInput('plugin').
 271                          hInput('plugin64', $plugin_encoded)
 272                      , 'margin: 0 auto; width: 65%;');
 273                      return;
 274                  }
 275              }
 276          }
 277          plugin_list(gTxt('bad_plugin_code'));
 278  
 279      }
 280  
 281  // -------------------------------------------------------------
 282  	function plugin_install()
 283      {
 284  
 285          $plugin = ps('plugin64');
 286  
 287          $plugin = preg_replace('@.*\$plugin=\'([\w=+/]+)\'.*@s', '$1', $plugin);
 288          $plugin = preg_replace('/^#.*$/m', '', $plugin);
 289  
 290          if(trim($plugin)) {
 291  
 292              $plugin = base64_decode($plugin);
 293              if (strncmp($plugin,"\x1F\x8B",2)===0)
 294                  $plugin = gzinflate(substr($plugin, 10));
 295  
 296              if ($plugin = unserialize($plugin)) {
 297  
 298                  if(is_array($plugin)){
 299  
 300                      extract($plugin);
 301  
 302                      $type  = empty($type)  ? 0 : min(max(intval($type), 0), 3);
 303                      $order = empty($order) ? 5 : min(max(intval($order), 1), 9);
 304  
 305                      $exists = fetch('name','txp_plugin','name',$name);
 306  
 307                      if (isset($help_raw) && empty($plugin['allow_html_help'])) {
 308                              // default: help is in Textile format
 309                              include_once txpath.'/lib/classTextile.php';
 310                              $textile = new Textile();
 311                              $help = $textile->TextileRestricted($help_raw, 0, 0);
 312                      }
 313  
 314                      if ($exists) {
 315                          $rs = safe_update(
 316                             "txp_plugin",
 317                              "status      = 0,
 318                              type         = $type,
 319                              author       = '".doSlash($author)."',
 320                              author_uri   = '".doSlash($author_uri)."',
 321                              version      = '".doSlash($version)."',
 322                              description  = '".doSlash($description)."',
 323                              help         = '".doSlash($help)."',
 324                              code         = '".doSlash($code)."',
 325                              code_restore = '".doSlash($code)."',
 326                              code_md5     = '".doSlash($md5)."'",
 327                              "name        = '".doSlash($name)."'"
 328                          );
 329  
 330                      } else {
 331  
 332                          $rs = safe_insert(
 333                             "txp_plugin",
 334                             "name         = '".doSlash($name)."',
 335                              status       = 0,
 336                              type         = $type,
 337                              author       = '".doSlash($author)."',
 338                              author_uri   = '".doSlash($author_uri)."',
 339                              version      = '".doSlash($version)."',
 340                              description  = '".doSlash($description)."',
 341                              help         = '".doSlash($help)."',
 342                              code         = '".doSlash($code)."',
 343                              code_restore = '".doSlash($code)."',
 344                              code_md5     = '".doSlash($md5)."',
 345                              load_order   = '".$order."'"
 346                          );
 347                      }
 348  
 349                      if ($rs and $code)
 350                      {
 351                          $message = gTxt('plugin_installed', array('{name}' => htmlspecialchars($name)));
 352  
 353                          plugin_list($message);
 354                      }
 355  
 356                      else
 357                      {
 358                          $message = gTxt('plugin_install_failed', array('{name}' => htmlspecialchars($name)));
 359  
 360                          plugin_list($message);
 361                      }
 362                  }
 363              }
 364  
 365              else
 366              {
 367                  plugin_list(gTxt('bad_plugin_code'));
 368              }
 369          }
 370      }
 371  
 372  // -------------------------------------------------------------
 373  
 374  	function plugin_form()
 375      {
 376          return n.n.form(
 377              graf(
 378              tag(gTxt('install_plugin'), 'span', ' style="vertical-align: top;"').sp.
 379  
 380              '<textarea id="plugin-install" class="code" name="plugin" cols="62" rows="1"></textarea>'.sp.
 381  
 382              tag(
 383                  popHelp('install_plugin').sp.
 384                  fInput('submit', 'install_new', gTxt('upload'), 'smallerbox')
 385             , 'span', ' style="vertical-align: 6px;"').
 386  
 387                  eInput('plugin').
 388                  sInput('plugin_verify')
 389              )
 390          , 'text-align: center;');
 391      }
 392  
 393  // -------------------------------------------------------------
 394  
 395  	function plugin_multiedit_form($page, $sort, $dir, $crit, $search_method)
 396      {
 397          $methods = array(
 398              'changestatus' => gTxt('changestatus'),
 399              'changeorder' => gTxt('changeorder'),
 400              'delete' => gTxt('delete')
 401          );
 402  
 403          return event_multiedit_form('plugin', $methods, $page, $sort, $dir, $crit, $search_method);
 404      }
 405  
 406  // -------------------------------------------------------------
 407  
 408  	function plugin_multi_edit()
 409      {
 410          $selected = ps('selected');
 411          $method   = ps('edit_method');
 412  
 413          if (!$selected or !is_array($selected))
 414          {
 415              return plugin_list();
 416          }
 417  
 418          $where = "name IN ('".join("','", doSlash($selected))."')";
 419  
 420          switch ($method)
 421          {
 422              case 'delete':
 423                  safe_delete('txp_plugin', $where);
 424                  break;
 425  
 426              case 'changestatus':
 427                  safe_update('txp_plugin', 'status = (1-status)', $where);
 428                  break;
 429  
 430              case 'changeorder':
 431                  $order = min(max(intval(ps('order')), 1), 9);
 432                  safe_update('txp_plugin', 'load_order = '.$order, $where);
 433                  break;
 434          }
 435  
 436          $message = gTxt('plugin_'.($method == 'delete' ? 'deleted' : 'updated'), array('{name}' => join(', ', $selected)));
 437  
 438          plugin_list($message);
 439      }
 440  ?>


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7