startup.php 13.8 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
Laurent's avatar
Laurent committed
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 21
 */

22
if (!function_exists('xdebug_break')) {
Patrick Barroca's avatar
Patrick Barroca committed
23
  function xdebug_break(){};
24 25 26
}


27
function defineConstant($name, $value) {
Patrick Barroca's avatar
Patrick Barroca committed
28 29
  if (!defined($name))
    define($name, $value);
30 31 32
}


33 34 35 36 37 38
class Bokeh_Engine {
  protected
    $_config,
    $_front_controller;

  public function powerOn() {
39 40
    return
      $this
41 42 43 44 45 46 47 48 49 50 51 52
      ->warmUp()
      ->setupDevOptions()
      ->setupControllerActionHelper()
      ->setupHTTPClient()
      ->setupCache()
      ->setupMail()
      ->setupFrontController()
      ->setupPagination()
      ->setupSearch()
      ->setupDigitalResources()
      ->setupCustomFields();
  }
53

54

55 56 57 58
  public function getFrontController() {
    return $this->_front_controller;
  }

59

60 61
  function warmUp() {
    require_once('Class/Url.php');
62

63
    defineConstant('BASE_URL',  Class_Url::baseUrl());
64

65 66
    require_once "Zend/Loader.php";
    Zend_Loader::registerAutoload();
67

68 69
    $this->setupConstants();
    require_once('requires.php');
70

71 72 73 74
    $this
      ->loadConfig()
      ->setupSession()
      ->setupDatabase();
75

76
    Class_AdminVar::findAll();
77

78 79
    return $this->setupLanguage();
  }
80

81

82
  function setupConstants() {
Ghislain Loas's avatar
Ghislain Loas committed
83
    defineConstant('BOKEH_MAJOR_VERSION','7.12');
Patrick Barroca's avatar
Patrick Barroca committed
84
    defineConstant('BOKEH_RELEASE_NUMBER', BOKEH_MAJOR_VERSION . '.56');
Laurent's avatar
Laurent committed
85

86
    defineConstant('BOKEH_REMOTE_FILES', 'http://git.afi-sa.fr/afi/opacce/');
87

88
    defineConstant('ROOT_PATH',  realpath(dirname(__FILE__).'/..').'/');
89

90
    defineConstant('ZEND_FRAMEWORK_PATH',  ROOT_PATH . 'library/storm/zf/library/');
Laurent's avatar
Laurent committed
91

92 93
    defineConstant('MODULEDIRECTORY', ROOT_PATH . 'application/modules');
    defineConstant('LANG_DIR', ROOT_PATH . 'library/translation/');
94

95 96 97 98
    defineConstant('USERFILES', 'userfiles');
    defineConstant('USERFILESPATH', './' . USERFILES);
    defineConstant('USERFILESURL', BASE_URL . '/' . USERFILES . '/');

99 100
    defineConstant('SKINS', 'skins');

101
    defineConstant('PATH_TEMP',  ROOT_PATH . 'temp/');
102

103 104
    defineConstant('CKBASEPATH',  'ckeditor/');
    defineConstant('CKBASEURL',  BASE_URL . '/ckeditor/');
105

106
    defineConstant('AMBERURL',  BASE_URL . '/amber/');
107

108 109 110 111
    defineConstant('URL_ADMIN_SKIN', BASE_URL . '/public/admin/skins/');
    defineConstant('URL_ADMIN_CSS', BASE_URL . '/public/admin/css/');
    defineConstant('URL_ADMIN_IMG', BASE_URL . '/public/admin/images/');
    defineConstant('URL_ADMIN_JS', BASE_URL . '/public/admin/js/');
112

113
    defineConstant('JQUERY', URL_ADMIN_JS . 'jquery-3.2.1.min.js');
114
    defineConstant('JQUERYMOBILE_VERSION',  '1.4.5');
115 116 117
    defineConstant('JQUERYUI', URL_ADMIN_JS . 'jquery-ui-1.12.1/jquery-ui.min.js');
    defineConstant('JQUERYUI_CSS', URL_ADMIN_JS.'jquery-ui-1.12.1/jquery-ui.min.css');

118 119 120
    defineConstant("URL_JS", BASE_URL . "/public/opac/js/");
    defineConstant("URL_EPUB", BASE_URL . "/temp/epub/");
    defineConstant("URL_SHARED_IMG", BASE_URL . "/public/opac/images/");
121

122 123 124 125 126
    // il y a des autre define URL dans ZendAfi_Controller_Plugin_DefineURLs
    // par exemple URL_IMG, URL_CSS, URL_HTML et URL_JS va chercher dans 'URL_SKIN . 'nom de le module' . /html' etc.
    defineConstant('BR','<br />');
    defineConstant('NL',"\n");
    defineConstant('CRLF', chr(13).chr(10));
127

128 129 130
    defineConstant('URL_JAVA', BASE_URL . '/public/opac/java/');
    defineConstant('PATH_JAVA', ROOT_PATH . 'public/opac/java/');
    defineConstant('PATH_ADMIN_SUPPORTS', ROOT_PATH . 'public/admin/images/supports/');
Patrick Barroca's avatar
Patrick Barroca committed
131

132 133 134 135 136 137 138
    defineConstant('PATH_FONTS', ROOT_PATH . 'public/opac/fonts/');
    defineConstant('URL_CAPTCHA', BASE_URL . '/temp/');
    defineConstant('PATH_CAPTCHA', PATH_TEMP);
    defineConstant('CACHE_LIFETIME', 3600);
    defineConstant('MEMCACHED_ENABLE', false);
    defineConstant('MEMCACHED_HOST', 'localhost');
    defineConstant('MEMCACHED_PORT', '11211');
Patrick Barroca's avatar
Patrick Barroca committed
139

140
    defineConstant('IMAGE_MAGICK_PATH', 'convert');
Patrick Barroca's avatar
Patrick Barroca committed
141

142 143 144
    defineConstant('PATCH_PATH', ROOT_PATH . '/cosmogramme/sql/patch/');
    return $this;
  }
145 146


147 148 149 150 151 152 153 154
  public function loadConfig($config_init_file_path = './config.ini') {
    // load configuration (local ou production)
    if(array_isset('REMOTE_ADDR', $_SERVER) and $_SERVER['REMOTE_ADDR'] == '127.0.0.1')
      $serveur='local';
    else
      $serveur='production';
    $this->_config = new Zend_Config_Ini($config_init_file_path, $serveur);
    Zend_Registry::set('cfg', $this->_config);
155

156 157 158
    $locale = $this->_config->locale
      ? $this->_config->locale
      : 'fr_FR.UTF-8';
159

160
    setlocale(LC_TIME, $locale);
161
    date_default_timezone_set($this->_config->timeZone);
Patrick Barroca's avatar
Patrick Barroca committed
162

163
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
164 165
  }

166

167 168 169 170
  public function setupCache() {
    $frontendOptions = ['lifetime' => CACHE_LIFETIME, // durée du cache: 1h
                        'automatic_serialization' => false,
                        'caching' => true];
171

172 173 174 175 176
    $use_memcached = (MEMCACHED_ENABLE === true);
    $backendOptions = $use_memcached
      ? ['servers' => [ ['host' => MEMCACHED_HOST,
                         'port' => MEMCACHED_PORT] ]]
      : ['cache_dir' => PATH_TEMP ];
177

178 179 180 181 182
    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core',
                                 $use_memcached ? 'Memcached' : 'File',
                                 $frontendOptions,
                                 $backendOptions);
183

184 185 186 187
    Storm_Cache::setDefaultZendCache($cache);
    Storm_Cache::setSeed($this->_config->sgbd->config->dbname.md5(BASE_URL));
    return $this;
  }
