Subversion Repositories SvarDOS

Rev

Rev 216 | Rev 345 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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