Subversion Repositories SvarDOS

Rev

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

Rev 812 Rev 874
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)
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
  $pv = explode('-', strtolower($_GET['p']));
111
  $pv = explode('-', strtolower($_GET['p']));
112
  $p = $pv[0];
112
  $p = $pv[0];
113
  if (!empty($pv[1])) $v = $pv[1];
113
  if (!empty($pv[1])) $v = $pv[1];
114
}
114
}
115
 
115
 
116
 
116
 
117
// is action valid?
117
// is action valid?
118
 
118
 
119
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
119
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull') && ($a !== 'pullsrc')) {
120
  http_response_code(404);
120
  http_response_code(404);
121
  echo "ERROR: invalid action\r\n";
121
  echo "ERROR: invalid action\r\n";
122
  exit(0);
122
  exit(0);
123
}
123
}
124
 
124
 
125
 
125
 
126
// load pkg db
126
// load pkg db
127
 
127
 
128
$db = json_decode(file_get_contents('_index.json'), true);
128
$db = json_decode(file_get_contents('_index.json'), true);
129
if (empty($db)) {
129
if (empty($db)) {
130
  http_response_code(404);
130
  http_response_code(404);
131
  echo "ERROR: server error, database not found\n";
131
  echo "ERROR: server error, database not found\n";
132
  exit(0);
132
  exit(0);
133
}
133
}
134
 
134
 
135
 
135
 
136
// pull action
136
// pull or pullsrc action (svp / zip)
137
 
137
 
138
if ($a === 'pull') {
138
if (($a === 'pull') || ($a === 'pullsrc')) {
139
  $fname = false;
139
  $fname = false;
140
  if (empty($v)) { // take first version (that's the preferred one)
140
  if (empty($v)) { // take first version (that's the preferred one)
141
    $fname = array_key_first($db[$p]['versions']);
141
    $fname = array_key_first($db[$p]['versions']);
142
  } else {
142
  } else {
143
    // look for a specific version string
143
    // look for a specific version string
144
    foreach ($db[$p]['versions'] as $f => $e) {
144
    foreach ($db[$p]['versions'] as $f => $e) {
145
      if (strcasecmp($e['ver'], $v) == 0) {
145
      if (strcasecmp($e['ver'], $v) == 0) {
146
        $fname = $f;
146
        $fname = $f;
147
        break;
147
        break;
148
      }
148
      }
149
    }
149
    }
150
  }
150
  }
151
  if (file_exists($fname)) {
151
  if (file_exists($fname)) {
-
 
152
    if ($a === 'pullsrc') { // is it about source?
-
 
153
      $fname = preg_replace('/svp$/', 'zip', $fname); // replace *.svp by *.zip
-
 
154
      if (file_exists($fname)) {
-
 
155
        header('Content-Disposition: attachment; filename="' . $fname);
-
 
156
        header('Content-Type: application/octet-stream');
-
 
157
        readfile($fname);
-
 
158
      } else {
-
 
159
        http_response_code(404);
-
 
160
        echo get_msg('PKG_NO_SRC', $lang) . "\r\n";
-
 
161
      }
-
 
162
    } else {
152
    header('Content-Disposition: attachment; filename="' . $fname);
163
      header('Content-Disposition: attachment; filename="' . $fname);
153
    header('Content-Type: application/octet-stream');
164
      header('Content-Type: application/octet-stream');
154
    readfile($fname);
165
      readfile($fname);
-
 
166
    }
155
  } else {
167
  } else {
156
    http_response_code(404);
168
    http_response_code(404);
157
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
169
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
158
  }
170
  }
159
  exit(0);
171
  exit(0);
160
}
172
}
161
 
173
 
162
 
174
 
163
// search action
175
// search action
164
 
176
 
