summaryrefslogtreecommitdiffstats
path: root/abs/mv-core/webcal/config.php
blob: d8034400f7c47265493ab461234c84cea90abc5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/* This file loads configuration settings from the data file settings.php and
 * sets up some needed variables.
 *
 * The settings.php file is created during installation using the web-based db
 * setup page (install/index.php).
 *
 * <b>Note:</b>
 * DO NOT EDIT THIS FILE!
 *
 *
 * @author Craig Knudsen <cknudsen@cknudsen.com>
 * @copyright Craig Knudsen, <cknudsen@cknudsen.com>, http://www.k5n.us/cknudsen
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @version $Id: config.php,v 1.75.2.9 2008/02/17 02:21:25 cknudsen Exp $
 * @package WebCalendar
 */

/* Prints a fatal error message to the user along with a link to the
 * Troubleshooting section of the WebCalendar System Administrator's Guide.
 *
 * Execution is aborted.
 *
 * @param string  $error  The error message to display
 * @internal We don't normally put functions in this file. But, since this
 *           file is included before some of the others, this function either
 *           goes here or we repeat this code in multiple files.
 *           Additionally, we don't want to call too many external functions
 *           from here since we could end up calling the function that called
 *           this one.
 * NOTES: Don't call translate from here.  This function if often called
 * before translation stuff is initialized!
 */
function die_miserable_death ( $error ) {
  global $APPLICATION_NAME, $LANGUAGE, $login, $TROUBLE_URL;
  // Make sure app name is set.
  $appStr = ( empty ( $APPLICATION_NAME ) ? 'WebCalendar' : $APPLICATION_NAME );

  $h2_label = $appStr . ' ' . 'Error';
  $title = $appStr . ': ' . 'Fatal Error';
  $trouble_label = 'Troubleshooting Help';
  $user_BGCOLOR = '#FFFFFF';

  echo <<<EOT
<html>
  <head><title>{$title}</title></head>
  <body bgcolor ="{$user_BGCOLOR}">
    <h2>{$h2_label}</h2>
    <p>{$error}</p><hr />
    <p><a href="{$TROUBLE_URL}" target="_blank">{$trouble_label}</a></p>
  </body>
</html>
EOT;
  exit;
}

function db_error ( $doExit = false, $sql = '' ) {
  global $settings;

  $ret = str_replace ( 'XXX', dbi_error (), translate ( 'Database error XXX.' ) )
   . ( ! empty ( $settings['mode'] ) && $settings['mode'] == 'dev' && !
    empty ( $sql ) ? '<br />SQL:<br />' . $sql : '' );

  if ( $doExit ) {
    echo $ret;
    exit;
  } else
    return $ret;
}

/**
  * Get the full path to a file located in the webcalendar includes
  * directory.
  */
function get_full_include_path ( $filename )
{
  if ( preg_match ( "/(.*)config.php/", __FILE__, $matches ) ) {
    $fileLoc = $matches[1] . $filename;
    return $fileLoc;
  } else {
    // Oops.  This file is not named config.php!
    die_miserable_death ( "Crap!  Someone renamed config.php" );
  }
}

