+    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;
+}
+
+function jirafeau_export_cfg($cfg)
+{
+    $content = '<?php' . NL;
+    $content .= '/* This file was generated by the install process. ' .
+               'You can edit it. Please see config.original.php to understand the ' .
+               'configuration items. */' . NL;
+    $content .= '$cfg = ' . var_export($cfg, true) . ';';
+
+    $fileWrite = file_put_contents(JIRAFEAU_CFG, $content);
+
+    if (false === $fileWrite) {
+        jirafeau_fatal_error(t('Can not write local configuration file'));
+    }
+}
+
+function jirafeau_mkdir($path)
+{
+    return !(!file_exists($path) && !@mkdir($path, 0755));
+}
+
+/**
+ * Returns true whether the path is writable or we manage to make it
+ * so, which essentially is the same thing.
+ * @param $path is the file or directory to be tested.
+ * @return true if $path is writable.
+ */
+function jirafeau_is_writable($path)
+{
+    /* "@" gets rid of error messages. */
+    return is_writable($path) || @chmod($path, 0777);
+}
+
+function jirafeau_check_var_dir($path)
+{
+    $mkdir_str1 = t('CANNOT_CREATE_DIR') . ':';
+    $mkdir_str2 = t('MANUAL_CREATE');
+    $write_str1 = t('DIR_NOT_W') . ':';
+    $write_str2 = t('You should give the write permission to the web server on ' .
+                    'this directory.');
+    $solution_str = t('HERE_SOLUTION') . ':';
+
+    if (!jirafeau_mkdir($path) || !jirafeau_is_writable($path)) {
+        return array('has_error' => true,
+                      'why' => $mkdir_str1 . '<br /><code>' .
+                               $path . '</code><br />' . $solution_str .
+                               '<br />' . $mkdir_str2);
+    }
+
+    foreach (array('files', 'links', 'async') as $subdir) {
+        $subpath = $path.$subdir;
+
+        if (!jirafeau_mkdir($subpath) || !jirafeau_is_writable($subpath)) {
+            return array('has_error' => true,
+                          'why' => $mkdir_str1 . '<br /><code>' .
+                                   $subpath . '</code><br />' . $solution_str .
+                                   '<br />' . $mkdir_str2);
+        }
+    }
+
+    return array('has_error' => false, 'why' => '');
+}
+
+function jirafeau_add_ending_slash($path)
+{
+    return $path . ((substr($path, -1) == '/') ? '' : '/');
+}
+
+function jirafeau_default_web_root()
+{
+    return $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__), '', $_SERVER['REQUEST_URI']);