public static function get_components_synchro_data($app_id) { $components = array(); $components_data = array(); WpakAddons::require_app_addons_php_files($app_id); $components_raw = WpakComponentsStorage::get_components($app_id); if (!empty($components_raw)) { $globals = array(); foreach ($components_raw as $component) { $component_data = WpakComponentsTypes::get_component_data($component, $globals); // // Don't include null component into the webservice's return // Component data could be null if an addon's component has been added to the app and the addon isn't activated anymore // An addon could be seen as deactivated either if the corresponding plugin is deactivated, or if the corresponding checkbox is unchecked for the given app // if (null === $component_data) { continue; } $globals = $component_data['globals']; $components[$component->slug] = $component_data['specific']; } $navigation_items = WpakNavigationItemsStorage::get_navigation_indexed_by_components_slugs($app_id, true); $navigation_items = apply_filters('wpak_navigation_items', $navigation_items, WpakApps::get_app_slug($app_id)); $components_data['navigation'] = $navigation_items; $components_data['components'] = $components; $components_data['globals'] = $globals; $components_data['addons'] = WpakAddons::get_app_addons_dynamic_data($app_id); } return $components_data; }
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)); }
protected static function load_addons($force_reload = false) { if (self::$addons === null || $force_reload) { $addons = array(); $addons_raw = apply_filters('wpak_addons', array()); if (!empty($addons_raw) && is_array($addons_raw)) { foreach ($addons_raw as $addon) { if ($addon instanceof WpakAddon) { $addons[$addon->slug] = $addon; } } } self::$addons = $addons; } }
public static function get_config_js($app_id, $echo = false) { $wp_ws_url = WpakWebServices::get_app_web_service_base_url($app_id); $theme = WpakThemesStorage::get_current_theme($app_id); $app_slug = WpakApps::get_app_slug($app_id); $app_main_infos = WpakApps::get_app_main_infos($app_id); $app_title = $app_main_infos['title']; $app_version = WpakApps::sanitize_app_version($app_main_infos['version']); $debug_mode = WpakBuild::get_app_debug_mode($app_id); $auth_key = WpakApps::get_app_is_secured($app_id) ? WpakToken::get_hash_key() : ''; //TODO : options to choose if the auth key is displayed in config.js. $options = WpakOptions::get_app_options($app_id); $addons = WpakAddons::get_app_addons_for_config($app_id); if (!$echo) { ob_start(); } //Indentation is a bit funky here so it appears ok in the config.js file source: ?> define( function ( require ) { "use strict"; return { app_slug : '<?php echo $app_slug; ?> ', wp_ws_url : '<?php echo $wp_ws_url; ?> ', theme : '<?php echo addslashes($theme); ?> ', version : '<?php echo $app_version; ?> ', app_title : '<?php echo addslashes($app_title); ?> ', debug_mode : '<?php echo $debug_mode; ?> '<?php if (!empty($auth_key)) { ?> , auth_key : '<?php echo $auth_key; ?> '<?php } ?> , options : <?php echo json_encode($options); ?> , addons : <?php echo json_encode($addons); ?> }; }); <?php $content = ''; if (!$echo) { $content = ob_get_contents(); ob_end_clean(); } return !$echo ? $content : ''; }
public static function build_app_sources($app_id, $export_type = 'phonegap-build') { $answer = array(); if (!extension_loaded('zip')) { $answer['ok'] = 0; $answer['msg'] = __('Zip PHP extension is required to run file export. See http://www.php.net/manual/fr/book.zip.php.', WpAppKit::i18n_domain); return $answer; } if (!self::create_export_directory_if_doesnt_exist()) { $export_directory = self::get_export_files_path(); $answer['ok'] = 0; $answer['msg'] = sprintf(__('The export directory [%s] could not be created. Please check that you have the right permissions to create this directory.', WpAppKit::i18n_domain), $export_directory); return $answer; } $current_theme = WpakThemesStorage::get_current_theme($app_id); $plugin_dir = plugin_dir_path(dirname(dirname(__FILE__))); $appli_dir = $plugin_dir . 'app'; $export_filename = self::get_export_file_base_name($app_id); $export_filename_full = self::get_export_files_path() . "/" . $export_filename . '.zip'; $answer = self::build_zip($app_id, $appli_dir, $export_filename_full, array($current_theme), WpakAddons::get_app_addons($app_id), $export_type); $answer['export'] = $export_filename; $answer['export_full_name'] = $export_filename_full; return $answer; }
public static function ajax_wpak_edit_component() { $answer = array('ok' => 0, 'message' => '', 'type' => 'error', 'html' => '', 'component' => array()); if (empty($_POST['post_id']) || empty($_POST['nonce']) || !check_admin_referer('wpak-component-data-' . $_POST['post_id'], 'nonce')) { exit('bad nonce'); } $action = $_POST['wpak_action']; $data = $_POST['data']; WpakAddons::require_app_addons_php_files(intval($_POST['post_id'])); if ($action == 'add_or_update') { // Unslash POST data before manipulating DB $data = wp_unslash($data); $post_id = $data['component_post_id']; if (empty($post_id)) { $answer['message'] = __("Application not found.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } $edit = !empty($data['component_id']); $edit_id = $edit ? intval($data['component_id']) : 0; $component_label = trim($data['component_label']); $component_type = $data['component_type']; if (empty($component_label)) { $answer['message'] = __('You must provide a label for the component!', WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (is_numeric($component_label)) { $answer['message'] = __("The component label can't be numeric.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } $component_slug = $edit ? trim($data['component_slug']) : $component_label; $component_slug = sanitize_title_with_dashes(remove_accents($component_slug)); if (empty($component_slug)) { $answer['message'] = __("You must provide a slug for the component.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (is_numeric($component_slug)) { $answer['message'] = __("The component slug can't be numeric.", WpAppKit::i18n_domain); self::exit_sending_json($answer); } if (WpakComponentsStorage::component_exists($post_id, $component_slug, $edit_id)) { $i = 0; do { $component_index = intval(preg_replace('/.*-(\\d+)$/', '$1', $component_slug)); $component_index++; $component_slug = preg_replace('/-(\\d+)$/', '', $component_slug) . '-' . $component_index; if ($i++ > 100) { break; } } while (WpakComponentsStorage::component_exists($post_id, $component_slug, $edit_id)); } $component_options = WpakComponentsTypes::get_component_type_options_from_posted_form($component_type, $data); $component = new WpakComponent($component_slug, $component_label, $component_type, $component_options); $component_id = WpakComponentsStorage::add_or_update_component($post_id, $component, $edit_id); $answer['component'] = array('id' => $component_id, 'slug' => $component_slug, 'label' => $component_label); $answer['html'] = self::get_component_row($post_id, WpakComponentsStorage::get_nb_components($post_id), $component_id, $component); if ($edit) { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = sprintf(__('Component "%s" updated successfuly', WpAppKit::i18n_domain), $component_label); } else { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = sprintf(__('Component "%s" created successfuly', WpAppKit::i18n_domain), $component_label); } self::exit_sending_json($answer); } elseif ($action == 'delete') { $id = $data['component_id']; $post_id = $data['post_id']; if (is_numeric($id) && is_numeric($post_id)) { if ($component_id = WpakComponentsStorage::component_exists($post_id, $id)) { if (WpakNavigationItemsStorage::navigation_item_exists_by_component($post_id, $component_id)) { $answer['message'] = __('The component to delete is in the app navigation. Please remove the component from app navigation before deleting it.', WpAppKit::i18n_domain); } else { if (!WpakComponentsStorage::delete_component($post_id, $id)) { $answer['message'] = __('Could not delete component', WpAppKit::i18n_domain); } else { $answer['ok'] = 1; $answer['type'] = 'updated'; $answer['message'] = __('Component deleted successfuly', WpAppKit::i18n_domain); } } } else { $answer['message'] = __('Component to delete not found', WpAppKit::i18n_domain); } } self::exit_sending_json($answer); } //We should not arrive here, but just in case : self::exit_sending_json($answer); }