friendica-addons/js_upload/js_upload.php

359 lines
9.7 KiB
PHP
Raw Normal View History

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