1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-30 12:24:11 +01:00
OwncloudContactsOfficial/lib/addressbook.php

328 lines
9.7 KiB
PHP
Raw Normal View History

2011-08-06 22:32:06 +02:00
<?php
/**
* ownCloud - Addressbook
*
* @author Jakob Sack
* @copyright 2011 Jakob Sack mail@jakobsack.de
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
/*
*
* The following SQL statement is just a help for developers and will not be
* executed!
*
* CREATE TABLE contacts_addressbooks (
* id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
* userid VARCHAR(255) NOT NULL,
* displayname VARCHAR(255),
* uri VARCHAR(100),
* description TEXT,
* ctag INT(11) UNSIGNED NOT NULL DEFAULT '1'
* );
2011-09-15 11:06:21 +02:00
*
2011-08-06 22:32:06 +02:00
*/
/**
* This class manages our addressbooks.
*/
2012-07-20 00:17:59 +02:00
class OC_Contacts_Addressbook {
2011-09-08 23:36:47 +02:00
/**
* @brief Returns the list of addressbooks for a specific user.
* @param string $uid
2012-07-05 17:12:52 +02:00
* @param boolean $active Only return addressbooks with this $active state, default(=false) is don't care
* @return array or false.
2011-09-08 23:36:47 +02:00
*/
2012-07-20 00:17:59 +02:00
public static function all($uid, $active=false) {
2012-07-04 22:12:56 +02:00
$values = array($uid);
$active_where = '';
2012-07-20 00:17:59 +02:00
if ($active) {
2012-07-04 22:12:56 +02:00
$active_where = ' AND active = ?';
$values[] = 1;
}
try {
2012-07-04 22:12:56 +02:00
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ' . $active_where . ' ORDER BY displayname' );
$result = $stmt->execute($values);
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' uid: '.$uid, OCP\Util::DEBUG);
return false;
}
2011-08-06 22:32:06 +02:00
$addressbooks = array();
2012-07-20 00:17:59 +02:00
while( $row = $result->fetchRow()) {
2011-08-06 22:32:06 +02:00
$addressbooks[] = $row;
}
2012-07-04 22:12:56 +02:00
if(!$active && !count($addressbooks)) {
self::addDefault($uid);
}
2011-08-06 22:32:06 +02:00
return $addressbooks;
}
2011-09-15 11:06:21 +02:00
2012-07-04 22:12:56 +02:00
/**
* @brief Get active addressbook IDs for a user.
* @param integer $uid User id. If null current user will be used.
* @return array
*/
2012-07-20 00:17:59 +02:00
public static function activeIds($uid = null) {
if(is_null($uid)) {
2012-07-04 22:12:56 +02:00
$uid = OCP\USER::getUser();
}
$activeaddressbooks = self::all($uid, true);
$ids = array();
foreach($activeaddressbooks as $addressbook) {
2012-07-09 19:10:34 +02:00
$ids[] = $addressbook['id'];
2012-07-04 22:12:56 +02:00
}
return $ids;
}
/**
* @brief Returns the list of active addressbooks for a specific user.
* @param string $uid
* @return array
*/
2012-07-20 00:17:59 +02:00
public static function active($uid) {
2012-07-04 22:12:56 +02:00
return self::all($uid, true);
}
2011-09-08 23:36:47 +02:00
/**
* @brief Returns the list of addressbooks for a principal (DAV term of user)
* @param string $principaluri
* @return array
*/
public static function allWherePrincipalURIIs($principaluri){
2011-08-06 22:32:06 +02:00
$uid = self::extractUserID($principaluri);
2011-12-16 17:42:07 +01:00
return self::all($uid);
2011-08-06 22:32:06 +02:00
}
2011-09-08 23:36:47 +02:00
/**
* @brief Gets the data of one address book
* @param integer $id
* @return associative array or false.
2011-09-08 23:36:47 +02:00
*/
2012-07-20 00:17:59 +02:00
public static function find($id) {
try {
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
$result = $stmt->execute(array($id));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id, OCP\Util::DEBUG);
return false;
}
2011-08-06 22:32:06 +02:00
return $result->fetchRow();
}
/**
* @brief Adds default address book
* @return $id ID of the newly created addressbook or false on error.
*/
2012-07-20 00:17:59 +02:00
public static function addDefault($uid = null) {
if(is_null($uid)) {
$uid = OCP\USER::getUser();
}
2012-07-20 17:09:03 +02:00
$id = self::add($uid, 'Contacts', 'Default Address Book');
if($id !== false) {
self::setActive($id, true);
}
return $id;
}
2011-09-08 23:36:47 +02:00
/**
* @brief Creates a new address book
* @param string $userid
* @param string $name
* @param string $description
* @return insertid
*/
2012-07-20 00:17:59 +02:00
public static function add($uid,$name,$description='') {
2012-07-06 13:24:56 +02:00
try {
$stmt = OCP\DB::prepare( 'SELECT uri FROM *PREFIX*contacts_addressbooks WHERE userid = ? ' );
$result = $stmt->execute(array($uid));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.' uid: '.$uid, OCP\Util::DEBUG);
2012-07-06 13:24:56 +02:00
return false;
}
2011-08-06 22:32:06 +02:00
$uris = array();
2012-07-06 13:24:56 +02:00
while($row = $result->fetchRow()){
$uris[] = $row['uri'];
2011-08-06 22:32:06 +02:00
}
2011-08-09 14:15:11 +02:00
$uri = self::createURI($name, $uris );
try {
$stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($uid,$name,$uri,$description,1));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uid: '.$uid, OCP\Util::DEBUG);
return false;
}
2011-08-06 22:32:06 +02:00
2012-05-03 13:06:08 +02:00
return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
2011-08-06 22:32:06 +02:00
}
2011-09-08 23:36:47 +02:00
/**
* @brief Creates a new address book from the data sabredav provides
* @param string $principaluri
* @param string $uri
* @param string $name
* @param string $description
* @return insertid or false
2011-09-08 23:36:47 +02:00
*/
2012-07-20 00:17:59 +02:00
public static function addFromDAVData($principaluri,$uri,$name,$description) {
$uid = self::extractUserID($principaluri);
2011-09-15 11:06:21 +02:00
try {
$stmt = OCP\DB::prepare('INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)');
$result = $stmt->execute(array($uid,$name,$uri,$description,1));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uid: '.$uid, OCP\Util::DEBUG);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', uri: '.$uri, OCP\Util::DEBUG);
return false;
}
2011-08-06 22:32:06 +02:00
2012-05-03 13:06:08 +02:00
return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
2011-08-06 22:32:06 +02:00
}
2011-09-08 23:36:47 +02:00
/**
* @brief Edits an addressbook
* @param integer $id
* @param string $name
* @param string $description
* @return boolean
*/
2012-07-20 00:17:59 +02:00
public static function edit($id,$name,$description) {
2011-08-06 22:32:06 +02:00
// Need these ones for checking uri
$addressbook = self::find($id);
2012-07-20 00:17:59 +02:00
if(is_null($name)) {
2011-08-06 22:32:06 +02:00
$name = $addressbook['name'];
}
2012-07-20 00:17:59 +02:00
if(is_null($description)) {
2011-08-06 22:32:06 +02:00
$description = $addressbook['description'];
}
2011-09-15 11:06:21 +02:00
try {
$stmt = OCP\DB::prepare('UPDATE *PREFIX*contacts_addressbooks SET displayname=?,description=?, ctag=ctag+1 WHERE id=?');
$result = $stmt->execute(array($name,$description,$id));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id, OCP\Util::DEBUG);
return false;
}
2011-08-06 22:32:06 +02:00
return true;
}
/**
* @brief Activates an addressbook
* @param integer $id
2012-06-20 20:59:33 +02:00
* @param boolean $active
* @return boolean
*/
2012-07-20 00:17:59 +02:00
public static function setActive($id,$active) {
2012-07-04 22:12:56 +02:00
$sql = 'UPDATE *PREFIX*contacts_addressbooks SET active = ? WHERE id = ?';
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', id: '.$id.', active: '.intval($active), OCP\Util::ERROR);
2012-07-04 22:12:56 +02:00
try {
$stmt = OCP\DB::prepare($sql);
$stmt->execute(array(intval($active), $id));
return true;
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(), OCP\Util::ERROR);
2012-07-04 22:12:56 +02:00
return false;
}
}
/**
* @brief Checks if an addressbook is active.
* @param integer $id ID of the address book.
* @return boolean
*/
2012-07-20 00:17:59 +02:00
public static function isActive($id) {
2012-07-04 22:12:56 +02:00
$sql = 'SELECT active FROM *PREFIX*contacts_addressbooks WHERE id = ?';
try {
$stmt = OCP\DB::prepare( $sql );
$result = $stmt->execute(array($id));
$row = $result->fetchRow();
return (bool)$row['active'];
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::ERROR);
2012-07-04 22:12:56 +02:00
}
}
2011-09-08 23:36:47 +02:00
/**
* @brief removes an address book
* @param integer $id
* @return boolean
*/
2012-07-20 00:17:59 +02:00
public static function delete($id) {
self::setActive($id, false);
2012-06-20 20:59:33 +02:00
try {
$stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
$stmt->execute(array($id));
} catch(Exception $e) {
2012-07-20 00:17:59 +02:00
OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(), OCP\Util::ERROR);
2012-06-20 20:59:33 +02:00
return false;
}
$cards = OC_Contacts_VCard::all($id);
foreach($cards as $card){
OC_Contacts_VCard::delete($card['id']);
2011-08-06 22:32:06 +02:00
}
return true;
}
2011-09-08 23:36:47 +02:00
/**
* @brief Updates ctag for addressbook
* @param integer $id
2011-09-08 23:36:47 +02:00
* @return boolean
*/
2012-07-20 00:17:59 +02:00
public static function touch($id) {
2012-05-03 13:06:08 +02:00
$stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET ctag = ctag + 1 WHERE id = ?' );
2011-08-06 22:32:06 +02:00
$stmt->execute(array($id));
return true;
}
2011-09-08 23:36:47 +02:00
/**
* @brief Creates a URI for Addressbook
* @param string $name name of the addressbook
2011-09-15 23:20:49 +02:00
* @param array $existing existing addressbook URIs
* @return string new name
2011-09-08 23:36:47 +02:00
*/
2012-07-20 00:17:59 +02:00
public static function createURI($name,$existing) {
$name = str_replace(' ', '_', strtolower($name));
2011-08-06 22:32:06 +02:00
$newname = $name;
$i = 1;
2012-07-20 00:17:59 +02:00
while(in_array($newname, $existing)) {
2011-08-06 22:32:06 +02:00
$newname = $name.$i;
$i = $i + 1;
}
return $newname;
}
2011-09-08 23:36:47 +02:00
/**
* @brief gets the userid from a principal path
* @return string
*/
2012-07-20 00:17:59 +02:00
public static function extractUserID($principaluri) {
list($prefix, $userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
2011-08-06 22:32:06 +02:00
return $userid;
}
}