Plugin Directory

Changeset 752719


Ignore:
Timestamp:
08/07/2013 05:25:00 AM (13 years ago)
Author:
marcelioleal
Message:

changes for signup

Location:
siteapps/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • siteapps/trunk/classes/PluginWPBase.php

    r752619 r752719  
    2121    $page = get_bloginfo( "wpurl" ) . "/wp-admin/admin.php?page=" . $page . $additional_flags;
    2222    wp_redirect($page);
     23        exit;
    2324    }
    2425}   
  • siteapps/trunk/classes/SiteAppsAPI.php

    r752619 r752719  
    33class SiteAppsAPI
    44{
     5   
     6    private $messages;
    57   
    68    public function __contruct()
     
    911            throw new Exception('curl not loaded');
    1012        }
     13        $this->messages = new SiteAppsMessage();
    1114    }
    1215   
     
    2023    }
    2124   
    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)
    2356    {
    2457        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);
    4661        } catch (Exception $e) {
    47             $segments = array();
     62            $this->messages->showCustomMessage($e->getMessage(), true);
    4863        }
    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        }
    5077    }
    5178   
    5279    public function getSegmentsByClient($siteAppsId, $email, $privateKey, $publicKey, $userKey)
    5380    {
    54         $segments = array();
    5581        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);
    6683        } catch (Exception $e) {
    67             $segments = array();
     84            $this->messages->showCustomMessage($e->getMessage(), true);
     85            return array();
    6886        }
    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        }
    7098    }
    7199   
  • siteapps/trunk/classes/SiteAppsCallbacks.php

    r752619 r752719  
    22
    33include_once SITEAPPS_CLASS_DIR . "PluginWPBase.php";
     4include_once SITEAPPS_CLASS_DIR . "SiteAppsAPI.php";
    45
    56class SiteAppsCallbacks extends PluginWPBase
     
    2829        } elseif (isset($_POST['siteapps_reset']) && $_POST['siteapps_reset']) {
    2930            $this->reset();
     31        } elseif (isset($_POST['siteapps_create_account']) && $_POST['siteapps_create_account']) {
     32            $this->signup();
    3033        }
    3134    }
     
    6669
    6770            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']);
    7372            }
    7473            $pluginConfig->saveOptions($options);
     
    7776           
    7877            $this->redirect(SiteAppsPages::SETTINGS);
    79         } elseif (isset($_POST['sa_id']) && $_POST['sa_id'] == 0) {
     78        } elseif (isset($_POST['sa_id']) && $_POST['sa_id'] < 1) {
    8079            $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);
    81117        }
    82118    }
     
    88124            $options = PluginConfig::getOptions();
    89125           
    90             include_once SITEAPPS_CLASS_DIR . "SiteAppsAPI.php";
     126
    91127            $siteAppsAPI    = new SiteAppsAPI();
    92128            $segments       = $siteAppsAPI->getSegmentsByClient($siteId, $email, $options['private_key'], $options['public_key'], $userKey);
  • siteapps/trunk/classes/SiteAppsMessage.php

    r752619 r752719  
    1212    const CONFIGURE_SITEAPPS = 'configureSiteApps';
    1313    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    }
    1445
    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   
    1656    public function curlNotLoaded()
    1757    {
     
    4888        self::notify(sprintf(__('We couldn\'t find your SiteApps ID or your Email/User Key. Your Segments won\'t be updated.')), true);
    4989    }
    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
    5991}
  • siteapps/trunk/views/admin/settings.php

    r752619 r752719  
    1717                        </th>
    1818                        <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">
    2020                        </td>
    2121                        </tr>
     
    2525                                </th>
    2626                        <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">
    2828                    </td>
    2929                        </tr>
     
    3333                                </th>
    3434                        <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">
    3636                        </td>
    3737                        </table>
Note: See TracChangeset for help on using the changeset viewer.