/** * This function attaches a authrization header to request and execute it * * @author Atul Atri * @param string $_url url to be executed * @param array $_postParams parameters array (e.g. array('username'=>'atul', 'password' => '1234'))to be sent with post or put request * @param string $_method Request Method. Any of 'GET', 'POST', 'PUT', 'DELETE'. If wrong method name is given...'GET' will be used * @param string $_method Headers to be sent with request. e.g. array('Conent-Type' =>'application/json'); * @param array $_reqOptions Any other curl request options * @param bool $_isRetry Is request being retried * @param int $_formContentType form content type * @return array array(self::SRV_CODE => 'http_response_code', self::SRV_RESPONSE => 'response') */ public function SendSingedRequest($_url, $_postParams = null, $_method = 'GET', $_headers = array(), $_reqOptions = array(), $_formContentType = self::HTTP_FORM_CONTENT_TYPE_APPLICATION, $_isRetry = false) { $_SWIFT = SWIFT::GetInstance(); $_authHeaderName = SWIFT_ConfigManager::Get('AUTH_HEADER_NAME'); $_headers[$_authHeaderName] = $_SWIFT->Settings->Get('bc_auth_token'); $_returnMe = $this->SendRequest($_url, $_postParams, $_method, $_headers, $_reqOptions, $_formContentType); if (!$_isRetry) { $this->_reqRetryCount = 0; } if ($_returnMe[SWIFT_APIHttp::SRV_CODE] == 401 && $this->_reqRetryCount < $this->_maxSendReqRetries) { //most probably token expired...ask oauth lib to regenerate token $_refreshed = SWIFT_APIOauth2::RefreshToken(); if ($_refreshed) { $this->_reqRetryCount++; $this->SendSingedRequest($_url, $_postParams, $_method, $_headers, $_reqOptions, $_formContentType, true); } } return $_returnMe; }
/** * Render manage form * * @author Atul Atri * * @return bool "true" on Success, "false" otherwise */ public function ManageForm() { //check if already autherised $_SWIFT = SWIFT::GetInstance(); $_authHeader = $_SWIFT->Settings->Get('bc_auth_token'); if ($_authHeader) { $this->Template->Assign('_alreadyAuthorised', true); } else { $this->Template->Assign('_alreadyAuthorised', false); } $this->Template->Assign('_settingsObj', $_SWIFT->Settings); $this->Template->Assign('_swiftpath', SWIFT::Get('swiftpath')); $this->Template->Assign('_Language', $this->Language); $this->Template->Assign('_UserInterface', $this->UserInterface); $_formTxt = ''; $_redirectUrl = SWIFT::Get('basename') . '/basecamp/Manager/CodeRedirect'; $this->Template->Assign('_redirectUrl', $_redirectUrl); //show only if user registered this application $_appName = $this->Settings->Get('bc_app_name'); $_appEmail = $this->Settings->Get('bc_email'); $_appId = $this->Settings->Get('bc_app_id'); $_appSecret = $this->Settings->Get('bc_app_secret'); if ($_appName || $_appEmail || $_appId || $_appSecret) { $_formTxt = $this->Language->Get('basecamp_update_app_txt'); $_formTxt = sprintf($_formTxt, '<b>' . $_redirectUrl . '</b>'); $_buttonTxt = 'basecamp_update_button'; } else { $_formTxt = $this->Language->Get('basecamp_new_app_txt'); $_bcCreatAppUrl = SWIFT_ConfigManager::Get('CREATE_APP_LNK'); $_clickHere = $this->Language->Get('basecamp_click_here_lnk'); $_bcCreateAppHref = "<b><a href='{$_bcCreatAppUrl}' target='_blank'>{$_clickHere}</a></b>"; $_formTxt = sprintf($_formTxt, $_bcCreateAppHref, '<b>' . $_redirectUrl . '</b>'); $_buttonTxt = 'basecamp_save_button'; } $_authLink = false; if ($_appName && $_appEmail && $_appId && $_appSecret) { $_authLink = SWIFT_APIOauth2::GetAuthReqURL(); } $this->Template->Assign('_authLink', $_authLink); $this->Template->Assign('_formTxt', $_formTxt); $this->Template->Assign('_buttonTxt', $_buttonTxt); $this->View->Assign('_buttonTxt', $_buttonTxt); $this->View->Assign('_appName', $_appName); $this->View->Assign('_appEmail', $_appEmail); $this->View->Assign('_appId', $_appId); $this->View->Assign('_appSecret', $_appSecret); $this->View->ManageForm(self::MENU_ID, self::NAV_ID); return true; }