Subversion Repositories SvarDOS

Rev

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

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