188 189


190 191 192 193 194 195 196 197 198 199 200 201
  function setupSession() {
    // Start Session
    $session = new Zend_Session_Namespace(md5(BASE_URL));
    if (!isset($session->initialized))
      {
        Zend_Session::regenerateId();
        $session->initialized = true;
      }
    if (!isset($session->baseUrl)) $session->baseUrl = BASE_URL;
    Zend_Registry::set('session', $session);
    return $this;
  }
202 203


204
  function setupLanguage() {
205
    $langue = Class_AdminVar::getDefaultLanguage();
206 207
    ZendAfi_Locale::setDefault($langue);
    Zend_Registry::set('locale', new ZendAfi_Locale());
208

209 210 211 212
    $translate = new ZendAfi_Translate('gettext',
                                       LANG_DIR . $langue . '.mo',
                                       $langue);

213
    foreach (Class_AdminVar::getLanguesWithoutDefault() as $language) {
214
      $filename = LANG_DIR . $language.'.mo';
215 216 217 218
      if (file_exists($filename))
        $translate->addTranslation($filename, $language);
    }
    Zend_Registry::set('translate', $translate);
219

220 221 222
    Zend_Validate_Abstract::setDefaultTranslator($translate);
    return $this;
  }
223

224

225 226 227 228 229
  function setupDatabase() {
    // setup database
    $sql = Zend_Db::factory($this->_config->sgbd->adapter, $this->_config->sgbd->config->toArray());
    $sql->usePrepared(false);
    Zend_Db_Table::setDefaultAdapter($sql);
Patrick Barroca's avatar
Patrick Barroca committed
230

231 232 233
    $afi_sql = new Class_Systeme_Sql();
    Zend_Registry::set('sql', $afi_sql);
    Zend_Db_Table::getDefaultAdapter()->query('set names "UTF8"');
234
    Zend_Db_Table::getDefaultAdapter()->query('set SQL_MODE = ""');
235
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
236 237
  }

