aboutsummaryrefslogtreecommitdiffstats
path: root/src/mvncache.php
blob: 7abb76555035233b9dfc31c6db12009840aa4eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

include_once(__DIR__.'/lib/functions.http.inc.php');
include_once(__DIR__.'/lib/error.handler.inc.php');
// Load configuration
$config = require(__DIR__.'/config/config.inc.php');

initTheDevStackErrorHandler($config["logfile"]);

$requestedArtifact = $_SERVER["REQUEST_URI"];
$requestedArtifact = str_replace('/mvn', '', $requestedArtifact);
$baseLocalFolder = __DIR__.'/..';
$localFolder = $baseLocalFolder.substr($requestedArtifact, 0, strripos($requestedArtifact, '/'));

if ('' == $requestedArtifact) { // no artifact is requested - return
  sendHttpReturnCodeAndMessage(403, 'Not allowed');
}

$tempFile = tmpfile();
foreach ($config['mavenBaseUrls'] as $mvnBaseUrl) {
  $srcUrl = $mvnBaseUrl.$requestedArtifact;
  //echo $srcUrl."<br>";
  $src = fopen($srcUrl, 'r');
  $found = FALSE !== $src;
  
  if ($found) {
    $filesize = @stream_copy_to_stream($src, $tempFile);
    $found = 0 < $filesize;
  }
  if ($found) {
    trigger_error("Artifact found at $srcUrl", E_USER_NOTICE);
    break;
  } else {
    trigger_error("Artifact NOT found at $srcUrl", E_USER_WARNING);
  }
}

if (!$found) {
  trigger_error("Artifact NOT found at any source", E_USER_WARNING);
  sendHttpReturnCodeAndMessage(404, 'Not found');
} else {
  //echo $localFolder."<br>";
  if (!is_dir($localFolder)) {
    mkdir($localFolder, 0770, true);
  }
  $dstPath = $baseLocalFolder.$requestedArtifact;
  //echo $dstPath;
  $dst = fopen($dstPath, 'w');
  fseek($tempFile, 0);
  stream_copy_to_stream($tempFile, $dst);
  if (is_file($dstPath)) {
    chmod($dstPath, 0660);
    header('Location: '.getServerProtocol().'://'.getRequestHostname().$_SERVER["REQUEST_URI"]);
  } else {
    header('HTTP/1.0 404 Not Found');
  }
}
?>