source: trunk/include/common.inc.php @ 31102

Last change on this file since 31102 was 31102, checked in by mistic100, 9 years ago

feature 3221 Add Logger class

  • Property svn:eol-style set to LF
File size: 8.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24defined('PHPWG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR);
25
26// determine the initial instant to indicate the generation time of this page
27$t2 = microtime(true);
28
29@set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
30
31//
32// addslashes to vars if magic_quotes_gpc is off this is a security
33// precaution to prevent someone trying to break out of a SQL statement.
34//
35if( !@get_magic_quotes_gpc() )
36{
37  function sanitize_mysql_kv(&$v, $k)
38  {
39    $v = addslashes($v);
40  }
41  if( is_array( $_GET ) )
42  {
43    array_walk_recursive( $_GET, 'sanitize_mysql_kv' );
44  }
45  if( is_array( $_POST ) )
46  {
47    array_walk_recursive( $_POST, 'sanitize_mysql_kv' );
48  }
49  if( is_array( $_COOKIE ) )
50  {
51    array_walk_recursive( $_COOKIE, 'sanitize_mysql_kv' );
52  }
53}
54if ( !empty($_SERVER["PATH_INFO"]) )
55{
56  $_SERVER["PATH_INFO"] = addslashes($_SERVER["PATH_INFO"]);
57}
58
59//
60// Define some basic configuration arrays this also prevents malicious
61// rewriting of language and otherarray values via URI params
62//
63$conf = array();
64$page = array(
65  'infos' => array(),
66  'errors' => array(),
67  'warnings' => array(),
68  );
69$user = array();
70$lang = array();
71$header_msgs = array();
72$header_notes = array();
73$filter = array();
74
75foreach(
76  array(
77    'gzopen'
78    ) as $func)
79{
80  if (!function_exists($func))
81  {
82    include_once(PHPWG_ROOT_PATH . 'include/php_compat/'.$func.'.php');
83  }
84}
85
86include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
87@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
88
89defined('PWG_LOCAL_DIR') or define('PWG_LOCAL_DIR', 'local/');
90
91@include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR .'config/database.inc.php');
92if (!defined('PHPWG_INSTALLED'))
93{
94  header('Location: install.php');
95  exit;
96}
97include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
98
99if(isset($conf['show_php_errors']) && !empty($conf['show_php_errors']))
100{
101  @ini_set('error_reporting', $conf['show_php_errors']);
102  @ini_set('display_errors', true);
103}
104
105include(PHPWG_ROOT_PATH . 'include/constants.php');
106include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
107include(PHPWG_ROOT_PATH . 'include/template.class.php');
108include(PHPWG_ROOT_PATH . 'include/cache.class.php');
109include(PHPWG_ROOT_PATH . 'include/Logger.class.php');
110
111$persistent_cache = new PersistentFileCache();
112
113// Database connection
114try
115{
116  pwg_db_connect($conf['db_host'], $conf['db_user'],
117                 $conf['db_password'], $conf['db_base']);
118}
119catch (Exception $e)
120{
121  my_error(l10n($e->getMessage()), true);
122}
123
124pwg_db_check_charset();
125
126load_conf_from_db();
127
128$logger = new Logger(array(
129  'directory' => PHPWG_ROOT_PATH . $conf['data_location'] . $conf['log_dir'],
130  'severity' => $conf['log_level'],
131  // we use an hashed filename to prevent direct file access, and we salt with
132  // the db_password instead of secret_key because the log must be usable in i.php
133  // (secret_key is in the database)
134  'filename' => 'log_' . date('Y-m-d') . '_' . sha1(date('Y-m-d') . $conf['db_password']) . '.txt',
135  'globPattern' => 'log_*.txt',
136  'archiveDays' => $conf['log_archive_days'],
137  ));
138
139if (!$conf['check_upgrade_feed'])
140{
141  if (!isset($conf['piwigo_db_version']) or $conf['piwigo_db_version'] != get_branch_from_version(PHPWG_VERSION))
142  {
143    redirect(get_root_url().'upgrade.php');
144  }
145}
146
147ImageStdParams::load_from_db();
148
149session_start();
150load_plugins();
151
152// users can have defined a custom order pattern, incompatible with GUI form
153if (isset($conf['order_by_custom']))
154{
155  $conf['order_by'] = $conf['order_by_custom'];
156}
157if (isset($conf['order_by_inside_category_custom']))
158{
159  $conf['order_by_inside_category'] = $conf['order_by_inside_category_custom'];
160}
161
162include(PHPWG_ROOT_PATH.'include/user.inc.php');
163
164if (in_array( substr($user['language'],0,2), array('fr','it','de','es','pl','hu','ru','nl','tr','da') ) )
165{
166  define('PHPWG_DOMAIN', substr($user['language'],0,2).'.piwigo.org');
167}
168elseif ('zh_CN' == $user['language']) {
169  define('PHPWG_DOMAIN', 'cn.piwigo.org');
170}
171elseif ('pt_BR' == $user['language']) {
172  define('PHPWG_DOMAIN', 'br.piwigo.org');
173}
174else {
175  define('PHPWG_DOMAIN', 'piwigo.org');
176}
177define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
178
179if(isset($conf['alternative_pem_url']) and $conf['alternative_pem_url']!='')
180{
181  define('PEM_URL', $conf['alternative_pem_url']);
182}
183else
184{
185  define('PEM_URL', 'http://'.PHPWG_DOMAIN.'/ext');
186}
187
188// language files
189load_language('common.lang');
190if ( is_admin() || (defined('IN_ADMIN') and IN_ADMIN) )
191{
192  load_language('admin.lang');
193}
194trigger_notify('loading_lang');
195load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, array('no_fallback'=>true, 'local'=>true) );
196
197// only now we can set the localized username of the guest user (and not in
198// include/user.inc.php)
199if (is_a_guest())
200{
201  $user['username'] = l10n('guest');
202}
203
204// template instance
205if (defined('IN_ADMIN') and IN_ADMIN )
206{// Admin template
207  $template = new Template(PHPWG_ROOT_PATH.'admin/themes', $conf['admin_theme']);
208}
209else
210{ // Classic template
211  $theme = $user['theme'];
212  if (script_basename() != 'ws' and mobile_theme())
213  {
214    $theme = $conf['mobile_theme'];
215  }
216  $template = new Template(PHPWG_ROOT_PATH.'themes', $theme );
217}
218
219if ( !isset($conf['no_photo_yet']) )
220{
221  include(PHPWG_ROOT_PATH.'include/no_photo_yet.inc.php');
222}
223
224if (isset($user['internal_status']['guest_must_be_guest'])
225    and
226    $user['internal_status']['guest_must_be_guest'] === true)
227{
228  $header_msgs[] = l10n('Bad status for user "guest", using default status. Please notify the webmaster.');
229}
230
231if ($conf['gallery_locked'])
232{
233  $header_msgs[] = l10n('The gallery is locked for maintenance. Please, come back later.');
234
235  if ( script_basename() != 'identification' and !is_admin() )
236  {
237    set_status_header(503, 'Service Unavailable');
238    @header('Retry-After: 900');
239    header('Content-Type: text/html; charset='.get_pwg_charset());
240    echo '<a href="'.get_absolute_root_url(false).'identification.php">'.l10n('The gallery is locked for maintenance. Please, come back later.').'</a>';
241    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
242    exit();
243  }
244}
245
246if ($conf['check_upgrade_feed'])
247{
248  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
249  if (check_upgrade_feed())
250  {
251    $header_msgs[] = 'Some database upgrades are missing, '
252      .'<a href="'.get_absolute_root_url(false).'upgrade_feed.php">upgrade now</a>';
253  }
254}
255
256if (count($header_msgs) > 0)
257{
258  $template->assign('header_msgs', $header_msgs);
259  $header_msgs=array();
260}
261
262if (!empty($conf['filter_pages']) and get_filter_page_value('used'))
263{
264  include(PHPWG_ROOT_PATH.'include/filter.inc.php');
265}
266else
267{
268  $filter['enabled'] = false;
269}
270
271if (isset($conf['header_notes']))
272{
273  $header_notes = array_merge($header_notes, $conf['header_notes']);
274}
275
276// default event handlers
277add_event_handler('render_category_literal_description', 'render_category_literal_description');
278if ( !$conf['allow_html_descriptions'] )
279{
280  add_event_handler('render_category_description', 'nl2br');
281}
282add_event_handler('render_comment_content', 'render_comment_content');
283add_event_handler('render_comment_author', 'strip_tags');
284add_event_handler('render_tag_url', 'str2url');
285add_event_handler('blockmanager_register_blocks', 'register_default_menubar_blocks', EVENT_HANDLER_PRIORITY_NEUTRAL-1);
286if ( !empty($conf['original_url_protection']) )
287{
288  add_event_handler('get_element_url', 'get_element_url_protection_handler');
289  add_event_handler('get_src_image_url', 'get_src_image_url_protection_handler');
290}
291trigger_notify('init');
292?>
Note: See TracBrowser for help on using the repository browser.