generate-firstpage.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. define("HOSTNAME", "ceata.org");
  3. define("JOURNAL", "blog");
  4. define("EVENTS", "events");
  5. define("EVENTS2", "events2");
  6. define("NEWS", "news");
  7. $events2 = FALSE;
  8. error_reporting(-1);
  9. ini_set('display_errors', '1');
  10. // Define the path to the site root directory.
  11. $site_path = "./";
  12. require_once 'read-feed.php';
  13. // Update home page.
  14. update_home_page();
  15. function update_home_page()
  16. {
  17. global $site_path;
  18. // Read header template content.
  19. $template_content = read_template($site_path."templates/header.ro.tpl");
  20. // Read templates content.
  21. $template_content .= read_template($site_path."templates/index.ro.tpl");
  22. // Read the footer template content.
  23. $template_content .= read_template($site_path."templates/footer.ro.tpl");
  24. // Update the the site title.
  25. $template_content = str_replace("<!-- site-title -->", "Fundația Ceata &mdash; Ceata eliberează artele și tehnologiile actuale", $template_content);
  26. // Get Ceata events block content.
  27. $block_content = feed_list("events.ro.rss", 5, EVENTS, TRUE);
  28. // Verify if block content variable is not empty.
  29. if (!empty($block_content))
  30. {
  31. $events2 = FALSE;
  32. $pre = '<div id="events">';
  33. $pre .= '<a href="/events.ro.rss" class="flux"><img src="/files/images/feed.png" /></a>';
  34. $pre .= '<h2 class="block-title"><a href="/events.ro.html">Evenimente</a></h2>';
  35. $post = '<p class="toate">';
  36. $post .= '<a href="/events.ro.html">Vezi și evenimentele trecute &gt;&gt;</a>';
  37. $post .= '</p></div>';
  38. $block_content = $pre.$block_content.$post;
  39. // Update journals block content.
  40. $template_content = str_replace(
  41. "<!-- Evenimente -->", $block_content, $template_content
  42. );
  43. }
  44. else
  45. $events2 = TRUE;
  46. // Get Ceata journals block content.
  47. $block_content = feed_list("blog.rss", 5, JOURNAL);
  48. // Verify if block content variable is not empty.
  49. if (!empty($block_content))
  50. {
  51. // Update journals block content.
  52. $template_content = str_replace(
  53. "<!-- Jurnal -->", $block_content, $template_content
  54. );
  55. }
  56. if ($events2)
  57. {
  58. // Get Ceata events block content.
  59. $block_content = feed_list("events.ro.rss", 5, EVENTS2);
  60. // Verify if block content variable is not empty.
  61. if (!empty($block_content))
  62. {
  63. $pre = '<div id="events">';
  64. $pre .= '<a href="/events.ro.rss" class="flux"><img src="/files/images/feed.png" /></a>';
  65. $pre .= '<h2 class="block-title"><a href="/events.ro.html">Evenimente</a></h2>';
  66. $post = '<p class="toate">';
  67. $post .= '<a href="/events.ro.html">Vezi toate evenimentele &gt;&gt;</a>';
  68. $post .= '</p></div>';
  69. $block_content = $pre.$block_content.$post;
  70. // Update journals block content.
  71. $template_content = str_replace(
  72. "<!-- Evenimente2 -->", $block_content, $template_content
  73. );
  74. }
  75. }
  76. // Get Ceata news block content.
  77. $block_content = feed_list("news.ro.rss", 5);
  78. // Verify if block content variable is not empty.
  79. if (!empty($block_content))
  80. {
  81. // Update journals block content.
  82. $template_content = str_replace(
  83. "<!-- News -->", $block_content, $template_content
  84. );
  85. }
  86. // Create or update the home page.
  87. create_page($site_path."index.ro.html", $template_content);
  88. }
  89. function feed_list($feed_url, $number, $type=NEWS, $reverse=FALSE)
  90. {
  91. $feed_list = '';
  92. // Get Ceata journals feeds.
  93. $feeds = get_feed($feed_url);
  94. $feedset = array();
  95. foreach($feeds as $key => $feed)
  96. {
  97. if ($key > $number-1) break;
  98. if ($type == EVENTS && (strtotime($feed['date']) < (strtotime("now") - 24*3600)))
  99. break;
  100. array_push($feedset, $feed);
  101. }
  102. if ($reverse)
  103. $feedset = array_reverse($feedset, TRUE);
  104. if (!empty($feedset))
  105. {
  106. foreach($feedset as $key => $feed)
  107. {
  108. $description = $feed['description'];
  109. $append = '<a href="'.$feed['link'].'" target="_blank">[...]</a>';
  110. if ($type == JOURNAL)
  111. $description = truncate_text($feed['description'], 255, $append);
  112. $feed_list .= '<li>';
  113. if ($type == JOURNAL)
  114. $feed_list .= '<a href="'.$feed['link'].'" target="_blank" class="titlu">';
  115. else
  116. {
  117. $link = str_replace("http://".HOSTNAME."/", "", $feed['link']);
  118. $feed_list .= '<a href="/'.$link.'" class="titlu">';
  119. }
  120. $title = $feed['title'];
  121. if ($type == JOURNAL)
  122. {
  123. $pos = strpos($feed['title'], ":");
  124. $author = substr($feed['title'], 0, $pos);
  125. $title = substr($feed['title'], $pos+1);
  126. }
  127. $feed_list .= $title.'</a>';
  128. if ($type == JOURNAL)
  129. $feed_list .= '<br /><i>de '.$author.'</i>';
  130. // $feed_list .= '<span class="data">'.$feed['date'].'</span>';
  131. $feed_list .= '<div class="rezumat"><p>'.$description.'</p></div></li>';
  132. }
  133. if ($type == EVENTS && empty($feed_list))
  134. return $feed_list;
  135. $feed_list = '<ul class="listă-simplă">'.$feed_list.'</ul>';
  136. }
  137. return $feed_list;
  138. }
  139. /**
  140. * Read template function.
  141. *
  142. * Reads the content of a page template.
  143. *
  144. * @param $template_file
  145. * A string, template file name.
  146. */
  147. function read_template($template_file)
  148. {
  149. // Get template content.
  150. $template_content = file_get_contents($template_file);
  151. return $template_content;
  152. }
  153. /**
  154. * Create page function.
  155. *
  156. * Creates or update a HTML page.
  157. *
  158. * @param $page_name
  159. * A string, page file name.
  160. *
  161. * @param $content
  162. * A string, page content.
  163. */
  164. function create_page($page_name, $content)
  165. {
  166. // Open file for writing.
  167. $file = fopen($page_name,"w");
  168. // Write content to file.
  169. fwrite($file, $content);
  170. fclose($file);
  171. }
  172. /**
  173. * Truncate text function.
  174. *
  175. * Truncates a text.
  176. *
  177. * @param $text
  178. * Text to be truncated.
  179. * @param $max_text_length
  180. * The length of truncated text.
  181. * @param $append
  182. * String to append to the end of truncated text (such as '...').
  183. * @param $strip
  184. * Strip tags or not.
  185. *
  186. * @return
  187. * Truncated text or an empty string.
  188. */
  189. function truncate_text($text, $text_length, $append = "", $strip = TRUE)
  190. {
  191. // Verify text.
  192. if ( empty($text) ) return '';
  193. // Verify wether to strip tags.
  194. if ($strip)
  195. {
  196. // Strip tags.
  197. $text = strip_tags($text);
  198. }
  199. if (strlen($text) > $text_length)
  200. {
  201. $text_length += strpos(substr($text, $text_length), ".");
  202. $text = utf8_encode($text);
  203. // Get the the substring of legn $text_length.
  204. $text = mb_substr($text, 0, $text_length, "UTF-8");
  205. $text = utf8_decode($text).'.';
  206. // Add $append string.
  207. $text .= " ".$append;
  208. }
  209. return $text;
  210. }
  211. ?>