diff --git a/ajax/uploadimport.php b/ajax/uploadimport.php index ab680c88..f44335a9 100644 --- a/ajax/uploadimport.php +++ b/ajax/uploadimport.php @@ -34,17 +34,52 @@ function debug($msg) { OC_Log::write('contacts','ajax/uploadimport.php: '.$msg, OC_Log::DEBUG); } +$view = OC_App::getStorage('contacts'); +$tmpfile = md5(rand()); + // If it is a Drag'n'Drop transfer it's handled here. $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false); if($fn) { - $view = OC_App::getStorage('contacts'); - $tmpfile = md5(rand()); if($view->file_put_contents('/'.$tmpfile, file_get_contents('php://input'))) { debug($fn.' uploaded'); OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile))); + exit(); } else { bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); } } +// File input transfers are handled here +if (!isset($_FILES['importfile'])) { + OC_Log::write('contacts','ajax/uploadphoto.php: No file was uploaded. Unknown error.', OC_Log::DEBUG); + OC_JSON::error(array('data' => array( 'message' => 'No file was uploaded. Unknown error' ))); + exit(); +} +$error = $_FILES['importfile']['error']; +if($error !== UPLOAD_ERR_OK) { + $errors = array( + 0=>OC_Contacts_App::$l10n->t("There is no error, the file uploaded with success"), + 1=>OC_Contacts_App::$l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini").ini_get('upload_max_filesize'), + 2=>OC_Contacts_App::$l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"), + 3=>OC_Contacts_App::$l10n->t("The uploaded file was only partially uploaded"), + 4=>OC_Contacts_App::$l10n->t("No file was uploaded"), + 6=>OC_Contacts_App::$l10n->t("Missing a temporary folder") + ); + bailOut($errors[$error]); +} +$file=$_FILES['importfile']; + +$tmpfname = tempnam("/tmp", "occOrig"); +if(file_exists($file['tmp_name'])) { + if($view->file_put_contents('/'.$tmpfile, file_get_contents($file['tmp_name']))) { + debug($fn.' uploaded'); + OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile))); + } else { + bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); + } +} else { + bailOut('Temporary file: \''.$file['tmp_name'].'\' has gone AWOL?'); +} + + ?> diff --git a/css/contacts.css b/css/contacts.css index 0bb9f975..e3d2cfbd 100644 --- a/css/contacts.css +++ b/css/contacts.css @@ -100,4 +100,4 @@ input[type="checkbox"] { width: 20px; height: 20px; vertical-align: bottom; } .propertylist li > input[type="checkbox"],input[type="radio"] { float: left; clear: left; width: 20px; height: 20px; vertical-align: middle; } .propertylist li > select { float: left; max-width: 8em; } .typelist { float: left; max-width: 10em; } /* for multiselect */ -.addresslist { clear: both; } \ No newline at end of file +.addresslist { clear: both; } diff --git a/import.php b/import.php index ca2c1e16..8e0a4273 100644 --- a/import.php +++ b/import.php @@ -24,6 +24,11 @@ if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { } else { $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); } +if(!$file) { + OC_JSON::error(array('message' => 'Import file was empty.')); + exit(); +} +error_log('File: '.$file); if(isset($_POST['method']) && $_POST['method'] == 'new'){ $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']); OC_Contacts_Addressbook::setActive($id, 1); diff --git a/js/contacts.js b/js/contacts.js index 18edb40a..7e0fe8b4 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -1287,6 +1287,7 @@ Contacts={ }, Addressbooks:{ droptarget:undefined, + droptext:t('contacts', 'Drop a VCF file to import contacts.'), overview:function(){ if($('#chooseaddressbook_dialog').dialog('isOpen') == true){ $('#chooseaddressbook_dialog').dialog('moveToTop'); @@ -1345,8 +1346,16 @@ Contacts={ } }, loadImportHandlers:function() { + $('#import_upload_start').change(function(){ + Contacts.UI.Addressbooks.uploadImport(this.files); + }); + $('#importaddressbook_dialog').find('.upload').click(function() { + Contacts.UI.Addressbooks.droptarget.html(t('contacts', 'Uploading...')); + Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, true); + $('#import_upload_start').trigger('click'); + }); + $('#importaddressbook_dialog').find('.upload').tipsy(); this.droptarget = $('#import_drop_target'); - console.log($('#import_drop_target').html()); $(this.droptarget).bind('dragover',function(event){ $(event.target).addClass('droppable'); event.stopPropagation(); @@ -1371,13 +1380,13 @@ Contacts={ console.log('size: '+file.size+', type: '+file.type); if(file.size > $('#max_upload').val()){ OC.dialogs.alert(t('contacts','The file you are trying to upload exceed the maximum size for file uploads on this server.'), t('contacts','Upload too large')); - $(Contacts.UI.Addressbooks.droptarget).html(t('contacts', 'Drop a VCF file to import contacts.')); + $(Contacts.UI.Addressbooks.droptarget).html(Contacts.UI.Addressbooks.droptext); Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, false); return; } if(file.type.indexOf('text') != 0) { OC.dialogs.alert(t('contacts','You have dropped a file type that cannot be imported: ') + file.type, t('contacts','Wrong file type')); - $(Contacts.UI.Addressbooks.droptarget).html(t('contacts', 'Drop a VCF file to import contacts.')); + $(Contacts.UI.Addressbooks.droptarget).html(Contacts.UI.Addressbooks.droptext); Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, false); return; } @@ -1392,11 +1401,9 @@ Contacts={ response = $.parseJSON(xhr.responseText); if(response.status == 'success') { if(xhr.status == 200) { - $(Contacts.UI.Addressbooks.droptarget).html(t('contacts', 'Importing...')); - Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, true); Contacts.UI.Addressbooks.doImport(response.data.path, response.data.file); } else { - $(Contacts.UI.Addressbooks.droptarget).html(t('contacts', 'Drop a VCF file to import contacts.')); + $(Contacts.UI.Addressbooks.droptarget).html(Contacts.UI.Addressbooks.droptext); Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, false); OC.dialogs.alert(xhr.status + ': ' + xhr.responseText, t('contacts', 'Error')); } @@ -1405,7 +1412,7 @@ Contacts={ } } }; - xhr.open("POST", 'ajax/uploadimport.php?file='+encodeURIComponent(file.name), true); + xhr.open('POST', OC.filePath('contacts', 'ajax', 'uploadimport.php') + '?file='+encodeURIComponent(file.name), true); xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('X_FILE_NAME', encodeURIComponent(file.name)); @@ -1414,12 +1421,39 @@ Contacts={ xhr.send(file); } }, + uploadImport:function(filelist) { + if(!filelist) { + OC.dialogs.alert(t('contacts','No files selected for upload.'), t('contacts', 'Error')); + return; + } + //var file = filelist.item(0); + var file = filelist[0]; + var target = $('#import_upload_target'); + var form = $('#import_upload_form'); + var totalSize=0; + if(file.size > $('#max_upload').val()){ + OC.dialogs.alert(t('contacts','The file you are trying to upload exceed the maximum size for file uploads on this server.'), t('contacts', 'Error')); + return; + } else { + target.load(function(){ + var response=jQuery.parseJSON(target.contents().text()); + if(response != undefined && response.status == 'success'){ + Contacts.UI.Addressbooks.doImport(response.data.path, response.data.file); + }else{ + OC.dialogs.alert(response.data.message, t('contacts', 'Error')); + } + }); + form.submit(); + } + }, importAddressbook:function(object){ var tr = $(document.createElement('tr')) .load(OC.filePath('contacts', 'ajax', 'importaddressbook.php')); $(object).closest('tr').after(tr).hide(); }, doImport:function(path, file){ + $(Contacts.UI.Addressbooks.droptarget).html(t('contacts', 'Importing...')); + Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, true); var id = $('#importaddressbook_dialog').find('#book').val(); console.log('Selected book: ' + id); $.post(OC.filePath('contacts', '', 'import.php'), { id: id, path: path, file: file, fstype: 'OC_FilesystemView' }, @@ -1428,6 +1462,10 @@ Contacts={ Contacts.UI.Addressbooks.droptarget.html(t('contacts', 'Import done. Success/Failure: ')+jsondata.data.imported+'/'+jsondata.data.failed); $('#chooseaddressbook_dialog').find('#close_button').val(t('contacts', 'OK')); Contacts.UI.Contacts.update(); + setTimeout( + function() { + $(Contacts.UI.Addressbooks.droptarget).html(Contacts.UI.Addressbooks.droptext); + }, 5000); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } @@ -1590,7 +1628,7 @@ $(document).ready(function(){ * Profile picture upload handling */ // New profile picture selected - $('#file_upload_start').live('change',function(){ + $('#file_upload_start').change(function(){ Contacts.UI.Card.uploadPhoto(this.files); }); $('#contacts_details_photo_wrapper').bind('dragover',function(event){ @@ -1662,7 +1700,7 @@ $(document).ready(function(){ }; // Start loading indicator. //$('#contacts_details_photo_progress').show()(); - xhr.open("POST", OC.filePath('contacts', 'ajax', 'uploadphoto.php')+'?id='+Contacts.UI.Card.id+'&imagefile='+encodeURIComponent(file.name), true); + xhr.open('POST', OC.filePath('contacts', 'ajax', 'uploadphoto.php')+'?id='+Contacts.UI.Card.id+'&imagefile='+encodeURIComponent(file.name), true); xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('X_FILE_NAME', encodeURIComponent(file.name)); @@ -1691,4 +1729,4 @@ $(document).ready(function(){ Contacts.UI.Card.addProperty(type); $('#contacts_propertymenu').hide(); }); -}); \ No newline at end of file +}); diff --git a/templates/part.contact.php b/templates/part.contact.php index dec081a9..64a024c0 100644 --- a/templates/part.contact.php +++ b/templates/part.contact.php @@ -24,7 +24,6 @@ $id = isset($_['id']) ? $_['id'] : '';