startup.php 14.4 KB
Newer Older
llaffont's avatar
llaffont committed
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
llaffont's avatar
llaffont committed
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,
llaffont's avatar
llaffont committed
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
6.49.2  
Laurent committed
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
llaffont's avatar
llaffont committed
20 21
 */

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


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


Laurent's avatar
Laurent committed
33 34 35 36 37 38
class Bokeh_Engine {
  protected
    $_config,
    $_front_controller;

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

55

Laurent's avatar
Laurent committed
56 57 58 59
  public function getFrontController() {
    return $this->_front_controller;
  }

60

Laurent's avatar
Laurent committed
61 62
  function warmUp() {
    require_once('Class/Url.php');
63

Laurent's avatar
Laurent committed
64
    defineConstant('BASE_URL',  Class_Url::baseUrl());
65

Laurent's avatar
Laurent committed
66 67
    require_once "Zend/Loader.php";
    Zend_Loader::registerAutoload();
llaffont's avatar
llaffont committed
68

Laurent's avatar
Laurent committed
69 70
    $this->setupConstants();
    require_once('requires.php');
llaffont's avatar
llaffont committed
71

Laurent's avatar
Laurent committed
72 73 74 75
    $this
      ->loadConfig()
      ->setupSession()
      ->setupDatabase();
llaffont's avatar
llaffont committed
76

Laurent's avatar
Laurent committed
77
    Class_AdminVar::findAll();
llaffont's avatar
llaffont committed
78

Laurent's avatar
Laurent committed
79 80
    return $this->setupLanguage();
  }
llaffont's avatar
llaffont committed
81

82

Laurent's avatar
Laurent committed
83
  function setupConstants() {
Ghislain Loas's avatar
Ghislain Loas committed
84
    defineConstant('BOKEH_MAJOR_VERSION','7.12');
Ghislain Loas's avatar
Ghislain Loas committed
85
    defineConstant('BOKEH_RELEASE_NUMBER', BOKEH_MAJOR_VERSION . '.2');
Laurent's avatar
6.49.2  
Laurent committed
86

Laurent's avatar
Laurent committed
87
    defineConstant('BOKEH_REMOTE_FILES', 'http://git.afi-sa.fr/afi/opacce/');
llaffont's avatar
llaffont committed
88

Laurent's avatar
Laurent committed
89
    defineConstant('BOKEH_VERSION', (substr(BOKEH_MAJOR_VERSION, -1) % 2) ? 'master' : 'stable');
llaffont's avatar
llaffont committed
90

Laurent's avatar
Laurent committed
91
    defineConstant('ROOT_PATH',  realpath(dirname(__FILE__).'/..').'/');
llaffont's avatar
llaffont committed
92

Laurent's avatar
Laurent committed
93
    defineConstant('ZEND_FRAMEWORK_PATH',  ROOT_PATH . 'library/storm/zf/library/');
Laurent's avatar
6.49.2  
Laurent committed
94

Laurent's avatar
Laurent committed
95 96
    defineConstant('MODULEDIRECTORY', ROOT_PATH . 'application/modules');
    defineConstant('LANG_DIR', ROOT_PATH . 'library/translation/');
llaffont's avatar
llaffont committed
97

98 99 100 101
    defineConstant('USERFILES', 'userfiles');
    defineConstant('USERFILESPATH', './' . USERFILES);
    defineConstant('USERFILESURL', BASE_URL . '/' . USERFILES . '/');

Laurent's avatar
Laurent committed
102
    defineConstant('PATH_TEMP',  ROOT_PATH . 'temp/');
llaffont's avatar
llaffont committed
103

Laurent's avatar
Laurent committed
104 105
    defineConstant('CKBASEPATH',  'ckeditor/');
    defineConstant('CKBASEURL',  BASE_URL . '/ckeditor/');
llaffont's avatar
llaffont committed
106

Laurent's avatar
Laurent committed
107
    defineConstant('AMBERURL',  BASE_URL . '/amber/');
llaffont's avatar
llaffont committed
108

Laurent's avatar
Laurent committed
109 110 111 112
    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/');
llaffont's avatar
llaffont committed
113

Laurent's avatar
Laurent committed
114
    defineConstant('JQUERY', URL_ADMIN_JS . 'jquery-3.2.1.min.js');
Laurent's avatar
Laurent committed
115
    defineConstant('JQUERYMOBILE_VERSION',  '1.4.5');
116 117 118
    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');

Laurent's avatar
Laurent committed
119 120 121
    defineConstant("URL_JS", BASE_URL . "/public/opac/js/");
    defineConstant("URL_EPUB", BASE_URL . "/temp/epub/");
    defineConstant("URL_SHARED_IMG", BASE_URL . "/public/opac/images/");
llaffont's avatar
llaffont committed
122

Laurent's avatar
Laurent committed
123 124 125 126 127
    // 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));
llaffont's avatar
llaffont committed
128

Laurent's avatar
Laurent committed
129 130 131
    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
132

Laurent's avatar
Laurent committed
133 134 135 136 137 138 139
    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
140

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

Laurent's avatar
Laurent committed
143 144 145
    defineConstant('PATCH_PATH', ROOT_PATH . '/cosmogramme/sql/patch/');
    return $this;
  }
llaffont's avatar
llaffont committed
146 147


Laurent's avatar
Laurent committed
148 149 150 151 152 153 154 155
  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);
