Offline Server  1.6
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
off_update.php
Go to the documentation of this file.
1 <?php
2 
3 function off_update(&$action) {
4  $parms = array();
5  $parms['download'] = getHttpVars('download', '');
6  $parms['version'] = getHttpVars('version', '');
7  $parms['buildid'] = getHttpVars('buildid', '');
8  $parms['os'] = getHttpVars('os', '');
9  $parms['arch'] = getHttpVars('arch', '');
10 
11  $parms['clientDir'] = $action->parent->getParam('OFFLINE_CLIENT_BUILD_OUTPUT_DIR', '');
12  if( ! is_dir($parms['clientDir']) ) {
13  http_400();
14  $err = sprintf(sprintf('OFFLINE_CLIENT_BUILD_OUTPUT_DIR: '._("OFFLINE:%s directory not found"), $parms['clientDir']));
15  error_log(__METHOD__." ".$err);
16  print $err;
17  exit(0);
18  }
19  $parms['clientDir'] = realpath($parms['clientDir']);
20 
21  switch( $parms['download'] ) {
22  case 'update':
23  return off_update_download_update($parms, $action);
24  break;
25  case 'complete':
26  return off_update_download_complete($parms, $action);
27  break;
28  case 'partial':
29  return off_update_download_partial($parms, $action);
30  break;
31  }
32  return off_update_unknown_download($parms, $action);
33 }
34 
35 function http_400() {
36  header('HTTP/1.1 400 Bad Request');
37  header('Content-Type: text/plain');
38 }
39 
40 function http_404() {
41  header('HTTP/1.1 404 Not Found');
42  header('Content-Type: text/plain');
43 }
44 
45 function parseInfo($infoFile) {
46  if( ! is_file($infoFile) ) {
47  return array();
48  }
49  $info = file_get_contents($infoFile);
50  if( $info === false ) {
51  return array();
52  }
53  $lines = preg_split('/\n/', $info);
54  $parsedInfo = array();
55  foreach( $lines as &$line ) {
56  $line = trim($line);
57  if( preg_match('/^(?<key>[^=]+)=(?<value>.*)$/', $line, $m) ) {
58  $parsedInfo[$m['key']] = $m['value'];
59  }
60  }
61  unset($line);
62  return $parsedInfo;
63 }
64 
65 function sendFile($type, $filename) {
66  if( ! is_file($filename) ) {
67  http_404();
68  $err = sprintf("File '%s' not found.", $filename);
69  error_log(__METHOD__." ".$err);
70  return $err;
71  }
72 
73  $fh = fopen($filename, 'r');
74  if( $fh === false ) {
75  http_404();
76  $err = sprintf("Error opening file '%s'.", $filename);
77  error_log(__METHOD__." ".$err);
78  return $err;
79  }
80 
81  header('HTTP/1.1 200 OK');
82  header('Content-Type: ' . $type);
83  header('Content-Length: ' . filesize($filename));
84  fpassthru($fh);
85  fclose($fh);
86  return '';
87 }
88 
89 function off_update_unknown_download(&$parms, &$action) {
90  http_400();
91  $err = sprintf("Unknown download '%s'.", $parms['download']);
92  error_log(__METHOD__." ".$err);
93  print $err;
94  exit(0);
95 }
96 
97 function off_update_download_update(&$parms, &$action) {
98  $buildList = getBuildFor($parms['os'], $parms['arch']);
99  if( count($buildList) <= 0 ) {
100  http_404();
101  $err = sprintf("No builds found for {os='%s', arch='%s'}.", $parms['os'], $parms['arch']);
102  error_log(__METHOD__." ".$err);
103  print $err;
104  exit(0);
105  }
106 
107  $core_externurl = getParam('CORE_EXTERNURL');
108 
109  $build = array_shift($buildList);
110 
111  $completeInfoFile = sprintf('%s/%s.complete.mar.info', $parms['clientDir'], $build['mar_basename']);
112  $completeInfo = parseInfo($completeInfoFile);
113 
114  $partialInfoFile = sprintf('%s/%s.partial.mar.info', $parms['clientDir'], $build['mar_basename']);
115  $partialInfo = parseInfo($partialInfoFile);
116 
117  $xml = "";
118  $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
119  $xml .= "<updates xmlns=\"http://www.mozilla.org/2005/app-update\">\n";
120  $xml .= sprintf("<update type=\"major\" version=\"%s\" extensionVersion=\"%s\" buildID=\"%s\" detailsURL=\"%s\" >\n",
121  $completeInfo['version'],
122  $completeInfo['version'],
123  $completeInfo['buildid'],
124  sprintf("%s?app=OFFLINE&amp;action=OFF_DLCLIENT", $core_externurl)
125  );
126 
127  if( $parms['version'] == $partialInfo['version_from'] && $parms['buildid'] == $partialInfo['buildid_from'] ) {
128  /* Serve the partial update only if the client match the partial update */
129  $xml .= sprintf(" <patch type=\"partial\" URL=\"%s\" hashFunction=\"%s\" hashValue=\"%s\" size=\"%s\" />\n",
130  sprintf("%sguest.php?app=OFFLINE&amp;action=OFF_UPDATE&amp;download=partial&amp;version=%%VERSION%%&amp;buildid=%%BUILD_ID%%&amp;os=%s&amp;arch=%s",
131  $core_externurl,
132  $parms['os'],
133  $parms['arch']
134  ),
135  $partialInfo['hashfunction'],
136  $partialInfo['hashvalue'],
137  $partialInfo['size']
138  );
139  }
140 
141  /* Serve the complete update */
142  $xml .= sprintf(" <patch type=\"complete\" URL=\"%s\" hashFunction=\"%s\" hashValue=\"%s\" size=\"%s\" />\n",
143  sprintf("%sguest.php?app=OFFLINE&amp;action=OFF_UPDATE&amp;download=complete&amp;version=%%VERSION%%&amp;buildid=%%BUILD_ID%%&amp;os=%s&amp;arch=%s",
144  $core_externurl,
145  $parms['os'],
146  $parms['arch']
147  ),
148  $completeInfo['hashfunction'],
149  $completeInfo['hashvalue'],
150  $completeInfo['size']
151  );
152 
153  $xml .= "</update>\n";
154  $xml .= "</updates>\n";
155 
156  header('HTTP/1.1 200 OK');
157  header('Content-Type: text/xml');
158  header('Content-Length: ' . strlen($xml));
159  echo $xml;
160  exit(0);
161 }
162 
163 function off_update_download_complete(&$parms, &$action) {
164  $buildList = getBuildFor($parms['os'], $parms['arch']);
165  if( count($buildList) <= 0 ) {
166  http_404();
167  $err = sprintf("No builds found for {os='%s', arch='%s'}.", $parms['os'], $parms['arch']);
168  error_log(__METHOD__." ".$err);
169  print $err;
170  exit(0);
171  }
172 
173  $build = array_shift($buildList);
174 
175  $completeMar = sprintf('%s/%s.complete.mar', $parms['clientDir'], $build['mar_basename']);
176  if( ! is_file($completeMar) ) {
177  http_404();
178  $err = sprintf("Complete MAR '%s' not found.", $completeMar);
179  error_log(__METHOD__." ".$err);
180  exit(0);
181  }
182 
183  sendFile('application/binary', $completeMar);
184  exit(0);
185 }
186 
187 function off_update_download_partial(&$parms, &$action) {
188  $buildList = getBuildFor($parms['os'], $parms['arch']);
189  if( count($buildList) <= 0 ) {
190  http_404();
191  $err = sprintf("No builds found for {os='%s', arch='%s'}.", $parms['os'], $parms['arch']);
192  error_log(__METHOD__." ".$err);
193  print $err;
194  exit(0);
195  }
196 
197  $build = array_shift($buildList);
198 
199  $partialMar = sprintf('%s/%s.partial.mar', $parms['clientDir'], $build['mar_basename']);
200 
201  sendfile('application/binary', $partialMar);
202  exit(0);
203 }
204 
205 function getBuildFor($os, $arch) {
206  include_once('OFFLINE/Class.OfflineClientBuilder.php');
207 
208  $ocb = new OfflineClientBuilder();
209  $buildList = $ocb->getOsArchList();
210 
211  $res = array();
212  foreach( $buildList as &$build ) {
213  if( $build['os'] == $os && $build['arch'] == $arch ) {
214  $res []= $build;
215  }
216  }
217  unset($build);
218 
219  return $res;
220 }
221 
222 ?>
off_update_download_partial(&$parms, &$action)
Definition: off_update.php:187
http_404()
Definition: off_update.php:40
getBuildFor($os, $arch)
Definition: off_update.php:205
off_update_download_update(&$parms, &$action)
Definition: off_update.php:97
http_400()
Definition: off_update.php:35
off_update_download_complete(&$parms, &$action)
Definition: off_update.php:163
parseInfo($infoFile)
Definition: off_update.php:45
sendFile($type, $filename)
Definition: off_update.php:65
off_update_unknown_download(&$parms, &$action)
Definition: off_update.php:89
off_update(&$action)
Definition: off_update.php:3
← centre documentaire © anakeen - published under CC License - Dynacase