Subversion Repositories SvarDOS

Rev

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

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