-
/*
-
Plugin Name: APOD Widget
-
Description: Adds a sidebar widget to display the Astronomy Picture of the Day (APOD)
-
Author: Paul Lamb
-
Version: 1.3
-
Author URI: http://www.digitalmeandering.com/
-
Notes: The framework for this widget was politely borrowed, with no intent to give back, from the Google Search widget
-
Version 1.3 - Modified by Erich Beyrent [http://www.beyrent.net]
-
uses a CURL method for retrieving the APOD images, when URL file access is disabled
-
*/
-
function widget_apodwidget_init() {
-
-
// Check for the required plugin functions. This will prevent fatal
-
// errors occurring when you deactivate the dynamic-sidebar plugin.
-
-
return;
-
-
function widget_apodwidget_returnimageandlink()
-
{
-
$apodAvailable = true;
-
// Grab our date-time stamps
-
-
-
// Set the image width to 150 pixels
-
$ImageWidthConst = 150;
-
// Set the base URL for the APOD site, thanks NASA!
-
$URL = 'http://antwrp.gsfc.nasa.gov/apod/';
-
// Create our HTML file name, following this format:
-
// 'ap060925.html' where today's date is 09/25/2006
-
$Filename = 'ap'.$TodayDt.'.html';
-
// Append the file to the URL to create the full URL to today's APOD
-
$FullURL = $URL.$Filename;
-
-
// Create the regular expressions for both the regular and large image, even though at the moment I'm not even using the large image
-
$RegExStringSmall = 'src="image/'.$MonthDay.'/(.*)" mce_src="image/'.$MonthDay.'/(.*)" ';
-
$RegExStringBig = 'href="image/'.$MonthDay.'/(.*)" mce_href="image/'.$MonthDay.'/(.*)" ';
-
-
// Regular expression to validate that we have a good image to present
-
$RegExImageExtension= '.*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg])';
-
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $FullURL);
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
-
$html = curl_exec ($ch);
-
curl_close ($ch);
-
-
if($html == '')
-
{
-
$apodAvailable = false;
-
}
-
-
if (eregi ($RegExStringBig,
$html,
$big))
-
{
-
$ImageSrcBig = $big[1];
-
}
-
-
// Check for the regular image
-
if (eregi ($RegExStringSmall,
$html,
$small))
-
{
-
$ImageSrcSmall = $small[1];
-
}
-
-
// If the APOD is available, use CURL to fetch the image and write it to a temp file
-
if ($apodAvailable)
-
{
-
// Create the full image URL based on what the regular expression found
-
$ImageUrlSmall = $URL.'image/'.$MonthDay.'/'.$ImageSrcSmall;
-
-
-
// Create the full big image URL based on what the regular expression found
-
$ImageUrlBig = $URL.'image/'.$MonthDay.'/'.$ImageSrcBig;
-
-
-
preg_match("/.*\.jpg/",
$ImageUrlSmall,
$matches);
-
$ImageUrlSmall = $matches[0];
-
-
-
$ImageUrlBig = $matches[0];
-
-
// Check to make sure the image is a compatible format
-
if(eregi($RegExImageExtension,
$ImageUrlSmall))
-
{
-
$ImageDimensionsBig =
array();
-
-
// Fetch the image
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $ImageUrlSmall);
-
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
-
$image = curl_exec ($ch);
-
curl_close ($ch);
-
-
// We need to get around the security restrictions, so we create a temp file to write the image to
-
-
$handle = @
fopen('/tmp/'.
$filename,
"w+");
-
if ($handle)
-
{
-
-
-
{
-
-
-
// Get the dimensions of the image for resizing
-
-
-
//Delete the file
-
-
}
-
}
-
-
// We want a proportional image, so create our resize percentage based on the width
-
$ImageResizePercentage = ($ImageDimensionsBig[0] / $ImageWidthConst);
-
// Set the image width to our constant
-
$ImageWidthSmall = ($ImageWidthConst);
-
// Set the image height using the resize percentage, again porpotions are the key
-
$ImageHeightSmall = @($ImageDimensionsBig[1] / $ImageResizePercentage);
-
-
// Create the hyperlink to the APOD site, wrapped around the image itself, setting the target to a new window and passing in the image height and width
-
$ImageAndLink = '<a href="'.$FullURL.'" target="_blank"><img src="'.$ImageUrlSmall.'" width="'.$ImageWidthSmall.'" height="'.$ImageHeightSmall.'"/></a>';
-
-
return $ImageAndLink;
-
}
-
else {
-
$apodAvailable = false;
-
}
-
}
-
// The APOD wasn't available, return a message indicating as such
-
if (!$apodAvailable) {
-
return 'APOD not available
-
';
-
}
-
}
-
-
function widget_apodwidget($args) {
-
-
-
// Each widget can store its own options. We keep strings here.
-
$options = get_option('widget_apodwidget');
-
$title = $options['title'];
-
-
-
echo $before_title .
$title .
$after_title;
-
$ApodImageAndLink = widget_apodwidget_returnimageandlink();
-
-
<div style="margin-top: 5px; text-align: left">'.$ApodImageAndLink.'</div>
-
';
-
-
}
-
-
// This function creates the widget control, using the built in widget abilities for controling widgets (they only get the change the title, it's no big deal)
-
function widget_apodwidget_control() {
-
-
// Get our options and see if we're handling a form submission.
-
$options = get_option('widget_apodwidget');
-
-
$options =
array('title'=>__
('APOD',
'widgets'));
-
if ( $_POST['apodwidget-submit'] ) {
-
-
// Remember to sanitize and format use input appropriately.
-
-
update_option('widget_apodwidget', $options);
-
}
-
-
// Be sure you format your options to be valid HTML attributes.
-
-
-
// Here is our little form segment. Notice that we don't need a
-
// complete form. This will be embedded into the existing form.
-
-
<p style="text-align: right"><label for="apodwidget-title">' . __('Title:') . ' <input type="text" value="'.$title.'" name="apodwidget-title" id="apodwidget-title" style="width: 200px" /></label></p>
-
';
-
echo '<input type="hidden" value="1" name="apodwidget-submit" id="apodwidget-submit" />';
-
}
-
-
// This registers our optional widget control form. Because of this
-
// our widget will have a button that reveals a 200x100 pixel form.
-
register_widget_control
(array('APOD',
'widgets'),
'widget_apodwidget_control',
200,
100);
-
-
// This registers our widget so it appears with the other available
-
// widgets and can be dragged and dropped into any active sidebars.
-
register_sidebar_widget
(array('APOD',
'widgets'),
'widget_apodwidget');
-
}
-
-
// Run our code later in case this loads prior to any required plugins.
-
add_action('widgets_init', 'widget_apodwidget_init');
-
?>