public function makeRequest($method, $service, $header = array(), $body = null, $options = array())
 {
     $settings = new SBTSettings();
     $store = SBTCredentialStore::getInstance();
     $server = $settings->getURL($this->endpointName);
     if ($settings->getAuthenticationMethod($this->endpointName) == "basic") {
         $endpoint = new SBTBasicAuthEndpoint();
     } else {
         if ($settings->getAuthenticationMethod($this->endpointName) == "oauth2") {
             $endpoint = new SBTOAuth2Endpoint();
         } else {
             if ($settings->getAuthenticationMethod($this->endpointName) == "oauth1") {
                 $endpoint = new SBTOAuth1Endpoint();
             }
         }
     }
     // Make request
     $response = $endpoint->makeRequest($settings->getURL($this->endpointName), $service, $method, $options, $body, $header, $this->endpointName);
     $this->document = $response->getBody(TRUE);
     $this->_parseXML();
     return $this->document;
 }
示例#2
0
 /**
  * Routes requests.
  * 
  * @param string server			The URL of the server to which to re-direct the request to. Uses SBTSettings if none given.
  */
 public function route($server = null)
 {
     $this->loadModel('SBTSettings');
     $this->loadModel('SBTCredentialStore');
     $proxyHelper = new SBTProxyHelper();
     $store = SBTCredentialStore::getInstance();
     $settings = new SBTSettings();
     $endpointName = $proxyHelper->determineEndpoint();
     if (!isset($_REQUEST["_redirectUrl"])) {
         // Request to check if the user is authenticated
         if (isset($_REQUEST["isAuthenticated"])) {
             $_REQUEST["_redirectUrl"] = '/files/basic/api/myuserlibrary/feed';
             //used to be /connections/files/basic/api/myuserlibrary/feed
             $_SERVER['REQUEST_METHOD'] = 'GET';
         } else {
             if (isset($_REQUEST["basicAuthLogout"])) {
                 // Logout request
                 $store->deleteBasicAuthCredentials($endpointName);
             }
         }
         if (isset($_REQUEST["OAuthLogout"])) {
             $store->deleteOAuthCredentials($endpointName);
             $timestamp = time();
             unset($_COOKIE['IBMSBTKOAuthLogin']);
             setcookie('IBMSBTKOAuthLogin', "", $timestamp - 604800);
             return;
         } else {
             return;
         }
         if (isset($_REQUEST["basicAuthLogout"])) {
             return;
         }
     }
     // Handle any file operations
     // If file operations exist, then control flow
     // will be interrupted and route() will be called
     // again
     if ($this->fileOperations()) {
         return;
     }
     $url = $_REQUEST["_redirectUrl"];
     $url = str_replace("/connections/", "", $url);
     if (isset($_REQUEST['basicAuthRequest']) && $_REQUEST['basicAuthRequest'] == 'true') {
         $store->storeBasicAuthUsername($_POST['username'], $endpointName);
         $store->storeBasicAuthPassword($_POST['password'], $endpointName);
         $result = array('status' => 200, 'result' => true);
         print_r(json_encode($result));
         return;
     }
     $method = $_SERVER['REQUEST_METHOD'];
     $options = $proxyHelper->getOptions();
     $response = null;
     $body = file_get_contents('php://input');
     $endpoint = null;
     if ($server == null) {
         $server = $settings->getURL($endpointName);
     }
     $method = $_SERVER['REQUEST_METHOD'];
     $forwardHeader = $proxyHelper->getHeader($method);
     if ($settings->getAuthenticationMethod($endpointName) == "basic") {
         $endpoint = new SBTBasicAuthEndpoint();
     } else {
         if ($settings->getAuthenticationMethod($endpointName) == "oauth2") {
             $endpoint = new SBTOAuth2Endpoint();
         } else {
             if ($settings->getAuthenticationMethod($endpointName) == "oauth1") {
                 $endpoint = new SBTOAuth1Endpoint();
             }
         }
     }
     $url = $proxyHelper->cleanURL($url, $server);
     // Make request
     $response = $endpoint->makeRequest($server, $url, $method, $options, $body, $forwardHeader, $endpointName);
     // Print response
     $proxyHelper->outputResponse($response, $url);
 }
