blogroll.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /*
  3. Template Name: Blogroll
  4. */
  5. function scratch99_fromthisblog() {
  6. $ref = $_SERVER['HTTP_REFERER'];
  7. $host = $_SERVER['HTTP_HOST'];
  8. if( $ref == "" ) return true;
  9. if( strpos($host, $ref) !== false ) return false;
  10. return false;
  11. }
  12. function scratch99_last_post_thumbnail_link () {
  13. $recent_posts = wp_get_recent_posts(array(
  14. 'numberposts' => 10, // Number of recent posts thumbnails to display
  15. 'post_status' => 'publish' // Show only the published posts
  16. ));
  17. foreach( $recent_posts as $recent ) {
  18. if ( has_post_thumbnail($recent['ID']) ) { // check if the post has a Post Thumbnail assigned to it.
  19. echo "<div style='text-align: right;'><a href='" . get_permalink($recent['ID']) . "'>";
  20. echo "<span style='padding-right: 1em; font-weight: bold;'>New on spring2life</span> ";
  21. echo get_the_post_thumbnail($recent['ID'],'thumbnail', 'style=float:right;') . "</a></div>";
  22. break;
  23. }
  24. }
  25. }
  26. function debug_echo ( $text ) {
  27. if ( true ) {
  28. /* echo 'debug: <![CDATA[ ', str_replace('<', '&lt;', $text), " ]]><br>\n"; */
  29. /* echo 'debug: ', $text, "<br>\n"; */
  30. }
  31. }
  32. function insert_chunk_before( $before, $newest_only, $chunk ) {
  33. return preg_replace("/<\/ul>\n<li class='linkhead'><h4>$before<\/h4>/",
  34. "<li>$chunk</li>\n</ul>\n<li class='linkhead'><h4>$before</h4>", $newest_only);
  35. }
  36. function process_oren_grad($chunk, $newest_only, $omitflag) {
  37. $chunk = preg_replace('#<div class=[\'"]item-content[\'"]>.*</div>#s', '', $chunk);
  38. $chunk = preg_replace('#</div>#s', '', $chunk);
  39. $now = date_timestamp_get(date_create());
  40. if ( false === ( $og = get_transient( 'orengrad' ) ) ) { /* no transient could be restored */
  41. $result = curlDownload('http://orengrad.com/thingsseen/index.html');
  42. $og[1] = md5($result);
  43. $og[0] = $now;
  44. set_transient('orengrad', $og);
  45. } else { /* there is a transient */
  46. if ( $now - $og[0] > 1*3600 ) { /* timestamp older than 12h, so compare again */
  47. $oldhash = $og[1];
  48. $result = curlDownload('http://orengrad.com/thingsseen/index.html');/* nochmal holen */
  49. $og[1] = md5($result);
  50. if ( $og[1] <> $oldhash ) { /* not equal, so save again */
  51. $og[0] = $now;
  52. set_transient('orengrad', $og);/* set transient with new timestamp */
  53. /* } else {
  54. debug_echo('Hashes are equal'); */
  55. }
  56. }
  57. }
  58. $diff_in_seconds = date_timestamp_get(date_create()) - $og[0];
  59. echo "<!-- orengrad diff_in_seconds: $diff_in_seconds -->";
  60. if ( $diff_in_seconds < 24 * 3600 ) {
  61. $newest_only = insert_chunk_before('A day and older', $newest_only, $chunk);
  62. } elseif ( $diff_in_seconds < 30 * 24 * 3600 ) {
  63. $newest_only = insert_chunk_before('Less than a month old', $newest_only, $chunk);
  64. } else {
  65. $newest_only = insert_chunk_before('Older', $newest_only, $chunk);
  66. }
  67. /* delete_transient('orengrad'); */
  68. $omitflag = true;
  69. return array($newest_only, $omitflag);
  70. }
  71. function process_lfi_online($chunk, $newest_only, $omitflag) {
  72. $chunk = preg_replace('#<div class=[\'"]item-content[\'"]>.*</div>#s', '', $chunk);
  73. $chunk = preg_replace('#</div>#s', '', $chunk);
  74. if ( false === ( $result = get_transient( 'lfionlinede' ) ) ) {
  75. $result = curlDownload('https://lfi-online.de/ceemes/en/blog/');
  76. $chunk = str_replace('<div class=[\'"]blog-title[\'"]>', '', $chunk);
  77. if ( preg_match('#<div class="titlebox30 cu-pointer" onclick="window.location = \'(.*?)\'">\s*<h1 class="typo-1">(.*?)</h1>\s*<span class="subline">\s*<h2 class="sl typo-26">.*?</h2>\s*<h3 class="sl typo-27">(.*?)</h3>#s', $result, $matches)) {
  78. $chunk = $chunk . ":&nbsp;<span class=\"item-title\">\n<a href=\"$matches[1]\" target=\"_blank\">\n$matches[2]</a></span>";
  79. set_transient('lfionlinede', $chunk, 12*3600);
  80. }
  81. } else {
  82. $chunk = $result;
  83. }
  84. /* delete_transient('lfionlinede'); */
  85. /* Assumption: LFI comes in the last section of all entries, because it does not show a date to google.
  86. Therefore all headlines are already written to $newestonly and we can insert before */
  87. /* $age_in_days = date_diff(date_create($matches[3]), date_create(date("d.m.Y")))->format('%a'); */
  88. $age_in_days = date_diff(date_create($matches[3]), date_create(date("d.m.Y")));
  89. #echo "<!-- lfi age in days: ", $age_in_days, " -->";
  90. if ($age_in_days < 1) {
  91. $newest_only = insert_chunk_before('A day and older', $newest_only, $chunk);
  92. } elseif ($age_in_days < 30) {
  93. $newest_only = insert_chunk_before('Less than a month old', $newest_only, $chunk);
  94. } else {
  95. $newest_only = insert_chunk_before('Older', $newest_only, $chunk);
  96. }
  97. $omitflag = true;
  98. return array($newest_only, $omitflag);
  99. }
  100. function cleanup_chunk ( $chunk ) {
  101. $chunk = preg_replace("/[\s]*<div class=[\"']item-time[\"']>.*<\/div>/ism", "", $chunk);
  102. $chunk = preg_replace("/<div class=[\"']item-content[\"']>[\r\n]?/ism", "", $chunk);
  103. $chunk = preg_replace("/<div class=[\"']blog-title[\"']>[\r\n]?/ism", "<li>", $chunk);
  104. $chunk = preg_replace("/<\/div>[\r\n]?/ism", ":&nbsp;", $chunk);
  105. $chunk = preg_replace("/<\/span>/ism", "</span></li>", $chunk);
  106. return $chunk;
  107. }
  108. function blogs_i_read() {
  109. $url = "http://spring2life-links.blogspot.de";
  110. if ( false === ( $html = get_transient( $url ) ) ) {
  111. $html = curlDownload($url);
  112. if ( ! $html ) {
  113. echo "<h2>There is a problem with the http request!</h2>";
  114. } else {
  115. if ( ! preg_match('/(<ul id=\'BlogList1_blogs.*?<\/ul>)/is', $html, $matches)) {
  116. echo "Error: could not retrieve valid spring2life-links blogroll<br>", $html;
  117. return;
  118. } else {
  119. set_transient( $url, $html, 3600 );
  120. }
  121. }
  122. }
  123. /* delete_transient($url); */
  124. $html = preg_replace('/.*<div class=["\']widget BlogList["\'] id=["\']BlogList1["\']>/ism', '', $html);
  125. $newest_only = "";
  126. $stundenflag = false;
  127. $tagflag = false;
  128. $wochenflag = false;
  129. $monatflag = false;
  130. $pleistoflag = false;
  131. preg_match_all('/<div class=["\']blog-title["\']>.*<div class=["\']item-time["\']>.*<\\/div>/Us', $html, $matches);
  132. foreach ($matches[0] as $chunk) {
  133. $omitflag = false;
  134. if ( preg_match("/orengrad.com/ism", $chunk) ) {
  135. list ($newest_only, $omitflag) = process_oren_grad($chunk, $newest_only, $omitflag);
  136. }
  137. if ( preg_match("#/lfi-online.de/ceemes/#", $chunk) ) {
  138. list ($newest_only, $omitflag) = process_lfi_online($chunk, $newest_only, $omitflag);
  139. }
  140. if ( preg_match("/vor\s+\d+\s+(Minute|Stunde)/ism", $chunk) )
  141. if (! $stundenflag) {
  142. $chunk = "<li class='linkhead'><h4>Hot from the Sphere</h4></li>\n\n<ul>\n" . $chunk;
  143. $stundenflag = true;
  144. }
  145. if ( preg_match("/vor\s+\d+\s+(Tag)/ism", $chunk) )
  146. if (! $tagflag) {
  147. $chunk = "</ul>\n<li class='linkhead'><h4>A day and older</h4></li>\n<ul>\n" . $chunk;
  148. $tagflag = true;
  149. }
  150. if ( preg_match("/vor\s+\d+\s+Woche/ism", $chunk) )
  151. if (! $wochenflag) {
  152. $chunk = "</ul>\n<li class='linkhead'><h4>Less than a month old</h4></li>\n<ul>\n" . $chunk;
  153. $wochenflag = true;
  154. }
  155. if ( preg_match("/vor\s+\d+\s+(Monat)/ism", $chunk) )
  156. if (! $monatflag) {
  157. $chunk = "</ul><li class='linkhead'><h4>Older</h4></li>\n<ul>\n" . $chunk; $monatflag = true;
  158. }
  159. if ( preg_match("/vor\s+\d+\s+(Jahr)/ism", $chunk) )
  160. if (! $pleistoflag) {
  161. $chunk = "</ul><li class='linkhead'><h4>From the Pleistozaen</h4></li>\n<ul>\n" . $chunk; $pleistoflag = true;
  162. }
  163. $chunk = cleanup_chunk($chunk);
  164. if ( $omitflag == false )
  165. $newest_only .= $chunk;
  166. if ( preg_match('/href=\'(http:\/\/feedproxy.google.com.*?)\'/', $chunk, $match) ) {
  167. $proxyurl = $match[1];
  168. if ( false === ( $cleanurl = get_transient( md5('cleanurlof' . $proxyurl) ) ) ) {
  169. $cleanurl = resolveUrl($proxyurl);
  170. $cleanurl = doHardcore($cleanurl);
  171. set_transient( md5("cleanurlof" . $proxyurl), $cleanurl, 90000 + rand(1,3600) );
  172. } $chunk = str_replace($match[1], $cleanurl, $chunk);
  173. }
  174. }
  175. return $newest_only;
  176. }
  177. function resolveUrl($url) {
  178. try {
  179. $header = doCurlRequest($url); return $header['redirect_url']; } catch (Exception $ex) {
  180. _log("error freeing url $url: " . $ex->getMessage()); return $url;
  181. }
  182. }
  183. function curlDownload($Url){
  184. $ch = curl_init();
  185. curl_setopt($ch, CURLOPT_URL, $Url);
  186. curl_setopt($ch, CURLOPT_REFERER, "https://markus-spring.info");
  187. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0");
  188. curl_setopt($ch, CURLOPT_HEADER, 0);
  189. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  190. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  191. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  192. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  193. $output = curl_exec($ch);
  194. curl_close($ch);
  195. return $output;
  196. }
  197. if(!function_exists('_log')) {
  198. function _log( $message ) {
  199. if( WP_DEBUG === true ) {
  200. if( is_array( $message ) || is_object( $message ) ) {
  201. error_log( print_r( $message, true ) );
  202. } else {
  203. error_log( $message );
  204. }
  205. }
  206. }
  207. }
  208. function doCurlRequest($url) {
  209. $curlSession = curl_init($url);
  210. curl_setopt($curlSession, CURLOPT_NOBODY, 1);
  211. $curlResponse = curl_exec($curlSession);
  212. $header = curl_getinfo($curlSession);
  213. curl_close($curlSession);
  214. return $header;
  215. }
  216. function doHardcore($url) {
  217. try {
  218. $parts = explode("?", $url);
  219. $ret = $parts[0];
  220. _log("do hardcore: [$url] -> [$ret]");
  221. return $ret;
  222. } catch (Exception $ex) {
  223. _log("error in hardcore: " . $ex->getMessage());
  224. return $url;
  225. }
  226. }
  227. ?>
  228. <?php get_header(); ?>
  229. <div id="content" class="site-content">
  230. <div id="primary" class="content-area">
  231. <main id="main" class="site-main" role="main">
  232. <article id="post--31201" class="post--31201 post type-post status-publish format-image hentry category-vernacular post_format-post-format-image entry">
  233. <header class="entry-header">
  234. <h2 class="entry-title default-max-width">Blogs I read</h2>
  235. </header><!-- .entry-header -->
  236. <div id="content" class="site-content">
  237. <div id="primary" class="content-area">
  238. <main id="main" class="site-main" role="main">
  239. <div class="entry-content">
  240. <ul class="linktable">
  241. <?php
  242. if (scratch99_fromthisblog()) {
  243. scratch99_last_post_thumbnail_link();
  244. }
  245. ?>
  246. <?php echo blogs_i_read(); ?>
  247. </ul>
  248. </div><!-- .entry-content -->
  249. </main>
  250. </div>
  251. </div>
  252. </article><!-- #post-31164 -->
  253. </main><!-- #main -->
  254. </div><!-- #primary -->
  255. </div><!-- #content -->
  256. <?php get_footer(); ?>