function import() { switch ($this->appinfo->version) { default: // All versions of the app have had the same db structure, so all can use the same import function $query = $this->content->prepare('SELECT * FROM `contacts_addressbooks` WHERE `userid` LIKE ?'); $results = $query->execute(array($this->olduid)); $idmap = array(); while ($row = $results->fetchRow()) { // Import each addressbook $addressbookquery = OCP\DB::prepare('INSERT INTO `*PREFIX*contacts_addressbooks` (`userid`, `displayname`, `uri`, `description`, `ctag`) VALUES (?, ?, ?, ?, ?)'); $addressbookquery->execute(array($this->uid, $row['displayname'], $row['uri'], $row['description'], $row['ctag'])); // Map the id $idmap[$row['id']] = OCP\DB::insertid('*PREFIX*contacts_addressbooks'); // Make the addressbook active OCA\Contacts\Addressbook::setActive($idmap[$row['id']], true); } // Now tags foreach ($idmap as $oldid => $newid) { $query = $this->content->prepare('SELECT * FROM `contacts_cards` WHERE `addressbookid` LIKE ?'); $results = $query->execute(array($oldid)); while ($row = $results->fetchRow()) { // Import the contacts $contactquery = OCP\DB::prepare('INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`, `fullname`, `carddata`, `uri`, `lastmodified`) VALUES (?, ?, ?, ?, ?)'); $contactquery->execute(array($newid, $row['fullname'], $row['carddata'], $row['uri'], $row['lastmodified'])); } } // All done! break; } return true; }
function import() { $existingURIs = array(); $existingNames = array(); $query = $this->content->prepare('SELECT `displayname`, `uri` FROM *PREFIX*contacts_addressbooks WHERE userid = ?'); $results = $query->execute(array($this->uid)); while ($row = $results->fetchRow()) { \OCP\Util::writeLog('contacts', __METHOD__ . ', row: ' . print_r($row, true), \OCP\Util::DEBUG); $existingURIs[] = $row['uri']; $existingNames[] = $row['displayname']; } switch ($this->appinfo->version) { default: // All versions of the app have had the same db structure, so all can use the same import function $query = $this->content->prepare('SELECT * FROM contacts_addressbooks WHERE userid = ?'); $results = $query->execute(array($this->olduid)); $idmap = array(); while ($row = $results->fetchRow()) { // Import each addressbook $addressbookquery = OCP\DB::prepare('INSERT INTO `*PREFIX*contacts_addressbooks` (`userid`, `displayname`, `uri`, `description`, `ctag`) VALUES (?, ?, ?, ?, ?)'); $uriSuffix = ''; $nameSuffix = ''; while (in_array($row['uri'] . $uriSuffix, $existingURIs)) { $uriSuffix++; } while (in_array($row['displayname'] . $nameSuffix, $existingNames)) { $nameSuffix++; } $addressbookquery->execute(array($this->uid, $row['displayname'] . $nameSuffix, $row['uri'] . $uriSuffix, $row['description'], $row['ctag'])); // Map the id $idmap[$row['id']] = OCP\DB::insertid('*PREFIX*contacts_addressbooks'); // Make the addressbook active OCA\Contacts\Addressbook::setActive($idmap[$row['id']], true); } // Now tags foreach ($idmap as $oldid => $newid) { $query = $this->content->prepare('SELECT * FROM contacts_cards WHERE addressbookid = ?'); $results = $query->execute(array($oldid)); while ($row = $results->fetchRow()) { // Import the contacts $contactquery = OCP\DB::prepare('INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`, `fullname`, `carddata`, `uri`, `lastmodified`) VALUES (?, ?, ?, ?, ?)'); $contactquery->execute(array($newid, $row['fullname'], $row['carddata'], $row['uri'], $row['lastmodified'])); } } // All done! break; } return true; }
<?php /** * Copyright (c) 2011 Thomas Tanghus <*****@*****.**> * Copyright (c) 2011 Bart Visscher <*****@*****.**> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ OCP\JSON::checkLoggedIn(); OCP\JSON::checkAppEnabled('contacts'); OCP\JSON::callCheck(); $id = $_POST['id']; try { $book = OCA\Contacts\Addressbook::find($id); // is owner access check } catch (Exception $e) { OCP\JSON::error(array('data' => array('message' => $e->getMessage(), 'file' => $_POST['file']))); exit; } if (!OCA\Contacts\Addressbook::setActive($id, $_POST['active'])) { OCP\Util::writeLog('contacts', 'ajax/activation.php: Error activating addressbook: ' . $id, OCP\Util::ERROR); OCP\JSON::error(array('data' => array('message' => OCA\Contacts\App::$l10n->t('Error (de)activating addressbook.')))); exit; } OCP\JSON::success(array('active' => OCA\Contacts\Addressbook::isActive($id), 'id' => $id, 'addressbook' => $book));
$view = OCP\Files::getStorage('contacts'); $file = $view->file_get_contents('/imports/' . $_POST['file']); } else { $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); } if (!$file) { OCP\JSON::error(array('data' => array('message' => 'Import file was empty.'))); exit; } if (isset($_POST['method']) && $_POST['method'] == 'new') { $id = OCA\Contacts\Addressbook::add(OCP\USER::getUser(), $_POST['addressbookname']); if (!$id) { OCP\JSON::error(array('data' => array('message' => 'Error creating address book.'))); exit; } OCA\Contacts\Addressbook::setActive($id, 1); } else { $id = $_POST['id']; if (!$id) { OCP\JSON::error(array('data' => array('message' => 'Error getting the ID of the address book.', 'file' => $_POST['file']))); exit; } try { OCA\Contacts\Addressbook::find($id); // is owner access check } catch (Exception $e) { OCP\JSON::error(array('data' => array('message' => $e->getMessage(), 'file' => $_POST['file']))); exit; } } //analyse the contacts file
<?php /** * Copyright (c) 2011-2012 Thomas Tanghus <*****@*****.**> * Copyright (c) 2011 Bart Visscher <*****@*****.**> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ // Check if we are a user OCP\JSON::checkLoggedIn(); OCP\JSON::checkAppEnabled('contacts'); OCP\JSON::callCheck(); require_once __DIR__ . '/../loghandler.php'; debug('name: ' . $_POST['name']); $userid = OCP\USER::getUser(); $name = isset($_POST['name']) ? trim(strip_tags($_POST['name'])) : null; $description = isset($_POST['description']) ? trim(strip_tags($_POST['description'])) : null; if (is_null($name)) { bailOut('Cannot add addressbook with an empty name.'); } $bookid = OCA\Contacts\Addressbook::add($userid, $name, $description); if (!$bookid) { bailOut('Error adding addressbook: ' . $name); } if (!OCA\Contacts\Addressbook::setActive($bookid, 1)) { bailOut('Error activating addressbook.'); } $addressbook = OCA\Contacts\Addressbook::find($bookid); OCP\JSON::success(array('data' => array('addressbook' => $addressbook)));