Subversion Repositories SvarDOS

Rev

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

Rev 719 Rev 722
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 (svp) containing PACKAGE
8
  ?a=pull&p=PACKAGE           downloads the zip archive (svp) containing PACKAGE
9
  ?a=pull&p=PACKAGE-VER       downloads the zip (svp) 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
// messages in different languages (and necessary mappings for codepage conversions)
-
 
16
include 'lang.php';
-
 
17
 
-
 
18
 
-
 
19
// convert a utf-8 string into codepage related to lang
-
 
20
function cp_conv($s, $lang) {
-
 
21
  global $CP_UTF, $CP_ENC;
-
 
22
  $res = str_replace($CP_UTF[$lang], $CP_ENC[$lang], $s);
-
 
23
  return($res);
-
 
24
}
-
 
25
 
-
 
26
 
-
 
27
function get_msg($id, $lang) {
-
 
28
  global $MSG;
-
 
29
  if (!empty($MSG[$id][$lang])) {
-
 
30
    return cp_conv($MSG[$id][$lang], $lang);
-
 
31
  } else {
-
 
32
    echo $MSG[$id]['en'];
-
 
33
  }
-
 
34
}
-
 
35
 
-
 
36
 
15
function nicesize($bytes) {
37
function nicesize($bytes) {
16
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
38
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
17
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
39
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
18
  return(round($bytes / 1024 / 1024, 1) . "M");
40
  return(round($bytes / 1024 / 1024, 1) . "M");
19
}
41
}
20
 
42
 
21
 
43
 
22
function csv_to_array($filename = '', $delimiter = "\t") {
44
function csv_to_array($filename = '', $delimiter = "\t") {
23
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
45
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
24
  $handle = fopen($filename, 'r');
46
  $handle = fopen($filename, 'r');
25
  if ($handle === false) return(false);
47
  if ($handle === false) return(false);
26
 
48
 
27
  $rez = array();
49
  $rez = array();
28
 
50
 
29
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
51
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
30
    $rez[] = $row;
52
    $rez[] = $row;
31
  }
53
  }
32
  fclose($handle);
54
  fclose($handle);
33
 
55
 
34
  return $rez;
56
  return $rez;
35
}
57
}
36
 
58
 
37
 
59
 
38
function tabulprint($ar_data, $ar_width, $margin = true) {
60
function tabulprint($ar_data, $ar_width, $margin = true) {
39
  $count = 0;
61
  $count = 0;
40
  foreach ($ar_data as $item) {
62
  foreach ($ar_data as $item) {
41
    if ($count == 0) {
63
    if ($count == 0) {
42
      echo '|';
64
      echo '|';
43
      if ($margin) echo ' ';
65
      if ($margin) echo ' ';
44
    }
66
    }
45
    echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]);
67
    echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]);
46
    if ($margin) {
68
    if ($margin) {
47
      echo ' | ';
69
      echo ' | ';
48
    } else {
70
    } else {
49
      echo '|';
71
      echo '|';
50
    }
72
    }
51
    $count++;
73
    $count++;
52
  }
74
  }
53
  echo "\r\n";
75
  echo "\r\n";
54
}
76
}
55
 
77
 
56
 
78
 
57
// *** MAIN START ************************************************************
79
// *** MAIN START ************************************************************
58
 
80
 
59
 
81
 
60
if (empty($_GET['a'])) {
82
if (empty($_GET['a'])) {
61
  http_response_code(404);
83
  http_response_code(404);
62
  echo "ERROR: no action specified\r\n";
84
  echo "ERROR: no action specified\r\n";
63
  exit(0);
85
  exit(0);
64
}
86
}
65
 
87
 
-
 
88
$lang = 'en';
-
 
89
if (!empty($_GET['lang'])) $lang = strtolower($_GET['lang']);
-
 
90
 
