示例#1
0
 private static function exit_handle_request($app_id_or_slug, $service_slug, $action, $id = 0)
 {
     global $wp_query;
     self::log($_SERVER['REQUEST_METHOD'] . ' : ' . $action . ' : ' . print_r($_REQUEST, true));
     //Set AJAX WP context :
     define('DOING_AJAX', true);
     if (self::cache_on()) {
         //TODO_WPAK
         /* $cached_webservice = WpakCache::get_cached_web_service(
         	  self::get_web_service_cache_id($service),
         	  isset($_GET['force_reload']) && is_numeric($_GET['force_reload']) && $_GET['force_reload'] == 1,
         	  isset($_GET['last_update']) && is_numeric($_GET['last_update']) ? $_GET['last_update'] : 0
         	  );
         	  if( !empty($cached_webservice) ){
         	  self::exit_sending_web_service_content($cached_webservice);
         	  } */
     }
     $app = WpakApps::get_app($app_id_or_slug);
     //Check that the asked app exists :
     if (empty($app)) {
         header("HTTP/1.0 404 Not Found");
         _e('App not found', WpAppKit::i18n_domain) . ' : [' . $app_id_or_slug . ']';
         exit;
     }
     $app_id = $app->ID;
     $app_slug = $app->post_name;
     WpakWebServiceContext::$current_app_id = $app_id;
     WpakWebServiceContext::$current_app_slug = $app_slug;
     //Some browsers or viewports on mobile devices cache HTTP resquests, we don't want this!
     header("Cache-Control: no-cache, must-revalidate");
     // HTTP/1.1
     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
     // Some time in the past
     if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
         header('Allow: GET, PUT, DELETE, POST');
         header('Access-Control-Allow-Origin: *');
         header('Access-Control-Allow-Methods: GET, PUT, DELETE, POST');
         header('Access-Control-Allow-Headers: origin, content-type, accept, x-http-method-override');
         header('Access-Control-Allow-Credentials: true');
         exit;
     }
     //If the app current theme has some PHP (hooks!) to be executed before the web
     //service process, include it here :
     WpakThemes::include_app_theme_php($app_id);
     //Include PHP files required by addons activated for this app :
     WpakAddons::require_app_addons_php_files($app_id);
     $service_answer = null;
     switch ($action) {
         case 'list':
             if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                 $headers = function_exists('apache_request_headers') ? apache_request_headers() : array();
                 $is_url_encoded = !empty($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/x-www-form-urlencoded') !== false || !empty($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false;
                 if ($is_url_encoded) {
                     if (isset($_POST['model'])) {
                         //Specific to backbone's "emulateJSON"
                         $json = stripslashes($_POST['model']);
                         $sent = json_decode($json);
                     } else {
                         $sent = $_POST;
                     }
                 } else {
                     $json = file_get_contents("php://input");
                     $sent = json_decode($json);
                 }
                 $service_answer = WpakWebServiceCrud::create($app_id, $service_slug, $sent);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
                 $service_answer = WpakWebServiceCrud::read($app_id, $service_slug, $wp_query->query_vars);
             }
             break;
         case 'one':
             if ($_SERVER['REQUEST_METHOD'] == 'GET') {
                 $service_answer = WpakWebServiceCrud::read_one($app_id, $service_slug, $id);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') {
                 $json = file_get_contents("php://input");
                 $new = json_decode($json);
                 $service_answer = WpakWebServiceCrud::update($app_id, $service_slug, $new);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
                 $service_answer = WpakWebServiceCrud::delete($app_id, $service_slug, $id);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
                 $http_method_override_method = '';
                 $headers = function_exists('apache_request_headers') ? apache_request_headers() : array();
                 if (!empty($headers['X-HTTP-Method-Override'])) {
                     $http_method_override_method = $headers['X-HTTP-Method-Override'];
                 } elseif (!empty($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
                     $http_method_override_method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
                 }
                 $is_url_encoded = !empty($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/x-www-form-urlencoded') !== false || !empty($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false;
                 //self::log('$_SERVER : '. print_r($_SERVER,true));
                 self::log('X-HTTP-Method-Override : ' . $http_method_override_method);
                 if (!empty($http_method_override_method)) {
                     if ($http_method_override_method == 'PUT') {
                         if ($is_url_encoded) {
                             if (isset($_POST['model'])) {
                                 //Specific to backbone's "emulateJSON"
                                 $json = stripslashes($_POST['model']);
                                 $sent = json_decode($json);
                             } else {
                                 $sent = $_POST;
                             }
                             self::log('PUT one (X-HTTP-Method-Override + emulateJSON) : ' . $id . ' - json :' . $json . ' - _POST : ' . print_r($_POST, true));
                         } else {
                             $data = file_get_contents("php://input");
                             $new = json_decode($data);
                             self::log('PUT one (X-HTTP-Method-Override) : ' . $id . ' : ' . $data);
                         }
                         if ($new !== null) {
                             $service_answer = WpakWebServiceCrud::update($app_id, $service_slug, $new);
                         }
                     } elseif ($http_method_override_method == 'DELETE') {
                         self::log('DELETE one (X-HTTP-Method-Override) : ' . $id);
                         $service_answer = WpakWebServiceCrud::delete($app_id, $service_slug, $id);
                     }
                 }
             }
             break;
     }
     //Simulate delay : TODO : make this configurable in WP BO :
     //time_nanosleep(rand(0,1), (floatval(rand(20,100))/100) * 1000000000);
     //sleep(2);
     if ($service_answer !== null) {
         self::exit_sending_answer($service_answer, $app_id, $service_slug);
     }
     exit(__('Error : Web service not recognised', WpAppKit::i18n_domain));
 }
示例#2
0
 public static function get_available_themes($with_data = false)
 {
     $available_themes = array();
     $directory = self::get_themes_directory();
     if (file_exists($directory) && is_dir($directory)) {
         if ($handle = opendir($directory)) {
             while (false !== ($entry = readdir($handle))) {
                 if ($entry != '.' && $entry != '..' && strpos($entry, '.') !== 0) {
                     $entry_full_path = $directory . '/' . $entry;
                     if (is_dir($entry_full_path)) {
                         if ($with_data) {
                             $available_themes[$entry] = WpakThemes::get_theme_data($entry);
                         } else {
                             $available_themes[] = $entry;
                         }
                     }
                 }
             }
             closedir($handle);
         }
     }
     return $available_themes;
 }
示例#3
0
    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 
        }
    }
