Subversion Repositories SvarDOS

Rev

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

Rev 722 Rev 735
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
    echo $MSG[$id]['en'];
32
    echo $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'])) $lang = strtolower($_GET['lang']);
89
if ((!empty($_GET['lang'])) && (preg_match('/[a-zA-Z][a-zA-Z]/', $_GET['lang']))) $lang = strtolower($_GET['lang']);
-
 
90
 
-
 
91
// load pkg desc translations
-
 
92
$descdb = json_decode(file_get_contents("pkg_desc_{$lang}.json"), true);
90
 
93
 
91
// switch to the packages directory
94
// switch to the packages directory
92
if (chdir('../../packages') === false) {
95
if (chdir('../../packages') === false) {
93
  http_response_code(404);
96
  http_response_code(404);
94
  echo "ERROR: server-side error, cannot access packages\r\n";
97
  echo "ERROR: server-side error, cannot access packages\r\n";
95
  exit(0);
98
  exit(0);
96
}
99
}
97
 
100
 
98
$a = strtolower($_GET['a']);
101
$a = strtolower($_GET['a']);
99
 
102
 
100
$p = '';
103
$p = '';
101
$v = '';
104
$v = '';
102
if ($a != 'checkup') {
105
if ($a != 'checkup') {
103
  if (empty($_GET['p'])) {
106
  if (empty($_GET['p'])) {
104
    http_response_code(404);
107
    http_response_code(404);
105
    echo "ERROR: no package specified\r\n";
108
    echo "ERROR: no package specified\r\n";
106
    exit(0);
109
    exit(0);
107
  }
110
  }
108
  $pv = explode('-', strtolower($_GET['p']));
111
  $pv = explode('-', strtolower($_GET['p']));
109
  $p = $pv[0];
112
  $p = $pv[0];
110
  if (!empty($pv[1])) $v = $pv[1];
113
  if (!empty($pv[1])) $v = $pv[1];
111
}
114
}
112
 
115
 
113
 
116
 
114
// is action valid?
117
// is action valid?
115
 
118
 
116
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
119
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
117
  http_response_code(404);
120
  http_response_code(404);
118
  echo "ERROR: invalid action\r\n";
121
  echo "ERROR: invalid action\r\n";
119
  exit(0);
122
  exit(0);
120
}
123
}
121
 
124
 
122
 
125
 
123
// load pkg db
126
// load pkg db
124
 
127
 
125
$db = json_decode(file_get_contents('_index.json'), true);
128
$db = json_decode(file_get_contents('_index.json'), true);
126
if (empty($db)) {
129
if (empty($db)) {
127
  http_response_code(404);
130
  http_response_code(404);
128
  echo "ERROR: server error, database not found\n";
131
  echo "ERROR: server error, database not found\n";
129
  exit(0);
132
  exit(0);
130
}
133
}
131
 
134
 
132
 
135
 
133
// pull action
136
// pull action
134
 
137
 
135
if ($a === 'pull') {
138
if ($a === 'pull') {
136
  $fname = false;
139
  $fname = false;
137
  if (empty($v)) { // take first version (that's the preferred one)
140
  if (empty($v)) { // take first version (that's the preferred one)
138
    $fname = array_key_first($db[$p]['versions']);
141
    $fname = array_key_first($db[$p]['versions']);
139
  } else {
142
  } else {
140
    // look for a specific version string
143
    // look for a specific version string
141
    foreach ($db[$p]['versions'] as $f => $e) {
144
    foreach ($db[$p]['versions'] as $f => $e) {
142
      if (strcasecmp($e['ver'], $v) == 0) {
145
      if (strcasecmp($e['ver'], $v) == 0) {
143
        $fname = $f;
146
        $fname = $f;
144
        break;
147
        break;
145
      }
148
      }
146
    }
149
    }
147
  }
150
  }
148
  if (file_exists($fname)) {
151
  if (file_exists($fname)) {
149
    header('Content-Disposition: attachment; filename="' . $fname);
152
    header('Content-Disposition: attachment; filename="' . $fname);
150
    header('Content-Type: application/octet-stream');
153
    header('Content-Type: application/octet-stream');
151
    readfile($fname);
154
    readfile($fname);
152
  } else {
155
  } else {
153
    http_response_code(404);
156
    http_response_code(404);
154
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
157
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
155
  }
158
  }
156
  exit(0);
159
  exit(0);
157
}
160
}
158
 
161
 
159
 
162
 
160
// search action
163
// search action
161
 
164
 
