Skip to content

Commit

Permalink
Merge pull request #4 from uriweb/release-1.1
Browse files Browse the repository at this point in the history
Release 1.1
  • Loading branch information
alexandragauss authored Mar 10, 2023
2 parents 81d5f75 + 2e2e781 commit 0d6657e
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 35 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ Set custom CSS class(s) (default: none)
**`alt`** (string)
The alternative text description of the image from the camera

**`link`** (boolean)
If set to true, the image element will be a link to the image (for lightbox or what not)

## Plugin Details

Contributors: Brandon Fuller, John Pennypacker
Tags: widgets
Requires at least: 4.0
Tested up to: 4.9
Stable tag: 1.0
Stable tag: 1.1
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "uriweb/uri-cams",
"type": "wordpress-plugin",
"description": "URI Cams is a Wordpress plugin that displays still images from a webcam.",
"authors": [
{
"name": "URI Web Communications",
"email": "web-group@uri.edu",
"homepage": "https://github.com/uriweb"
}
],
"homepage": "https://www.uri.edu",
"license": "GPL-3.0-only",
"require": {
"php": ">=7.4"
}
}
24 changes: 24 additions & 0 deletions templates/uri-cams-shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$classes = 'uri-cams';
$classes .= ( ! empty( $class ) ) ? ' ' . $class : '';

$timestamp = uri_cams_format_date( $time );
$alt .= ' (retrieved ' . $timestamp . ')';

?>

<figure class="<?php echo $classes; ?>">

<?php if ( false !== $link ): ?>
<a href="<?php echo $path . '?t=' . $time; ?>">
<?php endif; ?>

<img src="<?php echo $path . '?t=' . $time; ?>" alt="<?php echo $alt; ?>" title="<?php echo $alt; ?>" />

<?php if ( false !== $link ): ?>
</a>
<?php endif; ?>

</figure>

