Overview

Packages

  • None
  • Share

Classes

  • Horde_Share_Base
  • Horde_Share_Datatree
  • Horde_Share_Kolab
  • Horde_Share_Object
  • Horde_Share_Object_Datatree
  • Horde_Share_Object_DataTree_Share
  • Horde_Share_Object_Kolab
  • Horde_Share_Object_Sql
  • Horde_Share_Object_Sqlng
  • Horde_Share_Sql
  • Horde_Share_Sqlng
  • Horde_Share_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Extension of the Horde_Share_Object class for storing share information in
  4:  * the sql driver.
  5:  *
  6:  * @author  Duck <duck@obala.net>
  7:  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
  8:  * @package Share
  9:  */
 10: class Horde_Share_Object_Sql extends Horde_Share_Object implements Serializable
 11: {
 12:     /**
 13:      * Serializable version.
 14:      */
 15:     const VERSION = 2;
 16: 
 17:     /**
 18:      * The actual storage object that holds the data.
 19:      *
 20:      * @TODO: Check visibility - should be protected/private
 21:      * @var array
 22:      */
 23:     public $data = array();
 24: 
 25:     /**
 26:      * Constructor.
 27:      *
 28:      * @param array $data Share data array.
 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:      * Serialize this object.
 55:      *
 56:      * @return string  The serialized data.
 57:      */
 58:     public function serialize()
 59:     {
 60:         return serialize(array(
 61:             self::VERSION,
 62:             $this->data,
 63:             $this->_shareCallback,
 64:         ));
 65:     }
 66: 
 67:     /**
 68:      * Reconstruct the object from serialized data.
 69:      *
 70:      * @param string $data  The serialized data.
 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:      * Sets an attribute value in this object.
 90:      *
 91:      * @param string $attribute  The attribute to set.
 92:      * @param mixed $value       The value for $attribute.
 93:      * @param boolean $update    Update *only* this change immeditely.
 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:         /* Update the backend, but only this current change */
105:         if ($update) {
106:             $db = $this->getShareOb()->getStorage();
107:             // Manually convert the charset since we're not going through save()
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:      * Returns one of the attributes of the object, or null if it isn't
120:      * defined.
121:      *
122:      * @param string $attribute  The attribute to retrieve.
123:      *
124:      * @return mixed  The value of the attribute, or an empty string.
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:      * Returns the ID of this share.
139:      *
140:      * @return string  The share's ID.
141:      */
142:     public function getId()
143:     {
144:         return isset($this->data['share_id']) ? $this->data['share_id'] : null;
145:     }
146: 
147:     /**
148:      * Returns the name of this share.
149:      *
150:      * @return string  The share's name.
151:      */
152:     public function getName()
153:     {
154:         return $this->data['share_name'];
155:     }
156: 
157:     /**
158:      * Return a count of the number of children this share has
159:      *
160:      * @param string $user        The user to use for checking perms
161:      * @param integer $perm       A Horde_Perms::* constant
162:      * @param boolean $allLevels  Count grandchildren or just children
163:      *
164:      * @return integer  The number of child shares
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:      * Get all children of this share.
173:      *
174:      * @param string $user        The user to use for checking perms
175:      * @param integer $perm       Horde_Perms::* constant. If NULL will return
176:      *                            all shares regardless of permissions.
177:      * @param boolean $allLevels  Return all levels.
178:      *
179:      * @return array  An array of Horde_Share_Object objects
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:      * Returns a child's direct parent
192:      *
193:      * @return Horde_Share_Object The direct parent Horde_Share_Object
194:      */
195:     public function getParent()
196:     {
197:         return $this->getShareOb()->getParent($this);
198:     }
199: 
200:     /**
201:      * Get all of this share's parents.
202:      *
203:      * @return array()  An array of Horde_Share_Objects
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:      * Set the parent object for this share.
219:      *
220:      * @param mixed $parent    A Horde_Share object or share id for the parent.
221:      *
222:      * @return boolean
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:         /* If we are an existing share, check for any children */
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:         /* Can't set a child share as a parent */
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:         /* Now we can reset the children's parent */
259:         foreach ($children as $child) {
260:             $child->setParent($this);
261:         }
262: 
263:         $this->_shareOb->expireListCache();
264:         return true;
265:     }
266: 
267:     /**
268:      * Saves the current attribute values.
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:         // Build the parameter arrays for the sql statement.
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:         // Insert new share record, or update existing
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:         // Update the share's user permissions
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:         // Update the share's group permissions
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:      * Checks to see if a user has a given permission.
346:      *
347:      * @param string $userid       The userid of the user.
348:      * @param integer $permission  A Horde_Perms::* constant to test for.
349:      * @param string $creator      The creator of the event.
350:      *
351:      * @return boolean  Whether or not $userid has $permission.
352:      */
353:     public function hasPermission($userid, $permission, $creator = null)
354:     {
355:         /* Test identity because system share owners are null, and guests are
356:          * false. */
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:      * Sets the permission of this share.
365:      *
366:      * @param Horde_Perms_Permission $perm  Permission object.
367:      * @param boolean $update               Should the share be saved
368:      *                                      after this operation?
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:      * Returns the permission of this share.
380:      *
381:      * @return Horde_Perms_Permission  Permission object that represents the
382:      *                                 permissions on this share.
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: 
API documentation generated by ApiGen