Created
September 6, 2021 15:47
-
-
Save pupi1985/03778d79da0b13446b26eb81d169df3a to your computer and use it in GitHub Desktop.
Improved image upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Question2Answer by Gideon Greenspan and contributors | |
http://www.question2answer.org/ | |
File: qa-plugin/wysiwyg-editor/qa-wysiwyg-upload.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_wysiwyg_upload | |
{ | |
public function match_request($request) | |
{ | |
return $request === 'wysiwyg-editor-upload'; | |
} | |
public function process_request($request) | |
{ | |
$message = ''; | |
$url = ''; | |
if (qa_post_limit_exceeded()) { | |
ob_end_clean(); | |
ob_clean(); | |
qa_initialize_buffering($request); | |
$message = qa_lang('main/file_upload_limit_exceeded'); | |
} else { | |
if (isset($_FILES['upload']) && is_array($_FILES['upload'])) { | |
$fileError = $_FILES['upload']['error']; | |
// Note if $_FILES['upload']['error'] === 1 then upload_max_filesize has been exceeded | |
if ($fileError === 1) { | |
ob_end_clean(); | |
ob_clean(); | |
qa_initialize_buffering($request); | |
$message = qa_lang('main/file_upload_limit_exceeded'); | |
} else if ($fileError === 0 && $_FILES['upload']['size'] > 0) { | |
require_once QA_INCLUDE_DIR . 'app/limits.php'; | |
if (qa_opt('wysiwyg_editor_upload_images')) { | |
require_once QA_INCLUDE_DIR . 'app/upload.php'; | |
$onlyImage = qa_get('qa_only_image'); | |
$upload = qa_upload_file_one( | |
qa_opt('wysiwyg_editor_upload_max_size'), | |
$onlyImage || !qa_opt('wysiwyg_editor_upload_all'), | |
$onlyImage ? 600 : null, // max width if it's an image upload | |
null // no max height | |
); | |
if (isset($upload['error'])) { | |
$message = $upload['error']; | |
} else { | |
$url = $upload['bloburl']; | |
} | |
} else { | |
$message = qa_lang('users/no_permission'); | |
} | |
} // There shouldn't be any need to catch any other error | |
} | |
} | |
echo sprintf( | |
'<script>window.parent.CKEDITOR.tools.callFunction(%s, %s, %s);</script>', | |
qa_js(qa_get('CKEditorFuncNum')), | |
qa_js($url), | |
qa_js($message) | |
); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment