]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/externalAPI/Base/OAuthPluginBase.php
Release 6.4.0
[Github/sugarcrm.git] / include / externalAPI / Base / OAuthPluginBase.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once('include/externalAPI/Base/ExternalAPIBase.php');
39
40 /**
41  * External API based on OAuth
42  * @api
43  */
44 class OAuthPluginBase extends ExternalAPIBase implements ExternalOAuthAPIPlugin {
45     public $authMethod = 'oauth';
46     protected $oauthParams = array();
47     protected $oauth_keys_initialized = false;
48
49     public function __construct()
50     {
51     }
52
53     /**
54      * Setup oauth parameters from connector
55      */
56     public function setupOauthKeys()
57     {
58         if($this->oauth_keys_initialized) return;
59
60         $connector = $this->getConnector();
61         if(!empty($connector)) {
62             $cons_key = $connector->getProperty('oauth_consumer_key');
63             if(!empty($cons_key)) {
64                 $this->oauthParams['consumerKey'] = $cons_key;
65             }
66             $cons_secret = $connector->getProperty('oauth_consumer_secret');
67             if(!empty($cons_secret)) {
68                 $this->oauthParams['consumerSecret'] = $cons_secret;
69             }
70         }
71         $this->oauth_keys_initialized = true;
72     }
73
74     /**
75      * Load data from EAPM bean
76      * @see ExternalAPIBase::loadEAPM()
77      */
78     public function loadEAPM($eapmBean)
79     {
80         if ( !parent::loadEAPM($eapmBean) ) { return false; }
81
82         $this->oauth_token = $eapmBean->oauth_token;
83         $this->oauth_secret = $eapmBean->oauth_secret;
84
85         return true;
86     }
87
88     /**
89      * Check login
90      * @param EAPM $eapmBean
91      * @see ExternalAPIBase::checkLogin()
92      */
93     public function checkLogin($eapmBean = null)
94     {
95         $reply = parent::checkLogin($eapmBean);
96         if ( !$reply['success'] ) {
97             return $reply;
98         }
99
100         if ( $this->checkOauthLogin() ) {
101             return array('success' => true);
102         }
103     }
104
105     public function quickCheckLogin()
106     {
107         $reply = parent::quickCheckLogin();
108
109         if ( !$reply['success'] ) {
110             return $reply;
111         }
112
113         if ( !empty($this->oauth_token) && !empty($this->oauth_secret) ) {
114             return array('success'=>true);
115         } else {
116             return array('success'=>false,'errorMessage'=>translate('LBL_ERR_NO_TOKEN','EAPM'));
117         }
118     }
119
120     protected function checkOauthLogin()
121     {
122         if ( empty($this->oauth_token) || empty($this->oauth_secret) ) {
123             return $this->oauthLogin();
124         } else {
125             return false;
126         }
127     }
128
129     public function getOauthParams()
130     {
131         return $this->getValue("oauthParams");
132     }
133
134     public function getOauthRequestURL()
135     {
136         return $this->getValue("oauthReq");
137     }
138
139     public function getOauthAuthURL()
140     {
141         return $this->getValue("oauthAuth");
142     }
143
144     public function getOauthAccessURL()
145     {
146         return $this->getValue("oauthAccess");
147     }
148
149     /**
150      * Get OAuth client
151      * @return SugarOauth
152      */
153     public function getOauth()
154     {
155         $this->setupOauthKeys();
156         $oauth = new SugarOAuth($this->oauthParams['consumerKey'], $this->oauthParams['consumerSecret'], $this->getOauthParams());
157
158         if ( isset($this->oauth_token) && !empty($this->oauth_token) ) {
159             $oauth->setToken($this->oauth_token, $this->oauth_secret);
160         }
161
162         return $oauth;
163     }
164
165    public function oauthLogin()
166    {
167         global $sugar_config;
168         $oauth = $this->getOauth();
169         if(isset($_SESSION['eapm_oauth_secret']) && isset($_SESSION['eapm_oauth_token']) && isset($_REQUEST['oauth_token']) && isset($_REQUEST['oauth_verifier'])) {
170             $stage = 1;
171         } else {
172             $stage = 0;
173         }
174         if($stage == 0) {
175             $oauthReq = $this->getOauthRequestURL();
176             $callback_url = $sugar_config['site_url'].'/index.php?module=EAPM&action=oauth&record='.$this->eapmBean->id;
177             $callback_url = $this->formatCallbackURL($callback_url);
178
179             $GLOBALS['log']->debug("OAuth request token: {$oauthReq} callback: $callback_url");
180
181             $request_token_info = $oauth->getRequestToken($oauthReq, $callback_url);
182
183             $GLOBALS['log']->debug("OAuth token: ".var_export($request_token_info, true));
184
185             if(empty($request_token_info['oauth_token_secret']) || empty($request_token_info['oauth_token'])){
186                 return false;
187             }else{
188                 // FIXME: error checking here
189                 $_SESSION['eapm_oauth_secret'] = $request_token_info['oauth_token_secret'];
190                 $_SESSION['eapm_oauth_token'] = $request_token_info['oauth_token'];
191                 $authReq = $this->getOauthAuthURL();
192                 SugarApplication::redirect("{$authReq}?oauth_token={$request_token_info['oauth_token']}");
193             }
194         } else {
195             $accReq = $this->getOauthAccessURL();
196             $oauth->setToken($_SESSION['eapm_oauth_token'],$_SESSION['eapm_oauth_secret']);
197             $GLOBALS['log']->debug("OAuth access token: {$accReq}");
198             $access_token_info = $oauth->getAccessToken($accReq);
199             $GLOBALS['log']->debug("OAuth token: ".var_export($access_token_info, true));
200             // FIXME: error checking here
201             $this->oauth_token = $access_token_info['oauth_token'];
202             $this->oauth_secret = $access_token_info['oauth_token_secret'];
203             $this->eapmBean->oauth_token = $this->oauth_token;
204             $this->eapmBean->oauth_secret = $this->oauth_secret;
205             $oauth->setToken($this->oauth_token, $this->oauth_secret);
206             $this->eapmBean->validated = 1;
207             $this->eapmBean->save();
208             unset($_SESSION['eapm_oauth_token']);
209             unset($_SESSION['eapm_oauth_secret']);
210             return true;
211         }
212         return false;
213         }
214 }