friendica-addons/js_upload/js_upload.php

351 lines
9.6 KiB
PHP
Raw Normal View History

2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
* Uses Valum 'qq' Uploader.
2011-09-25 10:56:03 +02:00
* Module Author: Chris Case
*
*/
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-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
$upload_msg = t('Upload a file');
$drop_msg = t('Drop files here to upload');
$cancel = t('Cancel');
$failed = t('Failed');
2018-02-12 01:00:01 +01:00
$maximagesize = intval(get_config('system','maximagesize'));
2011-09-25 10:56:03 +02:00
$b['addon_text'] .= <<< EOT
2018-02-12 01:00:01 +01:00
<div id="file-uploader-demo1">
<noscript>
2011-09-25 10:56:03 +02:00
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
2018-02-12 01:00:01 +01:00
</noscript>
2011-09-25 10:56:03 +02:00
</div>
<script type="text/javascript">
2018-02-12 01:00:01 +01:00
var uploader = null;
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
}
2011-09-25 10:56:03 +02:00
function createUploader() {
uploader = new qq.FileUploader({
element: document.getElementById('file-uploader-demo1'),
action: '{$b['post_url']}',
2018-02-12 01:00:01 +01:00
template: '<div class="qq-uploader">' +
2011-09-25 10:56:03 +02:00
'<div class="qq-upload-drop-area"><span>$drop_msg</span></div>' +
'<div class="qq-upload-button">$upload_msg</div>' +
2018-02-12 01:00:01 +01:00
'<ul class="qq-upload-list"></ul>' +
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
'</li>',
2011-09-25 10:56:03 +02:00
debug: true,
sizeLimit: $maximagesize,
2011-09-25 10:56:03 +02: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 15:19:34 +02:00
not_visible : document.getElementById('photos-upload-noshare').checked,
2011-09-25 10:56:03 +02: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 15:19:34 +02:00
not_visible : document.getElementById('photos-upload-noshare').checked,
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
});
2011-09-25 10:56:03 +02:00
}
// in your app create uploader as soon as the DOM is ready
2018-02-12 01:00:01 +01:00
// don't wait for the window to load
window.onload = createUploader;
2011-09-25 10:56:03 +02:00
</script>
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
EOT;
}
function js_upload_post_init(&$a,&$b) {
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
2018-02-12 01:00:01 +01:00
$allowedExtensions = array("jpeg","gif","png","jpg");
2011-09-25 10:56:03 +02:00
// max file size in bytes
2018-02-12 01:00:01 +01:00
$sizeLimit = get_config('system','maximagesize'); //6 * 1024 * 1024;
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
function save() {
2011-09-25 10:56:03 +02:00
$input = fopen("php://input", "r");
2018-02-12 01:00:01 +01:00
$upload_dir = get_config('system','tempdir');
if(! $upload_dir)
$upload_dir = sys_get_temp_dir();
$this->pathnm = tempnam($upload_dir,'frn');
2011-09-25 10:56:03 +02:00
$temp = fopen($this->pathnm,"w");
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
fclose($temp);
2018-02-12 01:00:01 +01:00
if ($realSize != $this->getSize()){
2011-09-25 10:56:03 +02:00
return false;
}
return true;
}
function getPath() {
return $this->pathnm;
}
function getName() {
return $_GET['qqfile'];
}
function getSize() {
if (isset($_SERVER["CONTENT_LENGTH"])){
2018-02-12 01:00:01 +01:00
return (int)$_SERVER["CONTENT_LENGTH"];
2011-09-25 10:56:03 +02:00
} else {
throw new Exception('Getting content length is not supported.');
2018-02-12 01:00:01 +01:00
}
}
2011-09-25 10:56:03 +02:00
}
/**
* Handle file uploads via regular form post (uses the $_FILES array)
*/
2018-02-12 01:00:01 +01:00
class qqUploadedFileForm {
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
private $allowedExtensions = array();
2011-09-25 10:56:03 +02:00
private $sizeLimit = 10485760;
private $file;
2018-02-12 01:00:01 +01:00
function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
2011-09-25 10:56:03 +02:00
$allowedExtensions = array_map("strtolower", $allowedExtensions);
2018-02-12 01:00:01 +01:00
$this->allowedExtensions = $allowedExtensions;
2011-09-25 10:56:03 +02:00
$this->sizeLimit = $sizeLimit;
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
if (isset($_GET['qqfile'])) {
$this->file = new qqUploadedFileXhr();
} elseif (isset($_FILES['qqfile'])) {
$this->file = new qqUploadedFileForm();
} else {
2018-02-12 01:00:01 +01:00
$this->file = false;
2011-09-25 10:56:03 +02:00
}
}
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
case 'k': $val *= 1024;
2011-09-25 10:56:03 +02:00
}
return $val;
}
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
/**
* Returns array('success'=>true) or array('error'=>'error message')
*/
function handleUpload(){
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
if (!$this->file){
2018-02-12 01:00:01 +01:00
return array('error' => t('No files were uploaded.'));
2011-09-25 10:56:03 +02:00
}
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
$size = $this->file->getSize();
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
if ($size == 0) {
2018-02-12 01:00:01 +01:00
return array('error' => t('Uploaded file is empty'));
2011-09-25 10:56:03 +02:00
}
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
// if ($size > $this->sizeLimit) {
// return array('error' => t('Uploaded file is too large'));
// }
2018-02-12 01:00:01 +01:00
2018-01-15 14:15:33 +01:00
2018-02-12 01:00:01 +01:00
$maximagesize = get_config('system','maximagesize');
2011-09-25 10:56:03 +02:00
if(($maximagesize) && ($size > $maximagesize)) {
2018-02-12 01:00:01 +01:00
return array('error' => t('Image exceeds size limit of ') . $maximagesize );
2011-09-25 10:56:03 +02: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-02-12 01:00:01 +01:00
return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
2011-09-25 10:56:03 +02:00
}
2018-02-12 01:00:01 +01:00
2011-09-25 10:56:03 +02:00
if ($this->file->save()){
2018-02-12 01:00:01 +01:00
return array(
2011-09-25 10:56:03 +02:00
'success'=>true,
2018-02-12 01:00:01 +01:00
'path' => $this->file->getPath(),
2011-09-25 10:56:03 +02:00
'filename' => $filename . '.' . $ext
2018-02-12 01:00:01 +01:00
);
2011-09-25 10:56:03 +02:00
} else {
2018-02-12 01:00:01 +01:00
return array(
2011-09-25 10:56:03 +02:00
'error'=> t('Upload was cancelled, or server error encountered'),
2018-02-12 01:00:01 +01:00
'path' => $this->file->getPath(),
2011-09-25 10:56:03 +02:00
'filename' => $filename . '.' . $ext
2018-02-12 01:00:01 +01:00
);
2011-09-25 10:56:03 +02:00
}
2018-02-12 01:00:01 +01:00
}
2011-09-25 10:56:03 +02:00
}