123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- <?php
- date_default_timezone_set('Europe/Bucharest');
- error_reporting(-1);
- ini_set('display_errors', '1');
- $nest = "";
- $type = "pagini";
- define("HOSTNAME", "ceata.org");
- // Verify file argument.
- if (isset($argv[1]) && $argv[1] == '-a')
- {
- $all = get_all_files("tpl");
- foreach ($all as $file)
- {
- if ($file == "") continue;
- $file = $file.".tpl";
- $type = get_type($file);
- echo ucfirst($type).": ".$file."\n";
- publish($file, $type);
- }
- update_sitemap();
- }
- else if (isset($argv[1]) && $argv[1] == '-h')
- {
- update_sitemap();
- }
- else if (isset($argv[1]))
- {
- // Verify file name argument.
- if (substr($argv[1], -4) != ".tpl")
- {
- echo "Eroare: nu a fost specificat argumentul cu fișierul .tpl. Utilizare: php publish.php <fișier.tpl>\n";
- die();
- }
- // TODO: Filename should not start with '/' (should be relative)
- // TODO: Check if the file exists
- $type = get_type($argv[1]);
- // Publish or update the article.
- publish($argv[1], $type);
- }
- // else if(isset($argv[1]) && $argv[1] == '-all')
- // {
- // // Get all articles templates.
- // $articles = glob("*.tpl");
- // // Publish or update all articles.
- // foreach ($articles as $article)
- // {
- // // Publish or update the article.
- // publish($article);
- // }
- // }
- else
- {
- echo "Eroare: nu a fost specificat argumentul cu fișierul .tpl. Utilizare: php publish.php <fișier.tpl>\n";
- die();
- }
- function get_all_files($ext)
- {
- return explode("\n", shell_exec("find . -path ./templates -prune -o -name \"*.".$ext."\" -print | sed -e 's/\.".$ext."//g' | sort | sed -e \"s/\.\///g\""));
- }
- function get_type($file)
- {
- $type = "pagini";
- $pos = strpos($file, "news");
- if ($pos !== false && $pos == 0)
- $type = "news";
- else
- {
- $pos = strpos($file, "events");
- if ($pos !== false && $pos == 0)
- $type = "events";
- }
- return $type;
- }
- function get_news_brief($file)
- {
- $city = get_text($file, "city");
- $country = get_text($file, "country");
- $date = get_text($file, "date");
- $brief = get_text($file, "brief");
- $rodate = rodate($date);
- $brief = $city.", ".$country." -- ".$rodate[0].", ".$rodate[1]." -- ".$brief;
- return $brief;
- }
- function get_event_brief($file)
- {
- $date = get_text($file, "date");
- $hours = get_text($file, "hours");
- $city = get_text($file, "city");
- $country = get_text($file, "country");
- $room = get_text($file, "room");
- $host = get_text($file, "host");
- $address = get_text($file, "address");
- $brief = get_text($file, "brief");
- $rodate = rodate($date);
- $brief = $city.", ".$country." -- ".$rodate[0].", ".$rodate[1].", orele ".$hours." -- ".$room.", ".$host.", ".lcfirst($address).". ".$brief;
- return $brief;
- }
- function get_brief($file, $type)
- {
- $brief = "";
- if (!strcmp($type, "news"))
- {
- $brief = get_news_brief($file);
- }
- else if (!strcmp($type, "events"))
- {
- $brief = get_event_brief($file);
- }
- return $brief;
- }
- function get_event_details($file)
- {
- $date = get_text($file, "date");
- $rodate = rodate($date);
- $hours = get_text($file, "hours");
- $city = get_text($file, "city");
- $country = get_text($file, "country");
- $room = get_text($file, "room");
- $host = get_text($file, "host");
- $address = get_text($file, "address");
- $brief = get_text($file, "brief");
- $contactname = get_text($file, "contactname");
- $contactmail = tomail(get_text($file, "contactmail"));
- $contactphone = get_text($file, "contactphone");
- $details = "<table class=\"detalii\">";
- $details .= "<tr><th>Data</th><td>".$rodate[1]."</td></tr>";
- $details .= "<tr><th>Orele</th><td>".$hours."</td></tr>";
- $details .= "<tr><th>Orașul</th><td>".$city.", ".$country."</td></tr>";
- $details .= "<tr><th>Sala</th><td>".$room."</td></tr>";
- $details .= "<tr><th>Gazda</th><td>".$host."</td></tr>";
- $details .= "<tr><th>Adresa</th><td>".$address."</td></tr>";
- if ($contactname)
- {
- $details .= "<tr><th>Coordonator</th><td>";
- $details .= "<table class=\"contact\">";
- $details .= "<tr><td>".$contactname."</td></tr>";
- if ($contactmail)
- $details .= "<tr><td>".$contactmail."</td></tr>";
- if ($contactphone)
- $details .= "<tr><td>".$contactphone."</td></tr>";
- $details .= "</table>";
- $details .= "</td></tr>";
- }
- $details .= "<tr><th>Calendar</th><td>vCal, iCal</td></tr>";
- $details .= "</table>";
- return $details;
- }
- function get_press_release($file)
- {
- $press = get_text($file, "pressrelease");
- if ($press)
- $press = "Pentru mai multe informații, vă invităm să citiți <a href=\"/news/".$press."\">comunicatul oficial</a>.";
- return $press;
- }
- function get_content($file, $type)
- {
- $content = get_html($file, "content");
- if (!strcmp($type, "events"))
- {
- $details = get_event_details($file);
- $press = get_press_release($file);
-
- $report = get_html($file, "report");
- if ($report)
- $report = "<h3 id='raport'>Raportul evenimentului</h3>".$report;
- $content = $details.$content.$report.$press;
- }
- return $content;
- }
- function get_press_contact($file, $type)
- {
- $press = "";
- if (!strcmp($type, "news"))
- {
- $press = "<h3>Contact pentru presă</h3>";
- }
- else if (!strcmp($type, "events"))
- {
- $press = "<h3>Coordonatorul evenimentului</h3>";
- }
- $contactname = get_text($file, "contactname");
- $contactfunc = get_text($file, "contactfunc");
- $contactmail = tomail(get_text($file, "contactmail"));
- $contactphone = get_text($file, "contactphone");
- if ($contactname)
- {
- $press .= "<p>";
- $press .= $contactname."<br />";
- if ($contactfunc)
- $press .= $contactfunc.", Fundația Ceata<br />";
- if ($contactmail)
- $press .= $contactmail."<br />";
- if ($contactphone)
- $press .= $contactphone."<br />";
- $press .= "</p>";
- }
- else
- $press = "";
- return $press;
- }
- /**
- * Publish function.
- *
- * Publishes a post by creating a HTML file of the post and update the news page.
- *
- * @param $file
- * File name of the article content.
- */
- function publish($file, $type)
- {
- global $news_templates_path;
- $nested_level = substr_count($file, '/');
- $nested = str_repeat("../", $nested_level);
- // Read header template.
- $article = read_template("templates/header.ro.tpl");
- if (strcmp($type, "pagini"))
- {
- // Read content template.
- $article .= read_template("templates/article.ro.tpl");
- $brief = get_brief($file, $type);
- // Update the published date.
- $article = str_replace("<!-- brief -->", $brief, $article);
- $press_contact = get_press_contact($file, $type);
- // Update the the press contact.
- $article = str_replace("<!-- press -->", $press_contact, $article);
- }
- else
- {
- $article .= read_template("templates/page.ro.tpl");
- }
- // Read the article.
- $article .= read_template("templates/footer.ro.tpl");
- // Read footer template.
- $article_content = read_template($file);
-
- // Get the article data.
- $title = get_text($file, "title");
- $content = get_content($file, $type);
- // Update the the site title.
- $article = str_replace("<!-- site-title -->", $title . " — Fundația Ceata — Ceata eliberează artele și tehnologiile actuale", $article);
- // Update the the title.
- $article = str_replace("<!-- title -->", $title, $article);
- $article = str_replace("<!-- breadcrumbs -->", get_breadcrumbs($file), $article);
- // Update the the content.
- $article = str_replace("<!-- content -->", $content, $article);
- // Update the article style path.
- $article = str_replace("stylesheet\" href=\"", "stylesheet\" href=\"".$nested, $article);
- // Update the article favicon.
- $article = str_replace("shortcut icon\" href=\"", "shortcut icon\" href=\"".$nested, $article);
- // Update the article images path.
- // TODO: fix this if needed
- // $article = str_replace("<img src=\"", "<img src=\"", $article);
- // Get the article file name without extention.
- $filename = substr_replace($file,"",-4);
- if (file_exists($filename.".html"))
- {
- update_sitemap();
- }
- // Create or update the article.
- create_page($filename.".html", $article);
- echo "-- ".$filename.".html - articol publicat/actualizat.\n";
- if (strcmp($type, "pagini"))
- {
- // Generate feed from published articles.
- generate_feed($type);
- // Generate news page.
- generate_news_page($type);
- }
- }
- function get_breadcrumbs($file)
- {
- $trail = "";
- $count = 0;
- $breadcrumbs = "<li><a href='/'>Prima pagină</a></li>";
- $file = strtok($file, ".");
- $lvls = substr_count($file, "/");
- $parent = strtok($file, "/");
- while ($parent)
- {
- $trail .= $parent;
- $title = "";
- if (file_exists($trail.".ro.tpl"))
- $title = get_text($trail.".ro.tpl", "title");
- else if (is_link($trail.".ro.html"))
- {
- $count++;
- $trail .= "/";
- $parent = strtok("/");
- continue;
- }
- else if (file_exists($trail.".ro.html"))
- $title = ucfirst($trail);
- else $title = "Pagină necunoscută";
- if ($count < $lvls)
- $breadcrumbs .= "<li> » <a href='"."/".$trail.".ro.html'>".$title."</a></li>";
- else
- $breadcrumbs .= "<li> » ".$title."</li>";
- $trail .= "/";
- $count++;
- $parent = strtok("/");
- }
- return $breadcrumbs;
- }
- function update_sitemap()
- {
- // Read header template.
- $article = read_template("templates/header.ro.tpl");
- $article .= read_template("templates/page.ro.tpl");
- // Read the footer template.
- $article .= read_template("templates/footer.ro.tpl");
- $title = "Planul sitului";
- // Update the the site title.
- $article = str_replace("<!-- site-title -->", $title . " — Fundația Ceata — Ceata eliberează artele și tehnologiile actuale", $article);
- // Update the the title.
- $article = str_replace("<!-- title -->", $title, $article);
- $all = get_all_files("html");
- $content = "<ul>";
- $last = "";
- $level = 1;
- // TODO: fix sitemap links indentation
- foreach ($all as $file)
- {
- if ($file == "" || is_link($file.".html")) continue;
- /* echo "Părinte: ".get_parent($file).".ro =? ".$last." ???\n"; */
- if ($last != "" && get_parent($file).'.ro' == $last)
- {
- $content .= "<ul>";
- $level++;
- }
- else
- {
- $uplevels = get_uplevels($last, get_common_parent($file, $last));
- for ($i = 0; $i < $uplevels && $level > 1; $i++)
- {
- $content .= "</ul>";
- $level--;
- }
- }
- if ($file == "news.ro" ||
- $file == "events.ro" ||
- $file == "sitemap.ro" ||
- $file == "index.ro"
- )
- {
- $title = str_replace("-", " ", ucfirst($file));
- }
- else
- {
- $article_content = read_template($file.".tpl");
-
- // Get the article data.
- $title = get_text($file.".tpl", "title");
- }
- $content .= "<li><a href=\"/".$file.".html\">".$title."</a></li>";
- $last = $file;
- }
- $content .= "</ul>";
- // Update the the content.
- $article = str_replace("<!-- content -->", $content, $article);
- $sitemap = "sitemap.ro.html";
- // Create or update the article.
- create_page($sitemap, $article);
- echo "-- ".$sitemap." - pagină publicată/actualizată.\n";
- }
- function get_parent($file)
- {
- $parent = "";
- $pos = strrpos($file, "/");
- if ($pos !== false)
- {
- $parent = substr($file, 0, $pos);
- }
- return $parent;
- }
- function get_common_parent($file1, $file2)
- {
- $found = strpos("/".$file2, "/".get_parent($file1));
- while ($found === false)
- {
- $file1 = get_parent($file1);
- $found = strpos("/".$file2, "/".get_parent($file1));
- }
- return get_parent($file1);
- }
- function get_uplevels($file, $path)
- {
- $uplevels = 0;
- $level1 = substr_count($file, '/');
- $level2 = substr_count($path, '/');
- $uplevels = $level1 - $level2;
- if ($path != "" && strpos($file, $path) !== false)
- $uplevels--;
- return $uplevels;
- }
- /*
- * Get the text field function.
- *
- * Get the field from article in the original source. The source is a HTML
- * formatted '.tpl' file.
- *
- * @param $file
- * The original article content file (a '.tpl' file).
- *
- * @param $field
- * The field from the original article content file (a '.tpl' file).
- *
- * @return
- * Returns a string contaning the requested field.
- */
- function get_text($file, $field)
- {
- // Create dom object.
- $doc = new DOMDocument();
-
- // Load oricinal article.
- $doc->loadHTMLFile($file);
- // Get article field.
- $element = $doc->getElementById($field);
- if ($element == NULL)
- return "";
- $text = $element->textContent;
- return $text;
- }
- function get_tag_value($file, $field)
- {
- // Create dom object.
- $doc = new DOMDocument();
-
- // Load oricinal article.
- $doc->loadHTMLFile($file);
- // Get article field.
- $elements = $doc->getElementsByTagName($field);
- $element = $elements->item(0);
- if ($element == NULL)
- return "";
- $text = $element->textContent;
- return $text;
- }
- /*
- * Get the content function.
- *
- * Get the content of a field from the original source. The source is a HTML
- * formatted '.tpl' file.
- *
- * @param $file
- * The original field content file (a '.tpl' file).
- *
- * @param $field
- * The field from the original article file (a '.tpl' file).
- *
- * @return
- * Returns a string contaning the field content.
- */
- function get_html($file, $field)
- {
- // Create dom object.
- $doc = new DOMDocument('1.0', 'UTF-8');
-
- // Load oricinal article.
- $doc->loadHTMLFile($file);
- // Get article content.
- $element = $doc->getElementById($field);
- if ($element == NULL)
- return "";
- $content = get_inner_html($element);
- return $content;
- }
- /**
- * Read template function.
- *
- * Reads the content of a page template.
- *
- * @param $template_file
- * A string, template file name.
- */
- function read_template($template_file)
- {
- // Get template content.
- $template_content = file_get_contents($template_file);
- /* if ($template_content == FALSE) */
- /* { */
- /* echo "N-am găsit ".$template_file."\n"; */
- /* } */
- /* else echo "Am găsit ".$template_file."\n"; */
- return $template_content;
- }
- /**
- * Create page function.
- *
- * Creates or update a HTML page.
- *
- * @param $page_name
- * A string, page file name.
- *
- * @param $content
- * A string, page content.
- */
- function create_page($page_name, $content)
- {
- // Open file for writing.
- $file = fopen($page_name,"w");
- // Write content to file.
- fwrite($file, $content);
- fclose($file);
- }
- /**
- * Truncate text function.
- *
- * Truncates a text.
- *
- * @param $text
- * Text to be truncated.
- * @param $max_text_length
- * The length of truncated text.
- * @param $append
- * String to append to the end of truncated text (such as '...').
- * @param $strip
- * Strip tags or not.
- *
- * @return
- * Truncated text or an empty string.
- */
- function truncate_text($text, $text_length, $append = "", $strip = TRUE)
- {
- // Verify text.
- if ( empty($text) ) return '';
- // Verify wether to strip tags.
- if ($strip)
- {
- // Strip tags.
- $text = strip_tags($text);
- }
- if (strlen($text) > $text_length)
- {
- // Get the the substring of legn $text_length.
- $text = mb_substr($text, 0, $text_length, 'UTF-8');
- // Add $append string.
- $text .= " ".$append;
- }
- return $text;
- }
- /**
- * Generate freed function.
- *
- * Creates or updates the RSS file from the list published articles.
- *
- * @param $limit
- * Number of feeds.
- */
- function generate_feed($type, $limit = 10)
- {
- // Get the list of articles templates.
- $articles = glob($type."/*.tpl");
- // Create articles list.
- foreach ($articles as $article)
- {
- $list[$article] = get_text($article, "date");
- }
-
- // Sort articles by descending date.
- arsort($list);
- // Limit the number of articles.
- $list = array_slice($list, 0, $limit);
- $rss = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
- $rss .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
- xmlns:georss="http://www.georss.org/georss">'."\n";
- $rss .= '<channel>'."\n";
- $rss .= '<title>'.ucfirst($type).' — Fundația Ceata — Ceata eliberează artele și tehnologiile actuale</title>'."\n";
- $rss .= '<link>http://ceata.org/'.$type.'</link>'."\n";
- $rss .= '<description>
- Ceata eliberează artele și tehnologiile actuale</description>'."\n";
- $rss .= '<language>ro</language>'."\n";
- $rss .= '<ttl>480</ttl>'."\n";
- foreach($list as $key => $article)
- {
- $rss .= '<item>'."\n";
- $rss .= '<title><![CDATA['.get_text($key, "title").']]></title>'."\n";
- $rss .= '<description><![CDATA['
- .get_brief($key, $type).']]></description>'."\n";
- $rss .= '<pubDate>'
- .gmdate(DATE_RSS, strtotime(get_text($key, "date"))).'</pubDate>'."\n";
- // Get the article file name without extention.
- $filename = substr_replace($key,"",-4);
- $rss .= '<link>http://'.HOSTNAME.'/'.$filename.'.html</link>'."\n";
- $rss .= '</item>';
- }
- $rss .= '</channel>'."\n";
- $rss .= '</rss>'."\n";
- create_page($type.'.ro.rss', $rss);
- echo "-- ".$type.".ro.rss - flux de RSS creat/actualizat.\n";
- }
- /**
- * Generate news page function.
- *
- * Creates or updates the nwes page.
- *
- */
- function generate_news_page($type)
- {
- // Get the list of articles templates.
- $articles = glob($type."/*.tpl");
- // Create articles list.
- foreach ($articles as $article)
- {
- $list[$article] = get_text($article, "date");
- }
-
- // Sort articles by descending date.
- arsort($list);
- $news_page_content = "";
- foreach($list as $key => $article)
- {
- $article = '<li class="">';
- // Get the article file name without extention.
- $filename = substr_replace($key,"",-4);
- $rodate = rodate(get_text($key, "date"));
- $article .= '<a href="/'.$filename.'.html" class="titlu">'
- .get_text($key, "title").'</a>';
- $article .= '<span class="data">'
- .$rodate[1].'</span>';
- $article .= '<div class="rezumat"><p>'.get_brief($key, $type).'</p></div>';
- $article .= '</li> <!-- .aerisit -->';
- $news_page_content .= $article;
- }
- // Read header template.
- $news_page = read_template("templates/header.ro.tpl");
-
- // Read content template.
- $news_page .= read_template("templates/".$type.".ro.tpl");
- // Read footer template.
- $news_page .= read_template("templates/footer.ro.tpl");
- // Update the the site title.
- $news_page = str_replace("<!-- site-title -->", ucfirst($type)." — Fundația Ceata — Ceata eliberează artele și tehnologiile actuale", $news_page);
- // Update the content.
- $news_page = str_replace("<!-- news list -->", $news_page_content, $news_page);
- $news_page = str_replace("<!-- breadcrumbs -->", get_breadcrumbs($type.".html"), $news_page);
- create_page($type.'.ro.html', $news_page);
- echo "-- ".$type.".ro.html - pagină de știri publicată/actualizată.\n";
- }
- function get_inner_html($node)
- {
- $innerHTML = '';
- $children = $node->childNodes;
- foreach ($children as $child)
- {
- $innerHTML .= $child->ownerDocument->saveXML($child);
- }
-
- return $innerHTML;
- }
- function tomail($address)
- {
- // TODO: what if the address iș empty?
- return "<a href=\"mailto:".$address."\">".$address."</a>";
- }
- function rodate($date)
- {
- $time = strtotime(substr($date, 2));
- $strdate = strftime(('%A %d %B %Y'), $time);
- $rodate = _rodate($strdate);
- return $rodate;
- }
- function _rodate($strdate)
- {
- $day = strtok($strdate, " ");
- switch ($day)
- {
- case "Monday":
- $day = "luni";
- break;
- case "Tuesday":
- $day = "marți";
- break;
- case "Wednesday":
- $day = "miercuri";
- break;
- case "Thursday":
- $day = "joi";
- break;
- case "Friday":
- $day = "vineri";
- break;
- case "Saturday":
- $day = "sâmbătă";
- break;
- case "Sunday":
- $day = "duminică";
- }
- $dayn = ltrim(strtok(" "), "0");
- $month = strtok(" ");
- switch ($month)
- {
- case "January":
- $month = "ianuarie";
- break;
- case "February":
- $month = "februarie";
- break;
- case "March":
- $month = "martie";
- break;
- case "April":
- $month = "aprilie";
- break;
- case "May":
- $month = "mai";
- break;
- case "June":
- $month = "iunie";
- break;
- case "July":
- $month = "iulie";
- break;
- case "August":
- $month = "august";
- break;
- case "September":
- $month = "septembrie";
- break;
- case "October":
- $month = "octombrie";
- break;
- case "November":
- $month = "noiembrie";
- break;
- case "December":
- $month = "decembrie";
- }
- $year = strtok(" ");
- $daydate[0] = $day;
- $daydate[1] = $dayn . " " . $month . " " . $year;
- return $daydate;
- }
- ?>
|