From 6135bd451e868e251970fd5aa13818561dad289e Mon Sep 17 00:00:00 2001 From: dongsheng Date: Mon, 28 Jul 2008 04:08:22 +0000 Subject: [PATCH] "MDL-15822, deal with time-out problem of boxnet lib" --- lib/filelib.php | 8 ++--- repository/boxnet/boxlibphp5.php | 62 ++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index 1561e47069..cca046444c 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1203,7 +1203,7 @@ class curl { $this->options['CURLOPT_BINARYTRANSFER'] = 0; $this->options['CURLOPT_SSL_VERIFYPEER'] = 0; $this->options['CURLOPT_SSL_VERIFYHOST'] = 2; - $this->options['CURLOPT_TIMEOUT'] = 120; + $this->options['CURLOPT_CONNECTTIMEOUT'] = 30; } /** @@ -1409,7 +1409,7 @@ class curl { if ($this->cache && $ret = $this->cache->get($this->options)) { return $ret; } else { - $ret = curl_exec($curl); + $ret = curl_exec($curl); if ($this->cache) { $this->cache->set($this->options, $ret); } @@ -1429,10 +1429,10 @@ class curl { curl_close($curl); - if (!empty($ret)){ + if (empty($this->error)){ return $ret; } else { - return false; + throw new moodle_exception($this->error, 'curl'); } } diff --git a/repository/boxnet/boxlibphp5.php b/repository/boxnet/boxlibphp5.php index 71536af547..6d31c5f55d 100755 --- a/repository/boxnet/boxlibphp5.php +++ b/repository/boxnet/boxlibphp5.php @@ -24,26 +24,38 @@ class boxclient { private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload'; private $_error_code = ''; private $_error_msg = ''; + private $debug = false; - public function __construct($api_key, $auth_token = '') { + public function __construct($api_key, $auth_token = '', $debug = false) { $this->api_key = $api_key; $this->auth_token = $auth_token; + $this->debug = $debug; } // Setup for Functions function makeRequest($method, $params = array()) { $this->_clearErrors(); - $c = new curl(array('cache'=>true)); - if ($method == 'upload'){ - $request = $this->_box_api_upload_url.'/'. - $this->auth_token.'/'.$params['folder_id']; - $xml = $c->post($request, $params); - }else{ - $args = array(); - $xml = $c->get($this->_box_api_url, $params); + if($this->debug){ + $c = new curl(array('debug'=>true, 'cache'=>true)); + } else { + $c = new curl(array('debug'=>false, 'cache'=>true)); + } + try { + if ($method == 'upload'){ + $request = $this->_box_api_upload_url.'/'. + $this->auth_token.'/'.$params['folder_id']; + $xml = $c->post($request, $params); + }else{ + $args = array(); + $xml = $c->get($this->_box_api_url, $params); + } + $xml_parser = xml_parser_create(); + // set $data here + xml_parse_into_struct($xml_parser, $xml, $data); + xml_parser_free($xml_parser); + } catch (moodle_exception $e) { + $this->setError(0, 'connection time-out or invalid url'); + return false; } - $xml_parser = xml_parser_create(); - xml_parse_into_struct($xml_parser, $xml, $data); - xml_parser_free($xml_parser); return $data; } function getTicket($params = array()) { @@ -79,7 +91,11 @@ class boxclient { // 'password'=>'xxx')); // function getAuthToken($ticket, $username, $password) { - $c = new curl; + if($this->debug){ + $c = new curl(array('debug'=>true)); + } else { + $c = new curl(array('debug'=>false)); + } $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0)); $param = array( 'login_form1'=>'', @@ -88,7 +104,12 @@ class boxclient { 'dologin'=>1, '__login'=>1 ); - $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param); + try { + $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param); + } catch (moodle_exception $e) { + $this->setError(0, 'connection time-out or invalid url'); + return false; + } $header = $c->getResponse(); if(empty($header['location'])) { throw new repository_exception('invalidpassword', 'repository'); @@ -118,9 +139,7 @@ class boxclient { return false; } $tree_count=count($data); - global $tree_count; - $entry_count = 0; - for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) { + for ($i=0; $i<$tree_count; $i++) { $a = $data[$i]; switch ($a['tag']) { @@ -160,8 +179,6 @@ class boxclient { $ret_array = array(); $data = $this->makeRequest('action=create_folder', $params); - - if ($this->_checkForError($data)) { return false; } @@ -377,6 +394,9 @@ class boxclient { } } function _checkForError($data) { + if ($this->_error_msg != '') { + return true; + } if (@$data[0]['attributes']['STAT'] == 'fail') { $this->_error_code = $data[1]['attributes']['CODE']; $this->_error_msg = $data[1]['attributes']['MSG']; @@ -391,6 +411,10 @@ class boxclient { } return false; } + public function setError($code = 0, $msg){ + $this->_error_code = $code; + $this->_error_msg = $msg; + } function getErrorMsg() { return '

Error: (' . $this->_error_code . ') ' . $this->_error_msg . '

'; -- 2.39.5