12 |
efrain |
1 |
<?php
|
|
|
2 |
header('Access-Control-Allow-Origin: *');
|
|
|
3 |
header('Access-Control-Allow-Methods: GET');
|
|
|
4 |
//POST, GET, DELETE, PUT, PATCH, OPTIONS
|
|
|
5 |
header('Access-Control-Allow-Headers: token, Content-Type');
|
|
|
6 |
header('Access-Control-Max-Age: 1728000');
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
header('Content-Type: application/json');
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
require_once(__DIR__ . '/config.php');
|
|
|
14 |
require_once(__DIR__ . '/../config.php');
|
|
|
15 |
global $DB, $PAGE, $CFG;
|
|
|
16 |
|
|
|
17 |
require_once($CFG->libdir.'/moodlelib.php');
|
|
|
18 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
19 |
require_once($CFG->libdir.'/authlib.php');
|
|
|
20 |
require_once( $CFG->libdir . '/gdlib.php' );
|
|
|
21 |
|
|
|
22 |
require_once($CFG->dirroot.'/user/lib.php');
|
|
|
23 |
require_once __DIR__ . '/rsa.php';
|
|
|
24 |
require_once __DIR__ . '/lib.php';
|
|
|
25 |
|
37 |
efrain |
26 |
|
12 |
efrain |
27 |
$username = trim(isset($_SERVER['HTTP_USERNAME']) ? filter_var($_SERVER['HTTP_USERNAME'], FILTER_SANITIZE_STRING) : '');
|
|
|
28 |
$password = trim(isset($_SERVER['HTTP_PASSWORD']) ? filter_var($_SERVER['HTTP_PASSWORD'], FILTER_SANITIZE_STRING) : '');
|
|
|
29 |
$timestamp = trim(isset($_SERVER['HTTP_TIMESTAMP']) ? filter_var($_SERVER['HTTP_TIMESTAMP'], FILTER_SANITIZE_STRING) : '');
|
|
|
30 |
$rand = intval(isset($_SERVER['HTTP_RAND']) ? filter_var($_SERVER['HTTP_RAND'], FILTER_SANITIZE_NUMBER_INT) : 0,10);
|
|
|
31 |
|
57 |
efrain |
32 |
/*
|
12 |
efrain |
33 |
|
|
|
34 |
if(empty($username) || empty($password) || empty($timestamp) || empty($rand) || !is_integer($rand)) {
|
|
|
35 |
echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY1']) ;
|
|
|
36 |
exit;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
if($username != LLWS_USERNAME) {
|
|
|
40 |
echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY2']) ;
|
|
|
41 |
exit;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $timestamp);
|
|
|
45 |
if(!$dt) {
|
|
|
46 |
echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY3']) ;
|
|
|
47 |
exit;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s', gmdate('Y-m-d\TH:i:s'));
|
|
|
51 |
$dtMax = $dt->add(\DateInterval::createFromDateString('5 minutes'));
|
|
|
52 |
$dtMin = $dt->sub(\DateInterval::createFromDateString('5 minutes'));
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
$t0 = $dt->getTimestamp();
|
|
|
56 |
$t1 = $dtMin->getTimestamp();
|
|
|
57 |
$t2 = $dtMax->getTimestamp();
|
|
|
58 |
|
|
|
59 |
if($t0 < $t1 || $t0 > $t2) {
|
|
|
60 |
echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY4']) ;
|
|
|
61 |
exit;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if(!password_verify( $username.'-'. LLWS_PASSWORD. '-' . $rand. '-' . $timestamp, $password)) {
|
|
|
65 |
echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY5']) ;
|
|
|
66 |
exit;
|
|
|
67 |
}
|
57 |
efrain |
68 |
*/
|
12 |
efrain |
69 |
|
|
|
70 |
|
37 |
efrain |
71 |
|
12 |
efrain |
72 |
$response = [
|
|
|
73 |
'success' => true,
|
|
|
74 |
'data' => []
|
|
|
75 |
];
|
|
|
76 |
|
|
|
77 |
$enrolmethod = 'manual';
|
|
|
78 |
$url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
|
|
|
79 |
|
|
|
80 |
//$courses_with_autoenrol = $DB->get_records('enrol', ['enrol' => 'manual', 'status' => 0]);
|
|
|
81 |
//print_r($courses_with_autoenrol); exit;
|
|
|
82 |
|
21 |
efrain |
83 |
|
|
|
84 |
header('Content-type: text/plain');
|
12 |
efrain |
85 |
$enrol = enrol_get_plugin($enrolmethod);
|
|
|
86 |
if ($enrol) {
|
|
|
87 |
|
21 |
efrain |
88 |
|
16 |
efrain |
89 |
|
12 |
efrain |
90 |
$courses = get_courses();
|
24 |
efrain |
91 |
|
35 |
efrain |
92 |
$courses = $DB->get_records('course'); //, ['visible' => 1], 'shortname', 'courseid, enddate, fullname, idnumber, shortname, startdate, summary');
|
24 |
efrain |
93 |
|
12 |
efrain |
94 |
foreach($courses as $course)
|
|
|
95 |
{
|
33 |
efrain |
96 |
if(!$course->visible) {
|
|
|
97 |
continue;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
|
36 |
efrain |
101 |
|
12 |
efrain |
102 |
$instances = enrol_get_instances($course->id, true);
|
|
|
103 |
|
|
|
104 |
$manualinstance = null;
|
|
|
105 |
foreach ($instances as $instance)
|
|
|
106 |
{
|
|
|
107 |
if ($instance->enrol == $enrolmethod) {
|
|
|
108 |
$manualinstance = $instance;
|
|
|
109 |
break;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
if (!$manualinstance) {
|
|
|
115 |
continue;
|
|
|
116 |
}
|
|
|
117 |
|
37 |
efrain |
118 |
|
12 |
efrain |
119 |
if ($course instanceof stdClass) {
|
|
|
120 |
$coreCourseList = new core_course_list_element($course);
|
|
|
121 |
} else {
|
|
|
122 |
$coreCourseList = [];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$image = $url_noimage;
|
|
|
126 |
foreach ($coreCourseList->get_course_overviewfiles() as $file)
|
|
|
127 |
{
|
|
|
128 |
$isimage = $file->is_valid_image();
|
|
|
129 |
$image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
|
|
|
130 |
if (!$isimage) {
|
|
|
131 |
$image = $url_noimage;
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
array_push($response['data'], [
|
|
|
138 |
'courseid' => $course->id,
|
|
|
139 |
'enddate' => $course->enddate,
|
|
|
140 |
'fullname' => $course->fullname,
|
|
|
141 |
'shortname' => $course->shortname,
|
|
|
142 |
'startdate' => $course->startdate,
|
|
|
143 |
]);
|
|
|
144 |
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
echo json_encode($response) ;
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|