Commit c3fbf12d authored by David Libeau's avatar David Libeau

Change fediblog folders name, add serverConfig and a bunch of stuff

serverConfig is an xml file and a class who give the user personnal settings for his server.
parent 2daa558e
DirectoryIndex core/app.php?q=index
DirectoryIndex fedicore/app.php?q=index
RewriteEngine On
RewriteRule "^(.*)$" "core/app.php?q=$1" [L,QSA]
\ No newline at end of file
RewriteRule "^(.*)$" "fedicore/app.php?q=$1" [L,QSA]
\ No newline at end of file
<?php
class Content{
protected $initDone=false;
protected $id;
protected $data;
public function __construct(){
//return(1);
}
public function init($id){
global $core;
/*
Init
@params: $id
*/
if(!$this->initDone){
$this->data=file_get_contents("../".$id.".txt");
if(!$this->data){
$core->error("404");
}
$this->id=$id;
}
}
public function get($var){
return($this->$var);
}
public function view($id) {
$this->init($id);
View::render("content",$this);
}
public function dump($id) {
$this->init($id);
View::render("dump",$this);
}
}
?>
\ No newline at end of file
<?php
class View{
public static function render($viewName,$object) {
global $core;
if($viewName=="raw"){
echo($object);
}else if($viewName=="dump"){
var_dump($object);
}else if($viewName=="json"){
header('Content-type: application/json;charset=utf-8');
echo(json_encode($object));
}else if($viewName=="activityjson"){
header('Content-type: application/activity+json');
echo(json_encode($object));
}else if($viewName=="image"){
header('Content-type: image/jpeg;charset=utf-8');
echo($object);
}else{
$view=file_get_contents("views/".$viewName.".xml");
if($view==false){
$core->error("404","View named \"$viewName\" was not found");
}
//TO DO : Transform xml to html via the base template here
preg_match_all("/\{\{([^\}]*)\}\}/", $view, $matches); //isolate {{this thing}}
foreach($matches[1] as $i=>$m){
if (preg_match("/(.+\(.+\).*){1}/", $m)) {
$vfunction=strtolower(explode("(",$m)[0]);
$vparam=str_replace(")","",explode("(",$m)[1]);
if($vfunction=="markdown" || $vfunction=="parsedown"){
$Parsedown = new Parsedown();
if($vparam[0]=="$"){
$view=str_replace($matches[0][$i],$Parsedown->text($object->get(substr($vparam,1))),$view);
}
}
}
if($m[0]=="$"){
$view=str_replace($matches[0][$i],Secure::that($object->get(substr($m,1))),$view);
}
}
echo($view);
}
}
}
?>
\ No newline at end of file
......@@ -10,7 +10,7 @@ if(isset($_GET["q"]) && $_GET["q"]!=""){
foreach ($headers as $header => $value) {
$echolog.="$header: $value\n";
}
file_put_contents("../log/".date("d-m-Y").".txt",date("h:i:sa")." : ".$_SERVER["REQUEST_URI"]."\r".$echolog."\r",FILE_USE_INCLUDE_PATH | FILE_APPEND);
file_put_contents("../fedilog/".date("d-m-Y").".txt",date("h:i:sa")." : ".$_SERVER["REQUEST_URI"]."\r".$echolog."\r",FILE_USE_INCLUDE_PATH | FILE_APPEND);
}
$core=new Core();
$core->welcome($_GET["q"]);
......
......@@ -4,7 +4,7 @@ function autoloader($class) {
if(file_exists("controllers/".$class.".php")){
require_once("controllers/".$class.".php");
}
require_once("../lib/Parsedown.php");
require_once("../fedilib/Parsedown.php");
}
spl_autoload_register('autoloader');
?>
\ No newline at end of file
<?php
class Content{
protected $initDone=false;
protected $id;
protected $content;
protected $type;
public function __construct(){
//return(1);
}
public function init($id,$type="content"){
global $core;
/*
Init
@params: $id
*/
if(!$this->initDone && !is_null($id)){
$this->content=file_get_contents("../".$id.".txt");
if(!$this->content){
$core->error("404","../".$id.".txt was not found");
}
$this->id=$id;
$this->type=$type;
}
}
public function get($var){
return($this->$var);
}
public function view($id=null,$type="content") {
$this->init($id,$type);
View::render("content",$this);
}
public function dump($id=null) {
$this->init($id);
View::render("dump",$this);
}
public function exportAtom($id=null){
global $core;
$this->init($id);
/*
Export for Atom
@params: $id
*/
switch($this->type){
case "article":
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>');
break;
case "media":
break;
}
//TO DO: switch(content type){render differently}
}
}
?>
\ No newline at end of file
......@@ -4,6 +4,10 @@ class Core{
protected $domain="dev.fedi.blog";
public function welcome($query) {
/*
Routing
@params: $query
*/
$query=explode("/",$query);
$qPos=0;
......@@ -24,7 +28,19 @@ class Core{
$qPos++;
$foundRoute=false;
foreach($route->route as $currentRoute) {
if(preg_match("/^".$currentRoute->content."$/", $q)){
if(substr((string)$currentRoute->content,0,1)=="$"){//Route is user config
$cr=substr((string)$currentRoute->content,1);
if (preg_match("/(.+\(.+\).*){1}/", $cr)) {
$crfunction=strtolower(explode("(",$cr)[0]);
$crparam=str_replace(")","",explode("(",$cr)[1]);
if($crfunction=="server"){
if(Server::get("route/".$crparam)==$q){
$foundRoute=true;
$route=$currentRoute;
}
}
}
}elseif(preg_match("/^".$currentRoute->content."$/", $q)){
$foundRoute=true;
$route=$currentRoute;
}
......@@ -88,15 +104,19 @@ class Core{
call_user_func_array(array($class,$functionName),$params);
}else{
//var_dump($route->return);
call_user_func("".$route->return->function);
call_user_func(array($class,$functionName));
}
}else{
$this->error("404");
$this->error("404","Route not found");
}
}
}
public static function error($code=200,$message="") {
/*
Give error page
@params: $code, $message
*/
if($code!=200 && $message!=""){
http_response_code($code);
echo("Error ".$code.": ".$message);
......@@ -110,31 +130,93 @@ class Core{
}
public function get($var){
/*
Getter
@params: $var
*/
return($this->$var);
}
public function webfinger($query){
/*
Webfinger
@params: $query
*/
$query=explode(":",$query);
if($query[0]=="acct"){
$account=explode("@",$query[1]);
$username=$account[0];
$domain=$account[1];
if(User::isInDatabase($username) && $domain==$this->get("domain")){
header('Content-type: application/json;charset=utf-8');
if(User::isInDatabase($username) && $domain==Server::get("domain")){
header("Content-type: application/json;charset=utf-8");
echo(json_encode(array(
"subject" => join(":", $query),
"links" => array(array(
"rel" => "self",
"href" => "https://".$this->get("domain")."/account/".$username,
"href" => "https://".Server::get("domain")."/account/".$username,
"type" => "application/activity+json",
)),
"aliases" => array("https://".$this->get("domain")."/account/".$username),
"aliases" => array("https://".Server::get("domain")."/account/".$username),
)));
}else{
$this->error("404");
}
}
}
public function getAtomFeed(){
/*
Atom feed
*/
header("Content-type: text/xml;charset=utf-8");
$feed='<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>'.Server::get("domain").'</id>
<title>'.Server::get("title").'</title>
<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>
</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").'
</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;
}
return($return);
}
}
?>
\ No newline at end of file
<?php
class Secure{
public static function that($string) {
/*
Secure that
@params: $string
*/
return($string); //This is totally secure
}
}
?>
\ No newline at end of file
<?php
class Server{
public static function get($var){
/*
Get server var
@params: $var
*/
$serverConfig=simplexml_load_file("../fedidata/serverConfig.xml");
if(!$serverConfig){
//$core->error("404");
}else{
if(strpos($var, "/")!==false){
switch(explode("/",$var)[0]){
case "admin":
$admin=new User();
$admin->init($serverConfig->admin);
return($admin->get(explode("/",$var)[1]));
break;
case "route":
switch(explode("/",$var)[1]){
case "content":
$route = $serverConfig->xpath("customPath/object[id='content/".explode("/",$var)[2]."']/route/text()");
return((string)$route[0]);
break;
case "user":
$route = $serverConfig->xpath("customPath/object[id='user']/route/text()");
return((string)$route[0]);
break;
defautl:
break;
}
//return($serverConfig->customPath->$child);
break;
default:
$parent=explode("/",$var)[0];
$child=explode("/",$var)[1];
if(isset($serverConfig->$parent->$child)){
return($serverConfig->$parent->$child);
}else{
return null;
}
break;
}
}else{
switch($var){
case "domain":
case "url":
case "uri":
return($serverConfig->url);
break;
default:
if(isset($serverConfig->$var)){
return($serverConfig->$var);
}else{
return null;
}
break;
}
}
}
}
}
?>
\ No newline at end of file
......@@ -7,7 +7,6 @@ class User{
protected $description;
public function __construct(){
//return(1);
}
public function init($id){
global $core;
......@@ -15,8 +14,8 @@ class User{
Init
@params: $id
*/
if(!$this->initDone){
$this->data=simplexml_load_file("../data/user/".$id.".xml");
if(!$this->initDone && !is_null($id)){
$this->data=simplexml_load_file("../fedidata/user/".$id.".xml");
if(!$this->data){
//$core->error("404");
}
......@@ -29,11 +28,11 @@ class User{
return($this->$var);
}
public function view($id) {
public function view($id=null) {
$this->init($id);
View::render("user",$this);
}
public function image($id,$type) {
public function image($id=null,$type) {
global $core;
$this->init($id);
......@@ -43,12 +42,12 @@ class User{
}
View::render("image",$image);
}
public function viewJson($id) {
public function viewJson($id=null) {
$this->init($id);
View::render("json",$this->toJson());
}
public function viewActivityJson($id) {
public function viewActivityJson($id=null) {
global $core;
$this->init($id);
/*
......@@ -61,39 +60,39 @@ class User{
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
),
"id" => "https://".$core->get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id,
"type" => "Person",
"following" => "https://".$core->get("domain")."/account/".$this->id."/following",
"followers" => "https://".$core->get("domain")."/account/".$this->id."/followers",
"inbox" => "https://".$core->get("domain")."/account/".$this->id."/inbox",
"outbox" => "https://".$core->get("domain")."/account/".$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://".$core->get("domain")."/account/".$this->id,
"url" => "https://".Server::get("domain")."/".Server::get("route/user")."/".$this->id,
"publicKey" => array(
"id" => "https://".$core->get("domain")."/account/".$this->id."#main-key",
"owner" => "https://".$core->get("domain")."/account/".$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(
"type" => "Image",
"mediaType" => "image/jpeg",
"url" => "https://".$core->get("domain")."/account/".$this->id."/avatar.jpg",
"url" => "https://".Server::get("domain")."/account/".$this->id."/avatar.jpg",
),
"image" => array(
"type" => "Image",
"mediaType" => "image/jpeg",
"url" => "https://".$core->get("domain")."/account/".$this->id."/header.jpg",
"url" => "https://".Server::get("domain")."/account/".$this->id."/header.jpg",
),
"endpoints" => array(
"sharedInbox" => "https://".$core->get("domain")."/account/".$this->id."/inbox",
"sharedInbox" => "https://".Server::get("domain")."/account/".$this->id."/inbox",
),
);
View::render("activityjson",$activityjson);
}
public function viewFollowing($id){
public function viewFollowing($id=null){
global $core;
$this->init($id);
/*
......@@ -105,13 +104,13 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".$core->get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/account/".$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
View::render("activityjson",$activityjson);
}
public function viewFollowers($id){
public function viewFollowers($id=null){
global $core;
$this->init($id);
/*
......@@ -123,13 +122,13 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".$core->get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/account/".$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
View::render("activityjson",$activityjson);
}
public function viewOutbox($id){
public function viewOutbox($id=null){
global $core;
$this->init($id);
/*
......@@ -141,13 +140,13 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".$core->get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/account/".$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
View::render("activityjson",$activityjson);
}
public function viewInbox($id){
public function viewInbox($id=null){
global $core;
$this->init($id);
/*
......@@ -159,15 +158,19 @@ class User{
"@context" => array(
"https://www.w3.org/ns/activitystreams",
),
"id" => "https://".$core->get("domain")."/account/".$this->id,
"id" => "https://".Server::get("domain")."/account/".$this->id,
"type" => "OrderedCollection",
"totalItems" => 0,
);
View::render("activityjson",$activityjson);
}
public function dump($id) {
public function dump($id=null) {
$this->init($id);
/*
Dump user
@param: $id
*/
View::render("dump",$this);
}
public static function isInDatabase($id){
......
<?php
class View{
public static function render($viewName,$object) {
global $core;
switch($viewName){
case "raw":
echo($object);
break;
case "dump":
var_dump($object);
break;
case "json":
header('Content-type: application/json;charset=utf-8');
echo(json_encode($object));
break;
case "activityjson":
header('Content-type: application/activity+json');
echo(json_encode($object));
break;
case "image":
header('Content-type: image/jpeg;charset=utf-8');
echo($object);
break;
case "atom":
header("Content-type: text/xml;charset=utf-8");
/*if(is_array($object)){
foreach ($object as $key => $value) {
var_dump("{$key} => {$value} ");
}
}*/
echo('<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>'.Server::get("domain").'</id>
<title>'.Server::get("title").'</title>
<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>
</author>
<updated>2003-12-13T18:30:02Z</updated>
<rights>'.Server::get("licence").' '.Server::get("admin/name").' '.date("Y").'</rights>
<link rel="self" href="/feed" />
'.$object->exportAtom().'
</feed>');
break;
default :
$view=file_get_contents("views/".$viewName.".xml");
if($view==false){
$core->error("404","View named \"$viewName\" was not found");
}
//TO DO : Transform xml to html via the base template here
preg_match_all("/\{\{([^\}]*)\}\}/", $view, $matches); //isolate {{this thing}}
foreach($matches[1] as $i=>$m){
if (preg_match("/(.+\(.+\).*){1}/", $m)) {
$vfunction=strtolower(explode("(",$m)[0]);
$vparam=str_replace(")","",explode("(",$m)[1]);
if($vfunction=="markdown" || $vfunction=="parsedown"){
$Parsedown = new Parsedown();
if($vparam[0]=="$"){
$view=str_replace($matches[0][$i],$Parsedown->text($object->get(substr($vparam,1))),$view);
}
}
}
if($m[0]=="$"){
$view=str_replace($matches[0][$i],Secure::that($object->get(substr($m,1))),$view);
}
}
echo($view);
break;
}
}
}
?>
\ No newline at end of file
......@@ -25,25 +25,37 @@
</return>
</route>
<route>
<content>:default</content>
<content>atom|feed</content>
<return method="get">
<function>Content->view</function>
<params>:this</params>
<function>Core->getAtomFeed</function>
</return>
</route>
<route>
<content>$server(content/article)</content>
<route>
<content>comments</content>
<content>:id</content>
<return method="get">
<function>Content->viewComments</function>
<params>:this</params>
</return>
<return method="get" application="activity+json">
<function>Content->viewCommentsActivityJson</function>
<params>:this</params>
<function>Content->view</function><!--TO DO (?): Content(:this,"article")->view()-->
<params>
<param1>:this</param1>
<param2>article</param2>
</params>
</return>
<route>
<content>comments</content>
<return method="get">
<function>Content->viewComments</function>
<params>:this</params>
</return>
<return method="get" application="activity+json">
<function>Content->viewCommentsActivityJson</function>
<params>:this</params>
</return>
</route>
</route>
</route>
<route>
<content>media</content>
<content>$server(content/media)</content><!--TO DO: make content/* for all content type-->