freifunk-versions/freifunk-versions.php

258 lines
8.6 KiB
PHP
Raw Normal View History

2014-04-08 13:14:30 +02:00
<?php
/*
Plugin Name: Freifunk Hamburg Firmware List Shortcode
Plugin URI: http://mschuette.name/
Description: Defines shortcodes to display Freifunk Hamburg Firmware versions
2017-11-17 21:37:10 +01:00
Version: 0.5dev
2014-04-08 13:14:30 +02:00
Author: Martin Schuette
Author URI: http://mschuette.name/
Licence: 2-clause BSD
*/
2017-11-17 21:37:10 +01:00
define( 'FF_HH_UPDATES_URL', 'https://updates.hamburg.freifunk.net/' );
2015-05-31 17:40:57 +02:00
define( 'FF_HH_CACHETIME', 1 );
2014-04-08 13:14:30 +02:00
/* gets metadata from URL, handles caching */
2017-11-17 21:37:10 +01:00
function ff_hh_getmanifest( $branch_url, $domain, $branch ) {
2014-06-29 20:22:12 +02:00
// Caching
2017-11-17 21:37:10 +01:00
$cache_key = 'ff_hh_manifest_' . $domain . '_' . $branch;
if ( WP_DEBUG || ( false === ( $manifest = get_transient( $cache_key ) ) ) ) {
$manifest = array();
$url = $branch_url . '/sysupgrade/' . $branch . '.manifest';
2014-06-29 20:22:12 +02:00
$http_response = wp_remote_get( $url ); // TODO: error handling
2017-11-17 21:37:10 +01:00
$input = wp_remote_retrieve_body( $http_response );
2014-06-29 20:22:12 +02:00
foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ( $ret === 4 ) {
if ( preg_match( '/^(.*)-v(\d+)$/', $hw, $matches ) ) {
$hw = $matches[1];
$hw_ver = $matches[2];
} else {
$hw_ver = '1';
}
$manifest[$hw][$hw_ver] = $filename;
}
}
2014-04-08 13:14:30 +02:00
2014-06-29 20:22:12 +02:00
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
2017-11-17 21:37:10 +01:00
set_transient( $cache_key, $manifest, $cachetime );
2014-06-29 20:22:12 +02:00
}
return $manifest;
2014-04-08 13:14:30 +02:00
}
2014-04-08 13:55:11 +02:00
/* gets latest version from first manifest line */
2017-11-17 21:37:10 +01:00
function ff_hh_getlatest( $branch_url, $domain, $branch ) {
2014-06-29 20:22:12 +02:00
// Caching
2017-11-17 21:37:10 +01:00
$cache_key = 'ff_hh_latestversion_' . $domain . '_' . $branch;
if ( false === ( $sw_ver = get_transient( $cache_key ) ) ) {
2014-06-29 20:22:12 +02:00
$sw_ver = 'unknown';
2017-11-17 21:37:10 +01:00
$url = $branch_url . '/sysupgrade/' . $branch . '.manifest';
$input = wp_remote_retrieve_body( wp_remote_get( $url ) );
2014-06-29 20:22:12 +02:00
foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ( $ret === 4 ) {
// break processing on first matching line
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
2017-11-17 21:37:10 +01:00
set_transient( $cache_key, $sw_ver, $cachetime );
2014-06-29 20:22:12 +02:00
break;
}
}
}
return $sw_ver;
2014-04-08 13:55:11 +02:00
}
if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) {
2014-06-29 20:22:12 +02:00
add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion' );
2014-04-08 13:55:11 +02:00
}
// Example:
// [ff_hh_latestversion]
2017-11-17 21:37:10 +01:00
// [ff_hh_latestversion domain="ffhh-sued" branch="experimental"]
2014-04-08 13:55:11 +02:00
function ff_hh_shortcode_latestversion( $atts, $content, $name ) {
2017-11-17 21:37:10 +01:00
$domain = 'ffhh';
$branch = 'stable';
if ( is_array( $atts ) ) {
if ( array_key_exists( 'domain', $atts ) && ! empty( $atts['domain'] ) ) {
$domain = $atts['domain'];
}
if ( array_key_exists( 'branch', $atts ) && ! empty( $atts['branch'] ) ) {
$branch = $atts['branch'];
}
}
$branch_url = FF_HH_UPDATES_URL . $domain . '/' . $branch;
$sw_ver = ff_hh_getlatest( $branch_url, $domain, $branch );
2014-06-29 20:22:12 +02:00
$outstr = "<span class=\"ff $name\">$sw_ver</span>";
return $outstr;
2014-04-08 13:55:11 +02:00
}
2017-11-17 21:37:10 +01:00
2014-04-08 13:14:30 +02:00
if ( ! shortcode_exists( 'ff_hh_versions' ) ) {
2014-06-29 20:22:12 +02:00
add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions' );
2014-04-08 13:14:30 +02:00
}
// Example:
// [ff_hh_versions]
2017-11-17 21:37:10 +01:00
// [ff_hh_versions domain="ffhh-sued" branch="experimental" grep="ubiquiti"]
2014-04-08 13:55:11 +02:00
function ff_hh_shortcode_versions( $atts, $content, $name ) {
2017-11-17 21:37:10 +01:00
$domain = 'ffhh';
$branch = 'stable';
$grep = false;
2017-12-04 21:56:43 +01:00
$filter = false;
2017-11-17 21:37:10 +01:00
if ( is_array( $atts ) ) {
if ( array_key_exists( 'domain', $atts ) && ! empty( $atts['domain'] ) ) {
$domain = $atts['domain'];
}
if ( array_key_exists( 'branch', $atts ) && ! empty( $atts['branch'] ) ) {
$branch = $atts['branch'];
}
if ( array_key_exists( 'grep', $atts ) && ! empty( $atts['grep'] ) ) {
$grep = $atts['grep'];
}
2017-12-04 21:56:43 +01:00
if ( array_key_exists( 'filter', $atts ) && ! empty( $atts['filter'] ) ) {
$filter = explode ( ',', $atts['filter'] );
}
2017-11-17 21:37:10 +01:00
}
$branch_url = FF_HH_UPDATES_URL . $domain . '/' . $branch;
$manifest = ff_hh_getmanifest( $branch_url, $domain, $branch );
2014-04-08 13:15:20 +02:00
2014-06-29 20:22:12 +02:00
$outstr = "<div class=\"ff $name\">";
$outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>';
2014-04-08 13:15:20 +02:00
2014-06-29 20:22:12 +02:00
foreach ( $manifest as $hw => $versions ) {
2017-12-04 21:56:43 +01:00
// select some models
2014-06-29 20:22:12 +02:00
if ( $grep && ( false === strpos( $hw, $grep ) ) ) {
continue;
}
2017-12-04 21:56:43 +01:00
// filter others
if ( $filter ) {
$filtered = false;
foreach ( $filter as $flt ) {
if ( strpos ( $hw, $flt ) !== false ) {
$filtered = true;
break;
}
}
if ( $filtered ) {
continue;
}
}
2014-06-29 20:22:12 +02:00
$hw = ff_hh_beautify_hw_name( $hw, $grep );
$outstr .= sprintf( "\n<tr><td>%s</td>", $hw );
2014-04-08 13:15:20 +02:00
2014-06-29 20:22:12 +02:00
// factory versions
$hw_ver_links = array();
foreach ( $versions as $hw_ver => $filename ) {
if ( strpos( $hw, 'Unifi Ac Pro' ) || strpos( $hw, 'Unifi Ac Lite' ) ) {
continue;
}
2014-06-29 20:22:12 +02:00
$filename = str_replace( '-sysupgrade', '', $filename );
if (strpos($filename,'netgear') !== false) {
$filename = str_replace( '.bin', '.img', $filename );
2017-11-17 21:37:10 +01:00
$filename = str_replace( '.tar', '.img', $filename );
}
2014-06-29 20:22:12 +02:00
$hw_ver_links[] = sprintf(
'<a href="%s%s">%s.x</a>',
2017-11-17 21:37:10 +01:00
$branch_url . '/factory/',
$filename,
$hw_ver
2014-06-29 20:22:12 +02:00
);
}
if ( count($hw_ver_links) > 0) {
$outstr .= '<td>Hardware Ver. ' . join( ', ', $hw_ver_links ) . '</td>';
} else {
$outstr .= '<td><i>Benutze das Image</br>zur Aktualisierung</i></td>';
}
2014-04-08 13:14:30 +02:00
2014-06-29 20:22:12 +02:00
// sysupgrade versions
$hw_ver_links = array();
foreach ( $versions as $hw_ver => $filename ) {
$hw_ver_links[] = sprintf(
'<a href="%s%s">%s.x</a>',
2017-11-17 21:37:10 +01:00
$branch_url . '/sysupgrade/',
$filename,
$hw_ver
2014-06-29 20:22:12 +02:00
);
}
$outstr .= '<td>Hardware Ver. ' . join( ', ', $hw_ver_links ) . '</td>';
2014-04-08 13:15:20 +02:00
2014-06-29 20:22:12 +02:00
$outstr .= '</tr>';
}
2014-04-08 13:15:20 +02:00
2014-06-29 20:22:12 +02:00
$outstr .= '</table>';
$outstr .= '</div>';
// $outstr .= '<pre>'.print_r( $manifest, true ).'</pre>';
return $outstr;
2014-04-08 13:14:30 +02:00
}
2014-06-29 19:58:01 +02:00
// some crude rules to add capitalization and whitespace to the
// hardware model name.
// set $discard_vendor to strip the vendor name
2014-06-29 20:22:12 +02:00
// (used for single-vendor lists, e.g. $discard_vendor = 'tp-link' )
function ff_hh_beautify_hw_name( $hw, $discard_vendor = '' ) {
if ( ! strncmp( $hw, 'tp-link', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', ' ', $hw );
$hw = str_replace( ' TL ', ' TL-', $hw );
} elseif ( ! strncmp( $hw, 'ubiquiti', 8 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = str_replace( 'bullet-m', 'bullet-m', $hw );
2014-06-29 20:22:12 +02:00
$hw = str_replace( '-', ' ', $hw );
$hw = ucwords( $hw );
} elseif ( ! strncmp( $hw, 'd-link', 6 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
2015-01-18 23:15:39 +01:00
$hw = strtoupper( $hw );
2014-06-29 20:22:12 +02:00
$hw = str_replace( '-', ' ', $hw );
2015-01-18 23:15:39 +01:00
$hw = str_replace( ' DIR ', ' DIR-', $hw );
2014-08-07 16:57:21 +02:00
} elseif ( ! strncmp( $hw, 'linksys', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
2015-01-18 23:15:39 +01:00
$hw = strtoupper( $hw );
2014-08-07 16:57:21 +02:00
$hw = str_replace( '-', ' ', $hw );
2015-01-18 23:15:39 +01:00
$hw = str_replace( ' WRT', ' WRT-', $hw );
2015-01-18 22:25:35 +01:00
} elseif ( ! strncmp( $hw, 'buffalo', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
2015-01-18 23:15:39 +01:00
$hw = strtoupper( $hw );
$hw = str_replace( 'HP-AG300H-WZR-600DHP', 'HP-AG300H & WZR-600DHP', $hw );
$hw = str_replace( '-WZR', 'WZR', $hw );
2017-11-17 21:37:10 +01:00
} elseif ( ! strncmp( $hw, 'netgear', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'allnet', 6 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'gl-inet', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'onion-omega', 11 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
} elseif ( ! strncmp( $hw, 'alfa', 4 ) ) {
2017-11-17 21:37:10 +01:00
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'wd', 2 ) ) {
2017-11-17 21:37:10 +01:00
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, '8devices', 8 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( 'CARAMBOLA2-BOARD', 'Carambola 2', $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'meraki', 6 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( 'meraki', '', $hw );
$hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'openmesh', 8 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw );
$hw = str_replace( 'openmesh', '', $hw );
$hw = str_replace( '-', '', $hw );
2014-06-29 20:22:12 +02:00
}
return $hw;
2014-06-29 19:58:01 +02:00
}