';
switch ($e->getCode()) {
case 2002:
echo "Unable to connect to database, perhaps host info is wrong.\n";
break;
case 1044:
case 1045:
case 1698:
// access denied to db or generic access denied
echo "Unable to connect to database, perhaps username or password is wrong.\n";
break;
case 1049:
echo "Unable to connect to database, perhaps database name is wrong.\n";
break;
default:
print_r($e);
}
echo '
';
echo '';
exit(1);
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
# $pdo->query("SET lc_time_names='de_DE'");
try {
$sth = $pdo->query("SELECT valint FROM settings WHERE userid=0 AND sno=0");
} catch (PDOException $e) {
if ($e->getCode() == 1146) {
echo "
\n";
echo "Settings table not found! Perhaps database is not initialized?\n";
echo "
\n";
exit(1);
}
}
$g_db_scheme_version = $sth->fetchColumn();
$g_db_scheme_required = 1;
// Schema version check
if ($g_db_scheme_version !== $g_db_scheme_required) {
// TODO if required schema version is smaller than found version then
// an database scheme update is neccessary
// otherwise our program code is outdated an should be updated
echo "
\n";
echo "Database schema is wrong: detected version $g_db_scheme_version, but required version $g_db_scheme_required!\n";
echo "
\n";
exit(1);
}
// currency symbol is global
$sth = $pdo->query("SELECT valint FROM settings WHERE userid=0 AND sno=4");
$g_currsym = $sth->fetchColumn();
// allowed date formats
$g_opt_datefmt = array(
'Y-m-d' => _('YYYY-MM-DD'), // ISO / international /technical
'd.m.Y' => _('DD.MM.YYYY'), // Germany / Central & Northern Europe
'd/m/Y' => _('DD/MM/YYYY'), // UK / France / Spain / Italy / Australia / NZ
'd-m-Y' => _('DD-MM-YYYY'), // Netherlands / Denmark / Belgium
'd M Y' => _('DD Mon YYYY'), // nautical documents / logbooks / unambiguous
'm.d.Y' => _('MM.DD.YYYY'), // USA
'm/d/Y' => _('MM/DD/YYYY'), // USA
'Y/m/d' => _('YYYY/MM/DD') // Japan / China
);
$user = User::GetInstance(); // There can be only one
if ($g_scriptname != 'login.php') {
$user->init_from_session();
$user->load_vessel();
$user->load_settings();
}
$action = NULL;
$valid = FALSE;
// document storage
$g_doc_mimetypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf'];
// form option lists
$g_opt_all = array(-2 => _('― all ―'));
$g_opt_none = array(-1 => _('― none ―'));
$g_opt_empty = array('' => _('― none ―'));
$g_opt_unknown = array(-1 => _('― unknown ―'));
$g_opt_new = array(-3 => _('― new ―'));
// Initialize message system
$g_message = new Message;
$g_success = new MessageSuccess;
$g_warning = new MessageWarning;
$g_error = new MessageError;
// ========== MESSAGE FUNCTIONS ===============================================
class Message {
var $count = 0;
var $text = array();
var $caption;
var $alertclass;
function __construct() {
$this->caption = _('Information');
$this->alertclass = 'alert-info';
if (isset($_SESSION['message']['info'])) {
$this->Add($_SESSION['message']['info']);
unset($_SESSION['message']['info']);
}
}
function SetCaption($str) {
$this->caption = $str;
}
function Add($msg) {
$this->count++;
$this->text[$this->count] = $msg;
}
function GetCount() {
return $this->count;
}
function PrintOut() {
if ($this->count > 0) {
echo '
', "\n";
echo '
', $this->caption, "
\n";
echo "
\n";
for ($i=1; $i<=$this->count; $i++) {
echo "\t
", $this->text[$i],"
\n";
}
echo "
\n";
echo '', "\n";
echo "
\n";
}
}
}
class MessageSuccess extends Message {
function __construct() {
$this->caption = _('Success');
$this->alertclass = 'alert-success';
if (isset($_SESSION['message']['succ'])) {
$this->Add($_SESSION['message']['succ']);
unset($_SESSION['message']['succ']);
}
}
}
class MessageWarning extends Message {
function __construct() {
$this->caption = _('Warning');
$this->alertclass = 'alert-warning';
if (isset($_SESSION['message']['warn'])) {
$this->Add($_SESSION['message']['warn']);
unset($_SESSION['message']['warn']);
}
}
}
class MessageError extends Message {
function __construct() {
$this->caption = _('Error');
$this->alertclass = 'alert-danger';
if (isset($_SESSION['message']['err'])) {
$this->Add($_SESSION['message']['err']);
unset($_SESSION['message']['err']);
}
}
}
// ========== USER FUNCTIONS ==================================================
class User {
private static $instance = NULL;
private $loggedin;
private $errormessage;
public $id;
public $login;
public $displayname;
public $role;
public $flags;
public $vid; // current selected vessel
public $vessel; // name of current vessel
public $datefmt;
public $rows_pp;
public $menu;
public $language;
private function __construct() {
global $g_rows_pp;
$this->loggedin = FALSE;
$this->errormessage = NULL;
$this->flags = [];
$this->vid = 1;
$this->vessel = 'Yacht';
$this->datefmt = 'Y-m-d';
$this->rows_pp = $g_rows_pp;
$this->menu = [1, 2, 3, 4, 5, 6];
$this->language = 'en';
}
public static function getInstance() {
if (self::$instance == NULL) {
self::$instance = new User();
}
return self::$instance;
}
function login($user_name, $user_pass) {
global $pdo;
if ($user_name == '' or $user_pass == '') {
$this->loggedin = FALSE;
$this->errormessage = _('Username or password was empty');
return FALSE;
}
$sql = "SELECT userid, pass, displayname, role, flags FROM user WHERE login=?";
$sth = $pdo->prepare($sql);
$sth->execute([$user_name]);
$row = $sth->fetch(PDO::FETCH_OBJ);
$this->role = $row->role;
$this->flags = array_filter(explode(',', $row->flags));
if (($row->flags != 0) and ($row->flags != '')) {
// deleted or locked, intentionally not return detailed reason
$this->errormessage = _('User account does not exist');
} elseif (password_verify($user_pass, $row->pass)) {
// password check successful
$_SESSION['userid'] = $row->userid;
$this->loggedin = TRUE;
$target = $_SESSION['prelogin'] ?? 'index.php';
unset($_SESSION['prelogin']);
header_location($target);
} else {
$this->errormessage = _('Username or password invalid');
}
return FALSE;
}
function logout() {
global $pdo;
// remove session completely
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
}
function init_from_session() {
global $pdo;
if (!isset($_SESSION['userid'])) {
$this->loggedin = FALSE;
$this->errormessage = _('User not logged in');
$this->id = NULL;
return;
}
$this->id = $_SESSION['userid'];
$sql = "SELECT displayname, role, language FROM user WHERE userid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id]);
$row = $sth->fetch();
if ($row) {
$this->displayname = $row['displayname'];
$this->role = $row['role'];
$this->language = $row['language'];
}
}
function load_vessel() {
global $pdo;
$sql = "SELECT v.vid, v.vesselname "
. "FROM settings AS s INNER JOIN vessel AS v ON s.valint=v.vid "
. "WHERE s.userid=? and s.sno=1";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id]);
$row = $sth->fetch();
if ($row) {
$this->vid = $row['vid'];
$this->vessel = $row['vesselname'];
}
}
function load_settings() {
global $pdo;
// additional user settings
// date format from user settings
$sql = "SELECT valstr FROM settings WHERE userid=? AND sno=2";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id]);
$user_datefmt = $sth->fetchColumn();
if ($user_datefmt) {
$this->datefmt = $user_datefmt;
}
// pagination rows from user settings
$sql = "SELECT valint FROM settings WHERE userid=? AND sno=10";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id]);
$user_rows_pp = $sth->fetchColumn();
if ($user_rows_pp) {
$this->rows_pp = $user_rows_pp;
}
// user defined menu
$sql = "SELECT valstr FROM settings WHERE userid=? AND sno=3";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id]);
$res = $sth->fetchColumn();
if ($res === false) {
$sql = "INSERT INTO settings (userid, sno, valstr) VALUES (?, 3, ?)";
$sth = $pdo->prepare($sql);
$sth->execute([$this->id, implode(',', $this->menu)]);
} else {
$this->menu = array_map('intval', explode(',', $res));
}
}
function get_last_error() {
return $this->errormessage;
}
}
function get_userlist() {
// Get list of all users for fast name lookup
global $pdo;
$ulist = array(-1 => ['-', 0]); // unknown
$sql = "SELECT userid, displayname, role, flags FROM user ORDER BY displayname";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
foreach ($res as $row) {
$flags = array_filter(explode(',', $row['flags']));
$deleted = in_array('deleted', $flags);
$locked = in_array('locked', $flags);
$ulist[$row['userid']] = [$row['displayname'], $deleted, $locked];
}
return $ulist;
}
// ========== DATABASE FUNCTIONS ==============================================
// TODO transfer to lib/db.inc
function db_load_enum($table, $column, $lookup=false, $as_dict=false, $check_null=false) {
// returns array of enum-values as defined in database
// if lookup is true the name lookup and translaton is applied
// if as_dict is true the enum value is returned as the array key
// if check_null is true the nullable-check is applied and returned
// works also for set datatype
global $pdo;
$sql = "SELECT TRIM(TRAILING ')' FROM SUBSTRING(column_type,6)), is_nullable "
. "FROM information_schema.columns "
. "WHERE table_schema=DATABASE() AND table_name=? AND column_name=?";
$sth = $pdo->prepare($sql);
$sth->execute([$table, $column]);
$row = $sth->fetch(PDO::FETCH_NUM);
// for PHP < 7.4
// return array_map(function($x) { return trim($x, "'"); }, explode(',', $row[0]));
// for PHP => 7.4
$arr = array_map(fn($x) => trim($x, "'"), explode(',', $row[0]));
// prepend special value NULL if column is nullable
if ($check_null and $row[1] == 'YES') {
array_unshift($arr, 'NULL');
}
if ($lookup) {
if ($as_dict) {
$dict = array();
foreach ($arr as $x) {
$dict[$x] = get_enum($x);
}
return $dict;
} else {
return array_map(fn($x) => get_enum($x), $arr);
}
} else {
return $arr;
}
}
function db_load_taglist($objtype, $objid) {
// returns comma separated list for object
global $pdo;
global $g_error;
$taglist = '';
$sql = "SELECT GROUP_CONCAT(tagname) "
. "FROM tagref JOIN tag USING (tagid) "
. "WHERE objtype=? AND objid=?";
$sth = $pdo->prepare($sql);
try {
$sth->execute([$objtype, $objid]);
$taglist = $sth->fetchColumn();
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
}
return $taglist;
}
function db_save_taglist($objtype, $objid, $taglist) {
global $pdo;
global $g_error;
// WIP
// - taglist is comma separated string
// - create not existing tags
// - update tagref: create new, remove unused
$tags = array_filter(explode(',', preg_replace('/\s+/u', '', $taglist)));
if (empty($tags)) {
// remove all from tagref
$sth = $pdo->prepare("DELETE FROM tagref WHERE objtype=? AND objid=?");
try {
$sth->execute([$objtype, $objid]);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
}
} else {
// get list of non existing tags
$in = str_repeat('?,', count($tags) - 1) . '?';
$sql = "SELECT tagid, tagname FROM tag WHERE tagname IN ($in)";
$sth = $pdo->prepare($sql);
try {
$sth->execute(array_values($tags));
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
}
$tagids = array();
$existing = array();
foreach ($sth->fetchAll() as $row) {
$existing[] = $row['tagname'];
$tagids[] = $row['tagid'];
}
$newtags = array_diff($tags, $existing);
$sth = $pdo->prepare("INSERT INTO tag (tagname) VALUES (?)");
foreach ($newtags as $t) {
// maxlength 20 for each tag!
$sth->execute([substr($t, 0, 20)]);
$tagids[] = $pdo->LastInsertId();
}
// update references
$sql = "SELECT tagid FROM tagref WHERE objtype=? AND objid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$objtype, $objid]);
$refids = array();
$obsolete = array();
foreach ($sth->fetchAll() as $row) {
if (in_array($row['tagid'], $tagids)) {
// mark existing used references
$refids[] = $row['tagid'];
} else {
// obsolete reference
$obsolete[] = $row['tagid'];
}
}
// create missing references
$missing = array_diff($tagids, $refids);
if (! empty($missing)) {
$sql = "INSERT INTO tagref (tagid, objid, objtype) VALUES (?, ?, ?)";
$sth = $pdo->prepare($sql);
foreach ($missing as $m) {
$sth->execute([$m, $objid, $objtype]);
}
}
// remove obsolete references
if (! empty($obsolete)) {
$sql = "DELETE FROM tagref WHERE tagid=? AND objid=? and objtype=?";
$sth = $pdo->prepare($sql);
foreach ($obsolete as $o) {
$sth->execute([$o, $objid, $objtype]);
}
}
}
}
function db_get_tag_target($objtype, $objid) {
// get page name and desctiption e.g. for link creation
global $pdo;
global $g_error;
switch ($objtype) {
case 'equip':
$sql = "SELECT ename FROM equipment WHERE eid=?";
$target = "equipment.php";
break;
case 'inv':
$sql = "SELECT invname FROM inventory WHERE invid=?";
$target = "inventory.php";
break;
case 'prov':
$sql = "SELECT provname FROM provisions WHERE provvid=?";
$target = "provisions.php";
break;
case 'doc':
$sql = "SELECT title FROM document WHERE docid=?";
$target = "documents.php";
break;
case 'proj':
$sql = "SELECT projname FROM project WHERE projid=?";
$target = "projects.php";
break;
case 'task':
$sql = "SELECT taskname FROM task WHERE taskid=?";
$target = "task.php";
break;
case 'maint':
$sql = "SELECT activities FROM maintenance WHERE maintid=?";
$target = 'maintenance.php';
break;
}
$sth = $pdo->prepare($sql);
$sth->execute([$objid]);
$desc = $sth->fetchColumn();
return [$target, $desc];
}
function db_get_default_storage($vesselid, $fallback=true) {
global $pdo;
global $g_error, $g_warning;
$sql = "SELECT sid_default FROM vessel WHERE vid=?";
$sth = $pdo->prepare($sql);
try {
$sth->execute([$vesselid]);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
return false;
}
$sid = $sth->fetchColumn();
if ($sid == NULL && $fallback) {
// try to get another storage as last fallback
$g_warning->Add(_('No default storage defined for current vessel'));
$sql = "SELECT MIN(sid), sname FROM storage WHERE vid=?";
$sth = $pdo->prepare($sql);
try {
$sth->execute([$vesselid]);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
return false;
}
$row = $sth->fetch(PDO::FETCH_NUM);
if ($row[0] == NULL) {
$g_error->Add('Error: No storage defined for current vessel');
return false;
}
$sid = $row[0];
$g_warning->Add(sprintf(_("Selected fallback storage '%s'"), $row[1]));
}
return $sid;
}
function date_to_sql($var) {
if (!isset($var) || $var == '') {
return NULL;
} else {
list($d, $m, $y) = explode('.', $var);
return sprintf("%04d-%02d-%02d", $y, $m, $d);
}
}
function float_to_sql($var) {
global $g_lconv;
if (!isset($var) || $var == '') {
return NULL;
} else {
// remove thousands separator
$var = str_replace($g_lconv['thousands_sep'], '', $var);
// force decimal point
if ($g_lconv['decimal_point'] != '.') {
$var = str_replace($g_lconv['decimal_point'], '.', $var);
}
return $var;
}
}
// ========== FORMAT FUNCTIONS ================================================
function format_float($val, $decimals = 2, $suffix = '') {
global $g_lconv;
if (!isset($val) || $val == '') {
return '';
}
return number_format($val, $decimals, $g_lconv['decimal_point'], $g_lconv['thousands_sep']) . ($suffix ? " $suffix" : '');
}
function format_currency($val) {
global $g_lconv;
if (!isset($val) || $val == '') {
return '';
}
if ($g_lconv['currency_symbol'] == '$') {
return '$'.number_format($val, 2, $g_lconv['decimal_point'], $g_lconv['thousands_sep']);
} else {
return number_format($val, 2, $g_lconv['decimal_point'], $g_lconv['thousands_sep']) . ' ' . $g_lconv['currency_symbol'];
}
}
function format_date($date) {
// date is string in ISO format
global $user;
if (empty($date)) {
return '';
}
$timestamp = strtotime($date);
if ($timestamp === false) {
return '';
}
return date($user->datefmt, $timestamp);
}
function format_datetime($datetime, $seconds=false) {
// datetime is string in ISO format
global $user;
if (empty($datetime)) {
return '';
}
$timestamp = strtotime($datetime);
if ($timestamp === false) {
return '';
}
$timefmt = $seconds ? 'H:i:s' : 'H:i';
return date($user->datefmt. ' ' . $timefmt, $timestamp);
}
function format_color($color) {
if (empty($color)) {
return '';
}
$colstr = '#' . $color . '' ;
return $colstr;
}
function format_filesize($size, $format=2) {
switch ($format) {
case 1: // short
if ($size < 1024) {
$size_string = sprintf(_('%d B'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('%.1f kB'), $size/1024);
} else {
$size_string = sprintf(_('%.1f MB'), $size/1048576);
}
break;
case 2: // medium
if ($size < 1024) {
$size_string = sprintf(_('%d Byte'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('%.1f kByte'), $size/1024);
} else {
$size_string = sprintf(_('%.1f MByte'), $size/1048576);
}
break;
case 3: // long
if ($size < 1024) {
$size_string = sprintf(_('%d Byte'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('approx. %.1f kByte'), $size/1024);
} else {
$size_string = sprintf(_('approx. %.1f MByte'), $size/1048576);
}
break;
default:
$size_string = trim($size);
}
return $size_string;
}
function format_measurement($n, $unit, $v1, $v2, $v3) {
$m = '';
switch ($n) {
case 1:
$m = "$v1 $unit";
break;
case 2:
$m = "$v1×$v2 $unit";
break;
case 3:
$m = "$v1×$v2×$v3 $unit";
break;
}
return $m;
}
// ========== COMMON FUNCTIONS ================================================
function h($value, $br=false) {
// escape value coming from db for safe html output
// optional convert line breaks to tags
if (!isset($value)) {
return '';
}
$value = htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
return $br ? nl2br($value) : $value;
}
function header_location($location, $message=NULL) {
if (is_array($message)) {
$valid_keys = array('succ', 'info', 'warn', 'err');
foreach ($message as $k => $v) {
if (in_array($k, $valid_keys)) {
$_SESSION['message'][$k] = $v;
}
}
}
header("Location: $location");
exit;
}
function get_browser_lang($allowed, $default='en') {
$h = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
if (!$h) {
return $def;
}
$langs = [];
foreach (explode(',', $h) as $p) {
$p = explode(';q=', trim($p));
$l = strtolower($p[0]);
$q = isset($p[1]) ? (float) $p[1] : 1.0;
$langs[$l] = $q;
}
arsort($langs);
foreach ($langs as $bl => $q) {
foreach ($allowed as $l) {
$l = strtolower($l);
if ($bl === $l || str_starts_with($bl, $l . '-')) {
return $l;
}
}
}
return $default;
}
function pgettext($context, $msgid) {
// implement missing function
global $g_textdomain;
$ctxstr = "{$context}\004{$msgid}";
$translation = dcgettext($g_textdomain, $ctxstr, LC_MESSAGES);
if ($translation == $ctxstr) {
return $msgid;
} else {
return $translation;
}
}
function get_enum($enum) {
// translate and beautify enums
$lookup = array(
'NULL' => _('n/a'),
'active' => _('Active'),
'bad' => _('Bad'),
'box' => _('Box'),
'cable' => _('Cable'),
'cat' => _('Catamaran'),
'completed' => _('Completed'),
'closed' => _('Closed'),
'company' => _('Company'),
'day' => _('Day'),
'defect' => _('Defect'),
'deleted' => _('Deleted'),
'done' => _('Done'),
'drawing' => _('Drawing'),
'equipment' => _('Equipment'),
'estimated' => _('Estimated'),
'excellent' => _('Excellent'),
'fair' => _('Fair'),
'finished' => _('Finished'),
'fixed' => _('Fixed'),
'fuse' => _('Fuse'),
'generic' => _('Generic'),
'good' => _('Good'),
'high' => _('High'),
'historic' => _('Historic'),
'invoice' => _('Invoice'),
'locked' => _('Locked'),
'low' => _('Low'),
'manual' => _('Manual'),
'medium' => _('Medium'),
'missed' => _('Missed'),
'mono' => _('Monohull'),
'month' => _('Month'),
'new' => _('New'),
'none' => _('None'),
'normal' => _('Normal'),
'ongoing' => _('Ongoing'),
'open' => _('Open'),
'ordered' => _('Ordered'),
'paused' => _('Paused'),
'pending' => _('Pending'),
'picture' => _('Picture'),
'plan' => _('Planned'),
'precise' => _('Precise'),
'project' => _('Project'),
'quarter' => _('Quarter'),
'removed' => _('Removed'),
'repairable' => _('Repairable'),
'rough' => _('Rough'),
'skipped' => _('Skipped'),
'storage' => _('Storage'),
'task' => _('Task'),
'tri' => _('Trimaran'),
'unknown' => _('Unknown'),
'user' => _('User'),
'vessel' => _('Vessel'),
'waiting' => _('Waiting'),
'week' => _('Week'),
);
return $lookup[$enum] ?? $enum;
}
function get_color_brightness($color, $default=0.5) {
// returns a value between 0 and 1
// color is a 6 char hex string
if (strlen($color) != 6) {
return $default;
}
list($red, $green, $blue) = array_map('hexdec', str_split($color, 2));
$red = $red / 255 * 0.2126;
$green = $green / 255 * 0.7152;
$blue = $blue / 255 * 0.0722;
return $red + $green + $blue;
}
function page_caption_search($caption) {
// print page caption with searchbox
?>
=$caption?>
' . "\n";
$out .= '
' . "\n";
// First Prev ... Next Last
// TODO build entries for first, prev, next last
if ($page > 1) {
// Go to first page or previous page
$out .= '