article.phpDenna kod är public domain. Om ni hittar fel eller vill ändra något i koden blir jag jätteglad om ni skickar dessa ändringar till jesper [at] fantasi [punkt] se.
<?php
/**
* TODO: Separat program som genererar xml-filer av källkodsfilerna så att källkoder kan ligga kvar i ~/source och filerna visas av article.php (dumpa sourceview)
* TODO: Thumbnail i index
* TODO: <related>nyckelord#</related> -> Se även --- nyckelord1 | nyckelord2
* TODO: Nyckelord: <code>void</code>
* TODO: Parsa efter nyckelord i tabeller.
* TODO: Skriv om returtyper på Amigan i returnera.xml
* TODO: escape-sekvenser.xml är inte klar.
*/
include("framework.php");
function str2array($str) {
return explode("-+-", $str);
}
function strtourl($str) {
$result = "";
for ($i = 0; $i < strlen($str); $i++) {
$ch = $str[$i];
switch ($ch) {
case ' ': $result = $result."%20"; break;
case '&': $result = $result."%26"; break;
case '+': $result = $result."%2b"; break;
case '-': $result = $result."%2d"; break;
case 'Å': $result = $result."%e5"; break;
case 'Ä': $result = $result."%e4"; break;
case 'Ö': $result = $result."%f6"; break;
case 'å': $result = $result."%c5"; break;
case 'ä': $result = $result."%c4"; break;
case 'ö': $result = $result."%d6"; break;
default: $result = $result.$ch;
}
}
return $result;
}
function strtohtml($str) {
$result = "";
$isoStr = utf8_decode($str);
for ($i = 0; $i < strlen($isoStr); $i++) {
$ch = $isoStr[$i];
switch ($ch) {
case utf8_decode('Å'): $result = $result.utf8_encode("Å"); break;
case utf8_decode('Ä'): $result = $result.utf8_encode("Ä"); break;
case utf8_decode('Ö'): $result = $result.utf8_encode("Ö"); break;
case utf8_decode('å'): $result = $result.utf8_encode("å"); break;
case utf8_decode('ä'): $result = $result.utf8_encode("ä"); break;
case utf8_decode('ö'): $result = $result.utf8_encode("ö"); break;
case utf8_decode('é'): $result = $result.utf8_encode("é"); break;
default: $result = $result.utf8_encode($ch);
}
}
return $result;
}
function articleStart($parser, $elementName, $elementAttrs) {
echo strtohtml($GLOBALS["word"]);
$GLOBALS["word"] = "";
if (!$GLOBALS["display"]) {
return;
}
switch($elementName) {
case "KEYWORD":
xml_set_character_data_handler($parser, "ignore");
break;
case "TOPIC":
xml_set_character_data_handler($parser, "type");
if ($elementAttrs["LABEL"] != null) {
echo "<a name=\"".strtohtml($elementAttrs["LABEL"])."\"></a>";
}
if ($elementAttrs["SUBTITLE"] != null) {
$GLOBALS["subtitle"] = "<br><font size=\"-1\">".$elementAttrs["SUBTITLE"]."</font>";
} else {
$GLOBALS["subtitle"] = "";
}
echo "<h1>";
break;
case "SUBTOPIC":
if ($elementAttrs["LABEL"] != null) {
echo "<a name=\"".strtohtml($elementAttrs["LABEL"])."\"></a>";
}
if ($elementAttrs["SUBTITLE"] != null) {
$GLOBALS["subtitle"] = "<br><font size=\"-1\">".$elementAttrs["SUBTITLE"]."</font>";
} else {
$GLOBALS["subtitle"] = "";
}
if ($elementAttrs["DATE"] != null) {
$GLOBALS["topicdate"] = $elementAttrs["DATE"];
} else {
$GLOBALS["topicdate"] = "";
}
if ($elementAttrs["ORIGLINK"] != null) {
if ($GLOBALS["language"] == "SE") {
$orig_text = "Originaldesign";
} else {
$orig_text = "Original design";
}
$GLOBALS["topictext"] = $orig_text.": <a href=\"".$elementAttrs["ORIGLINK"]."\" target=\"_blank\">".$elementAttrs["TEXT"]."</a>";
} else if ($elementAttrs["TEXT"] != null) {
$GLOBALS["topictext"] = $elementAttrs["TEXT"];
} else {
$GLOBALS["topictext"] = "";
}
echo "<h3>";
break;
case "SECTION":
$GLOBALS["found_language"] = false;
break;
case "LANGUAGE":
if ($elementAttrs["LANG"] == $GLOBALS["language"]) {
$GLOBALS["found_language"] = true;
} else if ($GLOBALS["found_language"] || $elementAttrs["LANG"] != $GLOBALS["default_language"]) {
xml_set_character_data_handler($parser, "ignore");
$GLOBALS["display"] = false;
}
break;
case "INGRESS":
echo "<p class=\"ingress\">";
break;
case "TEXT":
if ($elementAttrs["STYLE"] == "small") {
echo "<p class=\"imgtext\">";
} else if ($elementAttrs["STYLE"] == "large") {
echo "<p class=\"slogan\">";
} else if ($elementAttrs["STYLE"] == "alert") {
echo "<p class=\"alert\">";
} else {
echo "<p class=\"maincontent\">";
}
if ($elementAttrs["TOPIC"] != null) {
echo "<b>".strtohtml($elementAttrs["TOPIC"])."</b> ";
}
break;
case "A":
xml_set_character_data_handler($parser, "type");
echo "<a href=\"".strtohtml($elementAttrs["HREF"])."\">";
break;
case "IMAGE":
if ($elementAttrs["ALIGN"] == "center") {
echo "<div align=\"center\">";
} else if ($elementAttrs["ALIGN"] == "left") {
echo "<div align=\"left\">";
} else if ($elementAttrs["ALIGN"] == "right") {
echo "<div align=\"right\">";
}
if ($elementAttrs["BOX"] == "true" || $elementAttrs["BOX"] == "notext") {
echo "<table class=\"imgframe\"><tr><td class=\"fullcell\">";
}
if ($elementAttrs["LINK"] != null) {
echo "<a href=\"".$elementAttrs["LINK"]."\">";
}
echo "<img width=\"".$elementAttrs["WIDTH"]."\" height=\"".$elementAttrs["HEIGHT"];
echo "\" src=\"gfx/".strtohtml($elementAttrs["SRC"])."\"";
echo " alt=\"".strtohtml($elementAttrs["ALT"])."\">";
if ($elementAttrs["LINK"] != null) {
echo "</a>";
}
if ($elementAttrs["BOX"] == "true")
{
echo "<p class=\"imgtext\">";
echo strtohtml($elementAttrs["ALT"]);
echo "</p></td></tr></table>";
} else if ($elementAttrs["BOX"] == "notext") {
echo "</td></tr></table>";
}
if ($elementAttrs["ALIGN"] != null) {
echo "</div>";
}
break;
case "IMAGEGROUP":
if ($elementAttrs["ALIGN"] == "center") {
echo "<div align=\"center\">";
} else if ($elementAttrs["ALIGN"] == "left") {
echo "<div align=\"left\">";
} else if ($elementAttrs["ALIGN"] == "right") {
echo "<div align=\"right\">";
} else {
echo "<div>";
}
echo "<table class=\"imgframe\"><tr><td class=\"fullcell\">";
$GLOBALS["imagetext"] = $elementAttrs["TEXT"];
break;
case "EM":
echo "<em>";
break;
case "CODE":
echo "<code>";
break;
case "CODEBOX":
if ($elementAttrs["TITLE"] != null) {
echo "<div class=\"codebox\"><h4 class=\"codeboxtitle\">".
strtohtml($elementAttrs["TITLE"])."</h4><pre class=\"nomargin\">";
} else {
echo "<div class=\"codebox\"><pre class=\"nomargin\">";
}
break;
case "FORMULABOX":
if ($elementAttrs["TITLE"] != null) {
echo "<div class=\"formulabox\"><h4 class=\"codeboxtitle\">".
strtohtml($elementAttrs["TITLE"])."</h4><pre class=\"nomargin\">";
} else {
echo "<div class=\"formulabox\"><pre class=\"nomargin\">";
}
break;
case "LT":
echo "<";
break;
case "GT":
echo ">";
break;
case "AMP":
echo "&";
break;
case "NBSP":
echo " ";
break;
case "LIST":
echo "<ul class=\"maincontent\">";
break;
case "ITEM":
echo "<li>";
break;
case "SUB":
echo "<sub>";
break;
case "COLUMN":
$width = "";
if ($elementAttrs["WIDTH"] != null) {
$width = " width=\"".$elementAttrs["WIDTH"]."\"";
}
if ($elementAttrs["ALIGN"] != null) {
echo "<table class=\"noborder fullwidth\"><tr><td".$width." valign=\"".$elementAttrs["ALIGN"]."\" class=\"fullcell-na\">";
} else {
echo "<table class=\"noborder fullwidth\"><tr><td".$width." class=\"fullcell\">";
}
break;
case "COLUMNBREAK":
$width = "";
if ($elementAttrs["WIDTH"] != null) {
$width = " width=\"".$elementAttrs["WIDTH"]."\"";
}
if ($elementAttrs["ALIGN"] != null) {
echo "</td><td".$width." valign=\"".$elementAttrs["ALIGN"]."\" class=\"fullcell-na\">";
} else {
echo "</td><td".$width." class=\"fullcell\">";
}
break;
case "TABLE":
if ($elementAttrs["START2"] == null) {
if ($elementAttrs["INNER"] == "true") {
fitable($elementAttrs["START"],
str2Array(strtohtml($elementAttrs["HEADER"])),
str2array(strtohtml($elementAttrs["STYLE"])));
} else {
ftable($elementAttrs["START"],
str2Array(strtohtml($elementAttrs["HEADER"])),
str2array(strtohtml($elementAttrs["STYLE"])));
}
} else {
ftable2($elementAttrs["START1"],
str2Array(strtohtml($elementAttrs["HEADER1"])),
$elementAttrs["START2"],
str2Array(strtohtml($elementAttrs["HEADER2"])),
str2array(strtohtml($elementAttrs["STYLE"])));
}
break;
case "HEADER":
ftheader(str2array(strtohtml($elementAttrs["DATA"])),
str2array(strtohtml($elementAttrs["STYLE"])));
break;
case "ROW":
if ($elementAttrs["STYLE"] != null) {
$style = str2array(strtohtml($elementAttrs["STYLE"]));
} else {
$style = null;
}
if ($elementAttrs["DATA2"] == null) {
ftline(str2array(strtohtml($elementAttrs["DATA"])), $style);
} else {
ftline2(str2array(strtohtml($elementAttrs["DATA1"])),
str2array(strtohtml($elementAttrs["DATA2"])), $style);
}
break;
case "APPLET":
echo "<p class=\"formulabox\"><applet codebase=\"java\" code=\"".
$elementAttrs["CLASS"]."\" width=\"".$elementAttrs["WIDTH"]."\" height=\"".
$elementAttrs["HEIGHT"]."\" alt=\"".strtohtml($elementAttrs["ALT"])."\">";
echo strtohtml($elementAttrs["ALT"]);
echo "</applet></p>";
break;
}
}
function articleStop($parser, $elementName) {
echo strtohtml($GLOBALS["word"]);
$GLOBALS["word"] = "";
if ($elementName == "LANGUAGE") {
xml_set_character_data_handler($parser, "keywordFinder");
$GLOBALS["display"] = true;
return;
}
if (!$GLOBALS["display"]) {
return;
}
switch($elementName) {
case "KEYWORD":
xml_set_character_data_handler($parser, "keywordFinder");
break;
case "TOPIC":
echo strtohtml($GLOBALS["subtitle"])."</h1>";
xml_set_character_data_handler($parser, "keywordFinder");
break;
case "SUBTOPIC":
echo strtohtml($GLOBALS["subtitle"])."</h3>";
echo "<table class=\"fullwidth maincontent\"><tr><td><div class=\"topictext\">".strtohtml($GLOBALS["topictext"])."</div></td><td><div class=\"topicdate\">".strtohtml($GLOBALS["topicdate"])."</div></td></tr></table>";
break;
case "SECTION":
break;
case "INGRESS":
case "TEXT":
echo "</p>";
break;
case "A":
echo "</a>";
xml_set_character_data_handler($parser, "keywordFinder");
break;
case "IMAGEGROUP":
if ($GLOBALS["imagetext"] != null) {
echo "<p class=\"imgtext\">";
echo strtohtml($GLOBALS["imagetext"]);
echo "</p>";
}
echo "</td></tr></table>";
echo "</div>";
break;
case "EM":
echo "</em>";
break;
case "CODE":
echo "</code>";
break;
case "CODEBOX":
case "FORMULABOX":
echo "</pre></div>";
break;
case "LIST":
echo "</ul>";
break;
case "SUB":
echo "</sub>";
break;
case "COLUMN":
echo "</td></tr></table>";
break;
case "TABLE":
ftend();
break;
case "FITABLE":
fitend();
break;
}
}
function type($parser, $data) {
// Only print this text.
echo strtohtml($data);
}
function keywordFinder($parser, $text) {
// Split data into words and search $keywords for a match.
$length = strlen($text);
for ($i = 0; $i < $length; $i++) {
$ch = substr($text, $i, 1);
if (preg_match("'[a-zA-ZåäöÅÄÖ0-9_\-]'", $ch)) {
$GLOBALS["word"] = $GLOBALS["word"].$ch;
} else {
if (array_key_exists(strtolower($GLOBALS["word"]), $GLOBALS["keys"]) &&
strcmp($GLOBALS["keys"][strtolower($GLOBALS["currentKey"])]["Target"],
$GLOBALS["keys"][strtolower($GLOBALS["word"])]["Target"]) != 0) {
echo "<a class=\"keyword\" href=\"article.php?cat=".
$GLOBALS["keys"][strtolower($GLOBALS["word"])]["category"]."&key=".$GLOBALS["word"].
"&lang=".$GLOBALS["language"]."\">".strtohtml($GLOBALS["word"])."</a>".strtohtml($ch);
} else {
echo strtohtml($GLOBALS["word"].$ch);
}
$GLOBALS["word"] = "";
}
}
}
/****************************************************************************
* Create the keyword table.
*/
$sourceDir = "articles/";
$category = $_GET["cat"];
$language = $_GET["lang"];
$default_language = "SE";
if ($language == null) {
$language = $default_language;
}
$display = true;
$statistics = isset($_GET["stat"]);
if (file_exists($sourceDir.$category)) {
$sourceDir = $sourceDir.$category."/";
}
keysIndexBuilder($sourceDir);
// foreach ($keys as $k) {
// echo "TheKey: ".$k["TheKey"]."<br>";
// echo "Target: ".$k["Target"]."<br>";
// echo "shortDesc: ".$k["shortDesc"]."<br>";
// echo "visible: ".$k["visible"]."<br>";
// echo "moreText: ".$k["moreText"]."<br>";
// echo "category: ".$k["category"]."<br>";
// echo "Image: ".$k["Image"]."<br>";
// echo "Count: ".$k["Count"]."<br>";
// }
if ($_GET["key"] != null) {
/**************************************************************************
* Display article based on keyword.
*/
$target = $keys[strtolower($_GET["key"])]["Target"];
if ($target != NULL) {
$topic = $keys[strtolower($_GET["key"])]["TheKey"];
$title = "Källkodskällan - ".$topic;
} else {
$topic = $_GET["key"];
$title = "Källkodskällan - Keyword not found";
}
$GLOBALS["currentKey"] = $topic;
if ($target != NULL && file_exists($sourceDir.$target.".xml")) {
$currentFile = $sourceDir.$target.".xml";
include("header.php");
$counter_file = "counters/".strtolower($target);
if (file_exists($counter_file)) {
$count = intval(file_get_contents($counter_file));
$count++;
} else {
$count = 1;
}
file_put_contents($counter_file, $count);
$parser = xml_parser_create();
xml_set_element_handler($parser, "articleStart", "articleStop");
xml_set_character_data_handler($parser, "keywordFinder");
$file = html_entity_decode(file_get_contents($sourceDir.$target.".xml"));
$GLOBALS["word"] = "";
xml_parse($parser, $file) or die (sprintf("XML Error during page read: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
xml_parser_free($parser);
if ($language == "EN") {
echo "<p class=\"formulabox\"><a href=\"article.php?cat=".$category."&lang=".$language.
"\">Back to index</a></p>";
} else {
echo "<p class=\"formulabox\"><a href=\"article.php?cat=".$category."&lang=".$language.
"\">Tillbaka till indexet</a></p>";
}
} else {
$currentFile = "KeyNotFound-".$topic;
include("header.php");
echo "<h1>".$topic."</h1>";
echo "<p class=\"maincontent\">Keyword not found.</p>";
}
} else {
/**************************************************************************
* Generate index over all articles.
*/
$title = "Artikelindex";
$category = $_GET["cat"];
$currentFile = __FILE__;
include("header.php");
if ($category == "software") {
if ($language == "EN") {
echo "<h1>Article index and prorgammers dictionary</h1>";
echo "<p class=\"maincontent\">";
echo "If you don't find the word you are looking for use ";
echo "<a href=\"ask.php\">this form</a> to tell me what you want to ";
echo "read about.";
echo "</p>";
} else {
echo "<h1>Artikelindex och ordlista med programmeringstermer</h1>";
echo "<p class=\"maincontent\">";
echo "Hittar du inte termen eller förklaringen du söker? ";
echo "Använd <b><a href=\"ask.php\">frågeformuläret</a></b> ";
echo "för att tala om vad du vill läsa om.";
echo "</p>";
}
} else if ($category == "hardware") {
if ($language == "EN") {
echo "<h1>Blueprints and other hardware information</h1>";
echo "<p class=\"maincontent\">";
echo "Most of these hardware projects are meant for the Amiga computer. ";
echo "Some of these schematics are my own design, some are modifications of schematics ";
echo "found on the Aminet, and some are simply copied and reworked to fit the layout ";
echo "of this page. I only publish schematics that I have built myself and know that ";
echo "they works. Do you want help to build or do you have other questions regarding ";
echo "Amiga hardware? <a href=\"contact.php\">Contact Fantasi.</a>";
echo "</p>";
} else {
echo "<h1>Ritningar och annan hårdvaruinformation</h1>";
echo "<p class=\"maincontent\">";
echo "De flesta av dessa hårdvaruprojekt är för Amiga classic. ";
echo "Några av byggprojekten som finns beskrivna här är översättningar ";
echo "av ritningar som finns på Aminet. ";
echo "Vill du ha hjälp att bygga eller har du andra frågor om ";
echo "Amigahårdvara? <a href=\"contact.php\">Hör av dig till Fantasi.</a>";
echo "</p>";
}
} else if ($category == "lyrics") {
if ($language == "EN") {
echo "<h1>Lyrics</h1>";
echo "<p class=\"maincontent\">";
echo "These are a bunch of lyrics that I have composed. Mainly computer related, almost ";
echo "all in Swedish. I take no responsibility for what Google translate might think that ";
echo "they mean. ";
echo "</p>";
} else {
echo "<h1>Sångtexter</h1>";
echo "<p class=\"alert\">";
echo "<font size=\"+2\"><b>Varning!</b></font><br>";
echo "De flesta sångtexter som publiceras här är skrivna i olika studentsammanhang och har ett ";
echo "ganska grabbigt studentgäng (datavetare) som målgrupp. En del texter ";
echo "är varken politiskt korrekta, genomtänkta eller lämpade för de yngre. ";
echo "<br><b>Läs dem på egen risk.</b>";
echo "</p>";
}
} else {
if ($language == "EN") {
echo "<h1>Fantasi's article database</h1>";
} else {
echo "<h1>Fantasis artikeldatabas</h1>";
}
}
$lastFirstChar = 0;
echo "<table class=\"ftable\">";
ksort($keys, SORT_STRING);
$index = count($keys);
reset($keys);
while ($index > 0) {
$index--;
$key = key($keys);
$keyObject = current($keys);
next($keys);
if (!$keyObject["visible"] ||
($category != NULL && $keyObject["category"] != $category && $keyObject["category"] != "")) {
continue;
}
$firstChar = ord(utf8_decode(strtoupper($key)));
if ($firstChar < 65) {
$firstChar = 64;
}
if ($firstChar != $lastFirstChar) {
echo "<tr><th> </th>";
if ($statistics) {
echo "<th> </th>";
}
echo "<td class=\"fheader\"><b>- ";
if ($firstChar > 64) {
echo strtohtml(utf8_encode(chr($firstChar)));
} else {
if ($language == "EN") {
echo "Numbers & Symbols";
} else {
echo "Siffror & Symboler";
}
}
echo " -</b></td></tr><tr><td class=\"line\" colspan=\"2\"></td></tr>";
$lastFirstChar = $firstChar;
}
$file = strtourl($keyObject["TheKey"]);
echo "<tr><th class=\"framelineheader\"><a class=\"menu-header\" name=\"";
echo strtohtml($key)."\" href=\""."article.php?cat=".$category."&key=".$file."&lang=".$language.
"\">".strtohtml($keyObject["TheKey"])."</a></th>";
if ($statistics) {
echo "<th class=\"framelineheader\">".$keyObject["count"]."</th>";
}
echo "<td class=\"framedata\">";
echo strtohtml($keyObject["shortDesc"]);
if ($keyObject["moreText"]) {
if ($language == "EN") {
$read_more = "Read more...";
} else {
$read_more = "Läs mer här...";
}
echo "<br><a href=\""."article.php?cat=".$category."&key=".$file."&lang=".$language.
"\">".$read_more."</a>";
}
echo "</td></tr><tr><td class=\"line\" colspan=\"2\"></td></tr>";
}
echo "</table>";
}
include("footer.php");
?>
|