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 . '

'; $notices = get_option('spreadus.notices'); if(is_array($notices)) { $notices[] = $notice; } else { $notices = array($notice); } update_option('spreadus.notices', $notices); } /** * Call a spread.us api method */ private function call_api( $action, $fields = Array() ) { $this->api_calls++; $url = $this->spreadus_url . '/actions/'; $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url . $action . '.json'); curl_setopt($c, CURLOPT_TIMEOUT, 4); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, array('data' => json_encode($fields))); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($c); curl_close($c); if(!$response || ($return = json_decode($response)) != true) { $return = new stdClass(); $return->error = 'The Spread.us API appears to be down.'; $return->code = 'down'; $return->body = $response; } if($this->debug) { echo '

API call no. ' . $this->api_calls . ':

'; var_dump($return); if($return->code == 'down') { echo '

Return body of fatal API call:

'; echo '
' .$return->body. '
'; } if($fields['account']) { var_dump($fields['account']); //die(); } } return $return; } /** * Create a secure hash */ private function hash() { $fields = func_get_args(); return sha1($this->secret . '_' . $this->implode_r('_', $fields)); } /** * Get current page url */ private function get_current_url() { $page_url = 'http'; if ($_SERVER['HTTPS'] == 'on') { $page_url .= 's'; } $page_url .= '://' . $_SERVER['SERVER_NAME']; if ($_SERVER['SERVER_PORT'] != '80') { $page_url .= ':' . $_SERVER['SERVER_PORT']; } $page_url .= $_SERVER['REQUEST_URI']; return $page_url; } /** * Refresh page, with the option to strip the current action */ private function refresh($action = true) { $new_action = ($action === true && isset($_GET['action']) && !empty($_GET['action'])) ? '&action=' . $_GET['action'] : ''; echo ' '; exit(); } /** * Recursively impode an array or object to a string */ private function implode_r($glue, $pieces) { // Not array or object? if(!is_array($pieces) && !is_object($pieces)) { return (string)$pieces; } // Do we have at least one item? if(count((array)$pieces)) { // Start new return buffer $return = ''; // Loop through the array or object foreach($pieces as $sub) { // Add new data to the return buffer $return .= $this->implode_r($glue, $sub) . $glue; } // Trim last glue piece $return = substr($return, 0, strlen($return) -strlen($glue)); } return $return; } } } /** * Initialize settings class. */ $spreadus_settings = new SpreadusSettings;