| 1 | 2 | Manuela | <?php
 | 
      
        | 2 |  |  | /**
 | 
      
        | 3 |  |  |  *
 | 
      
        | 4 |  |  |  * @category        admin
 | 
      
        | 5 |  |  |  * @package         templates
 | 
      
        | 6 |  |  |  * @author          WebsiteBaker Project
 | 
      
        | 7 |  |  |  * @copyright       Ryan Djurovich
 | 
      
        | 8 |  |  |  * @copyright       WebsiteBaker Org. e.V.
 | 
      
        | 9 |  |  |  * @link            http://websitebaker.org/
 | 
      
        | 10 |  |  |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
      
        | 11 |  |  |  * @platform        WebsiteBaker 2.8.3
 | 
      
        | 12 |  |  |  * @requirements    PHP 5.3.6 and higher
 | 
      
        | 13 |  |  |  * @version         $Id$
 | 
      
        | 14 |  |  |  * @filesource      $HeadURL$
 | 
      
        | 15 |  |  |  * @lastmodified    $Date$
 | 
      
        | 16 |  |  |  *
 | 
      
        | 17 |  |  |  */
 | 
      
        | 18 |  |  | 
 | 
      
        | 19 |  |  | // do not display notices and warnings during installation
 | 
      
        | 20 |  |  | error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
 | 
      
        | 21 |  |  | 
 | 
      
        | 22 |  |  | // Include config file and admin class file
 | 
      
        | 23 |  |  | if (!defined( 'WB_PATH' ) ){ require( dirname(dirname((__DIR__))).'/config.php' ); }
 | 
      
        | 24 |  |  | if (!class_exists('admin', false) ) { require(WB_PATH.'/framework/class.admin.php'); }
 | 
      
        | 25 |  |  | 
 | 
      
        | 26 |  |  | // suppress to print the header, so no new FTAN will be set
 | 
      
        | 27 |  |  | $admin = new admin('Addons', 'templates_install', false);
 | 
      
        | 28 |  |  | 
 | 
      
        | 29 |  |  | $js_back = ADMIN_URL.'/templates/index.php';
 | 
      
        | 30 |  |  | 
 | 
      
        | 31 |  |  | if( !$admin->checkFTAN() )
 | 
      
        | 32 |  |  | {
 | 
      
        | 33 |  |  |     $admin->print_header();
 | 
      
        | 34 |  |  |     $admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'], $js_back );
 | 
      
        | 35 |  |  | }
 | 
      
        | 36 |  |  | // After check print the header
 | 
      
        | 37 |  |  | $admin->print_header();
 | 
      
        | 38 |  |  | 
 | 
      
        | 39 |  |  | // Check if user uploaded a file
 | 
      
        | 40 |  |  | if(!isset($_FILES['userfile'])) {
 | 
      
        | 41 |  |  |     $admin->print_error($MESSAGE['GENERIC_ERROR_OPENING_FILE'], $js_back );
 | 
      
        | 42 |  |  | }
 | 
      
        | 43 |  |  | 
 | 
      
        | 44 |  |  | // Include the WB functions file
 | 
      
        | 45 |  |  | if (!function_exists('load_template')){require(WB_PATH.'/framework/functions.php');}
 | 
      
        | 46 |  |  | 
 | 
      
        | 47 |  |  | // Set temp vars
 | 
      
        | 48 |  |  | $temp_dir = WB_PATH.'/temp/';
 | 
      
        | 49 |  |  | $temp_file = $temp_dir . $_FILES['userfile']['name'];
 | 
      
        | 50 |  |  | $temp_unzip = WB_PATH.'/temp/unzip/';
 | 
      
        | 51 |  |  | 
 | 
      
        | 52 |  |  | // Try to upload the file to the temp dir
 | 
      
        | 53 |  |  | if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $temp_file)) {
 | 
      
        | 54 |  |  |     $admin->print_error($MESSAGE['GENERIC_CANNOT_UPLOAD'], $js_back );
 | 
      
        | 55 |  |  | }
 | 
      
        | 56 |  |  | 
 | 
      
        | 57 |  |  | // Include the PclZip class file (thanks to
 | 
      
        | 58 |  |  | if(!class_exists('PclZip')){ require(WB_PATH.'/include/pclzip/pclzip.lib.php');}
 | 
      
        | 59 |  |  | 
 | 
      
        | 60 |  |  | // Remove any vars with name "template_directory" and "theme_directory"
 | 
      
        | 61 |  |  | if (isset($template_directory)){unset($template_directory);}
 | 
      
        | 62 |  |  | if (isset($theme_directory)){unset($theme_directory);}
 | 
      
        | 63 |  |  | 
 | 
      
        | 64 |  |  | // Setup the PclZip object
 | 
      
        | 65 |  |  | $archive = new PclZip($temp_file);
 | 
      
        | 66 |  |  | // Unzip the files to the temp unzip folder
 | 
      
        | 67 |  |  | $list = $archive->extract(PCLZIP_OPT_PATH, $temp_unzip);
 | 
      
        | 68 |  |  | // Check if uploaded file is a valid Add-On zip file
 | 
      
        | 69 |  |  | if (!($list || !file_exists($temp_unzip . 'info.php'))) {
 | 
      
        | 70 |  |  |   $admin->print_error($MESSAGE['GENERIC_INVALID_ADDON_FILE']);
 | 
      
        | 71 |  |  | }
 | 
      
        | 72 |  |  | 
 | 
      
        | 73 |  |  | // Include the templates info file
 | 
      
        | 74 |  |  | require($temp_unzip.'info.php');
 | 
      
        | 75 |  |  | 
 | 
      
        | 76 |  |  | // Perform Add-on requirement checks before proceeding
 | 
      
        | 77 |  |  | require(WB_PATH . '/framework/addon.precheck.inc.php');
 | 
      
        | 78 |  |  | preCheckAddon($temp_file);
 | 
      
        | 79 |  |  | 
 | 
      
        | 80 |  |  | // Delete the temp unzip directory
 | 
      
        | 81 |  |  | rm_full_dir($temp_unzip);
 | 
      
        | 82 |  |  | 
 | 
      
        | 83 |  |  | // Check if the file is valid
 | 
      
        | 84 |  |  | if(!isset($template_directory)) {
 | 
      
        | 85 |  |  |     if(file_exists($temp_file)) { unlink($temp_file); } // Remove temp file
 | 
      
        | 86 |  |  |     $admin->print_error($MESSAGE['GENERIC_INVALID']);
 | 
      
        | 87 |  |  | }
 | 
      
        | 88 |  |  | // Check if this module is already installed
 | 
      
        | 89 |  |  | // and compare versions if so
 | 
      
        | 90 |  |  | $new_template_version = $template_version;
 | 
      
        | 91 |  |  | unset($template_version);
 | 
      
        | 92 |  |  | $success_message = $MESSAGE['GENERIC_MODULE_VERSION_ERROR'];
 | 
      
        | 93 |  |  | 
 | 
      
        | 94 |  |  | if (is_dir(WB_PATH.'/templates/'.$template_directory)) {
 | 
      
        | 95 |  |  | 
 | 
      
        | 96 |  |  |     if (file_exists(WB_PATH.'/templates/'.$template_directory.'/info.php')) {
 | 
      
        | 97 |  |  |         require(WB_PATH.'/templates/'.$template_directory.'/info.php');
 | 
      
        | 98 |  |  |         $sql  = 'SELECT `version` FROM `'.TABLE_PREFIX.'addons` '
 | 
      
        | 99 |  |  |               . 'WHERE `directory`=\''.$template_directory.'\'';
 | 
      
        | 100 |  |  |         $old_template_version = $database->get_one($sql);
 | 
      
        | 101 |  |  |         // Version to be installed is older than currently installed version
 | 
      
        | 102 |  |  |         if (versionCompare($old_template_version, $new_template_version, '>=')) {
 | 
      
        | 103 |  |  |             if(file_exists($temp_file)) { unlink($temp_file); } // Remove temp file
 | 
      
        | 104 |  |  |             $admin->print_error($template_directory.'<br />'.$MESSAGE['GENERIC_ALREADY_INSTALLED'].'<br />'.'Version '.$old_template_version);
 | 
      
        | 105 |  |  |         } else {
 | 
      
        | 106 |  |  |           if (is_readable(WB_PATH.'/templates/'.$template_directory.'/upgrade.php')) {
 | 
      
        | 107 |  |  |               require(WB_PATH.'/templates/'.$template_directory.'/upgrade.php');
 | 
      
        | 108 |  |  |           }
 | 
      
        | 109 |  |  |           $success_message = $MESSAGE['GENERIC_UPGRADED'];
 | 
      
        | 110 |  |  | 
 | 
      
        | 111 |  |  |         }
 | 
      
        | 112 |  |  |     }
 | 
      
        | 113 |  |  | } else {
 | 
      
        | 114 |  |  |     $success_message = $MESSAGE['GENERIC_INSTALLED'];
 | 
      
        | 115 |  |  | }
 | 
      
        | 116 |  |  | 
 | 
      
        | 117 |  |  | // Check if template dir is writable
 | 
      
        | 118 |  |  | if(!is_writable(WB_PATH.'/templates/')) {
 | 
      
        | 119 |  |  |     if(file_exists($temp_file)) { unlink($temp_file); } // Remove temp file
 | 
      
        | 120 |  |  |     $admin->print_error($MESSAGE['TEMPLATES_BAD_PERMISSIONS']);
 | 
      
        | 121 |  |  | }
 | 
      
        | 122 |  |  | 
 | 
      
        | 123 |  |  | // Set template dir
 | 
      
        | 124 |  |  | $template_dir = WB_PATH.'/templates/'.$template_directory;
 | 
      
        | 125 |  |  | 
 | 
      
        | 126 |  |  | // Make sure the template dir exists, and chmod if needed
 | 
      
        | 127 |  |  | if(!file_exists($template_dir)) {
 | 
      
        | 128 |  |  |     make_dir($template_dir);
 | 
      
        | 129 |  |  | } else {
 | 
      
        | 130 |  |  |     change_mode($template_dir, 'dir');
 | 
      
        | 131 |  |  | }
 | 
      
        | 132 |  |  | 
 | 
      
        | 133 |  |  | // Unzip template to the template dir
 | 
      
        | 134 |  |  | $list = (int)$archive->extract(PCLZIP_OPT_PATH, $template_dir, PCLZIP_OPT_REPLACE_NEWER);
 | 
      
        | 135 |  |  | if ($list==0)
 | 
      
        | 136 |  |  | {
 | 
      
        | 137 |  |  |     $admin->print_error($MESSAGE['GENERIC_CANNOT_UNZIP']);
 | 
      
        | 138 |  |  | }
 | 
      
        | 139 |  |  | 
 | 
      
        | 140 |  |  | // Delete the temp zip file
 | 
      
        | 141 |  |  | if (file_exists($temp_file)) { unlink($temp_file); }
 | 
      
        | 142 |  |  | 
 | 
      
        | 143 |  |  | // Chmod all the uploaded files
 | 
      
        | 144 |  |  | $dir = dir($template_dir);
 | 
      
        | 145 |  |  | while(false !== $entry = $dir->read()) {
 | 
      
        | 146 |  |  |     // Skip pointers
 | 
      
        | 147 |  |  |     if(substr($entry, 0, 1) != '.' AND $entry != '.svn' AND !is_dir($template_dir.'/'.$entry)) {
 | 
      
        | 148 |  |  |         // Chmod file
 | 
      
        | 149 |  |  |         change_mode($template_dir.'/'.$entry);
 | 
      
        | 150 |  |  |     }
 | 
      
        | 151 |  |  | }
 | 
      
        | 152 |  |  | 
 | 
      
        | 153 |  |  | // Load template info into DB
 | 
      
        | 154 |  |  | if (load_template($template_dir)===true){
 | 
      
        | 155 |  |  | // Print success message
 | 
      
        | 156 |  |  |     $admin->print_success($success_message);
 | 
      
        | 157 |  |  | } else {
 | 
      
        | 158 |  |  |     $admin->print_error($MESSAGE['GENERIC_ALREADY_INSTALLED']);
 | 
      
        | 159 |  |  | }
 | 
      
        | 160 |  |  | // Print admin footer
 | 
      
        | 161 |  |  | $admin->print_footer();
 |