<?php

/*
 * PIMPED APACHE-STATUS
 * FUNCTIONS
 * 
 */

// ----------------------------------------------------------------------
// FUNKTIONEN
// ----------------------------------------------------------------------

/**
 * get new querystring - create the new querystring by existing query string
 * of current request and given new parameters
 * @param array $aQueryParams
 * @return string
 */
function getNewQs($aQueryParams) {
    $s = false;
    if ($_GET) {
        $aQueryParams = array_merge($_GET, $aQueryParams);
    }

    foreach ($aQueryParams as $var => $value) {
        if ($value)
            $s.="&amp;" . $var . "=" . urlencode($value);
    }
    $s = "?" . $s;
    return $s;
}

/**
 * make an http get request and return the response body
 * @param string $url
 * @return string
 */
function httpGet($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_USERAGENT, 'php-curl :: pimped apache status');
    $res = curl_exec($ch);
    curl_close($ch);
    return ($res);
}

/**
 * check for an update of the product
 * @param array $aOptions options array with these keys: url, ttl, link
 * @return type
 */
function checkUpdate($aOptions) {
    global $aLangTxt;
    $sUrl = str_replace(" ", "%20", $aOptions["url"]);
    $iTtl = (int) $aOptions["ttl"];
    $sTarget = sys_get_temp_dir() . "/checkupdate_" . md5($sUrl) . ".tmp";
    
    $sOut = '';

    // if the user does not want an update check then respect it
    if (!$iTtl){
        return false;
    }

    $bExec = true;
    if (file_exists($sTarget)) {
        $bExec = false;
        $aStat = stat($sTarget);
        $iAge = time() - $aStat[9];
        if ($iAge > $iTtl) {
            $bExec = true;
        }
        $sOut.="last exec: " . $iAge . " s ago - timer is $iTtl<br>\n";
    } else {
        $sOut.="last exec: never (touchfile was not found)<br>\n";
    }

    if ($bExec) {
        $sOut.="fetching $sUrl ...<br>";
        $sResult = httpGet($sUrl);
        if (!$sResult) {
            $sResult = ' <span class="version-updateerror">' . $aLangTxt['versionError'] . '</span>';
        } else {
            $sVersion = str_replace("UPDATE: v", "", str_replace(" is available", "", $sResult)
            );
            if (strpos($sResult, "UPDATE") === 0) {
                $sResult = ' <span class="version-updateavailable" '
                        . 'title="' . $sResult . ' - ' . $aOptions['link']['label'] . '">'
                        . '<a href="' . $aOptions['link']['url'] . '" target="_blank">'
                        . sprintf($aLangTxt['versionUpdateAvailable'], $sVersion)
                        . '</a>'
                        . '</span>';
            }
            if (strpos($sResult, "OK") === 0) {
                $sResult = ' <span class="version-uptodate" title="' . $sResult . '">'
                        . $aLangTxt['versionUptodate'] . '</span>';
            }
            file_put_contents($sTarget, $sResult);
        }
    } else {
        $sOut.="reading cache $sTarget ...<br>";
        $sResult = file_get_contents($sTarget);
    }
    // for DEBUG
    // echo $sOut;

    return '<span id="checkversion">'.$sResult.'</span>';
}

?>
