2011-08-07 00:32:06 +04: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 13:06:21 +04:00
|
|
|
*
|
2011-08-07 00:32:06 +04:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* This class manages our addressbooks.
|
|
|
|
*/
|
|
|
|
class OC_Contacts_Addressbook{
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Returns the list of addressbooks for a specific user.
|
|
|
|
* @param string $uid
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-12-16 20:42:07 +04:00
|
|
|
public static function all($uid){
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ORDER BY displayname' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$result = $stmt->execute(array($uid));
|
2011-09-15 13:06:21 +04:00
|
|
|
|
2011-08-07 00:32:06 +04:00
|
|
|
$addressbooks = array();
|
|
|
|
while( $row = $result->fetchRow()){
|
|
|
|
$addressbooks[] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $addressbooks;
|
|
|
|
}
|
2011-09-15 13:06:21 +04:00
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Returns the list of addressbooks for a principal (DAV term of user)
|
|
|
|
* @param string $principaluri
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function allWherePrincipalURIIs($principaluri){
|
2011-08-07 00:32:06 +04:00
|
|
|
$uid = self::extractUserID($principaluri);
|
2011-12-16 20:42:07 +04:00
|
|
|
return self::all($uid);
|
2011-08-07 00:32:06 +04:00
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Gets the data of one address book
|
|
|
|
* @param integer $id
|
|
|
|
* @return associative array
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function find($id){
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$result = $stmt->execute(array($id));
|
|
|
|
|
|
|
|
return $result->fetchRow();
|
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Creates a new address book
|
|
|
|
* @param string $userid
|
|
|
|
* @param string $name
|
|
|
|
* @param string $description
|
|
|
|
* @return insertid
|
|
|
|
*/
|
2012-05-14 01:24:04 +04:00
|
|
|
public static function add($userid,$name,$description=''){
|
2011-12-16 20:42:07 +04:00
|
|
|
$all = self::all($userid);
|
2011-08-07 00:32:06 +04:00
|
|
|
$uris = array();
|
|
|
|
foreach($all as $i){
|
|
|
|
$uris[] = $i['uri'];
|
|
|
|
}
|
|
|
|
|
2011-08-09 16:15:11 +04:00
|
|
|
$uri = self::createURI($name, $uris );
|
2011-08-07 00:32:06 +04:00
|
|
|
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$result = $stmt->execute(array($userid,$name,$uri,$description,1));
|
|
|
|
|
2012-05-03 15:06:08 +04:00
|
|
|
return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
|
2011-08-07 00:32:06 +04:00
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04: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
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function addFromDAVData($principaluri,$uri,$name,$description){
|
2011-08-07 00:32:06 +04:00
|
|
|
$userid = self::extractUserID($principaluri);
|
2011-09-15 13:06:21 +04:00
|
|
|
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$result = $stmt->execute(array($userid,$name,$uri,$description,1));
|
|
|
|
|
2012-05-03 15:06:08 +04:00
|
|
|
return OCP\DB::insertid('*PREFIX*contacts_addressbooks');
|
2011-08-07 00:32:06 +04:00
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Edits an addressbook
|
|
|
|
* @param integer $id
|
|
|
|
* @param string $name
|
|
|
|
* @param string $description
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function edit($id,$name,$description){
|
2011-08-07 00:32:06 +04:00
|
|
|
// Need these ones for checking uri
|
|
|
|
$addressbook = self::find($id);
|
|
|
|
|
|
|
|
if(is_null($name)){
|
|
|
|
$name = $addressbook['name'];
|
|
|
|
}
|
|
|
|
if(is_null($description)){
|
|
|
|
$description = $addressbook['description'];
|
|
|
|
}
|
2011-09-15 13:06:21 +04:00
|
|
|
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET displayname=?,description=?, ctag=ctag+1 WHERE id=?' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$result = $stmt->execute(array($name,$description,$id));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-31 00:07:16 +04:00
|
|
|
public static function cleanArray($array, $remove_null_number = true){
|
|
|
|
$new_array = array();
|
|
|
|
|
|
|
|
$null_exceptions = array();
|
|
|
|
|
|
|
|
foreach ($array as $key => $value){
|
|
|
|
$value = trim($value);
|
|
|
|
|
|
|
|
if($remove_null_number){
|
|
|
|
$null_exceptions[] = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!in_array($value, $null_exceptions) && $value != "") {
|
|
|
|
$new_array[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $new_array;
|
|
|
|
}
|
|
|
|
|
2011-12-09 18:10:51 +04:00
|
|
|
/**
|
|
|
|
* @brief Get active addressbooks for a user.
|
|
|
|
* @param integer $uid User id. If null current user will be used.
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-12-28 01:44:52 +04:00
|
|
|
public static function activeIds($uid = null){
|
2011-12-09 18:10:51 +04:00
|
|
|
if(is_null($uid)){
|
2012-05-01 20:50:31 +04:00
|
|
|
$uid = OCP\USER::getUser();
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
2012-05-02 17:54:34 +04:00
|
|
|
$prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null);
|
2012-07-08 17:41:38 +04:00
|
|
|
$prefbooks = explode(';',$prefbooks);
|
|
|
|
for ($i = 0; $i < count($prefbooks); $i++) {
|
|
|
|
if(!self::find($prefbooks[$i])) {
|
|
|
|
unset($prefbooks[$i]);
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 18:29:56 +04:00
|
|
|
if(!$prefbooks){
|
2012-07-08 17:41:38 +04:00
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No active addressbooks',OCP\Util::DEBUG);
|
2011-12-16 20:42:07 +04:00
|
|
|
$addressbooks = OC_Contacts_Addressbook::all($uid);
|
2011-12-09 18:10:51 +04:00
|
|
|
if(count($addressbooks) == 0){
|
2012-07-08 17:41:38 +04:00
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No addressbooks',OCP\Util::DEBUG);
|
|
|
|
$id = self::add($uid,'default','Default Address Book');
|
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, Created addressbook: '.$id,OCP\Util::DEBUG);
|
|
|
|
self::setActive($id, true);
|
2011-12-16 20:42:07 +04:00
|
|
|
$addressbooks = OC_Contacts_Addressbook::all($uid);
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
2012-07-08 17:41:38 +04:00
|
|
|
$prefbooks[] = $addressbooks[0]['id'];
|
|
|
|
OCP\Config::setUserValue($uid,'contacts','openaddressbooks',implode(';',$prefbooks));
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
2012-07-08 17:41:38 +04:00
|
|
|
return $prefbooks;
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
|
|
|
|
2011-12-11 19:26:00 +04:00
|
|
|
/**
|
|
|
|
* @brief Returns the list of active addressbooks for a specific user.
|
|
|
|
* @param string $uid
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-12-16 20:42:07 +04:00
|
|
|
public static function active($uid){
|
|
|
|
$active = self::activeIds($uid);
|
2011-12-11 19:26:00 +04:00
|
|
|
$addressbooks = array();
|
2012-07-08 17:41:38 +04:00
|
|
|
if(!$active) {
|
|
|
|
return $addressbooks;
|
|
|
|
}
|
2011-12-31 04:34:22 +04:00
|
|
|
$ids_sql = join(',', array_fill(0, count($active), '?'));
|
|
|
|
$prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname';
|
|
|
|
try {
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( $prep );
|
2011-12-31 04:34:22 +04:00
|
|
|
$result = $stmt->execute($active);
|
|
|
|
} catch(Exception $e) {
|
2012-05-01 19:38:27 +04:00
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:active:, exception: '.$e->getMessage(),OCP\Util::DEBUG);
|
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:active, ids: '.join(',', $active),OCP\Util::DEBUG);
|
|
|
|
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook::active, SQL:'.$prep,OCP\Util::DEBUG);
|
2011-12-31 04:34:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while( $row = $result->fetchRow()){
|
|
|
|
$addressbooks[] = $row;
|
|
|
|
}
|
2011-12-11 19:26:00 +04:00
|
|
|
|
|
|
|
return $addressbooks;
|
|
|
|
}
|
|
|
|
|
2011-12-04 23:25:46 +04:00
|
|
|
/**
|
|
|
|
* @brief Activates an addressbook
|
|
|
|
* @param integer $id
|
|
|
|
* @param integer $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-07-08 17:41:38 +04:00
|
|
|
public static function setActive($id,$active=true){
|
2011-12-04 23:25:46 +04:00
|
|
|
// Need these ones for checking uri
|
|
|
|
//$addressbook = self::find($id);
|
|
|
|
|
|
|
|
if(is_null($id)){
|
|
|
|
$id = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-16 20:42:07 +04:00
|
|
|
$openaddressbooks = self::activeIds();
|
2011-12-09 18:10:51 +04:00
|
|
|
if($active) {
|
|
|
|
if(!in_array($id, $openaddressbooks)) {
|
2011-12-11 19:26:00 +04:00
|
|
|
$openaddressbooks[] = $id;
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
2011-12-11 19:26:00 +04:00
|
|
|
} else {
|
2011-12-09 18:10:51 +04:00
|
|
|
if(in_array($id, $openaddressbooks)) {
|
2011-12-11 19:26:00 +04:00
|
|
|
unset($openaddressbooks[array_search($id, $openaddressbooks)]);
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-11 06:56:53 +04:00
|
|
|
// NOTE: Ugly hack...
|
2011-12-31 00:07:16 +04:00
|
|
|
$openaddressbooks = self::cleanArray($openaddressbooks, false);
|
2011-12-09 18:10:51 +04:00
|
|
|
sort($openaddressbooks, SORT_NUMERIC);
|
2011-12-11 19:26:00 +04:00
|
|
|
// FIXME: I alway end up with a ';' prepending when imploding the array..?
|
2012-05-02 17:54:34 +04:00
|
|
|
OCP\Config::setUserValue(OCP\USER::getUser(),'contacts','openaddressbooks',implode(';', $openaddressbooks));
|
2011-12-04 23:25:46 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-09 18:10:51 +04:00
|
|
|
/**
|
|
|
|
* @brief Checks if an addressbook is active.
|
|
|
|
* @param integer $id ID of the address book.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function isActive($id){
|
2012-05-01 19:38:27 +04:00
|
|
|
//OCP\Util::writeLog('contacts','OC_Contacts_Addressbook::isActive('.$id.'):'.in_array($id, self::activeIds()), OCP\Util::DEBUG);
|
2011-12-16 20:42:07 +04:00
|
|
|
return in_array($id, self::activeIds());
|
2011-12-09 18:10:51 +04:00
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief removes an address book
|
|
|
|
* @param integer $id
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function delete($id){
|
2012-01-11 23:07:15 +04:00
|
|
|
// FIXME: There's no error checking at all.
|
2011-12-11 19:26:00 +04:00
|
|
|
self::setActive($id, false);
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$stmt->execute(array($id));
|
2011-09-15 13:06:21 +04:00
|
|
|
|
2011-09-17 02:26:57 +04:00
|
|
|
$cards = OC_Contacts_VCard::all($id);
|
|
|
|
foreach($cards as $card){
|
|
|
|
OC_Contacts_VCard::delete($card['id']);
|
2011-08-07 00:32:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
2011-09-17 02:26:57 +04:00
|
|
|
* @brief Updates ctag for addressbook
|
|
|
|
* @param integer $id
|
2011-09-09 01:36:47 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-09-17 02:26:57 +04:00
|
|
|
public static function touch($id){
|
2012-05-03 15:06:08 +04:00
|
|
|
$stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET ctag = ctag + 1 WHERE id = ?' );
|
2011-08-07 00:32:06 +04:00
|
|
|
$stmt->execute(array($id));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief Creates a URI for Addressbook
|
|
|
|
* @param string $name name of the addressbook
|
2011-09-16 01:20:49 +04:00
|
|
|
* @param array $existing existing addressbook URIs
|
|
|
|
* @return string new name
|
2011-09-09 01:36:47 +04:00
|
|
|
*/
|
2011-08-07 00:32:06 +04:00
|
|
|
public static function createURI($name,$existing){
|
|
|
|
$name = strtolower($name);
|
|
|
|
$newname = $name;
|
|
|
|
$i = 1;
|
|
|
|
while(in_array($newname,$existing)){
|
|
|
|
$newname = $name.$i;
|
|
|
|
$i = $i + 1;
|
|
|
|
}
|
|
|
|
return $newname;
|
|
|
|
}
|
|
|
|
|
2011-09-09 01:36:47 +04:00
|
|
|
/**
|
|
|
|
* @brief gets the userid from a principal path
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-08-07 00:32:06 +04:00
|
|
|
public static function extractUserID($principaluri){
|
|
|
|
list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
|
|
|
|
return $userid;
|
|
|
|
}
|
|
|
|
}
|