david_williams | 4a022dd | 2009-05-26 04:13:39 +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 | if (file_exists($filename)) { |
| 19 | $zipfilesize=filesize($filename); |
| 20 | if ($zipfilesize > $criteria) { |
| 21 | $zipfilesize=round($zipfilesize/$onemeg, 0); |
| 22 | $scaleChar = "M"; |
| 23 | } |
| 24 | else { |
| 25 | $zipfilesize=round($zipfilesize/$onekilo, 0); |
| 26 | $scaleChar = "K"; |
| 27 | } |
| 28 | } |
| 29 | else { |
| 30 | $zipfilesize = 0; |
| 31 | } |
| 32 | $result = "(" . $zipfilesize . $scaleChar . ")"; |
| 33 | return $result; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | function displayFileLine($downloadprefix, $filename, $zipfilesize, $fileShortDescription) { |
| 38 | echo "<td align=\"right\" valign=\"top\" width=\"30%\">"; |
| 39 | echo "<a href=\"$downloadprefix$filename\">" . $fileShortDescription . "</a>"; |
| 40 | echo "</td><td align=\"right\" valign=\"top\" width=\"3%\">"; |
| 41 | echo $zipfilesize; |
| 42 | echo "</td>"; |
| 43 | echo "<td align=\"right\" valign=\"top\" width=\"2%\">"; |
| 44 | echo "[<a href=\"checksum/$filename.md5\">md5</a>]"; |
| 45 | echo "</td>"; |
| 46 | } |
| 47 | |
| 48 | function resourceExist($url, $mirrorPrefixuri, $prereqfilename, $eclipseFSpathPrefix) |
| 49 | { |
| 50 | $result = false; |
| 51 | |
| 52 | $allowURLopen = ini_get('allow_url_fopen'); |
| 53 | |
| 54 | if ($allowURLopen && stream_last_modified($url)) { |
| 55 | $result = true; |
| 56 | } |
| 57 | else { |
| 58 | // TODO: for now, we'll do a raw check on the whole file name, since enable_url_open |
| 59 | // is off. better would be to check if we are on build.eclipse.org or download.eclipse.org? |
| 60 | $wholePath = trim($eclipseFSpathPrefix) . "/" . trim($mirrorPrefixuri) . "/" . trim($prereqfilename); |
| 61 | if (file_exists($wholePath)) { |
| 62 | $result = true; |
| 63 | } |
| 64 | } |
| 65 | return $result; |
| 66 | } |
| 67 | |
| 68 | function stream_last_modified($url) |
| 69 | { |
| 70 | if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0) |
| 71 | { |
| 72 | if (!($fp = @fopen($url, 'r'))) |
| 73 | return NULL; |
| 74 | |
| 75 | $meta = stream_get_meta_data($fp); |
| 76 | for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) |
| 77 | { |
| 78 | if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified')) |
| 79 | { |
| 80 | $modtime = substr($meta['wrapper_data'][$j], 15); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | fclose($fp); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | $parts = parse_url($url); |
| 89 | $host = $parts['host']; |
| 90 | $path = $parts['path']; |
| 91 | |
| 92 | if (!($fp = @fsockopen($host, 80))) |
| 93 | return NULL; |
| 94 | |
| 95 | $req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n"; |
| 96 | fputs($fp, $req); |
| 97 | |
| 98 | while (!feof($fp)) |
| 99 | { |
| 100 | $str = fgets($fp, 4096); |
| 101 | if (strstr(strtolower($str), 'last-modified')) |
| 102 | { |
| 103 | $modtime = substr($str, 15); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | fclose($fp); |
| 108 | } |
| 109 | return isset($modtime) ? strtotime($modtime) : time(); |
| 110 | } |
| 111 | |
| 112 | function isMirrored($uriToCheck) { |
| 113 | global $debugScript; |
| 114 | global $debugFunctions; |
| 115 | $localuri = $uriToCheck; |
| 116 | |
| 117 | $debugMirrorList = false; |
| 118 | if ($debugScript) { |
| 119 | echo "uriToCheck: " . $localuri . "<br />"; |
| 120 | } |
| 121 | |
| 122 | $xmlcount = 0; |
| 123 | |
| 124 | /* This method true and accurate method of parsing mirror results |
| 125 | * may be expensive, and would |
| 126 | * likely cause artificially high counts of "downloads". |
| 127 | * Could maybe use if somehow only checked once ever 5 minutes or something. |
| 128 | |
| 129 | |
| 130 | // turn off warnings, as sometimes HTML is returned, which causes lots of warnings |
| 131 | $holdLevel = error_reporting(E_ERROR); |
| 132 | $mirrorsxml=simplexml_load_file(rawurlencode($localuri) . urlencode("&format=xml")); |
| 133 | error_reporting($holdLevel); |
| 134 | |
| 135 | |
| 136 | if ($mirrorsxml) { |
| 137 | if ($debugFunctions) { |
| 138 | echo "root node: " . $mirrorsxml->getName() . "<br />"; |
| 139 | } |
| 140 | if (strcmp($mirrorsxml->getName(), "mirrors") == 0) { |
| 141 | foreach ($mirrorsxml->children() as $mirror) { |
| 142 | if (strcmp($mirror->getName(),"mirror") == 0) { |
| 143 | $xmlcount=$xmlcount+1; |
| 144 | } |
| 145 | if ($debugMirrorList) { |
| 146 | print_r($mirror); |
| 147 | echo "<br />"; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | if ($debugFunctions) { |
| 152 | echo "Mirror count: " . $xmlcount . "<br />"; |
| 153 | } |
| 154 | } |
| 155 | */ |
| 156 | /* |
| 157 | * Use simple heuristic based on pattern |
| 158 | * in the URI ... if it contains "/downloads/" then assume it's mirrored |
| 159 | */ |
| 160 | if (strpos($uriToCheck, "webtools/downloads/") > 0) { |
| 161 | $xmlcount = 1; |
| 162 | } |
| 163 | return ($xmlcount > 0); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | // TODO: replace with Phoenix variables |
| 168 | function getPlatform () { |
| 169 | global $debugScript; |
| 170 | global $debugFunctions; |
| 171 | // getBrowser is expensive, so cache the data |
| 172 | static $browser; |
| 173 | $platform = "unknown"; |
| 174 | |
| 175 | |
| 176 | if(ini_get("browscap")) { |
| 177 | if(!isset($browser)){ |
| 178 | $browser = get_browser(null, true); |
| 179 | } |
| 180 | |
| 181 | if ($browser) { |
| 182 | $rawPlatform = $browser['platform']; |
| 183 | if ($debugFunctions) { |
| 184 | echo "browser platfrom: " . $rawPlatform . "<br />" ; |
| 185 | } |
| 186 | |
| 187 | if ($debugFunctions) { |
| 188 | $browserKeys = array_keys($browser); |
| 189 | foreach ($browserKeys as $key) { |
| 190 | echo $key . ": " . $browser[$key] . "<br />"; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | if (strpos($rawPlatform, "Win") === 0) { |
| 195 | $platform="windows"; |
| 196 | } else if (strpos($rawPlatform, "Linux") === 0) { |
| 197 | $platform="linux"; |
| 198 | } else if (strpos($rawPlatform, "Mac") === 0) { |
| 199 | $platform="mac"; |
| 200 | } |
| 201 | } |
| 202 | return $platform; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | function getPrereqReferenceOrName($eclipseMirrorScript, $mirrorPrefixuri, $prerequrl, $prereqfilename, $eclipseFSpathPrefix) { |
| 207 | // todo: we really only need "if exists" so could make a bit more efficient |
| 208 | // I tried "file_exists" but is didn't seem to work on my test server |
| 209 | // For these pre-reqs, we assume if they exist, they are mirrored. This is true |
| 210 | // 99% of the time. |
| 211 | |
| 212 | if (resourceExist($prerequrl, $mirrorPrefixuri, $prereqfilename, $eclipseFSpathPrefix)) { |
| 213 | $reflink="<a href=\"" . $eclipseMirrorScript . $mirrorPrefixuri . "/" . $prereqfilename . "\">" . $prereqfilename . "</a>"; |
| 214 | } else { |
| 215 | $reflink=$prereqfilename; |
| 216 | } |
| 217 | return $reflink; |
| 218 | } |
| 219 | ?> |