[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/plugins/skinfiles/ -> index.php (source)

   1  <?php
   2  
   3     /* ==========================================================================================
   4      * Nucleus SkinFiles Plugin
   5      *
   6      * Copyright 2005-2007 by Jeff MacMichael and Niels Leenheer
   7      *
   8      * @version $Id: index.php 1200 2007-09-07 07:06:37Z kimitake $
   9      * @version $NucleusJP: index.php,v 1.5.2.1 2005/08/25 07:04:13 kimitake Exp $
  10      *
  11      * ==========================================================================================
  12      * This program is free software and open source software; you can redistribute
  13      * it and/or modify it under the terms of the GNU General Public License as
  14      * published by the Free Software Foundation; either version 2 of the License,
  15      * or (at your option) any later version.
  16      *
  17      * This program is distributed in the hope that it will be useful, but WITHOUT
  18      * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19      * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  20      * more details.
  21      *
  22      * You should have received a copy of the GNU General Public License along
  23      * with this program; if not, write to the Free Software Foundation, Inc.,
  24      * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
  25      * http://www.gnu.org/licenses/gpl.html
  26      * ==========================================================================================
  27      */
  28  
  29       $strRel = '../../../'; 
  30      require ($strRel . 'config.php');
  31      include ($DIR_LIBS . 'PLUGINADMIN.php');
  32  
  33      $language = ereg_replace( '[\\|/]', '', getLanguageName());
  34      $langfile = $language.'.php';
  35      if (file_exists($langfile))
  36          include_once($langfile);
  37      else
  38          include_once ('english.php');
  39  
  40      /**
  41        * Create admin area
  42        */
  43  
  44      $oPluginAdmin  = new PluginAdmin('SkinFiles');
  45  
  46      if (!($member->isLoggedIn() && $member->isAdmin()))
  47      {
  48          $oPluginAdmin->start();
  49          echo '<p>' . _ERROR_DISALLOWED . '</p>';
  50          $oPluginAdmin->end();
  51          exit;
  52      }
  53  
  54  
  55      
  56      /**
  57        * Setup main variables
  58        */
  59  
  60      $rootDirectory = sfRealPath($DIR_SKINS);            
  61      $rootUrl       = $CONF['SkinsURL'];
  62      $pluginUrl        = $oPluginAdmin->plugin->getAdminURL();
  63  
  64      $filetypes = array (
  65          'text'    => array ('inc', 'txt', 'css', 'js', 'php'),
  66          'html'    => array ('htm', 'html'),
  67          'img'    => array ('png', 'gif', 'jpg', 'jpeg', 'bmp', 'ico', 'swf'),
  68      );
  69      
  70      
  71      /**
  72        * Bypass admin area for downloads
  73        */
  74      
  75      $action = requestVar('action');
  76  
  77      if ($action == 'download') { 
  78          _skinfiles_download();
  79          exit;
  80      }
  81  
  82  
  83      /**
  84        * Build admin area
  85        */
  86  
  87      $oPluginAdmin->start("<style type='text/css'>
  88      <!--
  89      
  90          div#content a {
  91              text-decoration: none;
  92          }
  93          div#content img {
  94              vertical-align: middle;
  95              margin-top: -3px;
  96          }
  97          p.message {
  98              font-weight: bold;
  99          }
 100          p.error {
 101              font-size: 100%;
 102              font-weight: bold;
 103              color: #880000;
 104          }
 105          pre {
 106              overflow: auto;
 107              height: 400px;
 108          }
 109          iframe {
 110              width: 100%;
 111              height: 400px;
 112              border: 1px solid gray;
 113          }
 114          div.dialogbox {
 115              border: 1px solid #ddd;
 116              background-color: #F6F6F6;
 117              margin: 18px 0 1.5em 0;
 118          }
 119          div.dialogbox h4 {
 120              background-color: #bbc;
 121              color: #000;
 122              margin: 0;
 123              padding: 5px;
 124          }
 125          div.dialogbox h4.light {
 126              background-color: #ddd;
 127          }
 128          div.dialogbox div {
 129              margin: 0;
 130              padding: 10px;
 131          }
 132          div.dialogbox button {
 133              margin: 10px 0 0 6px;
 134              float: right;
 135          }
 136          div.dialogbox p {
 137              margin: 0;
 138          }
 139          div.dialogbox p.buttons {
 140              text-align: right;
 141              overflow: auto;
 142          }
 143          div.dialogbox textarea {
 144              width: 100%;
 145              margin: 0;
 146          }
 147      
 148      -->
 149      </style>");
 150      
 151      echo "<h2>" . _SKINFILES_MANAGEMENT . "</h2>";
 152      
 153      $actions = array (
 154          'renfile', 'renfile_process', 'delfile', 'delfile_process', 
 155          'editfile', 'editfile_process', 'uploadfile', 'createfile', 'viewfile',
 156          'rendir', 'rendir_process', 'deldir', 'deldir_process',
 157          'emptydir', 'emptydir_process', 'createdir'
 158      );
 159      
 160      if (in_array($action, $actions)) 
 161      { 
 162          if (!$manager->checkTicket())
 163          {
 164              echo '<p class="error">Error: ' . _ERROR_BADTICKET . '</p>';
 165              sfShowDirectory();
 166              
 167          } 
 168          else 
 169          {
 170              call_user_func('_skinfiles_' . $action);
 171          }
 172      } 
 173      else 
 174      {
 175          sfShowDirectory();
 176      }
 177  
 178      $oPluginAdmin->end();
 179      exit;
 180      
 181      
 182  
 183  
 184  
 185  
 186  
 187      /* Helper functions **************************************************************************************************************/
 188  
 189  	function sfExpandDirectory ($path) {
 190         /* IN:  relative directory
 191          * OUT: full path to directory
 192          */
 193  
 194          global $rootDirectory;
 195          return sfRealPath($rootDirectory . $path);
 196      }
 197      
 198  	function sfRealPath ($path) {
 199         /* IN:  full path 
 200          * OUT: canonicalized absolute pathname
 201          */
 202  
 203          $path = realpath($path);
 204          $path = str_replace('\\', '/', $path);    
 205          $path = substr($path, strlen($path) - 1) != '/' ? $path . '/' : $path;
 206          return $path;
 207      }
 208  
 209  	function sfFullUrl ($path) {
 210         /* IN:  full path including filename
 211          * OUT: url including filename
 212          */
 213  
 214          global $rootDirectory, $rootUrl;
 215          
 216          $path = str_replace($rootDirectory, '', $path);
 217          $path = rawurlencode($path);
 218          $path = str_replace('%2F', '/', $path);
 219          return $rootUrl . $path;
 220      }
 221  
 222  	function sfValidPath ($path) {
 223         /* IN:  full path excluding or including filename
 224          * OUT: boolean, true if full path is or is within rootDirectory
 225          */
 226  
 227          global $rootDirectory;
 228          return substr($path, 0, strlen($rootDirectory)) == $rootDirectory;
 229      }
 230      
 231  	function sfRelativePath ($path) {
 232         /* IN:  full path including or excluding filename
 233          * OUT: relative path from rootDirectory
 234          */
 235  
 236          global $rootDirectory;
 237          return str_replace($rootDirectory, '', $path);
 238      }
 239      
 240  	function sfIsFileType ($type, $file) {
 241  
 242          global $filetypes;
 243          return isset($filetypes[$type]) && in_array(strtolower(substr(strrchr($file, "."), 1)), $filetypes[$type]);
 244      }
 245  
 246  	function sfAllowEditing ($file) {
 247          return sfIsFileType('html', $file) || sfIsFileType('text', $file);
 248      }
 249      
 250  	function sfAllowViewing ($file) {
 251          return sfIsFileType('html', $file) || sfIsFileType('text', $file) || sfIsFileType('img', $file);
 252      }
 253  
 254  
 255  	function sfDisplayPath ($relative) {
 256      
 257          global $pluginUrl;
 258          
 259          $result  = '<a href="' . htmlspecialchars($pluginUrl) . '" title="Go back to &laquo;skins&raquo;">';
 260          $result .= '<img src="' . htmlspecialchars($pluginUrl . 'home.gif') . '" alt="" /> skins</a> / ';
 261  
 262          $parts = explode('/', $relative);
 263          $part = '';
 264          
 265          while (list(,$v) = each ($parts)) {
 266              if ($v != '') {
 267                  $part .= $v . '/';
 268                  
 269                  $result .= '<a href="' . htmlspecialchars($pluginUrl . '?dir=' . rawurlencode($part)) . '" ';
 270                  $result .= 'title="Go back to &laquo;' . htmlspecialchars($v) . '&raquo;">';
 271                  $result .= '<img src="' . htmlspecialchars($pluginUrl . 'dir.gif') . '" alt="" /> ';
 272                  $result .= htmlspecialchars($v) . '</a> / ';
 273              }
 274          }
 275          
 276          return $result;
 277      }
 278  
 279  	function sfIcon ($file) {
 280      
 281          global $pluginUrl;
 282      
 283          $ext = strtolower(substr(strrchr($file, "."), 1));
 284          
 285          switch ($ext) {
 286              case 'htm':
 287              case 'html':
 288                  return $pluginUrl . 'html.gif';
 289                  break;
 290  
 291              case 'txt':
 292              case 'js':
 293              case 'css':
 294              case 'inc':
 295                  return $pluginUrl . 'text.gif';
 296                  break;
 297  
 298              case 'gif':
 299              case 'png':
 300              case 'jpg':
 301              case 'jpeg':
 302              case 'bmp':
 303              case 'xbmp':
 304              case 'ico':
 305                  return $pluginUrl . 'image.gif';
 306                  break;
 307  
 308              case 'php':
 309              case 'php3':
 310              case 'php4':
 311                  return $pluginUrl . 'php.gif';
 312                  break;
 313                  
 314              default:
 315                  return $pluginUrl . 'generic.gif';
 316                  break;
 317          }
 318      }
 319  
 320  	function sfIllegalFilename($name) {
 321          return preg_match('#[\n\r\\\/\:\*\?\"\<\>\|]#', $name);
 322      }
 323  
 324  	function sfDirectoryIsEmpty($dir) {
 325          
 326          $count = 0;
 327          
 328          if ($dh = opendir($dir)) 
 329          {
 330                 while (($file = readdir($dh)) !== false) 
 331                     $count++;
 332                    
 333              closedir($dh);
 334          }
 335          
 336          // $count must be smaller or equal than 2, because '.' 
 337          // and '..' are always returned by readdir().
 338          return $count <= 2;
 339      }
 340  
 341  
 342  
 343  
 344  
 345  
 346  
 347  
 348  
 349      /* Show directory ****************************************************************************************************************/
 350  
 351  	function sfShowDirectory($default = '') {
 352  
 353          global $pluginUrl, $rootDirectory, $CONF, $manager;
 354          
 355          $directory = $default != '' ? 
 356              $default : 
 357              sfExpandDirectory(trim(requestVar('dir')));
 358          
 359          if (!sfValidPath($directory) || !is_dir($directory)) {
 360              $directory = $rootDirectory;
 361          }
 362  
 363          $relative  = sfRelativePath ($directory);
 364          
 365          echo '<p class="location">' . _SKINFILES_CURRENT_LOCATION . sfDisplayPath($relative) . '</p>';            
 366  
 367              
 368          $dirs = array();
 369          $files = array();
 370  
 371          if ($dh = @opendir($directory)) { 
 372              while (($file = readdir($dh)) !== false) { 
 373                  if (!preg_match("/^\.{1,2}$/", $file)) {
 374                      $fstat = @stat($directory . $file);
 375                  
 376                      if ($fstat['mode'] & 040000)
 377                          $dirs[$file] = $fstat;
 378                      else
 379                          $files[$file] = $fstat;
 380                  }
 381              }
 382              closedir($dh); 
 383          }             
 384          
 385          ksort($dirs);
 386          ksort($files);
 387          
 388          echo '<table><thead><tr>';
 389          echo '<th>' . _SKINFILES_NAME . '</th><th>' . _SKINFILES_SIZE . '</th><th>' . _SKINFILES_LAST_MODIFIED . '</th><th colspan="4">' . _SKINFILES_ACTIONS . '</th>';
 390          echo '</tr></thead>';
 391  
 392          while (list($name, $stat) = each($dirs)) {
 393              
 394              $dir = sfRelativePath($directory . $name . '/');
 395                  
 396              echo '<tr onmouseover="focusRow(this);" onmouseout="blurRow(this);"><td>';
 397              
 398              if (is_readable ($directory . $name)) 
 399              {
 400                  echo '<a href="' . htmlspecialchars($pluginUrl . '?dir=' . rawurlencode($dir)) . '">';
 401                  echo '<img src="' . htmlspecialchars($pluginUrl . 'dir.gif') . '" alt="folder" /> ';
 402                  echo htmlspecialchars($name).'</a>';            
 403              }
 404              else
 405              {
 406                  echo '<img src="' . htmlspecialchars($pluginUrl . 'dir.gif') . '" alt="folder" /> ';
 407                  echo htmlspecialchars($name);            
 408              }
 409                          
 410              echo '</td>';
 411                  
 412              $renUrl = $manager->addTicketToUrl($pluginUrl . '?action=rendir&dir=' . rawurlencode($dir));
 413              $delUrl = $manager->addTicketToUrl($pluginUrl . '?action=deldir&dir=' . rawurlencode($dir));
 414                  
 415              echo '<td>&ndash;</td>';
 416              echo '<td>' . date(_SKINFILES_DATE_FORMAT, $stat['mtime']);
 417              
 418              
 419              if (is_writable($directory . $name)) {
 420                  echo '<td><a href="' . htmlspecialchars($renUrl) . '" title="' . _SKINFILES_RENAME . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_RENAME . '</a></td>';
 421              } else {
 422                  echo '<td>&nbsp;</td>';
 423              }
 424              
 425              if (is_writable($directory . $name) && sfDirectoryIsEmpty($directory . $name)) {
 426                  echo '<td><a href="' . htmlspecialchars($delUrl) . '" title="' . _SKINFILES_DELETE . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_DELETE . '</a></td>';
 427              } else {
 428                  echo '<td>&nbsp;</td>';
 429              }
 430              
 431              echo '<td>&nbsp;</td><td>&nbsp;</td>';
 432              echo '</tr>';
 433          }
 434  
 435  
 436          while (list($name, $stat) = each($files)) {
 437  
 438              $file = sfRelativePath($directory . $name);
 439  
 440              $renUrl   = $manager->addTicketToUrl($pluginUrl . '?action=renfile&file='  . rawurlencode($file));
 441              $delUrl   = $manager->addTicketToUrl($pluginUrl . '?action=delfile&file='  . rawurlencode($file));
 442              $editUrl  = $manager->addTicketToUrl($pluginUrl . '?action=editfile&file=' . rawurlencode($file));
 443              $viewUrl  = $manager->addTicketToUrl($pluginUrl . '?action=viewfile&file=' . rawurlencode($file));
 444              $dlUrl       = $manager->addTicketToUrl($pluginUrl . '?action=download&file=' . rawurlencode($file));
 445  
 446              echo '<tr onmouseover="focusRow(this);" onmouseout="blurRow(this);"><td>';
 447              
 448              if (is_readable ($directory . $name) && sfAllowViewing($name)) 
 449              {
 450                  echo '<a href="' . htmlspecialchars($viewUrl) . '">';
 451                  echo '<img src="' . htmlspecialchars(sfIcon($name)) . '" alt="" /> ';
 452                  echo htmlspecialchars($name).'</a>';
 453              }
 454              else
 455              {
 456                  echo '<img src="' . htmlspecialchars(sfIcon($name)) . '" alt="" /> ';
 457                  echo htmlspecialchars($name);
 458              }
 459  
 460              echo '</td><td>';
 461              echo ceil($stat['size'] / 1024) . ' kB';
 462              echo '</td><td>';
 463              echo date(_SKINFILES_DATE_FORMAT, $stat['mtime']);
 464              echo '</td><td>';
 465                  
 466              if (is_writable($directory . $name)) {
 467                  echo '<a href="' . htmlspecialchars($renUrl) . '" title="' . _SKINFILES_RENAME . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_RENAME . '</a>';
 468              } else {
 469                  echo '&nbsp;';
 470              }
 471                  
 472              echo '</td><td>';
 473  
 474              if (is_writable($directory . $name)) {
 475                  echo '<a href="' . htmlspecialchars($delUrl) . '" title="' . _SKINFILES_DELETE . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_DELETE . '</a>';
 476              } else {
 477                  echo '&nbsp;';
 478              }
 479                  
 480              echo '</td><td>';
 481              
 482              if (is_writable($directory . $name) && sfAllowEditing($name))
 483                  echo '<a href="'. htmlspecialchars($editUrl) . '" title="' . _SKINFILES_EDIT . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_EDIT . '</a>';
 484              else
 485                  echo '&nbsp;';
 486  
 487              echo '</td><td>';
 488              
 489              if (is_readable ($directory . $name))
 490                  echo '<a href="' . htmlspecialchars($dlUrl) . '" title="' . _SKINFILES_DOWNLOAD . ' &laquo;' . htmlspecialchars($name) . '&raquo;">' . _SKINFILES_DOWNLOAD . '</a>';
 491              else
 492                  echo '&nbsp;';
 493                  
 494              echo '</td></tr>';
 495          }
 496  
 497          if (!count($dirs) && !count($files)) {
 498              echo '<tr><td colspan="7">' . _SKINFILES_ERR_DIR_DOES_NOT_CONTAIN . '</td></tr>';
 499          }
 500  
 501          echo '</table>';
 502  
 503          if ($relative != '') {
 504          
 505              if (is_writable($directory)) {
 506                  echo '<div class="dialogbox">';
 507                  echo '<h4 class="light">' . _SKINFILES_CREATE_NEW_FILE . '</h4><div>';
 508                  echo '<form method="post" action="' . htmlspecialchars($pluginUrl) . '">';
 509                  $manager->addTicketHidden();
 510                  echo '<input type="hidden" name="action" value="createfile" />';
 511                  echo '<input type="hidden" name="dir" value="' . htmlspecialchars($relative) . '" />';
 512                  echo '<input type="text" name="name" size="40" value="untitled.txt" />';
 513                  echo '<p class="buttons"><input type="submit" value="' . _SKINFILES_CREATE_FILE . '" /></p></form>';
 514                  echo '</div></div>';
 515      
 516                  echo '<div class="dialogbox">';
 517                  echo '<h4 class="light">' . _SKINFILES_UPLOAD_NEW_FILE . '</h4><div>';
 518                  echo '<form method="post" enctype="multipart/form-data" action="' . htmlspecialchars($pluginUrl) . '">';
 519                  $manager->addTicketHidden();
 520                  echo '<input type="hidden" name="action" value="uploadfile" />';
 521                  echo '<input type="hidden" name="dir" value="' . htmlspecialchars($relative) . '" />';
 522                  echo '<input type="hidden" name="MAX_FILE_SIZE" value="' . $CONF['MaxUploadSize'] . '" />';
 523                  echo '<input type="file" name="name" size="40" />';
 524                  echo '<p class="buttons"><input type="submit" value="' . _SKINFILES_UPLOAD . '" /></p></form>';
 525                  echo '</div></div>';
 526              }
 527      
 528              if (count($files)) {
 529                  echo '<div class="dialogbox">';
 530                  echo '<h4 class="light">' . _SKINFILES_DEL_ALL_FILES . '</h4><div>';
 531                  echo '<form method="post" action="' . htmlspecialchars($pluginUrl) . '">';
 532                  $manager->addTicketHidden();
 533                  echo '<input type="hidden" name="action" value="emptydir" />';
 534                  echo '<input type="hidden" name="dir" value="' . htmlspecialchars($relative) . '" />';
 535                  echo _SKINFILES_DEL_ALL_FILES_MSG;
 536                  echo '<p class="buttons"><input type="submit" value="' . _SKINFILES_DELETE_ALL . '" tabindex="140" onclick="return checkSubmit();" /></p>';
 537                  echo '</form>';
 538                  echo '</div></div>';
 539              }
 540          }
 541      
 542          if (is_writable($directory)) {
 543              echo '<div class="dialogbox">';
 544              echo '<h4 class="light">' . _SKINFILES_CREATE_NEW_DIR . '</h4><div>';
 545              echo '<form method="post" action="' . htmlspecialchars($pluginUrl) . '">';
 546              $manager->addTicketHidden();
 547              echo '<input type="hidden" name="action" value="createdir" />';
 548              echo '<input type="hidden" name="dir" value="' . htmlspecialchars($relative) . '" />';
 549              echo '<input type="text" name="name" value="untitled" tabindex="90" size="40" />';
 550              echo '<p class="buttons"><input type="submit" value="' . _SKINFILES_CREATE . '" tabindex="140" onclick="return checkSubmit();" /></p>';
 551              echo '</form>';
 552              echo '</div></div>';
 553          }
 554      }
 555      
 556      
 557      
 558  
 559      /* Rename directory **************************************************************************************************************/
 560  
 561  	function _skinfiles_rendir($preset = '') {
 562  
 563          global $pluginUrl, $manager;
 564          
 565          $file      = trim(basename(requestVar('dir')));
 566          $directory = trim(dirname(requestVar('dir')));
 567          $directory = sfExpandDirectory ($directory);
 568          
 569          if (sfValidPath($directory . $file) && file_exists($directory . $file) && 
 570