startup.php 14 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() {
Laurent's avatar
7.10.0  
Laurent committed
84
    defineConstant('BOKEH_MAJOR_VERSION','7.10');
Ghislain Loas's avatar
Ghislain Loas committed
85
    defineConstant('BOKEH_RELEASE_NUMBER', BOKEH_MAJOR_VERSION . '.4');
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

Laurent's avatar
Laurent committed
98 99 100
    defineConstant('USERFILESPATH', './userfiles');
    defineConstant('USERFILESURL', BASE_URL . '/userfiles/');
    defineConstant('PATH_TEMP',  ROOT_PATH . 'temp/');
llaffont's avatar
llaffont committed
101

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

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

Laurent's avatar
Laurent committed
107 108 109 110
    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
111

Laurent's avatar
Laurent committed
112 113 114 115 116 117
    defineConstant('JQUERY', URL_ADMIN_JS . 'jquery-1.8.3.js');
    defineConstant('JQUERYMOBILE_VERSION',  '1.4.5');
    defineConstant('JQUERYUI', URL_ADMIN_JS . 'jquery_ui/jquery-ui-1.10.3.full.js');
    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
118

Laurent's avatar
Laurent committed
119 120 121 122 123
    // 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
124

Laurent's avatar
Laurent committed
125 126 127
    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
128

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

Laurent's avatar
Laurent committed
137
    defineConstant('IMAGE_MAGICK_PATH', 'convert');
Patrick Barroca's avatar
Patrick Barroca committed
138

Laurent's avatar
Laurent committed
139 140 141
    defineConstant('PATCH_PATH', ROOT_PATH . '/cosmogramme/sql/patch/');
    return $this;
  }
llaffont's avatar
llaffont committed
142 143


Laurent's avatar
Laurent committed
144 145 146 147 148 149 150 151
  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
152 153


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

Laurent's avatar
Laurent committed
157
    return $this;
Patrick Barroca's avatar
Patrick Barroca committed
158 159
  }

llaffont's avatar
llaffont committed
160

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

Laurent's avatar
Laurent committed
166 167 168 169 170
    $use_memcached = (MEMCACHED_ENABLE === true);
    $backendOptions = $use_memcached
      ? ['servers' => [ ['host' => MEMCACHED_HOST,
                         'port' => MEMCACHED_PORT] ]]
      : ['cache_dir' => PATH_TEMP ];
171

Laurent's avatar
Laurent committed
172 173 174 175 176
    // getting a Zend_Cache_Core object
    $cache = Zend_Cache::factory('Core',
                                 $use_memcached ? 'Memcached' : 'File',
                                 $frontendOptions,
                                 $backendOptions);
177

Laurent's avatar
Laurent committed
178 179 180 181
    Storm_Cache::setDefaultZendCache($cache);
    Storm_Cache::setSeed($this->_config->sgbd->config->dbname.md5(BASE_URL));
    return $this;
  }
llaffont's avatar
llaffont committed
182 183


Laurent's avatar
Laurent committed
184 185 186 187 188 189 190 191 192 193 194 195
  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
196 197


Laurent's avatar
Laurent committed
198 199 200 201 202 203 204 205 206 207 208
  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
209

Laurent's avatar
Laurent committed
210 211 212
    Zend_Validate_Abstract::setDefaultTranslator($translate);
    return $this;
  }
llaffont's avatar
llaffont committed
213

214

Laurent's avatar
Laurent committed
215 216 217 218 219
  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
220

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

228

Laurent's avatar
Laurent committed
229 230 231 232 233 234
  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
235 236


Laurent's avatar
Laurent committed
237 238 239 240 241
  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
242 243
  }

llaffont's avatar
llaffont committed
244

Laurent's avatar
Laurent committed
245 246
  function setupHTTPClient() {
    $httpClient = new Zend_Http_Client();
llaffont's avatar
llaffont committed
247

Laurent's avatar
Laurent committed
248 249 250 251 252 253 254 255 256
    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
257

Laurent's avatar
Laurent committed
258 259 260 261
      $proxy_adapter = new Zend_Http_Client_Adapter_Proxy();
      $proxy_adapter->setConfig($proxy_config);
      $httpClient->setAdapter($proxy_adapter);
    }
llaffont's avatar
llaffont committed
262

Laurent's avatar
Laurent committed
263
    $httpClient->setConfig(['timeout' => 2]);
Patrick Barroca's avatar
Patrick Barroca committed
264

Laurent's avatar
Laurent committed
265 266 267
    Zend_Registry::set('httpClient',$httpClient);
    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 301
  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())
      ->setParam('useDefaultControllerAlways', false);
Patrick Barroca's avatar
Patrick Barroca committed
302 303
  }

304

Laurent's avatar
Laurent committed
305 306 307 308 309 310 311 312 313 314 315
  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
316
    $this->_front_controller->registerPlugin(new ZendAfi_Controller_Plugin_XHProfile());
Laurent's avatar
Laurent committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    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']
                                                            ]
                                                           ));
  }
333

334

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

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

Laurent's avatar
Laurent committed
344
    $front_controller
Laurent's avatar
Laurent committed
345 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',
                                                          'action' => 'sitemap']));
Laurent's avatar
Laurent committed
357
    return $this;
Laurent's avatar
Laurent committed
358
  }
359

Patrick Barroca's avatar
Patrick Barroca committed
360

Laurent's avatar
Laurent committed
361 362 363 364 365
  function setupPagination() {
    Zend_Paginator::setDefaultScrollingStyle('Sliding');
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    return $this;
  }
Patrick Barroca's avatar
Patrick Barroca committed
366

367

Laurent's avatar
Laurent committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  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;
  }
389

Laurent's avatar
Laurent committed
390 391

  function setupDigitalResources() {
392
    Class_DigitalResource::getInstance()->bootstrap($this->_front_controller);
Laurent's avatar
Laurent committed
393 394 395 396 397 398 399 400
    return $this;
  }


  function setupCustomFields() {
    Class_CustomField_Model::registerDefault();
    return $this;
  }
Laurent's avatar
Laurent committed
401 402 403
}


Laurent's avatar
Laurent committed
404
function setupOpac() {
Laurent's avatar
Laurent committed
405 406 407
  return (new Bokeh_Engine())
    ->powerOn()
    ->getFrontController();
Patrick Barroca's avatar
Patrick Barroca committed
408
}