Subversion Repositories SvarDOS

Rev

Rev 671 | Rev 722 | 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
 
15
function nicesize($bytes) {
16
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
17
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
18
  return(round($bytes / 1024 / 1024, 1) . "M");
19
}
20
 
21
 
345 mateuszvis 22
function csv_to_array($filename = '', $delimiter = "\t") {
23
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
24
  $handle = fopen($filename, 'r');
25
  if ($handle === false) return(false);
26
 
27
  $rez = array();
28
 
29
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
30
    $rez[] = $row;
31
  }
32
  fclose($handle);
33
 
34
  return $rez;
35
}
36
 
37
 
38
function tabulprint($ar_data, $ar_width, $margin = true) {
39
  $count = 0;
40
  foreach ($ar_data as $item) {
41
    if ($count == 0) {
42
      echo '|';
43
      if ($margin) echo ' ';
44
    }
45
    echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]);
46
    if ($margin) {
47
      echo ' | ';
48
    } else {
49
      echo '|';
50
    }
51
    $count++;
52
  }
53
  echo "\r\n";
54
}
55
 
56
 
57
// *** MAIN START ************************************************************
58
 
59
 
216 mateuszvis 60
if (empty($_GET['a'])) {
208 mateuszvis 61
  http_response_code(404);
216 mateuszvis 62
  echo "ERROR: no action specified\r\n";
63
  exit(0);
208 mateuszvis 64
}
65
 
631 mateusz.vi 66
// switch to the packages directory
67
if (chdir('../../packages') === false) {
68
  http_response_code(404);
69
  echo "ERROR: server-side error, cannot access packages\r\n";
70
  exit(0);
71
}
72
 
345 mateuszvis 73
$a = strtolower($_GET['a']);
74
 
75
$p = '';
719 mateusz.vi 76
$v = '';
345 mateuszvis 77
if ($a != 'checkup') {
78
  if (empty($_GET['p'])) {
79
    http_response_code(404);
80
    echo "ERROR: no package specified\r\n";
81
    exit(0);
82
  }
719 mateusz.vi 83
  $pv = explode('-', strtolower($_GET['p']));
84
  $p = $pv[0];
85
  if (!empty($pv[1])) $v = $pv[1];
216 mateuszvis 86
}
87
 
208 mateuszvis 88
 
719 mateusz.vi 89
// is action valid?
208 mateuszvis 90
 
719 mateusz.vi 91
if (($a !== 'search') && ($a !== 'checkup') && ($a !== 'pull')) {
92
  http_response_code(404);
93
  echo "ERROR: invalid action\r\n";
216 mateuszvis 94
  exit(0);
95
}
96
 
97
 
719 mateusz.vi 98
// load pkg db
99
 
100
$db = json_decode(file_get_contents('_index.json'), true);
101
if (empty($db)) {
216 mateuszvis 102
  http_response_code(404);
719 mateusz.vi 103
  echo "ERROR: server error, database not found\n";
216 mateuszvis 104
  exit(0);
105
}
106
 
107
 
719 mateusz.vi 108
// pull action
109
 
110
if ($a === 'pull') {
111
  $fname = false;
112
  if (empty($v)) { // take first version (that's the preferred one)
113
    $fname = array_key_first($db[$p]['versions']);
114
  } else {
115
    // look for a specific version string
116
    foreach ($db[$p]['versions'] as $f => $e) {
117
      if (strcasecmp($e['ver'], $v) == 0) {
118
        $fname = $f;
119
        break;
120
      }
121
    }
122
  }
123
  if (file_exists($fname)) {
124
    header('Content-Disposition: attachment; filename="' . $fname);
125
    header('Content-Type: application/octet-stream');
126
    readfile($fname);
127
  } else {
128
    http_response_code(404);
129
    echo "ERROR: package not found on server\r\n";
130
  }
216 mateuszvis 131
  exit(0);
132
}
133
 
719 mateusz.vi 134
 
135
// search action
136
 
216 mateuszvis 137
if ($a === 'search') {
138
  $matches = 0;
719 mateusz.vi 139
  foreach ($db as $pkg => $meta) {
140
    if ((stristr($pkg, $p)) || (stristr($meta['desc'], $p))) {
141
      // fetch first (preferred) version
142
      $prefver_fname = array_key_first($meta['versions']);
143
      $prefver = array_shift($meta['versions']);
144
      echo str_pad(strtoupper($pkg), 12) . str_pad("ver: {$prefver['ver']} ", 16) . str_pad("size: " . nicesize(filesize($prefver_fname)), 16) . "BSUM: " . sprintf("%04X", $prefver['bsum']) . "\r\n";
145
      echo wordwrap($meta['desc'], 79, "\r\n", true);
559 mateuszvis 146
      echo "\r\n";
147
      // do I have any alt versions?
719 mateusz.vi 148
      $altlist = array();
149
      foreach ($meta['versions'] as $altver) {
150
        $altlist[] = $pkg . '-' . $altver['ver'];
559 mateuszvis 151
      }
719 mateusz.vi 152
      if (!empty($altlist)) {
153
        echo wordwrap("[alt versions: " . implode(', ', $altlist), 79, "\r\n", true) . "]\r\n";
154
      }
559 mateuszvis 155
      echo "\r\n";
216 mateuszvis 156
      $matches++;
157
    }
158
  }
159
  if ($matches == 0) echo "No matching package found.\r\n";
160
}
161
 
719 mateusz.vi 162
 
163
// checkup action
164
 
216 mateuszvis 165
if ($a === 'checkup') {
345 mateuszvis 166
  $found = 0;
167
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
719 mateusz.vi 168
 
169
  foreach ($remote_pkgs as $rpkg) {
170
    $rpkg[0] = strtolower($rpkg[0]);
171
    if (empty($db[$rpkg[0]])) continue;
172
 
173
    $dbpkg = $db[$rpkg[0]];
174
    // compare user's version with preferred version in repo
175
    $prefver = array_shift($dbpkg['versions']);
176
    if (strcasecmp($prefver['ver'], $rpkg[1]) == 0) continue;
177
    // found version mismatch
178
    if ($found == 0) {
179
      echo str_pad('', 58, '-') . "\r\n";
180
      tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20));
181
      tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
216 mateuszvis 182
    }
719 mateusz.vi 183
    $found++;
184
    tabulprint(array('' . $rpkg[0], $rpkg[1], $prefver['ver']), array(8, 20, 20));
216 mateuszvis 185
  }
345 mateuszvis 186
  if ($found == 0) {
187
    echo "no available updates\r\n";
188
  } else {
189
    echo str_pad('', 58, '-') . "\r\n";
719 mateusz.vi 190
    echo "found {$found} differing package(s)\r\n";
345 mateuszvis 191
  }
216 mateuszvis 192
}
193
 
208 mateuszvis 194
?>