Matheus Garcia
6 years ago
commit
7428dde30a
7 changed files with 869 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||
|
Web service template plugin for Moodle 2.X |
||||
|
------------------------------------------ |
||||
|
|
||||
|
It is a template designed as a starting point for developing third party web services. |
||||
|
It includes a simple Hello World example. |
||||
|
|
||||
|
To test: |
||||
|
1- install the plugin in moodle/local/ws_evl/ |
||||
|
2- Read, setup and run moodle/local/ws_evl/client/client.php |
@ -0,0 +1,42 @@ |
|||||
|
<?php |
||||
|
// This client for local_ws_evl is free software: you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation, either version 3 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
|
||||
|
/** |
||||
|
* XMLRPC client for Moodle 2 - local_ws_evl |
||||
|
* |
||||
|
* This script does not depend of any Moodle code, |
||||
|
* and it can be called from a browser. |
||||
|
* |
||||
|
* @authorr Jerome Mouneyrac |
||||
|
*/ |
||||
|
|
||||
|
/// MOODLE ADMINISTRATION SETUP STEPS |
||||
|
// 1- Install the plugin |
||||
|
// 2- Enable web service advance feature (Admin > Advanced features) |
||||
|
// 3- Enable XMLRPC protocol (Admin > Plugins > Web services > Manage protocols) |
||||
|
// 4- Create a token for a specific user and for the service 'My service' (Admin > Plugins > Web services > Manage tokens) |
||||
|
// 5- Run this script directly from your browser: you should see 'Hello, FIRSTNAME' |
||||
|
|
||||
|
/// SETUP - NEED TO BE CHANGED |
||||
|
$token = 'ba67e5abeaf65bcb8397b39ebf1e1856'; //'49d164aa3585e3ff13c9ab1b123abd67'; |
||||
|
$domainname = 'http://localhost/moodle'; |
||||
|
|
||||
|
/// FUNCTION NAME |
||||
|
$functionname = 'local_ws_evl_get_usage_statistics'; |
||||
|
|
||||
|
/// PARAMETERS |
||||
|
$welcomemsg = 'Hello, '; |
||||
|
|
||||
|
///// XML-RPC CALL |
||||
|
header('Content-Type: text/plain'); |
||||
|
$serverurl = $domainname . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token; |
||||
|
require_once('./curl.php'); |
||||
|
$curl = new curl; |
||||
|
$post = xmlrpc_encode_request($functionname, array()); |
||||
|
$resp = xmlrpc_decode($curl->post($serverurl, $post)); |
||||
|
print_r($resp); |
||||
|
//print_r ($post); |
@ -0,0 +1,622 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* cURL class |
||||
|
* |
||||
|
* This is a wrapper class for curl, it is quite easy to use: |
||||
|
* <code> |
||||
|
* $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'); |
||||
|
* </code> |
||||
|
* |
||||
|
* @author Dongsheng Cai <dongsheng@moodle.com> - https://github.com/dongsheng/cURL |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
||||
|
*/ |
||||
|
|
||||
|
class curl { |
||||
|
/** @var bool */ |
||||
|
public $cache = false; |
||||
|
public $proxy = false; |
||||
|
/** @var array */ |
||||
|
public $response = array(); |
||||
|
public $header = array(); |
||||
|
/** @var string */ |
||||
|
public $info; |
||||
|
public $error; |
||||
|
|
||||
|
/** @var array */ |
||||
|
private $options; |
||||
|
/** @var string */ |
||||
|
private $proxy_host = ''; |
||||
|
private $proxy_auth = ''; |
||||
|
private $proxy_type = ''; |
||||
|
/** @var bool */ |
||||
|
private $debug = false; |
||||
|
private $cookie = false; |
||||
|
|
||||
|
/** |
||||
|
* @param array $options |
||||
|
*/ |
||||
|
public function __construct($options = array()){ |
||||
|
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 = 'curl_cookie.txt'; |
||||
|
} else { |
||||
|
$this->cookie = $options['cookie']; |
||||
|
} |
||||
|
} |
||||
|
if (!empty($options['cache'])) { |
||||
|
if (class_exists('curl_cache')) { |
||||
|
$this->cache = new curl_cache(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
/** |
||||
|
* Resets the CURL options that have already been set |
||||
|
*/ |
||||
|
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_CONNECTTIMEOUT'] = 30; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reset Cookie |
||||
|
*/ |
||||
|
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 |
||||
|
* |
||||
|
* @param mixed $ch Apparently not used |
||||
|
* @param string $header |
||||
|
* @return int The strlen of the 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 |
||||
|
* |
||||
|
* @param object $curl A curl handle |
||||
|
* @param array $options |
||||
|
* @return object The curl handle |
||||
|
*/ |
||||
|
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 |
||||
|
* |
||||
|
* Calls {@link multi()} with specific download headers |
||||
|
* |
||||
|
* <code> |
||||
|
* $c = new curl; |
||||
|
* $c->download(array( |
||||
|
* array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')), |
||||
|
* array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb')) |
||||
|
* )); |
||||
|
* </code> |
||||
|
* |
||||
|
* @param array $requests An array of files to request |
||||
|
* @param array $options An array of options to set |
||||
|
* @return array An array of results |
||||
|
*/ |
||||
|
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. |
||||
|
* |
||||
|
* @param array $requests An array of files to request |
||||
|
* @param array $options An array of options to set |
||||
|
* @return array An array of results |
||||
|
*/ |
||||
|
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($options['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 |
||||
|
* |
||||
|
* @param string $url The URL to request |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
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($this->error)){ |
||||
|
return $ret; |
||||
|
} else { |
||||
|
return $this->error; |
||||
|
// exception is not ajax friendly |
||||
|
//throw new moodle_exception($this->error, 'curl'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* HTTP HEAD method |
||||
|
* |
||||
|
* @see request() |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function head($url, $options = array()){ |
||||
|
$options['CURLOPT_HTTPGET'] = 0; |
||||
|
$options['CURLOPT_HEADER'] = 1; |
||||
|
$options['CURLOPT_NOBODY'] = 1; |
||||
|
return $this->request($url, $options); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Recursive function formating an array in POST parameter |
||||
|
* @param array $arraydata - the array that we are going to format and add into &$data array |
||||
|
* @param string $currentdata - a row of the final postdata array at instant T |
||||
|
* when finish, it's assign to $data under this format: name[keyname][][]...[]='value' |
||||
|
* @param array $data - the final data array containing all POST parameters : 1 row = 1 parameter |
||||
|
*/ |
||||
|
function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) { |
||||
|
foreach ($arraydata as $k=>$v) { |
||||
|
$newcurrentdata = $currentdata; |
||||
|
if (is_object($v)) { |
||||
|
$v = (array) $v; |
||||
|
} |
||||
|
if (is_array($v)) { //the value is an array, call the function recursively |
||||
|
$newcurrentdata = $newcurrentdata.'['.urlencode($k).']'; |
||||
|
$this->format_array_postdata_for_curlcall($v, $newcurrentdata, $data); |
||||
|
} else { //add the POST parameter to the $data array |
||||
|
$data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Transform a PHP array into POST parameter |
||||
|
* (see the recursive function format_array_postdata_for_curlcall) |
||||
|
* @param array $postdata |
||||
|
* @return array containing all POST parameters (1 row = 1 POST parameter) |
||||
|
*/ |
||||
|
function format_postdata_for_curlcall($postdata) { |
||||
|
if (is_object($postdata)) { |
||||
|
$postdata = (array) $postdata; |
||||
|
} |
||||
|
$data = array(); |
||||
|
foreach ($postdata as $k=>$v) { |
||||
|
if (is_object($v)) { |
||||
|
$v = (array) $v; |
||||
|
} |
||||
|
if (is_array($v)) { |
||||
|
$currentdata = urlencode($k); |
||||
|
$this->format_array_postdata_for_curlcall($v, $currentdata, $data); |
||||
|
} else { |
||||
|
$data[] = urlencode($k).'='.urlencode($v); |
||||
|
} |
||||
|
} |
||||
|
$convertedpostdata = implode('&', $data); |
||||
|
return $convertedpostdata; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* HTTP POST method |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array|string $params |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function post($url, $params = '', $options = array()){ |
||||
|
$options['CURLOPT_POST'] = 1; |
||||
|
if (is_array($params)) { |
||||
|
$params = $this->format_postdata_for_curlcall($params); |
||||
|
} |
||||
|
$options['CURLOPT_POSTFIELDS'] = $params; |
||||
|
return $this->request($url, $options); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* HTTP GET method |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $params |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
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 |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $params |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
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 |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $params |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
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 |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function trace($url, $options = array()){ |
||||
|
$options['CURLOPT_CUSTOMREQUEST'] = 'TRACE'; |
||||
|
$ret = $this->request($url, $options); |
||||
|
return $ret; |
||||
|
} |
||||
|
/** |
||||
|
* HTTP OPTIONS method |
||||
|
* |
||||
|
* @param string $url |
||||
|
* @param array $options |
||||
|
* @return bool |
||||
|
*/ |
||||
|
public function options($url, $options = array()){ |
||||
|
$options['CURLOPT_CUSTOMREQUEST'] = 'OPTIONS'; |
||||
|
$ret = $this->request($url, $options); |
||||
|
return $ret; |
||||
|
} |
||||
|
public function get_info() { |
||||
|
return $this->info; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* This class is used by cURL class, use case: |
||||
|
* |
||||
|
* <code> |
||||
|
* |
||||
|
* $c = new curl(array('cache'=>true), 'module_cache'=>'repository'); |
||||
|
* $ret = $c->get('http://www.google.com'); |
||||
|
* </code> |
||||
|
* |
||||
|
* @package core |
||||
|
* @subpackage file |
||||
|
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
class curl_cache { |
||||
|
/** @var string */ |
||||
|
public $dir = ''; |
||||
|
/** |
||||
|
* |
||||
|
* @param string @module which module is using curl_cache |
||||
|
* |
||||
|
*/ |
||||
|
function __construct() { |
||||
|
$this->dir = '/tmp/'; |
||||
|
if (!file_exists($this->dir)) { |
||||
|
mkdir($this->dir, 0700, true); |
||||
|
} |
||||
|
$this->ttl = 1200; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get cached value |
||||
|
* |
||||
|
* @param mixed $param |
||||
|
* @return bool|string |
||||
|
*/ |
||||
|
public function get($param){ |
||||
|
$this->cleanup($this->ttl); |
||||
|
$filename = 'u_'.md5(serialize($param)); |
||||
|
if(file_exists($this->dir.$filename)) { |
||||
|
$lasttime = filemtime($this->dir.$filename); |
||||
|
if(time()-$lasttime > $this->ttl) |
||||
|
{ |
||||
|
return false; |
||||
|
} else { |
||||
|
$fp = fopen($this->dir.$filename, 'r'); |
||||
|
$size = filesize($this->dir.$filename); |
||||
|
$content = fread($fp, $size); |
||||
|
return unserialize($content); |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Set cache value |
||||
|
* |
||||
|
* @param mixed $param |
||||
|
* @param mixed $val |
||||
|
*/ |
||||
|
public function set($param, $val){ |
||||
|
$filename = 'u_'.md5(serialize($param)); |
||||
|
$fp = fopen($this->dir.$filename, 'w'); |
||||
|
fwrite($fp, serialize($val)); |
||||
|
fclose($fp); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Remove cache files |
||||
|
* |
||||
|
* @param int $expire The number os seconds before expiry |
||||
|
*/ |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
/** |
||||
|
* delete current user's cache file |
||||
|
* |
||||
|
*/ |
||||
|
public function refresh(){ |
||||
|
if($dir = opendir($this->dir)){ |
||||
|
while (false !== ($file = readdir($dir))) { |
||||
|
if(!is_dir($file) && $file != '.' && $file != '..') { |
||||
|
if(strpos($file, 'u_')!==false){ |
||||
|
@unlink($this->dir.$file); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,56 @@ |
|||||
|
<?php |
||||
|
|
||||
|
// Moodle is free software: you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation, either version 3 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// Moodle is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||||
|
|
||||
|
/** |
||||
|
* Web service local plugin template external functions and service definitions. |
||||
|
* |
||||
|
* @package localws_evl |
||||
|
* @copyright 2011 Jerome Mouneyrac |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
// We define the services to install as pre-build services. A pre-build service is not editable by administrator. |
||||
|
$services = array( |
||||
|
'My service' => array( |
||||
|
'functions' => array ('local_ws_evl_hello_world'), |
||||
|
'restrictedusers' => 0, |
||||
|
'enabled'=>1, |
||||
|
), |
||||
|
'Usage Estatistics' => array( |
||||
|
'functions' => array ('local_ws_evl_get_usage_statistics'), |
||||
|
'restrictedusers' => 0, |
||||
|
'enabled'=>1 |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
// We defined the web service functions to install. |
||||
|
$functions = array( |
||||
|
'local_ws_evl_hello_world' => array( |
||||
|
'classname' => 'local_ws_evl_external', |
||||
|
'methodname' => 'hello_world', |
||||
|
'classpath' => 'local/ws_evl/externallib.php', |
||||
|
'description' => 'Return Hello World FIRSTNAME. Can change the text (Hello World) sending a new text as parameter', |
||||
|
'type' => 'read', |
||||
|
), |
||||
|
'local_ws_evl_get_usage_statistics' => array( |
||||
|
'classname' => 'local_ws_evl_external', |
||||
|
'methodname' => 'get_usage_statistics', |
||||
|
'classpath' => 'local/ws_evl/externallib.php', |
||||
|
'description' => 'Retorna estatísticas de uso desta instalação', |
||||
|
'type' => 'read', |
||||
|
), |
||||
|
); |
||||
|
|
||||
|
|
@ -0,0 +1,90 @@ |
|||||
|
<?php |
||||
|
|
||||
|
// Moodle is free software: you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation, either version 3 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// Moodle is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||||
|
|
||||
|
/** |
||||
|
* External Web Service Template |
||||
|
* |
||||
|
* @package localws_evl |
||||
|
* @copyright 2011 Moodle Pty Ltd (http://moodle.com) |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
require_once($CFG->libdir . "/externallib.php"); |
||||
|
|
||||
|
class local_ws_evl_external extends external_api { |
||||
|
|
||||
|
/** |
||||
|
* Returns description of method parameters |
||||
|
* @return external_function_parameters |
||||
|
*/ |
||||
|
public static function hello_world_parameters() { |
||||
|
return new external_function_parameters( |
||||
|
array('welcomemessage' => new external_value(PARAM_TEXT, 'The welcome message. By default it is "Hello world,"', VALUE_DEFAULT, 'Hello world, ')) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Returns welcome message |
||||
|
* @return string welcome message |
||||
|
*/ |
||||
|
public static function hello_world($welcomemessage = 'Hello world, ') { |
||||
|
global $USER; |
||||
|
|
||||
|
//Parameter validation |
||||
|
//REQUIRED |
||||
|
$params = self::validate_parameters(self::hello_world_parameters(), |
||||
|
array('welcomemessage' => $welcomemessage)); |
||||
|
|
||||
|
//Context validation |
||||
|
//OPTIONAL but in most web service it should present |
||||
|
$context = get_context_instance(CONTEXT_USER, $USER->id); |
||||
|
self::validate_context($context); |
||||
|
|
||||
|
//Capability checking |
||||
|
//OPTIONAL but in most web service it should present |
||||
|
if (!has_capability('moodle/user:viewdetails', $context)) { |
||||
|
throw new moodle_exception('cannotviewprofile'); |
||||
|
} |
||||
|
|
||||
|
return $params['welcomemessage'] . $USER->firstname ;; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Returns description of method result value |
||||
|
* @return external_description |
||||
|
*/ |
||||
|
public static function hello_world_returns() { |
||||
|
return new external_value(PARAM_TEXT, 'The welcome message + user first name'); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static function get_usage_statistics($whatever='qq coisa') { |
||||
|
global $USER; |
||||
|
|
||||
|
return '{"cursos": "10", "alunos":"20", "certificados":"12"}'; |
||||
|
} |
||||
|
|
||||
|
public static function get_usage_statistics_parameters() { |
||||
|
return new external_function_parameters( |
||||
|
array('welcomemessage' => new external_value(PARAM_TEXT, 'The welcome message. By default it is "Hello world,"', VALUE_DEFAULT, 'Hello world, ')) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public static function get_usage_statistics_returns() { |
||||
|
return new external_value(PARAM_TEXT, 'The welcome message + user first name'); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
<?php |
||||
|
// This file is part of Moodle - http://moodle.org/ |
||||
|
// |
||||
|
// Moodle is free software: you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation, either version 3 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// Moodle is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||||
|
|
||||
|
/** |
||||
|
* Web service template plugin related strings |
||||
|
* @package localws_evl |
||||
|
* @copyright 2011 Moodle Pty Ltd (http://moodle.com) |
||||
|
* @author Jerome Mouneyrac |
||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
||||
|
*/ |
||||
|
|
||||
|
$string['pluginname'] = 'Web service template'; |
@ -0,0 +1,25 @@ |
|||||
|
<?php |
||||
|
// Moodle is free software: you can redistribute it and/or modify |
||||
|
// it under the terms of the GNU General Public License as published by |
||||
|
// the Free Software Foundation, either version 3 of the License, or |
||||
|
// (at your option) any later version. |
||||
|
// |
||||
|
// Moodle is distributed in the hope that it will be useful, |
||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
// GNU General Public License for more details. |
||||
|
// |
||||
|
// You should have received a copy of the GNU General Public License |
||||
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
||||
|
|
||||
|
$plugin->version = 2018092603; // The (date) version of this module + 2 extra digital for daily versions |
||||
|
// This version number is displayed into /admin/forms.php |
||||
|
// TODO: if ever this plugin get branched, the old branch number |
||||
|
// will not be updated to the current date but just incremented. We will |
||||
|
// need then a $plugin->release human friendly date. For the moment, we use |
||||
|
// display this version number with userdate (dev friendly) |
||||
|
$plugin->requires = 2010112400; // Requires this Moodle version - at least 2.0 |
||||
|
$plugin->component = 'local_ws_evl'; |
||||
|
$plugin->cron = 0; |
||||
|
$plugin->release = '1.0 (Build: 2011101202)'; |
||||
|
$plugin->maturity = MATURITY_STABLE; |
Loading…
Reference in new issue