Commit 8c6b7df3 authored by David Libeau's avatar David Libeau

Handling database for Article and minor changes

parent 3c11b9af
......@@ -3,24 +3,41 @@ class Content{
protected $initDone=false;
protected $id;
protected $content;
protected $data=[];
protected $type;
public function __construct(){
//return(1);
}
public function init($id,$type="content"){
public function init($id,$type="Content"){
global $core;
/*
Init
@params: $id
*/
$this->type=$type;
if(!$this->initDone && !is_null($id)){
$this->content=file_get_contents("../".Server::get("path/content/".$this->type."/content")."/".$id.".txt");
if(!$this->content){
$core->error("404","../".$id.".txt was not found");
}
$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;
}
}
}
}
public function get($var){
......@@ -45,24 +62,26 @@ class Content{
@params: $id
*/
switch($this->type){
case "article":
case "Article":
$authorUser=new User($this->data["author"]);
$authorName=$authorUser->get("name");
$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>2003-12-13T09:17:51-08:00</published>
<updated>2003-12-13T18:30:02-05:00</updated>
<author>
<name>David Libeau</name>
<uri>http://dev.fedi.blog/account/david</uri>
</author>
<content type="text/markdown">
'. $this->content.'
</content>
<link rel="alternante" href="http://dev.fedi.blog/'.$this->id.'.txt"/>
</entry>');
'<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>
<uri>'.$authorUri.'</uri>
</author>
<content type="text/markdown">
'.$this->content.'
</content>
</entry>');
break;
case "media":
case "Media":
break;
}
......
......@@ -99,26 +99,24 @@ class Core{
if(count($route->return->params->children())!=0){
foreach ($route->return->params->children() as $param) {
if($param==":this"){
$params[]=$query[$qPos-1];
$params[]=(string)$query[$qPos-1];
}else if($param==":parent"){
$params[]=$query[$qPos-2];
$params[]=(string)$query[$qPos-2];
}else{
$params[]=$param;
$params[]=(string)$param;
}
}
}else{
if($route->return->params==":this"){
$params[]=strtolower($query[$qPos-1]);
$params[]=(string)$query[$qPos-1];
}else if($route->return->params==":parent"){
$params[]=strtolower($query[$qPos-2]);
$params[]=(string)$query[$qPos-2];
}else{
$params[]=strtolower($route->return->params);
$params[]=(string)$route->return->params;
}
}
//var_dump($class);
call_user_func_array(array($class,$functionName),$params);
}else{
//var_dump($route->return);
call_user_func(array($class,$functionName));
}
}else{
......@@ -192,13 +190,13 @@ $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->listAll("Article","atom").'
</feed>';
echo($feed);
}
......@@ -211,7 +209,7 @@ $feed='<?xml version="1.0" encoding="utf-8"?>
$objects=[];
$return=null;
switch($contentType){
case "article":
case "Article":
foreach (glob("../*.txt") as $filename) {
$articleid=str_replace("../","",substr($filename,0,-4));
$article=new Content();
......
......@@ -19,11 +19,11 @@ class Server{
break;
case "route":
switch(explode("/",$var)[1]){
case "content":
$route = $serverConfig->xpath("customPath/object[id='content/".explode("/",$var)[2]."']/route/text()");
case "Content":
$route = $serverConfig->xpath("customPath/object[id='Content/".explode("/",$var)[2]."']/route/text()");
break;
case "user":
$route = $serverConfig->xpath("customPath/object[id='user']/route/text()");
case "User":
$route = $serverConfig->xpath("customPath/object[id='User']/route/text()");
break;
default:
break;
......@@ -32,12 +32,12 @@ class Server{
break;
case "path":
switch(explode("/",$var)[1]){
case "content":
$route = $serverConfig->xpath("customPath/object[id='content/".explode("/",$var)[2]."']/path/".explode("/",$var)[3]."/text()");
case "Content":
$route = $serverConfig->xpath("customPath/object[id='Content/".explode("/",$var)[2]."']/path/".explode("/",$var)[3]."/text()");
return((string)$route[0]);
break;
case "user":
$route = $serverConfig->xpath("customPath/object[id='user']/path/".explode("/",$var)[2]."/text()");
case "User":
$route = $serverConfig->xpath("customPath/object[id='User']/path/".explode("/",$var)[2]."/text()");
return((string)$route[0]);
break;
default:
......
......@@ -6,7 +6,8 @@ class User{
protected $name;
protected $description;
public function __construct(){
public function __construct($id=null){
$this->init($id);
}
public function init($id){
global $core;
......@@ -15,10 +16,11 @@ class User{
@params: $id
*/
if(!$this->initDone && !is_null($id)){
$this->initDone=true;
if($id[0]=="@"){
$id=substr($id,1);
}
$this->data=simplexml_load_file("../".Server::get("path/user/data")."/".$id.".xml");
$this->data=simplexml_load_file("../".Server::get("path/User/data")."/".$id.".xml");
if(!$this->data){
//$core->error("404");
}
......@@ -39,7 +41,7 @@ class User{
global $core;
$this->init($id);
$image=file_get_contents("../".Server::get("path/user/data")."/".$this->id."-".$type);
$image=file_get_contents("../".Server::get("path/User/data")."/".$this->id."-".$type);
if(!$image){
$core->error("404");
}
......@@ -63,20 +65,20 @@ class User{
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
),
"id" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id,
"type" => "Person",
"following" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id."/following",
"followers" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id."/followers",
"inbox" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id."/inbox",
"outbox" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id."/outbox",
"following" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id."/following",
"followers" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id."/followers",
"inbox" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id."/inbox",
"outbox" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id."/outbox",
"preferredUsername" => "David",
"name" => "".$this->name,
"summary" => "<p>".$this->description."</p>",
"manuallyApprovesFollowers" => false,
"url" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id,
"url" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id,
"publicKey" => array(
"id" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id."#main-key",
"owner" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id."#main-key",
"owner" => "https://".Server::get("domain")."/".Server::get("route/User")."/".$this->id,
"publicKeyPem" => "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyJAjDVrhKSGRQaG8Z59E\nHB7Q7c58pYOGmEpzV2Vrboi9A0EOOH7qrmVsnuncPOoxd31z3cwAQyCz+WkXVsG5\npUbvG3XjzUQKXSwOUemg8jCJ7/JHrqCpaSX5f4i028F+eNX8yjuYlj208COk37qz\nR96p2Nvzm+3RSodcYIf0qEi2d0x+cmoDSMSf3K/AdkgKIi5IA02kStOAt1bXnpeA\nhw0bdMGjq+z6B083zfZKi4Ya6s51fh/kV/dB/K4VxNsKaMXUhwk/558x5v43OiuO\ntBP4bbJwJm8txCt2eG3WxoDxbZvRbenp4DK4P6F0JLi42oVRWnGTcTKzt0F3KK4d\njQIDAQAB\n-----END PUBLIC KEY-----\n",
),
"icon" => array(
......
......@@ -39,7 +39,7 @@ echo('<?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>
......@@ -48,7 +48,7 @@ echo('<?xml version="1.0" encoding="utf-8"?>
</feed>');
break;
case "htmlSummary":
$render="<a href=\"/".Server::get("route/content/".$object->get("type"))."/".$object->get("id")."\">".$object->get("id")."</a>";
$render="<a href=\"/".Server::get("route/Content/".$object->get("type"))."/".$object->get("id")."\">".$object->get("id")."</a>";
break;
default :
$render=file_get_contents("views/".$viewName.".xml");
......
......@@ -31,14 +31,14 @@
</return>
</route>
<route>
<content>$server(content/article)</content>
<content>$server(Content/Article)</content>
<route>
<content>:id</content>
<return method="get">
<function>Content->view</function><!--TO DO (?): Content(:this,"article")->view()-->
<function>Content->view</function><!--TO DO (?): Content(:this,"Article")->view()-->
<params>
<param1>:this</param1>
<param2>article</param2>
<param2>Article</param2>
</params>
</return>
<route>
......@@ -55,33 +55,33 @@
</route>
</route>
<route>
<content>$server(content/static)</content><!--TO DO: make content/* for all content type (Maybe not ?)-->
<content>$server(Content/Static)</content><!--TO DO: make content/* for all content type (Maybe not ?)-->
<route>
<content>:id</content>
<return method="get">
<function>Content->view</function>
<params>
<param1>:this</param1>
<param2>static</param2>
<param2>Static</param2>
</params>
</return>
</route>
</route>
<route>
<content>$server(content/media)</content>
<content>$server(Content/Media)</content>
<route>
<content>:media</content>
<return method="get">
<function>Content->viewMedia</function>
<params>
<param1>:this</param1>
<param2>media</param2>
<param2>Media</param2>
</params>
</return>
</route>
</route>
<route>
<content>$server(user)</content>
<content>$server(User)</content>
<return method="get">
<function>User->viewAll</function>
</return>
......
<?xml version="1.0" encoding="UTF-8"?>
<customPath>
<object>
<id>user</id>
<id>User</id>
<route>user</route>
<path>
<data>fedidata/user</data>
</path>
</object>
<object>
<id>content/article</id>
<id>Content/Article</id>
<route>article</route>
<path>
<data>fedidata/content/article</data>
......@@ -16,7 +16,7 @@
</path>
</object>
<object>
<id>content/static</id>
<id>Content/Static</id>
<route>/</route>
<path>
<data>fedidata/content/static</data>
......@@ -24,7 +24,7 @@
</path>
</object>
<object>
<id>content/media</id>
<id>Content/Media</id>
<route>media</route>
<path>
<data>fedidata/content/media</data>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment