Subversion Repositories SvarDOS

Rev

Rev 345 | Rev 560 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 mateuszvis 1
<?php
2
 
3
/*
4
  pkgnet interface
559 mateuszvis 5
  Copyright (C) 2021-2022 Mateusz Viste
208 mateuszvis 6
 
7
 === API ===
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
559 mateuszvis 9
  ?a=pull&p=PACKAGE-VER       downloads the zip containing PACKAGE in version VER
208 mateuszvis 10
  ?a=search&p=PHRASE          list packages that match PHRASE
345 mateuszvis 11
  ?a=checkup                  list of packages+versions in $_POST
208 mateuszvis 12
*/
13
 
216 mateuszvis 14
 
15
function nicesize($bytes) {
16
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
17
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
18
  return(round($bytes / 1024 / 1024, 1) . "M");
19
}
20
 
21
 
345 mateuszvis 22
function csv_to_array($filename = '', $delimiter = "\t") {
23
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
24
  $handle = fopen($filename, 'r');
25
  if ($handle === false) return(false);
26
 
27
  $rez = array();
28
 
29
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
30
    $rez[] = $row;
31
  }
32
  fclose($handle);
33
 
34
  return $rez;
35
}
36
 
37
 
38
function tabulprint($ar_data, $ar_width, $margin = true) {
39
  $count = 0;
40
  foreach ($ar_data as $item) {
41
    if ($count == 0) {
42
      echo '|';
43
      if ($margin) echo ' ';
44
    }
45
    echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]);
46
    if ($margin) {
47
      echo ' | ';
48
    } else {
49
      echo '|';
50
    }
51
    $count++;
52
  }
53
  echo "\r\n";
54
}
55
 
56
 
57
// *** MAIN START ************************************************************
58
 
59
 
216 mateuszvis 60
if (empty($_GET['a'])) {
208 mateuszvis 61
  http_response_code(404);
216 mateuszvis 62
  echo "ERROR: no action specified\r\n";
63
  exit(0);
208 mateuszvis 64
}
65
 
345 mateuszvis 66
$a = strtolower($_GET['a']);
67
 
68
$p = '';
69
if ($a != 'checkup') {
70
  if (empty($_GET['p'])) {
71
    http_response_code(404);
72
    echo "ERROR: no package specified\r\n";
73
    exit(0);
74
  }
75
  $p = strtolower($_GET['p']);
216 mateuszvis 76
}
77
 
208 mateuszvis 78
$v = '';
79
if (!empty($_GET['v'])) $v = $_GET['v'];
80
 
216 mateuszvis 81
// pull action is easy (does not require looking into pkg list), do it now
208 mateuszvis 82
 
216 mateuszvis 83
if ($a === 'pull') {
84
  if (file_exists($p . '.zip')) {
85
    readfile($p . '.zip');
86
  } else {
87
    http_response_code(404);
88
    echo "ERROR: package not found on server\r\n";
89
  }
90
  exit(0);
91
}
92
 
93
// is action valid?
94
 
95
if (($a !== 'search') && ($a !== 'checkup')) {
96
  http_response_code(404);
97
  echo "ERROR: invalid action\r\n";
98
  exit(0);
99
}
100
 
101
// iterate over packages now
102
 
103
$handle = fopen("index.tsv", "rb");
104
if ($handle === FALSE) {
105
  http_response_code(404);
106
  echo "ERROR: Server-side internal error\r\n";
107
  exit(0);
108
}
109
 
110
if ($a === 'search') {
111
  $matches = 0;
112
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
113
    if ((stristr($pkg[0], $p)) || (stristr($pkg[2], $p))) {
323 mateuszvis 114
      echo str_pad(strtoupper($pkg[0]), 12) . str_pad("ver: {$pkg[1]}", 13) . str_pad("size: " . nicesize(filesize($pkg[0] . '.zip')), 13) . "BSUM: " . sprintf("%04X", $pkg[3]) . "\r\n";
216 mateuszvis 115
      echo wordwrap($pkg[2], 79, "\r\n", true);
559 mateuszvis 116
      echo "\r\n";
117
      // do I have any alt versions?
118
      $altvers = glob("{$pkg[0]}-*.zip");
119
      if (!empty($altvers)) {
120
        $alts = str_replace('.zip', '', $altvers);
121
        echo wordwrap("[alt versions: " . implode(', ', $alts), 79, "\r\n", true) . "]\r\n";
122
      }
123
      echo "\r\n";
216 mateuszvis 124
      $matches++;
125
    }
126
  }
127
  if ($matches == 0) echo "No matching package found.\r\n";
128
}
129
 
130
if ($a === 'checkup') {
345 mateuszvis 131
  $found = 0;
132
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
216 mateuszvis 133
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
345 mateuszvis 134
    // is $pkg part of remote packages?
135
    foreach ($remote_pkgs as $rpkg) {
136
      if (strcasecmp($pkg[0], $rpkg[0]) != 0) continue;
137
      if ($pkg[1] === $rpkg[1]) continue; // skip same version
138
      if ($found == 0) {
139
        echo str_pad('', 58, '-') . "\r\n";
140
        tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20));
141
        tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
142
      }
143
      $found++;
144
      tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20));
216 mateuszvis 145
      break;
146
    }
147
  }
345 mateuszvis 148
  if ($found == 0) {
149
    echo "no available updates\r\n";
150
  } else {
151
    echo str_pad('', 58, '-') . "\r\n";
152
    echo "found {$found} differing packages\r\n";
153
  }
216 mateuszvis 154
}
155
fclose($handle);
156
 
157
 
208 mateuszvis 158
?>