+ foreach (str_split($hex, 2) as $pair) {
+ $b .= chr(hexdec($pair));
+ }
+ return base64_encode($b);
+}
+
+/**
+ * Replace markers in templates.
+ *
+ * Available markers have the scheme "###MARKERNAME###".
+ *
+ * @param $content string Template text with markers
+ * @param $htmllinebreaks boolean Convert linebreaks to BR-Tags
+ * @return Template with replaced markers
+ */
+function jirafeau_replace_markers($content, $htmllinebreaks = false)
+{
+ $patterns = array(
+ '/###ORGANISATION###/',
+ '/###CONTACTPERSON###/',
+ '/###WEBROOT###/'
+ );
+ $replacements = array(
+ $GLOBALS['cfg']['organisation'],
+ $GLOBALS['cfg']['contactperson'],
+ $GLOBALS['cfg']['web_root']
+ );
+ $content = preg_replace($patterns, $replacements, $content);
+
+ if (true === $htmllinebreaks) {
+ $content = nl2br($content);
+ }
+
+ return $content;
+}
+
+function jirafeau_escape($string)
+{
+ return htmlspecialchars($string, ENT_QUOTES);
+}
+
+function jirafeau_admin_session_start()
+{
+ $_SESSION['admin_auth'] = true;
+ $_SESSION['admin_csrf'] = md5(uniqid(mt_rand(), true));
+}
+
+function jirafeau_admin_session_end()
+{
+ $_SESSION = array();
+ session_destroy();
+}
+
+function jirafeau_admin_session_logged()
+{
+ return isset($_SESSION['admin_auth']) &&
+ isset($_SESSION['admin_csrf']) &&
+ isset($_POST['admin_csrf']) &&
+ $_SESSION['admin_auth'] === true &&
+ $_SESSION['admin_csrf'] === $_POST['admin_csrf'];
+}
+
+function jirafeau_admin_csrf_field()
+{
+ return "<input type='hidden' name='admin_csrf' value='". $_SESSION['admin_csrf'] . "'/>";
+}
+
+function jirafeau_dir_size($dir)
+{
+ $size = 0;
+ foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $entry) {
+ $size += is_file($entry) ? filesize($entry) : jirafeau_dir_size($entry);
+ }
+ return $size;