Subversion Repositories SvarDOS

Rev

Rev 323 | Rev 559 | 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
5
  Copyright (C) 2021 Mateusz Viste
6
 
7
 === API ===
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
9
  ?a=search&p=PHRASE          list packages that match PHRASE
345 mateuszvis 10
  ?a=checkup                  list of packages+versions in $_POST
208 mateuszvis 11
*/
12
 
216 mateuszvis 13
 
14
function nicesize($bytes) {
15
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
16
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
17
  return(round($bytes / 1024 / 1024, 1) . "M");
18
}
19
 
20
 
345 mateuszvis 21
function csv_to_array($filename = '', $delimiter = "\t") {
22
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
23
  $handle = fopen($filename, 'r');
24
  if ($handle === false) return(false);
25
 
26
  $rez = array();
27
 
28
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
29
    $rez[] = $row;
30
  }
31
  fclose($handle);
32
 
33
  return $rez;
34
}
35
 
36
 
37
function tabulprint($ar_data, $ar_width, $margin = true) {
38
  $count = 0;
39
  foreach ($ar_data as $item) {
40
    if ($count == 0) {
41
      echo '|';
42
      if ($margin) echo ' ';
43
    }
44
    echo substr(str_pad($item, $ar_width[$count]), 0, $ar_width[$count]);
45
    if ($margin) {
46
      echo ' | ';
47
    } else {
48
      echo '|';
49
    }
50
    $count++;
51
  }
52
  echo "\r\n";
53
}
54
 
55
 
56
// *** MAIN START ************************************************************
57
 
58
 
216 mateuszvis 59
if (empty($_GET['a'])) {
208 mateuszvis 60
  http_response_code(404);
216 mateuszvis 61
  echo "ERROR: no action specified\r\n";
62
  exit(0);
208 mateuszvis 63
}
64
 
345 mateuszvis 65
$a = strtolower($_GET['a']);
66
 
67
$p = '';
68
if ($a != 'checkup') {
69
  if (empty($_GET['p'])) {
70
    http_response_code(404);
71
    echo "ERROR: no package specified\r\n";
72
    exit(0);
73
  }
74
  $p = strtolower($_GET['p']);
216 mateuszvis 75
}
76
 
208 mateuszvis 77
$v = '';
78
if (!empty($_GET['v'])) $v = $_GET['v'];
79
 
216 mateuszvis 80
// pull action is easy (does not require looking into pkg list), do it now
208 mateuszvis 81
 
216 mateuszvis 82
if ($a === 'pull') {
83
  if (file_exists($p . '.zip')) {
84
    readfile($p . '.zip');
85
  } else {
86
    http_response_code(404);
87
    echo "ERROR: package not found on server\r\n";
88
  }
89
  exit(0);
90
}
91
 
92
// is action valid?
93
 
94
if (($a !== 'search') && ($a !== 'checkup')) {
95
  http_response_code(404);
96
  echo "ERROR: invalid action\r\n";
97
  exit(0);
98
}
99
 
100
// iterate over packages now
101
 
102
$handle = fopen("index.tsv", "rb");
103
if ($handle === FALSE) {
104
  http_response_code(404);
105
  echo "ERROR: Server-side internal error\r\n";
106
  exit(0);
107
}
108
 
109
if ($a === 'search') {
110
  $matches = 0;
111
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
112
    if ((stristr($pkg[0], $p)) || (stristr($pkg[2], $p))) {
323 mateuszvis 113
      echo str_pad(strtoupper($pkg[0]), 12) . str_pad("ver: {$pkg[1]}", 13) . str_pad("size: " . nicesize(filesize($pkg[0] . '.zip')), 13) . "BSUM: " . sprintf("%04X", $pkg[3]) . "\r\n";
216 mateuszvis 114
      echo wordwrap($pkg[2], 79, "\r\n", true);
115
      echo "\r\n\r\n";
116
      $matches++;
117
    }
118
  }
119
  if ($matches == 0) echo "No matching package found.\r\n";
120
}
121
 
122
if ($a === 'checkup') {
345 mateuszvis 123
  $found = 0;
124
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
216 mateuszvis 125
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
345 mateuszvis 126
    // is $pkg part of remote packages?
127
    foreach ($remote_pkgs as $rpkg) {
128
      if (strcasecmp($pkg[0], $rpkg[0]) != 0) continue;
129
      if ($pkg[1] === $rpkg[1]) continue; // skip same version
130
      if ($found == 0) {
131
        echo str_pad('', 58, '-') . "\r\n";
132
        tabulprint(array('PACKAGE', 'INSTALLED (LOCAL)', 'AVAILABLE (REMOTE)'), array(8, 20, 20));
133
        tabulprint(array('----------', '----------------------', '----------------------'), array(10, 22, 22), false);
134
      }
135
      $found++;
136
      tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20));
216 mateuszvis 137
      break;
138
    }
139
  }
345 mateuszvis 140
  if ($found == 0) {
141
    echo "no available updates\r\n";
142
  } else {
143
    echo str_pad('', 58, '-') . "\r\n";
144
    echo "found {$found} differing packages\r\n";
145
  }
216 mateuszvis 146
}
147
fclose($handle);
148
 
149
 
208 mateuszvis 150
?>