<?php
function __autoload($Class) {
if (is_dir($dir=dirname(dirname(__FILE__))."/modules/$Class/")) return require_once $dir.$Class . '.php';
else require_once dirname(__FILE__)."/".$Class . '.php';
}
class Diple {
const FORCE=true;
const MULTIBOOK=true;
static $request;
static $dipleDir;
static $dipleHref;
static $corpusId;
static $corpusDir;
static $corpusHref;
static $bookId;
static $htmlDir;
static $docDir;
static $xmlDir;
static $xmlFile;
static $xslHtml;
static $xslSplit;
static function theme($Class=null, $href=null) {
if (!$Class) $Class='neutre';
$dir=mb_strtolower($Class, "utf-8");
$Class=mb_strtoupper(mb_substr($dir, 0, 1, "UTF-8"), "UTF-8").mb_substr($dir, 1, 10000, "UTF-8");
require_once(self::$dipleDir."theme/".$dir.'/'.$Class.'.php');
if(!$href) $href=Diple::$dipleHref."theme/";
return new $Class($href);
}
static function doc($path=null, $nosplit=false) {
$docs=self::docs($path, $nosplit);
return $docs[0];
}
static function docs($path=null, $nosplit=false) {
if ($path === null) $path=self::$request;
$docs=array();
@list($book, $item)=explode("/", $path);
if ($book==="") {
$doc=new HtmlInc(self::$htmlDir."index.html");
if ($doc->error) $doc=new HtmlInc(self::$htmlDir."welcome.html");
if ($doc->error) $doc=new HtmlInc(self::$docDir."index.html");
if (!$doc->error) return array($doc);
} else {
if (file_exists(self::$xmlDir.$book.'.xml')) self::$bookId=$book;
}
if (($book=="src" || $book=="doc") &&$html=self::htmlFile($path)) return array (new HtmlInc($html) );
if ($nosplit);
else {
self::split(self::$xmlDir."$book.xml", true);
self::split(self::$xmlDir.self::$corpusId.".xml");
}
if (is_dir($dir=self::$htmlDir.$book."/")) {
if ($item==="") {
$docs[]=new HtmlInc($dir."index.html");
return $docs;
}
$idList=explode(",",$item); foreach ($idList as $id) {
$id=trim($id);
if ($id === "") continue; $doc=new HtmlInc($dir.$book."_".$id.".html");
if ($doc->error) $doc=new HtmlInc($dir.$id.".html");
if (!$doc->error) $docs[]=$doc;
}
if (!count($docs)) {
$docs[0]=new HtmlInc(null);
$docs[0]->error="L'item <b>$item</b> semble introuvable dans : <i>$book</i>.";
}
return $docs;
}
$idList=explode(",",$book); foreach ($idList as $id) {
$id=trim($id);
if ($id === "") continue; $doc=new HtmlInc(self::$htmlDir.self::$corpusId."_$id.html");
if ($doc->error) $doc=new HtmlInc(self::$htmlDir."$id.html");
if (!$doc->error) $docs[]=$doc;
}
if (count($docs)) return $docs;
$doc=new HtmlInc(self::$docDir."$book.html");
if (!$doc->error) {
$docs[]=$doc;
return $docs;
}
if ($book=="schema") $doc=new HtmlInc(self::schema());
$htmlFile=self::htmlFile(null, null, self::$corpusDir);
if ($htmlFile) $doc=new HtmlInc($htmlFile);
if ($doc->error) $doc->error="Le document <b>$path</b> semble introuvable.";
$docs[]=$doc;
return $docs;
}
static function split($xmlFile, $multibook=null, $force=null) {
if (!file_exists($xmlFile)) return false;
self::$xmlFile=$xmlFile;
$xmlBasename=basename($xmlFile, ".xml");
$destFile=self::$htmlDir.$xmlBasename.".html";
if ($multibook===false) $destDir=self::$htmlDir;
else if(self::$corpusId == $xmlBasename) $destDir=self::$htmlDir;
else {
$destDir=self::$htmlDir.$xmlBasename."/";
$multibook=true;
}
if (!$force && is_file($destFile) && filemtime($destFile) > filemtime($xmlFile)) return $destDir;
self::$xmlFile=$xmlFile;
if (file_exists($xsl=self::$corpusDir."transform/".self::$corpusId."_split.xsl"));
else if (file_exists($f=self::$corpusDir."transform/".self::$corpusId."_html.xsl")) {
$xsl='<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="'.strtr($f, array('\\'=>"/")).'"/>
<xsl:import href="'.strtr(self::$dipleDir, array('\\'=>"/")).'transform/tei_split.xsl"/>
</xsl:transform>';
}
else {
$xsl='<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="'.strtr(self::$dipleDir, array('\\'=>"/")).'transform/tei_html.xsl"/>
<xsl:import href="'.strtr(self::$dipleDir, array('\\'=>"/")).'transform/tei_split.xsl"/>
</xsl:transform>'; }
if (!is_writable($destDir)) {
mkdir($destDir, 0775, true);
@chmod($destDir, 0775); }
if ($multibook) $theme="../theme/";
else $theme="theme/";
Transform::xsl (
$xmlFile, $xsl,
$destFile, array('dir'=> $destDir, 'corpusId'=>self::$corpusId, 'bookId'=>self::$bookId, 'theme'=>$theme),
null,
$force
);
if (Transform::$done) {
if (strpos($destDir, sys_get_temp_dir()) === 0) echo '<p class="error">Régénération, impossible d’écrire dans <tt>html</tt> ou <tt>var</tt>, pages écrites dans le dossier temporaire : ',$destDir,'</p>';
File::scan($destDir, null, null, "chmod", null, array(0664));
$xsl=self::$corpusDir."transform/".self::$corpusId."_html.xsl";
if (!file_exists($xsl)) $xsl=self::$dipleDir."transform/tei_html.xsl";
Transform::xsl (
$xmlFile, $xsl,
$destFile, array('dir'=> $destDir, 'bookId'=>$xmlBasename, 'corpusId'=>self::$corpusId, 'theme'=>'theme/'),
null,
true
);
File::cp(self::$dipleDir."theme/neutre", self::$htmlDir."theme");
File::cp(self::$dipleDir."js/Tree.js", self::$htmlDir."theme/");
File::cp(self::$corpusDir."theme", self::$htmlDir."theme");
}
return $destDir;
}
static function rewrite($html, $base="") {
$reLinks=array(
'@href="./@' => 'href="'.self::$corpusHref.$base, '/href="('.self::$bookId.'|'.self::$corpusId.')_([^"?#]+)\.html/' => 'href="$2',
'/href="([A-Za-z0-9\-_]+)\.html/' => 'href="$1',
'/ id="(nav|article)"/'=>' id="${1}2"', '@src="../@' => 'src="',
);
return preg_replace( array_keys($reLinks), array_values($reLinks), $html );
}
static function schema($srcDir=false) {
if (!$srcDir) $srcDir=self::$xmlDir;
$destDir=$srcDir;
if (!is_writable($destDir)) $destDir=self::$htmlDir;
$schemaRng= $destDir."schema.rng";
$schemaHtml=$destDir."schema.html";
foreach(glob($srcDir."*.rng") as $xml) {
if ($xml==$schemaRng) continue;
Transform::xsl(
$xml,
self::$dipleDir.'modules/xrem/rng_inc.xsl',
$schemaRng,
array("inclien"=>"1")
);
if (!Transform::$done) return $schemaHtml;
if (!is_writable(self::$xmlDir)) {
echo '<p class="error">Schéma, les droits ne permettent pas de générer la documentation dans le dossier des sources.</p>';
}
Transform::xsl(
$schemaRng,
self::$dipleDir.'modules/xrem/rng_html.xsl',
$schemaHtml,
array("css"=>"../../diple/modules/xrem/xrem.css"),
null,
true
);
Transform::xsl(
$xml,
self::$dipleDir.'modules/xrem/rng_inc.xsl',
$schemaRng,
null,
null,
true
);
break;
}
return $schemaHtml;
}
static function ariane($doc, $welcomeFile="index.html,index_fr.html,welcome.html,couverture.html") {
$html="";
$count=0;
if (!is_array($doc));
else if($count=count($doc) < 1) return $html; else $doc=$doc[0];
$props=$doc->props();
if (isset($props['isPartOf'])) {
$meta=array_shift($props['isPartOf']);
if (isset($meta['string'])) {
$html = ' <a href="'. $meta['uri']. '">'. $meta['string'] .'</a> »';
}
}
if (isset($_REQUEST['q'])) return $html." » Recherche"; if (strpos( ",".$welcomeFile.",", ",".basename($doc->uri).",") !== false ) return $html; if ($count > 1) return $html." » Accueil"; if (isset($props['isPartOf'])) {
foreach ($props['isPartOf'] as $meta) {
$html .= ' <a href="'. $meta['uri']. '">'. $meta['string'] .'</a> » ';
}
}
$html .= ' <a href="">'.$doc->label() . '</a>';
return $html;
}
static function redirect() {
$basepath=dirname(realpath($_SERVER['SCRIPT_FILENAME'])).'/'.Diple::$request; $index_php=basename($_SERVER['SCRIPT_FILENAME']);
$request_uri=$_SERVER['REQUEST_URI'];
if (is_file($basepath)) {
if ( ($pos=strpos($request_uri, $index_php.'/')) !== false) $location=substr($request_uri, 0, $pos);
else $location=$request_uri;
header( "Location: $location" ) ;
echo '<a href="'.$location.'">Redirection vers '. $location .'</a>';
exit;
}
if (Diple::$request=="" && strpos($request_uri, $index_php.'/') === false) {
$location=dirname($_SERVER['REQUEST_URI']."o" )."/".basename($_SERVER['SCRIPT_FILENAME'])."/";
header( "Location: $location" ) ;
echo '<a href="'.$location.'">redirection vers '. $location .'</a>';
exit;
}
Diple::$dipleHref="../../diple/";
}
protected static $ob_file;
static function ob_file_callback($buffer) {
fwrite(Diple::$ob_file,$buffer);
}
static function phpHtml($pathinfo=null) {
if ($pathinfo === null) $pathinfo=self::$request;
$srcPath=self::$xmlDir.$pathinfo;
if (realpath($file=$srcPath.".php"));
else if (realpath($file=$srcPath."/index.php"));
else {
echo '<!-- Diple::phpHtml not found for :',$srcPath," -->\n";
return false;
}
if ($file ==__FILE__) return null;
else if ($file == realpath($_SERVER['SCRIPT_FILENAME']) ) return null;
$class=basename($file, ".php");
if ($class == "index") $class=basename(dirname($file));
define('CLASS_TMP',$class);
ob_start();
require_once $file;
$html= ob_get_contents();
ob_end_clean();
$class=CLASS_TMP;
if (class_exists($class, false)) define('CLASS_INC',$class);
return $html;
}
static function htmlFile($pathinfo=null, $force=null, $srcDir=null, $destDir=null) {
if ($pathinfo === null) $pathinfo=self::$request;
if ($srcDir === null) $srcDir=self::$xmlDir;
if ($destDir === null) $destDir=self::$htmlDir;
$srcPath=rtrim($srcDir.$pathinfo, '/');
$ext=explode(".", $srcPath);
$ext=end($ext);
if (is_file($srcPath)) return $srcPath;
$destFile=$destDir.$pathinfo.".html";
if (file_exists($file=$srcPath.".rng")) {
return Transform::xsl(
$file, dirname(dirname(__FILE__)).'/modules/xrem/rng_html.xsl', $destFile, null, null, $force );
}
if ( ($ext == "phps") && file_exists($file=substr($srcPath, 0, -1))) $phpdoc="php";
else if (file_exists($file=$srcPath.".css")) $phpdoc="css";
else if (file_exists($file=$srcPath.".js")) $phpdoc="js";
else $phpdoc=null;
if ($phpdoc){
if (!file_exists($destFile) || filemtime($destFile) < filemtime($file) || $force) PhpDoc::html($file, $destFile, $phpdoc);
return $destFile;
}
if (file_exists($file=$srcPath.".xsl")) {
return Transform::xsl(
$file, dirname(dirname(__FILE__)).'/modules/xrem/xsl_html.xsl', $destFile, null, null, $force );
}
if (file_exists($srcPath.".odt")) {
if (file_exists($destFile) && filemtime($destFile) > filemtime($srcPath.".odt")) return $destFile;
$odt_tei=dirname(dirname(__FILE__)).'/modules/odt_tei/';
include_once $odt_tei.'Odt.php';
$tei=Odt::tei($srcPath.".odt", $destDir.$pathinfo.".xml");
return Transform::xsl(
$tei, dirname(dirname(__FILE__)).'/transform/tei_html.xsl', $destFile, array(
"dipleHref"=>self::$dipleHref,
"corpusId" =>basename($srcPath),
), array (
$srcPath.".odt",
$odt_tei.'Odt.php',
$odt_tei.'odt_tei.xsl',
),
$force
);
}
if (file_exists($file=$srcPath.".xml")) {
Diple::$xslHtml=Diple::$dipleDir."transform/tei_html.xsl";
if (file_exists($file_xsl=Diple::$corpusDir.'transform/'.Diple::$corpusId.'_html.xsl'))Diple::$xslHtml=$file_xsl;
Transform::xsl (
$file, self::$xslHtml, $destFile, null,
null,
$force
);
return $destFile;
}
if (is_dir($srcPath)) {
$file=self::htmlFile($pathinfo.'/welcome', $force, $srcDir, $destDir);
if (!$file) $file=self::htmlFile($pathinfo.'/index', $force, $srcDir, $destDir);
return $file;
}
if (file_exists($file=$srcPath.".htm")) return $file;
if (file_exists($file=$srcPath.".html")) return $file;
echo "<!-- Diple::htmlFile Failed with src=",$srcPath,"-->\n";
return null;
}
static public function writeMeta($corpus, $htmlIncArray) {
if (count($htmlIncArray) > 1) {
foreach ($corpus as $key => $value) {
if (is_array($value)) {
foreach ($value as $lastvalue) echo "<meta name=\"dc.$key\" content=\"$lastvalue\"/>\n";
}
elseif ($key == "title" && $value) echo "<title>$value</title>\n"; elseif ($value) echo "<meta name=\"dc.$key\" content=\"$value\"/>\n";
}
}
elseif ($var=reset($htmlIncArray)) echo $var->meta(); unset($var);
}
}
Diple::$dipleDir=dirname(dirname(__FILE__))."/";
Diple::$request=Web::pathinfo();
if ( ($corpus=basename($_SERVER['SCRIPT_FILENAME'], ".php")) != "index" && is_dir(dirname($_SERVER['SCRIPT_FILENAME'])."/".$corpus) ) {
Diple::$corpusId=$corpus;
Diple::$corpusDir=dirname($_SERVER['SCRIPT_FILENAME'])."/".$corpus."/";
Diple::$corpusHref=str_repeat("../", substr_count(Diple::$request, '/')); }
else {
Diple::$corpusId=basename(dirname($_SERVER['SCRIPT_FILENAME']));
Diple::$corpusDir=dirname($_SERVER['SCRIPT_FILENAME'])."/";
Diple::$corpusHref=str_repeat("../", substr_count(Diple::$request, '/')); }
Diple::$dipleHref=Diple::$corpusHref."../diple/";
if (file_exists(Diple::$xmlDir=Diple::$corpusDir."xml/"));
else if (file_exists(Diple::$xmlDir=Diple::$corpusDir."src/"));
else Diple::$xmlDir=Diple::$corpusDir;
if (file_exists(Diple::$docDir=Diple::$corpusDir."doc/"));
require_once dirname(__FILE__).'/File.php';
if(is_writable(Diple::$htmlDir=Diple::$corpusDir."html/"));
else if (Diple::$xmlDir == Diple::$corpusDir && is_writable(Diple::$htmlDir=Diple::$corpusDir));
else if(is_writable($dir=dirname(Diple::$corpusDir).'/cache/') || is_writable($dir=dirname(Diple::$corpusDir).'/html/')) {
Diple::$htmlDir=$dir.Diple::$corpusId."/";
if (!is_dir(Diple::$htmlDir)) mkdir(Diple::$htmlDir, 0777, true);
chmod(Diple::$htmlDir, 0777);
}
else {
Diple::$htmlDir=sys_get_temp_dir()."/diple/".Diple::$corpusId."/";
if (!file_exists(Diple::$htmlDir)) mkdir(Diple::$htmlDir, 0777, true);
chmod(Diple::$htmlDir, 0777);
}
@set_time_limit(3600); ?>