Cannnot open source directory
rss.xml not updated

"; exit(-1); } While ($fnm[] = readdir($dir)); closedir($dir); rsort($fnm); reset($fnm); $htmlfiles[] = array(''); while($a = each($fnm)){ if(eregi("[0-9]+\.html", $a[1]) && !eregi("~", $a[1])){ array_push($htmlfiles, $a[1]); } } array_shift($htmlfiles); if(count($htmlfiles) == 0){ print "

no html files in source directory
rss.xml not updated

"; exit(-1); } // open title file to get basic description and title if(!($fi = file("index_title.html"))){ print "

no title file in source directory
rss.xml not updated

"; exit(-1); } // open RSS.XML file if(!($fo = fopen("rss.xml", 'w'))){ print"

Cannot open rss.xml file
rss.xml not updated

"; exit(-1); } // add header info into target file $header = "\n\n\n"; // prefix of all files $prefix_file = "http://www.tol.oulu.fi/~tkuroda/diary2/"; // write header as write test. After this, fwrite performed with no check if(!(fwrite($fo, $header))){ print"

Cannot write header

"; exit(-1); } // scan inside of title file reset($fi); $start_main = false; $end_main=false; foreach ($fi as $a){ // pick up title and main link if(eregi("", $a)){ $b = ltrim(rtrim($a)); fwrite($fo, "$b\n"); fwrite($fo, "<link>".$prefix_file."index.php</link>\n"); } // pick up description if(!$end_main){ if(!$start_main && eregi("<div class=\"description\">", $a)){ $start_main = true; $message = ""; } elseif($start_main && eregi("</div>", $a)){ $end_main = true; fwrite($fo, " <description>$message</description>\n"); } elseif($start_main){ $message .=htmlspecialchars(ltrim(rtrim($a))); } } } // add items from given data files while($a=each($htmlfiles)){ // open file if(!($fi = file($a[1]))){ print"<p>Cannot open $a[1] file<br />This file skipped</p>"; continue; } // reset all parameters to pick up first paragraph of main part $start_main = false; $start_firstp = false; $end_main = false; // opening mark, id, and link tags (filename mapped to .php) $filename = eregi_replace("html", "php", $a[1]); fwrite($fo, "<item>\n"); fwrite($fo, " <guid>$prefix_file$filename</guid>\n"); fwrite($fo, " <link>$prefix_file$filename</link>\n"); // if no $filename found, create from main_wrapper.php if(!file_exists($filename)){ echo "<p>Creating $filename ... "; if(!($result = copy("main_wrapper.php", $filename))){ echo "Warning, cannot create $filename </p>\n"; } else{ echo " done.</p>\n"; } } // pubdate tag getting from filename $year = substr($a[1],0,4); $month = substr($a[1],4,2); $day = substr($a[1],6,2); $hour = substr($a[1],8,2); $time = mktime($hour, 0, 0, $month, $day, $year); $pubdate = date('D, d M Y H:i:s O', $time); fwrite($fo, " <pubDate>$pubdate</pubDate>\n"); // scan inside file reset($fi); foreach ($fi as $a){ // pick up title if(eregi("<title>", $a)){ $b = ltrim(rtrim($a)); fwrite($fo, " $b\n"); } // pick up image (enclosure && main part) //// it assumes image comes before main part if(eregi("<img class=\"main\"", $a)){ $list = preg_split("/[\s\"]+/",$a); $srcnext=false; foreach($list as $b){ if(eregi("src=", $b)){ $srcnext=true; } elseif($srcnext){ $srcnext=false; $imgfile=$prefix_file . $b; $imgsize=filesize($b); // select one of following, upper is better // 1. Fileinfo extension //// $finfo = finifo_open(FILEINFO_MIME); //// $imgtype = finifo_file($finifo, $b); //// finifo_close($finfo); // 2. mime_content_type is available //// $imgtype=mime_content_type($b); // 3. none of them $list2 = preg_split("/\./", $b); $imgtype ="image/" . $list2[count($list2)-1]; // till here (--; fwrite($fo, " <enclosure url=\"$imgfile\" length=\"$imgsize\" type=\"$imgtype\" />\n"); $imgmessage = "<a href=\"$imgfile\"><img src=\"$imgfile\" style=\"float:left; padding-right:10px; padding_bottom:10px; width:174px; height:131px;\"></a> "; } } } // pick up first paragraph of main story //// it assumes image part comes before main part //// cut line with <img tag //// it copy lines between <p>~</p> after <div class="body"> if(!$end_main){ if(!$start_main && eregi("<div class=\"body\">", $a)){ $start_main = true; $message = $imgmessage; } elseif($start_main && !$start_firstp && eregi("<p>", $a)){ $start_firstp = true; } elseif($start_firstp && eregi("</p>", $a)){ $end_main = true; fwrite($fo, " <description>" . htmlspecialchars($message) . "</description>\n"); } elseif($start_firstp && !eregi("<img", $a)){ $message .=mb_substr(ltrim(rtrim($a)),0,400,'UTF-8'); } } } // closing item fwrite($fo, "</item>\n"); } //close rss file fwrite($fo, "</channel>\n"); fwrite($fo, "</rss>"); // close RSS.XML file fclose($fo); ?>