linkpage2.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. Template Name: NEW Blogroll TEST
  4. */
  5. /*
  6. * get html from linkpage blog
  7. * for each blog save url, title, last post url, last post title, timestamp as transient, key is ????
  8. * for leicaphilia and oren grad, get last post title and url with special function
  9. */
  10. $pdo = new PDO(
  11. 'sqlite::memory:',
  12. null,
  13. null,
  14. array(PDO::ATTR_PERSISTENT => false)
  15. );
  16. function scratch99_fromthisblog() {
  17. $ref = $_SERVER['HTTP_REFERER'];
  18. $host = $_SERVER['HTTP_HOST'];
  19. /* echo "<p style='color:green!important;'>Referer: $ref <br>Host: $host</p>"; */
  20. if( $ref == "" ) return true;
  21. if( strpos($host, $ref) !== false ) return false;
  22. return false;
  23. }
  24. function scratch99_last_post_thumbnail_link () {
  25. $recent_posts = wp_get_recent_posts(array(
  26. 'numberposts' => 10, // Number of recent posts thumbnails to display
  27. 'post_status' => 'publish' // Show only the published posts
  28. ));
  29. foreach( $recent_posts as $recent ) {
  30. if ( has_post_thumbnail($recent['ID']) ) { // check if the post has a Post Thumbnail assigned to it.
  31. echo "<div style='text-align: right;'><a href='" . get_permalink($recent['ID']) . "'>";
  32. echo "<span style='padding-right: 1em; font-weight: bold;'>New on spring2life</span> ";
  33. echo get_the_post_thumbnail($recent['ID'],'thumbnail', 'style=float:right;') . "</a></div>";
  34. break;
  35. }
  36. }
  37. }
  38. function blogs_i_read() {
  39. $url = "http://spring2life-links.blogspot.de";
  40. //$html = file_get_contents('/tmp/spring2life-links.html');
  41. $html = curlDownload($url);
  42. set_transient( $url, $html, 1800 );
  43. if ( false === ( $html = get_transient( $url ) ) ) {
  44. $html = curlDownload($url);
  45. if ( ! $html )
  46. echo "<h2>There is a problem with the http request!</h2>";
  47. else
  48. set_transient( $url, $html, 1800 );
  49. }
  50. if ( ! preg_match('/(<ul id=\'BlogList1_blogs.*?<\/ul>)/is', $html, $matches)) {
  51. echo $html;
  52. echo "Error: could not retrieve spring2life-links blogroll";
  53. return;
  54. }
  55. $html = preg_replace('/.*<div class=["\']widget BlogList["\'] id=["\']BlogList1["\']>/ism', '', $html);
  56. $newest_only = "";
  57. $stundenflag = false;
  58. $tagflag = false;
  59. $wochenflag = false;
  60. $monatflag = false;
  61. $pleistoflag = false;
  62. preg_match_all('/<div class=["\']blog-title["\']>.*<div class=["\']item-time["\']>.*<\\/div>/Us', $html, $matches);
  63. foreach ($matches[0] as $chunk) {
  64. /* if ( preg_match("/orengrad.com/ism", $chunk) ) {
  65. $result = curlDownload('http://orengrad.com/thingsseen/index.html');
  66. $result = preg_replace('/</ism', '&lt;', $result);
  67. $result = preg_replace('/>/ism', '&gt;', $result);
  68. echo "<br><tt>Orengrad: $chunk</tt><br><pre>$result</pre><br>";
  69. } */
  70. if ( preg_match("/vor\s+\d+\s+(Minute|Stunde)/ism", $chunk) )
  71. if (! $stundenflag) {
  72. $chunk = "<li class='linkhead'>Hot from the Sphere</li>\n\n" . $chunk; $stundenflag = true;
  73. }
  74. if ( preg_match("/vor\s+\d+\s+(Tag)/ism", $chunk) )
  75. if (! $tagflag) {
  76. $chunk = "<li class='linkhead'>A day and older</li>\n\n" . $chunk; $tagflag = true;
  77. }
  78. if ( preg_match("/vor\s+\d+\s+Woche/ism", $chunk) )
  79. if (! $wochenflag) {
  80. $chunk = "<li class='linkhead'>Less than a month old</li>\n\n" . $chunk; $wochenflag = true;
  81. }
  82. if ( preg_match("/vor\s+\d+\s+(Monat)/ism", $chunk) )
  83. if (! $monatflag) {
  84. $chunk = "<li class='linkhead'>Older</li>\n\n" . $chunk; $monatflag = true;
  85. }
  86. if ( preg_match("/vor\s+\d+\s+(Jahr)/ism", $chunk) )
  87. if (! $pleistoflag) {
  88. $chunk = "<li class='linkhead'>From the Pleistozaen</li>\n\n" . $chunk; $pleistoflag = true;
  89. }
  90. /* if (! preg_match("/<li>/ism", $chunk) )
  91. $chunk = "<li>" . $chunk; */
  92. $chunk = preg_replace("/[\s]*<div class=[\"']item-time[\"']>.*<\/div>/ism", "", $chunk);
  93. $chunk = preg_replace("/<div class=[\"']item-content[\"']>[\r\n]?/ism", "", $chunk);
  94. $chunk = preg_replace("/<div class=[\"']blog-title[\"']>[\r\n]?/ism", "<li>", $chunk);
  95. $chunk = preg_replace("/<\/div>[\r\n]?/ism", ":&nbsp;", $chunk);
  96. $chunk = preg_replace("/<\/span>/ism", "</span></li>", $chunk);
  97. $newest_only .= $chunk;
  98. if ( preg_match('/href=\'(http:\/\/feedproxy.google.com.*?)\'/', $chunk, $match) ) {
  99. $proxyurl = $match[1];
  100. if ( false === ( $cleanurl = get_transient( md5('cleanurlof' . $proxyurl) ) ) ) {
  101. $cleanurl = resolveUrl($proxyurl);
  102. $cleanurl = doHardcore($cleanurl);
  103. set_transient( md5("cleanurlof" . $proxyurl), $cleanurl, 90000 + rand(1,3600) );
  104. } $chunk = str_replace($match[1], $cleanurl, $chunk);
  105. /* print_r($newest_only); die; */
  106. }
  107. /* $chunk = preg_replace( '/<\/li>[\s\r\n]*<\/ul>/ism', '', $chunk ); */
  108. /* $newest_only .= $chunk . "</div></li>\n"; */
  109. }
  110. return $newest_only;
  111. }
  112. function resolveUrl($url) {
  113. try {
  114. $header = doCurlRequest($url); return $header['redirect_url']; } catch (Exception $ex) {
  115. _log("error freeing url $url: " . $ex->getMessage()); return $url;
  116. }
  117. }
  118. function curlDownload($Url){
  119. $ch = curl_init();
  120. curl_setopt($ch, CURLOPT_URL, $Url);
  121. curl_setopt($ch, CURLOPT_REFERER, "http://markus-spring.info");
  122. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0");
  123. curl_setopt($ch, CURLOPT_HEADER, 0);
  124. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  125. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  126. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  127. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  128. $output = curl_exec($ch);
  129. curl_close($ch);
  130. return $output;
  131. }
  132. if(!function_exists('_log')) {
  133. function _log( $message ) {
  134. if( WP_DEBUG === true ) {
  135. if( is_array( $message ) || is_object( $message ) ) {
  136. error_log( print_r( $message, true ) );
  137. } else {
  138. error_log( $message );
  139. }
  140. }
  141. }
  142. }
  143. function doCurlRequest($url) {
  144. $curlSession = curl_init($url);
  145. curl_setopt($curlSession, CURLOPT_NOBODY, 1);
  146. $curlResponse = curl_exec($curlSession);
  147. $header = curl_getinfo($curlSession);
  148. curl_close($curlSession);
  149. return $header;
  150. }
  151. function doHardcore($url) {
  152. try {
  153. $parts = explode("?", $url);
  154. $ret = $parts[0];
  155. _log("do hardcore: [$url] -> [$ret]");
  156. return $ret;
  157. } catch (Exception $ex) {
  158. _log("error in hardcore: " . $ex->getMessage());
  159. return $url;
  160. }
  161. }
  162. ?>
  163. <?php get_header(); ?>
  164. <div id="content">
  165. <div id="inner-content" class="wrap group">
  166. <?php get_sidebar(); ?>
  167. <div id="main" class="col-4-5 group" role="main">
  168. <ul class="blogroll">
  169. <?php
  170. if (scratch99_fromthisblog()) {
  171. /* echo "<p style='color:ping!important;'>Referer: $ref POSITIV</p>"; */
  172. scratch99_last_post_thumbnail_link();
  173. }
  174. ?>
  175. <?php echo blogs_i_read(); ?>
  176. </ul>
  177. </div> <!-- #main -->
  178. </div> <!-- #inner-content -->
  179. </div> <!-- #content -->
  180. <?php get_footer(); ?>