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