Overview

Packages

  • None
  • Trean

Classes

  • DataTreeObject_Folder
  • SearchForm
  • Trean
  • Trean_Bookmark
  • Trean_Bookmarks
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Trean Base Class.
  4:  *
  5:  * $Horde: trean/lib/Trean.php,v 1.93 2009-11-29 15:51:42 chuck Exp $
  6:  *
  7:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  8:  *
  9:  * See the enclosed file LICENSE for license information (BSD). If you did not
 10:  * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 11:  *
 12:  * @author  Mike Cochrane <mike@graftonhall.co.nz>
 13:  * @package Trean
 14:  */
 15: class Trean
 16: {
 17:     public static function initialize()
 18:     {
 19:         // Make sure "My Bookmarks" folder exists
 20:         if ($GLOBALS['registry']->getAuth() && !$GLOBALS['trean_shares']->exists($GLOBALS['registry']->getAuth())) {
 21:             $identity = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create();
 22:             $name = $identity->getValue('fullname');
 23:             if (trim($name) == '') {
 24:                 $name = $GLOBALS['registry']->getAuth();
 25:             }
 26:             $folder = $GLOBALS['trean_shares']->newFolder($GLOBALS['registry']->getAuth(), array('name' => sprintf(_("%s's Bookmarks"), $name)));
 27:             $result = $GLOBALS['trean_shares']->addFolder($folder);
 28:             if ($result instanceof PEAR_Error) {
 29:                 throw new Horde_Exception($result);
 30:             }
 31:         }
 32:     }
 33: 
 34:     /**
 35:      */
 36:     function getDb()
 37:     {
 38:         $config = $GLOBALS['conf']['sql'];
 39:         unset($config['charset']);
 40:         return MDB2::factory($config);
 41:     }
 42: 
 43:     /**
 44:      * List folders.
 45:      *
 46:      * @return array  A list of folders.
 47:      */
 48:     function listFolders($perm = Horde_Perms::SHOW, $parent = null, $allLevels = true)
 49:     {
 50:         return $GLOBALS['trean_shares']->getFolders($GLOBALS['registry']->getAuth(), $perm, $parent, $allLevels);
 51:     }
 52: 
 53:     /**
 54:      * Counts the number of current user's folders list from storage.
 55:      *
 56:      * @param integer $perm       The level of permissions to require for a
 57:      *                            folder to return it.
 58:      *
 59:      * @return integer  The number of matching folders.
 60:      */
 61:     function countFolders($perm = Horde_Perms::SHOW)
 62:     {
 63:         $folders = $GLOBALS['trean_shares']->listFolders($GLOBALS['registry']->getAuth(), $perm);
 64:         if (is_a($folders, 'PEAR_Error')) {
 65:             $GLOBALS['notification']->push(sprintf(_("An error occurred counting folders: %s"), $folders->getMessage()), 'horde.error');
 66:             return 0;
 67:         }
 68:         return count($folders);
 69:     }
 70: 
 71:     /**
 72:      * Generates the body of a &lt;select&gt; form input to select a
 73:      * folder. The &lt;select&gt; and &lt;/select&gt; tags are NOT included
 74:      * in the output of this function.
 75:      *
 76:      * @param string $selected  The folder to have selected by default.
 77:      *                          Defaults to the first option in the list.
 78:      *
 79:      * @return string  A string containing <option> elements for each folder
 80:      *                 in the list.
 81:      */
 82:     function folderSelect($selected = null, $perm = Horde_Perms::SHOW, $new = false)
 83:     {
 84:         // Default to the user's own main bookmarks.
 85:         if (is_null($selected)) {
 86:             $selected = $GLOBALS['registry']->getAuth();
 87:         }
 88: 
 89:         $folders = Trean::listFolders($perm);
 90:         if (is_a($folders, 'PEAR_Error')) {
 91:             $folders = array();
 92:         }
 93: 
 94:         $tree = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Tree')->create('folder_select', 'Select');
 95: 
 96:         foreach ($folders as $folder_name => $folder) {
 97:             /* Selected or not? */
 98:             $params['selected'] = ($folder->getId() == $selected || $folder_name == $selected);
 99: 
100:             /* Add the node and add the node params. */
101:             $tree->addNode($folder->getId(), $folder->getParent(), $folder->get('name'), substr_count($folder_name, ':'), true, $params);
102:         }
103: 
104:         $select = $new
105:             ? '<option value="">----</option><option value="*new*">' . _("New Folder") . '</option>'
106:             : '';
107: 
108:         return $select . $tree->renderTree();
109:     }
110: 
111:     /**
112:      */
113:     function sortOrder($sortby)
114:     {
115:         switch ($sortby) {
116:         case 'title':
117:             return 0;
118: 
119:         case 'rating':
120:         case 'clicks':
121:             return 1;
122:         }
123:     }
124: 
125:     /**
126:      * Returns the specified permission for the current user.
127:      *
128:      * @param string $permission  A permission, currently only 'max_folders'
129:      *                            and 'max_bookmarks'.
130:      *
131:      * @return mixed  The value of the specified permission.
132:      */
133:     function hasPermission($permission)
134:     {
135:         $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
136:         if (!$perms->exists('trean:' . $permission)) {
137:             return true;
138:         }
139: 
140:         $allowed = $perms->getPermissions('trean:' . $permission, $GLOBALS['registry']->getAuth());
141:         if (is_array($allowed)) {
142:             switch ($permission) {
143:             case 'max_folders':
144:             case 'max_bookmarks':
145:                 $allowed = max($allowed);
146:                 break;
147:             }
148:         }
149: 
150:         return $allowed;
151:     }
152: 
153:     /**
154:      * Returns the "Reason Phrase" associated with the given HTTP status code
155:      * according to rfc2616.
156:      */
157:     function HTTPStatus($status_code)
158:     {
159:         switch ($status_code) {
160:         case '100': return _("Continue");
161:         case '101': return _("Switching Protocols");
162:         case '200': return _("OK");
163:         case '201': return _("Created");
164:         case '202': return _("Accepted");
165:         case '203': return _("Non-Authoritative Information");
166:         case '204': return _("No Content");
167:         case '205': return _("Reset Content");
168:         case '206': return _("Partial Content");
169:         case '300': return _("Multiple Choices");
170:         case '301': return _("Moved Permanently");
171:         case '302': return _("Found");
172:         case '303': return _("See Other");
173:         case '304': return _("Not Modified");
174:         case '305': return _("Use Proxy");
175:         case '307': return _("Temporary Redirect");
176:         case '400': return _("Bad Request");
177:         case '401': return _("Unauthorized");
178:         case '402': return _("Payment Required");
179:         case '403': return _("Forbidden");
180:         case '404': return _("Not Found");
181:         case '405': return _("Method Not Allowed");
182:         case '406': return _("Not Acceptable");
183:         case '407': return _("Proxy Authentication Required");
184:         case '408': return _("Request Time-out");
185:         case '409': return _("Conflict");
186:         case '410': return _("Gone");
187:         case '411': return _("Length Required");
188:         case '412': return _("Precondition Failed");
189:         case '413': return _("Request Entity Too Large");
190:         case '414': return _("Request-URI Too Large");
191:         case '415': return _("Unsupported Media Type");
192:         case '416': return _("Requested range not satisfiable");
193:         case '417': return _("Expectation Failed");
194:         case '500': return _("Internal Server Error");
195:         case '501': return _("Not Implemented");
196:         case '502': return _("Bad Gateway");
197:         case '503': return _("Service Unavailable");
198:         case '504': return _("Gateway Time-out");
199:         case '505': return _("HTTP Version not supported");
200:         default: return '';
201:         }
202:     }
203: 
204:     /**
205:      * Returns an apropriate icon for the given bookmark.
206:      */
207:     function getFavicon($bookmark)
208:     {
209:         global $registry;
210: 
211:         // Initialize VFS.
212:         try {
213:             $vfs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create();
214:             if ($bookmark->favicon
215:                 && $vfs->exists('.horde/trean/favicons/', $bookmark->favicon)) {
216:                 return Horde_Util::addParameter(Horde::url('favicon.php'),
217:                                                 'bookmark_id', $bookmark->id);
218:             }
219:         } catch (Exception $e) {
220:         }
221: 
222:         // Default to the protocol icon.
223:         $protocol = substr($bookmark->url, 0, strpos($bookmark->url, '://'));
224:         return Horde_Themes::img('/protocol/' . (empty($protocol) ? 'http' : $protocol) . '.png');
225:     }
226: }
227: 
API documentation generated by ApiGen