1<?php
2
3# This file is a part of RackTables, a datacenter and server room management
4# framework. See accompanying file "COPYING" for the full copyright and
5# licensing information.
6
7/*
8*
9* This file is a library needed by all modules of RackTables (installer, upgrader)
10* to make them able to find code and data.
11*
12*/
13
14// required by exceptions.php
15function isCLIMode()
16{
17	return ! isset ($_SERVER['REQUEST_METHOD']);
18}
19
20require_once 'exceptions.php';
21require_once 'interface-lib.php';
22
23// Always have default values for these options, so if a user didn't
24// care to set, something would be working anyway.
25$user_auth_src = 'database';
26$require_local_account = TRUE;
27
28$racktables_rootdir = realpath (dirname (__FILE__) . '/..'); // you can not override this
29# Below are default values for several paths. The right way to change these
30# is to add respective line(s) to secret.php, unless this is a "shared
31# code, multiple instances" deploy, in which case the paths could be changed
32# in the custom entry point wrapper (like own index.php)
33if (! isset ($racktables_staticdir)) // the directory containing 'pix', 'js', 'css' dirs
34	$racktables_staticdir = $racktables_rootdir;
35if (! isset ($racktables_gwdir)) // the directory containing the 'telnet' and 'ssh' scripts
36	$racktables_gwdir = realpath ($racktables_rootdir . '/../gateways');
37if (! isset ($racktables_confdir)) // the directory containing secret.php (default is wwwroot/inc)
38	$racktables_confdir = dirname (__FILE__);
39if (! isset ($path_to_secret_php)) // you can overrride the path to secret.php separately from $racktables_confdir (legacy feature)
40	$path_to_secret_php = $racktables_confdir . '/secret.php';
41if (! isset ($racktables_plugins_dir)) // the directory where RT will load additional *.php files (like local.php) from
42	$racktables_plugins_dir = realpath ($racktables_rootdir . '/../plugins');
43
44// secret.php may be missing, generally it is OK
45if (fileSearchExists ($path_to_secret_php))
46{
47	$found_secret_file = TRUE;
48	require_once $path_to_secret_php;
49}
50else
51	$found_secret_file = FALSE;
52
53// determine local paths after loading of secret.php (maybe it has overrided racktables_plugins_dir)
54if (! isset ($local_gwdir)) // the directory where RT will search gateway scripts if not found in $racktables_gwdir
55	$local_gwdir = $racktables_plugins_dir . '/gateways';
56if (! isset ($local_staticdir)) // the directory where RT will search static files (js/*, css/*, pix/*) if not found in $racktables_staticdir
57	$local_staticdir = $racktables_plugins_dir;
58
59// (re)connects to DB, stores PDO object in $dbxlink global var
60function connectDB()
61{
62	global $dbxlink, $pdo_dsn, $db_username, $db_password, $pdo_bufsize, $pdo_ssl_key, $pdo_ssl_cert, $pdo_ssl_ca;
63	$dbxlink = NULL;
64	$drvoptions = array
65	(
66		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
67		// Cancel one specific SQL mode option that RackTables has been non-compliant
68		// with, but which used to be off by default until MySQL 5.7. As soon as
69		// respective SQL queries and table columns become compliant with those options
70		// stop changing @@SQL_MODE but still keep SET NAMES in place.
71		// RackTables requires the strict SQL mode, which is not enabled by default
72		// in MariaDB <= 10.2.3.
73		PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8", @@SQL_MODE = CONCAT("STRICT_ALL_TABLES,", REPLACE(@@SQL_MODE, "NO_ZERO_DATE", ""))',
74	);
75	if (isset ($pdo_bufsize))
76		$drvoptions[PDO::MYSQL_ATTR_MAX_BUFFER_SIZE] = $pdo_bufsize;
77	if (isset ($pdo_ssl_key))
78		$drvoptions[PDO::MYSQL_ATTR_SSL_KEY] = $pdo_ssl_key;
79	if (isset ($pdo_ssl_cert))
80		$drvoptions[PDO::MYSQL_ATTR_SSL_CERT] = $pdo_ssl_cert;
81	if (isset ($pdo_ssl_ca))
82		$drvoptions[PDO::MYSQL_ATTR_SSL_CA] = $pdo_ssl_ca;
83	try
84	{
85		$dbxlink = new PDO ($pdo_dsn, $db_username, $db_password, $drvoptions);
86	}
87	catch (PDOException $e)
88	{
89		throw new RackTablesError ("Database connection failed:\n\n" . $e->getMessage(), RackTablesError::INTERNAL);
90	}
91}
92
93// tries to guess the existance of the file before the php's include using the same searching method.
94// in addition to calling file_exists, searches the current file's directory if the path is not looks
95// like neither absolute nor relative.
96function fileSearchExists ($filename)
97{
98	if (! preg_match ('@^(\.+)?/@', $filename))
99	{
100		$this_file_dir = dirname (__FILE__);
101		if (file_exists ($this_file_dir . '/' . $filename))
102			return TRUE;
103	}
104	return file_exists ($filename);
105}
106