diff options
author | Wojciech Polak <polak@gnu.org> | 2004-10-09 11:50:42 +0000 |
---|---|---|
committer | Wojciech Polak <polak@gnu.org> | 2004-10-09 11:50:42 +0000 |
commit | 228298be889b5b9e5cd981fff1bb1fa3df54521d (patch) | |
tree | 30bd8ce4ac47fa6fa578cacea799cb3089619174 | |
download | blogright-228298be889b5b9e5cd981fff1bb1fa3df54521d.tar.gz blogright-228298be889b5b9e5cd981fff1bb1fa3df54521d.tar.bz2 |
Initial revision
-rw-r--r-- | config.php | 27 | ||||
-rw-r--r-- | index.php | 476 |
2 files changed, 503 insertions, 0 deletions
diff --git a/config.php b/config.php new file mode 100644 index 0000000..2a1ea6f --- /dev/null +++ b/config.php @@ -0,0 +1,27 @@ +<?php + +// BEGIN CONFIG + +$site = 'http://your.site.name'; +$weblogdir = 'blogright'; +$imagedir = 'blogright/graphics'; +$photodir = 'blogright/photos'; + +$suffix = '.html'; // entries' file extension +$recent = 'month'; // number of entries shown by default +$sortby = 'name'; // sort by: 'name', 'timestamp' +$sortorder = 'desc'; // sorting order: 'asc', 'desc' +$orderfile = '.order'; // [optional] order file name + +$header_file = 'templates/header.html'; +$footer_file = 'templates/footer.html'; + +$rssTitle = "RSS Title"; +$rssDescription = "Some descriptive title"; +$rssCopyright = "Your Name"; +$rssEditor = "Editor Name"; +$rssLanguage = "qu"; // Two-letter language code as per ISO 639 + +// END CONFIG + +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..910fa25 --- /dev/null +++ b/index.php @@ -0,0 +1,476 @@ +<?php + +// blogRight! version 1.3 +// Copyright (C) 2004 Wojciech Polak. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require 'config.php'; + +///////////////////////////// + +$start_time = microtime (); + +if (isset ($_REQUEST['q'])) +{ + $q = $_REQUEST['q']; + $result = array (); + ereg ('([0-9]{4})?[-/]?([0-1][0-9])?[-/]?([0-3][0-9])?', $q, $result); + + if (isset ($result[1]) && $result[1] != '') + { + $search_y = $result[1]; + + if (isset ($result[2]) && $result[2] != '') + $search_m = $result[2] <= 12 ? $result[2] > 0 ? $result[2] : 1 : 12; + + if (isset ($result[3]) && $result[3] != '') + $search_d = $result[3] <= 31 ? $result[3] > 0 ? $result[3] : 1 : 31; + } + else + { + if (eregi ('^all$', $q)) + unset ($recent); + } +} + +///////////////////////////// + +if (isset ($datadir) && $datadir != '') +{ + $cwd = $datadir; +} +else +{ + $datadir = ''; + $cwd = getcwd (); +} + +if (file_exists ($datadir.$orderfile)) +{ + $lines = file ($datadir.$orderfile); + foreach ($lines as $line) + $searchdirs[] = chop ($line); + $searchdirs = array_unique ($searchdirs); +} +else if (isset ($search_y)) +{ + $searchdirs[] = $search_y; +} +else // scan the directory +{ + if (!($dp = opendir ($cwd))) + die ("Can't open the data directory."); + while ($name = readdir ($dp)) + { + if (is_numeric ($name)) + $searchdirs[] = $name; + } + closedir ($dp); + sort ($searchdirs); + + if ($sortorder == 'desc') + $searchdirs = array_reverse ($searchdirs); +} + +///////////////////////////// + +$include_files = array (); + +foreach ($searchdirs as $dir) +{ + $path = $datadir.$dir; + $dirlist = array (); + + // ignore hidden files/dirs. + if ($dir[0] != '.' && $dir[0] != '' && is_dir ($path)) + { + if (file_exists ($path.'/'.$orderfile)) + { + $lines = file ($path.'/'.$orderfile); + foreach ($lines as $line) + $dirlist[] = $path.'/'.chop ($line); + } + else // scan the directory + { + if (!($dp = opendir ($path))) + die ("Can't open the data subdirectory."); + else + { + $i = 0; + while ($name = readdir ($dp)) + { + if ($name[0] != '.' && $name != 'index.html') + { + $dirlist[filemtime ($path.'/'.$name) . $i] = $path.'/'.$name; + $i++; + } + } + closedir ($dp); + + if ($sortby == "timestamp") + ksort ($dirlist); + else // sort by name + sort ($dirlist); + } + } + + if ($sortorder == 'desc' && !isset ($search_y)) + $dirlist = array_reverse ($dirlist); + + $include_files = array_merge ($include_files, $dirlist); + } +} + +///////////////////////////// + +$cur_y = date ('Y'); +$cur_m = date ('m'); + +function cmonth ($var) +{ + global $cur_y, $cur_m, $suffix; + if (ereg ("$cur_y/$cur_m-.*$suffix\$", $var)) + return true; + else + return false; +} + +// show only N recent entries or current month +if (!isset ($search_y) && isset ($recent)) +{ + if (is_numeric ($recent)) + { + $include_files = array_slice ($include_files, 0, $recent); + } + else if ($recent == 'month') + { + $month_files = array_filter ($include_files, 'cmonth'); + while (!count ($month_files)) + { + $cur_m = sprintf ('%02d', $cur_m - 1); + if ($cur_m == '00') + { + $cur_m = 12; + $cur_y -= 1; + if ($cur_y < 1983) // set your minimal value ;) + break; // fatal error; anti-loop protection + } + $month_files = array_filter ($include_files, 'cmonth'); + } + $include_files = $month_files; + } +} + +///////////////////////////// + +if (isset ($RSS) || isset ($_REQUEST['rss'])) +{ + global $site, $rssTitle, $rssDescription; + global $rssCopyright, $rssLanguage, $rssEditor; + + $pubDate = date ("D, d M Y H:i:s O", filemtime ($include_files[0])); + + print '<?xml version="1.0"?>'."\n"; + print '<rss version="2.0">'."\n"; + print " <channel>\n"; + print " <title>$rssTitle</title>\n"; + print " <description>$rssDescription</description>\n"; + print " <link>$site/$weblogdir/</link>\n"; + print " <copyright>$rssCopyright</copyright>\n"; + print " <language>$rssLanguage</language>\n"; + print " <pubDate>$pubDate</pubDate>\n"; + print " <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n"; + print " <generator>blogRight!</generator>\n"; + print " <managingEditor>$rssEditor</managingEditor>\n"; + + foreach ($include_files as $inc) + { + if (ereg (".*$suffix\$", basename ($inc))) + { + $cdd = basename (dirname ($inc)); // current data dir + if (is_numeric ($cdd)) + { + ereg ('^(.*)-(.*)\.', basename ($inc), $res); + $year = $cdd; // let's assume that... + + if (isset ($res[1]) && isset ($res[2])) + { + $month = $res[1]; + $day = $res[2]; + } + + $fmtime = mktime (0, 0, 0, $month, $day, $year); + } + else { + $fmtime = filemtime ($inc); + } + + $pubDate = date ("D, d M Y H:i:s O", $fmtime); + $itemTitle = date ('d M Y', $fmtime); + $permaLink = date ('Ymd', $fmtime); + + print " <item>\n"; + print " <title>$itemTitle</title>\n"; + print " <link>$site/$weblogdir/?q=$permaLink</link>\n"; + print " <description>\n"; + + indent_include (9, $inc); + + print " </description>\n"; + print " <guid isPermaLink=\"true\">$site/$weblogdir/?q=$permaLink</guid>\n"; + print " <pubDate>$pubDate</pubDate>\n"; + print " </item>\n"; + } + } + + print " </channel>\n"; + print '</rss>'."\n"; +} +else // HTML +{ + @include ($datadir.$header_file); + + foreach ($include_files as $inc) + { + // the query engine... + if (isset ($search_m)) + { + if (isset ($search_d)) + { + if (ereg ("^$search_m-$search_d.*$suffix\$", basename ($inc))) + include_file ($inc); + } + else + if (ereg ("^$search_m-.*$suffix\$", basename ($inc))) + include_file ($inc); + } + else + if (ereg (".*$suffix\$", basename ($inc))) + include_file ($inc); + } + + @include ($datadir.$footer_file); + $duration = microtime_diff ($start_time, microtime ()); + $duration = sprintf ("%0.6f", $duration); + print "\n<!-- processing took $duration seconds -->"; + print "\n<!-- powered by blogRight! -->\n"; +} + +///////////////////////////// + +$footnotes = array(); // Array of footnote texts. Indexed by $local_fn +$local_fn = 0; // Local (within the entry) number of the next footnote. +$global_fn = 0; // Global number of the next footnote. This is unique + // within a html page and is used for creating unique + // anchors. + +// Return full url of the script +function script_url() +{ + global $site,$weblogdir; + if (isset($_REQUEST['q'])) + return $site.'/'.$weblogdir.'?q='.$_REQUEST['q']; + return $site.'/'.$weblogdir.'/'; +} + +// Save away the collected footnote text and replace it by an appropriate +// link. +function footnote_handler ($matches) +{ + global $site,$global_fn,$local_fn,$footnotes; + $footnotes[$local_fn++] = $matches[1]; + $global_fn++; + return '<a href="'.script_url().'#footnote'.$global_fn.'" name="FNRET'.$global_fn.'"><sup>'.$local_fn.')</sup></a> '; +} + +// Process a single input line from the file being included. +// Returns processed line, if it is ready for output. Otherwise, +// tucks the line to the global variable $collect and returns false. +// Make sure you unset $collect before the *first* invocation of process_line() +// for the given input file. +function process_line ($line) +{ + global $site, $photodir, $imagedir, $search_d; + global $local_fn, $global_fn, $footnotes; + global $collect; + + if (isset($collect)) + { + $collect = $collect . ' ' . rtrim ($line); + + if (!preg_match('^</footnote>^', $line)) + return false; + $line = $collect . "\n"; + unset($GLOBALS['collect']); + } + + $line = preg_replace_callback('^<footnote>(.*)</footnote>^', + "footnote_handler", + $line); + + if (preg_match ('^<footnote>^', $line)) + { + $collect = rtrim ($line); + return false; + } + + if (!preg_match ('/="(https?|ftp|mailto):\/\/.*?"/i', $line)) + // is a local path + { + $line = preg_replace ('/<img src="(.*?)"(.*?)>/i', + '<img src="'.$site.'/'.$imagedir.'/$1" border="0" alt="[image]"$2>', + $line); + + $line = preg_replace ('/<photo src="(.*?)"(.*?)>/i', + '<a href="'.$photodir.'/photo.php?q=$1"><span class="photo"><img src="' + .$site.'/'.$photodir.'/small/small-$1" alt="[photo]"$2></span></a>', + $line); + + $line = preg_replace ('/<a href="(.*?)">/i', '<a href="'.$site.'/$1">', $line); + } + return $line; +} + +// Process the footnotes collected for the entry. +// Arguments: +// $out - name of the output function. The function must be +// declared as 'function foo($data, $string). It is passed $data closure +// and the $string to be output. +// $data - Any function-specific data $out may need. +function process_footnotes ($out, $data) +{ + global $local_fn, $global_fn, $footnotes; + + if ($local_fn > 0) + { + $out ($data, '<div class="footnote">'); + for ($i = 0; $i < $local_fn; $i++) + { + $n = $global_fn-$local_fn+$i+1; + $out ($data, '<p><a href="'.script_url().'#FNRET'.$n.'" name="footnote'.$n.'"><sup>'.($i+1).')</sup></a> '); + $out ($data, $footnotes[$i]); + $out ($data, '</p>'); + } + $out ($data, '</div>'); + } +} + +// Simple printer (alias for print, to be used with process_footnotes) +function printer ($data, $string) +{ + print ($string); +} + +// HTML output +function include_file ($file) +{ + global $site, $photodir, $imagedir, $search_d; + global $local_fn; + + $res = array (); + $cdd = basename (dirname ($file)); // current data dir + + $local_fn = 0; + if (is_numeric ($cdd)) + { + ereg ('^(.*)-(.*)\.', basename ($file), $res); + $year = $cdd; // assume that... + + if (isset ($res[1]) && isset ($res[2])) + { + $month = $res[1]; + $day = $res[2]; + } + + $entryDate = mktime (0, 0, 0, $month, $day, $year); + $permaLink = date ('Ymd', $entryDate); + $entryDate = date ('d M Y', $entryDate); + } + else + { + $permaLink = date ('Ymd', filemtime ($file)); + $entryDate = date ('d M Y', filemtime ($file)); + } + + $fd = file ($file); + if ($fd) + { + print '<p><div class="entryTitle">'.$entryDate; + + if (!isset ($search_d)) + print ' (<a href="?q='.$permaLink.'">permalink</a>)'; + + print '</div>'."\n"; + print '<div class="entryBody">'."\n"; + + unset($GLOBALS['collect']); + foreach ($fd as $line) + { + $line = process_line($line); + if ($line) + print ($line); + } + + process_footnotes ('printer', 0); + + print '</div>'."\n\n"; + } +} + +function printer_html ($level, $string) +{ + for ($i = 0; $i < $level; $i++) + print ' '; + print (htmlentities ($string) . "\n"); +} + + +// RSS 2.0 output +function indent_include ($level, $file) +{ + global $site, $imagedir, $photodir, $weblogdir; + global $local_fn; + + $cdd = basename (dirname ($file)); // current data dir + + $local_fn = 0; + $fd = file ($file); + if ($fd) + { + unset($GLOBALS['collect']); + foreach ($fd as $line) + { + $line = process_line($line); + if ($line) + { + for ($i = 0; $i < $level; $i++) + print ' '; + print (htmlentities ($line)); + } + } + + process_footnotes ('printer_html', $level); + } +} + +function microtime_diff ($a, $b) +{ + list ($a_dec, $a_sec) = explode (' ', $a); + list ($b_dec, $b_sec) = explode (' ', $b); + return $b_sec - $a_sec + $b_dec - $a_dec; +} + +?> |