function do_config ( $fileLoc ) {
  global $db_database, $db_host, $db_login, $db_password, $db_persistent,
  $db_type, $NONUSER_PREFIX, $phpdbiVerbose, $PROGRAM_DATE, $PROGRAM_NAME,
  $PROGRAM_URL, $PROGRAM_VERSION, $readonly, $run_mode, $settings, $single_user,
  $single_user_login, $TROUBLE_URL, $use_http_auth, $user_inc;

  $PROGRAM_VERSION = 'v1.2.b1';
  $PROGRAM_DATE = '25 Feb 2008';
  $PROGRAM_NAME = 'WebCalendar ' . "$PROGRAM_VERSION ($PROGRAM_DATE)";
  $PROGRAM_URL = 'http://www.k5n.us/webcalendar.php';
  $TROUBLE_URL = 'docs/WebCalendar-SysAdmin.html#trouble';

  // Open settings file to read.
  $settings = array ();
  if ( file_exists ( $fileLoc ) ) {
    $fd = @fopen ( $fileLoc, 'rb', true );
  }
  if ( empty ( $fd ) && defined ( '__WC_INCLUDEDIR' ) ) {
    $fd = @fopen ( __WC_INCLUDEDIR . '/settings.php', 'rb', true );
    if ( $fd )
      $fileLoc = __WC_INCLUDEDIR . '/settings.php';
  }
  // If still empty.... use __FILE__.
  if ( empty ( $fd ) ) {
    $testName = get_full_include_path ( "settings.php" );
    $fd = @fopen ( $fileLoc, 'rb', true );
    if ( $fd )
      $fileLoc = $testName;
  }
  if ( empty ( $fd ) || filesize ( $fileLoc ) == 0 ) {
    // There is no settings.php file.
    // Redirect user to install page if it exists.
    if ( file_exists ( 'install/index.php' ) ) {
      header ( 'Location: install/index.php' );
      exit;
    } else
      die_miserable_death (
        translate ( 'Could not find settings.php file...' ) );
  }

  // We don't use fgets () since it seems to have problems with Mac-formatted
  // text files. Instead, we read in the entire file, and split the lines manually.
  $data = '';
  while ( ! feof ( $fd ) ) {
    $data .= fgets ( $fd, 4096 );
  }
  fclose ( $fd );

  // Replace any combination of carriage return (\r) and new line (\n)
  // with a single new line.
  $data = preg_replace ( "/[\r\n]+/", "\n", $data );

  // Split the data into lines.
  $configLines = explode ( "\n", $data );

  for ( $n = 0, $cnt = count ( $configLines ); $n < $cnt; $n++ ) {
    $buffer = trim ( $configLines[$n], "\r\n " );
    if ( preg_match ( '/^#|\/\*/', $buffer ) || // comments
        preg_match ( '/^<\?/', $buffer ) || // start PHP code
        preg_match ( '/^\?>/', $buffer ) ) // end PHP code
      continue;
    if ( preg_match ( '/(\S+):\s*(\S+)/', $buffer, $matches ) )
      $settings[$matches[1]] = $matches[2];
    // echo "settings $matches[1] => $matches[2]<br />";
  }
  $configLines = $data = '';

  // Extract db settings into global vars.
  $db_database = $settings['db_database'];
  $db_host = $settings['db_host'];
  $db_login = $settings['db_login'];
  $db_password = $settings['db_password'];
  $db_persistent = ( preg_match ( '/(1|yes|true|on)/i',
      $settings['db_persistent'] ) ? '1' : '0' );
  $db_type = $settings['db_type'];

  // If no db settings, then user has likely started install but not yet
  // completed.  So, send them back to the install script.
  if ( empty ( $db_type ) ) {
    if ( file_exists ( 'install/index.php' ) ) {
      header ( 'Location: install/index.php' );
      exit;
    } else
      die_miserable_death (
        translate ( 'Incomplete settings.php file...' ) );
  }

  // Use 'db_cachedir' if found, otherwise look for 'cachedir'.
  if ( ! empty ( $settings['db_cachedir'] ) )
    dbi_init_cache ( $settings['db_cachedir'] );
  else
  if ( ! empty ( $settings['cachedir'] ) )
    dbi_init_cache ( $settings['cachedir'] );

  if ( ! empty ( $settings['db_debug'] ) &&
      preg_match ( '/(1|true|yes|enable|on)/i', $settings['db_debug'] ) )
    dbi_set_debug ( true );

  foreach ( array ( 'db_type', 'db_host', 'db_login', 'db_password' ) as $s ) {
    if ( empty ( $settings[$s] ) )
      die_miserable_death ( str_replace ( 'XXX', $s,
          translate ( 'Could not find XXX defined in...' ) ) );
  }

  // Allow special settings of 'none' in some settings[] values.
  // This can be used for db servers not using TCP port for connection.
  $db_host = ( $db_host == 'none' ? '' : $db_host );
  $db_password = ( $db_password == 'none' ? '' : $db_password );

  $readonly = preg_match ( '/(1|yes|true|on)/i',
    $settings['readonly'] ) ? 'Y' : 'N';

  if ( empty ( $settings['mode'] ) )
    $settings['mode'] = 'prod';

  $run_mode = ( preg_match ( '/(dev)/i', $settings['mode'] ) ? 'dev' : 'prod' );
  $phpdbiVerbose = ( $run_mode == 'dev' ) ;
  $single_user = preg_match ( '/(1|yes|true|on)/i',
    $settings['single_user'] ) ? 'Y' : 'N';

  if ( $single_user == 'Y' )
    $single_user_login = $settings['single_user_login'];

  if ( $single_user == 'Y' && empty ( $single_user_login ) )
    die_miserable_death ( str_replace ( 'XXX', 'single_user_login',
        translate ( 'You must define XXX in' ) ) );

  $use_http_auth = ( preg_match ( '/(1|yes|true|on)/i',
      $settings['use_http_auth'] ) ? true : false );

  // Type of user authentication.
  $user_inc = $settings['user_inc'];

  // If sqlite, the db file is in the include directory
  if ( $db_type == 'sqlite' )
    $db_database = get_full_include_path ( $db_database );

  // Check the current installation version.
  // Redirect user to install page if it is different from stored value.
  // This will prevent running WebCalendar until UPGRADING.html has been
  // read and required upgrade actions completed.
  $c = @dbi_connect ( $db_host, $db_login, $db_password, $db_database, false );
  if ( $c ) {
    $rows = dbi_get_cached_rows ( 'SELECT cal_value FROM webcal_config
       WHERE cal_setting = \'WEBCAL_PROGRAM_VERSION\'' );
    if ( ! $rows ) {
      // &amp; does not work here...leave it as &.
      header ( 'Location: install/index.php?action=mismatch&version=UNKNOWN' );
      exit;
    } else {
      $row = $rows[0];
      if ( empty ( $row ) || $row[0] != $PROGRAM_VERSION ) {
        // &amp; does not work here...leave it as &.
        header ( 'Location: install/index.php?action=mismatch&version='
         . ( empty ( $row ) ? 'UNKNOWN' : $row[0] ) );
        exit;
      }
    }
    dbi_close ( $c );
  } else { // Must mean we don't have a settings.php file.
    // NOTE: if we get a connect error when running send_reminders.php,
    // we may want to show that error message here.
    // &amp; does not work here...leave it as &.
    header ( 'Location: install/index.php?action=mismatch&version=UNKNOWN' );
    exit;
  }

  // We can add extra 'nonuser' calendars such as a holiday, corporate,
  // departmental, etc. We need a unique prefix for these calendars
  // so we don't get them mixed up with real logins. This prefix should be
  // a maximum of 5 characters and should NOT change once set!
  $NONUSER_PREFIX = '_NUC_';

  if ( $single_user != 'Y' )
    $single_user_login = '';
}

?>