Subversion Repositories SvarDOS

Rev

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

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