87 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
	error_reporting(E_ALL);
 | 
						|
	date_default_timezone_set('Europe/Paris');
 | 
						|
	require 'settings.php';
 | 
						|
 | 
						|
	function removefromindex($title)
 | 
						|
	{
 | 
						|
		global $dir;
 | 
						|
		$key = array_search($dir . '/' . $title, $_SESSION['index']);
 | 
						|
		if ($key !== FALSE)
 | 
						|
		{
 | 
						|
			array_splice($_SESSION['index'], $key, 1);
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	function downloadnote($path)
 | 
						|
	{
 | 
						|
		header('Content-disposition: attachment; filename=' . basename($path));
 | 
						|
		header('Content-type: text/markdown');
 | 
						|
		readfile($path);
 | 
						|
	}
 | 
						|
 | 
						|
	function linksdiv($content)
 | 
						|
	{
 | 
						|
		$divcontent = '<div class="grey">';
 | 
						|
		$links = array();
 | 
						|
		if (preg_match_all('/\[\[(.*)\]\]/', $content, $links, PREG_SET_ORDER))
 | 
						|
		{
 | 
						|
			foreach($links as $link)
 | 
						|
			{
 | 
						|
				$divcontent .=  '<div>
 | 
						|
				<a href="index.php?open=true¶m=' . urlencode($link[1]) . '">' . $link[1] . '</a>
 | 
						|
				</div>';
 | 
						|
			}
 | 
						|
		}
 | 
						|
		$links = array();
 | 
						|
		if (preg_match_all('/(https?:.*)\b/', $content, $links, PREG_SET_ORDER))
 | 
						|
		{
 | 
						|
			foreach($links as $link)
 | 
						|
			{
 | 
						|
				$url =  $link[1];
 | 
						|
				$title = webpagetitle($url);
 | 
						|
				$divcontent .=  '<div>
 | 
						|
				<a target="_blank" href="' . $url . '">' . $title . '</a>
 | 
						|
				</div>';
 | 
						|
			}
 | 
						|
		}
 | 
						|
		$divcontent .= '</div>';
 | 
						|
		return $divcontent;
 | 
						|
	}
 | 
						|
 | 
						|
	function computepreview($title, $content)
 | 
						|
	{
 | 
						|
		require 'libs/Parsedown.php';
 | 
						|
		$pos = 0;
 | 
						|
		if (str_starts_with($content, '---'))
 | 
						|
		{
 | 
						|
			$pos = strpos($content, '---', 3) + 3;
 | 
						|
		}
 | 
						|
		$Parsedown = new Parsedown();
 | 
						|
		$Parsedown->setBreaksEnabled(true);
 | 
						|
		$preview = $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos));
 | 
						|
		return $preview;
 | 
						|
	}
 | 
						|
 | 
						|
	// authent
 | 
						|
	if ($password && (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] != $password))
 | 
						|
	{
 | 
						|
		header('WWW-Authenticate: Basic realm="bbn"');
 | 
						|
		header('HTTP/1.0 401 Unauthorized');
 | 
						|
		echo '<p>Access denied.</p></body></html>';
 | 
						|
		exit;
 | 
						|
	}
 | 
						|
 | 
						|
	// reindex if needed
 | 
						|
	session_start();
 | 
						|
	if (!isset($_SESSION['index']) || isset($_GET['reindex']) || empty($_SESSION['index']))
 | 
						|
	{
 | 
						|
		$files = glob($dir . '/*.md');
 | 
						|
		usort($files, function($a, $b)
 | 
						|
		{
 | 
						|
			return filemtime($b) - filemtime($a);
 | 
						|
		});
 | 
						|
		$_SESSION['index'] = array_map(fn($value): string => str_replace('.md', '', $value), $files);
 | 
						|
	}
 | 
						|
?>
 |