5 * JavaScript Photo/Image Uploader
7 * Uses Valum 'qq' Uploader.
8 * Module Author: Chris Case
10 * Prior to enabling, ensure that you have a directory 'uploads'
11 * which is writable by the web server.
16 function js_upload_install() {
17 register_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
18 register_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
19 register_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
20 register_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
24 function js_upload_uninstall() {
25 unregister_hook('photo_upload_form', 'addon/js_upload/js_upload.php', 'js_upload_form');
26 unregister_hook('photo_post_init', 'addon/js_upload/js_upload.php', 'js_upload_post_init');
27 unregister_hook('photo_post_file', 'addon/js_upload/js_upload.php', 'js_upload_post_file');
28 unregister_hook('photo_post_end', 'addon/js_upload/js_upload.php', 'js_upload_post_end');
32 function js_upload_form(&$a,&$b) {
34 $b['default_upload'] = false;
36 $b['addon_text'] .= '<link href="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.css" rel="stylesheet" type="text/css">';
37 $b['addon_text'] .= '<script src="' . $a->get_baseurl() . '/addon/js_upload/file-uploader/client/fileuploader.js" type="text/javascript"></script>';
39 $b['addon_text'] .= <<< EOT
41 <div id="file-uploader-demo1">
43 <p>Please enable JavaScript to use file uploader.</p>
44 <!-- or put a simple form for upload here -->
48 <script type="text/javascript">
50 function getSelected(opt) {
51 var selected = new Array();
53 for (var intLoop = 0; intLoop < opt.length; intLoop++) {
54 if ((opt[intLoop].selected) ||
55 (opt[intLoop].checked)) {
56 index = selected.length;
57 //selected[index] = new Object;
58 selected[index] = opt[intLoop].value;
59 //selected[index] = intLoop;
64 function createUploader() {
65 uploader = new qq.FileUploader({
66 element: document.getElementById('file-uploader-demo1'),
67 action: '{$b['post_url']}',
69 onSubmit: function(id,filename) {
72 newalbum : document.getElementById('photos-upload-newalbum').value,
73 album : document.getElementById('photos-upload-album-select').value,
74 group_allow : getSelected(document.getElementById('group_allow')).join(','),
75 contact_allow : getSelected(document.getElementById('contact_allow')).join(','),
76 group_deny : getSelected(document.getElementById('group_deny')).join(','),
77 contact_deny : getSelected(document.getElementById('contact_deny')).join(',')
84 // in your app create uploader as soon as the DOM is ready
85 // don't wait for the window to load
86 window.onload = createUploader;
96 function js_upload_post_init(&$a,&$b) {
98 // list of valid extensions, ex. array("jpeg", "xml", "bmp")
100 $allowedExtensions = array("jpeg","gif","png","jpg");
102 // max file size in bytes
104 $sizeLimit = 6 * 1024 * 1024;
106 $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
108 $result = $uploader->handleUpload('uploads/');
111 // to pass data through iframe you will need to encode all html tags
112 $a->data['upload_jsonresponse'] = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
114 if(isset($result['error'])) {
115 logger('mod/photos.php: photos_post(): error uploading photo: ' . $result['error'] , 'LOGGER_DEBUG');
119 $a->data['upload_result'] = $result;
123 function js_upload_post_file(&$a,&$b) {
125 $result = $a->data['upload_result'];
127 $b['src'] = 'uploads/' . $result['filename'];
128 $b['filename'] = $result['filename'];
129 $b['filesize'] = filesize($b['src']);
131 logger('post_file' . print_r($b, true));
135 function js_upload_post_end(&$a,&$b) {
137 logger('upload_post_end');
138 if(x($a->data,'upload_jsonresponse')) {
139 echo $a->data['upload_jsonresponse'];
147 * Handle file uploads via XMLHttpRequest
149 class qqUploadedFileXhr {
151 * Save the file to the specified path
152 * @return boolean TRUE on success
154 function save($path) {
155 $input = fopen("php://input", "r");
157 $realSize = stream_copy_to_stream($input, $temp);
160 if ($realSize != $this->getSize()){
164 $target = fopen($path, "w");
165 fseek($temp, 0, SEEK_SET);
166 stream_copy_to_stream($temp, $target);
173 return $_GET['qqfile'];
177 if (isset($_SERVER["CONTENT_LENGTH"])){
178 return (int)$_SERVER["CONTENT_LENGTH"];
180 throw new Exception('Getting content length is not supported.');
186 * Handle file uploads via regular form post (uses the $_FILES array)
189 class qqUploadedFileForm {
191 * Save the file to the specified path
192 * @return boolean TRUE on success
194 function save($path) {
195 if(!move_uploaded_file($_FILES['qqfile']['tmp_name'], $path)){
201 return $_FILES['qqfile']['name'];
204 return $_FILES['qqfile']['size'];
208 class qqFileUploader {
209 private $allowedExtensions = array();
210 private $sizeLimit = 10485760;
213 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
214 $allowedExtensions = array_map("strtolower", $allowedExtensions);
216 $this->allowedExtensions = $allowedExtensions;
217 $this->sizeLimit = $sizeLimit;
219 $this->checkServerSettings();
221 if (isset($_GET['qqfile'])) {
222 $this->file = new qqUploadedFileXhr();
223 } elseif (isset($_FILES['qqfile'])) {
224 $this->file = new qqUploadedFileForm();
230 private function checkServerSettings(){
231 $postSize = $this->toBytes(ini_get('post_max_size'));
232 $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
233 logger('mod/photos.php: qqFileUploader(): upload_max_filesize=' . $uploadSize , 'LOGGER_DEBUG');
234 if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
235 $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
236 die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
240 private function toBytes($str){
242 $last = strtolower($str[strlen($str)-1]);
244 case 'g': $val *= 1024;
245 case 'm': $val *= 1024;
246 case 'k': $val *= 1024;
252 * Returns array('success'=>true) or array('error'=>'error message')
254 function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
255 if (!is_writable($uploadDirectory)){
256 return array('error' => t('Server error. Upload directory isn\'t writable.'));
260 return array('error' => t('No files were uploaded.'));
263 $size = $this->file->getSize();
266 return array('error' => t('Uploaded file is empty'));
269 if ($size > $this->sizeLimit) {
271 return array('error' => t('Uploaded file is too large'));
275 $maximagesize = get_config('system','maximagesize');
277 if(($maximagesize) && ($size > $maximagesize)) {
278 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
282 $pathinfo = pathinfo($this->file->getName());
283 $filename = $pathinfo['filename'];
285 $ext = $pathinfo['extension'];
287 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
288 $these = implode(', ', $this->allowedExtensions);
289 return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
292 if(!$replaceOldFile){
293 /// don't overwrite previous files that were uploaded
294 while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
295 $filename .= rand(10, 99);
299 if ($this->file->save($uploadDirectory . $filename . '.' . $ext)){
300 return array('success'=>true,'filename' => $filename . '.' . $ext);
302 return array('error'=> t('Could not save uploaded file.') .
303 t('The upload was cancelled, or server error encountered'),'filename' => $filename . '.' . $ext);