summaryrefslogtreecommitdiff
path: root/mirror.php
blob: 763bf47157dc7ac66763194b6c306243a9e5836d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

  function myBaseEncode(&$item, $key) {
    $item = base64_encode($item);
    if ($item == "")
      $item = "_";
  }

  function myBaseDecode(&$item, $key) {
    if ($item == "_")
      $item = "";
    $item = base64_decode($item);
  }

  $quelle = $_SERVER["REQUEST_URI"];
  $wurzel = dirname(__FILE__)."/";
  $datenWurzel = $wurzel."daten/";

  $ea = fopen($wurzel."erlaubteAdressen","r");
  while (! feof($ea)) {
    $zeile = trim(fgets($ea));
    if ((strpos($zeile,"x:")!==false) &&
      (strpos($_SERVER["SERVER_ADDR"],":")!==false)) {
      $zeile = explode(":",$zeile);
      $my_addr = explode(":",$_SERVER["SERVER_ADDR"]);
      foreach ($zeile as $key => $val)
        if ($val=="x")
          $zeile[$key]=$my_addr[$key];
      $zeile = implode(":",$zeile);
    }
    if ($zeile == $_SERVER["REMOTE_ADDR"])
      break;
  }
  if ($zeile != $_SERVER["REMOTE_ADDR"]) {
    header("Status: 403 Forbidden");
    print "Error 403: Access Forbidden for " . $_SERVER["REMOTE_ADDR"] . "\n";
    die();
  }

  if (substr($quelle,0,1) != "/") {
    header("Status: 404 Not Found");
    print "Error 403: Invalid path\n";
    die();
  }

  $quelle = substr($quelle,1);

  if ($quelle == "") {
    exec("find '".$datenWurzel."' -type f -not \( -name .rsync-filter -or -name CACHEDIR.TAG \) | sort",$dateien);
    foreach ($dateien as $datei) {
      $pfad = explode("/",substr($datei,strlen($datenWurzel)));
      array_walk($pfad,"myBaseDecode");
      $pfad = implode("/",$pfad);
      echo "<a href=\"file://".$datei."\">.</a> <a href=\"/".$pfad."\">".$pfad."</a><br>\n";
    }
  }
  else {
    $pfad = explode("/",$quelle);
    array_walk($pfad,"myBaseEncode");
    $pfad = implode("/",$pfad);
    $pfad = $datenWurzel.$pfad;

    $al = fopen($wurzel."access.log","a");
    if (! $al)
      die("Kann 'access.log' nicht &ouml;ffnen!");
    fwrite($al,date('Y-m-d H:i:s')." ".$pfad."\n");
    fclose($al);

    if (file_exists($pfad)) {
      $stat = stat($pfad);
      if ($stat !== false) {
        if ($stat['size'] == 0)
          unlink($pfad);
      }
    }
    if (! file_exists($pfad)) {
      passthru(
        "mkdir -p '".dirname($pfad)."'; ".
        "( ".
        "curl -Ls '".$quelle."' || ".
        "curl -Ls -A 'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0' '".$quelle."' || ".
        "wget -O - '".$quelle."' || " .
        "wget -O - 'https://crux.ster.zone/distfiles/" . basename($quelle) . "'; ".
        ") | ". // oder curl!
          "tee \"".$pfad."\""
      );
    }
    else {
      header("Content-Type: ".mime_content_type($pfad));
      header('Content-Length: ' . $stat['size']);
      $handle = fopen($pfad,"r");
      fpassthru($handle);
      fclose($handle);
    }
  }