66
// switch to the packages directory
91
// switch to the packages directory
67
if (chdir('../../packages') === false) {
92
if (chdir('../../packages') === false) {
68
  http_response_code(404);
93
  http_response_code(404);
69
  echo "ERROR: server-side error, cannot access packages\r\n";
94
  echo "ERROR: server-side error, cannot access packages\r\n";
70
  exit(0);
95
  exit(0);
71
}
96
}
72
 
97
 
73
$a = strtolower($_GET['a']);
98
$a = strtolower($_GET['a']);
74
 
99
 
75
$p = '';
100
$p = '';
76
$v = '';
101
$v = '';
77
if ($a != 'checkup') {
102
if ($a != 'checkup') {
78
  if (empty($_GET['p'])) {
103
  if (empty($_GET['p'])) {
79
    http_response_code(404);
104
    http_response_code(404);
80
    echo "ERROR: no package specified\r\n";
105
    echo "ERROR: no package specified\r\n";
81
    exit(0);
106
    exit(0);
82
  }
107
  }
83
  $pv = explode('-', strtolower($_GET['p']));
108
  $pv = explode('-', strtolower($_GET['p']));
84
  $p = $pv[0];
109
  $p = $pv[0];
85
  if (!empty($pv[1])) $v = $pv[1];
110
  if (!empty($pv[1])) $v = $pv[1];
86
}
111
}
87
 
112
 
88
 
113
 
89
// is action valid?
114
// is action valid?
90
 
115
 
91
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
116
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
92
  http_response_code(404);
117
  http_response_code(404);
93
  echo "ERROR: invalid action\r\n";
118
  echo "ERROR: invalid action\r\n";
94
  exit(0);
119
  exit(0);
95
}
120
}
96
 
121
 
97
 
122
 
98
// load pkg db
123
// load pkg db
99
 
124
 
100
$db = json_decode(file_get_contents('_index.json'), true);
125
$db = json_decode(file_get_contents('_index.json'), true);
101
if (empty($db)) {
126
if (empty($db)) {
102
  http_response_code(404);
127
  http_response_code(404);
103
  echo "ERROR: server error, database not found\n";
128
  echo "ERROR: server error, database not found\n";
104
  exit(0);
129
  exit(0);
105
}
130
}
106
 
131
 
107
 
132
 
108
// pull action
133
// pull action
109
 
134
 
110
if ($a === 'pull') {
135
if ($a === 'pull') {
111
  $fname = false;
136
  $fname = false;
112
  if (empty($v)) { // take first version (that's the preferred one)
137
  if (empty($v)) { // take first version (that's the preferred one)
113
    $fname = array_key_first($db[$p]['versions']);
138
    $fname = array_key_first($db[$p]['versions']);
114
  } else {
139
  } else {
115
    // look for a specific version string
140
    // look for a specific version string
116
    foreach ($db[$p]['versions'] as $f => $e) {
141
    foreach ($db[$p]['versions'] as $f => $e) {
117
      if (strcasecmp($e['ver'], $v) == 0) {
142
      if (strcasecmp($e['ver'], $v) == 0) {
118
        $fname = $f;
143
        $fname = $f;
119
        break;
144
        break;
120
      }
145
      }
121
    }
146
    }
122
  }
147
  }
123
  if (file_exists($fname)) {
148
  if (file_exists($fname)) {
124
    header('Content-Disposition: attachment; filename="' . $fname);
149
    header('Content-Disposition: attachment; filename="' . $fname);
125
    header('Content-Type: application/octet-stream');
150
    header('Content-Type: application/octet-stream');
126
    readfile($fname);
151
    readfile($fname);
127
  } else {
152
  } else {
128
    http_response_code(404);
153
    http_response_code(404);
129
    echo "ERROR: package not found on server\r\n";
154
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
130
  }
155
  }
131
  exit(0);
156
  exit(0);
132
}
157
}
133
 
158
 
134
 
159
 
135
// search action
160
// search action
136
 
161
 
