Skip to content

Commit 396ded2

Browse files
committed
WebServices: download forum attachment
1 parent ff516d1 commit 396ded2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

main/forum/forumfunction.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6420,6 +6420,8 @@ function getAttachedFiles(
64206420
if ($result !== false && Database::num_rows($result) > 0) {
64216421
while ($row = Database::fetch_array($result, 'ASSOC')) {
64226422
// name contains an URL to download attachment file and its filename
6423+
$json['filename'] = $row['filename'];
6424+
$json['path'] = $row['path'];
64236425
$json['name'] = Display::url(
64246426
api_htmlentities($row['filename']),
64256427
api_get_path(WEB_CODE_PATH).'forum/download.php?file='.$row['path'].'&'.api_get_cidreq(),

main/inc/lib/webservices/Rest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Rest extends WebService
6060
const SAVE_FORUM_POST = 'save_forum_post';
6161
const SAVE_FORUM_THREAD = 'save_forum_thread';
6262
const SET_THREAD_NOTIFY = 'set_thread_notify';
63+
const DOWNLOAD_FORUM_ATTACHMENT= 'download_forum_attachment';
6364

6465
const GET_WORK_LIST = 'get_work_list';
6566
const GET_WORK_STUDENTS_WITHOUT_PUBLICATIONS = 'get_work_students_without_publications';
@@ -879,6 +880,13 @@ public function getCourseForumThread($forumId, $threadId)
879880
'author' => api_get_person_name($postInfo['firstname'], $postInfo['lastname']),
880881
'date' => api_convert_and_format_date($postInfo['post_date'], DATE_TIME_FORMAT_LONG_24H),
881882
'parentId' => $postInfo['post_parent_id'],
883+
'attachments' => getAttachedFiles(
884+
$forumId,
885+
$threadId,
886+
$postInfo['iid'],
887+
0,
888+
$this->course->getId()
889+
)
882890
];
883891
}
884892

@@ -2934,6 +2942,28 @@ public function viewMessage(int $messageId)
29342942
exit;
29352943
}
29362944

2945+
public function downloadForumPostAttachment(string $path)
2946+
{
2947+
2948+
$courseCode = $this->course->getCode();
2949+
$sessionId = $this->session ? $this->session->getId() : 0;
2950+
2951+
$url = api_get_path(WEB_CODE_PATH).'forum/download.php?'
2952+
.http_build_query(
2953+
[
2954+
'cidReq' => $courseCode,
2955+
'id_session' => $sessionId,
2956+
'gidReq' => 0,
2957+
'gradebook' => 0,
2958+
'origin' => self::SERVICE_NAME,
2959+
'file' => Security::remove_XSS($path),
2960+
]
2961+
);
2962+
2963+
header("Location: $url");
2964+
exit;
2965+
}
2966+
29372967
public static function isAllowedByRequest(bool $inpersonate = false): bool
29382968
{
29392969
$username = $_GET['username'] ?? null;

main/webservices/api/v2.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@
300300
]
301301
);
302302
break;
303+
case Rest::DOWNLOAD_FORUM_ATTACHMENT:
304+
if (empty($_GET['path'])) {
305+
throw new Exception(get_lang('ActionNotAllowed'));
306+
}
307+
308+
$restApi->downloadForumPostAttachment($_GET['path']);
309+
break;
303310

304311
case Rest::GET_WORK_LIST:
305312
if (!isset($_GET['work'])) {

0 commit comments

Comments
 (0)