7 * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
8 * @copyright Copyright (c) 2007-2016 Metaways Infosystems GmbH (http://www.metaways.de)
9 * @author Philipp Schuele <p.schuele@metaways.de>
14 * dispatcher and initialisation class (functions are static)
15 * - dispatchRequest() function
16 * - initXYZ() functions
17 * - has registry and config
21 class Setup_Core extends Tinebase_Core
24 * constant for config registry index
27 const CHECKDB = 'checkDB';
30 * init setup framework
32 public static function initFramework()
34 Setup_Core::setupConfig();
36 Setup_Core::setupTempDir();
38 //Database Connection must be setup before cache because setupCache uses constant "SQL_TABLE_PREFIX"
39 Setup_Core::setupDatabaseConnection();
41 Setup_Core::setupStreamWrapper();
43 //Cache must be setup before User Locale because otherwise Zend_Locale tries to setup
44 //its own cache handler which might result in a open_basedir restriction depending on the php.ini settings
45 Setup_Core::setupCache();
47 Setup_Core::setupBuildConstants();
49 // setup a temporary user locale/timezone. This will be overwritten later but we
50 // need to handle exceptions during initialisation process such as seesion timeout
51 Setup_Core::set('locale', new Zend_Locale('en_US'));
52 Setup_Core::set(Tinebase_Core::USERTIMEZONE, 'UTC');
54 Setup_Core::setupUserLocale();
56 header('X-API: http://www.tine20.org/apidocs/tine20/');
62 * TODO remove redundancy with Tinebase_Core::startCoreSession()
64 public static function startSetupSession ()
66 Tinebase_Session::setSessionBackend();
68 Zend_Session::start();
70 $setupSession = Setup_Session::getSessionNamespace();
72 if (isset($setupSession->setupuser)) {
73 self::set(self::USER, $setupSession->setupuser);
76 if (!isset($setupSession->jsonKey)) {
77 $setupSession->jsonKey = Tinebase_Record_Abstract::generateUID();
79 self::set('jsonKey', $setupSession->jsonKey);
85 * @see Tinebase_Core::dispatchRequest()
87 public static function dispatchRequest()
89 $request = new \Zend\Http\PhpEnvironment\Request();
90 self::set(self::REQUEST, $request);
94 /**************************** JSON API *****************************/
95 if ( (isset($_SERVER['HTTP_X_TINE20_REQUEST_TYPE']) && $_SERVER['HTTP_X_TINE20_REQUEST_TYPE'] == 'JSON') ||
96 (isset($_POST['requestType']) && $_POST['requestType'] == 'JSON')
98 $server = new Setup_Server_Json();
100 /**************************** CLI API *****************************/
101 } elseif (php_sapi_name() == 'cli') {
102 $server = new Setup_Server_Cli();
104 /**************************** HTTP API ****************************/
106 $server = new Setup_Server_Http();
113 * setups global config
115 * NOTE a config object will be instantiated regardless of the existance of
120 public static function setupConfig()
122 if(self::configFileExists()) {
123 $config = new Zend_Config(require self::getConfigFilePath());
125 $config = new Zend_Config(array());
127 self::set(self::CONFIG, $config);
131 * checks if global config file exists
135 public static function configFileExists()
137 return (bool)self::getConfigFilePath();
141 * Searches for config.inc.php in include paths and returnes the first match
145 public static function getConfigFilePath()
147 $includePaths = explode(PATH_SEPARATOR, get_include_path());
148 foreach ($includePaths as $includePath) {
149 $path = $includePath . '/config.inc.php';
150 if (file_exists($path)) {
159 * checks if global config file or tine root is writable
163 public static function configFileWritable()
165 if (self::configFileExists()) {
166 $configFilePath = self::getConfigFilePath();
167 return is_writable($configFilePath);
169 $path = dirname(dirname(__FILE__));
170 $testfilename = $path . DIRECTORY_SEPARATOR . uniqid(mt_rand()).'.tmp';
171 if (!($f = @fopen($testfilename, 'w'))) {
172 error_log(__METHOD__ . '::' . __LINE__ . ' Your tine root dir ' . $path . ' is not writable for the webserver! Config file can\'t be created.');
176 unlink($testfilename);
182 * initializes the database connection
186 * @todo try to write to db, if it fails: self::set(Setup_Core::CHECKDB, FALSE);
188 public static function setupDatabaseConnection()
192 // check database first
193 if (self::configFileExists()) {
194 $dbConfig = Tinebase_Core::getConfig()->database;
196 if ($dbConfig->adapter === self::PDO_MYSQL && (! defined(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY) || ! defined(PDO::MYSQL_ATTR_INIT_COMMAND))) {
197 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
198 . ' MySQL PDO constants not defined.');
203 parent::setupDatabaseConnection();
205 $serverVersion = self::getDb()->getServerVersion();
207 switch ($dbConfig->adapter) {
208 case self::PDO_MYSQL:
209 if (version_compare(self::MYSQL_MINIMAL_VERSION, $serverVersion, '<')) {
212 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
213 . ' MySQL server version incompatible! ' . $serverVersion
214 . ' < ' . self::MYSQL_MINIMAL_VERSION
221 if (version_compare(self::ORACLE_MINIMAL_VERSION, $serverVersion, '<')) {
222 self::set(Setup_Core::CHECKDB, TRUE);
224 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
225 . ' Oracle server version incompatible! ' . $serverVersion
226 . ' < ' . self::ORACLE_MINIMAL_VERSION
233 case self::PDO_PGSQL:
234 if (version_compare(self::PGSQL_MINIMAL_VERSION, $serverVersion, '<')) {
237 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
238 . ' PostgreSQL server version incompatible! ' . $serverVersion
239 . ' < ' . self::PGSQL_MINIMAL_VERSION
245 // @todo check version requirements for other db adapters
250 } catch (Zend_Db_Adapter_Exception $zae) {
251 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ' . $zae->getMessage());
252 } catch (Zend_Db_Exception $zde) {
253 Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ' . $zde->getMessage());
257 self::set(Setup_Core::CHECKDB, $dbcheck);
264 * NOTE: if no logger is configured, we write to stderr in setup
266 * @param $_defaultWriter Zend_Log_Writer_Abstract default log writer
268 public static function setupLogger(Zend_Log_Writer_Abstract $_defaultWriter = NULL)
270 $writer = new Zend_Log_Writer_Stream('php://stderr');
271 parent::setupLogger($writer);
275 * initializes the build constants like buildtype, package information, ...
277 public static function setupBuildConstants()
279 $config = self::getConfig();
280 define('TINE20_BUILDTYPE', strtoupper($config->get('buildtype', 'DEVELOPMENT')));
281 define('TINE20SETUP_CODENAME', Tinebase_Helper::getDevelopmentRevision());
282 define('TINE20SETUP_PACKAGESTRING', 'none');
283 define('TINE20SETUP_RELEASETIME', 'none');
287 * setup the cache and add it to zend registry
289 * Ignores {@param $_enabled} and always sets it to false
292 public static function setupCache($_enabled = true)
294 // disable caching for setup
295 parent::setupCache(false);
299 * returns TRUE if doctrine is available for model config v2 stuff
303 public static function isDoctrineAvailable()
305 return PHP_VERSION_ID >= 50500 && interface_exists('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');