/usr/share/horde/turba/lib/Object/Group.php is in php-horde-turba 4.1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <?php
/**
* The Turba_Object_Group:: class provides a set of methods for dealing with
* contact groups.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@csh.rit.edu>
* @package Turba
*/
class Turba_Object_Group extends Turba_Object
{
/**
* Constructs a new Turba_Object_Group.
*
* @param Turba_Driver $driver The driver object that this group comes
* from.
* @param array $attributes Hash of attributes for this group.
*/
public function __construct(Turba_Driver $driver, array $attributes = array())
{
parent::__construct($driver, $attributes);
$this->attributes['__type'] = 'Group';
}
/**
* Returns true if this object is a group of multiple contacts.
*
* @return boolean True.
*/
public function isGroup()
{
return true;
}
/**
* Contact url.
*
* @return Horde_Url
*/
public function url($view = null, $full = false)
{
return Horde::url('browse.php', $full)->add(array(
'source' => $this->getSource(),
'key' => $this->getValue('__key')
));
}
/**
* Adds a new contact entry to this group.
*
* @param string $contactId The id of the contact to add.
* @param string $sourceId The source $contactId is from.
*
* @throws Turba_Exception
* @throws Horde_Exception_NotFound
*/
public function addMember($contactId, $sourceId = null)
{
// Default to the same source as the group.
if (is_null($sourceId)) {
$sourceId = $this->getSource();
}
// Can't add a group to itself.
if ($contactId == $this->attributes['__key']) {
throw new Turba_Exception(_("Can't add a group to itself."));
}
// Try to find the contact being added.
if ($sourceId == $this->getSource()) {
$contact = $this->driver->getObject($contactId);
} else {
$driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
$contact = $driver->getObject($contactId);
}
// Explode members.
$members = @unserialize($this->attributes['__members']);
if (!is_array($members)) {
$members = array();
}
// If the contact is from a different source, store its source
// id as well.
$members[] = ($sourceId == $this->getSource())
? $contactId
: $sourceId . ':' . $contactId;
// Remove duplicates.
$this->attributes['__members'] = serialize(array_unique($members));
}
/**
* Deletes a contact from this group.
*
* @param string $contactId The id of the contact to remove.
* @param string $sourceId The source $contactId is from.
*/
public function removeMember($contactId, $sourceId = null)
{
$members = @unserialize($this->attributes['__members']);
if (is_null($sourceId) || $sourceId == $this->getSource()) {
$i = array_search($contactId, $members);
} else {
$i = array_search($sourceId . ':' . $contactId, $members);
}
if ($i !== false) {
unset($members[$i]);
}
$this->attributes['__members'] = serialize($members);
return true;
}
/**
* Count the number of contacts in this group.
*
* @return integer
*/
public function count()
{
$children = @unserialize($this->attributes['__members']);
if (!is_array($children)) {
return 0;
} else {
return count($children);
}
}
/**
* Retrieve the Objects in this group
*
* @param array $sort The requested sort order which is passed to
* Turba_List::sort().
*
* @return Turba_List List containing the members of this group
*/
public function listMembers($sort = null)
{
$list = new Turba_List();
$children = unserialize($this->attributes['__members']);
if (!is_array($children)) {
$children = array();
}
reset($children);
$modified = false;
foreach ($children as $member) {
if (strpos($member, ':') === false) {
try {
$contact = $this->driver->getObject($member);
} catch (Horde_Exception_NotFound $e) {
// Remove the contact if it no longer exists
$this->removeMember($member);
$modified = true;
continue;
}
} else {
list($sourceId, $contactId) = explode(':', $member, 2);
if (strpos($contactId, ':')) {
list($owner, $contactId) = explode(':', $contactId, 2);
$sourceId .= ':' . $owner;
}
try {
$driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
} catch (Turba_Exception $e) {
continue;
}
try {
$contact = $driver->getObject($contactId);
} catch (Horde_Exception_NotFound $e) {
// Remove the contact if it no longer exists
$this->removeMember($member);
$modified = true;
continue;
}
}
$list->insert($contact);
}
// If we've pruned any dead entries, store the changes.
if ($modified) {
$this->store();
}
$list->sort($sort);
return $list;
}
}
|