165
if ($a === 'search') {
177
if ($a === 'search') {
166
  $matches = 0;
178
  $matches = 0;
167
  header('Content-Type: text/plain');
179
  header('Content-Type: text/plain');
168
  foreach ($db as $pkg => $meta) {
180
  foreach ($db as $pkg => $meta) {
169
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
181
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
170
      // fetch first (preferred) version
182
      // fetch first (preferred) version
171
      $prefver_fname = array_key_first($meta['versions']);
183
      $prefver_fname = array_key_first($meta['versions']);
172
      $prefver = array_shift($meta['versions']);
184
      $prefver = array_shift($meta['versions']);
173
      echo str_pad(strtoupper($pkg), 12);
185
      echo str_pad(strtoupper($pkg), 12);
174
      echo str_pad(get_msg('VER', $lang) . " {$prefver['ver']} ", 16);
186
      echo str_pad(get_msg('VER', $lang) . " {$prefver['ver']} ", 16);
175
      echo str_pad(get_msg('SIZE', $lang) . ' ' . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
187
      echo str_pad(get_msg('SIZE', $lang) . ' ' . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
176
 
188
 
177
      // do I have a localized version of the description?
189
      // do I have a localized version of the description?
178
      if (!empty($descdb[$pkg])) {
190
      if (!empty($descdb[$pkg])) {
179
        echo wordwrap(cp_conv($descdb[$pkg], $lang), 79, "\r\n", true);
191
        echo wordwrap(cp_conv($descdb[$pkg], $lang), 79, "\r\n", true);
180
      } else {
192
      } else {
181
        echo wordwrap($meta['desc'], 79, "\r\n", true);
193
        echo wordwrap($meta['desc'], 79, "\r\n", true);
182
      }
194
      }
183
 
195
 
184
      echo "\r\n";
196
      echo "\r\n";
185
      // do I have any alt versions?
197
      // do I have any alt versions?
186
      $altlist = array();
198
      $altlist = array();
187
      foreach ($meta['versions'] as $altver) {
199
      foreach ($meta['versions'] as $altver) {
188
        $altlist[] = $pkg . '-' . $altver['ver'];
200
        $altlist[] = $pkg . '-' . $altver['ver'];
189
      }
201
      }
190
      if (!empty($altlist)) {
202
      if (!empty($altlist)) {
191
        echo wordwrap('[' . get_msg('ALT_VERS', $lang) . ' ' . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
203
        echo wordwrap('[' . get_msg('ALT_VERS', $lang) . ' ' . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
192
      }
204
      }
193
      echo "\r\n";
205
      echo "\r\n";
194
      $matches++;
206
      $matches++;
195
    }
207
    }
196
  }
208
  }
197
  if ($matches == 0) echo get_msg('NO_MATCHING_PKG', $lang) . "\r\n";
209
  if ($matches == 0) echo get_msg('NO_MATCHING_PKG', $lang) . "\r\n";
198
}
210
}
199
 
211
 
200
 
212
 
201
// checkup action
213
// checkup action
202
 
214
 
203
if ($a === 'checkup') {
215
if ($a === 'checkup') {
204
  $found = 0;
216
  $found = 0;
205
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
217
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
206
 
218
 
207
  foreach ($remote_pkgs as $rpkg) {
219
  foreach ($remote_pkgs as $rpkg) {
208
    $rpkg[0] = strtolower($rpkg[0]);
220
    $rpkg[0] = strtolower($rpkg[0]);
209
    if (empty($db[$rpkg[0]])) continue;
221
    if (empty($db[$rpkg[0]])) continue;
210
 
222
 
211
    $dbpkg = $db[$rpkg[0]];
223
    $dbpkg = $db[$rpkg[0]];
212
    // compare user's version with preferred version in repo
224
    // compare user's version with preferred version in repo
213
    $prefver = array_shift($dbpkg['versions']);
225
    $prefver = array_shift($dbpkg['versions']);
214
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
226
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
215
    // found version mismatch
227
    // found version mismatch
216
    if ($found == 0) {
228
    if ($found == 0) {
217
      echo str_pad('', 78, '-') . "\r\n";
229
      echo str_pad('', 78, '-') . "\r\n";
218
      tabulprint(array(get_msg('PACKAGE', $lang), get_msg('INSTALLED', $lang), get_msg('AVAILABLE', $lang)), array(8, 30, 30));
230
      tabulprint(array(get_msg('PACKAGE', $lang), get_msg('INSTALLED', $lang), get_msg('AVAILABLE', $lang)), array(8, 30, 30));
219
      tabulprint(array('----------', '--------------------------------', '--------------------------------'), array(10, 32, 32), false);
231
      tabulprint(array('----------', '--------------------------------', '--------------------------------'), array(10, 32, 32), false);
220
    }
232
    }
221
    $found++;
233
    $found++;
222
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 30, 30));
234
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 30, 30));
223
  }
235
  }
224
  if ($found == 0) {
236
  if ($found == 0) {
225
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
237
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
226
  } else {
238
  } else {
227
    echo str_pad('', 78, '-') . "\r\n";
239
    echo str_pad('', 78, '-') . "\r\n";
228
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
240
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
229
  }
241
  }
230
}
242
}
231
 
243
 
232
?>
244
?>
233
 
245