示例#4
0
 protected static function add_rewrite_rules()
 {
     WpakWebServices::add_rewrite_tags_and_rules();
     WpakConfigFile::rewrite_rules();
     WpakThemes::rewrite_rules();
 }
示例#5
0
 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;
 }
    public static function inner_main_infos_box($post, $current_box)
    {
        $available_themes = WpakThemes::get_available_themes(true);
        $current_theme = WpakThemesStorage::get_current_theme($post->ID);
        $main_infos = WpakApps::get_app_main_infos($post->ID);
        ?>

		<?php 
        if (!empty($available_themes)) {
            ?>
			<label><?php 
            _e('Choose theme', WpAppKit::i18n_domain);
            ?>
 : </label>
			<select name="wpak_app_theme_choice" id="wpak_app_theme_choice">
				<?php 
            foreach ($available_themes as $theme_slug => $theme_data) {
                ?>
					<?php 
                $selected = $theme_slug == $current_theme ? 'selected="selected"' : '';
                ?>
					<option value="<?php 
                echo $theme_slug;
                ?>
" <?php 
                echo $selected;
                ?>
><?php 
                echo $theme_data['Name'];
                ?>
 </option>
				<?php 
            }
            ?>
			</select>
		<?php 
        } else {
            ?>
			<div class="wpak_no_theme">
				<strong><?php 
            _e('No WP AppKit theme found!', WpAppKit::i18n_domain);
            ?>
</strong>
				<br/>
				<?php 
            echo sprintf(__('Please upload a WP AppKit theme from the "<a href="%s" >Upload Themes</a>" panel or copy a theme directly to the %s directory.', WpAppKit::i18n_domain), '/wp-admin/admin.php?page=wpak_bo_upload_themes', basename(WP_CONTENT_DIR) . '/' . WpakThemes::themes_directory);
            ?>
			</div>
		<?php 
        }
        ?>

		<?php 
        foreach ($available_themes as $theme => $theme_data) {
            ?>
			<div class="wpak-theme-data" id="wpak-theme-data-<?php 
            echo $theme;
            ?>
" style="display:none">
				<div class="theme-data-content">
					<?php 
            echo $theme_data['Description'];
            ?>

					<?php 
            $theme_meta = array();
            if (!empty($theme_data['Version'])) {
                $theme_meta[] = sprintf(__('Version %s'), $theme_data['Version']);
            }
            if (!empty($theme_data['Author'])) {
                $author = $theme_data['Author'];
                if (!empty($theme_data['AuthorURI'])) {
                    $author = '<a href="' . $theme_data['AuthorURI'] . '">' . $theme_data['Author'] . '</a>';
                }
                $theme_meta[] = sprintf(__('By %s'), $author);
            }
            if (!empty($theme_data['ThemeURI'])) {
                $theme_meta[] = sprintf('<a href="%s">%s</a>', esc_url($theme_data['ThemeURI']), __('Visit theme site'));
            }
            ?>

					<?php 
            if (!empty($theme_meta)) {
                ?>
						<div class="theme-meta-data"><?php 
                echo implode(' | ', $theme_meta);
                ?>
</div>
					<?php 
            }
            ?>
				</div>
			</div>
		<?php 
        }
        ?>

		<div class="wpak-app-title wpak_settings">
			<label><?php 
        _e('Application Title (displayed in app top bar)', WpAppKit::i18n_domain);
        ?>
</label> : <br/>
			<input id="wpak_app_title" type="text" name="wpak_app_title" value="<?php 
        echo $main_infos['title'];
        ?>
" />
		</div>

		<?php 
        wp_nonce_field('wpak-theme-data-' . $post->ID, 'wpak-nonce-theme-data');
        ?>

		<style>
			.wpak-theme-data{ padding:9px 12px; margin-bottom: 10px }
			.theme-data-content{ margin-top: 0 }
			.wpak-app-title{ margin-top: 15px; border-top: 1px solid #ddd; padding-top:10px }
			.theme-meta-data{ margin-top: 7px }
			.wpak_no_theme{ text-align: center; font-size:120%; line-height: 2em; margin:30px }
		</style>

		<script>
			(function(){
				var $ = jQuery;
				$('#wpak_app_theme_choice').change(function(){
					$('.wpak-theme-data').hide();
					var theme = this.value;
					$('#wpak-theme-data-'+ theme).show();
				});
				$('#wpak_app_theme_choice').change();
			})();
		</script>

		<?php 
        do_action('wpak_inner_main_infos_box', $post, $current_box);
    }