Subversion Repositories SvarDOS

Rev

Rev 213 | Rev 323 | 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
5
  Copyright (C) 2021 Mateusz Viste
6
 
7
 === API ===
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
9
  ?a=search&p=PHRASE          list packages that match PHRASE
10
  ?a=checkup&p=PACKAGE&v=ver  check if package available in version > v
11
*/
12
 
216 mateuszvis 13
 
14
function nicesize($bytes) {
15
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
16
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
17
  return(round($bytes / 1024 / 1024, 1) . "M");
18
}
19
 
20
 
21
if (empty($_GET['a'])) {
208 mateuszvis 22
  http_response_code(404);
216 mateuszvis 23
  echo "ERROR: no action specified\r\n";
24
  exit(0);
208 mateuszvis 25
}
26
 
216 mateuszvis 27
if (empty($_GET['p'])) {
28
  http_response_code(404);
29
  echo "ERROR: no package specified\r\n";
30
  exit(0);
31
}
32
 
33
$a = strtolower($_GET['a']);
34
$p = strtolower($_GET['p']);
35
 
208 mateuszvis 36
$v = '';
37
if (!empty($_GET['v'])) $v = $_GET['v'];
38
 
216 mateuszvis 39
// pull action is easy (does not require looking into pkg list), do it now
208 mateuszvis 40
 
216 mateuszvis 41
if ($a === 'pull') {
42
  if (file_exists($p . '.zip')) {
43
    readfile($p . '.zip');
44
  } else {
45
    http_response_code(404);
46
    echo "ERROR: package not found on server\r\n";
47
  }
48
  exit(0);
49
}
50
 
51
// is action valid?
52
 
53
if (($a !== 'search') && ($a !== 'checkup')) {
54
  http_response_code(404);
55
  echo "ERROR: invalid action\r\n";
56
  exit(0);
57
}
58
 
59
// iterate over packages now
60
 
61
$handle = fopen("index.tsv", "rb");
62
if ($handle === FALSE) {
63
  http_response_code(404);
64
  echo "ERROR: Server-side internal error\r\n";
65
  exit(0);
66
}
67
 
68
if ($a === 'search') {
69
  $matches = 0;
70
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
71
    if ((stristr($pkg[0], $p)) || (stristr($pkg[2], $p))) {
72
      echo str_pad(strtoupper($pkg[0]), 12) . str_pad("ver: {$pkg[1]}", 13) . str_pad("size: " . nicesize(filesize($pkg[0] . '.zip')), 13) . "BSUM: {$pkg[3]}\r\n";
73
      echo wordwrap($pkg[2], 79, "\r\n", true);
74
      echo "\r\n\r\n";
75
      $matches++;
76
    }
77
  }
78
  if ($matches == 0) echo "No matching package found.\r\n";
79
}
80
 
81
if ($a === 'checkup') {
82
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
83
    if (strcasecmp($pkg[0], $p)) {
84
      echo "found package {$pkg[0]} ver {$pkg[1]} -> is it newer than '{$v}' ?\r\n";
85
      break;
86
    }
87
  }
88
}
89
fclose($handle);
90
 
91
 
208 mateuszvis 92
?>