Skip to content

Commit

Permalink
"MDL-15822, deal with time-out problem of boxnet lib"
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed Jul 28, 2008
1 parent 2edde5c commit 6135bd4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
8 changes: 4 additions & 4 deletions lib/filelib.php
Expand Up @@ -1203,7 +1203,7 @@ public function resetopt(){
$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;
}

/**
Expand Down Expand Up @@ -1409,7 +1409,7 @@ protected function request($url, $options = array()){
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);
}
Expand All @@ -1429,10 +1429,10 @@ protected function request($url, $options = array()){

curl_close($curl);

if (!empty($ret)){
if (empty($this->error)){
return $ret;
} else {
return false;
throw new moodle_exception($this->error, 'curl');
}
}

Expand Down
62 changes: 43 additions & 19 deletions repository/boxnet/boxlibphp5.php
Expand Up @@ -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()) {
Expand Down Expand Up @@ -79,7 +91,11 @@ function getTicket($params = array()) {
// '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'=>'',
Expand All @@ -88,7 +104,12 @@ function getAuthToken($ticket, $username, $password) {
'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');
Expand Down Expand Up @@ -118,9 +139,7 @@ function getAccountTree($params = array()) {
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'])
{
Expand Down Expand Up @@ -160,8 +179,6 @@ function CreateFolder($new_folder_name, $params = array()) {

$ret_array = array();
$data = $this->makeRequest('action=create_folder', $params);


if ($this->_checkForError($data)) {
return false;
}
Expand Down Expand Up @@ -377,6 +394,9 @@ function Logout($params = array()) {
}
}
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'];
Expand All @@ -391,6 +411,10 @@ public function isError() {
}
return false;
}
public function setError($code = 0, $msg){
$this->_error_code = $code;
$this->_error_msg = $msg;
}

function getErrorMsg() {
return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
Expand Down

0 comments on commit 6135bd4

Please sign in to comment.