Refactor: move common routines into separate methods
[cacert-mgr.git] / manager / application / Bootstrap.php
1 <?php
2 require_once('plugins/plugin.charsetheader.php');
3 require_once('plugins/plugin.forceauth.php');
4 require_once('plugins/plugin.loginlogout.php');
5 require_once('plugins/plugin.buildmenu.php');
6 require_once('config/Config.php');
7 require_once('log/Log.php');
8 require_once('l10n/L10n.php');
9 require_once('i18n/I18n.php');
10
11 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
12 protected function _initAutoload() {
13 $autoloader = new Zend_Application_Module_Autoloader(array(
14 'namespace' => 'Default_',
15 'basePath' => dirname(__FILE__)
16 ));
17 return $autoloader;
18 }
19
20 protected function _initPlugins() {
21 $this->bootstrap('session');
22
23 $fc = Zend_Controller_Front::getInstance();
24
25 $charset_header = new CharsetHeader();
26 $fc->registerPlugin($charset_header);
27
28 $force_auth = new ForceAuth();
29 $fc->registerPlugin($force_auth);
30
31 $buildmenu = new BuildMenu();
32 $fc->registerPlugin($buildmenu);
33
34 $loginlogout = new LoginLogout();
35 $fc->registerPlugin($loginlogout);
36 }
37
38 protected function _initDoctype() {
39 $this->bootstrap('view');
40 $this->bootstrap('log');
41 $this->bootstrap('I18n');
42 $this->bootstrap('session');
43
44 $view = $this->getResource('view');
45 Zend_Registry::set('view', $view);
46 $view->doctype('XHTML1_STRICT');
47 $view->addHelperPath(APPLICATION_PATH . '/views/helpers/');
48 $view->headTitle = I18n::_('CAcert Test Manager');
49 }
50
51 /**
52 * @todo expireSessionCookie()
53 * @todo rememberMe(xx)
54 * @todo forgetMe()
55 * @see Zend_Registry::get('session');
56 * @return Zend_Session_Namespace
57 */
58 protected function _initSession() {
59 $options = $this->getOption('ca_mgr');
60
61 $db = Zend_Db::factory($options['db']['session']['pdo'], $options['db']['session']);
62
63 /**
64 * automatically clean up expired session entries from session cache
65 * use the modified and lifetime stamps to calculate expire time
66 */
67 if ($options['db']['session']['autocleanup'] == '1') {
68 $stmt = $db->query('delete from front_session where (modified + lifetime * 2) < unix_timestamp()');
69 # $stmt->execute();
70 }
71
72 //you can either set the Zend_Db_Table default adapter
73 //or you can pass the db connection straight to the save handler $config
74 // @see lifetimeColumn / lifetime / overrideLifetime, lifetime defaults to php.ini: session.gc_maxlifetime
75 Zend_Db_Table_Abstract::setDefaultAdapter($db);
76 $config = array(
77 'name' => 'front_session',
78 'primary' => 'id',
79 'modifiedColumn' => 'modified',
80 'dataColumn' => 'data',
81 'lifetimeColumn' => 'lifetime'
82 );
83
84 //create your Zend_Session_SaveHandler_DbTable and
85 //set the save handler for Zend_Session
86 Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
87
88 // Zend_Session::rememberMe(7200);
89
90 //start your session!
91 Zend_Session::start();
92
93 $session = new Zend_Session_Namespace();
94 if (!isset($session->started))
95 $session->started = time();
96 if (!isset($session->authdata))
97 $session->authdata = array('authed' => false);
98
99 Zend_Registry::set('session', $session);
100 return $session;
101 }
102
103 /**
104 * get the basic system config from database, store the config object in the bootstrap registry
105 * @see Zend_Registry::get('config');
106 * @return Config
107 */
108 protected function _initConfig() {
109 $options = $this->getOption('ca_mgr');
110 $db = Zend_Db::factory($options['db']['config']['pdo'], $options['db']['config']);
111 $config = Config::getInstance(SYSTEM_CONFIG, $db);
112
113 Zend_Registry::set('config', $config);
114 Zend_Registry::set('config_dbc', $db);
115
116 return $config;
117 }
118
119 /**
120 * make singleton system logger
121 * @see Zend_Registry::get('log');
122 * @return Log
123 */
124 public function _initLog() {
125 $this->bootstrap('Config');
126
127 $op = $this->getOption('log');
128 $log = Log::getInstance(SYSTEM_LOG, $op['application']);
129
130 Zend_Registry::set('log', $log);
131 return $log;
132 }
133
134 /**
135 * make singleton I18n (internationalization) object (translation)
136 */
137 public function _initI18n() {
138 $this->bootstrap('Config');
139 // need existing L10n object for initialization
140 $this->bootstrap('L10n');
141
142 $I18n = I18n::getInstance(L10n::getInstance()->getLanguage());
143 }
144
145 /**
146 * make singleton L10n (localization) object (set locale, convert date and
147 * number formats)
148 */
149 public function _initL10n() {
150 $this->bootstrap('Config');
151
152 $L10n = L10n::getInstance();
153 }
154 }