162
if ($a === 'search') {
165
if ($a === 'search') {
163
  $matches = 0;
166
  $matches = 0;
164
  header('Content-Type: text/plain');
167
  header('Content-Type: text/plain');
165
  foreach ($db as $pkg => $meta) {
168
  foreach ($db as $pkg => $meta) {
166
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
169
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
167
      // fetch first (preferred) version
170
      // fetch first (preferred) version
168
      $prefver_fname = array_key_first($meta['versions']);
171
      $prefver_fname = array_key_first($meta['versions']);
169
      $prefver = array_shift($meta['versions']);
172
      $prefver = array_shift($meta['versions']);
-
 
173
      echo str_pad(strtoupper($pkg), 12);
-
 
174
      echo str_pad(get_msg('VER', $lang) . " {$prefver['ver']} ", 16);
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";
175
      echo str_pad(get_msg('SIZE', $lang) . ' ' . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
-
 
176
 
-
 
177
      // do I have a localized version of the description?
-
 
178
      if (!empty($descdb[$pkg])) {
-
 
179
        echo wordwrap(cp_conv($descdb[$pkg], $lang), 79, "\r\n", true);
-
 
180
      } else {
171
      echo wordwrap($meta['desc'], 79, "\r\n", true);
181
        echo wordwrap($meta['desc'], 79, "\r\n", true);
-
 
182
      }
-
 
183
 
172
      echo "\r\n";
184
      echo "\r\n";
173
      // do I have any alt versions?
185
      // do I have any alt versions?
174
      $altlist = array();
186
      $altlist = array();
175
      foreach ($meta['versions'] as $altver) {
187
      foreach ($meta['versions'] as $altver) {
176
        $altlist[] = $pkg . '-' . $altver['ver'];
188
        $altlist[] = $pkg . '-' . $altver['ver'];
177
      }
189
      }
178
      if (!empty($altlist)) {
190
      if (!empty($altlist)) {
179
        echo wordwrap('[' . get_msg('ALT_VERS', $lang) . ' ' . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
191
        echo wordwrap('[' . get_msg('ALT_VERS', $lang) . ' ' . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
180
      }
192
      }
181
      echo "\r\n";
193
      echo "\r\n";
182
      $matches++;
194
      $matches++;
183
    }
195
    }
184
  }
196
  }
185
  if ($matches == 0) echo get_msg('NO_MATCHING_PKG', $lang) . "\r\n";
197
  if ($matches == 0) echo get_msg('NO_MATCHING_PKG', $lang) . "\r\n";
186
}
198
}
187
 
199
 
188
 
200
 
189
// checkup action
201
// checkup action
190
 
202
 
191
if ($a === 'checkup') {
203
if ($a === 'checkup') {
192
  $found = 0;
204
  $found = 0;
193
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
205
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
194
 
206
 
195
  foreach ($remote_pkgs as $rpkg) {
207
  foreach ($remote_pkgs as $rpkg) {
196
    $rpkg[0] = strtolower($rpkg[0]);
208
    $rpkg[0] = strtolower($rpkg[0]);
197
    if (empty($db[$rpkg[0]])) continue;
209
    if (empty($db[$rpkg[0]])) continue;
198
 
210
 
199
    $dbpkg = $db[$rpkg[0]];
211
    $dbpkg = $db[$rpkg[0]];
200
    // compare user's version with preferred version in repo
212
    // compare user's version with preferred version in repo
201
    $prefver = array_shift($dbpkg['versions']);
213
    $prefver = array_shift($dbpkg['versions']);
202
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
214
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
203
    // found version mismatch
215
    // found version mismatch
204
    if ($found == 0) {
216
    if ($found == 0) {
205
      echo str_pad('', 58, '-') . "\r\n";
217
      echo str_pad('', 58, '-') . "\r\n";
206
      tabulprint(array(get_msg('PACKAGE', $lang), get_msg('INSTALLED', $lang), get_msg('AVAILABLE', $lang)), array(8, 20, 20));
218
      tabulprint(array(get_msg('PACKAGE', $lang), get_msg('INSTALLED', $lang), get_msg('AVAILABLE', $lang)), array(8, 20, 20));
207
      tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
219
      tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
208
    }
220
    }
209
    $found++;
221
    $found++;
210
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20));
222
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20));
211
  }
223
  }
212
  if ($found == 0) {
224
  if ($found == 0) {
213
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
225
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
214
  } else {
226
  } else {
215
    echo str_pad('', 58, '-') . "\r\n";
227
    echo str_pad('', 58, '-') . "\r\n";
216
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
228
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
217
  }
229
  }
218
}
230
}
219
 
231
 
220
?>
232
?>
221
 
233