forked from svivian/q2a-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqa-markdown-upload.php
139 lines (100 loc) · 4.88 KB
/
qa-markdown-upload.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
Question2Answer (c) Gideon Greenspan
http://www.question2answer.org/
File: qa-plugin/wysiwyg-editor/qa-wysiwyg-upload.php
Version: See define()s at top of qa-include/qa-base.php
Description: Page module class for WYSIWYG editor (CKEditor) file upload receiver
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
class qa_markdown_upload {
function match_request($request)
{
return ($request=='markdown-editor-upload');
}
function process_request($request)
{
$message='';
$url='';
$success = false;
if (is_array($_FILES) && count($_FILES)) {
// Check that we're allowed to upload images (if not, no other uploads are allowed either)
if (!qa_opt('markdown_editor_upload_images'))
$message=qa_lang('users/no_permission');
// Check that we haven't reached the upload limit and are not blocked
if (empty($message)) {
require_once QA_INCLUDE_DIR.'qa-app-users.php';
require_once QA_INCLUDE_DIR.'qa-app-limits.php';
switch (qa_user_permit_error(null, QA_LIMIT_UPLOADS))
{
case 'limit':
$message=qa_lang('main/upload_limit');
break;
case false:
qa_limits_increment(qa_get_logged_in_userid(), QA_LIMIT_UPLOADS);
break;
default:
$message=qa_lang('users/no_permission');
break;
}
}
// Find out some information about the uploaded file and check it's not too large
if (empty($message)) {
require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
$file=reset($_FILES);
$pathinfo=pathinfo($file['name']);
$extension=strtolower(@$pathinfo['extension']);
$filesize=$file['size'];
$maxsize=min(qa_opt('markdown_editor_upload_max_size'), qa_get_max_upload_size());
if ( ($filesize<=0) || ($filesize>$maxsize) ) // if file was too big for PHP, $filesize will be zero
$message=qa_lang_sub('main/max_upload_size_x', number_format($maxsize/1048576, 1).'MB');
}
// If it's only allowed to be an image, check it's an image
if (empty($message))
if (qa_get('qa_only_image') || !qa_opt('markdown_editor_upload_all')) // check if we need to confirm it's an image
switch ($extension) {
case 'png': // these are allowed image extensions
case 'gif':
case 'jpeg':
case 'jpg':
if (function_exists('getimagesize')) // getimagesize() does not require GD library
if (!is_array(@getimagesize($file['tmp_name'])))
$message=qa_lang_sub('main/image_not_read', 'GIF, JPG, PNG');
break;
default:
$message=qa_lang_sub('main/image_not_read', 'GIF, JPG, PNG');
break;
}
// If there have been no errors, looks like we're all set...
if (empty($message)) {
require_once QA_INCLUDE_DIR.'qa-db-blobs.php';
$userid=qa_get_logged_in_userid();
$cookieid=isset($userid) ? qa_cookie_get() : qa_cookie_get_create();
$blobid=qa_db_blob_create(file_get_contents($file['tmp_name']), $extension, @$file['name'], $userid, $cookieid, qa_remote_ip_address());
if (isset($blobid)) {
$url=qa_get_blob_url($blobid, true);
$success = true;
}
else
$message='Failed to create object in database - please try again';
}
}
if($success) {
echo "<script type='text/javascript'>window.parent.qaFileUploadCallBack(true, ".qa_js($url).", ".qa_js($message).");</script>";
} else {
echo "<script type='text/javascript'>window.parent.qaFileUploadCallBack(false, ".qa_js($url).", ".qa_js($message).");</script>";
}
return null;
}
}
/*
Omit PHP closing tag to help avoid accidental output
*/