Changeset 752719
- Timestamp:
- 08/07/2013 05:25:00 AM (13 years ago)
- Location:
- siteapps/trunk
- Files:
-
- 5 edited
-
classes/PluginWPBase.php (modified) (1 diff)
-
classes/SiteAppsAPI.php (modified) (3 diffs)
-
classes/SiteAppsCallbacks.php (modified) (5 diffs)
-
classes/SiteAppsMessage.php (modified) (2 diffs)
-
views/admin/settings.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
siteapps/trunk/classes/PluginWPBase.php
r752619 r752719 21 21 $page = get_bloginfo( "wpurl" ) . "/wp-admin/admin.php?page=" . $page . $additional_flags; 22 22 wp_redirect($page); 23 exit; 23 24 } 24 25 } -
siteapps/trunk/classes/SiteAppsAPI.php
r752619 r752719 3 3 class SiteAppsAPI 4 4 { 5 6 private $messages; 5 7 6 8 public function __contruct() … … 9 11 throw new Exception('curl not loaded'); 10 12 } 13 $this->messages = new SiteAppsMessage(); 11 14 } 12 15 … … 20 23 } 21 24 22 public function signupUser($name, $email, $url, $privateKey, $publicKey) 25 private function createUser($name, $email, $privateKey, $publicKey) 26 { 27 $userParam = json_encode(array('user_name' => $name, 'user_email' => $email)); 28 $hash = hash_hmac('sha256', $userParam, $privateKey); 29 $user = $this->getResponse('Account/add', $hash, $userParam, $publicKey); 30 if ( !array_key_exists('user_id', $user) || !array_key_exists('user_key', $user)) { 31 throw new Exception('No user data.'); 32 } 33 return $user; 34 } 35 36 private function createSite($email, $url, $userKey , $privateKey, $publicKey) 37 { 38 $siteParam = json_encode(array('user_email' => $email, 'site_url' => $url)); 39 $hash = hash_hmac('sha256', $siteParam, $privateKey . $userKey); 40 $site = $this->getResponse('Site/add', $hash, $siteParam, $publicKey); 41 if ( !array_key_exists('site_id', $site) || !array_key_exists('site_key', $site)) { 42 throw new Exception('No site data.'); 43 } 44 return $site; 45 } 46 47 private function getSegments($siteAppsId, $email, $userKey, $privateKey, $publicKey) 48 { 49 $segmentsParam = json_encode(array('site_id' => $siteAppsId, 'email' => $email)); 50 $hash = hash_hmac('sha256', $segmentsParam, $privateKey . $userKey); 51 $segments = $this->getResponse('Segment/getSegments', $hash, $segmentsParam, $publicKey); 52 return $segments; 53 } 54 55 private function addFlags($flags, $siteId, $email, $userKey , $privateKey, $publicKey) 23 56 { 24 57 try { 25 $user = json_encode(array('user_name' => $name, 'user_email' => $email)); 26 $hash = hash_hmac('sha256', $params, $privateKey); 27 28 $newUser = $this->submit('User/add', $hash, $user, $publicKey); 29 30 //new Site 31 $site = json_encode(array('user_name' => $name, 'user_email' => $email)); 32 33 34 $newSite = $this->submit('Site/add', $hash, $site, $publicKey); 35 //save Flags 36 $flags = json_encode(array('user_name' => $name, 'user_email' => $email)); 37 38 39 $newSite = $this->submit('Site/addFlags', $hash, $flags, $publicKey); 40 41 $segmentsArray = json_decode($segmentsReturn, 1); 42 43 if ($segmentsArray['status'] == 100 && is_array($segmentsArray['content'])) { 44 $segments = $segmentsArray['content']; 45 } 58 $flagsParam = json_encode(array('flags' => $flags, 'site_id' => $siteId, 'user_email' => $email)); 59 $hash = hash_hmac('sha256', $flagsParam, $privateKey . $userKey); 60 $this->getResponse('Site/addFlags', $hash, $flagsParam, $publicKey); 46 61 } catch (Exception $e) { 47 $ segments = array();62 $this->messages->showCustomMessage($e->getMessage(), true); 48 63 } 49 return $segments; 64 } 65 66 public function createAccount($name, $email, $url, $privateKey, $publicKey) 67 { 68 try { 69 $user = $this->createUser($name, $email, $privateKey, $publicKey); 70 $site = $this->createSite($email, $url, $user['user_key'], $privateKey, $publicKey); 71 $this->addFlags(array('platform' => array('wordpress', 'plugin-wordpress')), $site['site_id'], $email, $user['user_key'], $privateKey, $publicKey); 72 return array_merge($user, $site); 73 } catch (Exception $e) { 74 $this->messages->showCustomMessage($e->getMessage(), true); 75 return null; 76 } 50 77 } 51 78 52 79 public function getSegmentsByClient($siteAppsId, $email, $privateKey, $publicKey, $userKey) 53 80 { 54 $segments = array();55 81 try { 56 $params = json_encode(array('site_id' => $siteAppsId, 'email' => $email)); 57 58 $hash = hash_hmac('sha256', $params, $privateKey . $userKey); 59 60 $segmentsReturn = $this->submit('Segment/getSegments', $hash, $params, $publicKey); 61 $segmentsArray = json_decode($segmentsReturn, 1); 62 63 if ($segmentsArray['status'] == 100 && is_array($segmentsArray['content'])) { 64 $segments = $segmentsArray['content']; 65 } 82 return $this->getSegments($siteAppsId, $email, $userKey, $privateKey, $publicKey); 66 83 } catch (Exception $e) { 67 $segments = array(); 84 $this->messages->showCustomMessage($e->getMessage(), true); 85 return array(); 68 86 } 69 return $segments; 87 } 88 89 private function getResponse($endpoint, $hash, $params, $publicKey) 90 { 91 $response = json_decode($this->submit($endpoint, $hash, $params, $publicKey), 1); 92 93 if ($response['status'] == 100 && is_array($response['content'])) { 94 return $response['content']; 95 } else { 96 throw new Exception($response['msg']); 97 } 70 98 } 71 99 -
siteapps/trunk/classes/SiteAppsCallbacks.php
r752619 r752719 2 2 3 3 include_once SITEAPPS_CLASS_DIR . "PluginWPBase.php"; 4 include_once SITEAPPS_CLASS_DIR . "SiteAppsAPI.php"; 4 5 5 6 class SiteAppsCallbacks extends PluginWPBase … … 28 29 } elseif (isset($_POST['siteapps_reset']) && $_POST['siteapps_reset']) { 29 30 $this->reset(); 31 } elseif (isset($_POST['siteapps_create_account']) && $_POST['siteapps_create_account']) { 32 $this->signup(); 30 33 } 31 34 } … … 66 69 67 70 if ($options['id']) { 68 $exists = $this->updateSegments($options['id'], $options['user_email'], $options['user_key']); 69 } 70 //Is it ok ? No curl, no tag 71 if ($exists) { 72 // Colocar uma mensagem falando que nao foi possível se comunicar com a api 71 $this->updateSegments($options['id'], $options['user_email'], $options['user_key']); 73 72 } 74 73 $pluginConfig->saveOptions($options); … … 77 76 78 77 $this->redirect(SiteAppsPages::SETTINGS); 79 } elseif (isset($_POST['sa_id']) && $_POST['sa_id'] == 0) {78 } elseif (isset($_POST['sa_id']) && $_POST['sa_id'] < 1) { 80 79 $this->addHeadWarning(SiteAppsMessage::INVALID_SITEAPPS_ID); 80 } 81 } 82 83 private function signup() 84 { 85 $this->canEditOptions(); 86 $pluginConfig = new PluginConfig(); 87 $options = PluginConfig::getOptions(); 88 89 if ($_POST['siteapps_signup_site_url'] && $_POST['siteapps_signup_email'] && $_POST['siteapps_signup_name']) { 90 $options['site_url'] = $_POST['siteapps_signup_site_url']; 91 $options['user_email'] = $_POST['siteapps_signup_email']; 92 $options['user_name'] = $_POST['siteapps_signup_name']; 93 94 $siteAppsAPI = new SiteAppsAPI(); 95 $account = $siteAppsAPI->createAccount($options['user_name'], $options['user_email'], $options['site_url'], $options['private_key'], $options['public_key']); 96 97 if ($account == null) { 98 return false; 99 } 100 101 $options['id'] = $account['site_id']; 102 $options['user_id'] = $account['user_id']; 103 $options['user_key'] = $account['user_key']; 104 $options['site_key'] = $account['site_key']; 105 106 $options['tag'] = $pluginConfig->getTag($options['id'], $options['type']); 107 if ($options['id']) { 108 $this->updateSegments($options['id'], $options['user_email'], $options['user_key']); 109 } 110 $pluginConfig->saveOptions($options); 111 112 $this->addHeadWarning(SiteAppsMessage::TAG_ON); 113 114 $this->redirect(SiteAppsPages::SETTINGS); 115 } else { 116 $this->addHeadWarning(SiteAppsMessage::INVALID_SIGNUP_PARAMS); 81 117 } 82 118 } … … 88 124 $options = PluginConfig::getOptions(); 89 125 90 include_once SITEAPPS_CLASS_DIR . "SiteAppsAPI.php"; 126 91 127 $siteAppsAPI = new SiteAppsAPI(); 92 128 $segments = $siteAppsAPI->getSegmentsByClient($siteId, $email, $options['private_key'], $options['public_key'], $userKey); -
siteapps/trunk/classes/SiteAppsMessage.php
r752619 r752719 12 12 const CONFIGURE_SITEAPPS = 'configureSiteApps'; 13 13 const SITEAPPS_ID_NOT_FOUND = 'siteAppsIdNotFound'; 14 const USER_NOT_FOUND = 'userNotFound'; 15 const INVALID_SIGNUP_PARAMS = 'invalidSignupParams'; 16 17 private $msg; 18 private $error; 19 20 public function addHeadWarning($msg) 21 { 22 add_action('admin_head', array($this, $msg)); 23 } 24 25 public function addCustomMessage($msg, $error = false) 26 { 27 $this->msg = $msg; 28 $this->error = $error; 29 add_action('admin_head', array($this, 'showCustomMessage')); 30 } 31 32 public function showCustomMessage() 33 { 34 self::notify(sprintf(__($this->msg)), $this->error); 35 } 36 37 public static function notify($message, $error=false) 38 { 39 if (!$error) { 40 print '<div class="updated fade"><p>'.$message.'</p></div>'; 41 } else { 42 print '<div class="error"><p>'.$message.'</p></div>'; 43 } 44 } 14 45 15 46 public function invalidSignupParams() 47 { 48 self::notify('Your Name, User E-mail or Site Url is invalid.', true); 49 } 50 51 public function userNotFound() 52 { 53 self::notify('User not found.', true); 54 } 55 16 56 public function curlNotLoaded() 17 57 { … … 48 88 self::notify(sprintf(__('We couldn\'t find your SiteApps ID or your Email/User Key. Your Segments won\'t be updated.')), true); 49 89 } 50 51 public static function notify($message, $error=false) 52 { 53 if (!$error) { 54 print '<div class="updated fade"><p>'.$message.'</p></div>'; 55 } else { 56 print '<div class="error"><p>'.$message.'</p></div>'; 57 } 58 } 90 59 91 } -
siteapps/trunk/views/admin/settings.php
r752619 r752719 17 17 </th> 18 18 <td> 19 <input type="text" value="<?php print $name; ?>" name=" name" id="name" placeholder="Your Name">19 <input type="text" value="<?php print $name; ?>" name="siteapps_signup_name" id="siteapps_signup_name" placeholder="Your Name"> 20 20 </td> 21 21 </tr> … … 25 25 </th> 26 26 <td> 27 <input type="text" value="<?php print $email; ?>" name=" email" id="email" placeholder="Email">27 <input type="text" value="<?php print $email; ?>" name="siteapps_signup_email" id="siteapps_signup_email" placeholder="Email"> 28 28 </td> 29 29 </tr> … … 33 33 </th> 34 34 <td> 35 <input type="text" value="<?php print $siteUrl; ?>" name="site _url" id="site_url" placeholder="www.siteapps.com">35 <input type="text" value="<?php print $siteUrl; ?>" name="siteapps_signup_site_url" id="siteapps_signup_site_url" placeholder="www.siteapps.com"> 36 36 </td> 37 37 </table>
Note: See TracChangeset
for help on using the changeset viewer.