-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocallib.php
More file actions
114 lines (106 loc) · 4.05 KB
/
locallib.php
File metadata and controls
114 lines (106 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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/>.
/**
* remoteprocessed question definition class.
*
* @package qtype
* @subpackage remoteprocessed
* @copyright 2013 Leif Johnson (leif.t.johnson@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Helper functions for remote processed questions.
*
* @copyright 2013 Leif Johnson (leif.t.johnson@gmail.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/************************************************************************
* functions for xmlrpc client
*/
/* function do_rpc_call
* $url -- url of rpc server
* $request -- request to send to server
* handles sending the body of a request to a server and getting the response
* returns the xml from the server
*/
function do_rpc_call($url, $request) {
$header[] = "Content-type: text.xml";
$header[] = "Content-length: ".strlen($request);
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_TIMEOUT, 10);
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POSTFIELDS, $request);
$data = curl_exec($crl);
$response = new stdClass;
$response->success = true;
$response->data = NULL;
$response->warning = NULL;
if (curl_errno($crl)) {
$response->success = false;
$response->warning = "RECEIVED curl_errno: ". curl_errno($crl) .": ". curl_error($crl);
} else {
$response->data = substr($data, strpos($data, "<methodResponse>"));
}
curl_close($crl);
return $response;
}
/*
* function xmlrpc_request
* $request_args -- XmlRpc request args. contains:
* * server -- xml rpc server
* * method -- method to request form the server
* * args -- php args to send in the request
* This function handles all the details of an xmlrpc request to a remote
* server, returning a php class containing three variables:
* * 'success' True or False for success of request.
* * 'warning' a string containing a warning if the request was not
* successful.
* * 'data' a php version of the response if the request was successful.
*/
function xmlrpc_request($request_args) {
$request = xmlrpc_encode_request($request_args->method, $request_args->args);
$response = do_rpc_call($request_args->server, $request);
$response->data = xmlrpc_decode($response->data);
$response->success = $response->success &&
(is_array($response->data) &&
!array_key_exists('faultCode', $response->data));
if (!$response->success) {
// Not quite done looking for errors yet, we need to see if
// a faultcode was sent back
if (is_array($response->data) &&
array_key_exists('faultCode', $response->data)) {
$response->warning = "Error processing question: <br>".
"faultCode[". $response->data['faultCode'] . "] <br>".
"faultString[". $response->data['faultString'] . "] <br>";
} else {
$response->warning = "Unknown error processing question.<br>" .
$response->warning . "<br>";
}
$response->data = NULL;
} else {
$response->data = $response->data;
}
return $response;
}
/* Functions for working with base64 images. */
function base64_png_img_tag($base64_png) {
$tag = "<img style='display:block;' id='base64image' " .
"src='data:image/png;base64, " . $base64_png . "' />";
return $tag;
}