-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.php
121 lines (92 loc) · 3.5 KB
/
test.php
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
115
116
117
118
119
120
121
<?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/>.
/**
* Test file for catquiz
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH
* @author Georg Maißer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\dataformat\base;
use local_catquiz\data\catquiz_base;
use local_catquiz\event\catscale_updated;
use local_catquiz\subscription;
require_once('../../config.php');
global $USER;
$context = \context_system::instance();
$PAGE->set_context($context);
require_login();
$PAGE->set_url(new moodle_url('/local/catquiz/test.php', []));
$title = "Test cases";
$PAGE->set_title($title);
$PAGE->set_heading($title);
echo $OUTPUT->header();
$sftpserver = 'dedi458.your-server.de/';
$stfpport = '22';
$sftppath = '/';
$sftpusername = 'wunder_1';
$sftppassword = 'Gcb2BWeBtLpWVZt4';
$username = $sftpusername;
$password = $sftppassword;
$server = $sftpserver;
$remotedir = $sftppath;
$localdir = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "sftp://$username:$password@$server$remotedir");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if ($result === false) {
echo 'cURL error: ' . curl_error($ch);
} else {
// List of remote files and directories.
$remotelist = explode("\n", trim($result));
foreach ($remotelist as $line) {
$parts = preg_split('/\s+/', trim($line));
$file = end($parts);
if (!empty($file) && $file !== '.' && $file !== '..') {
$remotepath = "$remotedir$file";
$localpath = $localdir . $file;
// Todo: Check filename and get all information.
$filehandle = curl_init();
// Set cURL options for file download.
curl_setopt($filehandle, CURLOPT_URL, "sftp://$username:$password@$server$remotepath");
curl_setopt($filehandle, CURLOPT_RETURNTRANSFER, 1);
// Download the file.
$filedata = curl_exec($filehandle);
if ($filedata !== false) {
// TODO: Store Data in Moodle.
$fs = get_file_storage();
$draftitemid = file_get_submitted_draft_itemid('file'); // Assuming 'file' is the form field name.
$file = $fs->create_file_from_string(
[
'contextid' => $context->id, // Replace with the appropriate context if necessary.
'component' => 'datalynx',
'filearea' => 'draft',
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => $file,
],
$filedata
);
echo "Downloaded successfully." . PHP_EOL;
} else {
echo "Failed to download ." . PHP_EOL;
}
curl_close($filehandle);
}
}
}
echo $OUTPUT->footer();