| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @package Dotclear |
|---|
| 4 | * @subpackage Backend |
|---|
| 5 | * |
|---|
| 6 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 7 | * @copyright GPL-2.0-only |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | require dirname(__FILE__) . '/../inc/admin/prepend.php'; |
|---|
| 11 | |
|---|
| 12 | dcPage::check('usage,contentadmin'); |
|---|
| 13 | |
|---|
| 14 | $post_id = !empty($_REQUEST['post_id']) ? (integer) $_REQUEST['post_id'] : null; |
|---|
| 15 | $media_id = !empty($_REQUEST['media_id']) ? (integer) $_REQUEST['media_id'] : null; |
|---|
| 16 | $link_type = !empty($_REQUEST['link_type']) ? $_REQUEST['link_type'] : null; |
|---|
| 17 | |
|---|
| 18 | if (!$post_id) { |
|---|
| 19 | exit; |
|---|
| 20 | } |
|---|
| 21 | $rs = $core->blog->getPosts(['post_id' => $post_id, 'post_type' => '']); |
|---|
| 22 | if ($rs->isEmpty()) { |
|---|
| 23 | exit; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | try { |
|---|
| 27 | if ($post_id && $media_id && !empty($_REQUEST['attach'])) { |
|---|
| 28 | $core->media = new dcMedia($core); |
|---|
| 29 | $core->media->addPostMedia($post_id, $media_id, $link_type); |
|---|
| 30 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
|---|
| 31 | header('Content-type: application/json'); |
|---|
| 32 | echo json_encode(['url' => $core->getPostAdminURL($rs->post_type, $post_id, false)]); |
|---|
| 33 | exit(); |
|---|
| 34 | } else { |
|---|
| 35 | http::redirect($core->getPostAdminURL($rs->post_type, $post_id, false)); |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | $core->media = new dcMedia($core); |
|---|
| 40 | $f = $core->media->getPostMedia($post_id, $media_id, $link_type); |
|---|
| 41 | if (empty($f)) { |
|---|
| 42 | $post_id = $media_id = null; |
|---|
| 43 | throw new Exception(__('This attachment does not exist')); |
|---|
| 44 | } |
|---|
| 45 | $f = $f[0]; |
|---|
| 46 | } catch (Exception $e) { |
|---|
| 47 | $core->error->add($e->getMessage()); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | # Remove a media from en |
|---|
| 51 | if (($post_id && $media_id) || $core->error->flag()) { |
|---|
| 52 | if (!empty($_POST['remove'])) { |
|---|
| 53 | $core->media->removePostMedia($post_id, $media_id, $link_type); |
|---|
| 54 | |
|---|
| 55 | dcPage::addSuccessNotice(__('Attachment has been successfully removed.')); |
|---|
| 56 | http::redirect($core->getPostAdminURL($rs->post_type, $post_id, false)); |
|---|
| 57 | } elseif (isset($_POST['post_id'])) { |
|---|
| 58 | http::redirect($core->getPostAdminURL($rs->post_type, $post_id, false)); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if (!empty($_GET['remove'])) { |
|---|
| 62 | dcPage::open(__('Remove attachment')); |
|---|
| 63 | |
|---|
| 64 | echo '<h2>' . __('Attachment') . ' › <span class="page-title">' . __('confirm removal') . '</span></h2>'; |
|---|
| 65 | |
|---|
| 66 | echo |
|---|
| 67 | '<form action="' . $core->adminurl->get("admin.post.media") . '" method="post">' . |
|---|
| 68 | '<p>' . __('Are you sure you want to remove this attachment?') . '</p>' . |
|---|
| 69 | '<p><input type="submit" class="reset" value="' . __('Cancel') . '" /> ' . |
|---|
| 70 | ' <input type="submit" class="delete" name="remove" value="' . __('Yes') . '" />' . |
|---|
| 71 | form::hidden('post_id', $post_id) . |
|---|
| 72 | form::hidden('media_id', $media_id) . |
|---|
| 73 | $core->formNonce() . '</p>' . |
|---|
| 74 | '</form>'; |
|---|
| 75 | |
|---|
| 76 | dcPage::close(); |
|---|
| 77 | exit; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|