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();
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'] = $result['path'];
128 $b['filename'] = $result['filename'];
129 $b['filesize'] = filesize($b['src']);
134 function js_upload_post_end(&$a,&$b) {
136 logger('upload_post_end');
137 if(x($a->data,'upload_jsonresponse')) {
138 echo $a->data['upload_jsonresponse'];
146 * Handle file uploads via XMLHttpRequest
148 class qqUploadedFileXhr {
150 private $pathnm = '';
153 * Save the file in the temp dir.
154 * @return boolean TRUE on success
157 $input = fopen("php://input", "r");
158 $this->pathnm = tempnam(sys_get_temp_dir(),'frn');
159 $temp = fopen($this->pathnm,"w");
160 $realSize = stream_copy_to_stream($input, $temp);
165 if ($realSize != $this->getSize()){
172 return $this->pathnm;
176 return $_GET['qqfile'];
180 if (isset($_SERVER["CONTENT_LENGTH"])){
181 return (int)$_SERVER["CONTENT_LENGTH"];
183 throw new Exception('Getting content length is not supported.');
189 * Handle file uploads via regular form post (uses the $_FILES array)
192 class qqUploadedFileForm {
196 * Save the file to the specified path
197 * @return boolean TRUE on success
206 return $_FILES['qqfile']['tmp_name'];
210 return $_FILES['qqfile']['name'];
213 return $_FILES['qqfile']['size'];
217 class qqFileUploader {
218 private $allowedExtensions = array();
219 private $sizeLimit = 10485760;
222 function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
223 $allowedExtensions = array_map("strtolower", $allowedExtensions);
225 $this->allowedExtensions = $allowedExtensions;
226 $this->sizeLimit = $sizeLimit;
228 $this->checkServerSettings();
230 if (isset($_GET['qqfile'])) {
231 $this->file = new qqUploadedFileXhr();
232 } elseif (isset($_FILES['qqfile'])) {
233 $this->file = new qqUploadedFileForm();
240 private function checkServerSettings(){
241 $postSize = $this->toBytes(ini_get('post_max_size'));
242 $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));
243 logger('mod/photos.php: qqFileUploader(): upload_max_filesize=' . $uploadSize , 'LOGGER_DEBUG');
244 if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
245 $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
246 die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
250 private function toBytes($str){
252 $last = strtolower($str[strlen($str)-1]);
254 case 'g': $val *= 1024;
255 case 'm': $val *= 1024;
256 case 'k': $val *= 1024;
262 * Returns array('success'=>true) or array('error'=>'error message')
264 function handleUpload(){
267 return array('error' => t('No files were uploaded.'));
270 $size = $this->file->getSize();
273 return array('error' => t('Uploaded file is empty'));
276 if ($size > $this->sizeLimit) {
278 return array('error' => t('Uploaded file is too large'));
282 $maximagesize = get_config('system','maximagesize');
284 if(($maximagesize) && ($size > $maximagesize)) {
285 return array('error' => t('Image exceeds size limit of ') . $maximagesize );
289 $pathinfo = pathinfo($this->file->getName());
290 $filename = $pathinfo['filename'];
292 $ext = $pathinfo['extension'];
294 if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
295 $these = implode(', ', $this->allowedExtensions);
296 return array('error' => t('File has an invalid extension, it should be one of ') . $these . '.');
299 if ($this->file->save()){
302 'path' => $this->file->getPath(),
303 'filename' => $filename . '.' . $ext
307 'error'=> t('Upload was cancelled, or server error encountered'),
308 'path' => $this->file->getPath(),
309 'filename' => $filename . '.' . $ext