示例#3
0
 /**
  * Returns the block contents.
  * 
  * @return stdClass		Block content.
  */
 public function get_content()
 {
     if ($this->content !== null) {
         return $this->content;
     }
     $this->content = new stdClass();
     if (!isset($this->config->plugin) || $this->config->plugin == '') {
         return $this->content;
     }
     if (isset($this->config->plugin)) {
         ob_start();
         // Load dependencies
         if (!defined('BASE_LOCATION')) {
             $autoload = __DIR__ . '/core/autoload.php';
             include $autoload;
         }
         $this->loadModel('SBTSettings');
         $this->loadModel('SBTCredentialStore');
         $settings = new SBTSettings();
         $store = SBTCredentialStore::getInstance();
         global $CFG;
         global $USER;
         $blockPath = $CFG->dirroot . '/blocks/ibmsbt/';
         // Our default assumption is that user supplies incorrect credentials; only if
         // the test request shows a 401 do we change this flag
         $incorrectCredentials = false;
         // If the user clicked "login", then trigger the OAuth dance
         if (isset($_COOKIE['IBMSBTKOAuthLogin']) && $_COOKIE['IBMSBTKOAuthLogin'] == 'yes' && $store->getOAuthAccessToken($this->config->endpoint) == null) {
             if ($settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2') {
                 $store->deleteOAuthCredentials($this->config->endpoint);
                 $this->_startOAuthDance($settings);
                 $this->content->text = ob_get_clean();
                 return;
             }
         }
         // Check if token expired. If yes, clear the credential store and load login display
         if (($settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->config->endpoint) != null) {
             $endpoint = null;
             if ($settings->getAuthenticationMethod($this->config->endpoint) == "oauth2") {
                 $endpoint = new SBTOAuth2Endpoint();
             } else {
                 if ($settings->getAuthenticationMethod($this->config->endpoint) == "oauth1") {
                     $endpoint = new SBTOAuth1Endpoint();
                 }
             }
             $service = '/files/basic/api/myuserlibrary/feed';
             $response = $endpoint->makeRequest($settings->getURL($this->config->endpoint), $service, 'GET', array(), null, null, $this->config->endpoint);
             if (is_string($response)) {
                 echo response;
                 $this->content->text = ob_get_clean();
                 return;
             } else {
                 if ($response->getStatusCode() == 401) {
                     // Delete old credentials.
                     $store->deleteOAuthCredentials($this->config->endpoint);
                 }
             }
         } else {
             $endpoint = new SBTBasicAuthEndpoint();
             $service = '/files/basic/api/myuserlibrary/feed';
             $response = $endpoint->makeRequest($settings->getURL($this->config->endpoint), $service, 'GET', array(), null, null, $this->config->endpoint);
             if ($response->getStatusCode() == 401) {
                 // Delete old credentials.
                 $store->deleteBasicAuthCredentials($this->config->endpoint);
                 if ($store->getBasicAuthPassword($this->config->endpoint) != null) {
                     $incorrectCredentials = true;
                 }
             }
         }
         echo '<div name="ibm_sbtk_widget">';
         if (($settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->config->endpoint) == null && (!isset($_COOKIE['IBMSBTKOAuthLogin']) || $_COOKIE['IBMSBTKOAuthLogin'] != 'yes')) {
             if (isloggedin() === false || !isset($USER->id) || $USER->id === null) {
                 if (!$incorrectCredentials) {
                     print get_string('please_login', 'block_ibmsbt');
                 } else {
                     print get_string('incorrect_credentials', 'block_ibmsbt');
                 }
             } else {
                 require $blockPath . '/core/views/oauth-login-display.php';
             }
             echo '</div>';
             $this->content->text = ob_get_clean();
             return;
         }
         $plugin = new SBTPlugin($this->config->endpoint);
         $plugin->createHeader();
         if ($settings->getAuthenticationMethod($this->config->endpoint) == 'basic' && $store->getBasicAuthUsername($this->config->endpoint) != null && $store->getBasicAuthPassword($this->config->endpoint) != null || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' && $store->getRequestToken($this->config->endpoint) != null || $settings->getAuthenticationMethod($this->config->endpoint) == 'basic' && $settings->getBasicAuthMethod($this->config->endpoint) == 'global' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2' && $store->getOAuthAccessToken($this->config->endpoint) != null) {
             if (isloggedin()) {
                 if (IBM_SBT_CRYPTO_ENABLED === false && IBM_SBT_DEMO_MODE === false) {
                     echo '<strong style="color: red;">WARNING: Your data is not encrypted. Please install php-mcrypt to secure your data.</strong><br/>';
                 }
                 require $this->config->plugin;
             }
             if ($settings->getAuthenticationMethod($this->config->endpoint) == 'basic') {
                 // 					require $blockPath . '/core/views/endpoint-logout.php'; // Uncomment to show logout button
                 // 					echo '<button onclick="ibm_sbt_endpoint_logout()">Logout from this Endpoint</button>'; // Uncomment to show logout button
             }
         }
         if ($settings->getAuthenticationMethod($this->config->endpoint) == 'basic' && ($settings->getBasicAuthMethod($this->config->endpoint) == 'prompt' || $settings->getBasicAuthMethod($this->config->endpoint) == 'profile') && $store->getBasicAuthUsername($this->config->endpoint) == null) {
             if (!isloggedin()) {
                 print get_string('please_login', 'block_ibmsbt');
                 echo '</div>';
                 $this->content->text = ob_get_clean();
                 return $this->content;
             } else {
                 require $blockPath . '/core/views/basic-auth-login-display.php';
             }
         } else {
             if ($settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2') {
                 // 			require_once BASE_PATH . '/views/oauth-logout-display.php'; TODO: Uncomment when OAuth logout has been fixed
             }
         }
         if (($settings->getAuthenticationMethod($this->config->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->config->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->config->endpoint) != null) {
             // require $blockPath . '/core/views/endpoint-logout.php'; Uncomment to show logout button
             // echo '<button onclick="ibm_sbt_endpoint_logout()">Logout from this Endpoint</button>'; Uncomment to show logout button
         }
         echo '</div>';
         if (!isloggedin()) {
             print get_string('please_login', 'block_ibmsbt');
         }
         $this->content->text = ob_get_clean();
     } else {
         $this->content->text = get_string('please_login', 'block_ibmsbt');
     }
     return $this->content;
 }
 /**
  * Outputs the content of the widget.
  *
  * @param array $args
  * @param array $instance
  */
 public function widget($args, $instance)
 {
     $settings = new SBTSettings();
     $store = SBTCredentialStore::getInstance();
     if (isset($instance['ibm-sbtk-endpoint'])) {
         $this->endpoint = $instance['ibm-sbtk-endpoint'];
     } else {
         $this->endpoint = "connections";
     }
     if (!$this->_isUserLoggedIn()) {
         // Add && $settings->requireSignOn($this->endpoint) to enable both anon and non-anon access
         echo '<div class="widget-area" style="width: 100%;"><aside class="widget widget_recent_entries"><h3 class="widget-title">' . $this->widget_name . '</h3>';
         echo '' . $GLOBALS[LANG]['must_login'] . '</aside></div>';
         return;
     }
     // If tokens exist, make sure that they are valid. Otherwise clear the store and force the
     // user to re-log
     if (($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->endpoint) != null) {
         $endpoint = null;
         if ($settings->getAuthenticationMethod($this->endpoint) == "oauth2") {
             $endpoint = new SBTOAuth2Endpoint();
         } else {
             if ($settings->getAuthenticationMethod($this->endpoint) == "oauth1") {
                 $endpoint = new SBTOAuth1Endpoint();
             }
         }
         $service = '/files/basic/api/myuserlibrary/feed';
         $response = $endpoint->makeRequest($settings->getURL($this->endpoint), $service, 'GET', array(), null, null, $this->endpoint);
         if ($response->getStatusCode() == 401) {
             $store->deleteOAuthCredentials($this->endpoint);
             setcookie('IBMSBTKOAuthLogin', "", $timestamp - 604800);
             require BASE_PATH . '/core/views/oauth-login-display.php';
         }
     } else {
         $endpoint = new SBTBasicAuthEndpoint();
         $service = '/files/basic/api/myuserlibrary/feed';
         $response = $endpoint->makeRequest($settings->getURL($this->endpoint), $service, 'GET', array(), null, null, $this->endpoint);
         if ($response->getStatusCode() == 401) {
             // Delete old credentials.
             $store->deleteBasicAuthCredentials($this->endpoint);
         }
     }
     echo '<div name="ibm_sbtk_widget" class="widget-area" style="width:100%"><aside class="widget widget_recent_entries">';
     echo '<h3 class="widget-title">' . $this->widget_name . '</h3>';
     if (($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') && $store->getOAuthAccessToken($this->endpoint) == null && (!isset($_COOKIE['IBMSBTKOAuthLogin']) || $_COOKIE['IBMSBTKOAuthLogin'] != 'yes') && !$this->_isUserLoggedIn()) {
         require BASE_PATH . '/core/views/oauth-login-display.php';
         echo '</aside></div>';
         return;
     }
     $plugin = new SBTPlugin($this->endpoint);
     if ($settings->getAuthenticationMethod($this->endpoint) == 'basic' && $store->getBasicAuthUsername($this->endpoint) != null && $store->getBasicAuthPassword($this->endpoint) != null || $settings->getAuthenticationMethod($this->endpoint) == 'oauth1' && $store->getRequestToken($this->endpoint) != null || $settings->getAuthenticationMethod($this->endpoint) == 'basic' && $settings->getBasicAuthMethod($this->endpoint) == 'global' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2' && $store->getOAuthAccessToken($this->endpoint) != null) {
         require $this->widget_location;
     }
     if ($settings->getAuthenticationMethod($this->endpoint) == 'basic' && $settings->getBasicAuthMethod($this->endpoint) == 'prompt' && $store->getBasicAuthPassword($this->endpoint) == null) {
         require_once BASE_PATH . '/views/basic-auth-login-display.php';
     } else {
         if ($settings->getAuthenticationMethod($this->endpoint) == 'oauth1' || $settings->getAuthenticationMethod($this->endpoint) == 'oauth2') {
             // 			require_once BASE_PATH . '/views/oauth-logout-display.php'; TODO: Uncomment when OAuth logout has been fixed
         }
     }
     echo '</aside></div>';
 }