llaffont's avatar
llaffont committed
156 157


Laurent's avatar
Laurent committed
158 159
    setlocale(LC_TIME, 'fr_FR.UTF-8');
    date_default_timezone_set($this->_config->timeZone);
Patrick Barroca's avatar
Patrick Barroca committed
160

Laurent's avatar
Laurent committed
161
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
162 163
  }

llaffont's avatar
llaffont committed
164

Laurent's avatar
Laurent committed
165 166 167 168
  public function setupCache() {
    $frontendOptions = ['lifetime' => CACHE_LIFETIME, // durée du cache: 1h
                        'automatic_serialization' => false,
                        'caching' => true];
llaffont's avatar
llaffont committed
169

Laurent's avatar
Laurent committed
170 171 172 173 174
    $use_memcached = (MEMCACHED_ENABLE === true);
    $backendOptions = $use_memcached
      ? ['servers' => [ ['host' => MEMCACHED_HOST,
                         'port' => MEMCACHED_PORT] ]]
      : ['cache_dir' => PATH_TEMP ];
175

Laurent's avatar
Laurent committed
176 177 178 179 180
    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core',
                                 $use_memcached ? 'Memcached' : 'File',
                                 $frontendOptions,
                                 $backendOptions);
181

Laurent's avatar
Laurent committed
182 183 184 185
    Storm_Cache::setDefaultZendCache($cache);
    Storm_Cache::setSeed($this->_config->sgbd->config->dbname.md5(BASE_URL));
    return $this;
  }
llaffont's avatar
llaffont committed
186 187


Laurent's avatar
Laurent committed
188 189 190 191 192 193 194 195 196 197 198 199
  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;
  }
llaffont's avatar
llaffont committed
200 201


Laurent's avatar
Laurent committed
202 203 204 205 206 207 208 209 210 211 212
  function setupLanguage() {
    Zend_Locale::setDefault(Class_AdminVar::getDefaultLanguage());
    Zend_Registry::set('locale', new Zend_Locale());

    $translate = new ZendAfi_Translate('gettext', LANG_DIR.'fr.mo', 'fr');
    foreach (Class_AdminVar::getLanguesWithoutDefault() as $language) {
      $filename = LANG_DIR.$language.'.mo';
      if (file_exists($filename))
        $translate->addTranslation($filename, $language);
    }
    Zend_Registry::set('translate', $translate);
llaffont's avatar
llaffont committed
213

Laurent's avatar
Laurent committed
214 215 216
    Zend_Validate_Abstract::setDefaultTranslator($translate);
    return $this;
  }
llaffont's avatar
llaffont committed
217

218

Laurent's avatar
Laurent committed
219 220 221 222 223
  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
224

Laurent's avatar
Laurent committed
225 226 227
    $afi_sql = new Class_Systeme_Sql();
    Zend_Registry::set('sql', $afi_sql);
    Zend_Db_Table::getDefaultAdapter()->query('set names "UTF8"');
228
    Zend_Db_Table::getDefaultAdapter()->query('set SQL_MODE = ""');
Laurent's avatar
Laurent committed
229
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
230 231
  }

232

Laurent's avatar
Laurent committed
233 234 235 236 237 238
  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;
  }
llaffont's avatar
llaffont committed
239 240


Laurent's avatar
Laurent committed
241 242 243 244 245
  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
246 247
  }

llaffont's avatar
llaffont committed
248

Laurent's avatar
Laurent committed
249
  function setupHTTPClient() {
llaffont's avatar
llaffont committed
250

Laurent's avatar
Laurent committed
251 252 253 254 255 256 257 258 259
    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);
llaffont's avatar
llaffont committed
260

Laurent's avatar
Laurent committed
261 262
      $proxy_adapter = new Zend_Http_Client_Adapter_Proxy();
      $proxy_adapter->setConfig($proxy_config);
efalcy's avatar
efalcy committed
263
      Class_HttpClientFactory::setAdapter($proxy_adapter);
