URL function firmware_get_versions( $base_url, $domain, $branch ) { $release = firmware_get_release( $base_url, $domain, $branch ); $factory_regex = "/^(?:.*$release-)(.*)\.(?:bin|img|img\.gz)$/"; $upgrade_regex = "/^(?:.*$release-)(.*)(?:-sysupgrade\..*)$/"; $cache_key = 'ff_firmware_versions_' . $domain . '_' . $branch; if ( WP_DEBUG || ( false === ( $versions = get_transient( $cache_key ) ) ) ) { $versions = []; $url = "$base_url/$domain/$branch/images/images.list"; $http_response = wp_remote_get( $url ); $input = wp_remote_retrieve_body( $http_response ); foreach ( explode( "\n", $input ) as $line ) { $ret = sscanf( $line, '%s %s', $folder, $filename ); if ( $ret !== 2 ) { continue; } $url = "$base_url/$domain/$branch/images/$folder/$filename"; if ( $folder === 'sysupgrade' ) { $result = firmware_parse_version( $filename, $upgrade_regex ); if ( $result !== NULL ) { $versions[$result[0]][$result[1]]['upgrade'] = $url; } } else { $result = firmware_parse_version( $filename, $factory_regex ); if ( $result !== NULL ) { $versions[$result[0]][$result[1]]['factory'] = $url; } } } $cachetime = FIRMWARE_CACHETIME * MINUTE_IN_SECONDS; set_transient( $cache_key, $versions, $cachetime ); } return $versions; } function firmware_parse_version( $filename, $regex ) { if ( preg_match( $regex, $filename, $matches ) ) { $hw = $matches[1]; $hw_ver = '1'; if ( preg_match( '/^(.*?)-?v(\d+)$/', $hw, $matches ) ) { $hw = $matches[1]; $hw_ver = $matches[2]; } return [$hw, $hw_ver]; } else { return NULL; } } // Example: // [ff_firmware_release] // [ff_firmware_release domain="ffhh" branch="experimental"] function firmware_release( $atts, $content, $name ) { $domain = 'multi'; $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']; } } $sw_ver = firmware_get_release( FIRMWARE_BASE_URL, $domain, $branch ); $outstr = "$sw_ver"; return $outstr; } // Example: // [ff_firmware_versions] // [ff_firmware_versions prefix="ubiquiti,ubnt" filter="ac-pro"] function firmware_versions( $atts, $content, $name ) { $domain = 'multi'; $branch = 'stable'; $prefix = false; $filter = false; 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( 'prefix', $atts ) && ! empty( $atts['prefix'] ) ) { $prefix = explode ( ',', $atts['prefix'] ); } if ( array_key_exists( 'filter', $atts ) && ! empty( $atts['filter'] ) ) { $filter = explode ( ',', $atts['filter'] ); } } $versions = firmware_get_versions( FIRMWARE_BASE_URL, $domain, $branch ); if ( $prefix ) { $filtered = []; foreach ( $versions as $hw => $hw_versions ) { foreach ( $prefix as $pfx ) { if ( strpos ( $hw, $pfx ) === 0 ) { $hw = substr( $hw, strlen($pfx) ); $hw = trim( $hw, '-' ); $filtered[$hw] = $hw_versions; } } } $versions = $filtered; } if ( $filter ) { $filtered = []; foreach ( $versions as $hw => $hw_versions ) { foreach ( $filter as $flt ) { if ( strpos ( $hw, $flt ) === FALSE ) { $filtered[$hw] = $hw_versions; } } } $versions = $filtered; } $outstr = "\n
Modell | Erstinstallation | Aktualisierung | ||
---|---|---|---|---|
%s | ", $hw ); // factory images $factory_links = []; foreach ( $hw_versions as $hw_ver => $urls ) { if ( ! array_key_exists( 'factory', $urls ) ) { continue; } $factory_url = $urls['factory']; $factory_links[] = "$hw_ver.x"; } if ( count($factory_links) > 0 ) { $outstr .= 'Version ' . join( ', ', $factory_links ) . ' | '; } else { $outstr .= ''; } // upgrade images $upgrade_links = []; foreach ( $hw_versions as $hw_ver => $urls ) { if ( ! array_key_exists( 'upgrade', $urls ) ) { continue; } $upgrade_url = $urls['upgrade']; $upgrade_links[] = "$hw_ver.x"; } if ( count($upgrade_links) > 0 ) { $outstr .= ' | Version ' . join( ', ', $upgrade_links ) . ' | '; } else { $outstr .= ''; } $outstr .= " |