Subversion Repositories SvarDOS

Rev

Rev 560 | Rev 719 | 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 ===
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
559 mateuszvis 9
  ?a=pull&p=PACKAGE-VER       downloads the zip 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 = '';
76
if ($a != 'checkup') {
77
  if (empty($_GET['p'])) {
78
    http_response_code(404);
79
    echo "ERROR: no package specified\r\n";
80
    exit(0);
81
  }
82
  $p = strtolower($_GET['p']);
216 mateuszvis 83
}
84
 
208 mateuszvis 85
$v = '';
86
if (!empty($_GET['v'])) $v = $_GET['v'];
87
 
216 mateuszvis 88
// pull action is easy (does not require looking into pkg list), do it now
208 mateuszvis 89
 
216 mateuszvis 90
if ($a === 'pull') {
91
  if (file_exists($p . '.zip')) {
631 mateusz.vi 92
    header('Content-Disposition: attachment; filename="' . $p . '.zip"');
93
    header('Content-Type: application/octet-stream');
216 mateuszvis 94
    readfile($p . '.zip');
95
  } else {
96
    http_response_code(404);
97
    echo "ERROR: package not found on server\r\n";
98
  }
99
  exit(0);
100
}
101
 
102
// is action valid?
103
 
104
if (($a !== 'search') && ($a !== 'checkup')) {
105
  http_response_code(404);
106
  echo "ERROR: invalid action\r\n";
107
  exit(0);
108
}
109
 
110
// iterate over packages now
111
 
112
$handle = fopen("index.tsv", "rb");
113
if ($handle === FALSE) {
114
  http_response_code(404);
115
  echo "ERROR: Server-side internal error\r\n";
116
  exit(0);
117
}
118
 
119
if ($a === 'search') {
120
  $matches = 0;
121
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
122
    if ((stristr($pkg[0], $p)) || (stristr($pkg[2], $p))) {
560 mateuszvis 123
      echo str_pad(strtoupper($pkg[0]), 12) . str_pad("ver: {$pkg[1]} ", 16) . str_pad("size: " . nicesize(filesize($pkg[0] . '.zip')), 16) . "BSUM: " . sprintf("%04X", $pkg[3]) . "\r\n";
216 mateuszvis 124
      echo wordwrap($pkg[2], 79, "\r\n", true);
559 mateuszvis 125
      echo "\r\n";
126
      // do I have any alt versions?
127
      $altvers = glob("{$pkg[0]}-*.zip");
128
      if (!empty($altvers)) {
129
        $alts = str_replace('.zip', '', $altvers);
130
        echo wordwrap("[alt versions: " . implode(', ', $alts), 79, "\r\n", true) . "]\r\n";
131
      }
132
      echo "\r\n";
216 mateuszvis 133
      $matches++;
134
    }
135
  }
136
  if ($matches == 0) echo "No matching package found.\r\n";
137
}
138
 
139
if ($a === 'checkup') {
345 mateuszvis 140
  $found = 0;
141
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
216 mateuszvis 142
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
345 mateuszvis 143
    // is $pkg part of remote packages?
144
    foreach ($remote_pkgs as $rpkg) {
145
      if (strcasecmp($pkg[0], $rpkg[0]) != 0) continue;
146
      if ($pkg[1] === $rpkg[1]) continue; // skip same version
147
      if ($found == 0) {
148
        echo str_pad('', 58, '-') . "\r\n";
149
        tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20));
150
        tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
151
      }
152
      $found++;
153
      tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20));
216 mateuszvis 154
      break;
155
    }
156
  }
345 mateuszvis 157
  if ($found == 0) {
158
    echo "no available updates\r\n";
159
  } else {
160
    echo str_pad('', 58, '-') . "\r\n";
161
    echo "found {$found} differing packages\r\n";
162
  }
216 mateuszvis 163
}
164
fclose($handle);
165
 
166
 
208 mateuszvis 167
?>