MDL-13766

1. Move curl and repository_cache class to filelib.php
2. Name the file before downloading it
3. clean up code
This commit is contained in:
dongsheng 2008-07-18 07:40:12 +00:00
parent eec1d200b5
commit bb2c046d5c
9 changed files with 601 additions and 604 deletions

View File

@ -1090,4 +1090,485 @@ function file_modify_html_header($text) {
return $text;
}
?>
/**
* RESTful cURL class
*
* This is a wrapper class for curl, it is quite easy to use:
*
* $c = new curl;
* // enable cache
* $c = new curl(array('cache'=>true));
* // enable cookie
* $c = new curl(array('cookie'=>true));
* // enable proxy
* $c = new curl(array('proxy'=>true));
*
* // HTTP GET Method
* $html = $c->get('http://example.com');
* // HTTP POST Method
* $html = $c->post('http://example.com/', array('q'=>'words', 'name'=>'moodle'));
* // HTTP PUT Method
* $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
*
* @author Dongsheng Cai <dongsheng@cvs.moodle.org>
* @version 0.4 dev
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class curl {
public $cache = false;
public $proxy = false;
public $version = '0.4 dev';
public $response = array();
public $header = array();
public $info;
public $error;
private $options;
private $proxy_host = '';
private $proxy_auth = '';
private $proxy_type = '';
private $debug = false;
private $cookie = false;
public function __construct($options = array()){
global $CFG;
if (!function_exists('curl_init')) {
$this->error = 'cURL module must be enabled!';
trigger_error($this->error, E_USER_ERROR);
return false;
}
// the options of curl should be init here.
$this->resetopt();
if (!empty($options['debug'])) {
$this->debug = true;
}
if(!empty($options['cookie'])) {
if($options['cookie'] === true) {
$this->cookie = $CFG->dataroot.'/curl_cookie.txt';
} else {
$this->cookie = $options['cookie'];
}
}
if (!empty($options['cache'])) {
if (class_exists('curl_cache')) {
$this->cache = new curl_cache;
}
}
if (!empty($options['proxy'])) {
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
$this->proxy_host = $CFG->proxyhost;
} else {
$this->proxy_host = $CFG->proxyhost.':'.$CFG->proxyport;
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
$this->proxy_auth = $CFG->proxyuser.':'.$CFG->proxypassword;
$this->setopt(array(
'proxyauth'=> CURLAUTH_BASIC | CURLAUTH_NTLM,
'proxyuserpwd'=>$this->proxy_auth));
}
if (!empty($CFG->proxytype)) {
if ($CFG->proxytype == 'SOCKS5') {
$this->proxy_type = CURLPROXY_SOCKS5;
} else {
$this->proxy_type = CURLPROXY_HTTP;
$this->setopt(array('httpproxytunnel'=>true));
}
$this->setopt(array('proxytype'=>$this->proxy_type));
}
}
if (!empty($this->proxy_host)) {
$this->proxy = array('proxy'=>$this->proxy_host);
}
}
}
public function resetopt(){
$this->options = array();
$this->options['CURLOPT_USERAGENT'] = 'MoodleBot/1.0';
// True to include the header in the output
$this->options['CURLOPT_HEADER'] = 0;
// True to Exclude the body from the output
$this->options['CURLOPT_NOBODY'] = 0;
// TRUE to follow any "Location: " header that the server
// sends as part of the HTTP header (note this is recursive,
// PHP will follow as many "Location: " headers that it is sent,
// unless CURLOPT_MAXREDIRS is set).
$this->options['CURLOPT_FOLLOWLOCATION'] = 1;
$this->options['CURLOPT_MAXREDIRS'] = 10;
$this->options['CURLOPT_ENCODING'] = '';
// TRUE to return the transfer as a string of the return
// value of curl_exec() instead of outputting it out directly.
$this->options['CURLOPT_RETURNTRANSFER'] = 1;
$this->options['CURLOPT_BINARYTRANSFER'] = 0;
$this->options['CURLOPT_SSL_VERIFYPEER'] = 0;
$this->options['CURLOPT_SSL_VERIFYHOST'] = 2;
$this->options['CURLOPT_TIMEOUT'] = 120;
}
/**
* Reset Cookie
*
* @param array $options If array is null, this function will
* reset the options to default value.
*
*/
public function resetcookie() {
if (!empty($this->cookie)) {
if (is_file($this->cookie)) {
$fp = fopen($this->cookie, 'w');
if (!empty($fp)) {
fwrite($fp, '');
fclose($fp);
}
}
}
}
/**
* Set curl options
*
* @param array $options If array is null, this function will
* reset the options to default value.
*
*/
public function setopt($options = array()) {
if (is_array($options)) {
foreach($options as $name => $val){
if (stripos($name, 'CURLOPT_') === false) {
$name = strtoupper('CURLOPT_'.$name);
}
$this->options[$name] = $val;
}
}
}
/**
* Reset http method
*
*/
public function cleanopt(){
unset($this->options['CURLOPT_HTTPGET']);
unset($this->options['CURLOPT_POST']);
unset($this->options['CURLOPT_POSTFIELDS']);
unset($this->options['CURLOPT_PUT']);
unset($this->options['CURLOPT_INFILE']);
unset($this->options['CURLOPT_INFILESIZE']);
unset($this->options['CURLOPT_CUSTOMREQUEST']);
}
/**
* Set HTTP Request Header
*
* @param array $headers
*
*/
public function setHeader($header) {
if (is_array($header)){
foreach ($header as $v) {
$this->setHeader($v);
}
} else {
$this->header[] = $header;
}
}
/**
* Set HTTP Response Header
*
*/
public function getResponse(){
return $this->response;
}
/**
* private callback function
* Formatting HTTP Response Header
*
*/
private function formatHeader($ch, $header)
{
$this->count++;
if (strlen($header) > 2) {
list($key, $value) = explode(" ", rtrim($header, "\r\n"), 2);
$key = rtrim($key, ':');
if (!empty($this->response[$key])) {
if (is_array($this->response[$key])){
$this->response[$key][] = $value;
} else {
$tmp = $this->response[$key];
$this->response[$key] = array();
$this->response[$key][] = $tmp;
$this->response[$key][] = $value;
}
} else {
$this->response[$key] = $value;
}
}
return strlen($header);
}
/**
* Set options for individual curl instance
*/
private function apply_opt($curl, $options) {
// Clean up
$this->cleanopt();
// set cookie
if (!empty($this->cookie) || !empty($options['cookie'])) {
$this->setopt(array('cookiejar'=>$this->cookie,
'cookiefile'=>$this->cookie
));
}
// set proxy
if (!empty($this->proxy) || !empty($options['proxy'])) {
$this->setopt($this->proxy);
}
$this->setopt($options);
// reset before set options
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
// set headers
if (empty($this->header)){
$this->setHeader(array(
'User-Agent: MoodleBot/1.0',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive'
));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
if ($this->debug){
echo '<h1>Options</h1>';
var_dump($this->options);
echo '<h1>Header</h1>';
var_dump($this->header);
}
// set options
foreach($this->options as $name => $val) {
if (is_string($name)) {
$name = constant(strtoupper($name));
}
curl_setopt($curl, $name, $val);
}
return $curl;
}
/*
* Download multiple files in parallel
* $c = new curl;
* $c->download(array(
* array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')),
* array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb'))
* ));
*/
public function download($requests, $options = array()) {
$options['CURLOPT_BINARYTRANSFER'] = 1;
$options['RETURNTRANSFER'] = false;
return $this->multi($requests, $options);
}
/*
* Mulit HTTP Requests
* This function could run multi-requests in parallel.
*/
protected function multi($requests, $options = array()) {
$count = count($requests);
$handles = array();
$results = array();
$main = curl_multi_init();
for ($i = 0; $i < $count; $i++) {
$url = $requests[$i];
foreach($url as $n=>$v){
$options[$n] = $url[$n];
}
$handles[$i] = curl_init($url['url']);
$this->apply_opt($handles[$i], $options);
curl_multi_add_handle($main, $handles[$i]);
}
$running = 0;
do {
curl_multi_exec($main, $running);
} while($running > 0);
for ($i = 0; $i < $count; $i++) {
if (!empty($optins['CURLOPT_RETURNTRANSFER'])) {
$results[] = true;
} else {
$results[] = curl_multi_getcontent($handles[$i]);
}
curl_multi_remove_handle($main, $handles[$i]);
}
curl_multi_close($main);
return $results;
}
/**
* Single HTTP Request
*/
protected function request($url, $options = array()){
// create curl instance
$curl = curl_init($url);
$options['url'] = $url;
$this->apply_opt($curl, $options);
if ($this->cache && $ret = $this->cache->get($this->options)) {
return $ret;
} else {
$ret = curl_exec($curl);
if ($this->cache) {
$this->cache->set($this->options, $ret);
}
}
$this->info = curl_getinfo($curl);
$this->error = curl_error($curl);
if ($this->debug){
echo '<h1>Return Data</h1>';
var_dump($ret);
echo '<h1>Info</h1>';
var_dump($this->info);
echo '<h1>Error</h1>';
var_dump($this->error);
}
curl_close($curl);
if (!empty($ret)){
return $ret;
} else {
return false;
}
}
/**
* HTTP HEAD method
*/
public function head($url, $options = array()){
$options['CURLOPT_HTTPGET'] = 0;
$options['CURLOPT_HEADER'] = 1;
$options['CURLOPT_NOBODY'] = 1;
return $this->request($url, $options);
}
/**
* HTTP POST method
*/
public function post($url, $params = array(), $options = array()){
$options['CURLOPT_POST'] = 1;
$options['CURLOPT_POSTFIELDS'] = $params;
return $this->request($url, $options);
}
/**
* HTTP GET method
*/
public function get($url, $params = array(), $options = array()){
$options['CURLOPT_HTTPGET'] = 1;
if (!empty($params)){
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= http_build_query($params, '', '&');
}
return $this->request($url, $options);
}
/**
* HTTP PUT method
*/
public function put($url, $params = array(), $options = array()){
$file = $params['file'];
if (!is_file($file)){
return null;
}
$fp = fopen($file, 'r');
$size = filesize($file);
$options['CURLOPT_PUT'] = 1;
$options['CURLOPT_INFILESIZE'] = $size;
$options['CURLOPT_INFILE'] = $fp;
if (!isset($this->options['CURLOPT_USERPWD'])){
$this->setopt(array('CURLOPT_USERPWD'=>'anonymous: noreply@moodle.org'));
}
$ret = $this->request($url, $options);
fclose($fp);
return $ret;
}
/**
* HTTP DELETE method
*/
public function delete($url, $param = array(), $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'DELETE';
if (!isset($options['CURLOPT_USERPWD'])) {
$options['CURLOPT_USERPWD'] = 'anonymous: noreply@moodle.org';
}
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP TRACE method
*/
public function trace($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'TRACE';
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP OPTIONS method
*/
public function options($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'OPTIONS';
$ret = $this->request($url, $options);
return $ret;
}
}
/**
* This class is used by cURL class, use case:
*
* $CFG->repository_cache_expire = 120;
* $c = new curl(array('cache'=>true));
* $ret = $c->get('http://www.google.com');
*
*/
class curl_cache {
public $dir = '';
function __construct(){
global $CFG;
if (!file_exists($CFG->dataroot.'/repository/cache')) {
mkdir($CFG->dataroot.'/repository/cache/', 0777, true);
}
if(is_dir($CFG->dataroot.'/repository/cache')) {
$this->dir = $CFG->dataroot.'/repository/cache/';
}
}
public function get($param){
global $CFG;
$filename = md5(serialize($param));
if(file_exists($this->dir.$filename)) {
$lasttime = filemtime($this->dir.$filename);
if(time()-$lasttime > $CFG->repository_cache_expire) {
return false;
} else {
$fp = fopen($this->dir.$filename, 'r');
$size = filesize($this->dir.$filename);
$content = fread($fp, $size);
return unserialize($content);
}
}
return false;
}
public function set($param, $val){
$filename = md5(serialize($param));
$fp = fopen($this->dir.$filename, 'w');
fwrite($fp, serialize($val));
fclose($fp);
}
public function cleanup($expire){
if($dir = opendir($this->dir)){
while (false !== ($file = readdir($dir))) {
if(!is_dir($file) && $file != '.' && $file != '..') {
$lasttime = @filemtime($this->dir.$file);
if(time() - $lasttime > $expire){
@unlink($this->dir.$file);
}
}
}
}
}
}

View File

@ -1,3 +1,6 @@
<html>
<head>
<title> Ajax picker demo page </title>
<?php
/*******************************************************\
@ -9,10 +12,18 @@ require_once('../config.php');
require_once('lib.php');
$itempic = $CFG->pixpath.'/i/item.gif';
$meta = <<<EOD
<link rel="stylesheet" type="text/css" href="../lib/yui/reset-fonts-grids/reset-fonts-grids.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/reset/reset-min.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/resize/assets/skins/sam/resize.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/container/assets/skins/sam/container.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/layout/assets/skins/sam/layout.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/button/assets/skins/sam/button.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/menu/assets/skins/sam/menu.css" />
<style type="text/css">
body {
margin:0;
padding:0;
background: #FFF7C6;
}
#demo .yui-resize-handle-br {
height: 11px;
@ -24,7 +35,7 @@ body {
#list{line-height: 1.5em}
#list li{
background: url($itempic) no-repeat 0 2px;
padding-left: 24px
padding-left: 16px
}
#list a{
padding: 3px
@ -35,18 +46,11 @@ color:white;
}
.t{width:80px; float:left;text-align:center;}
.t div{width: 80px; height: 36px; overflow: hidden}
.repo-logout{font-size: 10px;}
.repo-opt{font-size: 10px;color:red}
img{margin:0;padding:0;border:0}
#paging{margin:10px 5px; clear:both}
#paging a{padding: 4px; border: 1px solid gray}
</style>
<link rel="stylesheet" type="text/css" href="../lib/yui/reset-fonts-grids/reset-fonts-grids.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/reset/reset-min.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/resize/assets/skins/sam/resize.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/container/assets/skins/sam/container.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/layout/assets/skins/sam/layout.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/button/assets/skins/sam/button.css" />
<link rel="stylesheet" type="text/css" href="../lib/yui/menu/assets/skins/sam/menu.css" />
<script type="text/javascript" src="../lib/yui/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="../lib/yui/event/event-min.js"></script>
<script type="text/javascript" src="../lib/yui/dom/dom-min.js"></script>
@ -62,14 +66,16 @@ img{margin:0;padding:0;border:0}
<script type="text/javascript" src="../lib/yui/button/button-min.js"></script>
<script type="text/javascript" src="../lib/yui/selector/selector-beta-min.js"></script>
EOD;
print_header('', '', '', '', $meta, false);
echo $meta;
?>
<div id='control' style="margin: 5em">
</head>
<body class=" yui-skin-sam">
<div id='control'>
<input type="button" id="con1" onclick='openpicker()' value="Open File Picker" /> <br/>
<textarea rows=12 cols=50 id="result">
</textarea>
</div>
<div class=" yui-skin-sam">
<div>
<div id="file-picker"></div>
</div>
@ -116,13 +122,6 @@ function openpicker(){
units: [
{position: 'top', height: 32, resize: false, body:'<div class="yui-buttongroup" id="viewbar"></div>', gutter: '2'},
{ position: 'left', width: 150, resize: true, body: '<ul id="list"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
{ position: 'bottom',
height: 30,
body: '<div id="toolbar">'+
'<input type="button" id="select" value="Select" />'+
'<input type="button" id="search" value="Search" />'+
'</div>',
gutter: '2'},
{ position: 'center', body: '<div id="panel"></div>', scroll: true, gutter: '0 2 0 0' }
]
});
@ -157,10 +156,11 @@ function openpicker(){
list.on('contentReady', function(e){
for(var i=0; i<repos.length; i++) {
var repo = repos[i];
var li = document.createElement('li');
li = document.createElement('li');
li.innerHTML = '<a href="###" id="repo-call-'+repo.id+'">'+
repo.repositoryname+'</a> ';
li.innerHTML += '<a href="###" class="repo-logout" id="repo-logout-'+repo.id+'">Logout</a>';
repo.repositoryname+'</a><br/>';
li.innerHTML += '<a href="###" class="repo-opt" onclick=\'dosearch('+repo.id+')\'>search</a>';
li.innerHTML += '<a href="###" class="repo-opt" id="repo-logout-'+repo.id+'">Logout </a>';
li.id = 'repo-'+repo.id;
this.appendChild(li);
var e = new YAHOO.util.Element('repo-call-'+repo.id);
@ -188,27 +188,6 @@ function openpicker(){
var btn_thumb = {label: 'Thumbnail', value: 't', onclick: {fn: viewthumb}};
viewbar.addButtons([btn_list, btn_thumb]);
var select = new YAHOO.util.Element('select');
select.on('click', function(e){
var nodes = YAHOO.util.Selector.query('input:checked');
var str = '';
var files = [];
for(k in nodes){
files.push(nodes[k].value);
}
loading();
var trans = YAHOO.util.Connect.asyncRequest('POST', 'ws.php?id='+repositoryid+'&action=download', loadfile, postdata({'files':files}));
})
var search = new YAHOO.util.Element('search');
search.on('click', function(e) {
if(repositoryid==0){
alert('Select a repository first.');
return;
}
var data = window.prompt("What are you searching for?");
if(data != null && data != '') {
dosearch(data);
}
})
};
function postdata(obj) {
@ -248,6 +227,24 @@ function loading(){
panel.get('element').innerHTML = '<img src="<?php echo $CFG->pixpath.'/i/loading.gif'?>" alt="loading..." />';
}
// name the file
function rename(oldname, url){
var panel = new YAHOO.util.Element('panel');
var html = '<div>';
html += '<label for="newname">Name:</label>';
html += '<input type="text" id="newname" value="'+oldname+'" /><br/>';
html += '<input type="hidden" id="fileurl" value="'+url+'" />';
html += '<input type="button" onclick="download()" value="Download" />';
html += '<a href="###" onclick="viewfiles()">Back</a>';
html += '</div>';
panel.get('element').innerHTML = html;
}
function download(){
var title = document.getElementById('newname').value;
var file = document.getElementById('fileurl').value;
loading();
var trans = YAHOO.util.Connect.asyncRequest('POST', 'ws.php?id='+repositoryid+'&action=download', loadfile, postdata({'file':file, 'title':title}));
}
// produce thumbnail view
function viewthumb(){
viewbar.check(1);
@ -261,7 +258,8 @@ function viewthumb(){
for(k in obj){
str += '<div class="t">';
str += '<img title="'+obj[k].title+'" src="'+obj[k].thumbnail+'" />';
str += '<div style="text-align:center"><input type="radio" name="selected-files" value="'+obj[k].source+'"/><br/>'
str += '<div style="text-align:center">';
str += ('<input type="radio" title="'+obj[k].title+'" name="selected-files" value="'+obj[k].source+'" onclick=\'rename("'+obj[k].title+'", "'+obj[k].source+'")\' />');
str += obj[k].title+'</div>';
str += '</div>';
}
@ -270,6 +268,13 @@ function viewthumb(){
return str;
}
function viewfiles(){
if(viewmode) {
viewthumb();
} else {
viewlist();
}
}
// produce list view
function viewlist(){
var str = '';
@ -281,7 +286,7 @@ function viewlist(){
var panel = new YAHOO.util.Element('panel');
str += makepage();
for(k in obj){
str += '<input type="radio" name="selected-files" value="'+obj[k].source+'" />';
str += ('<input type="radio" title="'+obj[k].title+'" name="selected-files" value="'+obj[k].source+'" onclick=\'rename("'+obj[k].title+'", "'+obj[k].source+'")\' />');
str += obj[k].title;
str += '<br/>';
}
@ -324,7 +329,9 @@ var loadfile = {
alert('Invalid JSON String\n'+o.responseText);
}
var panel = new YAHOO.util.Element('panel');
panel.get('element').innerHTML = '<h1>Download Successfully!</h1>';
var html = '<h1>Download Successfully!</h1>';
html += '<a href="###" onclick="viewfiles()">Back</a>';
panel.get('element').innerHTML = html;
}
}
@ -336,10 +343,15 @@ function cr(id, path, reset){
loading();
var trans = YAHOO.util.Connect.asyncRequest('GET', 'ws.php?id='+id+'&p='+path+'&reset='+reset, callback);
}
function dosearch(text){
function dosearch(id){
var data = window.prompt("What are you searching for?");
if(data == null && data == '') {
alert('nothing entered');
return;
}
viewbar.set('disabled', false);
loading();
var trans = YAHOO.util.Connect.asyncRequest('GET', 'ws.php?id='+repositoryid+'&s='+text, callback);
var trans = YAHOO.util.Connect.asyncRequest('GET', 'ws.php?id='+id+'&s='+data, callback);
}
function dologin(){
@ -348,5 +360,5 @@ function dologin(){
var trans = YAHOO.util.Connect.asyncRequest('POST', 'ws.php', callback);
}
</script>
<?php
print_footer('empty');
</body>
</html>

View File

@ -17,8 +17,6 @@
*
*/
require_once($CFG->dirroot.'/repository/'.'curl.class.php');
class boxclient {
public $auth_token = '';
@ -80,13 +78,13 @@ class boxclient {
// 'username'=>'dongsheng@moodle.com',
// 'password'=>'xxx'));
//
function getAuthToken($ticket, $options = array()) {
function getAuthToken($ticket, $username, $password) {
$c = new curl;
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
$param = array(
'login_form1'=>'',
'login'=>$options['username'],
'password'=>$options['password'],
'login'=>$username,
'password'=>$password,
'dologin'=>1,
'__login'=>1
);

View File

@ -7,14 +7,11 @@
* @version 0.1 dev
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->dirroot.'/repository/'.'lib.php');
require_once($CFG->dirroot.'/repository/'.'curl.class.php');
require_once($CFG->dirroot.'/repository/boxnet/'.'boxlibphp5.php');
class repository_boxnet extends repository{
var $box;
var $ticket;
private $box;
public function __construct($repositoryid, $context = SITEID, $options = array()){
global $SESSION, $action;
@ -32,31 +29,21 @@ class repository_boxnet extends repository{
&& !empty($options['password'])
&& !empty($options['ticket']) )
{
$c = new curl;
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
$param = array(
'login_form1'=>'',
'login'=>$options['username'],
'password'=>$options['password'],
'dologin'=>1,
'__login'=>1
);
$ret = $c->post('http://www.box.net/api/1.0/auth/'.$options['ticket'], $param);
$header = $c->getResponse();
$location = $header['location'];
preg_match('#auth_token=(.*)$#i', $location, $matches);
$auth_token = $matches[1];
$SESSION->box_token = $auth_token;
$this->box = new boxclient($options['api_key']);
$SESSION->box_token = $this->box->getAuthToken($options['ticket'],
$options['username'], $options['password']);
}
// already logged
if(!empty($SESSION->box_token)) {
if(empty($this->box)) {
$this->box = new boxclient($options['api_key'], $SESSION->box_token);
}
$options['auth_token'] = $SESSION->box_token;
if(empty($action)) {
$action = 'list';
}
} else {
$this->box = new boxclient($options['api_key'], '');
$this->box = new boxclient($options['api_key']);
if(!empty($action)) {
$action = '';
}
@ -109,19 +96,18 @@ class repository_boxnet extends repository{
// print list
}
} else {
// get a ticket from box.net
$ticket_return = $this->box->getTicket();
$this->ticket = $ticket_return['ticket'];
$t = $this->box->getTicket();
if(empty($this->options['auth_token'])) {
$str = '';
$str .= '<form id="moodle-repo-login">';
$str .= '<input type="hidden" name="ticket" value="'.$this->ticket.'" />';
$str .= '<input type="hidden" name="ticket" value="'.
$t['ticket'].'" />';
$str .= '<input type="hidden" name="id" value="'.$this->repositoryid.'" />';
$str .= '<label for="box_username">Username: <label>';
$str .= '<label for="box_username">Username: <label><br/>';
$str .= '<input type="text" id="box_username" name="username" />';
$str .= '<br/>';
$str .= '<label for="box_password">Password: <label>';
$str .= '<input type="password" id="box_password" name="password" />';
$str .= '<label for="box_password">Password: <label><br/>';
$str .= '<input type="password" id="box_password" name="password" /><br/>';
$str .= '<input type="button" onclick="dologin()" value="Go" />';
$str .= '</form>';
if($this->options['ajax']){
@ -136,7 +122,6 @@ class repository_boxnet extends repository{
}
public function print_search(){
echo '<input type="text" disabled="true" name="Search" value="search terms..." size="40" class="right"/>';
return false;
}
}

View File

@ -1,428 +0,0 @@
<?php // $Id$
/**
* RESTful cURL class
*
* This is a wrapper class for curl, it is quite easy to use:
*
* $c = new curl;
* // enable cache
* $c = new curl(array('cache'=>true));
* // enable cookie
* $c = new curl(array('cookie'=>true));
* // enable proxy
* $c = new curl(array('proxy'=>true));
*
* // HTTP GET Method
* $html = $c->get('http://example.com');
* // HTTP POST Method
* $html = $c->post('http://example.com/', array('q'=>'words', 'name'=>'moodle'));
* // HTTP PUT Method
* $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
*
* @author Dongsheng Cai <dongsheng@cvs.moodle.org>
* @version 0.4 dev
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class curl {
public $cache = false;
public $proxy = false;
public $version = '0.4 dev';
public $response = array();
public $header = array();
public $info;
public $error;
private $options;
private $proxy_host = '';
private $proxy_auth = '';
private $proxy_type = '';
private $debug = false;
private $cookie = false;
public function __construct($options = array()){
global $CFG;
if (!function_exists('curl_init')) {
$this->error = 'cURL module must be enabled!';
trigger_error($this->error, E_USER_ERROR);
return false;
}
// the options of curl should be init here.
$this->resetopt();
if (!empty($options['debug'])) {
$this->debug = true;
}
if(!empty($options['cookie'])) {
if($options['cookie'] === true) {
$this->cookie = $CFG->dataroot.'/curl_cookie.txt';
} else {
$this->cookie = $options['cookie'];
}
}
if (!empty($options['cache'])) {
if (class_exists('repository_cache')) {
$this->cache = new repository_cache;
}
}
if (!empty($options['proxy'])) {
if (!empty($CFG->proxyhost)) {
if (empty($CFG->proxyport)) {
$this->proxy_host = $CFG->proxyhost;
} else {
$this->proxy_host = $CFG->proxyhost.':'.$CFG->proxyport;
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
$this->proxy_auth = $CFG->proxyuser.':'.$CFG->proxypassword;
$this->setopt(array(
'proxyauth'=> CURLAUTH_BASIC | CURLAUTH_NTLM,
'proxyuserpwd'=>$this->proxy_auth));
}
if (!empty($CFG->proxytype)) {
if ($CFG->proxytype == 'SOCKS5') {
$this->proxy_type = CURLPROXY_SOCKS5;
} else {
$this->proxy_type = CURLPROXY_HTTP;
$this->setopt(array('httpproxytunnel'=>true));
}
$this->setopt(array('proxytype'=>$this->proxy_type));
}
}
if (!empty($this->proxy_host)) {
$this->proxy = array('proxy'=>$this->proxy_host);
}
}
}
public function resetopt(){
$this->options = array();
$this->options['CURLOPT_USERAGENT'] = 'MoodleBot/1.0';
// True to include the header in the output
$this->options['CURLOPT_HEADER'] = 0;
// True to Exclude the body from the output
$this->options['CURLOPT_NOBODY'] = 0;
// TRUE to follow any "Location: " header that the server
// sends as part of the HTTP header (note this is recursive,
// PHP will follow as many "Location: " headers that it is sent,
// unless CURLOPT_MAXREDIRS is set).
$this->options['CURLOPT_FOLLOWLOCATION'] = 1;
$this->options['CURLOPT_MAXREDIRS'] = 10;
$this->options['CURLOPT_ENCODING'] = '';
// TRUE to return the transfer as a string of the return
// value of curl_exec() instead of outputting it out directly.
$this->options['CURLOPT_RETURNTRANSFER'] = 1;
$this->options['CURLOPT_BINARYTRANSFER'] = 0;
$this->options['CURLOPT_SSL_VERIFYPEER'] = 0;
$this->options['CURLOPT_SSL_VERIFYHOST'] = 2;
$this->options['CURLOPT_TIMEOUT'] = 120;
}
/**
* Reset Cookie
*
* @param array $options If array is null, this function will
* reset the options to default value.
*
*/
public function resetcookie() {
if (!empty($this->cookie)) {
if (is_file($this->cookie)) {
$fp = fopen($this->cookie, 'w');
if (!empty($fp)) {
fwrite($fp, '');
fclose($fp);
}
}
}
}
/**
* Set curl options
*
* @param array $options If array is null, this function will
* reset the options to default value.
*
*/
public function setopt($options = array()) {
if (is_array($options)) {
foreach($options as $name => $val){
if (stripos($name, 'CURLOPT_') === false) {
$name = strtoupper('CURLOPT_'.$name);
}
$this->options[$name] = $val;
}
}
}
/**
* Reset http method
*
*/
public function cleanopt(){
unset($this->options['CURLOPT_HTTPGET']);
unset($this->options['CURLOPT_POST']);
unset($this->options['CURLOPT_POSTFIELDS']);
unset($this->options['CURLOPT_PUT']);
unset($this->options['CURLOPT_INFILE']);
unset($this->options['CURLOPT_INFILESIZE']);
unset($this->options['CURLOPT_CUSTOMREQUEST']);
}
/**
* Set HTTP Request Header
*
* @param array $headers
*
*/
public function setHeader($header) {
if (is_array($header)){
foreach ($header as $v) {
$this->setHeader($v);
}
} else {
$this->header[] = $header;
}
}
/**
* Set HTTP Response Header
*
*/
public function getResponse(){
return $this->response;
}
/**
* private callback function
* Formatting HTTP Response Header
*
*/
private function formatHeader($ch, $header)
{
$this->count++;
if (strlen($header) > 2) {
list($key, $value) = explode(" ", rtrim($header, "\r\n"), 2);
$key = rtrim($key, ':');
if (!empty($this->response[$key])) {
if (is_array($this->response[$key])){
$this->response[$key][] = $value;
} else {
$tmp = $this->response[$key];
$this->response[$key] = array();
$this->response[$key][] = $tmp;
$this->response[$key][] = $value;
}
} else {
$this->response[$key] = $value;
}
}
return strlen($header);
}
/**
* Set options for individual curl instance
*/
private function apply_opt($curl, $options) {
// Clean up
$this->cleanopt();
// set cookie
if (!empty($this->cookie) || !empty($options['cookie'])) {
$this->setopt(array('cookiejar'=>$this->cookie,
'cookiefile'=>$this->cookie
));
}
// set proxy
if (!empty($this->proxy) || !empty($options['proxy'])) {
$this->setopt($this->proxy);
}
$this->setopt($options);
// reset before set options
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
// set headers
if (empty($this->header)){
$this->setHeader(array(
'User-Agent: MoodleBot/1.0',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive'
));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
if ($this->debug){
echo '<h1>Options</h1>';
var_dump($this->options);
echo '<h1>Header</h1>';
var_dump($this->header);
}
// set options
foreach($this->options as $name => $val) {
if (is_string($name)) {
$name = constant(strtoupper($name));
}
curl_setopt($curl, $name, $val);
}
return $curl;
}
/*
* Download multiple files in parallel
* $c = new curl;
* $c->download(array(
* array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')),
* array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb'))
* ));
*/
public function download($requests, $options = array()) {
$options['CURLOPT_BINARYTRANSFER'] = 1;
$options['RETURNTRANSFER'] = false;
return $this->multi($requests, $options);
}
/*
* Mulit HTTP Requests
* This function could run multi-requests in parallel.
*/
protected function multi($requests, $options = array()) {
$count = count($requests);
$handles = array();
$results = array();
$main = curl_multi_init();
for ($i = 0; $i < $count; $i++) {
$url = $requests[$i];
foreach($url as $n=>$v){
$options[$n] = $url[$n];
}
$handles[$i] = curl_init($url['url']);
$this->apply_opt($handles[$i], $options);
curl_multi_add_handle($main, $handles[$i]);
}
$running = 0;
do {
curl_multi_exec($main, $running);
} while($running > 0);
for ($i = 0; $i < $count; $i++) {
if (!empty($optins['CURLOPT_RETURNTRANSFER'])) {
$results[] = true;
} else {
$results[] = curl_multi_getcontent($handles[$i]);
}
curl_multi_remove_handle($main, $handles[$i]);
}
curl_multi_close($main);
return $results;
}
/**
* Single HTTP Request
*/
protected function request($url, $options = array()){
// create curl instance
$curl = curl_init($url);
$options['url'] = $url;
$this->apply_opt($curl, $options);
if ($this->cache && $ret = $this->cache->get($this->options)) {
return $ret;
} else {
$ret = curl_exec($curl);
if ($this->cache) {
$this->cache->set($this->options, $ret);
}
}
$this->info = curl_getinfo($curl);
$this->error = curl_error($curl);
if ($this->debug){
echo '<h1>Return Data</h1>';
var_dump($ret);
echo '<h1>Info</h1>';
var_dump($this->info);
echo '<h1>Error</h1>';
var_dump($this->error);
}
curl_close($curl);
if (!empty($ret)){
return $ret;
} else {
return false;
}
}
/**
* HTTP HEAD method
*/
public function head($url, $options = array()){
$options['CURLOPT_HTTPGET'] = 0;
$options['CURLOPT_HEADER'] = 1;
$options['CURLOPT_NOBODY'] = 1;
return $this->request($url, $options);
}
/**
* HTTP POST method
*/
public function post($url, $params = array(), $options = array()){
$options['CURLOPT_POST'] = 1;
$options['CURLOPT_POSTFIELDS'] = $params;
return $this->request($url, $options);
}
/**
* HTTP GET method
*/
public function get($url, $params = array(), $options = array()){
$options['CURLOPT_HTTPGET'] = 1;
if (!empty($params)){
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= http_build_query($params, '', '&');
}
return $this->request($url, $options);
}
/**
* HTTP PUT method
*/
public function put($url, $params = array(), $options = array()){
$file = $params['file'];
if (!is_file($file)){
return null;
}
$fp = fopen($file, 'r');
$size = filesize($file);
$options['CURLOPT_PUT'] = 1;
$options['CURLOPT_INFILESIZE'] = $size;
$options['CURLOPT_INFILE'] = $fp;
if (!isset($this->options['CURLOPT_USERPWD'])){
$this->setopt(array('CURLOPT_USERPWD'=>'anonymous: noreply@moodle.org'));
}
$ret = $this->request($url, $options);
fclose($fp);
return $ret;
}
/**
* HTTP DELETE method
*/
public function delete($url, $param = array(), $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'DELETE';
if (!isset($options['CURLOPT_USERPWD'])) {
$options['CURLOPT_USERPWD'] = 'anonymous: noreply@moodle.org';
}
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP TRACE method
*/
public function trace($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'TRACE';
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP OPTIONS method
*/
public function options($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'OPTIONS';
$ret = $this->request($url, $options);
return $ret;
}
}

View File

@ -62,7 +62,6 @@ class phpFlickr {
//Find the PHP version and store it for future reference
$this->php_version = explode("-", phpversion());
$this->php_version = explode(".", $this->php_version[0]);
require_once($CFG->dirroot.'/repository/curl.class.php');
$this->curl = new curl(array('cache'=>true));
}

View File

@ -8,7 +8,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->dirroot.'/repository/lib.php');
require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
class repository_flickr extends repository{
@ -24,6 +23,7 @@ class repository_flickr extends repository{
$reset = optional_param('reset', 0, PARAM_INT);
if(!empty($reset)) {
// logout from flickr
unset($SESSION->flickrmail);
set_user_preference('flickrmail', '');
}
@ -33,6 +33,7 @@ class repository_flickr extends repository{
$action = 'list';
}
} else {
// get flickr account
$options['flickrmail'] = optional_param('flickrmail', '', PARAM_RAW);
if(!empty($options['flickrmail'])) {
$people = $this->flickr->people_findByEmail($options['flickrmail']);
@ -61,11 +62,11 @@ class repository_flickr extends repository{
if(empty($SESSION->flickrmail)) {
$str =<<<EOD
<form id="moodle-repo-login">
<label for="account">Flickr Account (Email)</label>
<label for="account">Account (Email)</label><br/>
<input type='text' name='flickrmail' id='account' />
<input type='hidden' name='id' value='$this->repositoryid' /><br/>
<input type='checkbox' name='remember' value='true' /> Remember <br/>
<a href="###" onclick="dologin()">Go</a>
<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
<p><input type='button' onclick="dologin()" value="Go" /></p>
</form>
EOD;
if($ajax){
@ -145,7 +146,7 @@ EOD;
echo '<input type="text" name="Search" value="search terms..." size="40" class="right"/>';
return true;
}
public function get_file($photo_id){
public function get_file($photo_id, $file = ''){
global $CFG;
$result = $this->flickr->photos_getSizes($photo_id);
$url = '';
@ -162,19 +163,19 @@ EOD;
if(is_dir($CFG->dataroot.'/repository/download')) {
$dir = $CFG->dataroot.'/repository/download/';
}
if(file_exists($CFG->dirroot.'/repository/curl.class.php')) {
if(empty($file)) {
$file = $photo_id.'_'.time().'.jpg';
}
if(file_exists($dir.$file)) {
$file = uniqid('m').$file;
}
$fp = fopen($dir.$file, 'w');
require_once($CFG->dirroot.'/repository/curl.class.php');
$c = new curl;
$c->download(array(
array('url'=>$url, 'file'=>$fp)
));
return $dir.$file;
} else {
return '!!!fail!!!';
}
return $result;
}
}
?>

View File

@ -51,6 +51,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once('../config.php');
require_once('../lib/filelib.php');
abstract class repository {
protected $options;
@ -104,7 +105,7 @@ abstract class repository {
* @param string $url the url of file
* @param string $file save location
*/
public function get_file($url) {
public function get_file($url, $file = '') {
global $CFG;
if (!file_exists($CFG->dataroot.'/repository/download')) {
mkdir($CFG->dataroot.'/repository/download/', 0777, true);
@ -113,7 +114,12 @@ abstract class repository {
$dir = $CFG->dataroot.'/repository/download/';
}
if(file_exists($CFG->dirroot.'/repository/curl.class.php')) {
if(empty($file)) {
$file = uniqid('repo').'_'.time().'.tmp';
}
if(file_exists($dir.$file)){
$file = uniqid('m').$file;
}
$fp = fopen($dir.$file, 'w');
require_once($CFG->dirroot.'/repository/curl.class.php');
$c = new curl;
@ -247,60 +253,6 @@ abstract class repository {
abstract class repository_listing {
}
/**
* This class is used by cURL class, use case:
*
* $CFG->repository_cache_expire = 120;
* $c = new curl(array('cache'=>true));
* $ret = $c->get('http://www.google.com');
*
*/
class repository_cache {
public $dir = '';
function __construct(){
global $CFG;
if (!file_exists($CFG->dataroot.'/repository/cache')) {
mkdir($CFG->dataroot.'/repository/cache/', 0777, true);
}
if(is_dir($CFG->dataroot.'/repository/cache')) {
$this->dir = $CFG->dataroot.'/repository/cache/';
}
}
public function get($param){
global $CFG;
$filename = md5(serialize($param));
if(file_exists($this->dir.$filename)) {
$lasttime = filemtime($this->dir.$filename);
if(time()-$lasttime > $CFG->repository_cache_expire) {
return false;
} else {
$fp = fopen($this->dir.$filename, 'r');
$size = filesize($this->dir.$filename);
$content = fread($fp, $size);
return unserialize($content);
}
}
return false;
}
public function set($param, $val){
$filename = md5(serialize($param));
$fp = fopen($this->dir.$filename, 'w');
fwrite($fp, serialize($val));
fclose($fp);
}
public function cleanup($expire){
if($dir = opendir($this->dir)){
while (false !== ($file = readdir($dir))) {
if(!is_dir($file) && $file != '.' && $file != '..') {
$lasttime = @filemtime($this->dir.$file);
if(time() - $lasttime > $expire){
@unlink($this->dir.$file);
}
}
}
}
}
}
function repository_set_option($id, $position, $config = array()){
global $DB;

View File

@ -7,12 +7,12 @@ $CFG->repository_cache_expire = 12000;
$id = optional_param('id', PARAM_INT);
// action of client
$action = optional_param('action', '', PARAM_RAW);
// path
$p = optional_param('p', '', PARAM_RAW);
// Search text
$search = optional_param('s', '', PARAM_RAW);
// files to be downloaded
$files = optional_param('files', '', PARAM_RAW);
$file = optional_param('file', '', PARAM_RAW);
$title = optional_param('title', '', PARAM_RAW);
$p = optional_param('p', '', PARAM_RAW);
if(!$repository = $DB->get_record('repository', array('id'=>$id))) {
echo json_encode('wrong');
@ -39,13 +39,10 @@ if($action == 'list') {
}
} elseif($action == 'download') {
$ret = array();
foreach($files as $url) {
$ret[] = $repo->get_file($url);
$ret = $repo->get_file($file, $title);
// TODO
// Need to communicate with FILE API
// Copy the tmp file to final location
}
echo json_encode($ret);
} else {
echo json_encode($repo->print_login());