| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Attachment; |
| 6 |
use FluentBoards\App\App; |
| 7 |
use FluentBoards\App\Models\CommentImage; |
| 8 |
use FluentBoards\App\Models\Meta; |
| 9 |
use FluentBoards\App\Models\Task; |
| 10 |
use FluentBoards\App\Services\Constant; |
| 11 |
use FluentBoards\App\Services\Libs\FileSystem; |
| 12 |
|
| 13 |
class ExternalPages |
| 14 |
{ |
| 15 |
public function view_uploaded_comment_image() |
| 16 |
{ |
| 17 |
$attachmentHash = sanitize_text_field($_REQUEST['fbs_comment_image']); |
| 18 |
$boardId = sanitize_text_field($_REQUEST['fbs_bid']); |
| 19 |
|
| 20 |
if (empty($attachmentHash)) { |
| 21 |
die('Invalid Attachment Hash'); |
| 22 |
} |
| 23 |
|
| 24 |
$attachment = $this->getUploadedImageByHash($attachmentHash); |
| 25 |
|
| 26 |
if (!$attachment) { |
| 27 |
die('Invalid Attachment Hash'); |
| 28 |
} |
| 29 |
|
| 30 |
$fileName = $attachment->file_path; |
| 31 |
$boardId = $boardId; |
| 32 |
$filePath = $fileName; |
| 33 |
if(!file_exists($fileName)){ |
| 34 |
$filePath = FileSystem::setSubDir('board_' . $boardId)->getDir() . DIRECTORY_SEPARATOR . $fileName; |
| 35 |
} |
| 36 |
|
| 37 |
if (!file_exists($filePath)) { |
| 38 |
die('File could not be found.'); |
| 39 |
} |
| 40 |
|
| 41 |
$this->serveLocalAttachment($attachment, $filePath); |
| 42 |
} |
| 43 |
|
| 44 |
public function view_comment_image() |
| 45 |
{ |
| 46 |
$attachmentHash = sanitize_text_field($_REQUEST['fbs_comment_image']); |
| 47 |
|
| 48 |
if (empty($attachmentHash)) { |
| 49 |
die('Invalid Attachment Hash'); |
| 50 |
} |
| 51 |
|
| 52 |
$attachment = $this->getUploadedImageByHash($attachmentHash); |
| 53 |
|
| 54 |
if (in_array($attachment->object_type, [Constant::COMMENT_IMAGE])) { |
| 55 |
$attachment->load('comment'); |
| 56 |
} elseif (in_array($attachment->object_type, [Constant::TASK_DESCRIPTION])) { |
| 57 |
$attachment['task'] = Task::find($attachment->object_id); |
| 58 |
} |
| 59 |
|
| 60 |
if (!$attachment) { |
| 61 |
die('Invalid Attachment Hash'); |
| 62 |
} |
| 63 |
|
| 64 |
// check signature hash |
| 65 |
if (!$this->validateAttachmentSignature($attachment)) { |
| 66 |
$dieMessage = __('Sorry, Your secure sign is invalid, Please reload the previous page and get new signed url', 'fluent-support'); |
| 67 |
die($dieMessage); |
| 68 |
} |
| 69 |
|
| 70 |
//If external file |
| 71 |
if ('local' !== $attachment->driver) { |
| 72 |
if(!empty($attachment->file_path)){ |
| 73 |
$this->redirectToExternalAttachment($attachment->full_url); |
| 74 |
}else{ |
| 75 |
die('File could not be found'); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
//Handle Local file |
| 80 |
if (in_array($attachment->object_type, [Constant::COMMENT_IMAGE])) { |
| 81 |
$fileName = $attachment->file_path; |
| 82 |
$boardId = $attachment->comment->board_id; |
| 83 |
} elseif (in_array($attachment->object_type, [Constant::TASK_DESCRIPTION])) { |
| 84 |
$fileName = $attachment->file_path; |
| 85 |
$boardId = $attachment->task->board_id; |
| 86 |
} |
| 87 |
|
| 88 |
$filePath = $fileName; |
| 89 |
if(!file_exists($fileName)){ |
| 90 |
$filePath = FileSystem::setSubDir('board_' . $boardId)->getDir() . DIRECTORY_SEPARATOR . $fileName; |
| 91 |
} |
| 92 |
|
| 93 |
if (!file_exists($filePath)) { |
| 94 |
die('File could not be found.'); |
| 95 |
} |
| 96 |
|
| 97 |
$this->serveLocalAttachment($attachment, $filePath); |
| 98 |
} |
| 99 |
|
| 100 |
private function getUploadedImageByHash($attachmentHash) |
| 101 |
{ |
| 102 |
return CommentImage::where('file_hash', $attachmentHash)->first(); |
| 103 |
} |
| 104 |
|
| 105 |
private function serveLocalAttachment($attachment, $filePath) |
| 106 |
{ |
| 107 |
ob_get_clean(); |
| 108 |
header("Content-Type: {$attachment->attachment_type}"); |
| 109 |
header("Content-Disposition: inline; filename=\"{$attachment->title}\"");; |
| 110 |
echo readfile($filePath); |
| 111 |
die(); |
| 112 |
} |
| 113 |
|
| 114 |
private function validateAttachmentSignature($attachment) |
| 115 |
{ |
| 116 |
$sign = md5($attachment->id . date('YmdH')); |
| 117 |
return $sign === $_REQUEST['secure_sign']; |
| 118 |
} |
| 119 |
|
| 120 |
public function redirectToPage() |
| 121 |
{ |
| 122 |
$taskId = $_GET['taskId']; |
| 123 |
$task = Task::findOrFail($taskId); |
| 124 |
if ($this->isFrontendEnabled() == 'no') { |
| 125 |
$urlBase = apply_filters('fluent_boards/app_url', admin_url('admin.php?page=fluent-boards#/')); |
| 126 |
$page_url = $urlBase . 'boards/' . $task->board_id . '/tasks/' . $task->id . '-' .substr($task->title, 0, 10); |
| 127 |
wp_redirect($page_url); |
| 128 |
exit; |
| 129 |
} else { |
| 130 |
$urlBase = apply_filters('fluent_boards/app_url'); |
| 131 |
$page_url = $urlBase . 'boards/' . $task->board_id . '/tasks/' . $task->id . '-' .substr($task->title, 0, 10); |
| 132 |
wp_redirect($page_url); |
| 133 |
exit; |
| 134 |
} |
| 135 |
|
| 136 |
die(); |
| 137 |
} |
| 138 |
|
| 139 |
private function isFrontendEnabled() |
| 140 |
{ |
| 141 |
$storedSettings = get_option('fluent_boards_modules', []); |
| 142 |
if ($storedSettings && is_array($storedSettings)) { |
| 143 |
$settings = maybe_serialize($storedSettings); |
| 144 |
} |
| 145 |
|
| 146 |
if (isset($settings['frontend']['enabled'])) { |
| 147 |
return $settings['frontend']['enabled']; |
| 148 |
} |
| 149 |
|
| 150 |
return 'no'; |
| 151 |
} |
| 152 |
} |