Laurent's avatar
Laurent committed
264
    }
llaffont's avatar
llaffont committed
265

Laurent's avatar
Laurent committed
266 267
    return $this;
  }
Patrick Barroca's avatar
Patrick Barroca committed
268 269


Laurent's avatar
Laurent committed
270 271 272 273 274 275
  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;
    }
llaffont's avatar
llaffont committed
276

Laurent's avatar
Laurent committed
277 278 279 280
    if (defined('SMTP_HOST'))
      ZendAfi_Mail::setDefaultTransport(new Zend_Mail_Transport_Smtp(SMTP_HOST));
    return $this;
  }
llaffont's avatar
llaffont committed
281

Patrick Barroca's avatar
Patrick Barroca committed
282

Laurent's avatar
Laurent committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  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_SelectionBib())
      ->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())
301
      ->registerPlugin(new ZendAfi_Controller_Plugin_FeaturesTracking())
Laurent's avatar
Laurent committed
302
      ->setParam('useDefaultControllerAlways', false);
Patrick Barroca's avatar
Patrick Barroca committed
303 304
  }

305

Laurent's avatar
Laurent committed
306 307 308 309 310 311 312 313 314 315 316
  function setupFrontController() {
    $this->_front_controller = $this
      ->newFrontController()
      ->addControllerDirectory(ROOT_PATH.'afi/application/modules/opacpriv/controllers','opacpriv')
      ->setBaseURL(BASE_URL);

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

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

Laurent's avatar
Laurent committed
317
    $this->_front_controller->registerPlugin(new ZendAfi_Controller_Plugin_XHProfile());
Laurent's avatar
Laurent committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    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']
                                                            ]
                                                           ));
  }
334

335

Laurent's avatar
Laurent committed
336
  function setupRoutes($front_controller, $cfg) {
Laurent's avatar
Laurent committed
337 338
    if ('1' == $cfg->get('enable_rewriting')) {
      $front_controller
Laurent's avatar
Laurent committed
339 340
        ->setBaseUrl('')
        ->setRouter(new ZendAfi_Controller_Router_RewriteWithoutBaseUrl());
341

Laurent's avatar
Laurent committed
342 343
      Class_Url::doNotAddBaseUrl();
    }
344

Laurent's avatar
Laurent committed
345
    $front_controller
Laurent's avatar
Laurent committed
346 347 348 349 350 351 352 353 354 355 356
      ->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',
357 358 359 360
                                                          'action' => 'sitemap']))
      ->addRoute('filemanager',
                 new ZendAfi_Controller_Router_Route_QueryString('admin/file-manager',
                                                                 ['module' => 'admin',
361
                                                                  'controller' => 'file-manager']));
Laurent's avatar
Laurent committed
362
    return $this;
Laurent's avatar
Laurent committed
363
  }
364

Patrick Barroca's avatar
Patrick Barroca committed
365

Laurent's avatar
Laurent committed
366 367 368 369 370
  function setupPagination() {
    Zend_Paginator::setDefaultScrollingStyle('Sliding');
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    return $this;
  }
Patrick Barroca's avatar
Patrick Barroca committed
371

372

Laurent's avatar
Laurent committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  function setupRestful() {
    if (!(new Class_Testing_FileSystem())->file_exists(ROOT_PATH . 'restful.php'))
      return $this;

    set_include_path(ROOT_PATH . '/restful/library' . PATH_SEPARATOR .
                     get_include_path());

    $config = new Zend_Config(include ROOT_PATH . 'restful.php');
    Restful_Model_Configuration::setConfig($config);
    Restful_Bootstrap::asPlugin(Zend_Controller_Front::getInstance(),
                                'rest',
                                Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer'));
    return $this;
  }


  function setupSearch() {
    defineConstant('MAX_SEARCH_RESULTS', '');
    Class_CriteresRecherche::setMaxSearchResults(MAX_SEARCH_RESULTS);
    return $this;
  }
394

Laurent's avatar
Laurent committed
395 396

  function setupDigitalResources() {
397
    Class_DigitalResource::getInstance()->bootstrap($this->_front_controller);
Laurent's avatar
Laurent committed
398 399 400 401 402 403 404 405
    return $this;
  }


  function setupCustomFields() {
    Class_CustomField_Model::registerDefault();
    return $this;
  }
Laurent's avatar
Laurent committed
406 407 408
}


Laurent's avatar
Laurent committed
409
function setupOpac() {
Laurent's avatar
Laurent committed
410 411 412
  return (new Bokeh_Engine())
    ->powerOn()
    ->getFrontController();
Patrick Barroca's avatar
Patrick Barroca committed
413
}