238

239 240 241 242 243 244
  function setupDevOptions() {
    //permet d'activer les fonctions en développement
    if (null !== $experimental_dev = $this->_config->get('experimental_dev'))
      defineConstant('DEVELOPMENT', $experimental_dev);
    return $this;
  }
245 246


247 248 249 250 251
  function setupControllerActionHelper() {
    Zend_Controller_Action_HelperBroker::resetHelpers();
    Zend_Controller_Action_HelperBroker::addHelper(new ZendAfi_Controller_Action_Helper_ViewRenderer());
    Zend_Controller_Action_HelperBroker::addPrefix('ZendAfi_Controller_Action_Helper');
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
252 253
  }

254

255
  function setupHTTPClient() {
256

257 258 259 260 261 262 263 264 265
    if ( (isset ($this->_config->proxy->host) ) && ($this->_config->proxy->host != '') ){
      //set up HTTP Client to use proxy settings
      $proxy_config = [
                       'adapter'    => 'Zend_Http_Client_Adapter_Proxy',
                       'proxy_host' => $this->_config->proxy->host,
                       'proxy_port' => $this->_config->proxy->port,
                       'proxy_user' => $this->_config->proxy->user,
                       'proxy_pass' => $this->_config->proxy->pass ];
      Zend_Registry::set('http_proxy',$proxy_config);
266

267 268
      $proxy_adapter = new Zend_Http_Client_Adapter_Proxy();
      $proxy_adapter->setConfig($proxy_config);
efalcy's avatar
efalcy committed
269
      Class_HttpClientFactory::setAdapter($proxy_adapter);
270
    }
llaffont's avatar
llaffont committed
271

272 273
    return $this;
  }
Patrick Barroca's avatar
Patrick Barroca committed
274 275


276 277 278 279 280 281
  function setupMail() {
    if (isset($this->_config->mail->transport->smtp)) {
      ZendAfi_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp($this->_config->mail->transport->smtp->host,
                                                                     $this->_config->mail->transport->smtp->toArray()));
      return $this;
    }
282

283 284 285 286
    if (defined('SMTP_HOST'))
      ZendAfi_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp(SMTP_HOST));
    return $this;
  }
287

