source: extensions/event_cats/include/ec_conf.inc.php @ 7099

Last change on this file since 7099 was 7084, checked in by LucMorizur, 14 years ago

At last a configuration management working as desired

File size: 13.9 KB
Line 
1<?php
2
3// +-----------------------------------------------------------------------+
4// | Piwigo - a PHP based picture gallery                                  |
5// +-----------------------------------------------------------------------+
6// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
7// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
8// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
9// +-----------------------------------------------------------------------+
10// | This program is free software; you can redistribute it and/or modify  |
11// | it under the terms of the GNU General Public License as published by  |
12// | the Free Software Foundation                                          |
13// |                                                                       |
14// | This program is distributed in the hope that it will be useful, but   |
15// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
16// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
17// | General Public License for more details.                              |
18// |                                                                       |
19// | You should have received a copy of the GNU General Public License     |
20// | along with this program; if not, write to the Free Software           |
21// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
22// | USA.                                                                  |
23// +-----------------------------------------------------------------------+
24
25// Keeps file coded in UTF-8 without BOM : é
26
27if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
28
29global $ec_conf, $page, $ec_conf_index, $ec_conf_possible_values, $ec_debug;
30
31// ---------------------------------------------------------------------------
32//                            $ec_conf initalization
33// ---------------------------------------------------------------------------
34
35// ----- Introduction
36// Not sure I'm very proud of this quite complex configuration initialization
37// part :-/ ...
38// The reason of the complexity is the inital intention of building a
39// configuration management allowing to use in the rest of the script only
40// two functions: change_ec_conf() and read_ec_conf() .
41// change_ec_conf() updates the DB systematically. The initialization part
42// has the only function of determining what is the value of $ec_conf, which
43// is the root configuration parameters provider.
44// -----
45
46// $ec_conf has numeric indexes (instead of string keys like 'activated',
47// 'howto'...) because of its first reading, done with the "explode" function.
48// It would be very easy to build a string keys indexed array, thanks to
49// following instructions :
50/*
51$t = pwg_db_fetch_row(pwg_query("
52  SELECT `value`
53  FROM `".CONFIG_TABLE."`
54  WHERE `param` = 'event_cats';
55"));
56$ec_conf_temp = explode(',', $t[0]);
57$ec_conf = array(); $i = 0;
58foreach ($ec_conf_default as $k => $v) $ec_conf[$k] = ec_conf_temp[$i++];
59*/
60// Though, I prefer to keep $ec_conf with numeric indexes, so to be compelled
61// to go through function change_ec_conf() to modify ec_conf, and thus update
62// the DB at the same time.
63
64//----------------------------------------------------------------------------
65
66// Give possible $ec_conf values. In case values cannot be predicted, just
67// provide an empty array : array() .
68$ec_conf_possible_values = array(
69  'activated'           => array('0', '1'),                           //  0
70    // plugin activated
71  'howto'               => array('0', '1'),                           //  1
72    // whether the small aknowledgement message has already been displayed
73  'dup_allow'           => array('0', '1', '2'),                      //  2
74    // duplication allowance :
75    //   0 : no account;
76    //   1 : all accounts;
77    //   2 : specified by group, type, and user ids (default).
78    // If Event Cats is in "simple mode", get the value of
79    // $conf['ec_dup_allow'] if it is set; and an allowance to duplicate only
80    // generic accounts will be defined if the value is 2.
81  'duplic_display'      => array('0', '1', '2'),                      //  3
82    // duplication link display in the menubar :
83    //   0 : "Register" for everybody;
84    //   1 : "Duplicate" for everybody;
85    //   2 : "Register" only for generic accounts,
86    //       "Duplicate" for all other accounts (default);
87    // If Event Cats is in "simple mode", takes the value of
88    // $conf['ec_duplic_display'] if it's set.
89  'auto_code_dg_nb'     => array( '4',  '5',  '8', '10', '12', '15',
90                                 '16', '20', '24', '25', '30', '32'), //  4
91    // number of digits when creating a new code automatically, several values
92    // (default: 10)
93    // If Event Cats is in "simple mode", get the value of
94    // $conf['ec_auto_code_dg_nb'] if set.
95  'unknown_code'        => array('0', '1', '2'),                      //  5
96    // what to do in case an unknown code is used with "autolog" parameter :
97    //   0 : nothing (display home page, index.php (not logged in of course));
98    //   1 : display "access denied" page (default);
99    //   2 : display an A. P. precised below.
100  'unknown_code_ap_id'  => array(),                                   //  6
101    // the id of the Additional Page which must be used in case of usage of an
102    // unknow code; several vals (default: 0).
103  'comment_display'     => array('0', '1'),                           //  7
104    // the way the comment is displayed in "entries" tab:
105    // 0, as tooltip; 1 (default), on a line under code
106    // If Event Cats is in "simple mode", get the value of
107    // $conf['ec_comment_display'] if set.
108  'in_help_display'     => array('0', '1'),                           //  8
109    // whether the help banner is displayed (default: 1).
110  'display_connection'  => array('0', '1'),                           //  9
111    // whether the "Connection" link must be displayed for generic users
112    // (default: 1)
113    // If Event Cats is in "simple mode", takes the value of
114    // $conf['ec_display_connection'] if set.
115  'simple_mode_on'      => array('0', '1'),                           // 10
116    // whether the presentation of the plugin is in "simple mode" or not.
117    // (default: 1)
118    // The default value can be modified to 0 (normal view) if Event Cats was
119    // already installed previously.
120  'display_full_button' => array('0', '1'),                           // 11
121    // whether the "Show full features" button must be displayed (default: 1).
122    // The default value can be modified to 0 (don't display) if
123    // $conf['ec_display_full_button'] is set, and false.
124);
125
126// $ec_conf_index is an array of the indexes of $ec_conf. Thus does it allow
127// to check if X exists when asking for $ec_conf[X].
128$ec_conf_index = array_flip(array_keys($ec_conf_possible_values));
129
130// Get current value of $ec_conf, if the entry in the DB exists.
131$t = array(); $u = '0'; $v = false; $w = false; $ec_conf = array(); $s = '0';
132if ($w = ( // $w becomes true if the query succeeds <=> if EC was already in
133  // $t gets event_cats config value (array to be "exploded")
134  ($t = pwg_db_fetch_row(pwg_query("
135    SELECT `value`
136    FROM `".CONFIG_TABLE."`
137    WHERE `param` = 'event_cats';
138  "))) !== false)
139) {
140  $v = (count($ec_conf = explode(',', $t[0])) == count($ec_conf_index));
141  $u = $ec_conf[$ec_conf_index['activated']];
142  $s = ($v) ? $ec_conf[$ec_conf_index['howto']] : '0'; // the aknowledgement
143  // message is displayed again if the configuration changes
144}
145if ($v and !$w) die('$v true with $w false in ec_conf.inc.php');
146// $w is false if the entry in the table doesn't exist;
147// $v is false if the entry in the table doesn't exist; or exists, but its
148// number of parameters is different than the number of default parameters.
149// $u is '0' if the plugin is not activated, '1' if it is.
150// $s allows to display the aknowledgement message when config changes.
151// If $v or $w are false, all $ec_conf values are set to default values. But
152// default values must first take values specified in $conf, if they are set.
153
154// Determine whether simple mode is ON or OFF: simple mode must be on if Event
155// Cats is just installed, or if it was ON previously, or if
156// $conf['ec_display_full_button'] is set and is false.
157$simple_mode_on = (
158  (!$v and !$w) or (
159    isset($ec_conf[$ec_conf_index['simple_mode_on']]) and
160    $ec_conf[$ec_conf_index['simple_mode_on']] == '1'
161  ) or (
162    isset($conf['ec_display_full_button']) and
163    $conf['ec_display_full_button'] == false
164  )
165);
166
167// Change $ec_conf values if needed
168if (!$v or $simple_mode_on) {
169 
170  // Perform repetitve work
171  function test_conf($tbl, $k, $k2 = NULL) {
172  global $ec_conf_possible_values;
173    if (!isset($k2)) $k2 = $k;
174    return (isset($tbl[$k]) and
175     in_array($tbl[$k], $ec_conf_possible_values[$k2]));
176  }
177 
178  // Avoid that $conf (very big) is passed as argument to function test_conf
179  $conf_temp = array();
180  foreach ($ec_conf_possible_values as $key => $val)
181   if (isset($conf['ec_'.$key])) $conf_temp[$key] = $conf['ec_'.$key];
182 
183  if (!$v) {
184    // Determine default values of $ec_conf
185    $ec_conf = array(
186      $ec_conf_index['activated'] => $u,
187      $ec_conf_index['howto'] => $s,
188      $ec_conf_index['dup_allow'] =>
189        ($simple_mode_on and test_conf($conf_temp, 'dup_allow')) ?
190          $conf_temp['dup_allow']
191          : (test_conf($ec_conf, $ec_conf_index['dup_allow'], 'dup_allow')) ? 
192            $ec_conf[$ec_conf_index['dup_allow']]
193            : '2',
194      $ec_conf_index['duplic_display'] =>
195        ($simple_mode_on and test_conf($conf_temp, 'duplic_display')) ?
196          $conf_temp['duplic_display']
197          : (test_conf($ec_conf, $ec_conf_index['duplic_display'],
198             'duplic_display')) ?
199            $ec_conf[$ec_conf_index['duplic_display']]
200            : '2',
201      $ec_conf_index['auto_code_dg_nb'] =>
202        ($simple_mode_on and test_conf($conf_temp, 'auto_code_dg_nb')) ?
203          $conf_temp['auto_code_dg_nb']
204          : (test_conf($ec_conf, $ec_conf_index['auto_code_dg_nb'],
205             'auto_code_dg_nb')) ?
206            $ec_conf[$ec_conf_index['auto_code_dg_nb']]
207            : '10',
208      $ec_conf_index['unknown_code'] =>
209        (test_conf($ec_conf, $ec_conf_index['unknown_code'],
210         'unknown_code')) ?
211          $ec_conf[$ec_conf_index['unknown_code']]
212          : '1',
213      $ec_conf_index['unknown_code_ap_id'] =>
214        (isset($ec_conf[$ec_conf_index['unknown_code_ap_id']])) ?
215          $ec_conf[$ec_conf_index['unknown_code_ap_id']]
216          : '0',
217      $ec_conf_index['comment_display'] =>
218        ($simple_mode_on and test_conf($conf_temp, 'comment_display')) ?
219          $conf_temp['comment_display']
220          : (test_conf($ec_conf, $ec_conf_index['comment_display'],
221             'comment_display')) ?
222            $ec_conf[$ec_conf_index['comment_display']]
223            : '1',
224      $ec_conf_index['in_help_display'] =>
225        (test_conf($ec_conf, $ec_conf_index['in_help_display'],
226         'in_help_display')) ?
227            $ec_conf[$ec_conf_index['in_help_display']]
228            : '1',
229      $ec_conf_index['display_connection'] =>
230        ($simple_mode_on and test_conf($conf_temp, 'display_connection')) ?
231          $conf_temp['display_connection']
232          : (test_conf($ec_conf, $ec_conf_index['display_connection'],
233             'display_connection')) ?
234            $ec_conf[$ec_conf_index['display_connection']]
235            : '1',
236      $ec_conf_index['simple_mode_on'] =>
237        ($simple_mode_on) ? '1' : '0',
238      $ec_conf_index['display_full_button'] =>
239        ($simple_mode_on and test_conf($conf_temp, 'display_full_button')) ?
240          $conf_temp['display_full_button']
241          : (test_conf($ec_conf, $ec_conf_index['display_full_button'],
242             'display_full_button')) ?
243            $ec_conf[$ec_conf_index['display_full_button']]
244            : '1',
245    );
246    change_ec_conf('activated', $u); // write in the DB
247  }
248  else { // $simple_mode_on is true ; check $conf values for Event Cats
249    foreach ($ec_conf_possible_values as $key => $val)
250     if (test_conf($conf_temp, $key))
251      change_ec_conf($key, $conf_temp[$key]);
252  }
253  unset($conf_temp);
254}
255// now :
256//   _ the number of parameters is the same in the DB as the number of
257//     possible parameters;
258//   _ the parameters have the default values if needed;
259//   _ the plugin shows it is active if it is the case;
260//   _ correct values will be returned when using procedure read_ec_conf($c).
261
262unset($s, $t, $u, $v, $w, $simple_mode_on);
263// And NOW can change_ec_conf and read_ec_conf be used properly...
264
265// ---------------------------------------------------------------------------
266//                  End of $ec_conf initalization
267// ---------------------------------------------------------------------------
268
269/*
270 * change_ec_conf($c, $v)
271 * updates Event Cats configuration values in the database as well as in
272 * $ec_conf.
273 *
274 * @param
275 *   $c : conf value to update;
276 *   $v : value to give to $ec_conf[$c].
277 * @return
278 *   true or false whether the change was OK or not
279 */
280function change_ec_conf($c, $v) {
281  global $ec_conf, $page, $ec_conf_index, $ec_conf_possible_values;
282
283  if (array_key_exists($c, $ec_conf_index)) {
284    if (
285      count($ec_conf_possible_values[$c]) == 0 or
286      in_array($v, $ec_conf_possible_values[$c])
287    ) {
288      $ec_conf[$ec_conf_index[$c]] = $v;
289      return (pwg_query("
290        UPDATE `".CONFIG_TABLE."`
291        SET `value` = \"".implode(',', $ec_conf)."\"
292        WHERE `param` = 'event_cats';
293      ") !== false);
294    }
295    else {
296      $page['errors'][] = 'Bad change_ec_conf value';
297      return false;
298    }
299  }
300  else {
301    $page['errors'][] = 'Bad change_ec_conf index';
302    return false;
303  }
304}
305
306/*
307 * read_ec_conf($c)
308 * returns the value in $ec_conf using string keys.
309 *
310 * @param
311 *   $c : conf value to update;
312 * @return
313 *   $ec_conf value
314 */
315function read_ec_conf($c) {
316  global $ec_conf, $page, $ec_conf_index;
317  if (array_key_exists($c, $ec_conf_index))
318   return $ec_conf[$ec_conf_index[$c]];
319  else {
320    $page['errors'][] = 'Bad read_ec_conf index';
321    return false;
322  }
323}
324
325?>
Note: See TracBrowser for help on using the repository browser.