public static function settings_panel() { if (isset($_GET['wpak_action']) && $_GET['wpak_action'] == 'upload-theme') { if (!current_user_can('upload_plugins') && !current_user_can('wpak_edit_apps')) { wp_die(__('You do not have sufficient permissions to install WP AppKit themes on this site.', WpAppKit::i18n_domain)); } check_admin_referer('wpak-theme-upload'); include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; $file_upload = new File_Upload_Upgrader('themezip', 'package'); $file_type = wp_check_filetype($file_upload->filename); if ($file_type['ext'] == 'zip' && $file_type['type'] == 'application/zip') { $title = sprintf(__('Installing WP AppKit from uploaded file: %s', WpAppKit::i18n_domain), esc_html(basename($file_upload->filename))); $nonce = 'wpak-theme-upload'; $url = add_query_arg(array('package' => $file_upload->id)); // A nonce is passed to WP_Upgrader_Skin class, so wp_nonce_url() is called and url is escaped there... $upgrader = new WP_Upgrader(new WP_Upgrader_Skin(compact('title', 'nonce', 'url'))); $destination_folder_name = basename(sanitize_file_name($file_upload->filename), ".zip"); $result = $upgrader->run(array('package' => $file_upload->package, 'destination' => WpakThemes::get_themes_directory() . '/' . $destination_folder_name, 'clear_destination' => true, 'clear_working' => true, 'hook_extra' => array())); if ($result || is_wp_error($result)) { $file_upload->cleanup(); } if (!is_wp_error($result)) { echo sprintf(__("WP AppKit theme '%s' installed successfully!", WpAppKit::i18n_domain), $destination_folder_name); } else { _e('An error occured', WpAppKit::i18n_domain); echo ' : ' . $result->get_error_message(); } echo '<br/><br/><a href="' . esc_url(remove_query_arg('wpak_action')) . '">' . __('Back to theme upload form', WpAppKit::i18n_domain) . '</a>'; echo '<br/><br/><a href="' . admin_url() . '/edit.php?post_type=wpak_apps">' . __('Go to my WP AppKit app list', WpAppKit::i18n_domain) . '</a>'; } else { _e("Uploaded file must be a valid zip file", WpAppKit::i18n_domain); } } else { ?> <div class="wrap" id="wpak-settings"> <h2><?php _e('WP AppKit Themes upload', WpAppKit::i18n_domain); ?> </h2> <?php if (!empty($result['message'])) { ?> <div class="<?php echo $result['type']; ?> " ><p><?php echo $result['message']; ?> </p></div> <?php } ?> <div class="upload-plugin"> <p class="install-help"><?php _e('If you have a WP AppKit theme in a .zip format, you may install it by uploading it here.'); ?> </p> <form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo esc_url(add_query_arg(array('wpak_action' => 'upload-theme'))); ?> "> <?php wp_nonce_field('wpak-theme-upload'); ?> <label class="screen-reader-text" for="themezip"><?php _e('WP AppKit Theme zip file', WpAppKit::i18n_domain); ?> </label> <input type="file" id="themezip" name="themezip" /> <?php submit_button(__('Install Now'), 'button', 'install-theme-submit', false); ?> </form> </div> </div> <?php } }
private static function build_zip($app_id, $source, $destination, $themes, $addons, $export_type) { $answer = array('ok' => 1, 'msg' => ''); if (!extension_loaded('zip') || !file_exists($source)) { $answer['msg'] = sprintf(__('The Zip archive file [%s] could not be created. Please check that you have the permissions to write to this directory.', WpAppKit::i18n_domain), $destination); $answer['ok'] = 0; return $answer; } $zip = new ZipArchive(); // // ZipArchive::open() returns TRUE on success and an error code on failure, not FALSE // All other used ZipArchive methods return FALSE on failure // // Apparently ZipArchive::OVERWRITE is not sufficient for recent PHP versions (>= 5.2.8, cf. comments here: http://fr.php.net/manual/en/ziparchive.open.php) // if (true !== ($error_code = $zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE))) { switch ($error_code) { case ZipArchive::ER_EXISTS: $error = _x('File already exists', 'ZipArchive::ER_EXISTS error', WpAppKit::i18n_domain); break; case ZipArchive::ER_INCONS: $error = _x('Zip archive inconsistent', 'ZipArchive::ER_INCONS error', WpAppKit::i18n_domain); break; case ZipArchive::ER_INVAL: $error = _x('Invalid argument', 'ZipArchive::ER_INVAL error', WpAppKit::i18n_domain); break; case ZipArchive::ER_MEMORY: $error = _x('Malloc failure', 'ZipArchive::ER_MEMORY error', WpAppKit::i18n_domain); break; case ZipArchive::ER_NOENT: $error = _x('No such file', 'ZipArchive::ER_NOENT error', WpAppKit::i18n_domain); break; case ZipArchive::ER_NOZIP: $error = _x('Not a zip archive', 'ZipArchive::ER_NOZIP error', WpAppKit::i18n_domain); break; case ZipArchive::ER_OPEN: $error = _x('Can\'t open file', 'ZipArchive::ER_OPEN error', WpAppKit::i18n_domain); break; case ZipArchive::ER_READ: $error = _x('Read error', 'ZipArchive::ER_READ error', WpAppKit::i18n_domain); break; case ZipArchive::ER_SEEK: $error = _x('Seek error', 'ZipArchive::ER_SEEK error', WpAppKit::i18n_domain); break; default: $error = ''; } $answer['msg'] = sprintf(__('The Zip archive file [%s] could not be opened (%s). Please check that you have the permissions to write to this directory.', WpAppKit::i18n_domain), $destination, $error); $answer['ok'] = 0; return $answer; } if (is_dir($source) === true) { $source_root = ''; if ($export_type === 'phonegap-cli') { //PhoneGap CLI export is made in www subdirectory //( only config.xml stays at zip root ) $source_root = 'www'; if (!$zip->addEmptyDir($source_root)) { $answer['msg'] = sprintf(__('Could not add directory [%s] to zip archive', WpAppKit::i18n_domain), $source_root); $answer['ok'] = 0; return $answer; } } if (!empty($source_root)) { $source_root .= '/'; } $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $filename = str_replace($source, '', $file); $filename = wp_normalize_path($filename); $filename = ltrim($filename, '/\\'); //Themes are included separately from the wpak themes directory if (preg_match('|themes[/\\\\].+|', $filename)) { continue; } $zip_filename = $source_root . $filename; if (is_dir($file) === true) { if (!$zip->addEmptyDir($zip_filename)) { $answer['msg'] = sprintf(__('Could not add directory [%s] to zip archive', WpAppKit::i18n_domain), $zip_filename); $answer['ok'] = 0; return $answer; } } elseif (is_file($file) === true) { if ($filename == 'index.html') { $index_content = self::filter_index(file_get_contents($file)); if (!$zip->addFromString($zip_filename, $index_content)) { $answer['msg'] = sprintf(__('Could not add file [%s] to zip archive', WpAppKit::i18n_domain), $zip_filename); $answer['ok'] = 0; return $answer; } } else { if (!$zip->addFile($file, $zip_filename)) { $answer['msg'] = sprintf(__('Could not add file [%s] to zip archive', WpAppKit::i18n_domain), $zip_filename); $answer['ok'] = 0; return $answer; } } } } //Add themes files : if (!empty($themes)) { $themes_directory = WpakThemes::get_themes_directory(); if (is_dir($themes_directory)) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($themes_directory), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $filename = str_replace($themes_directory, '', $file); $filename = wp_normalize_path($filename); $filename = ltrim($filename, '/\\'); //Filter themes : $theme = preg_replace('|([^/\\\\]*)[/\\\\].*|', '$1', $filename); if (!in_array($theme, $themes)) { continue; } //Filter php directory if (preg_match('|' . $theme . '[/\\\\]php|', $filename)) { continue; } $filename = 'themes/' . $filename; $zip_filename = $source_root . $filename; if (is_dir($file) === true) { if (!$zip->addEmptyDir($zip_filename)) { $answer['msg'] = sprintf(__('Could not add directory [%s] to zip archive', WpAppKit::i18n_domain), $zip_filename); $answer['ok'] = 0; return $answer; } } elseif (is_file($file) === true) { if (!$zip->addFile($file, $zip_filename)) { $answer['msg'] = sprintf(__('Could not add file [%s] to zip archive', WpAppKit::i18n_domain), $zip_filename); $answer['ok'] = 0; return $answer; } } } } } //Add addons files : if (!empty($addons)) { foreach ($addons as $addon) { $addon_files = $addon->get_all_files(); foreach ($addon_files as $addon_file) { $zip_filename = $source_root . 'addons/' . $addon->slug . '/' . $addon_file['relative']; $zip->addFile($addon_file['full'], $zip_filename); } } } //Create config.js file : $zip->addFromString($source_root . 'config.js', WpakConfigFile::get_config_js($app_id)); //Create config.xml file (stays at zip root) : $zip->addFromString('config.xml', WpakConfigFile::get_config_xml($app_id, false, $export_type)); } else { $answer['msg'] = sprintf(__('Zip archive source directory [%s] could not be found.', WpAppKit::i18n_domain), $source); $answer['ok'] = 0; return $answer; } if (!$zip->close()) { $answer['msg'] = __('Error during archive creation', WpAppKit::i18n_domain); $answer['ok'] = 0; return $answer; } return $answer; }