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
|
2014-06-29 19:58:01 +02:00
|
|
|
Version: 0.4dev
|
2014-04-08 13:14:30 +02:00
|
|
|
Author: Martin Schuette
|
|
|
|
Author URI: http://mschuette.name/
|
|
|
|
Licence: 2-clause BSD
|
|
|
|
*/
|
|
|
|
|
2014-05-25 22:10:12 +02:00
|
|
|
define('FF_HH_STABLE_BASEDIR', 'http://updates.hamburg.freifunk.net/stable/');
|
2014-04-08 13:14:30 +02:00
|
|
|
define('FF_HH_CACHETIME', 15);
|
|
|
|
|
|
|
|
/* gets metadata from URL, handles caching */
|
2014-04-08 13:15:20 +02:00
|
|
|
function ff_hh_getmanifest ($basedir) {
|
2014-04-08 13:14:30 +02:00
|
|
|
// Caching
|
2014-06-26 11:16:09 +02:00
|
|
|
if ( WP_DEBUG || ( false === ( $manifest = get_transient( "ff_hh_manifest" ) ) ) ) {
|
|
|
|
$manifest = array();
|
|
|
|
$url = $basedir . 'sysupgrade/manifest';
|
|
|
|
$http_response = wp_remote_get( $url ); // TODO: error handling
|
|
|
|
$input = wp_remote_retrieve_body( $http_response );
|
2014-04-08 13:14:30 +02:00
|
|
|
foreach ( explode("\n", $input) as $line ) {
|
2014-04-08 13:15:20 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
|
|
|
|
set_transient( "ff_hh_manifest", $manifest, $cachetime );
|
|
|
|
}
|
|
|
|
return $manifest;
|
|
|
|
}
|
|
|
|
|
2014-04-08 13:55:11 +02:00
|
|
|
/* gets latest version from first manifest line */
|
|
|
|
function ff_hh_getlatest ($basedir) {
|
|
|
|
// Caching
|
|
|
|
if ( false === ( $sw_ver = get_transient( "ff_hh_latestversion" ) ) ) {
|
|
|
|
$sw_ver = 'unknown';
|
|
|
|
$input = wp_remote_retrieve_body( wp_remote_get($basedir . 'sysupgrade/manifest') );
|
|
|
|
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;
|
|
|
|
set_transient( "ff_hh_latestversion", $sw_ver, $cachetime );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sw_ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) {
|
|
|
|
add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion');
|
|
|
|
}
|
|
|
|
// Example:
|
|
|
|
// [ff_hh_latestversion]
|
|
|
|
function ff_hh_shortcode_latestversion( $atts, $content, $name ) {
|
|
|
|
$sw_ver = ff_hh_getlatest(FF_HH_STABLE_BASEDIR);
|
|
|
|
$outstr = "<span class=\"ff $name\">$sw_ver</span>";
|
|
|
|
return $outstr;
|
|
|
|
}
|
2014-04-08 13:14:30 +02:00
|
|
|
if ( ! shortcode_exists( 'ff_hh_versions' ) ) {
|
2014-04-08 13:55:11 +02:00
|
|
|
add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions');
|
2014-04-08 13:14:30 +02:00
|
|
|
}
|
|
|
|
// Example:
|
|
|
|
// [ff_hh_versions]
|
2014-05-25 22:06:18 +02:00
|
|
|
// [ff_hh_versions grep="ubiquiti"]
|
2014-04-08 13:55:11 +02:00
|
|
|
function ff_hh_shortcode_versions( $atts, $content, $name ) {
|
2014-04-08 13:15:20 +02:00
|
|
|
$manifest = ff_hh_getmanifest(FF_HH_STABLE_BASEDIR);
|
|
|
|
|
2014-04-08 13:14:30 +02:00
|
|
|
$outstr = "<div class=\"ff $name\">";
|
2014-04-08 13:15:20 +02:00
|
|
|
$outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>';
|
|
|
|
|
2014-05-25 22:06:18 +02:00
|
|
|
# optionally filter output by given substring
|
|
|
|
if (is_array($atts)
|
|
|
|
&& array_key_exists('grep', $atts)
|
|
|
|
&& !empty($atts['grep'])) {
|
|
|
|
$grep = $atts['grep'];
|
|
|
|
} else {
|
|
|
|
$grep = false;
|
|
|
|
}
|
|
|
|
|
2014-04-08 13:15:20 +02:00
|
|
|
foreach ($manifest as $hw => $versions) {
|
2014-05-25 22:06:18 +02:00
|
|
|
// filter
|
|
|
|
if ($grep && (false === strpos($hw, $grep))) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-29 19:58:01 +02:00
|
|
|
$hw = ff_hh_beautify_hw_name($hw, $grep);
|
2014-05-25 22:06:18 +02:00
|
|
|
$outstr .= sprintf("\n<tr><td>%s</td>", $hw);
|
2014-04-08 13:15:20 +02:00
|
|
|
|
|
|
|
// factory versions
|
|
|
|
$hw_ver_links = array();
|
|
|
|
foreach ($versions as $hw_ver => $filename) {
|
|
|
|
$filename = str_replace('-sysupgrade', '', $filename);
|
|
|
|
$hw_ver_links[] = sprintf('<a href="%s%s">%s.x</a>',
|
|
|
|
FF_HH_STABLE_BASEDIR.'factory/', $filename, $hw_ver);
|
|
|
|
}
|
|
|
|
$outstr .= '<td>Hardware Version ' . join(', ', $hw_ver_links) . '</td>';
|
2014-04-08 13:14:30 +02:00
|
|
|
|
2014-04-08 13:15:20 +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>',
|
|
|
|
FF_HH_STABLE_BASEDIR.'sysupgrade/', $filename, $hw_ver);
|
|
|
|
}
|
|
|
|
$outstr .= '<td>Hardware Version ' . join(', ', $hw_ver_links) . '</td>';
|
|
|
|
|
|
|
|
$outstr .= '</tr>';
|
2014-04-08 13:14:30 +02:00
|
|
|
}
|
2014-04-08 13:15:20 +02:00
|
|
|
|
2014-04-08 13:14:30 +02:00
|
|
|
$outstr .= '</table>';
|
|
|
|
$outstr .= '</div>';
|
2014-04-08 13:15:20 +02:00
|
|
|
// $outstr .= '<pre>'.print_r($manifest, true).'</pre>';
|
2014-04-08 13:14:30 +02:00
|
|
|
return $outstr;
|
|
|
|
}
|
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
|
|
|
|
// (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('TP LINK ', 'TP-Link ', $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 / nanostation-loco-m', $hw);
|
|
|
|
$hw = str_replace('-m', ' M2', $hw);
|
|
|
|
$hw = str_replace('-', ' ', $hw);
|
|
|
|
$hw = ucwords($hw);
|
|
|
|
} elseif (!strncmp($hw, 'd-link', 6)) {
|
|
|
|
if ($discard_vendor) $hw = str_replace($discard_vendor, '', $hw);
|
|
|
|
$hw = str_replace('-', ' ', $hw);
|
|
|
|
$hw = str_replace('d link ', 'D-Link ', $hw);
|
|
|
|
$hw = str_replace(' dir ', ' DIR-', $hw);
|
|
|
|
}
|
|
|
|
return $hw;
|
|
|
|
}
|
|
|
|
|