]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/externalAPI/Base/OAuthPluginBase.php
Release 6.2.0beta4
[Github/sugarcrm.git] / include / externalAPI / Base / OAuthPluginBase.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM 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 class OAuthPluginBase extends ExternalAPIBase implements ExternalOAuthAPIPlugin {
41     public $authMethod = 'oauth';
42     protected $oauthParams = array();
43
44     public function __construct()
45     {
46         $connector = $this->getConnector();
47         if(!empty($connector)) {
48             $cons_key = $connector->getProperty('oauth_consumer_key');
49             if(!empty($cons_key)) {
50                 $this->oauthParams['consumerKey'] = $cons_key;
51             }
52             $cons_secret = $connector->getProperty('oauth_consumer_secret');
53             if(!empty($cons_secret)) {
54                 $this->oauthParams['consumerSecret'] = $cons_secret;
55             }
56         }
57     }
58
59     /**
60      * Load data from EAPM bean
61      * @see ExternalAPIBase::loadEAPM()
62      */
63     public function loadEAPM($eapmBean)
64     {
65         if ( !parent::loadEAPM($eapmBean) ) { return false; }
66
67         $this->oauth_token = $eapmBean->oauth_token;
68         $this->oauth_secret = $eapmBean->oauth_secret;
69
70         return true;
71     }
72
73     /**
74      * Check login
75      * @param EAPM $eapmBean
76      * @see ExternalAPIBase::checkLogin()
77      */
78     public function checkLogin($eapmBean = null)
79     {
80         $reply = parent::checkLogin($eapmBean);
81         if ( !$reply['success'] ) {
82             return $reply;
83         }
84
85         if ( $this->checkOauthLogin() ) {
86             return array('success' => true);
87         }
88     }
89
90     public function quickCheckLogin()
91     {
92         $reply = parent::quickCheckLogin();
93
94         if ( !$reply['success'] ) {
95             return $reply;
96         }
97
98         if ( !empty($this->oauth_token) && !empty($this->oauth_secret) ) {
99             return array('success'=>true);
100         } else {
101             return array('success'=>false,'errorMessage'=>translate('LBL_ERR_NO_TOKEN','EAPM'));
102         }
103     }
104
105     protected function checkOauthLogin()
106     {
107         if ( empty($this->oauth_token) || empty($this->oauth_secret) ) {
108             return $this->oauthLogin();
109         } else {
110             return false;
111         }
112     }
113
114     public function getOauthParams()
115     {
116         return $this->getValue("oauthParams");
117     }
118
119     public function getOauthRequestURL()
120     {
121         return $this->getValue("oauthReq");
122     }
123
124     public function getOauthAuthURL()
125     {
126         return $this->getValue("oauthAuth");
127     }
128
129     public function getOauthAccessURL()
130     {
131         return $this->getValue("oauthAccess");
132     }
133
134     /**
135      * Get OAuth client
136      * @return SugarOauth
137      */
138     public function getOauth()
139     {
140
141         $oauth = new SugarOAuth($this->oauthParams['consumerKey'], $this->oauthParams['consumerSecret'], $this->getOauthParams());
142
143         if ( isset($this->oauth_token) && !empty($this->oauth_token) ) {
144             $oauth->setToken($this->oauth_token, $this->oauth_secret);
145         }
146
147         return $oauth;
148     }
149
150    public function oauthLogin()
151    {
152         global $sugar_config;
153         $oauth = $this->getOauth();
154         if(isset($_SESSION['eapm_oauth_secret']) && isset($_SESSION['eapm_oauth_token']) && isset($_REQUEST['oauth_token']) && isset($_REQUEST['oauth_verifier'])) {
155             $stage = 1;
156         } else {
157             $stage = 0;
158         }
159         if($stage == 0) {
160             $oauthReq = $this->getOauthRequestURL();
161             $callback_url = $sugar_config['site_url'].'/index.php?module=EAPM&action=oauth&record='.$this->eapmBean->id;
162             $callback_url = $this->formatCallbackURL($callback_url);
163             
164             $GLOBALS['log']->debug("OAuth request token: {$oauthReq} callback: $callback_url");
165             
166             $request_token_info = $oauth->getRequestToken($oauthReq, $callback_url);
167             
168             $GLOBALS['log']->debug("OAuth token: ".var_export($request_token_info, true));
169
170             if(empty($request_token_info['oauth_token_secret']) || empty($request_token_info['oauth_token'])){
171                 return false;
172             }else{
173                 // FIXME: error checking here
174                 $_SESSION['eapm_oauth_secret'] = $request_token_info['oauth_token_secret'];
175                 $_SESSION['eapm_oauth_token'] = $request_token_info['oauth_token'];
176                 $authReq = $this->getOauthAuthURL();
177                 SugarApplication::redirect("{$authReq}?oauth_token={$request_token_info['oauth_token']}");
178             }
179         } else {
180             $accReq = $this->getOauthAccessURL();
181             $oauth->setToken($_SESSION['eapm_oauth_token'],$_SESSION['eapm_oauth_secret']);
182             $GLOBALS['log']->debug("OAuth access token: {$accReq}");
183             $access_token_info = $oauth->getAccessToken($accReq);
184             $GLOBALS['log']->debug("OAuth token: ".var_export($access_token_info, true));
185             // FIXME: error checking here
186             $this->oauth_token = $access_token_info['oauth_token'];
187             $this->oauth_secret = $access_token_info['oauth_token_secret'];
188             $this->eapmBean->oauth_token = $this->oauth_token;
189             $this->eapmBean->oauth_secret = $this->oauth_secret;
190             $oauth->setToken($this->oauth_token, $this->oauth_secret);
191             $this->eapmBean->validated = 1;
192             $this->eapmBean->save();
193             unset($_SESSION['eapm_oauth_token']);
194             unset($_SESSION['eapm_oauth_secret']);
195             return true;
196         }
197         return false;
198         }
199 }