summaryrefslogtreecommitdiff
path: root/common.php
blob: f252c12b04a69ad2253b87892ada76fe20460def (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Common file of PgpoolAdmin
 *
 * PHP versions 4 and 5
 *
 * LICENSE: Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * @author     Ryuma Ando <ando@ecomas.co.jp>
 * @copyright  2003-2007 PgPool Global Development Group
 * @version    SVN: $Id$
 */

require_once('version.php');
require_once('libs/Smarty.class.php');
error_reporting(E_ALL);

define('SESSION_LOGIN_USER', 'loginUser');
define('SESSION_LOGIN_USER_PASSWORD', 'md5pass');
define('SESSION_LANG', 'lang');
define('SESSION_MESSAGE', 'message');

session_start();

/**
 * Smarty Parameter
 */
define('SMARTY_TEMPLATE_DIR', dirname(__FILE__) . '/templates' );
define('SMARTY_COMPILE_DIR', dirname(__FILE__) . '/templates_c' );

/**
 * Initialize Smartry
 */
$tpl = new Smarty();
$tpl->assign('version', $version);

if(!file_exists('conf/pgmgt.conf.php')) {
    include('lang/en.lang.php');
    $tpl->assign('message', $message);
    $tpl->display('pgmgtNotFound.tpl');
    exit();
}

require_once('conf/pgmgt.conf.php');

/**
 * Check login
 */
$isLogin = FALSE;
if(isset($_SESSION[SESSION_LOGIN_USER])) {
    $isLogin = TRUE;
    $tpl->assign('isLogin', $isLogin);
}

/**
 * Check pgmgt.conf.php Parameter
 */
$errors = array();
if( !defined('_PGPOOL2_LANG')
    || !defined('_PGPOOL2_CONFIG_FILE')
    || !defined('_PGPOOL2_PASSWORD_FILE')
    || !defined('_PGPOOL2_COMMAND')
    || !defined('_PGPOOL2_PCP_DIR')
    || !defined('_PGPOOL2_PCP_HOSTNAME')
    || !defined('_PGPOOL2_STATUS_REFRESH_TIME')) {
        
        include('lang/en.lang.php');
        $tpl->assign('message', $message);
        $errorCode = 'e7';
        $tpl->assign('errorCode', $errorCode);
        $tpl->display('error.tpl');
        exit();
    }

/**
 * Create message catalog list
 */
$messageList = array();

$res_dir = opendir('lang/');
while($file_name = readdir( $res_dir )) {
    if(ereg('.*\.lang\.php$', $file_name)) {
        if(@is_file('lang/' . $file_name)) {
            include('lang/' . $file_name);
            $messageList[$message['lang']] = $message['strLang']; 
        } else {
            $errorCode = 'e2';
            $tpl->assign('errorCode', $errorCode);
            $tpl->display('error.tpl');
            exit();

        }
    }
}
$tpl->assign('messageList', $messageList);

/**
 * Load message catalog
 */

$lang = selectLanguage(_PGPOOL2_LANG, $messageList);
include('lang/' . $lang . '.lang.php');
$tpl->assign('message', $message);
$_SESSION[SESSION_MESSAGE] = $message;

/**
 * Open databse connection
 *
 * @param  array $param
 * @return resource
 */
function openDBConnection($param)
{
    $host= $param['hostname'];
    $port = $param['port'];
    $dbname = $param['dbname'];
    $user = $param['user'];
    $password = $param['password'];
    
    if($host != '') {
        $conStr = "host=$host port=$port dbname=$dbname user=$user password=$password" ;
    } else {
        $conStr = "port=$port dbname=$dbname" ;
    }
    
    $con = @pg_connect($conStr);
    return $con;
}

/**
 * Close database connection
 *
 * @param bool
 */
function closeDBConnection($connection)
{
    return pg_close($connection);
}

/**
 * Execute query
 *
 * @param resource $conn
 * @param string $sql
 * @return resource
 */
function execQuery($conn, $sql) {
    $rs = @pg_query($conn, $sql);
    if(!pg_result_status($rs) == PGSQL_TUPLES_OK) {
        return false;
    }

    return $rs;
}

/**
 * Select language registred in conf directory
 *
 * @return  string
 */
function selectLanguage($selectLang, $messageList) {
    if( $selectLang == null || $selectLang == 'auto') {
        if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $acceptLanguages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        } else {
            $acceptLanguages = FALSE;
        }
        
        $lang = null;
        
        if($acceptLanguages == FALSE) {
            $lang = 'en';
        } else {
            $langList = split(',|;', $acceptLanguages);
            foreach($langList as $acceptLanguage) {
                foreach(array_keys($messageList) as $messageLanguage) {
                    if( $acceptLanguage == $messageLanguage ) {
                        $lang = $messageLanguage;
                        break;
                    }
                }
                if( $lang != null) break;
            }
        }
    } else {
        $lang = $selectLang;
    }
    $_SESSION[SESSION_LANG] = $lang;
    return $lang;
}

