sTEFANs Subversion Web Contol (SWC)
Inc
[ class tree: Swc ] [ index: Swc ] [ all elements ]

Source for file swc_archive_content.inc.php

Documentation is available at swc_archive_content.inc.php

  1. <?php
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. SVN Web Control
  4. Copyright ©2006 by sTEFANs
  5. Created on 23.02.2006
  6. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. */
  23.  
  24. /**
  25.  * Provides user interface for creating, downloading and deleting archives.
  26.  * 
  27.  * @package Swc
  28.  * @subpackage Inc
  29.  * @author Stefan Schraml
  30.  * @copyright Copyright ©2006 by sTEFANs
  31.  * @license http://opensource.org/licenses/lgpl-license.php GNU General Public License
  32.  * @version v1.1.0
  33.  * @since v1.1.0
  34.  */
  35.  
  36. /** Common definitions */
  37. require_once('var.inc.php');
  38. /** ZIP archive creation library */
  39. require_once('zip.inc.php');
  40. /** HTML generation library */
  41. require_once('page.inc.php');
  42. /** Useful routines library */
  43. require_once('utils.inc.php');
  44.  
  45. /** Common filename for spaceball hint file. */
  46. define('SPACEBALL_HINT_FILE', '.spaceball.hints');
  47. /** Section name for revision map section in hint file. */
  48. define('SECTION_REV_MAP', 'Revision map');
  49.     
  50.     /** 
  51.      * Prints the complete content of the archive page
  52.      * including archive creation form and archive list.
  53.      * 
  54.      * @since v1.1.0
  55.      */
  56.     function SwcArchivePrintPage(){
  57.         $config = GetSelectedConfig();
  58.         if ($config == NULL){
  59.             echo '<span class="text_low_bold_red">'.T(TK_DETAILS_DEFAULT_TEXT).'</span><br/>';
  60.             return;
  61.         }
  62.         
  63.         echo '<br/>';
  64.         OpenTable(1, 2, 0, 'result');
  65.             PrintTableCell(T(TK_ARCHIVE_PAGE_TITLE), 'text_low_bold_red');
  66.         NewTableRow();
  67.             SwcArchivePrintCreationFormCell($config);
  68.         NewTableRow();
  69.             SwcArchivePrintFileListCell($config);
  70.         CloseTable();
  71.     }
  72.     
  73.     /** 
  74.      * Prints a cell containing the form to create an archive.
  75.      * @param SwcConfig $config Configuration for the concrete project.
  76.      * @since v1.1.0
  77.      */
  78.     function SwcArchivePrintCreationFormCell($config){            
  79.         $sel_frame = (isset($_POST[IDX_MAIN_FRAME_SRC]) ? $_POST[IDX_MAIN_FRAME_SRC] : '');
  80.         $sel_action = (isset($_POST[IDX_ACTION]) ? $_POST[IDX_ACTION] : '');
  81.         $actions = array(
  82.             array(IDX_MAIN_FRAME_SRC, STD_ARCHIVE_CONTENT), 
  83.             array(IDX_ACTION, ACTION_CREATE_ARCHIVE));
  84.             
  85.         $sel_go_button = strcmp($sel_frame, STD_ARCHIVE_CONTENT) == 0 && strcmp($sel_action, ACTION_CREATE_ARCHIVE) == 0;
  86.         $text = '';
  87.         if(isset($_POST[IDX_ACTION_CREATE_ARCHIVE])){
  88.             $text = $_POST[IDX_ACTION_CREATE_ARCHIVE];
  89.         } else {
  90.             $text = $config->GetProjectName();
  91.             $text .= '_r'.GetWebspaceRevision($config);
  92.             $text = CreateSystemFriendlyString($text);
  93.         }
  94.         
  95.         OpenTableCell();
  96.             OpenTable();
  97.                 OpenFormCell($_SESSION[IDX_ROOT_URL]);
  98.                     OpenTable();
  99.                         OpenTableCell('text_low_bold');                                                        
  100.                             PrintFormTextField(T(TK_CTRL_LABEL_CREATE_ARCHIVE), IDX_ACTION_CREATE_ARCHIVE, 50, $text);
  101.                         CloseTableCell();
  102.                         OpenTableCell();
  103.                             PrintFormButton(T(TK_CTRL_ACTION_CREATE_ARCHIVE), $actions, $sel_go_button);
  104.                         CloseTableCell();
  105.                         $hints = array(IDX_REUSE_SESSION, '1');
  106.                         $overwrite_file = (isset($_POST[OPT_OVERWRITE_ARCHIVE]) && $_POST[OPT_OVERWRITE_ARCHIVE]);
  107.                         PrintTableCheckboxCells(T(TK_CTRL_LABEL_OVERWRITE_ARCHIVE), OPT_OVERWRITE_ARCHIVE,
  108.                                                                     $_SESSION[IDX_ROOT_URL], $hints, 'text_low', 
  109.                                                                     'text_low', $overwrite_file, NULL, NULL, true);
  110.                     CloseTable();
  111.                 CloseFormCell();
  112.         
  113.         if (isset($_POST[IDX_ACTION]) && $_POST[IDX_ACTION] == ACTION_CREATE_ARCHIVE){
  114.             ob_flush();
  115.             NewTableRow();
  116.                 OpenTableCell('text_red', 2);
  117.                     $result_msg = CreateSpaceball($config, $_POST[IDX_ACTION_CREATE_ARCHIVE], $overwrite_file);
  118.                     echo $result_msg;
  119.                 CloseTableCell();
  120.         } 
  121.             CloseTable();
  122.         CloseTableCell();
  123.     }    
  124.     
  125.     /** 
  126.      * Prints a cell containing the list of existing archives including
  127.      * file selection.
  128.      * @param SwcConfig $config Configuration for the concrete project.
  129.      * @since v1.1.0
  130.      */
  131.     function SwcArchivePrintFileListCell($config){
  132.         $path = $config->GetSpaceballDir();
  133.         $url = $config->GetSpaceballUrl();
  134.         if (!file_exists($path)){
  135.             mkdir($path, 0777);
  136.         }
  137.         $files = array();
  138.         GetFiles($path, $files, true);
  139.         if (isset($files[IDX_EXEC_RES])){
  140.             PrintTableCell($files[IDX_EXEC_RES], 'text_bold_red');
  141.         } else if (empty($files)){
  142.             PrintTableCell(T(TK_RESULT_NO_SPACEBALLS));
  143.         } else {
  144.             natsort($files);
  145.             $spaceball_hints = parse_ini_file($path.'/'.SPACEBALL_HINT_FILE, true);
  146.             $revision_map = (isset($spaceball_hints[SECTION_REV_MAP]) ? $spaceball_hints[SECTION_REV_MAP] : array());
  147.     
  148.             OpenTableCell();
  149.                 OpenTable();
  150.                     PrintTableCell(T(TK_SB_LIST_LABEL_SPACEBALLS), 'text_low_bold');
  151.                     PrintTableCell(T(TK_SB_LIST_LABEL_REVISION), 'text_low_bold');
  152.                     PrintTableCell(T(TK_SB_LIST_LABEL_TIMESTAMP), 'text_low_bold');
  153.                     PrintTableCell(T(TK_SB_LIST_LABEL_FILESIZE), 'text_low_bold');
  154.                     foreach($files as $file){
  155.                             NewTableRow();
  156.                             $abs_path = $path.'/'.$file;
  157.                             $timestamp = filemtime($abs_path);
  158.                             $filetime = GetLocalizedTimestamp($timestamp, true);
  159.                             $filesize = filesize($abs_path);
  160.                             $size_unit_prefix = '';
  161.                             if ($filesize > (10 * 1024 - 1)){
  162.                                 $size_unit_prefix = 'k';
  163.                                 $filesize /= 1024;
  164.                             }
  165.                             if ($filesize > (10 * 1024 - 1)){
  166.                                 $size_unit_prefix = 'M';
  167.                                 $filesize /= 1024;
  168.                             }
  169.                             $filesize = number_format($filesize, 0, ',', ' ');
  170.                             $rev = (isset($revision_map[$file]) ? $revision_map[$file] : '?');
  171.                             PrintTableLinkCell($url.'/'.$file, $file, '_blank', 'explicit_link');
  172.                             PrintTableCell('&nbsp;'.$rev, 'text_low');
  173.                             PrintTableCell('&nbsp;'.$filetime, 'text_low');
  174.                             PrintTableCell('&nbsp;'.$filesize.' '.$size_unit_prefix.'B', 'text_low');
  175.                     }
  176.                 CloseTable();
  177.             CloseTableCell();
  178.         }
  179.     }    
  180.         
  181.     /** 
  182.      * Creates a file archive of the current webspace called "Spaceball".
  183.      * @param SwcConfig $config Configuration for the concrete project.
  184.      * @param string $file_name Name of the Spaceball file. It is stored within
  185.      *  archive path as set in SwcConfig.
  186.      * @param boolean $overwrite Whether or not an existing Spaceball should
  187.      *  be overwritten.
  188.      * @return string Textual result of the operation.
  189.      * @since v1.1.0
  190.      */
  191.     function CreateSpaceball($config, $file_name, $overwrite){
  192.         $sb_dir = $config->GetSpaceballDir();
  193.         $file_name .= '.zip';
  194.         $spaceball_file = $sb_dir.'/'.$file_name;
  195.         // Check if file exists and can be overwritten
  196.         if (file_exists($spaceball_file)){
  197.             if ($overwrite){
  198.                 if (!unlink($spaceball_file)){
  199.                     $msg = T(TK_ERROR_SPACEBALL_EXISTS);
  200.                     return $msg;
  201.                 }
  202.             } else {
  203.                 $msg = T(TK_ERROR_SPACEBALL_EXISTS_NO_OVERWRITE);
  204.                 return $msg;
  205.             }
  206.         }
  207.                 
  208.         $webspace = $config->GetWebspaceRootDir();
  209.         $msg = sprintf(T(TK_SB_MSG_SPACEBALL_CREATION_SUCCESSFULL__NAME), $file_name); 
  210.         $file_list = array();
  211.         GetFiles($webspace, $file_list);
  212.         if (count($file_list) == 0){
  213.             $msg = T(TK_ERROR_SPACEBALL_EMPTY_WEBSPACE);
  214.             return $msg;
  215.         }
  216.         
  217.         // Process zip file
  218.         $zip = new zipfile();
  219.         foreach($file_list as $file){
  220.             $path = $webspace.$file;
  221.             // check if it is not an spaceball
  222.             if (strlen($path) < strlen($sb_dir) ||
  223.                     substr_compare($path, $sb_dir, 0, strlen($sb_dir), true) != 0){
  224.                 $content = file_get_contents($path);
  225.                 $filetime = filemtime($path);
  226.                 $zip->addFile($content, $file, $filetime);
  227.             }
  228.         }
  229.         // Save the spaceball and create path if necessary.
  230.         $spaceball = str_replace('\\', '/', $file_name);
  231.         $path = dirname($file_name);
  232.         if (strlen($path) > 1){
  233.             $path = $sb_dir.'/'.$path;
  234.             if (!file_exists($path)){
  235.                 if (!mkdir($path)){
  236.                     $msg = T(TK_ERROR_SPACEBALL_GENERAL_ERROR);
  237.                     return $msg;
  238.                 }
  239.             }
  240.         }
  241.         if (!$zip->store($spaceball_file)){
  242.             $msg = T(TK_ERROR_SPACEBALL_GENERAL_ERROR);
  243.             return $msg;
  244.         }
  245.         
  246.         // Update revision map
  247.         $revision = GetWebspaceRevision($config);
  248.         $spaceball_hint_file = $sb_dir.'/'.SPACEBALL_HINT_FILE;
  249.         $spaceball_hints = array();
  250.         if (file_exists($spaceball_hint_file)){
  251.             $spaceball_hints = parse_ini_file($spaceball_hint_file, true);
  252.         }
  253.         $spaceball_hints[SECTION_REV_MAP][$file_name] = $revision;
  254.         $hint_content = "; Simple file that contains hints about generated Spaceballs.\n".
  255.                                         "; This file is automatically generated.\n".
  256.                                         "; PLEASE DO NOT EDIT IT MANUALLY!\n\n";    
  257.         foreach($spaceball_hints as $group => $data){
  258.             $hint_content .= '['.$group."]\n";
  259.             foreach($data as $key => $val){
  260.                 $hint_content .= $key.' = '.$val."\n";
  261.             }
  262.             $hint_content .= "\n";
  263.         }
  264.         file_put_contents($spaceball_hint_file, $hint_content);    
  265.         return $msg;
  266.     }
  267.     
  268.     /**
  269.      * Returns the files recursively including subdirectory starting with the
  270.      * given path. Hidden files and directories can be ignored,
  271.      * <i>.svn</i> directories are always ignored. If an error occurs,
  272.      * it is returned as only array entry.
  273.      * @param string $path Absolute path of the dirctory to start with.
  274.      * @param array $files Array in which the file structure or
  275.      *  translated error message in index <i>IDX_EXEC_RES</i> is returned.
  276.      * @param string $rel_path Relative path for files, empty per default.
  277.      * @since v1.1.0
  278.      */
  279.     function GetFiles($path, &$files, $ignore_hidden = false, $rel_path = ''){
  280.         clearstatcache();
  281.         if (!file_exists($path)){
  282.             $files[IDX_EXEC_RES] = (T(TK_ERROR_ARCHIVE_DIR_NOT_EXIST__PATH, $path));
  283.             return;
  284.         }
  285.         if (!is_dir($path)){
  286.             $files[IDX_EXEC_RES] = (T(TK_ERROR_ARCHIVE_DIR_IS_FILE_NOT_DIR__PATH, $path));
  287.             return;
  288.         }
  289.         
  290.         $src_dh  = opendir($path);
  291.         $filename = readdir($src_dh);
  292.         while ($filename !== false){
  293.             if ($filename != '.' && $filename != '..' && $filename != '.svn' &&
  294.                     (!$ignore_hidden || strpos($filename, '.') !== 0)){
  295.                 if (is_dir($path.'/'.$filename)){
  296.                     GetFiles($path.'/'.$filename, $files, $ignore_hidden, $rel_path.($rel_path != '' ? '/' : '').$filename);                    
  297.                 } else {
  298.                     $files[] = $rel_path.($rel_path != '' ? '/' : '').$filename;
  299.                 }
  300.             }        
  301.             $filename = readdir($src_dh);        
  302.         }
  303.     }
  304. ?>

Documentation generated on Fri, 03 Nov 2006 18:45:02 +0100 by phpDocumentor 1.3.0RC6
for sTEFANs POWERED BY eBC.bz