212 changes: 178 additions & 34 deletions uri-cams.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: URI Cams
Plugin URI: https://www.uri.edu
Description: Webcam picture importer
Version: 1.0.1
Version: 1.1.0
Author: URI Web Communications
Author URI:
@author: John Pennypacker <jpennypacker@uri.edu>
Expand All @@ -28,27 +28,30 @@ function uri_cams_styles() {


/**
* Shortcode callback
* IP camera shortcode callback
*/
function uri_cams_shortcode($attributes, $content, $shortcode) {

// Attributes
// get the shortcode attributes and add defaults
extract( shortcode_atts(
array(
'ip' => '131.128.104.45',
'username' => 'Viewer',
'password' => 'bay campus',
'alt' => '',
'class' => ''
'class' => '',
'link' => false
), $attributes )
);


// load the cached image

$transient_name = 'uri_cams_' . $ip;

if ( false === ( $photo = get_site_transient( $transient_name ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$photo = uri_cams_get_image($ip, $username, $password);
$photo = uri_cams_retrieve_image($ip, $username, $password);
set_site_transient( $transient_name, $photo, 10 * MINUTE_IN_SECONDS );
}

Expand All @@ -61,31 +64,31 @@ function uri_cams_shortcode($attributes, $content, $shortcode) {
$path = uri_cams_get_path() . $filename;
$time = filemtime($file);
}

// @todo what to do if $path isn't set and there's no old image?

if( ! empty( $time ) ) {
$alt .= ' (retrieved ' . Date('Y-m-d H:i:s', $time) . ')';
if ( empty($path) || empty($time) ) {
// we don't have a file we can use; bail out.
return '';
}

ob_start();
include 'templates/uri-cams-shortcode.php';
$html = ob_get_clean();
return $html;

$classes = 'uri-cams';
$classes .= ( ! empty( $class ) ) ? ' ' . $class : '';

$output = '<figure class="' . $classes . '">';
// $output .= strtotime('now');
$output .= '<img src="' . $path . '?t=' . $time . '" alt="' . $alt . '" />';

$output .= '</figure>';

return $output;

}
add_shortcode( 'uri-cams', 'uri_cams_shortcode' );




function uri_cams_get_image( $ip, $username, $password ) {
/**
* Retrieve a remote image
* @param str the camera's IP
* @param str the username for the camera
* @param str the password for the camera
* @return mixed arr on success; false on failure
*/
function uri_cams_retrieve_image( $ip, $username, $password ) {

$url = 'http://' . $ip . '/media/cam0/still.jpg?res=max';
$timeout = 10;
Expand All @@ -98,13 +101,8 @@ function uri_cams_get_image( $ip, $username, $password ) {
// return to variable
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// (don't) verify host ssl cert
// curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, $ssl_verifyhost );
// (don't) verify peer ssl cert
// curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, $ssl_verifypeer );

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
// send a user:password
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt( $ch, CURLOPT_USERPWD, $username . ':' . $password );

// fetch remote contents
Expand All @@ -123,15 +121,23 @@ function uri_cams_get_image( $ip, $username, $password ) {
'path' => $path,
'time' => strtotime('now')
);
} else {
return FALSE;
}

}


function uri_cams_save_file( $destination, $response, $ip ) {
/**
* Write a retrieved image to disk
* @param str the destination directory
* @param str the data to write i.e. [binary] source of the image
* @param str an identifier to add the filename e.g. the camera's domain name or IP address
* @return mixed
*/
function uri_cams_save_file( $destination, $response, $id ) {
$success = FALSE;
if ( wp_mkdir_p( $destination ) ) {
$filename = uri_cams_get_name($ip);
$filename = uri_cams_get_name($id);
$success = file_put_contents(trailingslashit( $destination ) . $filename, $response);
}
if( $success ) {
Expand All @@ -141,15 +147,153 @@ function uri_cams_save_file( $destination, $response, $ip ) {
}
}


/**
* Get the server-side path for the uploads directory
* @return str
*/
function uri_cams_get_directory() {
return trailingslashit( WP_CONTENT_DIR ) . 'uploads/uri-cams';
}

/**
* Get the client-side path for the uploads directory
* @return str
*/
function uri_cams_get_path() {
return trailingslashit( WP_CONTENT_URL ) . 'uploads/uri-cams/';
}

function uri_cams_get_name($ip) {
return 'uri-cams--' . $ip . '.jpg';
}
/**
* Generate the file name based on the IP address of the camera
* @param str an identifier to add the filename e.g. the camera's domain name or IP address
* @return str
*/
function uri_cams_get_name($id) {
$id = str_replace( '/', '-', $id );
return 'uri-cams--' . $id . '.jpg';
}

/**
* Format a UNIX timestamp in a consistent way
* @return str
*/
function uri_cams_format_date($timestamp) {
if( ! empty ( $timestamp ) ) {
$t = new DateTime( '@'.$timestamp );
} else {
$t = new DateTime( 'now' );
}
$t->setTimezone( new DateTimeZone( get_option('gmt_offset') ) );
return $t->format('Y-m-d H:i:s');
}





/** LOTS OF NON_DRY CODE AHEAD... SORRY **/


/**
* Shortcode callback for engineering images
*/
function uri_cams_engineering_shortcode($attributes, $content, $shortcode) {

// get the shortcode attributes and add defaults
extract( shortcode_atts(
array(
'url' => 'http://www.ele.uri.edu/camera/archive2',
'alt' => '',
'class' => '',
'link' => false
), $attributes )
);


// load the cached image

$transient_name = 'uri_cams_' . $url;

if ( false === ( $photo = get_site_transient( $transient_name ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$photo = uri_cams_retrieve_engineering_image( $url );
set_site_transient( $transient_name, $photo, 10 * MINUTE_IN_SECONDS );
}

$path = $photo['path'];
$time = $photo['time'];

$filename = uri_cams_get_name($url);
$file = uri_cams_get_directory() . '/' . $filename;
if( file_exists( $file ) ) {
$path = uri_cams_get_path() . $filename;
$time = filemtime($file);
}

if ( empty($path) || empty($time) ) {
// we don't have a file we can use; bail out.
return '';
}

ob_start();
include 'templates/uri-cams-shortcode.php';
$html = ob_get_clean();
return $html;



}
add_shortcode( 'uri-engineering-cams', 'uri_cams_engineering_shortcode' );

/**
* Retrieve a remote engineering image
* @param str the camera's host URL
* @return mixed arr on success; false on failure
*/
function uri_cams_retrieve_engineering_image( $base_url ) {

$now = new DateTime( 'now', new DateTimeZone( get_option('gmt_offset') ) );
if( $now->format('H') < 6 ) {
$t = new DateTime( 'yesterday', new DateTimeZone( get_option('gmt_offset') ) );
$url = trailingslashit($base_url) . $t->format('Y-m-d') . '/12:00.jpg';
} else if( $now->format('H') >= 18 ) {
$t = new DateTime( 'now', new DateTimeZone( get_option('gmt_offset') ) );
$url = trailingslashit($base_url) . $t->format('Y-m-d') . '/17:00.jpg';
} else {
$t = new DateTime( 'now', new DateTimeZone( get_option('gmt_offset') ) );
$url = trailingslashit($base_url) . $t->format('Y-m-d/H:i') . '.jpg';
}

$timeout = 10;

// set up curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );

// return to variable
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// fetch remote contents
if ( false === ( $response = curl_exec( $ch ) ) ) {
// if we get an error, use that
$error = curl_error( $ch );
}
// close the resource
curl_close( $ch );

if( empty( $error ) ) {
$destination = uri_cams_get_directory();
$file = uri_cams_save_file( $destination, $response, $base_url );
$path = uri_cams_get_path() . $file;
return array(
'path' => $path,
'time' => strtotime('now')
);
} else {
return FALSE;
}

}


0 comments on commit 0d6657e

Please sign in to comment.