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

Basic route manager, basic User controller, basic Views and others

parent 8dc2f973
DirectoryIndex core/app.php?q=index
RewriteEngine On
RewriteRule ^(.*)$ core/app.php?q=$1 [L,QSA]
\ No newline at end of file
<?php
require("autoload.php");
if(isset($_GET["q"]) && $_GET["q"]!=""){
$query=explode("/",$_GET["q"]);
$qPos=0;
$xmlroutes=simplexml_load_file("routes/core.xml");
$route=$xmlroutes;
foreach($query as $q){
$qPos++;
$foundRoute=false;
foreach($route->route as $currentRoute) {
if(preg_match("/^".$currentRoute->content."$/", $q)){
$foundRoute=true;
$route=$currentRoute;
}
}
if(!$foundRoute){
foreach($route->route as $currentRoute) {
if(substr($currentRoute->content,0,1)==":"){
$foundRoute=true;
$route=$currentRoute;
}
}
}
}
$className=explode("->",$route->return->function)[0];
//var_dump(explode("->",$route->return->function));
$functionName=explode("->",$route->return->function)[1];
$class=new $className();
if(isset($route->return->params)){
$params=array();
if(count($route->return->params->children())!=0){
foreach ($route->return->params->children() as $param) {
if($route->return->params==":this"){
$params[]=$query[$qPos-1];
}else{
$params[]=$param;
}
}
}else{
if($route->return->params==":this"){
$params[]=$query[$qPos-1];
}else{
$params[]=$route->return->params;
}
}
//var_dump($class);
call_user_func_array(array($class,$functionName),$params);
}else{
//var_dump($route->return);
call_user_func("".$route->return->function);
}
}
?>
\ No newline at end of file
<?php
function autoloader($class) {
//echo("<p>[Autoload] :".$class."</p>");
if(file_exists("controllers/".$class.".php")){
require_once("controllers/".$class.".php");
}
}
spl_autoload_register('autoloader');
?>
\ No newline at end of file
<?php
class Secure{
public static function that($string) {
return($string); //This is totally secure
}
}
?>
\ No newline at end of file
<?php
class Test{
protected $id;
public function __construct($id){
$this->id=$id;
echo("Construct, id :".$this->id);
}
public function view() {
echo("This is a test");
}
public function view1param($param) {
echo("This is a test with 1 param : ".$param." & id :".$this->id);
}
public function view2params($param1,$param2) {
echo("This is a test with 2 params : ".$param1." ".$param2);
}
public function view3params($param1,$param2,$param3) {
echo("This is a test with 3 params : ".$param1." ".$param2." ".$param3);
}
}
?>
\ No newline at end of file
<?php
class User{
protected $initDone=false;
protected $data;
protected $id;
protected $name;
protected $description;
public function __construct(){
//return(1);
}
public function init($id){
if(!$this->initDone){
$this->data=simplexml_load_file("../data/user/".$id.".xml");
$this->id=$id;
$this->name=$this->data->name;
$this->description=$this->data->description;
}
}
public function get($var){
return($this->$var);
}
public function view($id) {
$this->init($id);
View::render("user",$this);
}
}
?>
\ No newline at end of file
<?php
class View{
public static function render($viewName,$object) {
$view=file_get_contents("views/".$viewName.".xml");
//Transform xml to html via the base template here
preg_match_all("/\{\{([^\}]*)\}\}/", $view, $matches);
foreach($matches[1] as $i=>$m){
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
<?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>:default</content>
<return method="get">
<function>Content->view</function>
<params>:this</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>
<content>media</content>
<route>
<content>:media</content>
<return method="get">
<function>Content->viewMedia</function>
<params>:this</params>
</return>
</route>
</route>
<route>
<content>account(s)?</content>
<return method="get">
<function>User->viewAll</function>
</return>
<route>
<content>:username</content>
<return method="get">
<function>User->view</function>
<params>:this</params>
</return>
<return method="get" application="json">
<function>User->viewJson</function>
<params>:this</params>
</return>
<return method="get" application="activity+json">
<function>User->viewActivityJson</function>
<params>:this</params>
</return>
<route>
<content>json</content>
<return method="get">
<function>User->viewJson</function>
<params>:parent</params>
</return>
</route>
<route>
<content>inbox</content>
<return method="get">
<function>User->viewInbox</function>
<params>:parent</params>
</return>
</route>
<route>
<content>outbox</content>
<return method="get">
<function>User->viewOutbox</function>
<params>:parent</params>
</return>
</route>
<route>
<content>follows</content>
<return method="get">
<function>User->viewFollows</function>
<params>:parent</params>
</return>
</route>
<route>
<content>followers</content>
<return method="get">
<function>User->viewFollowers</function>
<params>:parent</params>
</return>
</route>
</route>
</route>
</routes>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<view>
<head>
<title>User : @{{$id}}</title>
</head>
<body>
<h1>{{$name}}</h1>
<h2>@{{$id}}</h2>
<p>{{$description}}</p>
</body>
</view>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>david</id>
<name>David Libeau</name>
<description>Hi there!</description>
</user>
\ No newline at end of file
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