-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle-upload.sh
82 lines (69 loc) · 2.55 KB
/
moodle-upload.sh
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
MOODLE_USER=$1
MOODLE_PW=$2
SUBMISSION_ID=$3
ZIP_FILENAME=$4
# Verify config
set -e
[[ $MOODLE_USER ]]
[[ $MOODLE_PW ]]
[[ $SUBMISSION_ID ]]
[[ $ZIP_FILENAME ]]
#------------------------------
# Get CSRF token
wget --save-cookies cookies.txt \
--keep-session-cookies \
-Otmp.html \
https://ucilnica.fri.uni-lj.si/login/index.php
LOGIN_TOKEN=$(grep -Po '"logintoken" value="\K\w+' tmp.html | head -n1)
# Login
wget --load-cookies cookies.txt \
--save-cookies cookies.txt \
--keep-session-cookies \
--post-data "anchor=&logintoken=$LOGIN_TOKEN&username=$MOODLE_USER&password=$MOODLE_PW" \
-Otmp.html \
https://ucilnica.fri.uni-lj.si/login/index.php
SESSKEY=$(grep -Po '"sesskey" value="\K\w+' tmp.html | head -n1)
USERID=$(grep -Po 'data-userid="\K\w+' tmp.html | head -n1)
echo $LOGIN_TOKEN $SESSKEY $USERID
# Delete old submission
wget --load-cookies cookies.txt \
--post-data "id=$SUBMISSION_ID&action=removesubmission&userid=$USERID&sesskey=$SESSKEY" \
-O/dev/null \
https://ucilnica.fri.uni-lj.si/mod/assign/view.php
# Get upload paramters
wget --load-cookies cookies.txt \
-Otmp.html \
"https://ucilnica.fri.uni-lj.si/mod/assign/view.php?id=$SUBMISSION_ID&action=editsubmission"
AUTHOR=$(grep -Po '"author":"\K[\w\s]+' tmp.html | head -n1)
ITEM_ID=$(grep -Po '"itemid":\K\w+' tmp.html | head -n1)
CLIENT_ID=$(grep -Po '"client_id":"\K\w+' tmp.html | head -n1)
CTX_ID=$(grep -Po '"context" value="\K\w+' tmp.html | head -n1)
LAST_MODIFIED=$(grep -Po '"lastmodified" type="hidden" value="\K\w+' tmp.html | head -n1)
echo $AUTHOR $ITEM_ID $CLIENT_ID $CTX_ID $LAST_MODIFIED
# Upload file
curl -b cookies.txt \
-o /dev/null \
-F "repo_upload_file=@$ZIP_FILENAME;type=application/x-zip-compressed" \
-F "title=" \
-F "author=$AUTHOR" \
-F "license=public" \
-F "itemid=$ITEM_ID" \
-F "repo_id=4" \
-F "p=" \
-F "page=" \
-F "env=filemanager" \
-F "sesskey=$SESSKEY" \
-F "client_id=$CLIENT_ID" \
-F "itemid=$ITEM_ID" \
-F "maxbytes=52428800" \
-F "areamaxbytes=-1" \
-F "ctx_id=$CTX_ID" \
-F "savepath=/" \
https://ucilnica.fri.uni-lj.si/repository/repository_ajax.php?action=upload
# Submit draft
curl -b cookies.txt \
-o /dev/null \
-d "lastmodified=$LAST_MODIFIED&id=$SUBMISSION_ID&userid=$USERID&action=savesubmission&sesskey=$SESSKEY&_qf__mod_assign_submission_form=1&submissionstatement=1&files_filemanager=$ITEM_ID&submitbutton=Shrani+spremembe" \
https://ucilnica.fri.uni-lj.si/mod/assign/view.php
rm cookies.txt
rm tmp.html