From a1cee6c2f4d67c70da17c3563402ffca3f13f330 Mon Sep 17 00:00:00 2001 From: conejoninja Date: Wed, 20 Nov 2013 11:30:53 +0100 Subject: [PATCH] added ajax uploader to osclass and bender theme by default. issues #60 , #849 --- .../themes/bender/css/ajax-uploader.css | 53 +++++ oc-content/themes/bender/functions.php | 4 + oc-content/themes/bender/item-post.php | 9 +- oc-includes/AjaxUploader.php | 30 +-- oc-includes/osclass/ItemActions.php | 13 ++ oc-includes/osclass/controller/ajax.php | 59 ++++- oc-includes/osclass/cron.php | 7 + oc-includes/osclass/frm/Item.form.class.php | 214 ++++++++++++++++-- oc-load.php | 2 + 9 files changed, 344 insertions(+), 47 deletions(-) create mode 100644 oc-content/themes/bender/css/ajax-uploader.css diff --git a/oc-content/themes/bender/css/ajax-uploader.css b/oc-content/themes/bender/css/ajax-uploader.css new file mode 100644 index 0000000000..e6d8d731f4 --- /dev/null +++ b/oc-content/themes/bender/css/ajax-uploader.css @@ -0,0 +1,53 @@ +.ajax_preview_img img { + max-width: 120px; + max-height: 120px; + display: block; + margin-left: auto; + margin-right: auto; +} + +#restricted-fine-uploader { + padding-top: 1em; + padding-left: 30px; + margin-bottom: 20px; +} + +.photos_div div, .ajax_preview_img { + margin-right: 15px; + padding: 10px; + border: 1px black solid; + width: 125px; + height: 125px; + background: white; + margin-top: 10px; +} +.qq-upload-success div.ajax_preview_img img { + width: initial; +} + +.qq-upload-list li { + float: left; + background-color: #dff0d8!important; + color: #333333!important; + display: block; + min-height: 200px; +} + +/* width: 47%;*/ + +.qq-upload-button { + background-color: #f2dede; + color: #333333; + margin-bottom: 10px; +} + +.qq-upload-file { + width: 145px; + height: 1em; + display: block; + overflow: hidden; +} + +.qq-uploader { + width: 100%; +} \ No newline at end of file diff --git a/oc-content/themes/bender/functions.php b/oc-content/themes/bender/functions.php index fc97518679..a419167393 100755 --- a/oc-content/themes/bender/functions.php +++ b/oc-content/themes/bender/functions.php @@ -33,6 +33,10 @@ osc_enqueue_script('fancybox'); // used for date/dateinterval custom fields osc_enqueue_script('php-date'); + osc_enqueue_style('fine-uploader-css', osc_assets_url('js/fineuploader/fineuploader.css')); + osc_enqueue_style('bender-fine-uploader-css', osc_current_web_theme_url('css/ajax-uploader.css')); + osc_enqueue_script('jquery-fineuploader'); + /** diff --git a/oc-content/themes/bender/item-post.php b/oc-content/themes/bender/item-post.php index 1cf4fea7b1..ced2dd170f 100755 --- a/oc-content/themes/bender/item-post.php +++ b/oc-content/themes/bender/item-post.php @@ -30,10 +30,11 @@ $action = 'item_edit_post'; $edit = true; } + ?> - +
@@ -76,7 +77,9 @@
- +

@@ -91,7 +94,7 @@
- +

