Files

59 lines
1.9 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2026 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************
*/
// ========== SVG-FUNCTIONS ===================================================
function create_shapeindex($shapeindexfile) {
// an existing index file will be overwritten
global $g_shapedir;
foreach (glob($g_shapedir.'/*.svg') as $f) {
$fh = fopen($f, 'r');
$fname = basename($f);
// try to read metadata from top of svg file
$header = fread($fh, 1024);
if (preg_match('/<title[^>]*>(.*?)<\/title>/is', $header, $match)) {
$title = trim($match[1]);
} else {
$title = $fname;
}
$shapeindex[$fname] = $title;
fclose($fh);
}
$json = json_encode($shapeindex, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents($shapeindexfile, $json);
return $shapeindex;
}
function get_shapeindex() {
global $g_doc_basepath;
$shapeindex = [];
$shapeindexfile = $g_doc_basepath . '/shapeindex.json';
if (!file_exists($shapeindexfile)) {
$shapeindex = create_shapeindex($shapeindexfile);
} else {
// check if shapeindex needs refresh (older than one day)
if (time() - filemtime($shapeindexfile) > 86400) {
$shapeindex = create_shapeindex($shapeindexfile);
} else {
// read svg metadata from existing file
$json = file_get_contents($shapeindexfile);
$shapeindex = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// some kind of error: just recreate
$shapeindex = create_shapeindex($shapeindexfile);
}
}
}
return $shapeindex;
}
function svg_create_box($cx, $cy, $w, $h, $fg, $bg) {
}