Patrick Barroca's avatar
Patrick Barroca committed
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  function newFrontController() {
    return Zend_Controller_Front::getInstance()
      ->setDispatcher(new ZendAfi_Controller_Dispatcher_Standard())
      ->addModuleDirectory(MODULEDIRECTORY)
      ->setDefaultModule('opac')
      ->registerPlugin(new ZendAfi_Controller_Plugin_SetupDomain())
      ->registerPlugin(new ZendAfi_Controller_Plugin_AdminAuth())
      ->registerPlugin(new ZendAfi_Controller_Plugin_SetupLocale())
      ->registerPlugin(new ZendAfi_Controller_Plugin_DefineURLs())
      ->registerPlugin(new ZendAfi_Controller_Plugin_InitModule())
      ->registerPlugin(new ZendAfi_Controller_Plugin_System())
      ->registerPlugin(new ZendAfi_Controller_Plugin_Popup())
      ->registerPlugin(new ZendAfi_Controller_Plugin_TogetherJS())
      ->registerPlugin(new ZendAfi_Controller_Plugin_Lectura())
      ->registerPlugin(new ZendAfi_Controller_Plugin_InspectorGadget())
      ->registerPlugin(new ZendAfi_Controller_Plugin_CnilConsent())
      ->registerPlugin(new ZendAfi_Controller_Plugin_Redmine())
306
      ->registerPlugin(new ZendAfi_Controller_Plugin_PhpParser())
307
      ->registerPlugin(new ZendAfi_Controller_Plugin_FeaturesTracking())
308
      ->setParam('useDefaultControllerAlways', false);
Patrick Barroca's avatar
Patrick Barroca committed
309 310
  }

311

312 313 314 315 316 317 318 319 320 321
  function setupFrontController() {
    $this->_front_controller = $this
      ->newFrontController()
      ->setBaseURL(BASE_URL);

    $this->setupRoutes($this->_front_controller, $this->_config);

    if (!Class_Users::isCurrentUserSuperAdmin())
      return $this;

322
    $this->_front_controller->registerPlugin(new ZendAfi_Controller_Plugin_XHProfile());
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    return $this;

    set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_PATH.'library/ZFDebug/library');
    $this->_front_controller
      ->registerPlugin(new ZFDebug_Controller_Plugin_Debug(['plugins' =>
                                                            [
                                                             'Variables',
                                                             'Constants',
                                                             'Html',
                                                             'Database',
                                                             'Memory',
                                                             'Time',
                                                             'Exception']
                                                            ]
                                                           ));
  }
339

340

341
  function setupRoutes($front_controller, $cfg) {
342 343
    if ('1' == $cfg->get('enable_rewriting')) {
      $front_controller
344 345
        ->setBaseUrl('')
        ->setRouter(new ZendAfi_Controller_Router_RewriteWithoutBaseUrl());
346

347 348
      Class_Url::doNotAddBaseUrl();
    }
349

350
    $front_controller
351 352 353 354 355 356 357 358 359 360 361
      ->getRouter()
      ->addRoute('embed',
                 new Zend_Controller_Router_Route(
                                                  'embed/:controller/:action/*',
                                                  ['module' => 'telephone',
                                                   'controller' => 'index',
                                                   'action' => 'index']))
      ->addRoute('sitemap',
                 new Zend_Controller_Router_Route_Static('sitemap.xml',
                                                         ['module' => 'opac',
                                                          'controller' => 'index',
362 363 364 365
                                                          'action' => 'sitemap']))
      ->addRoute('filemanager',
                 new ZendAfi_Controller_Router_Route_QueryString('admin/file-manager',
                                                                 ['module' => 'admin',
366
                                                                  'controller' => 'file-manager']));
367
    return $this;
368
  }
369

Patrick Barroca's avatar
Patrick Barroca committed
370

371 372 373 374 375
  function setupPagination() {
    Zend_Paginator::setDefaultScrollingStyle('Sliding');
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    return $this;
  }
Patrick Barroca's avatar
Patrick Barroca committed
376

377

378 379 380 381 382
  function setupSearch() {
    defineConstant('MAX_SEARCH_RESULTS', '');
    Class_CriteresRecherche::setMaxSearchResults(MAX_SEARCH_RESULTS);
    return $this;
  }
383

384 385

  function setupDigitalResources() {
386
    Class_DigitalResource::getInstance()->bootstrap($this->_front_controller);
387 388 389 390 391 392 393 394
    return $this;
  }


  function setupCustomFields() {
    Class_CustomField_Model::registerDefault();
    return $this;
  }
Laurent's avatar
Laurent committed
395 396 397
}


398
function setupOpac() {
399 400 401
  return (new Bokeh_Engine())
    ->powerOn()
    ->getFrontController();
Patrick Barroca's avatar
Patrick Barroca committed
402
}