1: <?php
2: /**
3: * Extension of the Horde_Share_Object class for storing share information in
4: * the Horde_DataTree driver.
5: *
6: * @author Mike Cochrane <mike@graftonhall.co.nz>
7: * @author Jan Schneider <jan@horde.org>
8: * @author Gunnar Wrobel <wrobel@pardus.de>
9: * @package Share
10: */
11: class Horde_Share_Object_Datatree extends Horde_Share_Object
12: {
13: /**
14: * Serializable version.
15: */
16: const VERSION = 1;
17:
18: /**
19: * The actual storage object that holds the data.
20: *
21: * @var mixed
22: */
23: public $datatreeObject;
24:
25: /**
26: * Constructor.
27: *
28: * @param Horde_Share_Object_DataTree_Share $datatreeObject A Horde_Share_Object_DataTree_Share
29: * instance.
30: */
31: public function __construct(Horde_Share_Object_Datatree_Share $datatreeObject)
32: {
33: $this->datatreeObject = $datatreeObject;
34: }
35:
36: /**
37: * Serialize this object.
38: *
39: * @return string The serialized data.
40: */
41: public function serialize()
42: {
43: return serialize(array(
44: self::VERSION,
45: $this->datatreeObject,
46: $this->_shareCallback,
47: ));
48: }
49:
50: /**
51: * Reconstruct the object from serialized data.
52: *
53: * @param string $data The serialized data.
54: */
55: public function unserialize($data)
56: {
57: $data = @unserialize($data);
58: if (!is_array($data) ||
59: !isset($data[0]) ||
60: ($data[0] != self::VERSION)) {
61: throw new Exception('Cache version change');
62: }
63:
64: $this->datatreeObject = $data[1];
65: if (empty($data[2])) {
66: throw new Exception('Missing callback for Horde_Share_Object unserializing');
67: }
68: $this->_shareCallback = $data[2];
69: }
70:
71: /**
72: * Sets an attribute value in this object.
73: *
74: * @param string $attribute The attribute to set.
75: * @param mixed $value The value for $attribute.
76: *
77: * @return mixed True if setting the attribute did succeed, a PEAR_Error
78: * otherwise.
79: */
80: public function set($attribute, $value, $update = false)
81: {
82: Horde_Exception_Pear::catchError($this->datatreeObject->set($attribute, $value));
83: }
84:
85: /**
86: * Returns one of the attributes of the object, or null if it isn't
87: * defined.
88: *
89: * @param string $attribute The attribute to retrieve.
90: *
91: * @return mixed The value of the attribute, or an empty string.
92: */
93: public function get($attribute)
94: {
95: return Horde_Exception_Pear::catchError($this->datatreeObject->get($attribute));
96: }
97:
98: /**
99: * Returns the ID of this share.
100: *
101: * @return string The share's ID.
102: */
103: public function getId()
104: {
105: return Horde_Exception_Pear::catchError($this->datatreeObject->getId());
106: }
107:
108: /**
109: * Returns the name of this share.
110: *
111: * @return string The share's name.
112: */
113: public function getName()
114: {
115: return Horde_Exception_Pear::catchError($this->datatreeObject->getName());
116: }
117:
118: /**
119: * Saves the current attribute values.
120: */
121: protected function _save()
122: {
123: Horde_Exception_Pear::catchError($this->datatreeObject->save());
124: }
125:
126: /**
127: * Checks to see if a user has a given permission.
128: *
129: * @param string $userid The userid of the user.
130: * @param integer $permission A Horde_Perms::* constant to test for.
131: * @param string $creator The creator of the event.
132: *
133: * @return boolean Whether or not $userid has $permission.
134: */
135: public function hasPermission($userid, $permission, $creator = null)
136: {
137: if ($userid && $userid == $this->datatreeObject->get('owner')) {
138: return true;
139: }
140:
141: return $this->getShareOb()->getPermsObject()->hasPermission($this->getPermission(), $userid, $permission, $creator);
142: }
143:
144: /**
145: * Sets the permission of this share.
146: *
147: * @param Horde_Perms_Permission $perm Permission object.
148: * @param boolean $update Should the share be saved
149: * after this operation?
150: *
151: * @return boolean True if no error occured, PEAR_Error otherwise
152: */
153: public function setPermission($perm, $update = true)
154: {
155: $this->datatreeObject->data['perm'] = $perm->getData();
156: if ($update) {
157: Horde_Exception_Pear::catchError($this->datatreeObject->save());
158: }
159: }
160:
161: /**
162: * Returns the permission of this share.
163: *
164: * @return Horde_Perms_Permission Permission object that represents the
165: * permissions on this share
166: */
167: public function getPermission()
168: {
169: $perm = new Horde_Perms_Permission(Horde_Exception_Pear::catchError($this->datatreeObject->getName()));
170: $perm->data = isset($this->datatreeObject->data['perm'])
171: ? $this->datatreeObject->data['perm']
172: : array();
173: return $perm;
174: }
175: }
176: