Subversion Repositories SvarDOS

Rev

Rev 719 | Rev 735 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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