137
if ($a === 'search') {
162
if ($a === 'search') {
138
  $matches = 0;
163
  $matches = 0;
-
 
164
  header('Content-Type: text/plain');
139
  foreach ($db as $pkg => $meta) {
165
  foreach ($db as $pkg => $meta) {
140
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
166
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
141
      // fetch first (preferred) version
167
      // fetch first (preferred) version
142
      $prefver_fname = array_key_first($meta['versions']);
168
      $prefver_fname = array_key_first($meta['versions']);
143
      $prefver = array_shift($meta['versions']);
169
      $prefver = array_shift($meta['versions']);
144
      echo str_pad(strtoupper($pkg), 12) . str_pad("ver: {$prefver['ver']} ", 16) . str_pad("size: " . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
170
      echo str_pad(strtoupper($pkg), 12) . str_pad(get_msg('VER', $lang) . " {$prefver['ver']} ", 16) . str_pad(get_msg('SIZE', $lang) . ' ' . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
145
      echo wordwrap($meta['desc'], 79, "\r\n", true);
171
      echo wordwrap($meta['desc'], 79, "\r\n", true);
146
      echo "\r\n";
172
      echo "\r\n";
147
      // do I have any alt versions?
173
      // do I have any alt versions?
148
      $altlist = array();
174
      $altlist = array();
149
      foreach ($meta['versions'] as $altver) {
175
      foreach ($meta['versions'] as $altver) {
150
        $altlist[] = $pkg . '-' . $altver['ver'];
176
        $altlist[] = $pkg . '-' . $altver['ver'];
151
      }
177
      }
152
      if (!empty($altlist)) {
178
      if (!empty($altlist)) {
153
        echo wordwrap("[alt versions: " . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
179
        echo wordwrap('[' . get_msg('ALT_VERS', $lang) . ' ' . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
154
      }
180
      }
155
      echo "\r\n";
181
      echo "\r\n";
156
      $matches++;
182
      $matches++;
157
    }
183
    }
158
  }
184
  }
159
  if ($matches == 0) echo "No matching package found.\r\n";
185
  if ($matches == 0) echo get_msg('NO_MATCHING_PKG', $lang) . "\r\n";
160
}
186
}
161
 
187
 
162
 
188
 
163
// checkup action
189
// checkup action
164
 
190
 
165
if ($a === 'checkup') {
191
if ($a === 'checkup') {
166
  $found = 0;
192
  $found = 0;
167
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
193
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
168
 
194
 
169
  foreach ($remote_pkgs as $rpkg) {
195
  foreach ($remote_pkgs as $rpkg) {
170
    $rpkg[0] = strtolower($rpkg[0]);
196
    $rpkg[0] = strtolower($rpkg[0]);
171
    if (empty($db[$rpkg[0]])) continue;
197
    if (empty($db[$rpkg[0]])) continue;
172
 
198
 
173
    $dbpkg = $db[$rpkg[0]];
199
    $dbpkg = $db[$rpkg[0]];
174
    // compare user's version with preferred version in repo
200
    // compare user's version with preferred version in repo
175
    $prefver = array_shift($dbpkg['versions']);
201
    $prefver = array_shift($dbpkg['versions']);
176
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
202
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
177
    // found version mismatch
203
    // found version mismatch
178
    if ($found == 0) {
204
    if ($found == 0) {
179
      echo str_pad('', 58, '-') . "\r\n";
205
      echo str_pad('', 58, '-') . "\r\n";
180
      tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20));
206
      tabulprint(array(get_msg('PACKAGE', $lang), get_msg('INSTALLED', $lang), get_msg('AVAILABLE', $lang)), array(8, 20, 20));
181
      tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
207
      tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
182
    }
208
    }
183
    $found++;
209
    $found++;
184
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20));
210
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20));
185
  }
211
  }
186
  if ($found == 0) {
212
  if ($found == 0) {
187
    echo "no available updates\r\n";
213
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
188
  } else {
214
  } else {
189
    echo str_pad('', 58, '-') . "\r\n";
215
    echo str_pad('', 58, '-') . "\r\n";
190
    echo "found {$found} differing package(s)\r\n";
216
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
191
  }
217
  }
192
}
218
}
193
 
219
 
194
?>
220
?>
195
 
221