fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

snapinclient.class.php
Go to the documentation of this file.
1 <?php
22 class SnapinClient extends FOGClient implements FOGClientSend
23 {
29  public $shortName = 'snapinclient';
35  public function json()
36  {
37  $HostName = self::$Host->get('name');
38  $Task = self::$Host->get('task');
39  $SnapinJob = self::$Host->get('snapinjob');
40  if ($Task->isValid() && !$Task->isSnapinTasking()) {
41  return array(
42  'error' => 'it'
43  );
44  }
45  if (!$SnapinJob->isValid()) {
46  return array(
47  'error' => 'ns'
48  );
49  }
50  $STaskCount = self::getClass('SnapinTaskManager')
51  ->count(
52  array(
53  'jobID' => $SnapinJob->get('id'),
54  'stateID' => self::fastmerge(
55  self::getQueuedStates(),
56  (array)self::getProgressState()
57  )
58  )
59  );
60  if ($STaskCount < 1) {
61  if ($Task->isValid()) {
62  $Task->set('stateID', self::getCompleteState())->save();
63  }
64  $SnapinJob->set('stateID', self::getCompleteState())->save();
65  self::$EventManager->notify(
66  'HOST_SNAPIN_COMPLETE',
67  array(
68  'Host' => &self::$Host,
69  'HostName' => &$HostName
70  )
71  );
72  return array('error' => 'ns');
73  }
74  if ($Task->isValid()) {
75  $Task
76  ->set('stateID', self::getCheckedInState())
77  ->set('checkInTime', self::niceDate()->format('Y-m-d H:i:s'))
78  ->save();
79  }
80  $SnapinJob->set('stateID', self::getCheckedInState())->save();
81  global $sub;
82  if ($sub === 'requestClientInfo'
83  || basename(self::$scriptname) === 'snapins.checkin.php'
84  ) {
85  if (!isset($_REQUEST['exitcode'])) {
86  $snapinIDs = self::getSubObjectIDs(
87  'SnapinTask',
88  array(
89  'stateID' => self::fastmerge(
90  self::getQueuedStates(),
91  (array)self::getProgressState()
92  ),
93  'jobID' => $SnapinJob->get('id'),
94  ),
95  'snapinID'
96  );
97  $snapinIDs = self::getSubObjectIDs(
98  'Snapin',
99  array('id' => $snapinIDs)
100  );
101  if (count($snapinIDs) < 1) {
102  $SnapinJob
103  ->set('stateID', self::getCancelledState())
104  ->save();
105  return array(
106  'error' => _('No valid tasks found')
107  );
108  }
109  $info = array();
110  $info['snapins'] = array();
111  foreach ((array)self::getClass('SnapinManager')
112  ->find(
113  array('id' => $snapinIDs)
114  ) as &$Snapin
115  ) {
116  $snapinTaskID = self::getSubObjectIDs(
117  'SnapinTask',
118  array(
119  'snapinID' => $Snapin->get('id'),
120  'jobID' => $SnapinJob->get('id'),
121  'stateID' => self::fastmerge(
122  self::getQueuedStates(),
123  (array)self::getProgressState()
124  )
125  )
126  );
127  $snapinTaskID = array_shift($snapinTaskID);
128  $SnapinTask = new SnapinTask($snapinTaskID);
129  if (!$SnapinTask->isValid()) {
130  continue;
131  }
132  $StorageNode = $StorageGroup = null;
133  self::$HookManager->processEvent(
134  'SNAPIN_GROUP',
135  array(
136  'Host' => &self::$Host,
137  'Snapin' => &$Snapin,
138  'StorageGroup' => &$StorageGroup,
139  )
140  );
141  self::$HookManager->processEvent(
142  'SNAPIN_NODE',
143  array(
144  'Host' => &self::$Host,
145  'Snapin' => &$Snapin,
146  'StorageNode' => &$StorageNode,
147  )
148  );
149  if (!($StorageGroup instanceof StorageGroup
150  && $StorageGroup->isValid())
151  ) {
152  $StorageGroup = $Snapin->getStorageGroup();
153  if (!$StorageGroup->isValid()) {
154  continue;
155  }
156  }
157  if (!($StorageNode instanceof StorageNode
158  && $StorageNode->isValid())
159  ) {
160  $StorageNode = $StorageGroup->getMasterStorageNode();
161  if (!$StorageNode->isValid()) {
162  continue;
163  }
164  }
165  $location = sprintf(
166  'http://%s/%s',
167  $StorageNode->get('ip'),
168  $StorageNode->get('webroot')
169  );
170  $path = sprintf(
171  '/%s',
172  trim($StorageNode->get('snapinpath'), '/')
173  );
174  $file = $Snapin->get('file');
175  $filepath = sprintf(
176  '%s/%s',
177  $path,
178  $file
179  );
180  $hash = $Snapin->get('hash');
181  $SnapinTask
182  ->set('checkin', self::niceDate()->format('Y-m-d H:i:s'))
183  ->set('stateID', self::getCheckedInState())
184  ->save();
185  $action = '';
186  if ($Snapin->get('shutdown')) {
187  $action = 'shutdown';
188  } elseif ($Snapin->get('reboot')) {
189  $action = 'reboot';
190  }
191  $info['snapins'][] = array(
192  'pack' =>( bool)$Snapin->get('packtype'),
193  'hide' => (bool)$Snapin->get('hide'),
194  'timeout' => $Snapin->get('timeout'),
195  'jobtaskid' => $SnapinTask->get('id'),
196  'jobcreation' => $SnapinJob->get('createdTime'),
197  'name' => $Snapin->get('name'),
198  'args' => $Snapin->get('args'),
199  'action' => $action,
200  'filename' =>$Snapin->get('file'),
201  'runwith' => $Snapin->get('runWith'),
202  'runwithargs' => $Snapin->get('runWithArgs'),
203  'hash' => strtoupper($hash),
204  'size' => $size,
205  'url' => rtrim($location, '/'),
206  );
207  unset($Snapin, $SnapinTask);
208  }
209  return $info;
210  } elseif (isset($_REQUEST['exitcode'])) {
211  $this->_closeout($Task, $SnapinJob, $date, $HostName);
212  }
213  } elseif (basename(self::$scriptname) === 'snapins.file.php') {
214  $this->_downloadfile($Task, $SnapinJob, $date, $HostName);
215  }
216  }
222  public function send()
223  {
224  $HostName = self::$Host->get('name');
225  $Task = self::$Host->get('task');
226  $SnapinJob = self::$Host->get('snapinjob');
227  if ($Task->isValid() && !$Task->isSnapinTasking()) {
228  throw new Exception('#!it');
229  }
230  if (!$SnapinJob->isValid()) {
231  throw new Exception('#!ns');
232  }
233  $STaskCount = self::getClass('SnapinTaskManager')
234  ->count(
235  array(
236  'jobID' => $SnapinJob->get('id'),
237  'stateID' => self::fastmerge(
238  self::getQueuedStates(),
239  (array)self::getProgressState()
240  )
241  )
242  );
243  if ($STaskCount < 1) {
244  if ($Task->isValid()) {
245  $Task->set('stateID', self::getCompleteState())->save();
246  }
247  $SnapinJob->set('stateID', self::getCompleteState())->save();
248  self::$EventManager->notify(
249  'HOST_SNAPIN_COMPLETE',
250  array(
251  'Host' => &self::$Host,
252  'HostName' => &$HostName
253  )
254  );
255  throw new Exception('#!ns');
256  }
257  if ($Task->isValid()) {
258  $Task
259  ->set('stateID', self::getCheckedInState())
260  ->set('checkInTime', self::niceDate()->format('Y-m-d H:i:s'))
261  ->save();
262  }
263  $SnapinJob->set('stateID', self::getCheckedInState())->save();
264  if (basename(self::$scriptname) === 'snapins.checkin.php') {
265  if (!isset($_REQUEST['exitcode'])) {
266  $snapinIDs = self::getSubObjectIDs(
267  'SnapinTask',
268  array(
269  'stateID' => self::fastmerge(
270  self::getQueuedStates(),
271  (array)self::getProgressState()
272  ),
273  'jobID' => $SnapinJob->get('id'),
274  ),
275  'snapinID'
276  );
277  $snapinIDs = self::getSubObjectIDs(
278  'Snapin',
279  array('id' => $snapinIDs)
280  );
281  if (count($snapinIDs) < 1) {
282  $SnapinJob
283  ->set('stateID', self::getCancelledState())
284  ->save();
285  throw new Exception(
286  sprintf(
287  '%s: %s',
288  '#!er',
289  _('No valid tasks found')
290  )
291  );
292  }
293  $Snapins = self::getClass('SnapinManager')
294  ->find(
295  array('id' => $snapinIDs)
296  );
297  $Snapin = array_shift($Snapins);
298  if (!($Snapin instanceof Snapin && $Snapin->isValid())) {
299  $SnapinJob
300  ->set('stateID', self::getCancelledState())
301  ->save();
302  throw new Exception(
303  sprintf(
304  '%s: %s',
305  '#!er',
306  _('Snapin is invalid')
307  )
308  );
309  }
310  $snapinTaskID = self::getSubObjectIDs(
311  'SnapinTask',
312  array(
313  'snapinID' => $Snapin->get('id'),
314  'jobID' => $SnapinJob->get('id'),
315  'stateID' => self::fastmerge(
316  self::getQueuedStates(),
317  (array)self::getProgressState()
318  )
319  )
320  );
321  $snapinTaskID = @max($snapinTaskID);
322  $SnapinTask = new SnapinTask($snapinTaskID);
323  if (!$SnapinTask->isValid()) {
324  throw new Exception(
325  sprintf(
326  '%s: %s',
327  '#!er',
328  _('Snapin Task is invalid')
329  )
330  );
331  }
332  $StorageNode = $StorageGroup = null;
333  self::$HookManager->processEvent(
334  'SNAPIN_GROUP',
335  array(
336  'Host' => &self::$Host,
337  'Snapin' => &$Snapin,
338  'StorageGroup' => &$StorageGroup,
339  )
340  );
341  self::$HookManager->processEvent(
342  'SNAPIN_NODE',
343  array(
344  'Host' => &self::$Host,
345  'Snapin' => &$Snapin,
346  'StorageNode' => &$StorageNode,
347  )
348  );
349  $file = $Snapin->get('file');
350  $goodArray = array(
351  '#!ok',
352  sprintf(
353  'JOBTASKID=%d',
354  $SnapinTask->get('id')
355  ),
356  sprintf(
357  'JOBCREATION=%s',
358  $SnapinJob->get('createdTime')
359  ),
360  sprintf(
361  'SNAPINNAME=%s',
362  $Snapin->get('name')
363  ),
364  sprintf(
365  'SNAPINARGS=%s',
366  $Snapin->get('args')
367  ),
368  sprintf(
369  'SNAPINBOUNCE=%s',
370  $Snapin->get('reboot')
371  ),
372  sprintf(
373  'SNAPINFILENAME=%s',
374  $file
375  ),
376  sprintf(
377  'SNAPINRUNWITH=%s',
378  $Snapin->get('runWith')
379  ),
380  sprintf(
381  'SNAPINRUNWITHARGS=%s',
382  $Snapin->get('runWithArgs')
383  )
384  );
385  $this->send = implode("\n", $goodArray);
386  } elseif (isset($_REQUEST['exitcode'])) {
387  $this->_closeout($Task, $SnapinJob, $date, $HostName);
388  }
389  } elseif (basename(self::$scriptname) === 'snapins.file.php') {
390  $this->_downloadfile($Task, $SnapinJob, $date, $HostName);
391  }
392  }
403  private function _closeout($Task, $SnapinJob, $date, $HostName)
404  {
405  $tID = $_REQUEST['taskid'];
406  if (!(empty($td) && is_numeric($tID))) {
407  throw new Exception(
408  sprintf(
409  '%s: %s',
410  '#!er',
411  _('Invalid task id sent')
412  )
413  );
414  }
415  $SnapinTask = new SnapinTask($tID);
416  if (!($SnapinTask->isValid()
417  && !in_array(
418  $SnapinTask->get('stateID'),
419  array(
420  self::getCompleteState(),
421  self::getCancelledState()
422  )
423  ))
424  ) {
425  throw new Exception(
426  sprintf(
427  '%s: %s',
428  '#!er',
429  _('Invalid Snapin Tasking')
430  )
431  );
432  }
433  $Snapin = $SnapinTask->getSnapin();
434  if (!$Snapin->isValid()) {
435  throw new Exception(
436  sprintf(
437  '%s: %s',
438  '#!er',
439  _('Invalid Snapin')
440  )
441  );
442  }
443  $SnapinTask
444  ->set('stateID', self::getCompleteState())
445  ->set('return', $_REQUEST['exitcode'])
446  ->set('details', $_REQUEST['exitdesc'])
447  ->set('complete', self::niceDate()->format('Y-m-d H:i:s'))
448  ->save();
449  self::$EventManager->notify(
450  'HOST_SNAPINTASK_COMPLETE',
451  array(
452  'Snapin' => &$Snapin,
453  'SnapinTask' => &$SnapinTask,
454  'Host' => &self::$Host,
455  'HostName' => &$HostName
456  )
457  );
458  $STaskCount = self::getClass('SnapinTaskManager')
459  ->count(
460  array(
461  'jobID' => $SnapinJob->get('id'),
462  'stateID' => self::fastmerge(
463  self::getQueuedStates(),
464  (array)self::getProgressState()
465  )
466  )
467  );
468  if ($STaskCount < 1) {
469  if ($Task->isValid()) {
470  $Task->set('stateID', self::getCompleteState())->save();
471  }
472  $SnapinJob->set('stateID', self::getCompleteState())->save();
473  self::$EventManager->notify(
474  'HOST_SNAPIN_COMPLETE',
475  array(
476  'HostName' => &$HostName,
477  'Host' => &self::$Host
478  )
479  );
480  }
481  }
492  private function _downloadfile($Task, $SnapinJob, $date, $HostName)
493  {
494  $tID = $_REQUEST['taskid'];
495  if (!(!empty($tID) && is_numeric($tID))) {
496  throw new Exception(
497  sprintf(
498  '%s: %s',
499  '#!er',
500  _('Invalid task id')
501  )
502  );
503  }
504  $SnapinTask = new SnapinTask($tID);
505  if (!$SnapinTask->isValid()) {
506  throw new Exception(
507  sprintf(
508  '%s: %s',
509  '#!er',
510  _('Invalid Snapin Tasking object')
511  )
512  );
513  }
514  $Snapin = $SnapinTask->getSnapin();
515  if (!$Snapin->isValid()) {
516  throw new Exception(_('Invalid Snapin'));
517  }
518  $StorageGroup = $StorageNode = null;
519  self::$HookManager->processEvent(
520  'SNAPIN_GROUP',
521  array(
522  'Host' => &self::$Host,
523  'Snapin' => &$Snapin,
524  'StorageGroup' => &$StorageGroup
525  )
526  );
527  self::$HookManager->processEvent(
528  'SNAPIN_NODE',
529  array(
530  'Host' => &self::$Host,
531  'Snapin' => &$Snapin,
532  'StorageNode' => &$StorageNode
533  )
534  );
535  if (!($StorageGroup instanceof StorageGroup
536  && $StorageGroup->isValid())
537  ) {
538  $StorageGroup = $Snapin->getStorageGroup();
539  if (!$StorageGroup->isValid()) {
540  throw new Exception(
541  sprintf(
542  '%s: %s',
543  '#!er',
544  _('Invalid Storage Group')
545  )
546  );
547  }
548  }
549  if (!($StorageNode instanceof StorageNode
550  && $StorageNode->isValid())
551  ) {
552  $StorageNode = $StorageGroup->getMasterStorageNode();
553  if (!($StorageNode instanceof StorageNode
554  && $StorageNode->isValid())
555  ) {
556  throw new Exception(
557  sprintf(
558  '%s: %s',
559  '#!er',
560  _('Invalid Storage Node')
561  )
562  );
563  }
564  }
565  $path = sprintf(
566  '/%s',
567  trim($StorageNode->get('snapinpath'), '/')
568  );
569  $file = $Snapin->get('file');
570  $filepath = sprintf(
571  '%s/%s',
572  $path,
573  $file
574  );
575  $host = $StorageNode->get('ip');
576  $user = $StorageNode->get('user');
577  $pass = $StorageNode->get('pass');
578  self::$FOGFTP
579  ->set('host', $host)
580  ->set('username', $user)
581  ->set('password', $pass);
582  if (!self::$FOGFTP->connect()) {
583  throw new Exception(
584  sprintf(
585  '%s: %s',
586  '#!er',
587  _('Cannot connect to ftp server')
588  )
589  );
590  }
591  self::$FOGFTP->close();
592  $SnapinFile = sprintf(
593  'ftp://%s:%s@%s%s',
594  $user,
595  urlencode($pass),
596  $host,
597  $filepath
598  );
599  if ($Task->isValid()) {
600  $Task
601  ->set('stateID', self::getProgressState())
602  ->set('checkInTime', self::niceDate()->format('Y-m-d H:i:s'))
603  ->save();
604  }
605  $SnapinJob
606  ->set('stateID', self::getProgressState())
607  ->save();
608  $SnapinTask
609  ->set('stateID', self::getProgressState())
610  ->set('return', -1)
611  ->set('details', _('Pending...'))
612  ->save();
613  while (ob_get_level()) {
614  ob_end_clean();
615  }
616  header("X-Sendfile: $SnapinFile");
617  header('Content-Description: File Transfer');
618  header('Content-Type: application/octet-stream');
619  header("Content-Disposition: attachment; filename=$file");
620  header('Expires: 0');
621  header('Cache-Control: must-revalidate');
622  header('Pragma: public');
623  if (($fh = fopen($SnapinFile, 'rb')) === false) {
624  throw new Exception(
625  sprintf(
626  '%s: %s',
627  '#!er',
628  _('Could not read snapin file')
629  )
630  );
631  }
632  while (feof($fh) === false) {
633  if (($line = fread($fh, 4096)) === false) {
634  break;
635  }
636  echo $line;
637  flush();
638  }
639  fclose($fh);
640  exit;
641  }
642 }
SnapinClient\send
send()
Definition: snapinclient.class.php:222
exit
exit
Definition: download.php:102
SnapinClient
Definition: snapinclient.class.php:23
FOGBase\getClass
static getClass($class, $data='', $props=false)
Definition: fogbase.class.php:439
$file
if(isset($_REQUEST['legclient'])) if(isset($_REQUEST['newclient'])) if(isset($_REQUEST['fogprep'])) if(isset($_REQUEST['fogcrypt'])) if(isset($_REQUEST['smartinstaller'])) if(!file_exists($filename)) $file
Definition: download.php:61
Host\isValid
isValid()
Definition: host.class.php:622
FOGClient
Definition: fogclient.class.php:23
Snapin
Definition: snapin.class.php:23
$SnapinJob
if(!FOGCore::$Host->isValid()) $SnapinJob
Definition: snapcheck.php:28
Host\set
set($key, $value)
Definition: host.class.php:156
$user
$user
Definition: advanced.php:38
FOGBase\$info
static $info
Definition: fogbase.class.php:139
FOGController\get
get($key='')
Definition: fogcontroller.class.php:207
SnapinClient\_downloadfile
_downloadfile($Task, $SnapinJob, $date, $HostName)
Definition: snapinclient.class.php:492
FOGClientSend
Definition: fogclientsend.class.php:23
StorageNode
Definition: storagenode.class.php:23
$Host
$Host
Definition: hostnameloop.php:30
SnapinClient\$shortName
$shortName
Definition: snapinclient.class.php:29
$path
if(!is_string($_GET['path'])) $path
Definition: getfiles.php:31
SnapinClient\_closeout
_closeout($Task, $SnapinJob, $date, $HostName)
Definition: snapinclient.class.php:403
$Task
$Task
Definition: hostinfo.php:26
FOGBase\getSubObjectIDs
static getSubObjectIDs( $object='Host', $findWhere=array(), $getField='id', $not=false, $operator='AND', $orderBy='name', $groupBy=false, $filter='array_unique')
Definition: fogbase.class.php:2006
StorageGroup
Definition: storagegroup.class.php:23
SnapinClient\json
json()
Definition: snapinclient.class.php:35
SnapinTask
Definition: snapintask.class.php:23
$pass
$pass
Definition: hostinfo.php:161
FOGController\isValid
isValid()
Definition: fogcontroller.class.php:865
$host
$host
Definition: hostnameloop.php:27