Offline Server  1.6
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.OfflineClientBuilder.php
Go to the documentation of this file.
1 <?php
2 
4  const targets_path = 'share/offline/targets';
5  const xulruntimes_path = 'share/offline/xulruntimes';
6  const xulapp_path = 'share/offline/xulapp';
7  const xulapp_appini = 'share/offline/xulapp/application.ini';
8  const customize_release = 'share/offline/customize/RELEASE';
9 
10  private $output_dir = '.';
11  private $opts = array();
12 
13  private $targets_path = '';
14  private $xulruntimes_path = '';
15  private $xulapp_path = '';
16  private $xulapp_appini = '';
17 
18  public $error = '';
19 
20  public function __construct($output_dir = '.', $opts = array()) {
21  $this->output_dir = $output_dir;
22  $this->opts = $opts;
23  $this->_initPath();
24  return $this;
25  }
26 
27  private function _initPath() {
28  include_once('WHAT/Lib.Prefix.php');
29 
30  global $pubdir;
31 
32  $this->targets_path = sprintf('%s/%s', $pubdir, self::targets_path);
33  $this->xulruntimes_path = sprintf('%s/%s', $pubdir, self::xulruntimes_path);
34  $this->xulapp_path = sprintf('%s/%s', $pubdir, self::xulapp_path);
35  $this->xulapp_appini = sprintf('%s/%s', $pubdir, self::xulapp_appini);
36  $this->customize_release = sprintf('%s/%s', $pubdir, self::customize_release);
37 
38  return true;
39  }
40 
41  public function build($buildid, $os, $arch, $prefix, $outputFile, $mar_basename='') {
42  include_once('WHAT/Lib.Common.php');
43 
44  global $pubdir;
45 
46  if( strpos($os, '/') !== false ) {
47  $this->error = sprintf("Malformed OS '%s' (cannot contain '/').", $os);
48  return false;
49  }
50  if( strpos($arch, '/') !== false ) {
51  $this->error = sprintf("Marlformed architecture '%s' (cannot contain '/').", $arch);
52  return false;
53  }
54 
55  $target_dir = sprintf("%s/%s/%s", $this->targets_path, $os, $arch);
56  if( ! is_dir($target_dir) ) {
57  $this->error = sprintf("Target directory '%s' is not a valid directory.", $target_dir);
58  return false;
59  }
60  $build_script = sprintf('%s/build.sh', $target_dir);
61  if( ! is_file($build_script) ) {
62  $this->error = sprintf("Build script '%s' is not a valid file.", $build_script);
63  return false;
64  }
65  if( ! is_executable($build_script) ) {
66  $this->error = sprintf("Build script '%s' is not executable.", $build_script);
67  return false;
68  }
69 
70  if( ! is_dir($this->output_dir) ) {
71  $this->error = sprintf("Output dir '%s' does not exists.", $this->output_dir);
72  return false;
73  }
74  if( ! is_writable($this->output_dir) ) {
75  $this->error = sprintf("Output dir '%s' is not writable.", $this->output_dir);
76  return false;
77  }
78 
79  $tmpfile = tempnam(getTmpDir(), __CLASS__);
80  if( $tmpfile === false ) {
81  $this->error = sprintf("Could not create temporary file.");
82  return false;
83  }
84 
85  $prefix = $this->expandFilename($prefix, array('OS' => $os, 'ARCH' => $arch));
86  $outputFile = sprintf('%s/%s', $this->output_dir, $this->expandFilename($outputFile, array('OS' => $os, 'ARCH' => $arch)));
87 
88  $envBackup = $this->getEnv(array(
89  'wpub',
90  'CUSTOMIZE_DIR',
91  'APP_VERSION',
92  'APP_BUILDID',
93  'CLIENTS_DIR',
94  'APP_UPDATE_ENABLED',
95  'OFFLINE_SERVER_VERSION',
96  'DCPOFFLINE_URL_BROWSER',
97  'DCPOFFLINE_URL_DATA',
98  'DCPOFFLINE_URL_UPDATE')
99  );
100  $env = array();
101  $env['wpub'] = $pubdir;
102  if( isset($this->opts['CUSTOMIZE_DIR']) ) {
103  $env['CUSTOMIZE_DIR'] = $this->opts['CUSTOMIZE_DIR'];
104  }
105  $env['APP_VERSION'] = $this->getOfflineInfo('Version.Customize');
106  $env['APP_BUILDID'] = $buildid;
107  $env['CLIENTS_DIR'] = $this->output_dir;
108  $env['APP_UPDATE_ENABLED'] = getParam('OFFLINE_CLIENT_UPDATE_ENABLED');
109  $env['OFFLINE_SERVER_VERSION'] = $this->getOfflineServerVersion();
110  $env['DCPOFFLINE_URL_BROWSER'] = getParam('DCPOFFLINE_URL_BROWSER');
111  $env['DCPOFFLINE_URL_DATA'] = getParam('DCPOFFLINE_URL_DATA');
112  $env['DCPOFFLINE_URL_UPDATE'] = getParam('DCPOFFLINE_URL_UPDATE');
113  $this->setEnv($env);
114 
115  $cmd = sprintf('%s %s %s %s > %s 2>&1', escapeshellarg($build_script), escapeshellarg($prefix), escapeshellarg($outputFile), escapeshellarg($mar_basename), escapeshellarg($tmpfile));
116  $out = system($cmd, $ret);
117  if( $ret !== 0 ) {
118  $this->error = sprintf("Error building client for {os='%s', arch='%s', prefix='%s', outputFile='%s', basename='%s'}: %s", $os, $arch, $prefix, $outputFile, $mar_basename, file_get_contents($tmpfile));
119  }
120 
121  $this->setEnv($envBackup);
122 
123  unlink($tmpfile);
124  return ($ret === 0) ? true : false;
125  }
126 
127  private function getEnv($varList = array()) {
128  $env = array();
129  foreach( $varList as $var ) {
130  $env []= array($var => getenv($var));
131  }
132  return $env;
133  }
134 
135  private function setEnv($env = array()) {
136  foreach( $env as $var => $value ) {
137  if( $value === false ) {
138  putenv($var);
139  } else {
140  putenv(sprintf('%s=%s', $var, $value));
141  }
142  }
143  }
144 
145  public function buildAll($buildid='') {
146  if( $buildid == '' ) {
147  $buildid = strftime("%Y%m%d%H%M%S");
148  }
149  foreach( $this->getOsArchList() as $spec ) {
150 
151  $argv = array(
152  0 => $buildid,
153  1 => $spec['os'],
154  2 => $spec['arch'],
155  3 => $spec['prefix'],
156  4 => $spec['file'],
157  5 => $spec['mar_basename']
158  );
159 
160  $ret = call_user_func_array(array($this, 'build'), $argv);
161  if( $ret === false ) {
162  $this->error = sprintf("Error building {os:'%s', arch:'%s'}: %s", $spec['os'], $spec['arch'], $this->error);
163  return false;
164  }
165  }
166  return true;
167  }
168 
169  public function getOsArchList() {
170  include_once('WHAT/Lib.Prefix.php');
171 
172  global $pubdir;
173 
174  $osArchList = array(
175  array(
176  'title' => 'Linux (i686)',
177  'os' => 'linux',
178  'arch' => 'i686',
179  'icon' => 'icon-linux.png',
180  'prefix' => 'dynacase-offline-%VERSION%',
181  'file' => 'dynacase-offline-linux-i686.tar.gz',
182  'mar_basename' => 'dynacase-offline-linux-i686'
183  ),
184  array(
185  'title' => 'Linux (x86_64)',
186  'os' => 'linux',
187  'arch' => 'x86_64',
188  'icon' => 'icon-linux.png',
189  'prefix' => 'dynacase-offline-%VERSION%',
190  'file' => 'dynacase-offline-linux-x86_64.tar.gz',
191  'mar_basename' => 'dynacase-offline-linux-x86_64'
192  ),
193  array(
194  'title' => 'Mac OS X (universal)',
195  'os' => 'mac',
196  'arch' => 'universal',
197  'icon' => 'icon-mac.png',
198  'prefix' => 'dynacase-offline-%VERSION%',
199  'file' => 'dynacase-offline-mac-universal.zip',
200  'mar_basename' => 'dynacase-offline-mac-universal'
201  ),
202  array(
203  'title' => 'Windows XP/Vista/7 (EXE 32 bits)',
204  'os' => 'win',
205  'arch' => '32',
206  'icon' => 'icon-win.png',
207  'prefix' => 'dynacase-offline-%VERSION%',
208  'file' => 'dynacase-offline-win-32.exe',
209  'mar_basename' => 'dynacase-offline-win-32'
210  ),
211  array(
212  'title' => 'Windows XP/Vista/7 (Zip 32 bits)',
213  'os' => 'win',
214  'arch' => '32_zip',
215  'icon' => 'icon-win.png',
216  'prefix' => 'dynacase-offline-%VERSION%',
217  'file' => 'dynacase-offline-win-32.zip',
218  'mar_basename' => 'dynacase-offline-win-32'
219  )
220  );
221 
222  $local_osArchList = sprintf('%s/OFFLINE/local_osArchList.php', $pubdir);
223  if( file_exists($local_osArchList) ) {
224  include_once($local_osArchList);
225  }
226 
227  return $osArchList;
228  }
229 
230  public function expandFilename($filename, $keymap = array()) {
231  $keymap['VERSION'] = $this->getOfflineInfo('Version.Customize');
232 
233  foreach( $keymap as $k => $v ) {
234  $filename = str_replace(sprintf('%%%s%%', $k), $v, $filename);
235  }
236 
237  return $filename;
238  }
239 
240  public static function test() {
241  $outputDir = '/tmp';
242  $ocb = new OfflineClientBuilder($outputDir);
243  $buildid = strftime("%Y%m%d%H%M%S");
244 
245  foreach( $ocb->getOsArchList() as $spec ) {
246 
247  $argv = array(
248  0 => $buildid,
249  1 => $spec['os'],
250  2 => $spec['arch'],
251  3 => $spec['prefix'],
252  4 => $spec['file'],
253  5 => $spec['mar_basename']
254  );
255 
256  print sprintf("Building %s/%s... ", $argv[0], $argv[1]);
257  $ret = call_user_func_array(array($ocb, 'build'), $argv);
258  if( $ret === false ) {
259  print "[ERROR]\n";
260  print sprintf("%s\n", $ocb->error);
261  print "\n";
262  } else {
263  print "[OK]\n";
264  }
265  }
266  }
267 
268  public function getOfflineInfo($info='Version') {
269  $conf = parse_ini_file($this->xulapp_appini, true);
270  if( ! is_array($conf) ) {
271  return false;
272  }
273  switch( $info ) {
274  case 'Version.Customize':
275  $customizeRelease = $this->getCustomizeRelease();
276  if( isset($conf['App']['Version']) ) {
277  return sprintf("%s.%d", $conf['App']['Version'], $customizeRelease);
278  }
279  break;
280  default:
281  if( isset($conf['App'][$info]) ) {
282  return $conf['App'][$info];
283  }
284  }
285  return false;
286  }
287 
288  public function getCustomizeRelease() {
289  $customizeRelease = 0;
290  if( ! is_file($this->customize_release) ) {
291  return $customizeRelease;
292  }
293  if( preg_match('/^\s*(?<release>\d+)/', file_get_contents($this->customize_release), $m) ) {
294  $customizeRelease = $m['release'];
295  }
296  return $customizeRelease;
297  }
298 
299  /**
300  * Get the version of the OFFLINE server application.
301  *
302  * @return string $version (e.g. "1.2.3-4")
303  */
304  public function getOfflineServerVersion() {
305  include_once('WHAT/Class.Application.php');
306  $app = new Application();
307  $app->Set('OFFLINE', $core);
308  $version = $app->getParam('VERSION');
309  return $version;
310  }
311 
312 }
313 
314 ?>
expandFilename($filename, $keymap=array())
__construct($output_dir= '.', $opts=array())
build($buildid, $os, $arch, $prefix, $outputFile, $mar_basename='')
← centre documentaire © anakeen - published under CC License - Dynacase