Overview

Packages

  • Chora
  • None

Classes

  • Chora_Application
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Chora application API.
  4:  *
  5:  * This file defines Horde's core API interface. Other core Horde libraries
  6:  * can interact with Chora through this API.
  7:  *
  8:  * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  9:  *
 10:  * See the enclosed file COPYING for license information (GPL). If you
 11:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 12:  *
 13:  * @package Chora
 14:  */
 15: 
 16: if (!defined('CHORA_BASE')) {
 17:     define('CHORA_BASE', dirname(__FILE__) . '/..');
 18: }
 19: 
 20: if (!defined('HORDE_BASE')) {
 21:     /* If horde does not live directly under the app directory, the HORDE_BASE
 22:      * constant should be defined in config/horde.local.php. */
 23:     if (file_exists(CHORA_BASE . '/config/horde.local.php')) {
 24:         include CHORA_BASE . '/config/horde.local.php';
 25:     } else {
 26:         define('HORDE_BASE', CHORA_BASE . '/..');
 27:     }
 28: }
 29: 
 30: /* Load the Horde Framework core (needed to autoload
 31:  * Horde_Registry_Application::). */
 32: require_once HORDE_BASE . '/lib/core.php';
 33: 
 34: class Chora_Application extends Horde_Registry_Application
 35: {
 36:     /**
 37:      */
 38:     public $version = 'H4 (3.0-git)';
 39: 
 40:     /**
 41:      * Global variables defined:
 42:      *   $chora_conf
 43:      *   $sourceroots
 44:      */
 45:     protected function _init()
 46:     {
 47:         global $acts, $conf, $defaultActs, $where, $atdir, $fullname, $sourceroot;
 48: 
 49:         try {
 50:             $GLOBALS['sourceroots'] = Horde::loadConfiguration('backends.php', 'sourceroots');
 51:         } catch (Horde_Exception $e) {
 52:             $GLOBALS['sourceroots'] = array();
 53:             // If chora isn't fully/properly setup, _init() will throw fatal
 54:             // errors. Don't want that if this class is being loaded simply to
 55:             // obtain basic chora application information.
 56:             if ($GLOBALS['registry']->initialApp != 'chora') {
 57:                 return;
 58:             }
 59:             $GLOBALS['notification']->push($e);
 60:         }
 61: 
 62:         $sourceroots = Chora::sourceroots();
 63: 
 64:         /**
 65:          * Variables we wish to propagate across web pages
 66:          *  ha  = Hide Attic Files
 67:          *  ord = Sort order
 68:          *  sbt = Sort By Type (name, age, author, etc)
 69:          *
 70:          * Obviously, defaults go into $defaultActs :)
 71:          * TODO: defaults of 1 will not get propagated correctly - avsm
 72:          * XXX: Rewrite this propagation code, since it sucks - avsm
 73:          */
 74:         $defaultActs = $acts = array(
 75:             'onb' => 0,
 76:             'ord' => Horde_Vcs::SORT_ASCENDING,
 77:             'rev' => 0,
 78:             'rt'  => null,
 79:             'sa'  => 0,
 80:             'sbt' => constant($conf['options']['defaultsort']),
 81:             'ws'  => 1,
 82:         );
 83: 
 84:         /* See if any actions have been passed as form variables, and if so,
 85:          * assign them into the acts array. */
 86:         $vars = Horde_Variables::getDefaultVariables();
 87:         foreach (array_keys($acts) as $key) {
 88:             if (isset($vars->$key)) {
 89:                 $acts[$key] = $vars->$key;
 90:             }
 91:         }
 92: 
 93:         /* Use the value of the 'rt' form value for the sourceroot. If not
 94:          * present, use the last sourceroot used as the default value if the
 95:          * user has that preference. Otherwise, use default sourceroot. */
 96:         if (is_null($acts['rt'])) {
 97:             $last_sourceroot = $GLOBALS['prefs']->getValue('last_sourceroot');
 98: 
 99:             if (!empty($last_sourceroot) &&
100:                 !empty($sourceroots[$last_sourceroot]) &&
101:                 is_array($sourceroots[$last_sourceroot])) {
102:                 $acts['rt'] = $last_sourceroot;
103:             } else {
104:                 foreach ($sourceroots as $key => $val) {
105:                     if (!isset($acts['rt']) || isset($val['default'])) {
106:                         $acts['rt'] = $key;
107:                         break;
108:                     }
109:                 }
110: 
111:                 if (is_null($acts['rt'])) {
112:                     Chora::fatal(new Chora_Exception(_("No repositories found.")));
113:                 }
114:             }
115:         }
116: 
117:         if (!isset($sourceroots[$acts['rt']])) {
118:             Chora::fatal(new Chora_Exception(sprintf(_("The repository with the slug '%s' was not found"), $acts['rt'])));
119:         }
120: 
121:         $sourcerootopts = $sourceroots[$acts['rt']];
122:         $sourceroot = $acts['rt'];
123: 
124:         // Cache.
125:         $cache = empty($conf['caching'])
126:             ? null
127:             : $GLOBALS['injector']->getInstance('Horde_Cache');
128: 
129:         $GLOBALS['chora_conf'] = array(
130:             'cvsusers' => $sourcerootopts['location'] . '/' . (isset($sourcerootopts['cvsusers']) ? $sourcerootopts['cvsusers'] : ''),
131:             'introText' => CHORA_BASE . '/config/' . (isset($sourcerootopts['intro']) ? $sourcerootopts['intro'] : ''),
132:             'introTitle' => (isset($sourcerootopts['title']) ? $sourcerootopts['title'] : ''),
133:             'sourceRootName' => $sourcerootopts['name']
134:         );
135:         $chora_conf = &$GLOBALS['chora_conf'];
136: 
137:         $GLOBALS['VC'] = Horde_Vcs::factory(Horde_String::ucfirst($sourcerootopts['type']), array(
138:             'cache' => $cache,
139:             'sourceroot' => $sourcerootopts['location'],
140:             'paths' => array_merge($conf['paths'], array('temp' => Horde::getTempDir())),
141:             'username' => isset($sourcerootopts['username']) ? $sourcerootopts['username'] : '',
142:             'password' => isset($sourcerootopts['password']) ? $sourcerootopts['password'] : ''
143:         ));
144: 
145:         $where = Horde_Util::getFormData('f', '/');
146: 
147:         /* Location relative to the sourceroot. */
148:         $where = preg_replace(array('|^/|', '|\.\.|'), '', $where);
149: 
150:         /* Store last repository viewed */
151:         $GLOBALS['prefs']->setValue('last_sourceroot', $acts['rt']);
152: 
153:         $fullname = $sourcerootopts['location'] . (substr($sourcerootopts['location'], -1) == '/' ? '' : '/') . $where;
154: 
155:         if ($sourcerootopts['type'] == 'cvs') {
156:             $fullname = preg_replace('|/$|', '', $fullname);
157:             $atdir = @is_dir($fullname);
158:         } else {
159:             $atdir = !$where || (substr($where, -1) == '/');
160:         }
161:         $where = preg_replace('|/$|', '', $where);
162: 
163:         if (($sourcerootopts['type'] == 'cvs') &&
164:             !@is_dir($sourcerootopts['location'])) {
165:             Chora::fatal(new Chora_Exception(_("Sourceroot not found. This could be a misconfiguration by the server administrator, or the server could be having temporary problems. Please try again later.")));
166:         }
167: 
168:         if (Chora::isRestricted($where)) {
169:             Chora::fatal(new Chora_Exception(sprintf(_("%s: Forbidden by server configuration"), $where)));
170:         }
171:     }
172: 
173:     /**
174:      */
175:     public function perms()
176:     {
177:         $perms = array(
178:             'sourceroots' => array(
179:                 'title' => _("Repositories")
180:             )
181:         );
182: 
183:         // Run through every source repository
184:         require dirname(__FILE__) . '/../config/backends.php';
185:         foreach ($sourceroots as $sourceroot => $srconfig) {
186:             $perms['sourceroots:' . $sourceroot] = array(
187:                 'title' => $srconfig['name']
188:             );
189:         }
190: 
191:         return $perms;
192:     }
193: 
194:     /**
195:      */
196:     public function menu($menu)
197:     {
198:         $menu->add(Chora::url('browsedir'), _("_Browse"), 'chora.png');
199:     }
200: 
201:     /* Sidebar method. */
202: 
203:     /**
204:      */
205:     public function sidebarCreate(Horde_Tree_Base $tree, $parent = null,
206:                                   array $params = array())
207:     {
208:         asort($GLOBALS['sourceroots']);
209: 
210:         foreach ($GLOBALS['sourceroots'] as $key => $val) {
211:             if (Chora::checkPerms($key)) {
212:                 $tree->addNode($parent . $key,
213:                     $parent,
214:                     $val['name'],
215:                     1,
216:                     false,
217:                     array(
218:                         'icon' => Horde_Themes::img('tree/folder.png'),
219:                         'url' => Chora::url('browsedir', '', array('rt' => $key))->setRaw(true)
220:                     )
221:                 );
222:             }
223:         }
224:     }
225: 
226: }
227: 
API documentation generated by ApiGen