diff --git a/oc-includes/AjaxUploader.php b/oc-includes/AjaxUploader.php index 75d15e8101..4f6cf93572 100644 --- a/oc-includes/AjaxUploader.php +++ b/oc-includes/AjaxUploader.php @@ -5,18 +5,18 @@ class AjaxUploader { private $_sizeLimit; private $_file; - function __construct($variable, array $allowedExtensions = null, $sizeLimit = null){ + function __construct(array $allowedExtensions = null, $sizeLimit = null){ if($allowedExtensions===null) { $allowedExtensions = osc_allowed_extension(); } if($sizeLimit===null) { $sizeLimit = 1024*osc_max_size_kb(); } - $this->_allowedExtensions = array_map("strtolower", $allowedExtensions); + $this->_allowedExtensions = $allowedExtensions; $this->_sizeLimit = $sizeLimit; if(!isset($_SERVER['CONTENT_TYPE'])) { $this->_file = false; } else if (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'multipart/') === 0) { - $this->_file = new AjaxUploadedFileForm($variable); + $this->_file = new AjaxUploadedFileForm(); } else { - $this->_file = new AjaxUploadedFileXhr($variable); + $this->_file = new AjaxUploadedFileXhr(); } } @@ -29,21 +29,24 @@ function handleUpload($uploadFilename, $replace = false){ if($size == 0) { return array('error' => __('File is empty')); } if($size > $this->_sizeLimit) { return array('error' => __('File is too large')); } - $pathinfo = pathinfo($this->_file->getName()); + $pathinfo = pathinfo($this->_file->getOriginalName()); $ext = @$pathinfo['extension']; + $uuid = pathinfo($uploadFilename); - if($this->_allowedExtensions && !in_array(strtolower($ext), $this->_allowedExtensions)){ - $these = implode(', ', $this->_allowedExtensions); - return array('error' => sprintf(__('File has an invalid extension, it should be one of %s.'), $these)); + if($this->_allowedExtensions && stripos($this->_allowedExtensions, strtolower($ext))===false){ + return array('error' => sprintf(__('File has an invalid extension, it should be one of %s.'), $this->_allowedExtensions)); } if(!$replace){ - if(file_exists($$uploadFilename)) { + if(file_exists($uploadFilename)) { return array('error' => 'Could not save uploaded file. File already exists'); } } - if($this->file->save($uploadFilename)){ + if($this->_file->save($uploadFilename)){ + $files = Session::newInstance()->_get('ajax_files'); + $files[Params::getParam('qquuid')] = $uuid['basename']; + Session::newInstance()->_set('ajax_files', $files); return array('success' => true); } else { return array('error' => 'Could not save uploaded file. The upload was cancelled, or server error encountered'); @@ -53,8 +56,7 @@ function handleUpload($uploadFilename, $replace = false){ } class AjaxUploadedFileXhr { - private $_variable; - function __construct($variable) { $this->_variable = $variable; } + function __construct() {} public function save($path) { $input = fopen("php://input", "r"); @@ -69,7 +71,7 @@ public function save($path) { return true; } - public function getOriginalName() { return Params::getParam($this->_variable); } + public function getOriginalName() { return Params::getParam('qqfile'); } public function getSize() { if(isset($_SERVER["CONTENT_LENGTH"])){ return (int)$_SERVER["CONTENT_LENGTH"]; @@ -81,7 +83,7 @@ public function getSize() { class AjaxUploadedFileForm { private $_file; - function __construct($variable) { $this->_file = Params::getFiles($variable); } + function __construct() { $this->_file = Params::getFiles('qqfile'); } public function save($path) { return move_uploaded_file($this->_file['tmp_name'], $path); } public function getOriginalName() { return $this->_file['name']; } public function getSize() { return $this->_file['size']; } diff --git a/oc-includes/osclass/ItemActions.php b/oc-includes/osclass/ItemActions.php index 953fa511ff..c5054df9de 100755 --- a/oc-includes/osclass/ItemActions.php +++ b/oc-includes/osclass/ItemActions.php @@ -1141,10 +1141,23 @@ public function prepareData( $is_add ) $aItem['title'] = Params::getParam('title'); $aItem['description'] = Params::getParam('description'); $aItem['photos'] = Params::getFiles('photos'); + $ajax_photos = Params::getParam('ajax_photos'); $aItem['s_ip'] = get_ip(); $aItem['d_coord_lat'] = (Params::getParam('d_coord_lat') != '') ? Params::getParam('d_coord_lat') : null; $aItem['d_coord_long'] = (Params::getParam('d_coord_long') != '') ? Params::getParam('d_coord_long') : null; + if(is_array($ajax_photos)) { + foreach($ajax_photos as $photo) { + if(file_exists(osc_content_path().'uploads/temp/'.$photo)) { + $aItem['photos']['name'][] = $photo; + $aItem['photos']['type'][] = 'image/*'; + $aItem['photos']['tmp_name'][] = osc_content_path().'uploads/temp/'.$photo; + $aItem['photos']['error'][] = UPLOAD_ERR_OK; + $aItem['photos']['size'][] = 0; + } + } + } + if($is_add || $this->is_admin) { $dt_expiration = Params::getParam('dt_expiration'); if($dt_expiration==-1) { diff --git a/oc-includes/osclass/controller/ajax.php b/oc-includes/osclass/controller/ajax.php index 3e68a84ace..f6c05e1ce0 100755 --- a/oc-includes/osclass/controller/ajax.php +++ b/oc-includes/osclass/controller/ajax.php @@ -66,12 +66,31 @@ function doModel() echo json_encode($cities); break; case 'delete_image': // Delete images via AJAX - $id = Params::getParam('id'); - $item = Params::getParam('item'); - $code = Params::getParam('code'); - $secret = Params::getParam('secret'); + $ajax_photo = Params::getParam('ajax_photo'); + $id = Params::getParam('id'); + $item = Params::getParam('item'); + $code = Params::getParam('code'); + $secret = Params::getParam('secret'); $json = array(); + if($ajax_photo!='') { + $files = Session::newInstance()->_get('ajax_files'); + $success = false; + + foreach($files as $uuid => $file) { + if($file==$ajax_photo) { + $filename = $files[$uuid]; + unset($files[$uuid]); + Session::newInstance()->_set('ajax_files', $files); + $success = @unlink(osc_content_path().'uploads/temp/'.$filename); + break; + } + } + + echo json_encode(array('success' => $success, 'msg' => $success?_m('The selected photo has been successfully deleted'):_m("The selected photo couldn't be deleted"))); + return false; + } + if( Session::newInstance()->_get('userId') != '' ){ $userId = Session::newInstance()->_get('userId'); $user = User::newInstance()->findByPrimaryKey($userId); @@ -254,15 +273,37 @@ function doModel() break; case 'ajax_upload': // Include the uploader class - $variable = Params::getParam('variable')?Params::getParam('variable'):'fu_images'; - $uploader = new AjaxUploader($variable); - @mkdir(osc_content_path().'/uploads/temp/'); + require_once(LIB_PATH."AjaxUploader.php"); + $uploader = new AjaxUploader(); + @mkdir(osc_content_path().'uploads/temp/'); $original = pathinfo($uploader->getOriginalName()); - $filename = uniqid($variable."_").".".$original['extension']; - $result = $uploader->handleUpload(osc_content_path().'/uploads/temp/'); + $filename = uniqid("qqfile_").".".$original['extension']; + $result = $uploader->handleUpload(osc_content_path().'uploads/temp/'.$filename); $result['uploadName'] = $filename; echo htmlspecialchars(json_encode($result), ENT_NOQUOTES); break; + case 'ajax_validate': + $id = Params::getParam('id'); + if(!is_numeric($id)) { echo json_encode(array('success' => false)); die();} + $secret = Params::getParam('secret'); + $item = Item::newInstance()->findByPrimaryKey($id); + if($item['s_secret']!=$secret) { echo json_encode(array('success' => false)); die();} + $nResources = ItemResource::newInstance()->countResources($id); + $result = array('success' => ($nResources $nResources); + echo json_encode($result); + break; + case 'delete_ajax_upload': + $files = Session::newInstance()->_get('ajax_files'); + $success = false; + $filename = ''; + if(isset($files[Params::getParam('qquuid')]) && $files[Params::getParam('qquuid')]!='') { + $filename = $files[Params::getParam('qquuid')]; + unset($files[Params::getParam('qquuid')]); + Session::newInstance()->_set('ajax_files', $files); + $success = @unlink(osc_content_path().'uploads/temp/'.$filename); + }; + echo json_encode(array('success' => $success, 'uploadName' => $filename)); + break; default: echo json_encode(array('error' => __('no action defined'))); break; diff --git a/oc-includes/osclass/cron.php b/oc-includes/osclass/cron.php index 1f520c762f..9e0b8a72ad 100755 --- a/oc-includes/osclass/cron.php +++ b/oc-includes/osclass/cron.php @@ -52,6 +52,13 @@ } } + $files = glob(osc_content_path().'uploads/temp/qqfile_*'); + foreach($files as $file) { + if((time()-filectime($file))>(2*3600)) { + @unlink($file); + } + } + osc_run_hook('cron_hourly'); } } diff --git a/oc-includes/osclass/frm/Item.form.class.php b/oc-includes/osclass/frm/Item.form.class.php index 75835f1e35..0019f81516 100755 --- a/oc-includes/osclass/frm/Item.form.class.php +++ b/oc-includes/osclass/frm/Item.form.class.php @@ -1279,63 +1279,235 @@ static public function plugin_edit_item() { } - static public function ajax_photos() { + static public function ajax_photos($resources = null) { + if($resources==null) { $resources = osc_get_item_resources(); }; $aImages = array(); - if( Session::newInstance()->_getForm('fu_pre_images') != '' ) { - $aImages = Session::newInstance()->_getForm('fu_pre_images'); - Session::newInstance()->_drop('fu_pre_images'); - Session::newInstance()->_dropKeepForm('fu_pre_images'); + if( Session::newInstance()->_getForm('photos') != '' ) { + $aImages = Session::newInstance()->_getForm('photos'); + $aImages = $aImages['name']; + Session::newInstance()->_drop('photos'); + Session::newInstance()->_dropKeepForm('photos'); } + ?>
- 0) { ?> -
+ 0 || ($resources!=null && is_array($resources) && count($resources)>0)) { ?>

    + +
  • + + +
    <?php echo osc_esc_html($img); ?>
    +
  • +
  • - Delete -
    <?php echo osc_esc_html($img); ?>
    - + +
    <?php echo osc_esc_html($img); ?>
    +
+ $value) { + $aExt[$key] = "'".$value."'"; + } + + $allowedExtensions = join(',', $aExt); + $maxSize = (int) osc_max_size_kb()*1024; + $maxImages = (int) osc_max_images_per_item(); + ?> +