Subversion Repositories SvarDOS

Rev

Rev 1508 | Details | Compare with Previous | Last modification | View Log | RSS feed

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