From fdd3b38d12d6e97d96e728aa2129faa2b29935f5 Mon Sep 17 00:00:00 2001 From: John Pennypacker Date: Fri, 4 Jan 2019 12:38:26 -0500 Subject: [PATCH 1/7] localizes time strings --- uri-cams.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/uri-cams.php b/uri-cams.php index b6085b3..5419a00 100755 --- a/uri-cams.php +++ b/uri-cams.php @@ -61,11 +61,14 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { $path = uri_cams_get_path() . $filename; $time = filemtime($file); } + + $timestamp = uri_cams_format_date( $time ); + // @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) . ')'; + $alt .= ' (retrieved ' . $timestamp . ')'; } $classes = 'uri-cams'; @@ -152,4 +155,10 @@ function uri_cams_get_path() { function uri_cams_get_name($ip) { return 'uri-cams--' . $ip . '.jpg'; +} + +function uri_cams_format_date($timestamp) { + $t = new DateTime( '@'.$timestamp ); + $t->setTimezone( new DateTimeZone( get_option('gmt_offset') ) ); + return $t->format('Y-m-d H:i:s'); } \ No newline at end of file From 8cb533908e0daf3f6326ac0a09fd2b5a9491f6d9 Mon Sep 17 00:00:00 2001 From: John Pennypacker Date: Fri, 4 Jan 2019 14:39:49 -0500 Subject: [PATCH 2/7] adds the option to include a link --- README.md | 3 +++ uri-cams.php | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6d22f6c..acdecf0 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ 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 diff --git a/uri-cams.php b/uri-cams.php index 5419a00..1ecdeef 100755 --- a/uri-cams.php +++ b/uri-cams.php @@ -39,7 +39,8 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { 'username' => 'Viewer', 'password' => 'bay campus', 'alt' => '', - 'class' => '' + 'class' => '', + 'link' => false ), $attributes ) ); @@ -75,9 +76,17 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { $classes .= ( ! empty( $class ) ) ? ' ' . $class : ''; $output = '
'; - // $output .= strtotime('now'); + + if ( false !== $link ) { + $output .= ''; + } + $output .= '' . $alt . ''; + if ( false !== $link ) { + $output .= ''; + } + $output .= '
'; return $output; From 9f1ea1069216b97b771efd9c2ccdf233f369b3af Mon Sep 17 00:00:00 2001 From: John Pennypacker Date: Fri, 4 Jan 2019 16:14:44 -0500 Subject: [PATCH 3/7] added code to pull in engineering's images --- templates/uri-cams-shortcode.php | 21 +++ uri-cams.php | 214 ++++++++++++++++++++++++------- 2 files changed, 192 insertions(+), 43 deletions(-) create mode 100644 templates/uri-cams-shortcode.php diff --git a/templates/uri-cams-shortcode.php b/templates/uri-cams-shortcode.php new file mode 100644 index 0000000..0733013 --- /dev/null +++ b/templates/uri-cams-shortcode.php @@ -0,0 +1,21 @@ + + +
+ + + + + + <?php echo $alt; ?> + + + + + +
+ diff --git a/uri-cams.php b/uri-cams.php index 1ecdeef..2eae523 100755 --- a/uri-cams.php +++ b/uri-cams.php @@ -28,11 +28,11 @@ 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', @@ -45,11 +45,13 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { ); + // 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 ); } @@ -63,41 +65,31 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { $time = filemtime($file); } - $timestamp = uri_cams_format_date( $time ); - - - // @todo what to do if $path isn't set and there's no old image? - - if( ! empty( $time ) ) { - $alt .= ' (retrieved ' . $timestamp . ')'; + if ( empty($path) || empty($time) ) { + // we don't have a file we can use; bail out. + return ''; } - - $classes = 'uri-cams'; - $classes .= ( ! empty( $class ) ) ? ' ' . $class : ''; - - $output = '
'; - if ( false !== $link ) { - $output .= ''; - } - - $output .= '' . $alt . ''; - - if ( false !== $link ) { - $output .= ''; - } - - $output .= '
'; + $timestamp = uri_cams_format_date( $time ); + ob_start(); + include 'templates/uri-cams-shortcode.php'; + $html = ob_get_clean(); + return $html; - 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; @@ -110,13 +102,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 @@ -135,15 +122,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 ) { @@ -153,21 +148,154 @@ 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) { - $t = new DateTime( '@'.$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'); -} \ No newline at end of file +} + + + + + +/** 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 ''; + } + + $timestamp = uri_cams_format_date( $time ); + 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; + } + +} + + From e019ed3649212caefd65691131614549c4893032 Mon Sep 17 00:00:00 2001 From: John Pennypacker Date: Fri, 4 Jan 2019 16:27:15 -0500 Subject: [PATCH 4/7] put the last retrieved date back in the alt text and title --- templates/uri-cams-shortcode.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/uri-cams-shortcode.php b/templates/uri-cams-shortcode.php index 0733013..5a4746c 100644 --- a/templates/uri-cams-shortcode.php +++ b/templates/uri-cams-shortcode.php @@ -3,6 +3,8 @@ $classes = 'uri-cams'; $classes .= ( ! empty( $class ) ) ? ' ' . $class : ''; + $alt .= ' (retrieved ' . $timestamp . ')'; + ?>
From 2c79c494f019329fd135dc31b087c49f1ccdf897 Mon Sep 17 00:00:00 2001 From: John Pennypacker Date: Fri, 4 Jan 2019 16:32:55 -0500 Subject: [PATCH 5/7] cleaned up a little duplicate code --- templates/uri-cams-shortcode.php | 1 + uri-cams.php | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/templates/uri-cams-shortcode.php b/templates/uri-cams-shortcode.php index 5a4746c..7016678 100644 --- a/templates/uri-cams-shortcode.php +++ b/templates/uri-cams-shortcode.php @@ -3,6 +3,7 @@ $classes = 'uri-cams'; $classes .= ( ! empty( $class ) ) ? ' ' . $class : ''; + $timestamp = uri_cams_format_date( $time ); $alt .= ' (retrieved ' . $timestamp . ')'; ?> diff --git a/uri-cams.php b/uri-cams.php index 2eae523..bbe481c 100755 --- a/uri-cams.php +++ b/uri-cams.php @@ -70,7 +70,6 @@ function uri_cams_shortcode($attributes, $content, $shortcode) { return ''; } - $timestamp = uri_cams_format_date( $time ); ob_start(); include 'templates/uri-cams-shortcode.php'; $html = ob_get_clean(); @@ -236,7 +235,6 @@ function uri_cams_engineering_shortcode($attributes, $content, $shortcode) { return ''; } - $timestamp = uri_cams_format_date( $time ); ob_start(); include 'templates/uri-cams-shortcode.php'; $html = ob_get_clean(); From ef475f9831b5d8acf30a5539413431c69dabe0c5 Mon Sep 17 00:00:00 2001 From: alexandragauss <“alexandra_gauss@uri.edu”> Date: Fri, 10 Mar 2023 12:13:28 -0500 Subject: [PATCH 6/7] Add a composer.json file --- composer.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ade29c6 --- /dev/null +++ b/composer.json @@ -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" + } +} \ No newline at end of file From 2e2e7817232779f65d3e472d6c08b55ea31795a7 Mon Sep 17 00:00:00 2001 From: alexandragauss <“alexandra_gauss@uri.edu”> Date: Fri, 10 Mar 2023 12:15:13 -0500 Subject: [PATCH 7/7] Bump version number to 1.1 --- README.md | 2 +- uri-cams.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index acdecf0..94f01c2 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,4 @@ 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 diff --git a/uri-cams.php b/uri-cams.php index bbe481c..2572e0a 100755 --- a/uri-cams.php +++ b/uri-cams.php @@ -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