Subversion Repositories SvarDOS

Rev

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

Rev 742 Rev 812
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
    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')) {
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 action
137
 
137
 
138
if ($a === 'pull') {
138
if ($a === 'pull') {
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
    header('Content-Disposition: attachment; filename="' . $fname);
152
    header('Content-Disposition: attachment; filename="' . $fname);
153
    header('Content-Type: application/octet-stream');
153
    header('Content-Type: application/octet-stream');
154
    readfile($fname);
154
    readfile($fname);
155
  } else {
155
  } else {
156
    http_response_code(404);
156
    http_response_code(404);
157
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
157
    echo get_msg('PKG_NOT_FOUND', $lang) . "\r\n";
158
  }
158
  }
159
  exit(0);
159
  exit(0);
160
}
160
}
161
 
161
 
162
 
162
 
163
// search action
163
// search action
164
 
164
 
165
if ($a === 'search') {
165
if ($a === 'search') {
166
  $matches = 0;
166
  $matches = 0;
167
  header('Content-Type: text/plain');
167
  header('Content-Type: text/plain');
168
  foreach ($db as $pkg => $meta) {
168
  foreach ($db as $pkg => $meta) {
169
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
169
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
170
      // fetch first (preferred) version
170
      // fetch first (preferred) version
171
      $prefver_fname = array_key_first($meta['versions']);
171
      $prefver_fname = array_key_first($meta['versions']);
172
      $prefver = array_shift($meta['versions']);
172
      $prefver = array_shift($meta['versions']);
173
      echo str_pad(strtoupper($pkg), 12);
173
      echo str_pad(strtoupper($pkg), 12);
174
      echo str_pad(get_msg('VER', $lang) . " {$prefver['ver']} ", 16);
174
      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";
175
      echo str_pad(get_msg('SIZE', $lang) . ' ' . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
176
 
176
 
177
      // do I have a localized version of the description?
177
      // do I have a localized version of the description?
178
      if (!empty($descdb[$pkg])) {
178
      if (!empty($descdb[$pkg])) {
179
        echo wordwrap(cp_conv($descdb[$pkg], $lang), 79, "\r\n", true);
179
        echo wordwrap(cp_conv($descdb[$pkg], $lang), 79, "\r\n", true);
180
      } else {
180
      } else {
181
        echo wordwrap($meta['desc'], 79, "\r\n", true);
181
        echo wordwrap($meta['desc'], 79, "\r\n", true);
182
      }
182
      }
183
 
183
 
184
      echo "\r\n";
184
      echo "\r\n";
185
      // do I have any alt versions?
185
      // do I have any alt versions?
186
      $altlist = array();
186
      $altlist = array();
187
      foreach ($meta['versions'] as $altver) {
187
      foreach ($meta['versions'] as $altver) {
188
        $altlist[] = $pkg . '-' . $altver['ver'];
188
        $altlist[] = $pkg . '-' . $altver['ver'];
189
      }
189
      }
190
      if (!empty($altlist)) {
190
      if (!empty($altlist)) {
191
        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";
192
      }
192
      }
193
      echo "\r\n";
193
      echo "\r\n";
194
      $matches++;
194
      $matches++;
195
    }
195
    }
196
  }
196
  }
197
  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";
198
}
198
}
199
 
199
 
200
 
200
 
201
// checkup action
201
// checkup action
202
 
202
 
203
if ($a === 'checkup') {
203
if ($a === 'checkup') {
204
  $found = 0;
204
  $found = 0;
205
  $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
206
 
206
 
207
  foreach ($remote_pkgs as $rpkg) {
207
  foreach ($remote_pkgs as $rpkg) {
208
    $rpkg[0] = strtolower($rpkg[0]);
208
    $rpkg[0] = strtolower($rpkg[0]);
209
    if (empty($db[$rpkg[0]])) continue;
209
    if (empty($db[$rpkg[0]])) continue;
210
 
210
 
211
    $dbpkg = $db[$rpkg[0]];
211
    $dbpkg = $db[$rpkg[0]];
212
    // compare user's version with preferred version in repo
212
    // compare user's version with preferred version in repo
213
    $prefver = array_shift($dbpkg['versions']);
213
    $prefver = array_shift($dbpkg['versions']);
214
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
214
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
215
    // found version mismatch
215
    // found version mismatch
216
    if ($found == 0) {
216
    if ($found == 0) {
217
      echo str_pad('', 78, '-') . "\r\n";
217
      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));
218
      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);
219
      tabulprint(array('----------', '--------------------------------', '--------------------------------'), array(10, 32, 32), false);
220
    }
220
    }
221
    $found++;
221
    $found++;
222
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 30, 30));
222
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 30, 30));
223
  }
223
  }
224
  if ($found == 0) {
224
  if ($found == 0) {
225
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
225
    echo get_msg('NO_UPDATES', $lang) . "\r\n";
226
  } else {
226
  } else {
227
    echo str_pad('', 78, '-') . "\r\n";
227
    echo str_pad('', 78, '-') . "\r\n";
228
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
228
    echo get_msg('FOUND_DIFFER', $lang) . ' ' . $found . "\r\n";
229
  }
229
  }
230
}
230
}
231
 
231
 
232
?>
232
?>
233
 
233