/**
 * Whether pgpool is operating in the parallel mode or not?
 *
 * @return bool
 */
function isParallelMode() {
    
    $params = readConfigParams(array('parallel_mode'));

    if($params['parallel_mode'] == 'true') {
        return true;
    } else {
        return false;
    }
}

/**
 * Confirmation whether node is active or is not.
 *
 * @return  bool
 */
function NodeActive($num) {
    $healthCheckDb = 'template1';
    
    $params = readHealthCheckParam();
    
    $healthCheckUser = $params['health_check_user'];
    $backendHostName = $params['backend_hostname'][$num];
    $backendPort = $params['backend_port'][$num];
    if($backendHostName != '') {
        $conStr = "dbname=$healthCheckDb user=$healthCheckUser host=$backendHostName port=$backendPort" ;
    } else {
        $conStr = "dbname=$healthCheckDb port=$backendPort" ;
    }

    $conn = @pg_connect($conStr);

    if($conn == FALSE) {
        @pg_close($conn);
        return FALSE;
    } else {
        @pg_close($conn);
        return TRUE;
    }
}

/**
 * Read parameter from pgpool.conf using health check 
 *
 * @return  array
 */
function readHealthCheckParam() {
    
    $params = readConfigParams(array('health_check_user',
                                                  'backend_hostname',
                                                  'backend_port',
                                                  'backend_weight'));
    
    return $params;
}

/**
 * Existence confirmation of pgpool.pid
 *
 * @return  bool
 */
function DoesPgpoolPidExist() {
    $pidFile = readLogDir() . '/pgpool.pid';
    if( file_exists($pidFile) ) {
        return true;
    }
    return false;
}

/**
 * Existence confirmation of pgpool.pid
 *
 * @return  bool
 */
function readLogDir() {
    
    $params = readConfigParams(array('logdir'));
    return $params['logdir'];
}

/**
 * Whether pgpool is operating in the replication mode or not?
 *
 * @return bool
 */
function isReplicationMode() {
    
    $params = readConfigParams(array('replication_mode'));

    if($params['replication_mode'] == 'true') {
        return true;
    } else {
        return false;
    }
}

/**
 * Whether pgpool is operating in the master slave mode or not?
 *
 * @return bool
 */
function isMasterSlaveMode() {
    
    $params = readConfigParams(array('master_slave_mode'));

    if($params['master_slave_mode'] == 'true') {
        return true;
    } else {
        return false;
    }
}

/**
 * Read parameters specified in $paramList from pgpool.conf.
 * If $paramList is not specified, all item is read from pgpool.conf.
 *
 * @param array $paramList
 * @return array
 */
function readConfigParams($paramList = FALSE) {

    $results = array();
    $configParam = array();
    
    $configFile = @file(_PGPOOL2_CONFIG_FILE);
    if($configFile == false) {
        $errTpl = new Smarty();
        $errTpl->assign('message', $_SESSION[SESSION_MESSAGE]);
        $errorCode = 'e4';
        $errTpl->assign('errorCode', $errorCode);
        $errTpl->display('error.tpl');
        exit();
    }
    
    foreach ($configFile as $line_num => $line) {
        $line = trim($line);
        if(preg_match("/^\w/", $line)) {
            list($key, $value) = split("=", $line);
            
            $key = trim($key);
            $value = trim($value);
            
            if(preg_match("/^backend_hostname/", $key)) {
                $num = str_replace('backend_hostname', '', $key);
                $configParam['backend_hostname'][$num] = ereg_replace("'", "", $value);
            }
            else if(preg_match("/^backend_port/", $key)) {
                $num = str_replace('backend_port', '', $key);
                $configParam['backend_port'][$num] = $value;
            }
            else if(preg_match("/^backend_weight/", $key)) {
                $num = str_replace('backend_weight', '', $key);
                $configParam['backend_weight'][$num] = $value;
            }
            else {
                $configParam[$key] = ereg_replace("'", "", $value);
            }
        }
    }
    
    if(is_array($paramList)) {
        foreach($paramList as $key) {
            if(isset($configParam[$key])) {
                $results[$key] = $configParam[$key];
            } else {
                include('definePgpoolConfParam.php');
                if(!preg_match("/^backend_hostname/", $key)
                  && !preg_match("/^backend_port/", $key)
                  && !preg_match("/^backend_weight/", $key)) {
                    $results[$key] = $pgpoolConfigParam[$key]['default'];
                }
            }
        }
    } else {
        $results = $configParam;
    }
    
    return $results;
    
}

?>