Subversion Repositories SvarDOS

Rev

Rev 323 | Rev 559 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 323 Rev 345
Line 5... Line 5...
5
  Copyright (C) 2021 Mateusz Viste
5
  Copyright (C) 2021 Mateusz Viste
6
 
6
 
7
 === API ===
7
 === API ===
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
8
  ?a=pull&p=PACKAGE           downloads the zip archive containing PACKAGE
9
  ?a=search&p=PHRASE          list packages that match PHRASE
9
  ?a=search&p=PHRASE          list packages that match PHRASE
10
  ?a=checkup&p=PACKAGE&v=ver  check if package available in version > v
10
  ?a=checkup                  list of packages+versions in $_POST
11
*/
11
*/
12
 
12
 
13
 
13
 
14
function nicesize($bytes) {
14
function nicesize($bytes) {
15
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
15
  if ($bytes < 1024) return(round($bytes / 1024, 1) . "K");
16
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
16
  if ($bytes < 1024 * 1024) return(round($bytes / 1024) . "K");
17
  return(round($bytes / 1024 / 1024, 1) . "M");
17
  return(round($bytes / 1024 / 1024, 1) . "M");
18
}
18
}
19
 
19
 
20
 
20
 
-
 
21
function csv_to_array($filename = '', $delimiter = "\t") {
-
 
22
  //if (! file_exists($filename) || ! is_readable($filename)) return FALSE;
21
if (empty($_GET['a'])) {
23
  $handle = fopen($filename, 'r');
-
 
24
  if ($handle === false) return(false);
-
 
25
 
22
  http_response_code(404);
26
  $rez = array();
-
 
27
 
23
  echo "ERROR: no action specified\r\n";
28
  while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
-
 
29
    $rez[] = $row;
-
 
30
  }
-
 
31
  fclose($handle);
-
 
32
 
24
  exit(0);
33
  return $rez;
25
}
34
}
26
 
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
 
27
if (empty($_GET['p'])) {
59
if (empty($_GET['a'])) {
28
  http_response_code(404);
60
  http_response_code(404);
29
  echo "ERROR: no package specified\r\n";
61
  echo "ERROR: no action specified\r\n";
30
  exit(0);
62
  exit(0);
31
}
63
}
32
 
64
 
33
$a = strtolower($_GET['a']);
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
  }
34
$p = strtolower($_GET['p']);
74
  $p = strtolower($_GET['p']);
-
 
75
}
35
 
76
 
36
$v = '';
77
$v = '';
37
if (!empty($_GET['v'])) $v = $_GET['v'];
78
if (!empty($_GET['v'])) $v = $_GET['v'];
38
 
79
 
39
// pull action is easy (does not require looking into pkg list), do it now
80
// pull action is easy (does not require looking into pkg list), do it now
Line 77... Line 118...
77
  }
118
  }
78
  if ($matches == 0) echo "No matching package found.\r\n";
119
  if ($matches == 0) echo "No matching package found.\r\n";
79
}
120
}
80
 
121
 
81
if ($a === 'checkup') {
122
if ($a === 'checkup') {
-
 
123
  $found = 0;
-
 
124
  $remote_pkgs = csv_to_array("php://input", "\t"); // [0] = pkgname ; [1] = version
82
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
125
  while (($pkg = fgetcsv($handle, 1024, "\t")) !== FALSE) {
-
 
126
    // is $pkg part of remote packages?
-
 
127
    foreach ($remote_pkgs as $rpkg) {
83
    if (strcasecmp($pkg[0], $p)) {
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++;
84
      echo "found package {$pkg[0]} ver {$pkg[1]} -> is it newer than '{$v}' ?\r\n";
136
      tabulprint(array('' . $pkg[0], $rpkg[1], $pkg[1]), array(8, 20, 20));
85
      break;
137
      break;
86
    }
138
    }
87
  }
139
  }
-
 
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
  }
88
}
146
}
89
fclose($handle);
147
fclose($handle);
90
 
148
 
91
 
149
 
92
?>
150
?>