simpub/shared.php

198 lines
7.6 KiB
PHP

<?php
define('PAGE_NAME', '[Simpub CMS]'); // Title of the page
define("PAGE_URL", "notsyncing.net"); // Base URL of the page
define('PAGE_AUTHOR', 'Simpub CMS'); // Author of the page (used in footer)
define('HOME_PAGE', 'blog'); // Entry page (and link for logo)
define('CACHE_POSTS_TTL', 3600); // Time to cache the posts list (1h)
define('POSTS_BASE', "/b/"); // Root directory of the posts (relative)
define('PREVIEW_BASE', "/pimg/"); // Directory to store auto-generated thumbnails
define('POSTS_ROOT', $_SERVER['DOCUMENT_ROOT'] . POSTS_BASE); // Root directory of the posts (absolute)
define('POSTS_PAGE_SIZE', 10); // Numbers of posts on a page
define('POSTS_DIR_SEP', "."); // Directory separator (/) in post names
define('POSTS_MAX_FILENAME', 30); // Maximum length of a post name (including suffix)
define('POSTS_TAG_SEARCH', 30); // Maximum length for the tag search argument
define('RSS_ENABLED', true); // Enable the RSS news feed
define('RSS_REFRESH_INTERVAL', 14400); // Advertised update interval for the RSS feed
define('RSS_SHOW_FULL', false); // Show extended section in RSS feed
define('RSS_DATE_FORMAT', 'D, d M Y H:i:s O');
$menuItems = array(
array("label" => "Page A", "link" => "?p=00-template", "highlight" => "related-subpages,00-template"),
array("label" => "Page B", "link" => "?p=404", "highlight" => "404"),
array("label" => "Repository", "link" => "https://git.notsyncing.net:8080", "highlight" => ""),
array("label" => "Contact", "link" => "?p=00-template", "highlight" => "")
);
$blogDirs = array("2017");
date_default_timezone_set('Europe/Berlin');
function opted_in($identifier) {
global $optin;
return (stripos($optin, $identifier) !== false);
}
function append_param($p) {
$url = $_SERVER['REQUEST_URI'];
if (stripos($url, "?") !== false)
$url .= "&";
else
$url .= "?";
$url .= $p;
return $url;
}
/*
* This function will include an image as figure.
* returns : Number of the figure for later referencing.
* $title : Image title, will be prefixed with Figure n.
* $path : If $path starts with http or / it will be
* treated as an absolute path and no downscaled preview will be rendered.
* Other paths will be prefixed with /b/img.
* $preview : When set to "", no preview will be generated. Otherwise the specified
* path will be used. Use "+" to auto-generate.
* $link : Link to follow when clicking the image. Default is the full resolution image.
*/
$counters['figure'] = 1;
function includeGraphics($title, $path, $preview="+", $style="", $link="+") {
global $blogentry;
global $counters;
$title = "Figure " . $counters['figure'] . ". $title";
// TODO: External image
if (substr($path, 0, 4) == "http" || $path[0] == "/") {
$preview="";
$spath=$path;
}
else {
$spath = POSTS_BASE . "img/" . $path;
$fpath = $_SERVER['DOCUMENT_ROOT'] . $spath;
}
if ($preview == "+") {
$previewPath = PREVIEW_BASE . str_replace("/", "_", $path);
$fpreviewPath = $_SERVER['DOCUMENT_ROOT'] . $previewPath;
if (!file_exists($fpreviewPath)) {
preg_match("/^[A-Za-z_\.\-0-9\/]*$/", $fpath, $matches); // SANITIZED!
if (!$matches) {
echo "<p>SERVER ERROR: Image not valid!</p>";
return ;
}
$page=substr($page,0,12); // SECURITY: Max 12 chars
exec("gm convert '$fpath' -resize 640 '$fpreviewPath';");
}
}
elseif ($preview == "") {
$previewPath = $spath;
}
else {
$previewPath = $preview;
}
if ($link == "+")
$link = $spath;
echo '<figure>
<a href="' . $link . '">
<img style="' . $style . '" title="' . $title . '" style="margin: 0px;padding: 0px;" src="' . $previewPath . '">
</a>
<figcaption>' . $title . '</figcaption>
</figure>';
return $counters['figure']++;
}
/*
* This function will include a video.
* returns : Number of the video for later referencing.
* $url : URL of the video. Autodetects YouTube. Otherwise HTML5 video.
* $title : Display "Video n. $title" below the video.
* $width : Width of the player
*/
$counters['video'] = 1;
function includeVideo($url, $title="", $width=640) {
global $counters;
$vlink = "NULL";
preg_match("/.*youtube.com\/embed\/(.*)$/", $url, $matches);
if (!$matches)
preg_match("/.*youtube.com\/watch\?v=([^&]*).*$/", $url, $matches);
if ($matches) {
$youtube['embed'] = "https://www.youtube.com/embed/" . $matches[1];
$youtube['link'] = "https://www.youtube.com/watch?v=" . $matches[1];
$youtube['vid'] = $matches[1];
}
echo '<videoframe><div class="videoframe" style="width:' . $width . 'px;height:' . $width*9/16 . 'px">';
if (isset($youtube)) {
if (isset($CMS_RSS)) {
echo '<div style="width:100%;height:100%;"><table border=0 style="height:100%;width:100%;"><tr><td style="text-align:center;">Embedded Content<br><br><a href="' . $youtube['link'] . '">Click here to watch the video on YouTube.</a></td></tr></table></div>';
} else if (opted_in('y')) {
echo '<iframe width="100%" height="100%" src="' . $url . '" frameborder="0" allowfullscreen></iframe>';
} else {
$preview_file = PREVIEW_BASE . $youtube['vid'] . ".jpg";
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $preview_file)) {
file_put_contents($_SERVER['DOCUMENT_ROOT'] . $preview_file, file_get_contents("https://img.youtube.com/vi/" . $youtube['vid'] . "/maxresdefault.jpg"));
}
echo "<div style='width:100%;height:100%;background-color:black;background-image: url(" . $preview_file . ");background-size:contain;'><table border=0 style='height:100%;width:100%;background-color:rgba(0,0,0,0.55);backdrop-filter:blur(3px);text-shadow:1px 1px black;'><tr><td style='text-align:center;'>Embedded content has been disabled to protect your privacy.<br><br><br><strong><form action='' method='post'><input type='hidden' name='optin-once' value='y' /><input type='submit' value='Click here to load the videos on this site once,'/></form></strong><br><br><strong><a href='/?p=privacy-controls'>or click here to allow them permanently,</a><br><br><a href=\"" . $youtube['link'] . "\">" . "or click here to watch the video on YouTube:<br>" . $youtube['link'] . "</a></strong><br><br><br>Please note that, by enabling this video, data is transferred <br>to YouTube LLC, and is subject to their privacy policy.</td></tr></table></div>";
}
} else {
// TODO: HTML5 video
echo "SERVER ERROR: Video format not supported.";
}
echo '</div></videoframe>';
if ($title != "") {
echo '<vidcaption>Video ' . $counters['video'] . ". $title</vidcaption>";
}
return $counters['video']++;
}
/*
* This function will add a reference
* returns : Number of the source for later referencing
* $url : URL of the source, if its a number it will not create a new entry.
*/
$meta['sources'] = array();
$counters['sources'] = 1;
function addSource($url, $text="") {
global $counters;
global $meta;
if ("$text" != "")
$text .= " ";
if (is_numeric($url)) {
echo "<a href=\"#src$url\">[" . $url . "]</a>";
return $url;
}
else {
// Check whether source already exists
$i = 1;
foreach ($meta['sources'] as $v) {
if ($v == $url) {
echo "<a href=\"$url\">" . $text . "[" . $i . "]</a>";
return $i;
}
$i++;
}
// If not found, then add
$meta['sources'][$counters['sources']] = $url;
echo "<a href=\"$url\">" . $text . "[" . $counters['sources'] . "]</a>";
return $counters['sources']++;
}
}
function getServerUrl() {
$temp = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$temp .= PAGE_URL;
return $temp;
}
?>