1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Fix scrutinizer issues

This commit is contained in:
LEDfan 2014-04-01 21:08:29 +02:00
parent a6393f9695
commit 12b2b1cd61
2 changed files with 213 additions and 213 deletions

View File

@ -56,7 +56,7 @@ class App {
'ldap' => 'OCA\Contacts\Backend\Ldap', 'ldap' => 'OCA\Contacts\Backend\Ldap',
'local' => 'OCA\Contacts\Backend\Database', 'local' => 'OCA\Contacts\Backend\Database',
'shared' => 'OCA\Contacts\Backend\Shared', 'shared' => 'OCA\Contacts\Backend\Shared',
'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers', 'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers',
); );
public function __construct( public function __construct(

View File

@ -6,7 +6,7 @@ use OCA\Contacts\Contact,
OCA\Contacts\VObject\VCard, OCA\Contacts\VObject\VCard,
OCA\Contacts\Utils\Properties, OCA\Contacts\Utils\Properties,
Sabre\VObject\Reader, Sabre\VObject\Reader,
OCA\Contacts\Addressbook; OCA\Contacts\Addressbook;
/** /**
@ -17,127 +17,127 @@ use OCA\Contacts\Contact,
class OwnCloudUsers extends AbstractBackend { class OwnCloudUsers extends AbstractBackend {
public $name = 'OwnCloudUsers'; public $name = 'OwnCloudUsers';
/** /**
* The table that holds the address books. * The table that holds the address books.
* For every user there is *1* addressbook. * For every user there is *1* addressbook.
* @var string * @var string
*/ */
private $addressBooksTableName = '*PREFIX*contacts_ocu_addressbooks'; private $addressBooksTableName = '*PREFIX*contacts_ocu_addressbooks';
/** /**
* The table that holds the contacts. * The table that holds the contacts.
* @var string * @var string
*/ */
private $cardsTableName = '*PREFIX*contacts_ocu_cards'; private $cardsTableName = '*PREFIX*contacts_ocu_cards';
public function __construct($userid){ public function __construct($userid){
$this->userid = $userid ? $userid : \OCP\User::getUser(); $this->userid = $userid ? $userid : \OCP\User::getUser();
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getAddressBooksForUser(array $options = array()) { public function getAddressBooksForUser(array $options = array()) {
// Only 1 addressbook for every user // Only 1 addressbook for every user
$sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?';
$args = array($this->userid); $args = array($this->userid);
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute($args); $result = $query->execute($args);
$row = $result->fetchRow(); $row = $result->fetchRow();
if(!$row){ // TODO -> better way? if(!$row){ // TODO -> better way?
// Create new addressbook // Create new addressbook
$sql = 'INSERT INTO ' . $this->addressBooksTableName $sql = 'INSERT INTO ' . $this->addressBooksTableName
. ' ( ' . ' ( '
. 'id, ' . 'id, '
. 'displayname, ' . 'displayname, '
//. 'uri, ' TODO //. 'uri, ' TODO
. 'description, ' . 'description, '
//. 'ctag, ' //. 'ctag, '
. 'active ' . 'active '
. ') VALUES ( ' . ') VALUES ( '
. '?, ' . '?, '
. '?, ' . '?, '
. '?, ' . '?, '
. '? ' . '? '
. ')'; . ')';
$args = array( $args = array(
$this->userid, $this->userid,
'ownCloud Users', 'ownCloud Users',
'ownCloud Users', 'ownCloud Users',
1 1
); );
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$query->execute($args); $query->execute($args);
return $this->getAddressBooksForUser(); return $this->getAddressBooksForUser();
} else { } else {
$row['permissions'] = \OCP\PERMISSION_ALL; $row['permissions'] = \OCP\PERMISSION_ALL;
return array($row); return array($row);
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* Only 1 addressbook for every user * Only 1 addressbook for every user
*/ */
public function getAddressBook($addressBookId, array $options = array()) { public function getAddressBook($addressBookId, array $options = array()) {
$sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?';
$args = array($addressBookId);
$query = \OCP\DB::prepare($sql);
$result = $query->execute($args);
$row = $result->fetchRow();
$row['permissions'] = \OCP\PERMISSION_ALL;
$row['backend'] = $this->name;
return array($row); $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?';
$args = array($addressBookId);
$query = \OCP\DB::prepare($sql);
$result = $query->execute($args);
$row = $result->fetchRow();
$row['permissions'] = \OCP\PERMISSION_ALL;
$row['backend'] = $this->name;
return array($row);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* There are as many contacts in this addressbook as in this ownCloud installation * There are as many contacts in this addressbook as in this ownCloud installation
*/ */
public function getContacts($addressbookid, array $options = array()){ public function getContacts($addressbookid, array $options = array()){
$contacts = array(); $contacts = array();
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid)); $result = $query->execute(array($this->userid));
while($row = $result->fetchRow()){ while($row = $result->fetchRow()){
$row['permissions'] = \OCP\PERMISSION_ALL; $row['permissions'] = \OCP\PERMISSION_ALL;
$contacts[] = $row; $contacts[] = $row;
} }
$contactsId = array(); $contactsId = array();
foreach($contacts as $contact){ foreach($contacts as $contact){
$contactsId[] = $contact['id']; $contactsId[] = $contact['id'];
} }
$users = \OCP\User::getUsers(); $users = \OCP\User::getUsers();
$recall = false; $recall = false;
$add = array_diff($users, $contactsId); $add = array_diff($users, $contactsId);
$remove = array_diff($contactsId, $users); $remove = array_diff($contactsId, $users);
if(count($add) > 0){ if(count($add) > 0){
$this->addContacts($add, $addressbookid); $this->addContacts($add, $addressbookid);
$recall = true; $recall = true;
} }
if(count($remove) > 0){ if(count($remove) > 0){
$this->removeContacts($remove, $addressbookid); $this->removeContacts($remove, $addressbookid);
$recall = true; $recall = true;
} }
if($recall === true){ if($recall === true){
return $this->getContacts($addressbookid); return $this->getContacts($addressbookid);
} else { } else {
return $contacts; return $contacts;
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
* If your username is "admin" and you want to retrieve your own contact * If your username is "admin" and you want to retrieve your own contact
@ -146,20 +146,20 @@ class OwnCloudUsers extends AbstractBackend {
* ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar'; * ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar';
*/ */
public function getContact($addressbookid, $id, array $options = array()){ public function getContact($addressbookid, $id, array $options = array()){
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid)); $result = $query->execute(array($this->userid));
$row = $result->fetchRow(); $row = $result->fetchRow();
$row['permissions'] = \OCP\PERMISSION_ALL; $row['permissions'] = \OCP\PERMISSION_ALL;
return $row; return $row;
} }
// Not needed since there is only one addressbook for every user // Not needed since there is only one addressbook for every user
public function createAddressBook(array $properties) { public function createAddressBook(array $properties) {
} }
/** /**
* Help function to add contacts to an addressbook. * Help function to add contacts to an addressbook.
* This only happens when an admin creates new users * This only happens when an admin creates new users
@ -168,43 +168,43 @@ class OwnCloudUsers extends AbstractBackend {
* @return bool * @return bool
*/ */
private function addContacts($contacts, $addressbookid){ private function addContacts($contacts, $addressbookid){
foreach($contacts as $user){ foreach($contacts as $user){
$sql = 'INSERT INTO ' . $this->cardsTableName . ' (' $sql = 'INSERT INTO ' . $this->cardsTableName . ' ('
. 'id, ' . 'id, '
. 'owner,' . 'owner,'
. 'addressbookid, ' . 'addressbookid, '
. 'fullname, ' . 'fullname, '
. 'carddata, ' . 'carddata, '
. 'uri, ' . 'uri, '
. 'lastmodified' . 'lastmodified'
. ') VALUES (' . ') VALUES ('
. '?,' . '?,'
. '?,' . '?,'
. '?,' . '?,'
. '?,' . '?,'
. '?,' . '?,'
. '?,' . '?,'
. '?' . '?'
. ')'; . ')';
$query = \OCP\DB::prepare($sql);
$contact = new Contact( $query = \OCP\DB::prepare($sql);
$addressBook = new AddressBook($this , $this->getAddressBooksForUser()), // since there is only one addressbook with OC users for each OC user we can use this function
$this, $contact = new Contact(
array( $addressBook = new AddressBook($this , $this->getAddressBooksForUser()), // since there is only one addressbook with OC users for each OC user we can use this function
"id" => $user, $this,
"lastmodified" => time(), array(
"displayname" => \OCP\User::getDisplayName($user), "id" => $user,
"fullname" => \OCP\User::getDisplayName($user) "lastmodified" => time(),
) "displayname" => \OCP\User::getDisplayName($user),
); "fullname" => \OCP\User::getDisplayName($user)
$carddata = $this->generateCardData($contact); )
$result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time())); );
// TODO Check if $result succeeded $carddata = $this->generateCardData($contact);
} $result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time()));
// TODO Check if $result succeeded
}
} }
/** /**
* Help function to remove contacts from an addressbook. * Help function to remove contacts from an addressbook.
* This only happens when an admin remove an ownCloud user * This only happens when an admin remove an ownCloud user
@ -213,104 +213,104 @@ class OwnCloudUsers extends AbstractBackend {
* @return bool * @return bool
*/ */
private function removeContacts($contacts, $addressbookid){ private function removeContacts($contacts, $addressbookid){
foreach($contacts as $user){ foreach($contacts as $user){
$sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?'; $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid, $user)); $result = $query->execute(array($this->userid, $user));
// TODO Check if $result succeeded // TODO Check if $result succeeded
} }
} }
/** /**
* Help function to generate the carddate which than can be stored in the db * Help function to generate the carddate which than can be stored in the db
* @param string|VCard $data * @param string|VCard $data
* @return Vcard * @return Vcard
*/ */
private function generateCardData($data){ private function generateCardData($data){
if (!$data instanceof VCard) { if (!$data instanceof VCard) {
try { try {
$data = Reader::read($data); $data = Reader::read($data);
} catch(\Exception $e) { } catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false; return false;
} }
} }
try { try {
$data->validate(VCard::REPAIR|VCard::UPGRADE); $data->validate(VCard::REPAIR|VCard::UPGRADE);
} catch (\Exception $e) { } catch (\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . \OCP\Util::writeLog('contacts', __METHOD__ . ' ' .
'Error validating vcard: ' . $e->getMessage(), \OCP\Util::ERROR); 'Error validating vcard: ' . $e->getMessage(), \OCP\Util::ERROR);
return false; return false;
} }
$now = new \DateTime; $now = new \DateTime;
$data->REV = $now->format(\DateTime::W3C); $data->REV = $now->format(\DateTime::W3C);
$appinfo = \OCP\App::getAppInfo('contacts'); $appinfo = \OCP\App::getAppInfo('contacts');
$appversion = \OCP\App::getAppVersion('contacts'); $appversion = \OCP\App::getAppVersion('contacts');
$prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN'; $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN';
$data->PRODID = $prodid; $data->PRODID = $prodid;
return $data; return $data;
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function updateContact($addressBookId, $id, $contact, array $options = array()) { public function updateContact($addressBookId, $id, $contact, array $options = array()) {
$updateRevision = true; $updateRevision = true;
$isCardDAV = false; $isCardDAV = false;
if (!$contact instanceof VCard) { if (!$contact instanceof VCard) {
try { try {
$contact = Reader::read($contact); $contact = Reader::read($contact);
} catch(\Exception $e) { } catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false; return false;
} }
} }
if (is_array($id)) { if (is_array($id)) {
if (isset($id['id'])) { if (isset($id['id'])) {
$id = $id['id']; $id = $id['id'];
} elseif (isset($id['uri'])) { } elseif (isset($id['uri'])) {
$updateRevision = false; $updateRevision = false;
$isCardDAV = true; $isCardDAV = true;
$id = $this->getIdFromUri($id['uri']); $id = $this->getIdFromUri($id['uri']);
if (is_null($id)) { if (is_null($id)) {
\OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR); \OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR);
return false; return false;
} }
} else { } else {
throw new \Exception( throw new \Exception(
__METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.' __METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.'
); );
} }
} }
if ($updateRevision || !isset($contact->REV)) { if ($updateRevision || !isset($contact->REV)) {
$now = new \DateTime; $now = new \DateTime;
$contact->REV = $now->format(\DateTime::W3C); $contact->REV = $now->format(\DateTime::W3C);
} }
$data = $contact->serialize(); $data = $contact->serialize();
$sql = 'UPDATE ' . $this->cardsTableName $sql = 'UPDATE ' . $this->cardsTableName
. ' SET ' . ' SET '
. '`addressbookid` = ?, ' . '`addressbookid` = ?, '
. '`fullname` = ?, ' . '`fullname` = ?, '
. '`carddata` = ?, ' . '`carddata` = ?, '
. '`lastmodified` = ? ' . '`lastmodified` = ? '
. ' WHERE ' . ' WHERE '
. '`id` = ? ' . '`id` = ? '
. 'AND `owner` = ? '; . 'AND `owner` = ? ';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($addressBookId, $contact->FN, $data, time(), $id, $this->userid)); $result = $query->execute(array($addressBookId, $contact->FN, $data, time(), $id, $this->userid));
return true; return true;
} }
} }