pierovdfn

APOD Widget [Flatpress]

Aug 3rd, 2011
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Apod Widget
  4. Version: 1.0
  5. Plugin URI: http://www.vdfn.altervista.org/
  6. Description: The APOD widget, like the wordpress one.
  7. Author: Piero VDFN
  8. Author URI: http://www.vdfn.altervista.org/
  9. */
  10.  
  11. /**
  12.  * This is the file of the image URL cache.
  13.  */
  14. define('APOD_CACHE', CACHE_DIR.'apod.tmp');
  15.  
  16. /**
  17.  * This is the thumb file.
  18.  */
  19. define('APOD_THUMB', IMAGES_DIR.'apod_thumb.jpg');
  20.  
  21. /**
  22.  * This is the width of the thumbnail in pixels.
  23.  */
  24. define('APOD_THUMB_WIDTH', 150);
  25.  
  26. /**
  27.  * This is the target attribute of the link of the widget.
  28.  * It it breaks your validation, leave it void.
  29.  */
  30. define('APOD_TARGET', '');
  31.  
  32. /**
  33.  * This is the an advanced function: if filemtime does
  34.  * not work, set it to true.
  35.  */
  36. define('APOD_CLEARSTAT', false);
  37.  
  38. /**
  39.  * This function returns the URL of the Apod Image.
  40.  *
  41.  * @return string: The URL
  42.  */
  43. function apod_geturl() {
  44.     global $apod_image;
  45.  
  46.     if(!empty($apod_image)) {
  47.         return $apod_image;
  48.     }
  49.  
  50.     if(APOD_CLEARSTAT) {
  51.         clearstatcache();
  52.     }
  53.  
  54.     if(@filemtime(APOD_CACHE)>strtotime('today')) {
  55.         $apod_image=io_load_file(APOD_CACHE);
  56.         return $apod_image;
  57.     }
  58.  
  59.     $baseurl='http://apod.nasa.gov/apod/';
  60.     $pageurl=$baseurl.'ap'.date('ymd').'.html';
  61.  
  62.     // Get the page with cURL
  63.     $ch=curl_init();
  64.     curl_setopt($ch, CURLOPT_URL, $pageurl);
  65.     curl_setopt($ch, CURLOPT_HEADER, false);
  66.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  67.     $pagehtml=curl_exec($ch);
  68.     curl_close($ch);
  69.  
  70.     // If we had an error with cURL...
  71.     if($pagehtml===false) {
  72.         return false;
  73.     }
  74.  
  75.     // Get the image URL
  76.     $searchfor='<IMG SRC="';
  77.     $src_beg=strpos($pagehtml, $searchfor);
  78.  
  79.     if($src_beg===FALSE) {
  80.         return false;
  81.     }
  82.  
  83.     $src_beg+=strlen($searchfor);
  84.     $src_end=strpos($pagehtml, '"', $src_beg);
  85.  
  86.     $url=$baseurl.substr($pagehtml, $src_beg, $src_end-$src_beg);
  87.     $apod_image=$url;
  88.     io_write_file(APOD_CACHE, $url);
  89.  
  90.     return $url;
  91. }
  92.  
  93. /**
  94.  * This function creates the thumbnail of the Apod Image.
  95.  *
  96.  * @return boolean: Is the thumb ok?
  97.  */
  98. function apod_thumb() {
  99.     if(APOD_CLEARSTAT) {
  100.         clearstatcache();
  101.     }
  102.  
  103.     if(@filemtime(APOD_THUMB)>strtotime('today')) {
  104.         return true;
  105.     }
  106.  
  107.     if(file_exists(APOD_THUMB)) {
  108.         @unlink(APOD_THUMB);
  109.     }
  110.  
  111.     // Get the image URL
  112.     @$image_url=apod_geturl();
  113.  
  114.     if($image_url===false) {
  115.         return false;
  116.     }
  117.  
  118.     // Download the image with cURL
  119.     $ch=curl_init();
  120.     curl_setopt($ch, CURLOPT_URL, $image_url);
  121.     curl_setopt($ch, CURLOPT_HEADER, false);
  122.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  123.     $image=curl_exec($ch);
  124.     curl_close($ch);
  125.  
  126.     if($image===false) {
  127.         return false;
  128.     }
  129.  
  130.     // Save a temporary copy of the image to get its width
  131.     $tmpfile=CACHE_DIR.'apod-tmp-'.mt_rand();
  132.     if(!io_write_file($tmpfile, $image)) {
  133.         $image='';
  134.         unset($image);
  135.         return false;
  136.     }
  137.     list($width, $height)=getimagesize($tmpfile);
  138.     unlink($tmpfile);
  139.     unset($tmpfile);
  140.  
  141.     // Save the new dimensions...
  142.     $newwidth=APOD_THUMB_WIDTH;
  143.     $newheight=$newwidth*$height/$width;
  144.  
  145.     // Make the thumb
  146.     // @link http://it.php.net/manual/en/function.imagecopyresized.php
  147.     $thumb=imagecreatetruecolor($newwidth, $newheight);
  148.     $source=imagecreatefromstring($image);
  149.     unset($image); // Free memory
  150.  
  151.     if($source===false) {
  152.         return false;
  153.     }
  154.  
  155.     imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  156.     $result=imagejpeg($thumb, APOD_THUMB);
  157.     imagedestroy($thumb);
  158.     imagedestroy($source);
  159.  
  160.     return $result;
  161. }
  162.  
  163. /**
  164.  * This function is the handler for the APOD Widget.
  165.  *
  166.  * @return array: The title and the content of the widget
  167.  */
  168. function apod_widget() {
  169.     $fullurl=apod_geturl();
  170.     $page='http://apod.nasa.gov/apod/ap'.date('ymd').'.html';
  171.  
  172.     if(apod_thumb()) {
  173.         $image=BLOG_BASEURL.APOD_THUMB;
  174.     } else {
  175.         $image=$fullurl.'" style="width: 150px;" ';
  176.     }
  177.  
  178.     if(APOD_TARGET!='') {
  179.         $target=' target="'.APOD_TARGET.'"';
  180.     }
  181.  
  182.     $content="<p>\n<a href=\"{$page}\"{$target}>\n"."<img src=\"{$image}\" ".
  183.         "alt=\"Astronomy Picture of the Day\" title=\"Astronomy Picture of the Day\" />\n</a>\n</p>";
  184.  
  185.     return array(
  186.         'subject'=>'APOD',
  187.         'content'=>$content
  188.     );
  189. }
  190. register_widget('apod', 'APOD', 'apod_widget');
Advertisement
Add Comment
Please, Sign In to add comment