|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AJAX: handles an attachment with the given id. |
|
5
|
|
|
* |
|
6
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
|
7
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
|
8
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
|
9
|
|
|
* |
|
10
|
|
|
* @package phpMyFAQ |
|
11
|
|
|
* @author Anatoliy Belsky <[email protected]> |
|
12
|
|
|
* @copyright 2010-2019 phpMyFAQ Team |
|
13
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
|
14
|
|
|
* @link https://www.phpmyfaq.de |
|
15
|
|
|
* @since 2010-12-20 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use phpMyFAQ\Attachment\Factory; |
|
19
|
|
|
use phpMyFAQ\Attachment\Exception; |
|
|
|
|
|
|
20
|
|
|
use phpMyFAQ\Helper\HttpHelper; |
|
21
|
|
|
use phpMyFAQ\Filter; |
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
if (!defined('IS_VALID_PHPMYFAQ')) { |
|
24
|
|
|
$protocol = 'http'; |
|
25
|
|
|
if (isset($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) === 'ON') { |
|
26
|
|
|
$protocol = 'https'; |
|
27
|
|
|
} |
|
28
|
|
|
header('Location: '.$protocol.'://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME'])); |
|
29
|
|
|
exit(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$http = new HttpHelper(); |
|
33
|
|
|
|
|
34
|
|
|
$ajaxAction = Filter::filterInput(INPUT_GET, 'ajaxaction', FILTER_SANITIZE_STRING); |
|
35
|
|
|
$attId = Filter::filterInput(INPUT_GET, 'attId', FILTER_VALIDATE_INT); |
|
36
|
|
|
$recordId = Filter::filterInput(INPUT_POST, 'record_id', FILTER_SANITIZE_STRING); |
|
37
|
|
|
$recordLang = Filter::filterInput(INPUT_POST, 'record_lang', FILTER_SANITIZE_STRING); |
|
38
|
|
|
$csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_SANITIZE_STRING); |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
|
|
$attachment = Factory::create($attId); |
|
42
|
|
|
|
|
43
|
|
|
switch ($ajaxAction) { |
|
44
|
|
|
case 'delete': |
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) { |
|
47
|
|
|
echo $PMF_LANG['err_NotAuth']; |
|
48
|
|
|
exit(1); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($attachment->delete()) { |
|
52
|
|
|
echo $PMF_LANG['msgAttachmentsDeleted']; |
|
53
|
|
|
} else { |
|
54
|
|
|
echo $PMF_LANG['ad_att_delfail']; |
|
55
|
|
|
} |
|
56
|
|
|
break; |
|
57
|
|
|
|
|
58
|
|
|
case 'upload': |
|
59
|
|
|
|
|
60
|
|
|
if (!isset($_FILES['filesToUpload'])) { |
|
61
|
|
|
$http->sendStatus(400); |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$files = Factory::rearrangeUploadedFiles($_FILES['filesToUpload']); |
|
66
|
|
|
$uploadedFiles = []; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($files as $file) { |
|
69
|
|
|
if ( |
|
70
|
|
|
is_uploaded_file($file['tmp_name']) && |
|
71
|
|
|
!($file['size'] > $faqConfig->get('records.maxAttachmentSize')) && |
|
72
|
|
|
$file['type'] !== "text/html" |
|
73
|
|
|
) { |
|
74
|
|
|
$attachment = Factory::create(); |
|
75
|
|
|
$attachment->setRecordId($recordId); |
|
76
|
|
|
$attachment->setRecordLang($recordLang); |
|
77
|
|
|
try { |
|
78
|
|
|
if (!$attachment->save($file['tmp_name'], $file['name'])) { |
|
79
|
|
|
throw new Exception(); |
|
80
|
|
|
} |
|
81
|
|
|
} catch (Exception $e) { |
|
82
|
|
|
$attachment->delete(); |
|
83
|
|
|
} |
|
84
|
|
|
$uploadedFiles[] = [ |
|
85
|
|
|
'attachmentId' => $attachment->getId(), |
|
86
|
|
|
'fileName' => $attachment->getFilename(), |
|
87
|
|
|
'faqId' => $recordId, |
|
88
|
|
|
'faqLanguage' => $recordLang |
|
89
|
|
|
]; |
|
90
|
|
|
} else { |
|
91
|
|
|
$http->sendStatus(400); |
|
92
|
|
|
$http->sendJsonWithHeaders('image too large'); |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$http->sendJsonWithHeaders($uploadedFiles); |
|
98
|
|
|
|
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
102
|
|
|
// handle exception |
|
103
|
|
|
} |
|
104
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: