1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: class Horde_Share_Object_Sql extends Horde_Share_Object implements Serializable
11: {
12: 13: 14:
15: const VERSION = 2;
16:
17: 18: 19: 20: 21: 22:
23: public $data = array();
24:
25: 26: 27: 28: 29:
30: public function __construct($data)
31: {
32: if (!isset($data['share_parents'])) {
33: $data['share_parents'] = null;
34: }
35: if (!isset($data['perm']) || !is_array($data['perm'])) {
36: $this->data['perm'] = array(
37: 'users' => array(),
38: 'type' => 'matrix',
39: 'default' => isset($data['perm_default'])
40: ? (int)$data['perm_default'] : 0,
41: 'guest' => isset($data['perm_guest'])
42: ? (int)$data['perm_guest'] : 0,
43: 'creator' => isset($data['perm_creator'])
44: ? (int)$data['perm_creator'] : 0,
45: 'groups' => array());
46:
47: unset($data['perm_creator'], $data['perm_guest'],
48: $data['perm_default']);
49: }
50: $this->data = array_merge($data, $this->data);
51: }
52:
53: 54: 55: 56: 57:
58: public function serialize()
59: {
60: return serialize(array(
61: self::VERSION,
62: $this->data,
63: $this->_shareCallback,
64: ));
65: }
66:
67: 68: 69: 70: 71:
72: public function unserialize($data)
73: {
74: $data = @unserialize($data);
75: if (!is_array($data) ||
76: !isset($data[0]) ||
77: ($data[0] != self::VERSION)) {
78: throw new Exception('Cache version change');
79: }
80:
81: $this->data = $data[1];
82: if (empty($data[2])) {
83: throw new Exception('Missing callback for Horde_Share_Object unserializing');
84: }
85: $this->_shareCallback = $data[2];
86: }
87:
88: 89: 90: 91: 92: 93: 94:
95: public function set($attribute, $value, $update = false)
96: {
97: if ($attribute == 'owner') {
98: $driver_key = 'share_owner';
99: } else {
100: $driver_key = 'attribute_' . $attribute;
101: }
102: $this->data[$driver_key] = $value;
103:
104:
105: if ($update) {
106: $db = $this->getShareOb()->getStorage();
107:
108: $data = $this->getshareOb()->toDriverCharset(array($driver_key => $value));
109: $sql = 'UPDATE ' . $this->getShareOb()->getTable() . ' SET ' . $driver_key . ' = ? WHERE share_id = ?';
110: try {
111: $db->update($sql, array($data[$driver_key], $this->getId()));
112: } catch (Horde_Db_Exception $e) {
113: throw new Horde_Share_Exception($e);
114: }
115: }
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125:
126: public function get($attribute)
127: {
128: if ($attribute == 'owner' || $attribute == 'parents') {
129: return $this->data['share_' . $attribute];
130: } elseif (isset($this->data['attribute_' . $attribute])) {
131: return $this->data['attribute_' . $attribute];
132: } else {
133: return null;
134: }
135: }
136:
137: 138: 139: 140: 141:
142: public function getId()
143: {
144: return isset($this->data['share_id']) ? $this->data['share_id'] : null;
145: }
146:
147: 148: 149: 150: 151:
152: public function getName()
153: {
154: return $this->data['share_name'];
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165:
166: public function countChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true)
167: {
168: return $this->getShareOb()->countShares($user, $perm, null, $this, $allLevels);
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179: 180:
181: public function getChildren($user, $perm = Horde_Perms::SHOW, $allLevels = true)
182: {
183: return $this->getShareOb()->listShares(
184: $user, array('perm' => $perm,
185: 'direction' => 1,
186: 'parent' => $this,
187: 'all_levels' => $allLevels));
188: }
189:
190: 191: 192: 193: 194:
195: public function getParent()
196: {
197: return $this->getShareOb()->getParent($this);
198: }
199:
200: 201: 202: 203: 204:
205: public function getParents()
206: {
207: $parents = array();
208: $share = $this->getParent();
209: while ($share instanceof Horde_Share_Object) {
210: $parents[] = $share;
211: $share = $share->getParent();
212: }
213:
214: return array_reverse($parents);
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: public function setParent($parent)
225: {
226: if (!is_null($parent) && !is_a($parent, 'Horde_Share_Object')) {
227: $parent = $this->getShareOb()->getShareById($parent);
228: }
229:
230:
231: if ($this->getId()) {
232: $children = $this->getShareOb()->listShares(null,
233: array('perm' => null,
234: 'parent' => $this,
235: 'all_levels' => true));
236: } else {
237: $children = array();
238: }
239:
240:
241: if (!empty($parent) && in_array($parent->getId(), array_keys($children))) {
242: throw new Horde_Share_Exception('Cannot set an existing child as the parent');
243: }
244:
245: if (!is_null($parent)) {
246: $parent_string = $parent->get('parents') . ':' . $parent->getId();
247: } else {
248: $parent_string = null;
249: }
250: $this->data['share_parents'] = $parent_string;
251: $sql = 'UPDATE ' . $this->getShareOb()->getTable() . ' SET share_parents = ? WHERE share_id = ?';
252: try {
253: $this->getShareOb()->getStorage()->update($sql, array($this->data['share_parents'], $this->getId()));
254: } catch (Horde_Db_Exception $e) {
255: throw new Horde_Share_Exception($e->getMessage());
256: }
257:
258:
259: foreach ($children as $child) {
260: $child->setParent($this);
261: }
262:
263: $this->_shareOb->expireListCache();
264: return true;
265: }
266:
267: 268: 269:
270: protected function _save()
271: {
272: $db = $this->getShareOb()->getStorage();
273: $table = $this->getShareOb()->getTable();
274:
275: $fields = array();
276: $params = array();
277:
278:
279: foreach ($this->getShareOb()->toDriverCharset($this->data) as $key => $value) {
280: if ($key != 'share_id' && $key != 'perm' && $key != 'share_flags') {
281: $fields[] = $key;
282: $params[] = $value;
283: }
284: }
285:
286: $fields[] = 'perm_creator';
287: $params[] = isset($this->data['perm']['creator']) ? (int)$this->data['perm']['creator'] : 0;
288:
289: $fields[] = 'perm_default';
290: $params[] = isset($this->data['perm']['default']) ? (int)$this->data['perm']['default'] : 0;
291:
292: $fields[] = 'perm_guest';
293: $params[] = isset($this->data['perm']['guest']) ? (int)$this->data['perm']['guest'] : 0;
294:
295: $fields[] = 'share_flags';
296: $flags = 0;
297: if (!empty($this->data['perm']['users'])) {
298: $flags |= Horde_Share_Sql::SQL_FLAG_USERS;
299: }
300: if (!empty($this->data['perm']['groups'])) {
301: $flags |= Horde_Share_Sql::SQL_FLAG_GROUPS;
302: }
303: $params[] = $flags;
304:
305:
306: if (empty($this->data['share_id'])) {
307: $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (?' . str_repeat(', ?', count($fields) - 1) . ')';
308: try {
309: $this->data['share_id'] = $db->insert($sql, $params);
310: } catch (Horde_Db_Exception $e) {
311: throw new Horde_Share_Exception($e);
312: }
313: } else {
314: $sql = 'UPDATE ' . $table . ' SET ' . implode(' = ?, ', $fields) . ' = ? WHERE share_id = ?';
315: $params[] = $this->data['share_id'];
316: try {
317: $db->update($sql, $params);
318: } catch (Horde_Db_Exception $e) {
319: throw new Horde_Share_Exception($e);
320: }
321: }
322:
323:
324: $db->delete('DELETE FROM ' . $table . '_users WHERE share_id = ?', array($this->data['share_id']));
325: if (!empty($this->data['perm']['users'])) {
326: $data = array();
327: foreach ($this->data['perm']['users'] as $user => $perm) {
328: $db->insert('INSERT INTO ' . $table . '_users (share_id, user_uid, perm) VALUES (?, ?, ?)', array($this->data['share_id'], $user, $perm));
329: }
330: }
331:
332:
333: $db->delete('DELETE FROM ' . $table . '_groups WHERE share_id = ?', array($this->data['share_id']));
334: if (!empty($this->data['perm']['groups'])) {
335: $data = array();
336: foreach ($this->data['perm']['groups'] as $group => $perm) {
337: $db->insert('INSERT INTO ' . $table . '_groups (share_id, group_uid, perm) VALUES (?, ?, ?)', array($this->data['share_id'], $group, $perm));
338: }
339: }
340:
341: return true;
342: }
343:
344: 345: 346: 347: 348: 349: 350: 351: 352:
353: public function hasPermission($userid, $permission, $creator = null)
354: {
355: 356:
357: if ($userid === $this->data['share_owner']) {
358: return true;
359: }
360: return $this->getShareOb()->getPermsObject()->hasPermission($this->getPermission(), $userid, $permission, $creator);
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: public function setPermission($perm, $update = true)
371: {
372: $this->data['perm'] = $perm->getData();
373: if ($update) {
374: $this->save();
375: }
376: }
377:
378: 379: 380: 381: 382: 383:
384: public function getPermission()
385: {
386: $perm = new Horde_Perms_Permission($this->getName());
387: $perm->data = isset($this->data['perm'])
388: ? $this->data['perm']
389: : array();
390:
391: return $perm;
392: }
393: }
394: