spreadus_url = 'http://preproduction.spread.us'; } // Check if the user wants to logout. if(isset($_GET['action']) && $_GET['action'] == 'logout') { $this->logout(); return; } // Render the header require_once('views/header.php'); // Set account_name, secret & authentication state $this->account_name = get_option('spreadus.account_name'); $this->secret = get_option('spreadus.secret'); $this->authenticated = get_option('spreadus.authenticated'); // Check if we can process a form if($_SERVER['REQUEST_METHOD'] == 'POST') { $this->process_form(); } // Setup finished? if( !(strlen($this->account_name) > 5) || !(strlen($this->secret) > 5) ) { $this->setup(); return; } // Correct account_name & secret? $response = $this->call_api('check_account', array( 'account_name' => $this->account_name, 'hash' => $this->hash($this->account_name) )); if(isset($response->error) && $response->code != 'down') { $this->logout(); return; } // Do we have a plan? $response = $this->call_api('check_plan', array( 'account_name' => $this->account_name, 'hash' => $this->hash($this->account_name) )); if(isset($response->code) && $response->code == 'no_plan') { $this->plan(); return; } else { $this->account_status = isset($response->success) ? $response->success : $response->error; } // All good, render the settings page. $this->settings(); return; } /** * Uninstall current Spread.us settings */ public function logout() { update_option('spreadus.account_name', false); update_option('spreadus.secret', false); //update_option('spreadus.account', false); update_option('spreadus.authenticated', false); $this->set_notice('Your Spread.us settings have been removed.'); $this->refresh(false); } /** * Render setup form */ private function setup() { $domain_parts = explode('.',$_SERVER['SERVER_NAME']); $domain = $domain_parts[count($domain_parts)-2] . '.' . $domain_parts[count($domain_parts)-1]; switch($_GET['action']) { /*case 'signup': $view = 'signup'; break;*/ case 'set_account': $callback = base64_decode($_GET['callback']); $callback = explode('|', $callback); $this->sanitize_post_data($callback); update_option('spreadus.account_name', $callback[0]); update_option('spreadus.secret', $callback[1]); $view = 'set_account'; break; default: $view = 'login'; break; } require_once('views/' . $view . '.php'); } /** * Render plan panel */ private function plan() { require_once('views/plans.php'); } /** * Render settings panel */ private function settings() { $spreading = get_option('spreadus.settings'); // Render settings top part require_once('views/settings_top.php'); /* // Get account settings $response = $this->call_api('get_account', array( 'account_name' => $this->account_name, 'hash' => $this->hash($this->account_name) )); // Can we get account settings? if(!isset($response->error)) { $account = get_object_vars($response->account); require_once('views/settings_main.php'); } else { require_once('views/down.php'); } */ // Render settings bottom part require_once('views/settings_bottom.php'); } /** * Process incoming forms */ private function process_form() { switch($_POST['form']) { case 'setup': $this->process_setup(); break; case 'settings': $this->process_settings(); break; default: $this->catch_form(); break; } } /** * Process the setup form */ private function process_setup() { switch($_GET['action']) { /*case 'signup': $response = $this->call_api('create_account', array( 'account_name' => $_POST['account_name'] )); $this->account_name = $response->account_name; $this->secret = $response->secret; break;*/ default: $this->account_name = $_POST['account_name']; $this->secret = $_POST['secret']; $response = $this->call_api('check_account', array( 'account_name' => $this->account_name, 'hash' => $this->hash($this->account_name) )); break; } if(!isset($response->error)) { update_option('spreadus.account_name', $this->account_name); update_option('spreadus.secret', $this->secret); update_option('spreadus.authenticated', '1'); $this->set_notice($response->success); $this->refresh(false); } else { $this->set_notice($response->error, 'error'); $this->refresh(); } } /** * Process the settings form */ private function process_settings() { $allowed_spreading_settings = array( 'message_caption', 'default_publish_address', 'no_automated_spreading' ); /*$allowed_account_settings = array( 'display_name', 'description', 'url', 'support_email', 'twitter_consumer_key', 'twitter_consumer_secret', 'facebook_app_id', 'facebook_consumer_secret', 'shortener', 'awesm_key', 'bitly_username', 'bitly_key', 'smtp_host', 'smtp_port', 'smtp_username', 'smtp_password' );*/ $spreading = array(); //$account_data = array(); foreach($_POST as $key => $value) { if(in_array($key, $allowed_spreading_settings)) { //update_option('spreadus.spreading.' . $key, htmlentities($value)); $spreading[$key] = $value; } /*elseif(in_array($key, $allowed_account_settings)) { //update_option('spreadus.account.' . $key, htmlentities($value)); $account_data[$key] = $value; }*/ } if(count($spreading) > 0) { $this->sanitize_post_data($spreading); update_option('spreadus.settings', $spreading); } /*if(count($account_data) > 0) { $data = array( 'account_name' => $this->account_name, 'hash' => $this->hash($this->account_name, $account_data), ); foreach($account_data as $key => $field) { $data['account'][$key] = $field; } //var_dump($account_data); //var_dump($data); //die(); $response = $this->call_api('save_account', $data); if(!isset($response->error)) { $this->set_notice($response->success); } else { $this->set_notice($response->error, 'error'); } }*/ $this->refresh(); } /** * Sanitize post data, make sure all data is safe. */ private function sanitize_post_data(&$value) { if(is_array($value)) { array_walk_recursive($value, array($this, 'sanitize_post_data')); } else { $value = htmlentities($value); } } /** * Catch unrecognized form submits */ private function catch_form() { $this->set_notice('I could not process that form action...', 'error'); $this->refresh(); } /** * Add a notice to the notices queue */ private function set_notice($message, $type = 'updated') { $notice = '
× ' . $message . '
API call no. ' . $this->api_calls . ':
'; var_dump($return); if($return->code == 'down') { echo 'Return body of fatal API call:
'; echo '