Subversion Repositories SvarDOS

Rev

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

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