friendica-addons/js_upload/js_upload.php

363 lines
9.0 KiB
PHP
Raw Normal View History

2011-09-25 04:56:03 -04:00
<?php
/**
* Name: JS Uploader
* Description: JavaScript photo/image uploader. Uses Valum 'qq' Uploader.
* Version: 1.0
* Author: Chris Case <http://friendika.openmindspace.org/profile/chris_case>
*/
/**
*
* JavaScript Photo/Image Uploader
*
2018-01-15 08:15:33 -05:00
* Uses Valum 'qq' Uploader.
2011-09-25 04:56:03 -04:00
* Module Author: Chris Case
*
*/
use Friendica\Core\Config;
use Friendica\Core\Hook;
2018-01-22 14:03:11 -05:00
use Friendica\Core\L10n;
use Friendica\Core\Logger;
2011-09-25 04:56:03 -04:00
function js_upload_install() {
Hook::register('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
Hook::register('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
Hook::register('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
Hook::register('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
2011-09-25 04:56:03 -04:00
}
function js_upload_uninstall() {
Hook::unregister('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
Hook::unregister('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
Hook::unregister('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
Hook::unregister('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
2011-09-25 04:56:03 -04:00
}
function js_upload_form(&$a,&$b) {
$b['default_upload'] = false;
$b['addon_text'] .= '<link href="' . $a->getBaseURL() . '/addon/js_upload/file-uploader/client/fileuploader.css" rel="stylesheet" type="text/css">';
$b['addon_text'] .= '<script src="' . $a->getBaseURL() . '/addon/js_upload/file-uploader/client/fileuploader.js" type="text/javascript"></script>';
2018-01-15 08:15:33 -05:00
$upload_msg = L10n::t('Select files for upload');
2018-01-22 14:03:11 -05:00
$drop_msg = L10n::t('Drop files here to upload');
$cancel = L10n::t('Cancel');
$failed = L10n::t('Failed');
2011-09-25 04:56:03 -04:00
$maximagesize = intval(Config::get('system','maximagesize'));
2011-09-25 04:56:03 -04:00
$b['addon_text'] .= <<< EOT
2018-01-15 08:15:33 -05:00
<div id="file-uploader-demo1">
<noscript>
2011-09-25 04:56:03 -04:00
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
2018-01-15 08:15:33 -05:00
</noscript>
2011-09-25 04:56:03 -04:00
</div>
<script type="text/javascript">
2018-01-15 08:15:33 -05:00
var uploader = null;
2011-09-25 04:56:03 -04:00
function getSelected(opt) {
2019-03-13 15:58:54 -04:00
var selected = new Array();
var index = 0;
for (var intLoop = 0; intLoop < opt.length; intLoop++) {
2019-03-13 16:01:09 -04:00
if ((opt[intLoop].selected) ||
(opt[intLoop].checked)) {
index = selected.length;
//selected[index] = new Object;
selected[index] = opt[intLoop].value;
//selected[index] = intLoop;
}
2019-03-13 15:58:54 -04:00
}
return selected;
}
2011-09-25 04:56:03 -04:00
function createUploader() {
uploader = new qq.FileUploader({
element: document.getElementById('file-uploader-demo1'),
action: '{$b['post_url']}',
2019-03-13 15:58:54 -04:00
template: '<div class="qq-uploader">' +
'<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
'<div class="qq-upload-button">$upload_msg</div>' +
'<ul class="qq-upload-list"></ul>' +
'</div>',
// template for one item in file list
fileTemplate: '<li>' +
'<span class="qq-upload-file"></span>' +
'<span class="qq-upload-spinner"></span>' +
'<span class="qq-upload-size"></span>' +
'<a class="qq-upload-cancel" href="#">$cancel</a>' +
'<span class="qq-upload-failed-text">$failed</span>' +
'</li>',
2011-09-25 04:56:03 -04:00
debug: true,
sizeLimit: $maximagesize,
2011-09-25 04:56:03 -04:00
onSubmit: function(id,filename) {
var newalbumElm = document.getElementById('photos-upload-newalbum');
var albumElm = document.getElementById('photos-upload-album-select');
var newalbum = newalbumElm ? newalbumElm.value : "";
var album = albumElm ? albumElm.value : "";
if (typeof acl != "undefined"){
2011-09-25 04:56:03 -04:00
uploader.setParams( {
2019-03-13 16:01:09 -04:00
newalbum : newalbum,
album : album,
not_visible : document.getElementById('photos-upload-noshare').checked,
group_allow : acl.allow_gid.join(','),
contact_allow : acl.allow_cid.join(','),
2019-03-13 16:01:09 -04:00
group_deny : acl.deny_gid.join(','),
contact_deny : acl.deny_cid.join(',')
2011-09-25 04:56:03 -04:00
});
} else {
uploader.setParams( {
2019-03-13 16:01:09 -04:00
newalbum : newalbum,
album : album,
not_visible : document.getElementById('photos-upload-noshare').checked,
group_allow : getSelected(document.getElementById('group_allow')).join(','),
contact_allow : getSelected(document.getElementById('contact_allow')).join(','),
2019-03-13 16:01:09 -04:00
group_deny : getSelected(document.getElementById('group_deny')).join(','),
contact_deny : getSelected(document.getElementById('contact_deny')).join(',')
2011-09-25 04:56:03 -04:00
});
}
}
2018-01-15 08:15:33 -05:00
});
2011-09-25 04:56:03 -04:00
}
// in your app create uploader as soon as the DOM is ready
2018-01-15 08:15:33 -05:00
// don't wait for the window to load
window.onload = createUploader;
2011-09-25 04:56:03 -04:00
</script>
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
EOT;
}
function js_upload_post_init(&$a,&$b) {
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
2018-01-15 08:15:33 -05:00
$allowedExtensions = ["jpeg","gif","png","jpg"];
2011-09-25 04:56:03 -04:00
// max file size in bytes
$sizeLimit = Config::get('system','maximagesize'); //6 * 1024 * 1024;
2011-09-25 04:56:03 -04:00
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload();
// to pass data through iframe you will need to encode all html tags
$a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
if(isset($result['error'])) {
2018-10-30 09:48:09 -04:00
Logger::log('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , Logger::DEBUG);
2011-09-25 04:56:03 -04:00
echo json_encode($result);
2018-12-26 00:39:53 -05:00
exit();
2011-09-25 04:56:03 -04:00
}
$a->data['upload_result'] = $result;
}
function js_upload_post_file(&$a,&$b) {
$result = $a->data['upload_result'];
2019-03-13 16:01:09 -04:00
$b['src'] = $result['path'];
$b['filename'] = $result['filename'];
$b['filesize'] = filesize($b['src']);
2011-09-25 04:56:03 -04:00
}
function js_upload_post_end(&$a,&$b) {
Logger::log('upload_post_end');
if(!empty($a->data['upload_jsonresponse'])) {
2011-09-25 04:56:03 -04:00
echo $a->data['upload_jsonresponse'];
2018-12-26 00:39:53 -05:00
exit();
2011-09-25 04:56:03 -04:00
}
}
/**
* Handle file uploads via XMLHttpRequest
*/
class qqUploadedFileXhr {
private $pathnm = '';
2019-03-13 15:58:54 -04:00
/**
* Save the file in the temp dir.
* @return boolean TRUE on success
*/
function save() {
$input = fopen("php://input", "r");
$upload_dir = Config::get('system','tempdir');
if(! $upload_dir)
$upload_dir = sys_get_temp_dir();
2019-03-13 15:58:54 -04:00
$this->pathnm = tempnam($upload_dir,'frn');
2011-09-25 04:56:03 -04:00
$temp = fopen($this->pathnm,"w");
2019-03-13 15:58:54 -04:00
$realSize = stream_copy_to_stream($input, $temp);
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
fclose($input);
2011-09-25 04:56:03 -04:00
fclose($temp);
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
if ($realSize != $this->getSize()) {
return false;
}
return true;
}
2011-09-25 04:56:03 -04:00
function getPath() {
return $this->pathnm;
}
2019-03-13 15:58:54 -04:00
function getName() {
return $_GET['qqfile'];
}
function getSize() {
if (isset($_SERVER["CONTENT_LENGTH"])){
return (int)$_SERVER["CONTENT_LENGTH"];
} else {
throw new Exception('Getting content length is not supported.');
}
}
2011-09-25 04:56:03 -04:00
}
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
2018-01-15 08:15:33 -05:00
class qqUploadedFileForm {
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
function save() {
return true;
}
2011-09-25 04:56:03 -04:00
function getPath() {
return $_FILES['qqfile']['tmp_name'];
}
2019-03-13 15:58:54 -04:00
function getName() {
return $_FILES['qqfile']['name'];
}
function getSize() {
return $_FILES['qqfile']['size'];
}
2011-09-25 04:56:03 -04:00
}
class qqFileUploader {
2019-03-13 15:58:54 -04:00
private $allowedExtensions = [];
private $sizeLimit = 10485760;
private $file;
function __construct(array $allowedExtensions = [], $sizeLimit = 10485760){
$allowedExtensions = array_map("strtolower", $allowedExtensions);
$this->allowedExtensions = $allowedExtensions;
$this->sizeLimit = $sizeLimit;
if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
$this->file = false;
}
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
private function toBytes($str){
$val = trim($str);
$last = strtolower($str[strlen($str)-1]);
switch($last) {
case 'g': $val *= 1024;
case 'm': $val *= 1024;
case 'k': $val *= 1024;
}
return $val;
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
/**
* Returns array('success'=>true) or array('error'=>'error message')
*/
function handleUpload(){
2018-01-15 08:15:33 -05:00
2019-03-13 16:01:09 -04:00
if (!$this->file) {
2019-03-13 15:58:54 -04:00
return ['error' => L10n::t('No files were uploaded.')];
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
$size = $this->file->getSize();
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
if ($size == 0) {
return ['error' => L10n::t('Uploaded file is empty')];
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
// if ($size > $this->sizeLimit) {
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
// return array('error' => L10n::t('Uploaded file is too large'));
// }
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
$maximagesize = Config::get('system','maximagesize');
2011-09-25 04:56:03 -04:00
if(($maximagesize) && ($size > $maximagesize)) {
2018-01-22 14:03:11 -05:00
return ['error' => L10n::t('Image exceeds size limit of ') . $maximagesize ];
2011-09-25 04:56:03 -04:00
}
2019-03-13 15:58:54 -04:00
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
if (!isset($pathinfo['extension'])) {
Logger::warning('extension isn\'t set.', ['filename' => $filename]);
}
$ext = $pathinfo['extension'] ?? '';
2011-09-25 04:56:03 -04:00
2019-03-13 15:58:54 -04:00
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return ['error' => L10n::t('File has an invalid extension, it should be one of ') . $these . '.'];
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
if ($this->file->save()){
return [
2019-03-13 16:01:09 -04:00
'success' => true,
'path' => $this->file->getPath(),
2011-09-25 04:56:03 -04:00
'filename' => $filename . '.' . $ext
2018-01-15 08:15:33 -05:00
];
2019-03-13 15:58:54 -04:00
} else {
return [
2019-03-13 16:01:09 -04:00
'error' => L10n::t('Upload was cancelled, or server error encountered'),
'path' => $this->file->getPath(),
2011-09-25 04:56:03 -04:00
'filename' => $filename . '.' . $ext
2018-01-15 08:15:33 -05:00
];
2019-03-13 15:58:54 -04:00
}
2018-01-15 08:15:33 -05:00
2019-03-13 15:58:54 -04:00
}
2011-09-25 04:56:03 -04:00
}