diff --git a/apps/contacts/ajax/importaddressbook.php b/apps/contacts/ajax/importaddressbook.php new file mode 100644 index 0000000000..5776c801a7 --- /dev/null +++ b/apps/contacts/ajax/importaddressbook.php @@ -0,0 +1,24 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +require_once('../../../lib/base.php'); +OC_JSON::checkLoggedIn(); +OC_Util::checkAppEnabled('contacts'); +$upload_max_filesize = OC_Helper::computerFileSize(ini_get('upload_max_filesize')); +$post_max_size = OC_Helper::computerFileSize(ini_get('post_max_size')); +$maxUploadFilesize = min($upload_max_filesize, $post_max_size); + +$freeSpace=OC_Filesystem::free_space('/'); +$freeSpace=max($freeSpace,0); +$maxUploadFilesize = min($maxUploadFilesize ,$freeSpace); + +$tmpl = new OC_Template('contacts', 'part.importaddressbook'); +$tmpl->assign('uploadMaxFilesize', $maxUploadFilesize); +$tmpl->assign('uploadMaxHumanFilesize', OC_Helper::humanFileSize($maxUploadFilesize)); +$tmpl->printpage(); +?> diff --git a/apps/contacts/ajax/uploadimport.php b/apps/contacts/ajax/uploadimport.php new file mode 100644 index 0000000000..ab680c8823 --- /dev/null +++ b/apps/contacts/ajax/uploadimport.php @@ -0,0 +1,50 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ +// Init owncloud +require_once('../../../lib/base.php'); + +// Check if we are a user +OC_JSON::checkLoggedIn(); +OC_JSON::checkAppEnabled('contacts'); +function bailOut($msg) { + OC_JSON::error(array('data' => array('message' => $msg))); + OC_Log::write('contacts','ajax/uploadimport.php: '.$msg, OC_Log::ERROR); + exit(); +} +function debug($msg) { + OC_Log::write('contacts','ajax/uploadimport.php: '.$msg, OC_Log::DEBUG); +} + +// 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))); + } else { + bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); + } +} + +?> diff --git a/apps/contacts/css/contacts.css b/apps/contacts/css/contacts.css index b0c68c96e6..7338eed4a7 100644 --- a/apps/contacts/css/contacts.css +++ b/apps/contacts/css/contacts.css @@ -33,7 +33,8 @@ dl.form { width: 100%; float: left; clear: right; margin: 0; padding: 0; } .form dd { display: table-cell; clear: right; float: left; margin: 0; padding: 0px; white-space: nowrap; vertical-align: text-bottom; } #address.form dt { min-width: 5em; } #address.form dl { min-width: 10em; } - +.droptarget { margin: 0.5em; padding: 0.5em; border: thin solid #ccc; -moz-border-radius:.3em; -webkit-border-radius:.3em; border-radius:.3em; } +.droppable { margin: 0.5em; padding: 0.5em; border: thin dashed #333; -moz-border-radius:.3em; -webkit-border-radius:.3em; border-radius:.3em; } .loading { background: url('../../../core/img/loading.gif') no-repeat center !important; /*cursor: progress; */ cursor: wait; } .ui-autocomplete-loading { background: url('../../../core/img/loading.gif') right center no-repeat; } .float { float: left; } @@ -59,6 +60,7 @@ dl.form { width: 100%; float: left; clear: right; margin: 0; padding: 0; } #identityprops { /*position: absolute; top: 2.5em; left: 0px;*/ } /*#contact_photo { max-width: 250px; }*/ #contact_identity { min-width: 30em; } +#note { min-width: 200px; } .contactsection { position: relative; float: left; /*max-width: 40em;*/ padding: 0.5em; height: auto: border: thin solid lightgray;/* -webkit-border-radius: 0.5em; -moz-border-radius: 0.5em; border-radius: 0.5em; background-color: #f8f8f8;*/ } .contactpart legend { width:auto; padding:.3em; border:1px solid #ddd; font-weight:bold; cursor:pointer; background:#f8f8f8; color:#555; text-shadow:#fff 0 1px 0; -moz-box-shadow:0 1px 1px #fff, 0 1px 1px #fff inset; -webkit-box-shadow:0 1px 1px #fff, 0 1px 1px #fff inset; -moz-border-radius:.5em; -webkit-border-radius:.5em; border-radius:.5em; } diff --git a/apps/contacts/import.php b/apps/contacts/import.php index 233ed5be22..93af7e6281 100644 --- a/apps/contacts/import.php +++ b/apps/contacts/import.php @@ -17,8 +17,14 @@ if(is_writable('import_tmp/')){ fwrite($progressfopen, '10'); fclose($progressfopen); } -$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); -if($_POST['method'] == 'new'){ +$view = $file = null; +if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { + $view = OC_App::getStorage('contacts'); + $file = $view->file_get_contents('/' . $_POST['file']); +} else { + $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); +} +if(isset($_POST['method']) && $_POST['method'] == 'new'){ $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']); OC_Contacts_Addressbook::setActive($id, 1); }else{ @@ -99,12 +105,16 @@ if(is_writable('import_tmp/')){ if(count($parts) == 1){ $importready = array($file); } +$imported = 0; +$failed = 0; foreach($importready as $import){ $card = OC_VObject::parse($import); if (!$card) { + $failed += 1; OC_Log::write('contacts','Import: skipping card. Error parsing VCard: '.$import, OC_Log::ERROR); continue; // Ditch cards that can't be parsed by Sabre. } + $imported += 1; OC_Contacts_VCard::add($id, $card); } //done the import @@ -117,4 +127,9 @@ sleep(3); if(is_writable('import_tmp/')){ unlink($progressfile); } -OC_JSON::success(); +if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { + if(!$view->unlink('/' . $_POST['file'])) { + OC_Log::write('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OC_Log::ERROR); + } +} +OC_JSON::success(array('data' => array('imported'=>$imported, 'failed'=>$failed))); diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js index f03ae1e46d..e00e9257e6 100644 --- a/apps/contacts/js/contacts.js +++ b/apps/contacts/js/contacts.js @@ -1263,7 +1263,8 @@ Contacts={ for(ptype in this.data.TEL[phone]['parameters'][param]) { var pt = this.data.TEL[phone]['parameters'][param][ptype]; $('#phonelist li:last-child').find('select option').each(function(){ - if ($(this).val().toUpperCase() == pt.toUpperCase()) { + //if ($(this).val().toUpperCase() == pt.toUpperCase()) { + if ($.inArray($(this).val().toUpperCase(), pt.toUpperCase().split(',')) > -1) { $(this).attr('selected', 'selected'); } }); @@ -1285,6 +1286,7 @@ Contacts={ }, }, Addressbooks:{ + droptarget:undefined, overview:function(){ if($('#chooseaddressbook_dialog').dialog('isOpen') == true){ $('#chooseaddressbook_dialog').dialog('moveToTop'); @@ -1317,14 +1319,13 @@ Contacts={ var tr = $(document.createElement('tr')) .load(OC.filePath('contacts', 'ajax', 'addbook.php')); $(object).closest('tr').after(tr).hide(); - /* TODO: Shouldn't there be some kinda error checking here? */ }, editAddressbook:function(object, bookid){ var tr = $(document.createElement('tr')) .load(OC.filePath('contacts', 'ajax', 'editaddressbook.php') + "?bookid="+bookid); $(object).closest('tr').after(tr).hide(); }, - deleteAddressbook:function(bookid){ + deleteAddressbook:function(obj, bookid){ var check = confirm("Do you really want to delete this address book?"); if(check == false){ return false; @@ -1332,9 +1333,10 @@ Contacts={ $.post(OC.filePath('contacts', 'ajax', 'deletebook.php'), { id: bookid}, function(jsondata) { if (jsondata.status == 'success'){ - $('#chooseaddressbook_dialog').dialog('destroy').remove(); + $(obj).closest('tr').remove(); + //$('#chooseaddressbook_dialog').dialog('destroy').remove(); Contacts.UI.Contacts.update(); - Contacts.UI.Addressbooks.overview(); + //Contacts.UI.Addressbooks.overview(); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); //alert('Error: ' + data.message); @@ -1342,8 +1344,95 @@ Contacts={ }); } }, - doImport:function(){ - Contacts.UI.notImplemented(); + loadImportHandlers:function() { + this.droptarget = $('#import_drop_target'); + console.log($('#import_drop_target').html()); + $(this.droptarget).bind('dragover',function(event){ + $(event.target).addClass('droppable'); + event.stopPropagation(); + event.preventDefault(); + }); + $(this.droptarget).bind('dragleave',function(event){ + $(event.target).removeClass('droppable'); + }); + $(this.droptarget).bind('drop',function(event){ + event.stopPropagation(); + event.preventDefault(); + console.log('drop'); + $(event.target).removeClass('droppable'); + $(event.target).html(t('contacts', 'Uploading...')); + Contacts.UI.loading(event.target, true); + $.fileUpload(event.originalEvent.dataTransfer.files); + }); + + $.fileUpload = function(files){ + console.log(files + ', ' + files.length); + var file = files[0]; + 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.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.loading(Contacts.UI.Addressbooks.droptarget, false); + return; + } + var xhr = new XMLHttpRequest(); + + if (!xhr.upload) { + OC.dialogs.alert(t('contacts', 'Your browser doesn\'t support AJAX upload. Please upload the contacts file to ownCloud and import that way.'), t('contacts', 'Error')) + } + fileUpload = xhr.upload, + xhr.onreadystatechange = function() { + if (xhr.readyState == 4){ + 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.loading(Contacts.UI.Addressbooks.droptarget, false); + OC.dialogs.alert(xhr.status + ': ' + xhr.responseText, t('contacts', 'Error')); + } + } else { + OC.dialogs.alert(response.data.message, t('contacts', 'Error')); + } + } + }; + xhr.open("POST", '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)); + xhr.setRequestHeader('X-File-Size', file.size); + xhr.setRequestHeader('Content-Type', file.type); + xhr.send(file); + } + }, + 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){ + 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' }, + function(jsondata){ + if(jsondata.status == 'success'){ + 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(); + } else { + OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); + } + }); + Contacts.UI.loading(Contacts.UI.Addressbooks.droptarget, false); }, submit:function(button, bookid){ var displayname = $("#displayname_"+bookid).val().trim(); @@ -1368,7 +1457,7 @@ Contacts={ } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } - }); + }); }, cancel:function(button, bookid){ $(button).closest('tr').prev().show().next().remove(); @@ -1385,7 +1474,6 @@ Contacts={ } else{ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); - //alert(jsondata.data.message); } }); setTimeout(Contacts.UI.Contacts.lazyupdate, 500); @@ -1507,13 +1595,13 @@ $(document).ready(function(){ }); $('#contacts_details_photo_wrapper').bind('dragover',function(event){ console.log('dragover'); - $(event.target).css('background-color','red'); + $(event.target).addClass('droppable'); event.stopPropagation(); event.preventDefault(); }); $('#contacts_details_photo_wrapper').bind('dragleave',function(event){ console.log('dragleave'); - $(event.target).css('background-color','white'); + $(event.target).removeClass('droppable'); //event.stopPropagation(); //event.preventDefault(); }); @@ -1521,7 +1609,7 @@ $(document).ready(function(){ event.stopPropagation(); event.preventDefault(); console.log('drop'); - $(event.target).css('background-color','white') + $(event.target).removeClass('droppable'); $.fileUpload(event.originalEvent.dataTransfer.files); }); diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php index 475d2c8dc2..2c2cc331ed 100644 --- a/apps/contacts/lib/app.php +++ b/apps/contacts/lib/app.php @@ -149,6 +149,7 @@ class OC_Contacts_App { 'WORK' => $l->t('Work'), 'TEXT' => $l->t('Text'), 'VOICE' => $l->t('Voice'), + 'MSG' => $l->t('Message'), 'FAX' => $l->t('Fax'), 'VIDEO' => $l->t('Video'), 'PAGER' => $l->t('Pager'), diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php index 90b037f76e..96fc8cf712 100644 --- a/apps/contacts/lib/vcard.php +++ b/apps/contacts/lib/vcard.php @@ -141,10 +141,38 @@ class OC_Contacts_VCard{ } /** - * @brief Tries to update imported VCards to adhere to rfc2426 (VERSION: 3.0) + * @brief Checks if a contact with the same UID already exist in the address book. + * @param $aid Address book ID. + * @param $uid UID (passed by reference). + * @returns true if the UID has been changed. + */ + protected static function trueUID($aid, &$uid) { + $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri = ?' ); + $uri = $uid.'.vcf'; + $result = $stmt->execute(array($aid,$uri)); + if($result->numRows() > 0){ + while(true) { + $tmpuid = substr(md5(rand().time()),0,10); + $uri = $tmpuid.'.vcf'; + $result = $stmt->execute(array($aid,$uri)); + if($result->numRows() > 0){ + continue; + } else { + $uid = $tmpuid; + return true; + } + } + } else { + return false; + } + } + + /** + * @brief Tries to update imported VCards to adhere to rfc2426 (VERSION: 3.0) and add mandatory fields if missing. + * @param aid Address book id. * @param vcard An OC_VObject of type VCARD (passed by reference). */ - protected static function updateValuesFromAdd(&$vcard) { // any suggestions for a better method name? ;-) + protected static function updateValuesFromAdd($aid, &$vcard) { // any suggestions for a better method name? ;-) $stringprops = array('N', 'FN', 'ORG', 'NICK', 'ADR', 'NOTE'); $typeprops = array('ADR', 'TEL', 'EMAIL'); $upgrade = false; @@ -207,14 +235,19 @@ class OC_Contacts_VCard{ } if(!$uid) { $vcard->setUID(); + $uid = $vcard->getAsString('UID'); OC_Log::write('contacts','OC_Contacts_VCard::updateValuesFromAdd. Added missing \'UID\' field: '.$uid,OC_Log::DEBUG); } + if(self::trueUID($aid, $uid)) { + $vcard->setString('UID', $uid); + } $vcard->setString('VERSION','3.0'); // Add product ID is missing. $prodid = trim($vcard->getAsString('PRODID')); if(!$prodid) { $appinfo = OC_App::getAppInfo('contacts'); - $prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appinfo['version'].'//EN'; + $appversion = OC_App::getAppVersion('contacts'); + $prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN'; $vcard->setString('PRODID', $prodid); } $now = new DateTime; @@ -236,7 +269,7 @@ class OC_Contacts_VCard{ OC_Contacts_App::loadCategoriesFromVCard($card); - self::updateValuesFromAdd($card); + self::updateValuesFromAdd($aid, $card); $fn = $card->getAsString('FN'); if (empty($fn)) { diff --git a/apps/contacts/templates/part.chooseaddressbook.php b/apps/contacts/templates/part.chooseaddressbook.php index 90894220ef..adfc8c1516 100644 --- a/apps/contacts/templates/part.chooseaddressbook.php +++ b/apps/contacts/templates/part.chooseaddressbook.php @@ -1,4 +1,4 @@ -
"> +
"> diff --git a/apps/contacts/templates/part.chooseaddressbook.rowfields.php b/apps/contacts/templates/part.chooseaddressbook.rowfields.php index 8518c1acd1..20b67a4161 100644 --- a/apps/contacts/templates/part.chooseaddressbook.rowfields.php +++ b/apps/contacts/templates/part.chooseaddressbook.rowfields.php @@ -2,4 +2,4 @@ // FIXME: Make this readable. echo ""; echo ""; - echo ""; + echo ""; diff --git a/apps/contacts/templates/part.contact.php b/apps/contacts/templates/part.contact.php index b90fa92c2c..dec081a9b8 100644 --- a/apps/contacts/templates/part.contact.php +++ b/apps/contacts/templates/part.contact.php @@ -112,7 +112,7 @@ $id = isset($_['id']) ? $_['id'] : '';
- +
diff --git a/lib/app.php b/lib/app.php index 9f7d23872f..2965377525 100755 --- a/lib/app.php +++ b/lib/app.php @@ -519,6 +519,10 @@ class OC_App{ public static function getStorage($appid){ if(OC_App::isEnabled($appid)){//sanity check if(OC_User::isLoggedIn()){ + $view = new OC_FilesystemView('/'.OC_User::getUser()); + if(!$view->file_exists($appid)) { + $view->mkdir($appid); + } return new OC_FilesystemView('/'.OC_User::getUser().'/'.$appid); }else{ OC_Log::write('core','Can\'t get app storage, app, user not logged in',OC_Log::ERROR);
t('New Address Book') ?> + t('Import from VCF') ?>
t("CardDav Link") . "\" class=\"action\">t("Download") . "\" class=\"action\">t("Edit") . "\" class=\"action\" onclick=\"Contacts.UI.Addressbooks.editAddressbook(this, " . $_['addressbook']["id"] . ");\">t("Delete") . "\" class=\"action\">t("CardDav Link") . "\" class=\"action\">t("Download") . "\" class=\"action\">t("Edit") . "\" class=\"action\" onclick=\"Contacts.UI.Addressbooks.editAddressbook(this, " . $_['addressbook']["id"] . ");\">t("Delete") . "\" class=\"action\">