blogroll_lfi.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /*
  3. Template Name: Blogroll Test
  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 ( false ) { */
  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. $matches = [];
  75. if ( false === ( $result = get_transient( 'lfionlinede' ) ) ) {
  76. $result = curlDownload('https://lfi-online.de/ceemes/en/blog/');
  77. $chunk = str_replace('<div class=[\'"]blog-title[\'"]>', '', $chunk);
  78. 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)) {
  79. $chunk = $chunk . ":&nbsp;<span class=\"item-title\">\n<a href=\"$matches[1]\" target=\"_blank\">\n$matches[2]</a></span>";
  80. set_transient('lfionlinede', $chunk, 12*3600);
  81. }
  82. } else {
  83. $chunk = $result;
  84. }
  85. /* delete_transient('lfionlinede'); */
  86. /* Assumption: LFI comes in the last section of all entries, because it does not show a date to google.
  87. Therefore all headlines are already written to $newestonly and we can insert before */
  88. /* $age_in_days = date_diff(date_create($matches[3]), date_create(date("d.m.Y")))->format('%a'); */
  89. $age_in_days = date_diff(date_create($matches[3]), date_create(date("d.m.Y")));
  90. debug_echo($matches[0]);
  91. /* debug_echo(date_create($matches[3])); */
  92. /* debug_echo(date_create(date("d.m.Y"))); */
  93. #echo "<!-- lfi age in days: ", $age_in_days, " -->";
  94. if ($age_in_days < 1) {
  95. $newest_only = insert_chunk_before('A day and older', $newest_only, $chunk);
  96. } elseif ($age_in_days < 30) {
  97. $newest_only = insert_chunk_before('Less than a month old', $newest_only, $chunk);
  98. } else {
  99. $newest_only = insert_chunk_before('Older', $newest_only, $chunk);
  100. }
  101. $omitflag = true;
  102. return array($newest_only, $omitflag);
  103. }
  104. function cleanup_chunk ( $chunk ) {
  105. $chunk = preg_replace("/[\s]*<div class=[\"']item-time[\"']>.*<\/div>/ism", "", $chunk);
  106. $chunk = preg_replace("/<div class=[\"']item-content[\"']>[\r\n]?/ism", "", $chunk);
  107. $chunk = preg_replace("/<div class=[\"']blog-title[\"']>[\r\n]?/ism", "<li>", $chunk);
  108. $chunk = preg_replace("/<\/div>[\r\n]?/ism", ":&nbsp;", $chunk);
  109. $chunk = preg_replace("/<\/span>/ism", "</span></li>", $chunk);
  110. return $chunk;
  111. }
  112. function blogs_i_read() {
  113. $url = "http://spring2life-links.blogspot.de";
  114. if ( false === ( $html = get_transient( $url ) ) ) {
  115. $html = curlDownload($url);
  116. if ( ! $html ) {
  117. echo "<h2>There is a problem with the http request!</h2>";
  118. } else {
  119. if ( ! preg_match('/(<ul id=\'BlogList1_blogs.*?<\/ul>)/is', $html, $matches)) {
  120. echo "Error: could not retrieve valid spring2life-links blogroll<br>", $html;
  121. return;
  122. } else {
  123. set_transient( $url, $html, 3600 );
  124. }
  125. }
  126. }
  127. /* delete_transient($url); */
  128. $html = preg_replace('/.*<div class=["\']widget BlogList["\'] id=["\']BlogList1["\']>/ism', '', $html);
  129. $newest_only = "";
  130. $stundenflag = false;
  131. $tagflag = false;
  132. $wochenflag = false;
  133. $monatflag = false;
  134. $pleistoflag = false;
  135. preg_match_all('/<div class=["\']blog-title["\']>.*<div class=["\']item-time["\']>.*<\\/div>/Us', $html, $matches);
  136. foreach ($matches[0] as $chunk) {
  137. $omitflag = false;
  138. if ( preg_match("/orengrad.com/ism", $chunk) ) {
  139. list ($newest_only, $omitflag) = process_oren_grad($chunk, $newest_only, $omitflag);
  140. }
  141. if ( preg_match("#/lfi-online.de/ceemes/#", $chunk) ) {
  142. list ($newest_only, $omitflag) = process_lfi_online($chunk, $newest_only, $omitflag);
  143. }
  144. if ( preg_match("/vor\s+\d+\s+(Minute|Stunde)/ism", $chunk) )
  145. if (! $stundenflag) {
  146. $chunk = "<li class='linkhead'><h4>Hot from the Sphere</h4></li>\n\n<ul>\n" . $chunk;
  147. $stundenflag = true;
  148. }
  149. if ( preg_match("/vor\s+\d+\s+(Tag)/ism", $chunk) )
  150. if (! $tagflag) {
  151. $chunk = "</ul>\n<li class='linkhead'><h4>A day and older</h4></li>\n<ul>\n" . $chunk;
  152. $tagflag = true;
  153. }
  154. if ( preg_match("/vor\s+\d+\s+Woche/ism", $chunk) )
  155. if (! $wochenflag) {
  156. $chunk = "</ul>\n<li class='linkhead'><h4>Less than a month old</h4></li>\n<ul>\n" . $chunk;
  157. $wochenflag = true;
  158. }
  159. if ( preg_match("/vor\s+\d+\s+(Monat)/ism", $chunk) )
  160. if (! $monatflag) {
  161. $chunk = "</ul><li class='linkhead'><h4>Older</h4></li>\n<ul>\n" . $chunk; $monatflag = true;
  162. }
  163. if ( preg_match("/vor\s+\d+\s+(Jahr)/ism", $chunk) )
  164. if (! $pleistoflag) {
  165. $chunk = "</ul><li class='linkhead'><h4>From the Pleistozaen</h4></li>\n<ul>\n" . $chunk; $pleistoflag = true;
  166. }
  167. $chunk = cleanup_chunk($chunk);
  168. if ( $omitflag == false )
  169. $newest_only .= $chunk;
  170. if ( preg_match('/href=\'(http:\/\/feedproxy.google.com.*?)\'/', $chunk, $match) ) {
  171. $proxyurl = $match[1];
  172. if ( false === ( $cleanurl = get_transient( md5('cleanurlof' . $proxyurl) ) ) ) {
  173. $cleanurl = resolveUrl($proxyurl);
  174. $cleanurl = doHardcore($cleanurl);
  175. set_transient( md5("cleanurlof" . $proxyurl), $cleanurl, 90000 + rand(1,3600) );
  176. } $chunk = str_replace($match[1], $cleanurl, $chunk);
  177. }
  178. }
  179. return $newest_only;
  180. }
  181. function resolveUrl($url) {
  182. try {
  183. $header = doCurlRequest($url); return $header['redirect_url']; } catch (Exception $ex) {
  184. _log("error freeing url $url: " . $ex->getMessage()); return $url;
  185. }
  186. }
  187. function curlDownload($Url){
  188. $ch = curl_init();
  189. curl_setopt($ch, CURLOPT_URL, $Url);
  190. curl_setopt($ch, CURLOPT_REFERER, "https://markus-spring.info");
  191. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0");
  192. curl_setopt($ch, CURLOPT_HEADER, 0);
  193. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  194. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  195. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  196. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  197. $output = curl_exec($ch);
  198. curl_close($ch);
  199. return $output;
  200. }
  201. if(!function_exists('_log')) {
  202. function _log( $message ) {
  203. if( WP_DEBUG === true ) {
  204. if( is_array( $message ) || is_object( $message ) ) {
  205. error_log( print_r( $message, true ) );
  206. } else {
  207. error_log( $message );
  208. }
  209. }
  210. }
  211. }
  212. function doCurlRequest($url) {
  213. $curlSession = curl_init($url);
  214. curl_setopt($curlSession, CURLOPT_NOBODY, 1);
  215. $curlResponse = curl_exec($curlSession);
  216. $header = curl_getinfo($curlSession);
  217. curl_close($curlSession);
  218. return $header;
  219. }
  220. function doHardcore($url) {
  221. try {
  222. $parts = explode("?", $url);
  223. $ret = $parts[0];
  224. _log("do hardcore: [$url] -> [$ret]");
  225. return $ret;
  226. } catch (Exception $ex) {
  227. _log("error in hardcore: " . $ex->getMessage());
  228. return $url;
  229. }
  230. }
  231. ?>
  232. <?php get_header(); ?>
  233. <div id="content" class="site-content">
  234. <div id="primary" class="content-area">
  235. <main id="main" class="site-main" role="main">
  236. <article id="post--31201" class="post--31201 post type-post status-publish format-image hentry category-vernacular post_format-post-format-image entry">
  237. <header class="entry-header">
  238. <h2 class="entry-title default-max-width">Blogs I read</h2>
  239. </header><!-- .entry-header -->
  240. <div id="content" class="site-content">
  241. <div id="primary" class="content-area">
  242. <main id="main" class="site-main" role="main">
  243. <div class="entry-content">
  244. <ul class="linktable">
  245. <?php
  246. if (scratch99_fromthisblog()) {
  247. scratch99_last_post_thumbnail_link();
  248. }
  249. ?>
  250. <?php echo blogs_i_read(); ?>
  251. </ul>
  252. </div><!-- .entry-content -->
  253. </main>
  254. </div>
  255. </div>
  256. </article><!-- #post-31164 -->
  257. </main><!-- #main -->
  258. </div><!-- #primary -->
  259. </div><!-- #content -->
  260. <?php get_footer(); ?>