<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  /*===================================================
    ShowThemes()
  ===================================================*/

  function ShowThemes() {

    // vars global configuration
    global $active_theme, $admin_template_path, $theme_path, $active_theme, $all_themes, $theme, $tbl_class, $is_active;

    // vars url & form

    // vars messages
    global $msg;

    // vars template
    global $theme_name, $file_name;

    // get themes

    $handle=opendir($theme_path);
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      elseif($file=="indexu_theme.idf") { }
      else $themes[] = $file;
    }
    closedir($handle);

    sort($themes);

    $i=0;
    foreach($themes as $theme) {
      $all_themes .= "<p><b><a href=template_export.php?pflag=dl&theme={$theme}>{$theme}</a></b></p>";
    }

    DisplayTemplate($admin_template_path . "template_export_form.html", "\$all_themes");
  }


  /*===================================================
    DLThemes()
  ===================================================*/

  function DLThemes() {

    // vars global configuration
    global $active_theme, $admin_template_path, $theme_path, $active_theme, $all_themes, $theme, $tbl_class, $is_active;

    // vars url & form

    // vars messages
    global $msg;

    // vars template
    global $theme_name, $file_name;
    
    // get all files
    
    
    $loc    = "{$theme_path}{$active_theme}";
    
    $handle = opendir($loc);
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/$file";
        $files_path[] = "{$active_theme}/$file";
      }
    }
    closedir($handle);
    
    $handle = opendir($loc.'/blocks/');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/blocks/$file";
        $files_path[] = "{$active_theme}/blocks/$file";
      }
    }
    closedir($handle);
    
    $handle = opendir($loc.'/cp/');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/cp/$file";
        $files_path[] = "{$active_theme}/cp/$file";
      }
    }
    closedir($handle);

    $handle = opendir($loc.'/mail/');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/mail/$file";
        $files_path[] = "{$active_theme}/mail/$file";
      }
    }
    closedir($handle);

    $handle = opendir($loc.'/rss/');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/rss/$file";
        $files_path[] = "{$active_theme}/rss/$file";
      }
    }
    closedir($handle);
    
    $handle = opendir($loc.'/images/');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/images/$file";
        $files_path[] = "{$active_theme}/images/$file";
      }
    }
    closedir($handle);

    $handle = opendir($loc.'/images/review');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/images/review/$file";
        $files_path[] = "{$active_theme}/images/review/$file";
      }
    }
    closedir($handle);
    
    $handle = opendir($loc.'/images/rating');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/images/rating/$file";
        $files_path[] = "{$active_theme}/images/rating/$file";
      }
    }
    closedir($handle);

    $handle = opendir($loc.'/images/pr');
    while ($file = readdir($handle)) {
      if($file[0]==".") { }
      else {
        $files[]      = "$loc/images/pr/$file";
        $files_path[] = "{$active_theme}/images/pr/$file";
      }
    }
    closedir($handle);

    // process zip
    $zip= new ss_zip('',6);
    
    $i = 0;
    $count_files = @count($files);
    while ($i < $count_files) {
      @$zip->add_file($files[$i],$files_path[$i]);
      $i++;
    }
    
    $filename = "../backup/{$active_theme}.tpl.zip";
    $zip->save($filename);
    
    // set header for download
    
    header("Pragma: public"); 
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    RunPostFilter(__FILE__);
  }


  /*===================================================
    main
  ===================================================*/

  include "../application.php";
  include "../lib/ss_zip.class.php";
  ini_set("max_execution_time", "3600");

  if ($pflag == "dl") {
    RemoveFilter("gzip_compression");
    RemoveFilter("runtime");
  }
  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    DisplayTemplate($admin_template_path . "header.html");
    ShowThemes();
    DisplayTemplate($admin_template_path . "footer.html");
  }
  elseif ($pflag == 'dl') {
    DLThemes();
  }

  RunPostFilter(__FILE__);

?>