david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | // todo: unsure if can handle filenames that are URLs? |
| 4 | // handy constant to compute file size in megabytes |
| 5 | |
| 6 | function fileSizeInMegs($filename) { |
| 7 | $onemeg=1024*1024; |
| 8 | $zipfilesize=filesize($filename); |
| 9 | $zipfilesize=round($zipfilesize/$onemeg, 0); |
| 10 | return $zipfilesize; |
| 11 | } |
| 12 | |
| 13 | function fileSizeForDisplay($filename) { |
| 14 | $onekilo=1024; |
| 15 | $onemeg=$onekilo * $onekilo; |
| 16 | $criteria = 10 * $onemeg; |
| 17 | $scaleChar = "M"; |
| 18 | $zipfilesize=filesize($filename); |
| 19 | if ($zipfilesize > $criteria) { |
| 20 | $zipfilesize=round($zipfilesize/$onemeg, 0); |
| 21 | $scaleChar = "M"; |
| 22 | } |
| 23 | else { |
| 24 | $zipfilesize=round($zipfilesize/$onekilo, 0); |
| 25 | $scaleChar = "K"; |
| 26 | } |
| 27 | $result = "(" . $zipfilesize . $scaleChar . ")"; |
| 28 | return $result; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | function displayFileLine($downloadprefix, $filename, $zipfilesize, $fileShortDescription) { |
| 33 | echo "<td align=\"right\" valign=\"top\" width=\"10%\">"; |
| 34 | echo "<a href=\"$downloadprefix$filename\">" . $fileShortDescription . "</a>"; |
| 35 | echo "</td><td align=\"right\" valign=\"top\" width=\"5%\">"; |
| 36 | echo $zipfilesize; |
| 37 | echo "</td>"; |
| 38 | echo "<td align=\"right\" valign=\"top\" width=\"5%\">"; |
| 39 | echo "[<a href=\"checksum/$filename.md5\">md5</a>]"; |
| 40 | echo "</td>"; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | function stream_last_modified($url) |
| 45 | { |
| 46 | if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0) |
| 47 | { |
| 48 | if (!($fp = @fopen($url, 'r'))) |
david_williams | 454f149 | 2007-09-30 23:03:52 +0000 | [diff] [blame^] | 49 | return NULL; |
david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 50 | |
| 51 | $meta = stream_get_meta_data($fp); |
| 52 | for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) |
| 53 | { |
| 54 | if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')) |
| 55 | { |
| 56 | $modtime = substr($meta['wrapper_data'][$j], 15); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | fclose($fp); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | $parts = parse_url($url); |
| 65 | $host = $parts['host']; |
| 66 | $path = $parts['path']; |
| 67 | |
| 68 | if (!($fp = @fsockopen($host, 80))) |
david_williams | 454f149 | 2007-09-30 23:03:52 +0000 | [diff] [blame^] | 69 | return NULL; |
david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 70 | |
| 71 | $req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n"; |
| 72 | fputs($fp, $req); |
| 73 | |
| 74 | while (!feof($fp)) |
| 75 | { |
| 76 | $str = fgets($fp, 4096); |
| 77 | if (strstr(strtolower($str), 'last-modified')) |
| 78 | { |
| 79 | $modtime = substr($str, 15); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | fclose($fp); |
| 84 | } |
| 85 | return isset($modtime) ? strtotime($modtime) : time(); |
| 86 | } |
| 87 | |
| 88 | function isMirrored($uriToCheck) { |
| 89 | global $debugScript; |
| 90 | global $debugFunctions; |
david_williams | 454f149 | 2007-09-30 23:03:52 +0000 | [diff] [blame^] | 91 | $localuri = $uriToCheck; |
| 92 | |
david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 93 | $debugMirrorList = false; |
david_williams | 454f149 | 2007-09-30 23:03:52 +0000 | [diff] [blame^] | 94 | if ($debugScript) { |
| 95 | echo "uriToCheck: " . $localuri . "<br />"; |
| 96 | } |
| 97 | // turn off warnings, as sometimes HTML is returned, which causes lots of warnings |
| 98 | $holdLevel = error_reporting(E_ERROR); |
| 99 | $mirrorsxml=simplexml_load_file(rawurlencode($localuri) . urlencode("&format=xml")); |
| 100 | error_reporting($holdLevel); |
david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 101 | |
| 102 | $xmlcount = 0; |
| 103 | if ($mirrorsxml) { |
david_williams | 454f149 | 2007-09-30 23:03:52 +0000 | [diff] [blame^] | 104 | if ($debugFunctions) { |
| 105 | echo "root node: " . $mirrorsxml->getName() . "<br />"; |
| 106 | } |
| 107 | if (strcmp($mirrorsxml->getName(), "mirrors") == 0) { |
| 108 | foreach ($mirrorsxml->children() as $mirror) { |
| 109 | if (strcmp($mirror->getName(),"mirror") == 0) { |
| 110 | $xmlcount=$xmlcount+1; |
| 111 | } |
| 112 | if ($debugMirrorList) { |
| 113 | print_r($mirror); |
| 114 | echo "<br />"; |
| 115 | } |
david_williams | c3b4f45 | 2007-09-30 08:28:17 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | if ($debugFunctions) { |
| 119 | echo "Mirror count: " . $xmlcount . "<br />"; |
| 120 | } |
| 121 | } |
| 122 | return ($xmlcount > 0); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | function getPlatform () { |
| 127 | global $debugScript; |
| 128 | global $debugFunctions; |
| 129 | $browser = get_browser(null, true); |
| 130 | $rawPlatform = $browser['platform']; |
| 131 | if ($debugFunctions) { |
| 132 | echo "browser platfrom: " . $rawPlatform . "<br />" ; |
| 133 | } |
| 134 | |
| 135 | if ($debugFunctions) { |
| 136 | $browserKeys = array_keys($browser); |
| 137 | foreach ($browserKeys as $key) { |
| 138 | echo $key . ": " . $browser[$key] . "<br />"; |
| 139 | } |
| 140 | } |
| 141 | $platform = "unknown"; |
| 142 | if (strpos($rawPlatform, "Win") === 0) { |
| 143 | $platform="windows"; |
| 144 | } else if (strpos($rawPlatform, "Linux") === 0) { |
| 145 | $platform="linux"; |
| 146 | } else if (strpos($rawPlatform, "Mac") === 0) { |
| 147 | $platform="mac"; |
| 148 | } |
| 149 | return $platform; |
| 150 | } |
| 151 | |
| 152 | function getPrereqReferenceOrName($prerequrl, $prereqname) { |
| 153 | // todo: we really only need "if exists" so could make a bit more efficient |
| 154 | // I tried "file_exists" but is didn't seem to work on my test server |
| 155 | if (stream_last_modified($prerequrl)) { |
| 156 | $reflink="<a href=\"" . $prerequrl . "\">" . $prereqname . "</a>"; |
| 157 | } else { |
| 158 | $reflink=$prereqname; |
| 159 | } |
| 160 | return $reflink; |
| 161 | } |
| 162 | ?> |