PukiWiki PHP Cross Reference Collaborative Wikis

Source: /plugin/backup.inc.php - 247 lines - 7557 bytes - Summary - Text - Print

   1  <?php
   2  // PukiWiki - Yet another WikiWikiWeb clone.
   3  // $Id: backup.inc.php,v 1.27 2005/12/10 12:48:02 henoheno Exp $
   4  // Copyright (C)
   5  //   2002-2005 PukiWiki Developers Team
   6  //   2001-2002 Originally written by yu-ji
   7  // License: GPL v2 or (at your option) any later version
   8  //
   9  // Backup plugin
  10  
  11  // Prohibit rendering old wiki texts (suppresses load, transfer rate, and security risk)
  12  define('PLUGIN_BACKUP_DISABLE_BACKUP_RENDERING', PKWK_SAFE_MODE || PKWK_OPTIMISE);
  13  
  14  function plugin_backup_action()
  15  {
  16      global $vars, $do_backup, $hr;
  17      global $_msg_backuplist, $_msg_diff, $_msg_nowdiff, $_msg_source, $_msg_backup;
  18      global $_msg_view, $_msg_goto, $_msg_deleted;
  19      global $_title_backupdiff, $_title_backupnowdiff, $_title_backupsource;
  20      global $_title_backup, $_title_pagebackuplist, $_title_backuplist;
  21  
  22      if (! $do_backup) return;
  23  
  24      $page = isset($vars['page']) ? $vars['page']  : '';
  25      if ($page == '') return array('msg'=>$_title_backuplist, 'body'=>plugin_backup_get_list_all());
  26  
  27      check_readable($page, true, true);
  28      $s_page = htmlspecialchars($page);
  29      $r_page = rawurlencode($page);
  30  
  31      $action = isset($vars['action']) ? $vars['action'] : '';
  32      if ($action == 'delete') return plugin_backup_delete($page);
  33  
  34      $s_action = $r_action = '';
  35      if ($action != '') {
  36          $s_action = htmlspecialchars($action);
  37          $r_action = rawurlencode($action);
  38      }
  39  
  40      $s_age  = (isset($vars['age']) && is_numeric($vars['age'])) ? $vars['age'] : 0;
  41      if ($s_age <= 0) return array( 'msg'=>$_title_pagebackuplist, 'body'=>plugin_backup_get_list($page));
  42  
  43      $script = get_script_uri();
  44  
  45      $body  = '<ul>' . "\n";
  46      $body .= ' <li><a href="' . $script . '?cmd=backup">' . $_msg_backuplist . '</a></li>' ."\n";
  47  
  48      $href    = $script . '?cmd=backup&amp;page=' . $r_page . '&amp;age=' . $s_age;
  49      $is_page = is_page($page);
  50  
  51      if ($is_page && $action != 'diff')
  52          $body .= ' <li>' . str_replace('$1', '<a href="' . $href .
  53              '&amp;action=diff">' . $_msg_diff . '</a>',
  54              $_msg_view) . '</li>' . "\n";
  55  
  56      if ($is_page && $action != 'nowdiff')
  57          $body .= ' <li>' . str_replace('$1', '<a href="' . $href .
  58              '&amp;action=nowdiff">' . $_msg_nowdiff . '</a>',
  59              $_msg_view) . '</li>' . "\n";
  60  
  61      if ($action != 'source')
  62          $body .= ' <li>' . str_replace('$1', '<a href="' . $href .
  63              '&amp;action=source">' . $_msg_source . '</a>',
  64              $_msg_view) . '</li>' . "\n";
  65  
  66      if (! PLUGIN_BACKUP_DISABLE_BACKUP_RENDERING && $action)
  67          $body .= ' <li>' . str_replace('$1', '<a href="' . $href .
  68              '">' . $_msg_backup . '</a>',
  69              $_msg_view) . '</li>' . "\n";
  70  
  71      if ($is_page) {
  72          $body .= ' <li>' . str_replace('$1',
  73              '<a href="' . $script . '?' . $r_page . '">' . $s_page . '</a>',
  74              $_msg_goto) . "\n";
  75      } else {
  76          $body .= ' <li>' . str_replace('$1', $s_page, $_msg_deleted) . "\n";
  77      }
  78  
  79      $backups = get_backup($page);
  80      $backups_count = count($backups);
  81      if ($s_age > $backups_count) $s_age = $backups_count;
  82  
  83      if ($backups_count > 0) {
  84          $body .= '  <ul>' . "\n";
  85          foreach($backups as $age => $val) {
  86              $date = format_date($val['time'], TRUE);
  87              $body .= ($age == $s_age) ?
  88                  '   <li><em>' . $age . ' ' . $date . '</em></li>' . "\n" :
  89                  '   <li><a href="' . $script . '?cmd=backup&amp;action=' .
  90                  $r_action . '&amp;page=' . $r_page . '&amp;age=' . $age .
  91                  '">' . $age . ' ' . $date . '</a></li>' . "\n";
  92          }
  93          $body .= '  </ul>' . "\n";
  94      }
  95      $body .= ' </li>' . "\n";
  96      $body .= '</ul>'  . "\n";
  97  
  98      if ($action == 'diff') {
  99          $title = & $_title_backupdiff;
 100          $old = ($s_age > 1) ? join('', $backups[$s_age - 1]['data']) : '';
 101          $cur = join('', $backups[$s_age]['data']);
 102          $body .= plugin_backup_diff(do_diff($old, $cur));
 103      } else if ($s_action == 'nowdiff') {
 104          $title = & $_title_backupnowdiff;
 105          $old = join('', $backups[$s_age]['data']);
 106          $cur = join('', get_source($page));
 107          $body .= plugin_backup_diff(do_diff($old, $cur));
 108      } else if ($s_action == 'source') {
 109          $title = & $_title_backupsource;
 110          $body .= '<pre>' . htmlspecialchars(join('', $backups[$s_age]['data'])) .
 111              '</pre>' . "\n";
 112      } else {
 113          if (PLUGIN_BACKUP_DISABLE_BACKUP_RENDERING) {
 114              die_message('This feature is prohibited');
 115          } else {
 116              $title = & $_title_backup;
 117              $body .= $hr . "\n" .
 118                  drop_submit(convert_html($backups[$s_age]['data']));
 119          }
 120      }
 121  
 122      return array('msg'=>str_replace('$2', $s_age, $title), 'body'=>$body);
 123  }
 124  
 125  // Delete backup
 126  function plugin_backup_delete($page)
 127  {
 128      global $vars, $_title_backup_delete, $_title_pagebackuplist, $_msg_backup_deleted;
 129      global $_msg_backup_adminpass, $_btn_delete, $_msg_invalidpass;
 130  
 131      if (! _backup_file_exists($page))
 132          return array('msg'=>$_title_pagebackuplist, 'body'=>plugin_backup_get_list($page)); // Say "is not found"
 133  
 134      $body = '';
 135      if (isset($vars['pass'])) {
 136          if (pkwk_login($vars['pass'])) {
 137              _backup_delete($page);
 138              return array(
 139                  'msg'  => $_title_backup_delete,
 140                  'body' => str_replace('$1', make_pagelink($page), $_msg_backup_deleted)
 141              );
 142          } else {
 143              $body = '<p><strong>' . $_msg_invalidpass . '</strong></p>' . "\n";
 144          }
 145      }
 146  
 147      $script = get_script_uri();
 148      $s_page = htmlspecialchars($page);
 149      $body .= <<<EOD
 150  <p>$_msg_backup_adminpass</p>
 151  <form action="$script" method="post">
 152   <div>
 153    <input type="hidden"   name="cmd"    value="backup" />
 154    <input type="hidden"   name="page"   value="$s_page" />
 155    <input type="hidden"   name="action" value="delete" />
 156    <input type="password" name="pass"   size="12" />
 157    <input type="submit"   name="ok"     value="$_btn_delete" />
 158   </div>
 159  </form>
 160  EOD;
 161      return    array('msg'=>$_title_backup_delete, 'body'=>$body);
 162  }
 163  
 164  function plugin_backup_diff($str)
 165  {
 166      global $_msg_addline, $_msg_delline, $hr;
 167      $ul = <<<EOD
 168  $hr
 169  <ul>
 170   <li>$_msg_addline</li>
 171   <li>$_msg_delline</li>
 172  </ul>
 173  EOD;
 174  
 175      return $ul . '<pre>' . diff_style_to_css(htmlspecialchars($str)) . '</pre>' . "\n";
 176  }
 177  
 178  function plugin_backup_get_list($page)
 179  {
 180      global $_msg_backuplist, $_msg_diff, $_msg_nowdiff, $_msg_source, $_msg_nobackup;
 181      global $_title_backup_delete;
 182  
 183      $script = get_script_uri();
 184      $r_page = rawurlencode($page);
 185      $s_page = htmlspecialchars($page);
 186      $retval = array();
 187      $retval[0] = <<<EOD
 188  <ul>
 189   <li><a href="$script?cmd=backup">$_msg_backuplist</a>
 190    <ul>
 191  EOD;
 192      $retval[1] = "\n";
 193      $retval[2] = <<<EOD
 194    </ul>
 195   </li>
 196  </ul>
 197  EOD;
 198  
 199      $backups = _backup_file_exists($page) ? get_backup($page) : array();
 200      if (empty($backups)) {
 201          $msg = str_replace('$1', make_pagelink($page), $_msg_nobackup);
 202          $retval[1] .= '   <li>' . $msg . '</li>' . "\n";
 203          return join('', $retval);
 204      }
 205  
 206      if (! PKWK_READONLY) {
 207          $retval[1] .= '   <li><a href="' . $script . '?cmd=backup&amp;action=delete&amp;page=' .
 208              $r_page . '">';
 209          $retval[1] .= str_replace('$1', $s_page, $_title_backup_delete);
 210          $retval[1] .= '</a></li>' . "\n";
 211      }
 212  
 213      $href = $script . '?cmd=backup&amp;page=' . $r_page . '&amp;age=';
 214      $_anchor_from = $_anchor_to   = '';
 215      foreach ($backups as $age=>$data) {
 216          if (! PLUGIN_BACKUP_DISABLE_BACKUP_RENDERING) {
 217              $_anchor_from = '<a href="' . $href . $age . '">';
 218              $_anchor_to   = '</a>';
 219          }
 220          $date = format_date($data['time'], TRUE);
 221          $retval[1] .= <<<EOD
 222     <li>$_anchor_from$age $date$_anchor_to
 223       [ <a href="$href$age&amp;action=diff">$_msg_diff</a>
 224       | <a href="$href$age&amp;action=nowdiff">$_msg_nowdiff</a>
 225       | <a href="$href$age&amp;action=source">$_msg_source</a>
 226       ]
 227     </li>
 228  EOD;
 229      }
 230  
 231      return join('', $retval);
 232  }
 233  
 234  // List for all pages
 235  function plugin_backup_get_list_all($withfilename = FALSE)
 236  {
 237      global $cantedit;
 238  
 239      $pages = array_diff(get_existpages(BACKUP_DIR, BACKUP_EXT), $cantedit);
 240  
 241      if (empty($pages)) {
 242          return '';
 243      } else {
 244          return page_list($pages, 'backup', $withfilename);
 245      }
 246  }
 247  ?>

title

Description

title

Description

title

Description

title

title

Body