diff options
-rw-r--r-- | blog.php | 519 | ||||
-rw-r--r-- | config.php | 3 |
2 files changed, 522 insertions, 0 deletions
diff --git a/blog.php b/blog.php new file mode 100644 index 0000000..60268bf --- /dev/null +++ b/blog.php @@ -0,0 +1,519 @@ +<?php + +// blogRight! version 1.7 +// Copyright (C) 2004, 2005 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'; + +if (!isset ($header_file)) + $header_file = "inc/header.html"; + +if (!isset ($footer_file)) + $footer_file = "inc/footer.html"; + +///////////////////////////// + +$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])); + + echo '<?xml version="1.0"?>'."\n"; + echo '<rss version="2.0">'."\n"; + echo " <channel>\n"; + echo " <title>$rssTitle</title>\n"; + echo " <description>$rssDescription</description>\n"; + echo " <link>$site/$weblogdir/</link>\n"; + echo " <copyright>$rssCopyright</copyright>\n"; + echo " <language>$rssLanguage</language>\n"; + echo " <pubDate>$pubDate</pubDate>\n"; + echo " <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n"; + echo " <generator>blogRight!</generator>\n"; + echo " <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); + + echo " <item>\n"; + echo " <title>$itemTitle</title>\n"; + echo " <link>$site/$weblogdir/?q=$permaLink</link>\n"; + echo " <description>\n"; + + indent_include (9, $inc); + + echo " </description>\n"; + echo " <guid isPermaLink=\"true\">$site/$weblogdir/?q=$permaLink</guid>\n"; + echo " <pubDate>$pubDate</pubDate>\n"; + echo " </item>\n"; + } + } + + echo " </channel>\n"; + echo '</rss>'."\n"; +} +else // HTML +{ + echo '<?xml version="1.0" encoding="'.$charsetEncoding.'"?>'."\n"; + @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); + } + + // Calculate a copyright footnote. + $copyrightYear = ''; + $year_array = array_unique ($year_array); + sort ($year_array); + foreach ($year_array as $y) + $copyrightYear .= "$y, "; + $copyrightYear = substr ($copyrightYear, 0, -2); + + @include ($datadir.$footer_file); + + $duration = microtime_diff ($start_time, microtime ()); + $duration = sprintf ("%0.6f", $duration); + echo "\n<!-- processing took $duration seconds -->"; + echo "\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, $gscript, $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)) // a local path + { + $line = preg_replace ('/<img src="(.*?)"(.*?)>/i', + '<img src="'.$site.'/'.$imagedir.'/$1" alt="[image]"$2 />', + $line); + + $line = preg_replace ('/<photo src="(.*?)"(.*?)>/i', + '<div class="photo"><a href="'.$photodir.'/photo.php?q=$1"><img src="' + .$site.'/'.$photodir.'/small/small-$1" alt="[photo]"$2 /></a></div>', + $line); + + $res = array (); + if (preg_match ('/<gallery album="(.*?)" img="(.*?)"(.*?)>/i', $line, $res)) + { + $pdir = dirname (trim ($res[2])); + if ($pdir[0] == '.') + $pdir = ''; + $pbase = basename (trim ($res[2])); + + $line = '<div class="gallery"><a href="'.$photodir.'/'.$gscript.'?q='.$res[1].'">' + .'<img src="'.$site.'/'.$photodir; + if ($pdir) + $line .= '/'.$pdir; + $line .= '/small/small-'.$pbase.'" alt="[gallery]"' + .$res[3]." /><br />»</a></div>\n"; + } + + $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) +{ + echo $string; +} + +///////////////////////////// + +$year_array = array (); // Array of years for copyright purposes + +// HTML output +function include_file ($file) +{ + global $site, $photodir, $imagedir, $search_d; + global $local_fn; + global $year_array; + $res = array (); + $cdd = basename (dirname ($file)); // current data dir + + $local_fn = 0; + if (is_numeric ($cdd)) + { + ereg ('^(.*)-(.*)\.', basename ($file), $res); + $year = $cdd; // assume that... + + $year_array[] = intval ($cdd); + + 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) + { + echo '<div class="entryTitle">'.$entryDate; + + if (!isset ($search_d)) + echo ' (<a href="?q='.$permaLink.'">permalink</a>)'; + + echo '</div>'."\n"; + echo '<div class="entryBody">'."\n"; + + unset ($GLOBALS['collect']); + foreach ($fd as $line) + { + $line = process_line($line); + if ($line) + echo ($line); + } + process_footnotes ('printer', 0); + echo '</div>'."\n\n"; + } +} + +function printer_html ($level, $string) +{ + for ($i = 0; $i < $level; $i++) + echo ' '; + echo (htmlentities ($string) . "\n"); +} + +// RSS 2.0 output +function indent_include ($level, $file) +{ + global $site, $imagedir, $photodir, $weblogdir, $local_fn; + $local_fn = 0; + $cdd = basename (dirname ($file)); // current data dir + $fd = file ($file); + if ($fd) + { + foreach ($fd as $line) + { + unset ($GLOBALS['collect']); + $line = process_line ($line); + if ($line) + { + for ($i = 0; $i < $level; $i++) + echo ' '; + $line = htmlentities2 ($line); + $patterns[0] = "/»/"; $replacements[0] = "&raquo;"; + $patterns[1] = "/ /"; $replacements[1] = "&nbsp;"; + $line = preg_replace ($patterns, $replacements, $line); + echo $line; + } + } + + process_footnotes ('printer_html', $level); + } +} + +function htmlentities2 ($htmlcode) +{ + $table = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); + $table[chr(38)] = '&'; + return preg_replace ("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&", + strtr ($htmlcode, $table)); +} + +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; +} + +?> @@ -6,6 +6,9 @@ $site = 'http://your.site.name'; $weblogdir = 'blogright'; $imagedir = 'blogright/graphics'; $photodir = 'blogright/photos'; +$gscript = ''; + +$charsetEncoding = 'UTF-8'; $suffix = '.html'; // entries' file extension $recent = 'month'; // number of entries shown by default |