<?php
namespace Taeluf\Docu;
class oldViewer {
public $projects = [];
public $safeDir;
public $baseUrl;
protected $requestUrl;
public function __construct($package){
parent::__construct($package);
$options = (object)$package->setup['options'];
$this->safeDir = realpath($options->safeDir);
$this->baseUrl = $options->baseUrl;
if (!is_dir($this->safeDir)){
throw new \Exception("You must pass a valid directory path as 'safeDir' to the wiki package setup. Received '{$this->safeDir}'.");
}
}
public function addProject($project){
$this->projects[] = $project;
}
//need to make the base url function with the static files & project files
public function onRequest_Setup($event, $url){
// echo 'oh good';exit;
//add each project's directory to the router
//probably do it as a callable, since will not use the regular file-routing mechanisms
//or add a new feature to liaison to fine-tune route handling
//or implement an existing event that lets me filter content handlers
}
public function onEvents_Sort($event, $name, $args, $events){
if ($name!='Request_Deliver')return $events;
$others = [];
foreach ($events as $callable){
if (!is_array($callable))continue;
if ($callable[0]!==$this)$others[] = $callable;
}
$baseUrl = $this->baseUrl;
$url = $args[0];
if (substr($url,0,strlen($baseUrl))!=$baseUrl)return $others;
// $staticUrl = $baseUrl.'static/';
// if (substr($url,0,strlen($staticUrl))==$staticUrl)return $others;
foreach ($events as $callable){
if (!is_array($callable))continue;
if ($callable[0]===$this)return [$callable];
}
}
public function urlFor($project, $relUrl){
$url = $this->baseUrl.'/'.$project->name.'/'.$relUrl;
$url = str_replace(['///','//'],'/',$url);
return $url;
}
public function urlForFilePath($project, $filePath){
$filePath = substr($filePath,strlen($this->safeDir));
$parts = explode('/',$filePath);
$relUrl = '/'.implode('/',array_slice($parts,4));
$url = $this->baseUrl.'/'.$project->name.'/'.$relUrl;
$url = str_replace(['///','//'],'/',$url);
$url = str_replace(' ', '%20', $url);
return $url;
}
public function isActive($relUrl){
$url = str_replace(['///','//'],'/',$relUrl);
$requested = $this->getFileUrl($this->requestUrl);
if (substr($requested,0,strlen($url))==$url){
return true;
}
return false;
}
public function getFileUrl($url){
$relPath = substr($url,strlen($this->baseUrl));
if (strlen($relPath)==0){
return null;
}
$parts = explode('/',$relPath);
$projectName = array_shift($parts);
return '/'.implode('/',$parts);
}
public function getProject($url){
$relPath = substr($url,strlen($this->baseUrl));
if (strlen($relPath)==0){
return null;
}
$parts = explode('/',$relPath);
$projectName = array_shift($parts);
foreach ($this->projects as $project){
if (strtolower($projectName)==strtolower($project->name)){
return $project;
}
}
return null;
}
public function onRequest_Deliver($event,$requestUrl){
$requestUrl = str_replace(['%20'],' ', $requestUrl);
$this->requestUrl = $requestUrl;
$url = $this->getFileUrl($requestUrl);
$project = $this->getProject($requestUrl);
if (($_GET['view_source']??false)=='true'){
$source = $project->getSource($url);
if (($_GET['raw']??false)=='true'){
header(('Content-type: text/plain'));
echo $source;
exit;
}
header(('Content-type: text/html'));
$extension = pathinfo($url,PATHINFO_EXTENSION);
$source = htmlentities($source);
$this->view('Docu/SourceCode',['source'=>$source,'language'=>$extension]);
exit;
}
ob_start();
$lia = $this->lia;
$lia->view('Docu/Resources');
if ($project==null){
echo $lia->view('Docu/ProjectSelector', ['viewer'=>$this]);
} else {
echo $lia->view('Docu/ProjectLayout', ['project'=>$project,'viewer'=>$this, 'url'=>$url]);
}
$pageOutput = ob_get_clean();
$themeView = $this->lia->view('Site/Theme',['content'=>$pageOutput,'without_content_area'=>true]);
if ($themeView==null)echo $pageOutput;
else echo $themeView;
}
public function projectUrl($project){
return str_replace('//','/',$this->baseUrl.'/'.$project->name);
}
static public function packageDir(){
return dirname(__DIR__);
}
}