friendica-addons/js_upload/js_upload.php

353 lines
9.4 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;
2011-09-25 04:56:03 -04:00
function js_upload_install() {
register_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
register_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
register_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
register_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
}
function js_upload_uninstall() {
unregister_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
unregister_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
unregister_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
unregister_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
}
function js_upload_form(&$a,&$b) {
$b['default_upload'] = false;
$b['addon_text'] .= '<link href="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.css" rel="stylesheet" type="text/css">';
$b['addon_text'] .= '<script src="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.js" type="text/javascript"></script>';
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
$upload_msg = t('Upload a file');
$drop_msg = t('Drop files here to upload');
$cancel = t('Cancel');
$failed = t('Failed');
$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) {
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 08:15:33 -05:00
}
2011-09-25 04:56:03 -04:00
function createUploader() {
uploader = new qq.FileUploader({
element: document.getElementById('file-uploader-demo1'),
action: '{$b['post_url']}',
2018-01-15 08:15:33 -05:00
template: '<div class="qq-uploader">' +
2011-09-25 04:56:03 -04:00
'<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
'<div class="qq-upload-button">$upload_msg</div>' +
2018-01-15 08:15:33 -05:00
'<ul class="qq-upload-list"></ul>' +
2011-09-25 04:56:03 -04: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 08:15:33 -05:00
'</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) {
if (typeof acl!="undefined"){
uploader.setParams( {
newalbum : document.getElementById('photos-upload-newalbum').value,
album : document.getElementById('photos-upload-album-select').value,
2011-10-01 09:19:34 -04:00
not_visible : document.getElementById('photos-upload-noshare').checked,
2011-09-25 04:56:03 -04:00
group_allow : acl.allow_gid.join(','),
contact_allow : acl.allow_cid.join(','),
group_deny : acl.deny_gid.join(','),
contact_deny : acl.deny_cid.join(',')
});
} else {
uploader.setParams( {
newalbum : document.getElementById('photos-upload-newalbum').value,
album : document.getElementById('photos-upload-album-select').value,
2011-10-01 09:19:34 -04:00
not_visible : document.getElementById('photos-upload-noshare').checked,
2011-09-25 04:56:03 -04:00
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(',')
});
}
}
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'])) {
logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
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('upload_post_end');
if(x($a->data,'upload_jsonresponse')) {
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 08:15:33 -05:00
function save() {
2011-09-25 04:56:03 -04: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 04:56:03 -04:00
$temp = fopen($this->pathnm,"w");
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
fclose($temp);
2018-01-15 08:15:33 -05:00
if ($realSize != $this->getSize()){
2011-09-25 04:56:03 -04: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 08:15:33 -05:00
return (int)$_SERVER["CONTENT_LENGTH"];
2011-09-25 04:56:03 -04:00
} else {
throw new Exception('Getting content length is not supported.');
2018-01-15 08:15:33 -05:00
}
}
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
/**
* 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 08:15:33 -05:00
private $allowedExtensions = [];
2011-09-25 04:56:03 -04:00
private $sizeLimit = 10485760;
private $file;
2018-01-15 08:15:33 -05:00
function __construct(array $allowedExtensions = [], $sizeLimit = 10485760){
2011-09-25 04:56:03 -04:00
$allowedExtensions = array_map("strtolower", $allowedExtensions);
2018-01-15 08:15:33 -05:00
$this->allowedExtensions = $allowedExtensions;
2011-09-25 04:56:03 -04:00
$this->sizeLimit = $sizeLimit;
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
2018-01-15 08:15:33 -05:00
$this->file = false;
2011-09-25 04:56:03 -04:00
}
}
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -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;
2018-01-15 08:15:33 -05:00
case 'k': $val *= 1024;
2011-09-25 04:56:03 -04:00
}
return $val;
}
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
/**
* Returns array('success'=>true) or array('error'=>'error message')
*/
function handleUpload(){
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
if (!$this->file){
2018-01-15 08:15:33 -05:00
return ['error' => t('No files were uploaded.')];
2011-09-25 04:56:03 -04:00
}
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
$size = $this->file->getSize();
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
if ($size == 0) {
2018-01-15 08:15:33 -05:00
return ['error' => t('Uploaded file is empty')];
2011-09-25 04:56:03 -04:00
}
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
// if ($size > $this->sizeLimit) {
// return array('error' => 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-15 08:15:33 -05:00
return ['error' => t('Image exceeds size limit of ') . $maximagesize ];
2011-09-25 04:56:03 -04: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-15 08:15:33 -05:00
return ['error' => t('File has an invalid extension, it should be one of ') . $these . '.'];
2011-09-25 04:56:03 -04:00
}
2018-01-15 08:15:33 -05:00
2011-09-25 04:56:03 -04:00
if ($this->file->save()){
2018-01-15 08:15:33 -05:00
return [
2011-09-25 04:56:03 -04:00
'success'=>true,
2018-01-15 08:15:33 -05:00
'path' => $this->file->getPath(),
2011-09-25 04:56:03 -04:00
'filename' => $filename . '.' . $ext
2018-01-15 08:15:33 -05:00
];
2011-09-25 04:56:03 -04:00
} else {
2018-01-15 08:15:33 -05:00
return [
2011-09-25 04:56:03 -04:00
'error'=> t('Upload was cancelled, or server error encountered'),
2018-01-15 08:15:33 -05:00
'path' => $this->file->getPath(),
2011-09-25 04:56:03 -04:00
'filename' => $filename . '.' . $ext
2018-01-15 08:15:33 -05:00
];
2011-09-25 04:56:03 -04:00
}
2018-01-15 08:15:33 -05:00
}
2011-09-25 04:56:03 -04:00
}