Verified Commit 5cfc37bb authored by David Libeau's avatar David Libeau

Add view system

parent ee58b44c
......@@ -5,6 +5,7 @@ function autoloader($class) {
require_once("controllers/".$class.".php");
}
require_once("../fedilib/Parsedown.php");
require_once("../fedilib/mime_content_type_modified.php");
}
spl_autoload_register('autoloader');
?>
\ No newline at end of file
......@@ -19,34 +19,81 @@ class Content{
$this->type=$type;
$this->initDone=true;
$this->id=$id;
$contentFilePath="../".Server::get("path/Content/".$this->type."/content")."/".$this->id.".txt";
if(!file_exists($contentFilePath)){
$core->error("404","$contentFilePath was not found");
}else{
$this->content=file_get_contents($contentFilePath);
}
$dataFilePath="../".Server::get("path/Content/".$this->type."/data")."/".$this->id.".xml";
if(!file_exists($dataFilePath)){
switch($this->type){
case "Article":
//TO DO (?): modular content type
$this->data["title"]=ucfirst(str_replace(array("-","_")," ",$this->id));
$this->data["publishedDate"]=date("c",filemtime($contentFilePath));
$this->data["updatedDate"]=date("c");
$this->data["author"]=(string)Server::get("defaultPublishing/author");
file_put_contents($dataFilePath,$this->exportAtom());
break;
if($type!="Static"){
$contentFilePath="../".Server::get("path/Content/".$this->type."/content","/").$this->id.".txt";
if(!file_exists($contentFilePath)){
$core->error("404","$contentFilePath was not found");
}else{
$this->content=file_get_contents($contentFilePath);
}
$dataFilePath="../".Server::get("path/Content/".$this->type."/data","/").$this->id.".xml";
if(!file_exists($dataFilePath)){
switch($this->type){
case "Article":
//TO DO (?): modular content type
$this->data["title"]=ucfirst(str_replace(array("-","_")," ",$this->id));
$this->data["publishedDate"]=date("c",filemtime($contentFilePath));
$this->data["updatedDate"]=date("c");
$this->data["author"]=(string)Server::get("defaultPublishing/author");
file_put_contents($dataFilePath,$this->exportAtom());
break;
}
}else{
$dataFile=simplexml_load_file($dataFilePath);
$this->data["title"]=$dataFile->xpath("title/text()")[0]->__toString();
$this->data["publishedDate"]=$dataFile->xpath("published/text()")[0]->__toString();
$this->data["updatedDate"]=$dataFile->xpath("updated/text()")[0]->__toString();
$this->data["author"]=$dataFile->xpath("author/@id")[0]->__toString();
}
}
}
}
public function get($var){
return($this->$var);
}
public function display($var){
echo(Secure::that($this->$var));
}
public function view($id=null,$type="content") {
global $core;
/*
View
@params: $id, $type
*/
$this->init($id,$type);
View::render("content",$this);
if($type=="Static"){
$staticQuery=$id;
if(file_exists("../".Server::get("path/Content/Static/content","/").$staticQuery)){
$fileToRender="../".Server::get("path/Content/Static/content","/").$staticQuery;
}else{
$allFiles=glob("../".Server::get("path/Content/Static/content","/").$staticQuery.".*");
if(sizeof($allFiles)>1){
if(glob("../".Server::get("path/Content/Static/content","/").$staticQuery.".php")){
$fileToRender="../".Server::get("path/Content/Static/content","/").$staticQuery.".php";
}elseif(glob("../".Server::get("path/Content/Static/content","/").$staticQuery.".html")){
$fileToRender="../".Server::get("path/Content/Static/content","/").$staticQuery.".html";
}else{
$fileToRender=$allFiles[0];
}
}elseif(sizeof($allFiles)==1){
$fileToRender=$allFiles[0];
}
}
if(isset($fileToRender)){
ob_start();
header("Content-type: ".mime_content_type_modified ($fileToRender));
include $fileToRender;
echo(ob_get_clean());
}else{
$core->error("404","$staticQuery was not found");
}
}else{
View::render($type,$this);
}
}
public function dump($id=null) {
......@@ -64,16 +111,15 @@ class Content{
switch($this->type){
case "Article":
$authorUser=new User($this->data["author"]);
$authorName=$authorUser->get("name");
$authorUri="http://".Server::get("domain")."/".Server::get("route/User")."/".$this->data["author"];
$authorUri="http://".Server::get("domain")."/".Server::get("route/User","/").$this->data["author"];
return(
'<entry>
<id>http://'.Server::get("domain").'/'.$this->id.'</id>
<title>Test article</title>
<published>'.$this->data["publishedDate"].'</published>
<updated>'.$this->data["updatedDate"].'</updated>
<author>
<name>'.$authorName.'</name>
<author id="'.$this->data["author"].'">
<name>'.$authorUser->get("name").'</name>
<uri>'.$authorUri.'</uri>
</author>
<content type="text/markdown">
......@@ -87,5 +133,46 @@ class Content{
//TO DO: switch(content type){render differently}
}
public static function list($scope="all",$contentType,$format="object"){ // TO DO : change $contentType with "Content/Article"
global $core;
/*
List all content
@param : $scope, $contentType, $format
*/
$objects=[];
$return=null;
if($scope=="all"){
switch(strtolower($contentType)){
case "article":
foreach (glob("../".Server::get("path/Content/Article/content","/")."*.txt") as $filename) {
$articleid=str_replace("../","",substr($filename,0,-4));
$article=new Content();
$article->init($articleid,$contentType);
array_push($objects,$article);
}
break;
}
}elseif($scope[0]=="@"){ // User
$user=new User($scope);
// TO DO
}
switch($format){
case "object":
$return=$objects;
break;
case "atom":
foreach ($objects as $object) {
$return.=$object->exportAtom();
}
break;
default:
foreach ($objects as $object) {
$return.=View::render($format,$object,"return");
}
break;
}
return($return);
}
}
?>
\ No newline at end of file
......@@ -11,6 +11,12 @@ class Core{
$query=explode("/",$query);
$qPos=0;
for($q=0; $q<sizeof($query); $q++){//deleting "" query (for // in url)
if($query[$q]==""){
unset($query[$q]);
}
}
if($query[0]==".well-known"){
if($query[1]=="webfinger"){
if(isset($_GET["resource"]) && $_GET["resource"]!=""){
......@@ -59,7 +65,7 @@ class Core{
$crfunction=strtolower(explode("(",$cr)[0]);
$crparam=str_replace(")","",explode("(",$cr)[1]);
if($crfunction=="server"){
if(Server::get("route/".$crparam)=="/"){//If the user defined "/" as route, means that he/she wants the root
if(Server::get("route/".$crparam)==""){//If the user defined "" as route, means that he/she wants the root
$foundRoute=true;
$route=$currentRoute->route;//Not this route but its child
}
......@@ -159,7 +165,7 @@ class Core{
$query=explode(":",$query);
if($query[0]=="acct"){
$account=explode("@",$query[1]);
$username=$account[0];
$username=strtolower($account[0]);
$domain=$account[1];
if(User::isInDatabase($username) && $domain==Server::get("domain")){
header("Content-type: application/json;charset=utf-8");
......@@ -167,13 +173,13 @@ class Core{
"subject" => join(":", $query),
"links" => array(array(
"rel" => "self",
"href" => "https://".Server::get("domain")."/account/".$username,
"href" => "https://".Server::get("domain")."/".Server::get("route/User","/").$username,
"type" => "application/activity+json",
)),
"aliases" => array("https://".Server::get("domain")."/account/".$username),
"aliases" => array("https://".Server::get("domain")."/".Server::get("route/User","/").$username),
)));
}else{
$this->error("404");
//$this->error("404");
}
}
}
......@@ -190,51 +196,17 @@ $feed='<?xml version="1.0" encoding="utf-8"?>
<subtitle>'.Server::get("subtitle").'</subtitle>
<author>
<name>'.Server::get("admin/name").'</name>
<uri>http://'.Server::get("domain")."/".Server::get("route/User")."/".Server::get("admin/id").'</uri>
<uri>http://'.Server::get("domain")."/".Server::get("route/User","/").Server::get("admin/id").'</uri>
</author>
<updated>2003-12-13T18:30:02Z</updated>
<rights>'.Server::get("licence").' '.Server::get("admin/name").' '.date("Y").'</rights>
<link rel="self" href="/feed" />
'.$this->listAll("Article","atom").'
'.$this->list("all","Article","atom").'
</feed>';
echo($feed);
}
public function listAll($contentType,$format="object"){
/*
List all content
@param : $contentType
*/
$objects=[];
$return=null;
switch($contentType){
case "Article":
foreach (glob("../*.txt") as $filename) {
$articleid=str_replace("../","",substr($filename,0,-4));
$article=new Content();
$article->init($articleid,$contentType);
array_push($objects,$article);
}
break;
}
switch($format){
case "object":
$return=$objects;
break;
case "atom":
foreach ($objects as $object) {
$return.=$object->exportAtom();
}
break;
default:
foreach ($objects as $object) {
$return.=View::render($format,$object,"return");
}
break;
}
return($return);
}
}
?>
\ No newline at end of file
<?php
class Server{
public static function get($var){
public static function get($var,$nextchar=""){
/*
Get server var
@params: $var
- route
- Content
- $contentType
- User
- path
- Content
- $contentType
- data
- content
- User
- domain or url or uri
*/
$serverConfig=simplexml_load_file("../fedidata/serverConfig.xml");
if(!$serverConfig){
......@@ -15,30 +26,34 @@ class Server{
case "admin":
$admin=new User();
$admin->init($serverConfig->admin);
return($admin->get(explode("/",$var)[1]));
$returnValue=$admin->get(explode("/",$var)[1]);
break;
case "route":
switch(explode("/",$var)[1]){
case "Content":
//$route = $serverConfig->xpath("customPath/object[id='content/".strtolower(explode("/",$var)[2])."']/route/text()");
$route = $serverConfig->xpath("customPath/object[id='Content/".explode("/",$var)[2]."']/route/text()");
break;
case "User":
//$route = $serverConfig->xpath("customPath/object[id='user']/route/text()"); //issue with maj : "User" do not work
$route = $serverConfig->xpath("customPath/object[id='User']/route/text()");
break;
default:
break;
}
return((string)$route[0]);
$returnValue=(string)$route[0];
break;
case "path":
switch(explode("/",$var)[1]){
case "Content":
//$route = $serverConfig->xpath("customPath/object[id='Content/".strtolower(explode("/",$var)[2])."']/path/".strtolower(explode("/",$var)[3])."/text()");
$route = $serverConfig->xpath("customPath/object[id='Content/".explode("/",$var)[2]."']/path/".explode("/",$var)[3]."/text()");
return((string)$route[0]);
$returnValue=(string)$route[0];
break;
case "User":
$route = $serverConfig->xpath("customPath/object[id='User']/path/".explode("/",$var)[2]."/text()");
return((string)$route[0]);
//$route = $serverConfig->xpath("customPath/object[id='user']/path/".strtolower(explode("/",$var)[2])."/text()");
$route = $serverConfig->xpath("customPath/object[id='User']/path/".strtolower(explode("/",$var)[2])."/text()");
$returnValue=(string)$route[0];
break;
default:
break;
......@@ -48,9 +63,9 @@ class Server{
$parent=explode("/",$var)[0];
$child=explode("/",$var)[1];
if(isset($serverConfig->$parent->$child)){
return($serverConfig->$parent->$child);
$returnValue=$serverConfig->$parent->$child;
}else{
return null;
$returnValue=null;
}
break;
}
......@@ -70,8 +85,21 @@ class Server{
break;
}
}
if($returnValue=="/"){
return("");
}else{
if($nextchar!=""){
return($returnValue.$nextchar);
}else{
return($returnValue);
}
}
}
}
public static function display($var,$nextchar=""){
echo(Secure::that(Server::get($var,$nextchar)));
}
}
?>
\ No newline at end of file
......@@ -4,7 +4,7 @@ class Test{
public function __construct($id){
$this->id=$id;
echo("Construct, id :".$this->id);
//echo("Construct, id :".$this->id);
}
public function view() {
......
......@@ -20,18 +20,23 @@ class User{
if($id[0]=="@"){
$id=substr($id,1);
}
$this->data=simplexml_load_file("../".Server::get("path/User/data")."/".$id.".xml");
if(!$this->data){
//$core->error("404");
if(User::isInDatabase($id)){
$this->data=simplexml_load_file("../".Server::get("path/User/data")."/".$id.".xml");
$this->id=$id;
$this->name="".$this->data->name;
$this->description="".$this->data->description;
}else{
$core->error("404", "User not found");
}
$this->id=$id;
$this->name=$this->data->name;
$this->description=$this->data->description;
}
}
public function get($var){
return($this->$var);
}
public function display($var){
echo(Secure::that($this->$var));
}
public function view($id=null) {
$this->init($id);
......@@ -84,15 +89,15 @@ class User{
"icon" => array(
"type" => "Image",
"mediaType" => "image/jpeg",
"url" => "https://".Server::get("domain")."/account/".$this->id."/avatar.jpg",
"url" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id."/avatar.jpg",
),
"image" => array(
"type" => "Image",
"mediaType" => "image/jpeg",
"url" => "https://".Server::get("domain")."/account/".$this->id."/header.jpg",
"url" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id."/header.jpg",
),
"endpoints" => array(
"sharedInbox" => "https://".Server::get("domain")."/account/".$this->id."/inbox",
"sharedInbox" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id."/inbox",
),
);
View::render("activityjson",$activityjson);
......@@ -109,7 +114,7 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".Server::get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
......@@ -127,7 +132,7 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".Server::get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
......@@ -145,10 +150,11 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".Server::get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
var_dump($core->list("@".$this->id,"Article"));
View::render("activityjson",$activityjson);
}
public function viewInbox($id=null){
......@@ -163,7 +169,7 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".Server::get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User","/").$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
......@@ -179,7 +185,7 @@ class User{
View::render("dump",$this);
}
public static function isInDatabase($id){
$data=simplexml_load_file("../data/user/".$id.".xml");
$data=simplexml_load_file("../fedidata/user/".strtolower($id).".xml");
if($data){
return true;
}else{
......
......@@ -50,8 +50,12 @@ echo('<?xml version="1.0" encoding="utf-8"?>
case "htmlSummary":
$render="<a href=\"/".Server::get("route/Content/".$object->get("type"))."/".$object->get("id")."\">".$object->get("id")."</a>";
break;
case "markdown":
$Parsedown = new Parsedown();
$render=$Parsedown->text($object->get("content"));
break;
default :
$render=file_get_contents("views/".$viewName.".xml");
/*$render=file_get_contents("views/".$viewName.".xml");
if($render==false){
$core->error("404","View named \"$viewName\" was not found");
}
......@@ -76,11 +80,7 @@ echo('<?xml version="1.0" encoding="utf-8"?>
if($vfunction=="list"){
$vparam=explode(",",$vparam);
if($vparam[0]=="all"){
$render=str_replace($matches[0][$i],$core->listAll($vparam[1],$vparam[2]),$render);
}elseif($vparam[0][1]=="@"){
//user
}
$render=str_replace($matches[0][$i],$core->list($vparam[0],$vparam[1],$vparam[2]),$render);
}
}
}
......@@ -90,6 +90,15 @@ echo('<?xml version="1.0" encoding="utf-8"?>
if($m[0]=="$"){
$render=str_replace($matches[0][$i],$object->get(substr($m,1)),$render);
}
}*/
$render=file_get_contents("views/".strtolower($viewName).".php");
if($render==false){
$core->error("404","View named \"$viewName\" was not found");
}else{
$fediview=$object;
ob_start();
include "views/".strtolower($viewName).".php";
$render=ob_get_clean();
}
break;
}
......@@ -100,5 +109,6 @@ echo('<?xml version="1.0" encoding="utf-8"?>
return(Secure::that($render));
}
}
}
?>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<routes>
<route>
<content>test</content>
<return method="get">
<function>Test->view</function>
</return>
<route>
<content>:param</content>
<return method="get">
<function>Test->view1param</function>
<params>:this</params>
</return>
</route>
</route>
<route>
<content>test3params</content>
<return method="get">
<function>Test->view3params</function>
<params>
<param1>yay</param1>
<param2>hey</param2>
<param3>ahoy</param3>
</params>
</return>
</route>
<route>
<content>atom|feed</content>
<return method="get">
......
<li><a href="/<?php Server::display("route/Content/Article","/"); $fediview->display("id"); ?>"><?php echo($fediview->get("data")["title"]); ?></a></li>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title><?php echo($fediview->get("data")["title"]); ?> - Article - <?php Server::display("url"); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#0070C0">
<link rel="stylesheet" href="https://dav.li/forkawesome/1.0.11/css/fork-awesome.min.css"/>
<link rel="stylesheet" href="/general.css" type="text/css"/>
<link rel="stylesheet" href="/article.css" type="text/css"/>
<link rel="stylesheet" href="/user-card.css" type="text/css"/>
</head>
<body>
<div class="content">
<header>
<div class="banner" style="background-image:url(<?php echo(Server::get("route/Content/Article")); ?>/<?php $fediview->display("id"); ?>/header.jpg)"></div>
<h1><?php echo($fediview->get("data")["title"]); ?></h1>
</header>
<main>
<?php View::render("markdown",$fediview); ?>
</main>
<footer>
<h2>The author :</h2>
<div id="author">
<?php View::render("user-card",new User($fediview->get("data")["author"])); ?>
</div>
</footer>
</div>
</body>
</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<html lang="{{$lang}}">
<head>
<meta charset="utf-8" />
<title>{{$title}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#0070C0">
</head>
<body>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<view>
<head>
<title>{{$type}} : {{$id}}</title>
</head>
<body>
<main class="content">
{{markdown($content)}}
</main>
</body>
</view>
\ No newline at end of file
<a href="/<?php echo(Server::get("route/User","/")); $fediview->display("id"); ?>" class="user-card">
<img class="avatar" src="/<?php echo(Server::get("route/User","/")); $fediview->display("id"); ?>/avatar.jpg"/>
<div>
<h4><?php $fediview->display("name"); ?></h4>
<h5>@<?php $fediview->display("id"); ?></h5>
<p><?php $fediview->display("description"); ?></p>
</div>
</a>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@<?php $fediview->display("id"); ?> - <?php Server::display("url"); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#0070C0">
<link rel="stylesheet" href="https://dav.li/forkawesome/1.0.11/css/fork-awesome.min.css"/>
<link rel="stylesheet" href="/general.css" type="text/css"/>
<link rel="stylesheet" href="/user.css" type="text/css"/>
</head>
<body>
<div class="content">
<header>
<div class="banner" style="background-image:url(/<?php echo(Server::get("route/User","/")); $fediview->display("id"); ?>/header.jpg)"></div>
<img class="avatar" src="/<?php echo(Server::get("route/User","/")); $fediview->display("id"); ?>/avatar.jpg"/>
<h1><?php $fediview->display("name"); ?></h1>
<h2>@<?php $fediview->display("id"); ?></h2>
<p><?php $fediview->display("description"); ?></p>
</header>
</div>
</body>
</html>
\ No newline at end of file
footer{
margin-top: 50px;
}
\ No newline at end of file
body{
margin: 0;
font-family: sans-serif;
background-color: #f0fbfd;
}
.content{
width: 80vw;
margin: auto;
}
img{
max-width: 100%;
}
/* header */
header{
margin-top: 7.5vh;
padding-bottom: 5vh;
border: 40px solid #171000;
border-bottom: 0;
text-align: center;
overflow: hidden;
}
.banner{
margin-bottom: 5vh;
height: 30vh;
background-color: white;
background-size: cover;
overflow: hidden;
}
header h1, header h2{
margin: 5px 0;
}
header h1{
font-size: 2.5em;
}
</