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