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)  

fogbase.class.php
Go to the documentation of this file.
1 <?php
24 abstract class FOGBase
25 {
31  public static $locale = '';
37  public static $fogpingactive = false;
43  public static $fogdeleteactive = false;
49  public static $fogexportactive = false;
55  public static $pendingMACs = 0;
61  public static $pendingHosts = 0;
67  public static $defaultscreen = '';
73  public static $pluginsinstalled = array();
79  public static $useragent;
85  public static $foglang;
91  public static $ajax = false;
97  public static $post = false;
103  public static $service = false;
109  public static $json = false;
115  public static $newService = false;
121  protected $isLoaded = array();
127  protected static $strlen;
133  protected static $debug = false;
139  protected static $info = false;
145  protected static $buildSelectBox;
151  protected static $selected;
157  protected static $DB;
163  protected static $FOGFTP;
169  protected static $FOGCore;
175  protected static $EventManager;
181  protected static $HookManager;
187  protected static $TimeZone;
193  protected static $FOGUser;
199  protected static $FOGPageManager;
205  protected static $FOGURLRequests;
211  protected static $FOGSubMenu;
217  public static $scriptname;
223  public static $querystring;
229  public static $httpreqwith;
235  public static $reqmethod;
241  public static $remoteaddr;
247  public static $httpreferer;
253  protected static $ips = array();
259  protected static $interface = array();
265  protected static $searchPages = array(
266  'user',
267  'host',
268  'group',
269  'image',
270  //'storage',
271  'snapin',
272  'printer',
273  'task',
274  );
280  private static $_initialized = false;
286  public static $mySchema = 0;
292  public static $showhtml = true;
298  public static $httpproto = false;
304  public static $httphost = '';
312  public static $Host = null;
318  private static function _init()
319  {
320  if (self::$_initialized === true) {
321  return;
322  }
323  global $foglang;
324  global $FOGFTP;
325  global $FOGCore;
326  global $DB;
327  global $currentUser;
328  global $EventManager;
329  global $HookManager;
330  global $FOGURLRequests;
331  global $FOGPageManager;
332  global $TimeZone;
334  self::$FOGFTP = &$FOGFTP;
335  self::$FOGCore = &$FOGCore;
336  self::$DB = &$DB;
337  self::$EventManager = &$EventManager;
338  self::$HookManager = &$HookManager;
339  self::$FOGUser = &$currentUser;
340  global $sub;
341  $scriptPattern = 'service';
342  $queryPattern = 'sub=requestClientInfo';
343  self::$querystring = filter_input(INPUT_SERVER, 'QUERY_STRING');
344  self::$scriptname = filter_input(INPUT_SERVER, 'SCRIPT_NAME');
345  self::$httpreqwith = filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH');
346  self::$reqmethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD');
347  self::$remoteaddr = filter_input(INPUT_SERVER, 'REMOTE_ADDR');
348  self::$httpreferer = filter_input(INPUT_SERVER, 'HTTP_REFERER');
349  if (false !== stripos(self::$scriptname, $scriptPattern)) {
350  self::$service = true;
351  } elseif (false !== stripos(self::$querystring, $queryPattern)) {
352  self::$service = true;
353  }
354  self::$ajax = false !== stripos(self::$httpreqwith, 'xmlhttprequest');
355  self::$post = false !== stripos(self::$reqmethod, 'post');
356  self::$newService = isset($_POST['newService'])
357  || isset($_GET['newService'])
358  || $sub == 'requestClientInfo';
359  self::$json = isset($_POST['json'])
360  || isset($_GET['json'])
361  || self::$newService
362  || $sub == 'requestClientInfo';
363  self::$FOGURLRequests = &$FOGURLRequests;
365  self::$TimeZone = &$TimeZone;
366  /*
367  * Lambda function to allow building of select boxes.
368  *
369  * @param string $option the option to iterate
370  * @param bool|int $index the index to operate on if needed.
371  *
372  * @return void
373  */
374  self::$buildSelectBox = function ($option, $index = false) {
375  $value = $option;
376  if ($index) {
377  $value = $index;
378  }
379  printf(
380  '<option value="%s"%s>%s</option>',
381  $value,
382  (self::$selected == $value ? ' selected' : ''),
383  $option
384  );
385  };
389  self::$httpproto = 'http'
390  . (
391  filter_input(INPUT_SERVER, 'HTTPS') ?
392  's' :
393  ''
394  );
395  self::$httphost = filter_input(INPUT_SERVER, 'HTTP_HOST');
396  self::$_initialized = true;
397  }
403  public function __construct()
404  {
405  self::$useragent = self::_getUserAgent();
406  self::_init();
407 
408  return $this;
409  }
415  private static function _getUserAgent()
416  {
417  return filter_input(INPUT_SERVER, 'HTTP_USER_AGENT');
418  }
424  public function __toString()
425  {
426  return get_class($this);
427  }
439  public static function getClass($class, $data = '', $props = false)
440  {
441  if (!is_string($class)) {
442  throw new Exception(_('Class name must be a string'));
443  }
444  // Get all args, even unnamed args.
445  $args = func_get_args();
446  array_shift($args);
447 
448  // Trim the class var
449  $class = trim($class);
450 
451  // Test what the class is and return if it is Reflection.
452  $lClass = strtolower($class);
453  if ($lClass === 'reflectionclass') {
454  return new ReflectionClass(count($args) === 1 ? $args[0] : $args);
455  }
456 
457  global $sub;
458  // If class is Storage, test if sub is group or node.
459  if ($class === 'Storage') {
460  $class = 'StorageNode';
461  if (preg_match('#storage[-|_]group#i', $sub)) {
462  $class = 'StorageGroup';
463  }
464  }
465 
466  // Initiate Reflection item.
467  $obj = new ReflectionClass($class);
468 
469  // If props is set to true return the properties of the class.
470  if ($props === true) {
471  return $obj->getDefaultProperties();
472  }
473 
474  // Return the main object
475  if ($obj->getConstructor()) {
476  // If there's only one argument return the instance using it.
477  // Otherwise return with full call.
478  if (count($args) === 1) {
479  $class = $obj->newInstance($args[0]);
480  } else {
481  $class = $obj->newInstanceArgs($args);
482  }
483  } else {
484  $class = $obj->newInstanceWithoutConstructor();
485  }
486 
487  return $class;
488  }
503  public static function getHostItem(
504  $service = true,
505  $encoded = false,
506  $hostnotrequired = false,
507  $returnmacs = false,
508  $override = false,
509  $mac = false
510  ) {
511  self::$Host = new Host(0);
512  // Store the mac
513  if (!$mac) {
514  $mac = filter_input(INPUT_POST, 'mac');
515  if (!$mac) {
516  $mac = filter_input(INPUT_GET, 'mac');
517  }
518  if (!$mac) {
519  parse_str(
520  file_get_contents('php://input'),
521  $vars
522  );
523  $mac = $vars['mac'];
524  }
525  }
526  // disabling sysuuid detection code for now as it is causing
527  // trouble with machines having the same UUID like we've seen
528  // on some MSI motherboards having FFFFFFFF-FFFF-FFFF-FFFF...
529  /* $sysuuid = filter_input(INPUT_POST, 'sysuuid');
530  if (!$sysuuid) {
531  $sysuuid = filter_input(INPUT_GET, 'sysuuid');
532  }
533  */
534  // If encoded decode and store value
535  if ($encoded === true) {
536  $mac = base64_decode($mac);
537  // $sysuuid = base64_decode($sysuuid);
538  }
539  // See if we can find the host by system uuid rather than by mac's first.
540  /* if ($sysuuid) {
541  $Inventory = self::getClass('Inventory')
542  ->set('sysuuid', $sysuuid)
543  ->load('sysuuid');
544  $Host = self::getClass('Inventory')
545  ->set('sysuuid', $sysuuid)
546  ->load('sysuuid')
547  ->getHost();
548  if ($Host->isValid() && !$returnmacs) {
549  self::$Host = $Host;
550  return;
551  }
552  }
553  */
554  // Trim the mac list.
555  $mac = trim($mac);
556  // Parsing the macs
558  $mac,
559  !$service,
560  $service
561  );
562  $macs = array();
563  foreach ((array) $MACs as &$mac) {
564  if (!$mac->isValid()) {
565  continue;
566  }
567  $macs[] = $mac->__toString();
568  unset($mac);
569  }
570  // Get the host element based on the mac address
571  self::getClass('HostManager')->getHostByMacAddresses($macs);
572  // If no macs are returned and the host is not required,
573  // throw message that it's an invalid mac.
574  if (count($macs) < 1 && $hostnotrequired === false) {
575  if ($service) {
576  $msg = '#!im';
577  } else {
578  $msg = sprintf(
579  '%s %s',
580  self::$foglang['InvalidMAC'],
581  $mac
582  );
583  }
584  throw new Exception($msg);
585  }
586 
587  // If returnmacs parameter is true, return the macs as an array
588  if ($returnmacs) {
589  if (!is_array($macs)) {
590  $macs = (array) $macs;
591  }
592 
593  return $macs;
594  }
595 
596  if ($hostnotrequired === false && $override === false) {
597  if (self::$Host->get('pending')) {
598  self::$Host = new Host(0);
599  }
600  if (!self::$Host->isValid()) {
601  if ($service) {
602  $msg = '#!ih';
603  } else {
604  $msg = _('Invalid Host');
605  }
606  throw new Exception($msg);
607  }
608  }
609  return;
610  }
618  public static function getAllBlamedNodes($Host)
619  {
620  $DateInterval = self::niceDate()->modify('-5 minutes');
629  $nodeFail = function ($NodeFailure) use ($DateInterval) {
630  if ($NodeFailure->isValid()) {
631  return false;
632  }
633  $DateTime = self::niceDate($NodeFailure->get('failureTime'));
634  if ($DateTime < $DateInterval) {
635  $NodeFailure->destroy();
636 
637  return false;
638  }
639 
640  return $NodeFailure->get('id');
641  };
642  $find = array(
643  'taskID' => self::$Host->get('task')->get('id'),
644  'hostID' => self::$Host->get('id'),
645  );
646  $nodeRet = array_map(
647  $nodeFail,
648  (array)self::getClass('NodeFailureManager')->find($find)
649  );
650  $nodeRet = array_filter($nodeRet);
651  $nodeRet = array_unique($nodeRet);
652  $nodeRet = array_values($nodeRet);
653 
654  return $nodeRet;
655  }
661  protected static function getActivePlugins()
662  {
663  $plugins = self::getSubObjectIDs(
664  'Plugin',
665  array(
666  'installed' => 1,
667  'state' => 1,
668  ),
669  'name'
670  );
671 
672  return array_map('strtolower', (array) $plugins);
673  }
682  private static function _setString($txt, $data = array())
683  {
684  if (count($data)) {
685  $data = vsprintf($txt, $data);
686  } else {
687  $data = $txt;
688  }
689 
690  return $data;
691  }
700  protected static function fatalError($txt, $data = array())
701  {
702  if (self::$service || self::$ajax) {
703  return;
704  }
705  $data = self::_setString($txt, $data);
706  $string = sprintf(
707  'FOG FATAL ERROR: %s: %s',
708  get_class($this),
709  $data
710  );
711  printf('<div class="debug debug-error">%s</div>', $string);
712  }
721  protected static function error($txt, $data = array())
722  {
723  if ((self::$service || self::$ajax) || !self::$debug) {
724  return;
725  }
726  $data = self::_setString($txt, $data);
727  $string = sprintf(
728  'FOG ERROR: %s: %s',
729  get_class($this),
730  $data
731  );
732  printf('<div class="debug debug-error">%s</div>', $string);
733  }
742  protected static function debug($txt, $data = array())
743  {
744  if ((self::$service || self::$ajax) || !self::$debug) {
745  return;
746  }
747  $data = self::_setString($txt, $data);
748  $string = sprintf(
749  'FOG DEBUG: %s: %s',
750  get_class($this),
751  $data
752  );
753  printf('<div class="debug debug-error">%s</div>', $string);
754  }
763  protected static function info($txt, $data = array())
764  {
765  if (!self::$info || self::$service || self::$ajax) {
766  return;
767  }
768  $data = self::_setString($txt, $data);
769  $string = sprintf(
770  'FOG INFO: %s: %s',
771  get_class($this),
772  $data
773  );
774  printf('<div class="debug debug-info">%s</div>', $string);
775  }
784  protected static function setMessage($txt, $data = array())
785  {
786  if (session_status() != PHP_SESSION_NONE) {
787  $_SESSION['FOG_MESSAGES'] = self::_setString($txt, $data);
788  }
789  }
795  protected static function getMessages()
796  {
797  if (session_status() == PHP_SESSION_NONE) {
798  return;
799  }
800  if (!isset($_SESSION['FOG_MESSAGES'])) {
801  $_SESSION['FOG_MESSAGES'] = array();
802  }
803  $messages = (array) $_SESSION['FOG_MESSAGES'];
804  unset($_SESSION['FOG_MESSAGES']);
805  // Create a hook in for messages
806  if (self::$HookManager instanceof HookManager) {
807  self::$HookManager->processEvent(
808  'MessageBox',
809  array('data' => &$messages)
810  );
811  }
817  $print_messages = function ($message) {
818  printf('<div class="fog-message-box">%s</div>', $message);
819  };
820  // Print the messages
821  array_map($print_messages, $messages);
822  unset($messages);
823  }
831  protected static function redirect($url = '')
832  {
833  if (self::$service) {
834  return;
835  }
836  header('Strict-Transport-Security: "max-age=15768000"');
837  header('X-Content-Type-Options: nosniff');
838  header('X-XSS-Protection: 1; mode=block');
839  header('X-Robots-Tag: none');
840  header('X-Frame-Options: SAMEORIGIN');
841  header("Location: $url");
842  exit;
843  }
855  protected static function arrayInsertBefore(
856  $key,
857  array &$array,
858  $new_key,
859  $new_value
860  ) {
861  if (!is_string($key)) {
862  throw new Exception(_('Key must be a string or index'));
863  }
864  $new = array();
865  foreach ($array as $k => &$value) {
866  if ($k === $key) {
867  $new[$new_key] = $new_value;
868  }
869  $new[$k] = $value;
870  unset($k, $value);
871  }
872  $array = $new;
873  }
885  protected static function arrayInsertAfter(
886  $key,
887  array &$array,
888  $new_key,
889  $new_value
890  ) {
891  if (!is_string($key) && !is_numeric($key)) {
892  throw new Exception(_('Key must be a string or index'));
893  }
894  $new = array();
895  foreach ($array as $k => &$value) {
896  $new[$k] = $value;
897  if ($k === $key) {
898  $new[$new_key] = $new_value;
899  }
900  unset($k, $value);
901  }
902  $array = $new;
903  }
913  protected static function arrayRemove($key, array &$array)
914  {
915  if (!(is_string($key) || is_array($key))) {
916  throw new Exception(_('Key must be an array of keys or a string.'));
917  }
918  if (is_array($key)) {
919  foreach ($key as &$k) {
920  self::arrayRemove($k, $array);
921  unset($k);
922  }
923  } else {
924  foreach ($array as &$value) {
925  if (is_array($value)) {
926  self::arrayRemove($key, $value);
927  } else {
928  unset($array[$key]);
929  }
930  unset($value);
931  }
932  }
933  }
943  protected static function arrayFind(
944  $needle,
945  array $haystack,
946  $ignorecase = false
947  ) {
948  $key = array_search($needle, $haystack);
949  if (false !== $key) {
950  return $key;
951  }
952  $cmd = $ignorecase !== false ? 'stripos' : 'strpos';
953  foreach ($haystack as $key => &$value) {
954  if (false !== $cmd($value, $needle)) {
955  return $key;
956  }
957  unset($value);
958  }
959 
960  return -1;
961  }
969  protected function isLoaded($key)
970  {
971  $key = $this->key($key);
972  $result = isset($this->isLoaded[$key]) ? $this->isLoaded[$key] : 0;
973  $this->isLoaded[$key] = true;
974  ++$this->isLoaded[$key];
975 
976  return $result ? $result : false;
977  }
983  protected static function resetRequest()
984  {
985  if (session_status() == PHP_SESSION_NONE) {
986  return;
987  }
988  if (!isset($_SESSION['post_request_vals'])) {
989  $_SESSION['post_request_vals'] = array();
990  }
991  $sesVars = $_SESSION['post_request_vals'];
992  $setReq = function (&$val, &$key) {
993  $_POST[$key] = $val;
994  unset($val, $key);
995  };
996  if (count($sesVars) > 0) {
997  array_walk($sesVars, $setReq);
998  }
999  unset($_SESSION['post_request_vals'], $sesVars, $reqVars);
1000  }
1006  protected function setRequest()
1007  {
1008  if (session_status() == PHP_SESSION_NONE) {
1009  return;
1010  }
1011  if (!isset($_SESSION['post_request_vals'])) {
1012  $_SESSION['post_request_vals'] = array();
1013  }
1014  if (!$_SESSION['post_request_vals'] && self::$post) {
1015  $_SESSION['post_request_vals'] = $_POST;
1016  }
1017  }
1025  protected static function formatByteSize($size)
1026  {
1027  $units = array('iB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
1028  $factor = floor((strlen($size) - 1) / 3);
1029 
1030  return sprintf('%3.2f %s', $size / pow(1024, $factor), $units[$factor]);
1031  }
1042  protected static function getGlobalModuleStatus($names = false, $keys = false)
1043  {
1044  // The shortnames are on the left, the long names are on the right
1045  // If the right is true it means the short is accurate.
1046  // If the left is not the right caller in form of:
1047  // FOG_CLIENT_<name>_ENABLED in lowercase.
1048  $services = array(
1049  'autologout' => 'autologoff',
1050  'clientupdater' => true,
1051  'dircleanup' => 'directorycleaner',
1052  'displaymanager' => true,
1053  'greenfog' => true,
1054  'hostnamechanger' => true,
1055  'hostregister' => true,
1056  'powermanagement' => true,
1057  'printermanager' => true,
1058  'snapinclient' => 'snapin',
1059  'taskreboot' => true,
1060  'usercleanup' => true,
1061  'usertracker' => true,
1062  );
1063  // If keys is set, return just the keys.
1064  if ($keys) {
1065  $keys = array_keys($services);
1066  $keys = array_filter($keys);
1067  $keys = array_unique($keys);
1068 
1069  return array_values($keys);
1070  }
1071  // Change the keys values
1072  foreach ($services as $short => &$value) {
1073  $tmp = $value === true ? $short : $value;
1074  $value = sprintf('FOG_CLIENT_%s_ENABLED', strtoupper($tmp));
1075  unset($value);
1076  }
1077  // If names is set, send back the short and long names together.
1078  if ($names) {
1079  return $services;
1080  }
1081  // Now lets get their status'
1082  $serviceEn = self::getSubObjectIDs(
1083  'Service',
1084  array(
1085  'name' => array_values($services),
1086  ),
1087  'value',
1088  false,
1089  'AND',
1090  'name',
1091  false,
1092  false
1093  );
1094 
1095  return array_combine(array_keys($services), $serviceEn);
1096  }
1105  public static function niceDate($date = 'now', $utc = false)
1106  {
1107  if ($utc || empty(self::$TimeZone)) {
1108  $tz = new DateTimeZone('UTC');
1109  } else {
1110  $tz = new DateTimeZone(self::$TimeZone);
1111  }
1112 
1113  return new DateTime($date, $tz);
1114  }
1124  public static function formatTime($time, $format = false, $utc = false)
1125  {
1126  if (!$time instanceof DateTime) {
1127  $time = self::niceDate($time, $utc);
1128  }
1129  if ($format) {
1130  if (!self::validDate($time)) {
1131  return _('No Data');
1132  }
1133 
1134  return $time->format($format);
1135  }
1136  $now = self::niceDate('now', $utc);
1137  // Get difference of the current to supplied.
1138  $diff = $now->format('U') - $time->format('U');
1139  $absolute = abs($diff);
1140  if (is_nan($diff)) {
1141  return _('Not a number');
1142  }
1143  if (!self::validDate($time)) {
1144  return _('No Data');
1145  }
1146  $date = $time->format('Y/m/d');
1147  if ($now->format('Y/m/d') == $date) {
1148  if (0 <= $diff && $absolute < 60) {
1149  return 'Moments ago';
1150  } elseif ($diff < 0 && $absolute < 60) {
1151  return 'Seconds from now';
1152  } elseif ($absolute < 3600) {
1153  return self::humanify($diff / 60, 'minute');
1154  } else {
1155  return self::humanify($diff / 3600, 'hour');
1156  }
1157  }
1158  $dayAgo = clone $now;
1159  $dayAgo->modify('-1 day');
1160  $dayAhead = clone $now;
1161  $dayAhead->modify('+1 day');
1162  if ($dayAgo->format('Y/m/d') == $date) {
1163  return 'Ran Yesterday at '.$time->format('H:i');
1164  } elseif ($dayAhead->format('Y/m/d') == $date) {
1165  return 'Runs today at '.$time->format('H:i');
1166  } elseif ($absolute / 86400 <= 7) {
1167  return self::humanify($diff / 86400, 'day');
1168  } elseif ($absolute / 604800 <= 5) {
1169  return self::humanify($diff / 604800, 'week');
1170  } elseif ($absolute / 2628000 < 12) {
1171  return self::humanify($diff / 2628000, 'month');
1172  }
1173 
1174  return self::humanify($diff / 31536000, 'year');
1175  }
1184  protected static function validDate($date, $format = '')
1185  {
1186  if ($format == 'N') {
1187  if ($date instanceof DateTime) {
1188  return $date->format('N') >= 0;
1189  } else {
1190  return $date >= 0 && $date <= 7;
1191  }
1192  }
1193  if (!$date instanceof DateTime) {
1194  $date = self::niceDate($date);
1195  }
1196  if (!$format) {
1197  $format = 'm/d/Y';
1198  }
1199  if (empty(self::$TimeZone)) {
1200  $tz = new DateTimeZone('UTC');
1201  } else {
1202  $tz = new DateTimeZone(self::$TimeZone);
1203  }
1204 
1205  return DateTime::createFromFormat(
1206  $format,
1207  $date->format($format),
1208  $tz
1209  );
1210  }
1222  protected static function pluralize($count, $text, $space = false)
1223  {
1224  if (!is_bool($space)) {
1225  throw new Exception(_('Space variable must be boolean'));
1226  }
1227 
1228  return sprintf(
1229  '%d %s%s%s',
1230  $count,
1231  $text,
1232  $count != 1 ? 's' : '',
1233  $space === true ? ' ' : ''
1234  );
1235  }
1247  protected static function diff($start, $end, $ago = false)
1248  {
1249  if (!is_bool($ago)) {
1250  throw new Exception(_('Ago must be boolean'));
1251  }
1252  if (!$start instanceof DateTime) {
1253  $start = self::niceDate($start);
1254  }
1255  if (!$end instanceof DateTime) {
1256  $end = self::niceDate($end);
1257  }
1258  $Duration = $start->diff($end);
1259  $str = '';
1260  $suffix = '';
1261  if ($ago === true) {
1262  $str = '%s %s';
1263  if ($Duration->invert) {
1264  $suffix = 'ago';
1265  }
1266  if (($v = $Duration->y) > 0) {
1267  return sprintf(
1268  $str,
1269  self::pluralize($v, 'year'),
1270  $suffix
1271  );
1272  }
1273  if (($v = $Duration->m) > 0) {
1274  return sprintf(
1275  $str,
1276  self::pluralize($v, 'month'),
1277  $suffix
1278  );
1279  }
1280  if (($v = $Duration->d) > 0) {
1281  return sprintf(
1282  $str,
1283  self::pluralize($v, 'day'),
1284  $suffix
1285  );
1286  }
1287  if (($v = $Duration->h) > 0) {
1288  return sprintf(
1289  $str,
1290  self::pluralize($v, 'hour'),
1291  $suffix
1292  );
1293  }
1294  if (($v = $Duration->i) > 0) {
1295  return sprintf(
1296  $str,
1297  self::pluralize($v, 'minute'),
1298  $suffix
1299  );
1300  }
1301  if (($v = $Duration->s) > 0) {
1302  return sprintf(
1303  $str,
1304  self::pluralize($v, 'second'),
1305  $suffix
1306  );
1307  }
1308  }
1309  if (($v = $Duration->y) > 0) {
1310  $str .= self::pluralize($v, 'year', true);
1311  }
1312  if (($v = $Duration->m) > 0) {
1313  $str .= self::pluralize($v, 'month', true);
1314  }
1315  if (($v = $Duration->d) > 0) {
1316  $str .= self::pluralize($v, 'day', true);
1317  }
1318  if (($v = $Duration->h) > 0) {
1319  $str .= self::pluralize($v, 'hour', true);
1320  }
1321  if (($v = $Duration->i) > 0) {
1322  $str .= self::pluralize($v, 'minute', true);
1323  }
1324  if (($v = $Duration->s) > 0) {
1325  $str .= self::pluralize($v, 'second');
1326  }
1327 
1328  return $str;
1329  }
1340  protected static function humanify($diff, $unit)
1341  {
1342  if (!is_numeric($diff)) {
1343  throw new Exception(_('Diff parameter must be numeric'));
1344  }
1345  if (!is_string($unit)) {
1346  throw new Exception(_('Unit of time must be a string'));
1347  }
1348  $before = $after = '';
1349  if ($diff < 0) {
1350  $before = sprintf('%s ', _('In'));
1351  }
1352  if ($diff < 0) {
1353  $after = sprintf(' %s', _('ago'));
1354  }
1355  $diff = floor(abs($diff));
1356  if ($diff != 1) {
1357  $unit .= 's';
1358  }
1359 
1360  return sprintf(
1361  '%s%d %s%s',
1362  $before,
1363  $diff,
1364  $unit,
1365  $after
1366  );
1367  }
1378  protected static function arrayChangeKey(array &$array, $old_key, $new_key)
1379  {
1380  if (!is_string($old_key)) {
1381  throw new Exception(_('Old key must be a string'));
1382  }
1383  if (!is_string($new_key)) {
1384  throw new Exception(_('New key must be a string'));
1385  }
1386  $array[$old_key] = (
1387  is_string($array[$old_key]) ?
1388  trim($array[$old_key]) :
1389  $array[$old_key]
1390  );
1391  if (!self::$service && is_string($array[$old_key])) {
1392  $item = mb_convert_encoding(
1393  $array[$old_key],
1394  'utf-8'
1395  );
1396  $array[$new_key] = Initiator::sanitizeItems(
1397  $item
1398  );
1399  } else {
1400  $array[$new_key] = $array[$old_key];
1401  }
1402  if ($old_key != $new_key) {
1403  unset($array[$old_key]);
1404  }
1405  }
1413  protected static function byteconvert($kilobytes)
1414  {
1415  return ($kilobytes / 8) * 1024;
1416  }
1424  protected static function hex2bin($hex)
1425  {
1426  if (function_exists('hex2bin')) {
1427  return hex2bin($hex);
1428  }
1429  $n = strlen($hex);
1430  $i = 0;
1431  $sbin = '';
1432  while ($i < $n) {
1433  $a = substr($hex, $i, 2);
1434  $sbin .= pack('H*', $a);
1435  $i += 2;
1436  }
1437 
1438  return $sbin;
1439  }
1445  public static function createSecToken()
1446  {
1447  if (function_exists('random_bytes')) {
1448  $token = bin2hex(
1449  random_bytes(64)
1450  );
1451  } elseif (function_exists('openssl_random_pseudo_bytes')) {
1452  $token = bin2hex(
1453  openssl_random_pseudo_bytes(
1454  64
1455  )
1456  );
1457  }
1458  return $token;
1459  }
1469  public static function aesencrypt(
1470  $data,
1471  $key = false,
1472  $enctype = 'aes-256-cbc'
1473  ) {
1474  if (!$data) {
1475  echo json_encode(
1476  array(
1477  'error' => _('Data is blank')
1478  )
1479  );
1480  exit;
1481  }
1482  $iv_size = openssl_cipher_iv_length($enctype);
1483  $key = self::hex2bin($key);
1484  if (mb_strlen($key, '8bit') !== ($iv_size * 2)) {
1485  echo json_encode(
1486  array(
1487  'error' => _('Needs a 256-bit key')
1488  )
1489  );
1490  exit;
1491  }
1492  $iv = openssl_random_pseudo_bytes($iv_size, $cstrong);
1493 
1494  // Pad the plaintext
1495  if (strlen($data) % $iv_size) {
1496  $data = str_pad(
1497  $data,
1498  ((strlen($data) + $iv_size) - (strlen($data) % $iv_size)),
1499  "\0"
1500  );
1501  }
1502 
1503  $cipher = openssl_encrypt(
1504  $data,
1505  $enctype,
1506  $key,
1507  OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,
1508  $iv
1509  );
1510  if (!$cipher) {
1511  echo json_encode(
1512  array(
1513  'error' => openssl_error_string()
1514  )
1515  );
1516  exit;
1517  }
1518  $iv = bin2hex($iv);
1519  $cipher = bin2hex($cipher);
1520  return sprintf(
1521  '%s|%s',
1522  $iv,
1523  $cipher
1524  );
1525  }
1536  public static function aesdecrypt(
1537  $encdata,
1538  $key = false,
1539  $enctype = 'aes-128-cbc'
1540  ) {
1541  $iv_size = openssl_cipher_iv_length($enctype) * 2;
1542  if (false === strpos($encdata, '|')) {
1543  return $encdata;
1544  }
1545  $data = explode('|', $encdata);
1546  if (strlen($data[0]) != $iv_size || strlen($data[1]) != $iv_size) {
1547  return $encdata;
1548  }
1549  // add error handler to catch warnings we might get from pack() with non-hex strings
1550  set_error_handler(
1551  function ($severity, $message, $file, $line) {
1552  throw new ErrorException($message, $severity, $severity, $file, $line);
1553  }
1554  );
1555  try {
1556  $iv = pack('H*', $data[0]);
1557  $encoded = pack('H*', $data[1]);
1558  if (!$key && isset($data[2]) && strlen($data[2]) == $iv_size) {
1559  $key = pack('H*', $data[2]);
1560  }
1561  } catch (Exception $e) {
1562  return $encdata;
1563  }
1564  restore_error_handler();
1565  if (empty($key)) {
1566  return '';
1567  }
1568  $decipher = openssl_decrypt(
1569  $encoded,
1570  $enctype,
1571  $key,
1572  OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,
1573  $iv
1574  );
1575  if (!$decipher) {
1576  echo json_encode(
1577  array(
1578  'error' => openssl_error_string()
1579  )
1580  );
1581  exit;
1582  }
1583 
1584  return trim($decipher);
1585  }
1596  protected static function certEncrypt($data)
1597  {
1598  if (!self::$Host->isValid()) {
1599  throw new Exception('#!ih');
1600  }
1601  if (!self::$Host->get('pub_key')) {
1602  throw new Exception('#!ihc');
1603  }
1604  return self::aesencrypt($data, self::$Host->get('pub_key'));
1605  }
1616  protected static function certDecrypt($dataArr, $padding = true)
1617  {
1618  if ($padding) {
1619  $padding = OPENSSL_PKCS1_PADDING;
1620  } else {
1621  $padding = OPENSSL_NO_PADDING;
1622  }
1623  $tmpssl = array();
1624  $sslfile = self::getSubObjectIDs('StorageNode', '', 'sslpath');
1625  foreach ($sslfile as &$path) {
1626  if (!file_exists($path) || !is_readable($path)) {
1627  continue;
1628  }
1629  $tmpssl[] = $path;
1630  unset($path);
1631  }
1632  if (count($tmpssl) < 1) {
1633  throw new Exception(_('Private key path not found'));
1634  }
1635  $sslfile = sprintf(
1636  '%s%s.srvprivate.key',
1637  str_replace(
1638  array('\\', '/'),
1639  array(
1640  DS,
1641  DS
1642  ),
1643  $tmpssl[0]
1644  ),
1645  DS
1646  );
1647  unset($tmpssl);
1648  if (!file_exists($sslfile)) {
1649  throw new Exception(_('Private key not found'));
1650  }
1651  if (!is_readable($sslfile)) {
1652  throw new Exception(_('Private key not readable'));
1653  }
1654  $sslfilecontents = file_get_contents($sslfile);
1655  $priv_key = openssl_pkey_get_private($sslfilecontents);
1656  if (!$priv_key) {
1657  throw new Exception(_('Private key failed'));
1658  }
1659  $a_key = openssl_pkey_get_details($priv_key);
1660  $chunkSize = ceil($a_key['bits'] / 8);
1661  $output = array();
1662  foreach ((array) $dataArr as &$data) {
1663  $dataun = '';
1664  while ($data) {
1666  $chunk = substr($data, 0, $chunkSize);
1667  $data = substr($data, $chunkSize);
1668  $decrypt = '';
1669  $test = openssl_private_decrypt(
1670  $chunk,
1671  $decrypt,
1672  $priv_key,
1673  $padding
1674  );
1675  if (!$test) {
1676  throw new Exception(_('Failed to decrypt data on server'));
1677  }
1678  $dataun .= $decrypt;
1679  }
1680  unset($data);
1681  $output[] = $dataun;
1682  }
1683  openssl_free_key($priv_key);
1684 
1685  return (array) $output;
1686  }
1696  public static function parseMacList(
1697  $stringlist,
1698  $image = false,
1699  $client = false
1700  ) {
1701  $MAClist = array();
1702  $MACs = $stringlist;
1703  $lowerAndTrim = function ($element) {
1704  return strtolower(trim($element));
1705  };
1706  if (!is_array($stringlist)) {
1707  $MACs = array_map($lowerAndTrim, explode('|', $stringlist));
1708  } else {
1709  $MACs = array_map($lowerAndTrim, $stringlist);
1710  }
1711  $MACs = array_filter($MACs);
1712  $MACs = array_unique($MACs);
1713  $MACs = array_values($MACs);
1714  if (count($MACs) < 1) {
1715  return array();
1716  }
1717  $pending_filter = explode(
1718  ',',
1719  self::getSetting('FOG_QUICKREG_PENDING_MAC_FILTER')
1720  );
1721  $Ignore = array_map($lowerAndTrim, $pending_filter);
1722  $Ignore = array_filter($Ignore);
1723  if (count($Ignore) > 0) {
1724  $pattern = sprintf(
1725  '#%s#i',
1726  implode('|', (array) $Ignore)
1727  );
1728  $found_macs = preg_grep($pattern, $MACs);
1729  $MACs = array_diff($MACs, $found_macs);
1730  $MACs = array_filter($MACs);
1731  $MACs = array_unique($MACs);
1732  $MACs = array_values($MACs);
1733  }
1734  if (count($MACs) < 1) {
1735  return array();
1736  }
1737  $count = self::getClass('MACAddressAssociationManager')->count(
1738  array(
1739  'mac' => $MACs,
1740  'pending' => array(0, ''),
1741  )
1742  );
1743  if ($count > 0) {
1744  $existingMACs = self::getSubObjectIDs(
1745  'MACAddressAssociation',
1746  array(
1747  'mac' => $MACs,
1748  'pending' => array(0, ''),
1749  ),
1750  'mac'
1751  );
1752  $existingMACs = array_map($lowerAndTrim, $existingMACs);
1753  $existingMACs = array_filter($existingMACs);
1754  $existingMACs = array_unique($existingMACs);
1755  $existingMACs = array_values($existingMACs);
1756  $MACs = self::fastmerge((array) $MACs, (array) $existingMACs);
1757  $MACs = array_unique($MACs);
1758  }
1759  if ($client) {
1760  $clientIgnored = self::getSubObjectIDs(
1761  'MACAddressAssociation',
1762  array(
1763  'mac' => $MACs,
1764  'clientIgnore' => 1,
1765  ),
1766  'mac'
1767  );
1768  $clientIgnored = array_map($lowerAndTrim, $clientIgnored);
1769  $MACs = array_diff((array) $MACs, (array) $clientIgnored);
1770  unset($clientIgnored);
1771  }
1772  if ($image) {
1773  $imageIgnored = self::getSubObjectIDs(
1774  'MACAddressAssociation',
1775  array(
1776  'mac' => $MACs,
1777  'imageIgnore' => 1,
1778  ),
1779  'mac'
1780  );
1781  $imageIgnored = array_map($lowerAndTrim, (array) $imageIgnored);
1782  $MACs = array_diff((array) $MACs, (array) $imageIgnored);
1783  unset($imageIgnored);
1784  }
1785  $MACs = array_filter($MACs);
1786  $MACs = array_unique($MACs);
1787  $MACs = array_values($MACs);
1788  if (count($MACs) < 1) {
1789  return array();
1790  }
1791  $validMACs = array();
1792  foreach ($MACs as &$MAC) {
1793  $MAC = self::getClass('MACAddress', $MAC);
1794  if (!$MAC->isValid()) {
1795  continue;
1796  }
1797  $validMACs[] = $MAC;
1798  unset($MAC);
1799  }
1800  $validMACs = array_filter($validMACs);
1801 
1802  return $validMACs;
1803  }
1813  protected function sendData(
1814  $datatosend,
1815  $service = true,
1816  $array = array()
1817  ) {
1818  global $sub;
1819  if (false === $service) {
1820  return;
1821  }
1822  try {
1823  if (!self::$Host->isValid()) {
1824  throw new Exception('#!ih');
1825  }
1826  $datatosend = trim($datatosend);
1827  $curdate = self::niceDate();
1828  $secdate = self::niceDate(self::$Host->get('sec_time'));
1829  if ($curdate >= $secdate) {
1830  self::$Host
1831  ->set('pub_key', '')
1832  ->save()
1833  ->load();
1834  if (self::$newService || self::$json) {
1835  throw new Exception('#!ihc');
1836  }
1837  }
1838  if (self::$newService) {
1839  printf(
1840  '#!enkey=%s',
1841  self::certEncrypt($datatosend)
1842  );
1843  exit;
1844  } else {
1845  echo $datatosend;
1846  exit;
1847  }
1848  } catch (Exception $e) {
1849  if (self::$json) {
1850  if ($e->getMessage() === '#!ihc') {
1851  echo $e->getMessage();
1852  exit;
1853  }
1854  $repData = str_replace('#!', '', $e->getMessage());
1855  $array['error'] = $repData;
1856  $data = array('error' => $repData);
1857  if ($sub === 'requestClientInfo') {
1858  echo json_encode($array);
1859  exit;
1860  } else {
1861  return $data;
1862  }
1863  }
1864  throw new Exception($e->getMessage());
1865  }
1866  }
1876  protected static function arrayStrpos($haystack, $needles, $case = true)
1877  {
1878  $cmd = sprintf('str%spos', ($case ? 'i' : ''));
1879  $mapinfo = array();
1880  foreach ((array) $needles as &$needle) {
1881  $mapinfo[] = $cmd($haystack, $needle);
1882  unset($needle);
1883  }
1884  $mapinfo = array_filter($mapinfo);
1885 
1886  return count($mapinfo) > 0;
1887  }
1900  protected static function log(
1901  $txt,
1902  $curlog,
1903  $logfile,
1904  $logbrow,
1905  $obj,
1906  $level = 1
1907  ) {
1908  if (!is_string($txt)) {
1909  throw new Exception(_('Txt must be a string'));
1910  }
1911  if (!is_int($level)) {
1912  throw new Exception(_('Level must be an integer'));
1913  }
1914  if (self::$ajax) {
1915  return;
1916  }
1917  $findStr = array("\r", "\n", "\t", ' ,');
1918  $repStr = array('', ' ', ' ', ',');
1919  $txt = str_replace($findStr, $repStr, $txt);
1920  $txt = trim($txt);
1921  if (empty($txt)) {
1922  return;
1923  }
1924  $txt = sprintf('[%s] %s', self::niceDate()->format('Y-m-d H:i:s'), $txt);
1925  if ($curlog >= $level) {
1926  echo $txt;
1927  }
1928  self::logHistory($txt);
1929  }
1937  protected static function logHistory($string)
1938  {
1939  if (!is_string($string)) {
1940  throw new Exception(_('String must be a string'));
1941  }
1942  $string = sprintf(
1943  '[%s] %s',
1944  self::niceDate()->format('Y-m-d H:i:s'),
1945  $string
1946  );
1947  $string = trim($string);
1948  if (!$string) {
1949  return;
1950  }
1951  $name = (
1952  self::$FOGUser->isValid() ?
1953  self::$FOGUser->get('name') :
1954  'fog'
1955  );
1956  if (!self::$FOGUser->isValid()) {
1957  return;
1958  }
1959  if (self::$DB) {
1960  self::getClass('History')
1961  ->set('info', $string)
1962  ->set('ip', self::$remoteaddr)
1963  ->save();
1964  }
1965  }
1973  public function orderBy(&$orderBy)
1974  {
1975  if (empty($orderBy)) {
1976  $orderBy = 'name';
1977  if (!array_key_exists($orderBy, $this->databaseFields)) {
1978  $orderBy = 'id';
1979  }
1980  } else {
1981  if (!is_array($orderBy)) {
1982  $orderBy = trim($orderBy);
1983  if (!array_key_exists($orderBy, $this->databaseFields)) {
1984  $orderBy = 'name';
1985  }
1986  if (!array_key_exists($orderBy, $this->databaseFields)) {
1987  $orderBy = 'id';
1988  }
1989  }
1990  }
1991  }
2006  public static function getSubObjectIDs(
2007  $object = 'Host',
2008  $findWhere = array(),
2009  $getField = 'id',
2010  $not = false,
2011  $operator = 'AND',
2012  $orderBy = 'name',
2013  $groupBy = false,
2014  $filter = 'array_unique'
2015  ) {
2016  if (empty($object)) {
2017  $object = 'Host';
2018  }
2019  if (empty($getField)) {
2020  $getField = 'id';
2021  }
2022  if (empty($operator)) {
2023  $operator = 'AND';
2024  }
2025  if (is_array($getField)) {
2026  foreach ((array)$getField as &$field) {
2027  $data[$field] = self::getSubObjectIDs(
2028  $object,
2029  $findWhere,
2030  $field,
2031  $not,
2032  $operator,
2033  $orderBy,
2034  $groupBy,
2035  $filter
2036  );
2037  unset($field);
2038  }
2039  return $data;
2040  }
2041  return self::getClass($object)->getManager()->find(
2042  $findWhere,
2043  $operator,
2044  $orderBy,
2045  '',
2046  '',
2047  $groupBy,
2048  $not,
2049  $getField,
2050  '',
2051  $filter
2052  );
2053  }
2063  public static function getSetting($key)
2064  {
2065  if (!is_string($key)) {
2066  throw new Exception(_('Key must be a string'));
2067  }
2068  $findStr = '\r\n';
2069  $repStr = "\n";
2070  $value = self::getClass('Service')
2071  ->set('name', $key)
2072  ->load('name')
2073  ->get('value');
2074 
2075  return trim(
2076  str_replace(
2077  $findStr,
2078  $repStr,
2079  $value
2080  )
2081  );
2082  }
2093  public static function setSetting($key, $value)
2094  {
2095  self::getClass('ServiceManager')->update(
2096  array('name' => $key),
2097  '',
2098  array('value' => trim($value))
2099  );
2100  }
2106  public static function getQueuedStates()
2107  {
2108  return (array)TaskState::getQueuedStates();
2109  }
2115  public static function getQueuedState()
2116  {
2117  return TaskState::getQueuedState();
2118  }
2124  public static function getCheckedInState()
2125  {
2127  }
2133  public static function getProgressState()
2134  {
2135  return TaskState::getProgressState();
2136  }
2142  public static function getCompleteState()
2143  {
2144  return TaskState::getCompleteState();
2145  }
2151  public static function getCancelledState()
2152  {
2154  }
2164  public static function stringBetween($string, $start, $end)
2165  {
2166  $string = " $string";
2167  $ini = strpos($string, $start);
2168  if ($ini == 0) {
2169  return '';
2170  }
2171  $ini += strlen($start);
2172  $len = strpos($string, $end, $ini) - $ini;
2173 
2174  return substr($string, $ini, $len);
2175  }
2183  public static function stripAndDecode(&$item)
2184  {
2185  foreach ((array) $item as $key => &$val) {
2186  $tmp = str_replace(' ', '+', $val);
2187  $tmp = base64_decode($tmp);
2188  $tmp = trim($tmp);
2189  if (mb_detect_encoding($tmp, 'utf-8', true)) {
2190  $val = $tmp;
2191  }
2192  unset($tmp);
2193  $item[$key] = trim($val);
2194  unset($val);
2195  }
2196 
2197  return $item;
2198  }
2206  public static function getMasterInterface($ip_find)
2207  {
2208  if (count(self::$interface) > 0) {
2209  return self::$interface;
2210  }
2212  exec(
2213  "/sbin/ip route | grep '$ip_find' | awk -F'[ /]+' '/kernel.*src/ {print $4}'",
2214  $Interfaces,
2215  $retVal
2216  );
2217  $ip_find = trim($ip_find);
2218  if (!$ip_find) {
2219  return;
2220  }
2221  self::$interface = array();
2222  $index = 0;
2223  foreach ((array) self::$ips as &$ip) {
2224  $ip = trim($ip);
2225  if ($ip_find !== $ip) {
2226  continue;
2227  }
2228  self::$interface[] = $Interfaces[$index++];
2229  unset($ip);
2230  }
2231  if (count(self::$interface) < 1) {
2232  return false;
2233  }
2234 
2235  return array_shift(self::$interface);
2236  }
2242  protected static function getIPAddress($force = false)
2243  {
2244  if (!$force && count(self::$ips) > 0) {
2245  return self::$ips;
2246  }
2247  $output = array();
2248  exec(
2249  "/sbin/ip -4 addr | awk -F'[ /]+' '/global/ {print $3}'",
2250  $IPs,
2251  $retVal
2252  );
2253  if (!count($IPs)) {
2254  exec(
2255  "/sbin/ifconfig -a | awk -F'[ /:]+' '/(cast)/ {print $4}'",
2256  $IPs,
2257  $retVal
2258  );
2259  }
2260  natcasesort($IPs);
2261  $retIPs = function (&$IP) {
2262  $IP = trim($IP);
2263  if (!filter_var($IP, FILTER_VALIDATE_IP)) {
2264  $IP = gethostbyname($IP);
2265  }
2266  if (filter_var($IP, FILTER_VALIDATE_IP)) {
2267  return $IP;
2268  }
2269  };
2270  $retNames = function (&$IP) {
2271  $IP = trim($IP);
2272  if (filter_var($IP, FILTER_VALIDATE_IP)) {
2273  return gethostbyaddr($IP);
2274  }
2275 
2276  return $IP;
2277  };
2278  $IPs = array_map($retIPs, (array) $IPs);
2279  $Names = array_map($retNames, (array) $IPs);
2281  $IPs,
2282  $Names,
2283  array('127.0.0.1', '127.0.1.1')
2284  );
2285  unset($IPs, $Names);
2286  natcasesort($output);
2287  self::$ips = array_values(array_filter(array_unique((array) $output)));
2288 
2289  return self::$ips;
2290  }
2296  public static function lasterror()
2297  {
2298  $error = error_get_last();
2299 
2300  return sprintf(
2301  '%s: %s, %s: %s, %s: %s, %s: %s',
2302  _('Type'),
2303  $error['type'],
2304  _('File'),
2305  $error['file'],
2306  _('Line'),
2307  $error['line'],
2308  _('Message'),
2309  $error['message']
2310  );
2311  }
2319  public static function getFilesize($path)
2320  {
2321  $size = 0;
2322  if (is_dir($path)) {
2323  foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) {
2324  if ($file->getFilename() != ".") {
2325  $size += filesize($file);
2326  }
2327  }
2328  } else {
2329  $size = filesize($path);
2330  }
2331  return is_numeric($size) ? $size : 0;
2332  }
2340  public static function wakeUp($macs)
2341  {
2342  if (!is_array($macs)) {
2343  $macs = array($macs);
2344  }
2345  session_write_close();
2346  ignore_user_abort(true);
2347  set_time_limit(0);
2348  $macs = self::parseMacList($macs);
2349  if (count($macs) < 1) {
2350  return;
2351  }
2352  $macStr = implode(
2353  '|',
2354  $macs
2355  );
2356  $macStr = trim($macStr);
2357  if (empty($macStr)) {
2358  return;
2359  }
2360  $url = '%s://%s/fog/management/index.php?';
2361  $url .= 'node=client&sub=wakeEmUp';
2362  $nodeURLs = array();
2363  $macCount = count($macs);
2364  if ($macCount < 1) {
2365  return;
2366  }
2367  foreach ((array)self::getClass('StorageNodeManager')
2368  ->find(
2369  array('isEnabled' => 1)
2370  ) as &$Node
2371  ) {
2372  $ip = $Node->get('ip');
2373  $nodeURLs[] = sprintf(
2374  $url,
2375  self::$httpproto,
2376  $ip
2377  );
2378  unset($Node);
2379  }
2380  list(
2381  $gHost
2383  'Service',
2384  array(
2385  'name' => array(
2386  'FOG_WEB_HOST'
2387  ),
2388  ),
2389  'value',
2390  false,
2391  'AND',
2392  'name',
2393  false,
2394  ''
2395  );
2396  $ip = $gHost;
2397  $nodeURLs[] = $ip;
2398  $ret = self::$FOGURLRequests->process(
2399  $nodeURLs,
2400  'POST',
2401  array('mac' => $macStr),
2402  false,
2403  false,
2404  false,
2405  false
2406  );
2407  }
2415  public static function fastmerge($array1)
2416  {
2417  $others = func_get_args();
2418  array_shift($others);
2419  foreach ((array)$others as &$other) {
2420  foreach ((array)$other as $key => &$oth) {
2421  if (is_numeric($key)) {
2422  $array1[] = $oth;
2423  continue;
2424  } elseif (isset($array1[$key])) {
2425  $array1[$key] = $oth;
2426  continue;
2427  }
2428  unset($oth);
2429  }
2430  $array1 += $other;
2431  unset($other);
2432  }
2433 
2434  return $array1;
2435  }
2443  public static function getHash($file)
2444  {
2445  $filesize = self::getFilesize($file);
2446  $fp = fopen($file, 'r');
2447  if ($fp) {
2448  $data = fread($fp, 10485760);
2449  if ($filesize >= 20971520) {
2450  fseek($fp, -10485760, SEEK_END);
2451  $data .= fread($fp, 10485760);
2452  }
2453  fclose($fp);
2454  }
2455  return isset($data) ? hash('sha256', $data) : '';
2456  }
2465  public static function attemptLogin($username, $password)
2466  {
2467  return self::getClass('User')
2468  ->validatePw($username, $password);
2469  }
2475  public static function clearMACLookupTable()
2476  {
2477  $OUITable = self::getClass('OUI', '', true);
2478  $OUITable = $OUITable['databaseTable'];
2479  return self::$DB->query("TRUNCATE TABLE `$OUITable`");
2480  }
2486  public static function getMACLookupCount()
2487  {
2488  return self::getClass('OUIManager')->count();
2489  }
2497  public static function resolveHostname($host)
2498  {
2499  $host = trim($host);
2500  if (filter_var($host, FILTER_VALIDATE_IP)) {
2501  return $host;
2502  }
2503  $host = gethostbyname($host);
2504  $host = trim($host);
2505  return $host;
2506  }
2512  public static function getBroadcast()
2513  {
2514  $output = array();
2515  $cmd = sprintf(
2516  '%s | %s | %s',
2517  '/sbin/ip -4 addr',
2518  "awk -F'[ /]+' '/global/ {print $6}'",
2519  "grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
2520  );
2521  exec($cmd, $IPs, $retVal);
2522  if (!count($IPs)) {
2523  $cmd = sprintf(
2524  '%s | %s | %s | %s',
2525  '/sbin/ifconfig -a',
2526  "awk '/(cast)/ {print $3}'",
2527  "cut -d':' -f2",
2528  "grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
2529  );
2530  exec($cmd, $IPs, $retVal);
2531  }
2532  $IPs = array_map('trim', (array)$IPs);
2533  $IPs = array_filter($IPs);
2534  $IPs = array_values($IPs);
2535  return $IPs;
2536  }
2542  public static function randWait()
2543  {
2544  usleep(
2545  rand(
2546  5000,
2547  2000000
2548  )
2549  );
2550  }
2551 }
FOGBase\$fogpingactive
static $fogpingactive
Definition: fogbase.class.php:37
FOGBase\$pluginsinstalled
static $pluginsinstalled
Definition: fogbase.class.php:73
FOGBase\niceDate
static niceDate($date='now', $utc=false)
Definition: fogbase.class.php:1105
FOGBase\fatalError
static fatalError($txt, $data=array())
Definition: fogbase.class.php:700
exit
exit
Definition: download.php:102
FOGBase\getBroadcast
static getBroadcast()
Definition: fogbase.class.php:2512
FOGBase\$showhtml
static $showhtml
Definition: fogbase.class.php:292
FOGBase\getClass
static getClass($class, $data='', $props=false)
Definition: fogbase.class.php:439
FOGBase\getQueuedState
static getQueuedState()
Definition: fogbase.class.php:2115
$FOGPageManager
$FOGPageManager
Definition: index.php:23
FOGBase\info
static info($txt, $data=array())
Definition: fogbase.class.php:763
FOGBase\getIPAddress
static getIPAddress($force=false)
Definition: fogbase.class.php:2242
FOGBase\formatByteSize
static formatByteSize($size)
Definition: fogbase.class.php:1025
FOGBase\fastmerge
static fastmerge($array1)
Definition: fogbase.class.php:2415
$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
FOGBase\$FOGFTP
static $FOGFTP
Definition: fogbase.class.php:163
FOGBase\$FOGURLRequests
static $FOGURLRequests
Definition: fogbase.class.php:205
FOGBase\$defaultscreen
static $defaultscreen
Definition: fogbase.class.php:67
FOGBase\$strlen
static $strlen
Definition: fogbase.class.php:127
FOGBase\certEncrypt
static certEncrypt($data)
Definition: fogbase.class.php:1596
FOGBase\getHash
static getHash($file)
Definition: fogbase.class.php:2443
FOGBase\hex2bin
static hex2bin($hex)
Definition: fogbase.class.php:1424
FOGBase\getMasterInterface
static getMasterInterface($ip_find)
Definition: fogbase.class.php:2206
FOGBase\$EventManager
static $EventManager
Definition: fogbase.class.php:175
FOGBase\humanify
static humanify($diff, $unit)
Definition: fogbase.class.php:1340
FOGBase\$searchPages
static $searchPages
Definition: fogbase.class.php:265
FOGBase\error
static error($txt, $data=array())
Definition: fogbase.class.php:721
FOGBase\pluralize
static pluralize($count, $text, $space=false)
Definition: fogbase.class.php:1222
FOGBase\__construct
__construct()
Definition: fogbase.class.php:403
FOGBase\$newService
static $newService
Definition: fogbase.class.php:115
$test
$test
Definition: freespace.php:37
FOGBase\__toString
__toString()
Definition: fogbase.class.php:424
TaskState\getCompleteState
static getCompleteState()
Definition: taskstate.class.php:124
FOGBase\stringBetween
static stringBetween($string, $start, $end)
Definition: fogbase.class.php:2164
FOGBase\sendData
sendData( $datatosend, $service=true, $array=array())
Definition: fogbase.class.php:1813
TaskState\getProgressState
static getProgressState()
Definition: taskstate.class.php:109
FOGBase\$info
static $info
Definition: fogbase.class.php:139
$val
$val
Definition: check_node_exists.php:25
FOGBase\$_initialized
static $_initialized
Definition: fogbase.class.php:280
FOGBase\_setString
static _setString($txt, $data=array())
Definition: fogbase.class.php:682
FOGBase\$TimeZone
static $TimeZone
Definition: fogbase.class.php:187
FOGBase\$fogexportactive
static $fogexportactive
Definition: fogbase.class.php:49
FOGBase\formatTime
static formatTime($time, $format=false, $utc=false)
Definition: fogbase.class.php:1124
FOGBase\getQueuedStates
static getQueuedStates()
Definition: fogbase.class.php:2106
FOGBase\stripAndDecode
static stripAndDecode(&$item)
Definition: fogbase.class.php:2183
$keys
$keys
Definition: schema.php:1610
FOGBase\$locale
static $locale
Definition: fogbase.class.php:31
FOGBase\getMACLookupCount
static getMACLookupCount()
Definition: fogbase.class.php:2486
FOGBase\arrayRemove
static arrayRemove($key, array &$array)
Definition: fogbase.class.php:913
FOGBase\$service
static $service
Definition: fogbase.class.php:103
HookManager
Definition: hookmanager.class.php:25
FOGBase\certDecrypt
static certDecrypt($dataArr, $padding=true)
Definition: fogbase.class.php:1616
FOGBase\logHistory
static logHistory($string)
Definition: fogbase.class.php:1937
FOGBase\attemptLogin
static attemptLogin($username, $password)
Definition: fogbase.class.php:2465
$ret
$ret
Definition: bandwidth.php:112
FOGBase\$buildSelectBox
static $buildSelectBox
Definition: fogbase.class.php:145
$mac
$mac
Definition: hostinfo.php:151
$password
$password
Definition: checkcredentials.php:27
FOGBase\aesencrypt
static aesencrypt( $data, $key=false, $enctype='aes-256-cbc')
Definition: fogbase.class.php:1469
FOGBase\diff
static diff($start, $end, $ago=false)
Definition: fogbase.class.php:1247
FOGBase\orderBy
orderBy(&$orderBy)
Definition: fogbase.class.php:1973
FOGBase\$reqmethod
static $reqmethod
Definition: fogbase.class.php:235
FOGBase\$post
static $post
Definition: fogbase.class.php:97
FOGBase\$httphost
static $httphost
Definition: fogbase.class.php:304
TaskState\getCheckedInState
static getCheckedInState()
Definition: taskstate.class.php:94
FOGBase\$httpproto
static $httpproto
Definition: fogbase.class.php:298
FOGBase\_getUserAgent
static _getUserAgent()
Definition: fogbase.class.php:415
FOGBase\$ajax
static $ajax
Definition: fogbase.class.php:91
FOGBase\getFilesize
static getFilesize($path)
Definition: fogbase.class.php:2319
FOGBase\arrayInsertBefore
static arrayInsertBefore( $key, array &$array, $new_key, $new_value)
Definition: fogbase.class.php:855
FOGBase\getCompleteState
static getCompleteState()
Definition: fogbase.class.php:2142
FOGBase\$DB
static $DB
Definition: fogbase.class.php:157
FOGBase\validDate
static validDate($date, $format='')
Definition: fogbase.class.php:1184
FOGBase\_init
static _init()
Definition: fogbase.class.php:318
FOGBase\resolveHostname
static resolveHostname($host)
Definition: fogbase.class.php:2497
FOGBase\log
static log( $txt, $curlog, $logfile, $logbrow, $obj, $level=1)
Definition: fogbase.class.php:1900
$Host
$Host
Definition: hostnameloop.php:30
TaskState\getCancelledState
static getCancelledState()
Definition: taskstate.class.php:139
FOGBase\getProgressState
static getProgressState()
Definition: fogbase.class.php:2133
TaskState\getQueuedStates
static getQueuedStates()
Definition: taskstate.class.php:64
FOGBase\getActivePlugins
static getActivePlugins()
Definition: fogbase.class.php:661
FOGBase\$FOGUser
static $FOGUser
Definition: fogbase.class.php:193
FOGBase\getGlobalModuleStatus
static getGlobalModuleStatus($names=false, $keys=false)
Definition: fogbase.class.php:1042
TaskState\getQueuedState
static getQueuedState()
Definition: taskstate.class.php:79
Initiator\sanitizeItems
static sanitizeItems(&$value='')
Definition: init.php:311
$MACs
$MACs
Definition: hostinfo.php:152
FOGBase\$querystring
static $querystring
Definition: fogbase.class.php:223
FOGBase\lasterror
static lasterror()
Definition: fogbase.class.php:2296
FOGBase\aesdecrypt
static aesdecrypt( $encdata, $key=false, $enctype='aes-128-cbc')
Definition: fogbase.class.php:1536
FOGBase\$httpreqwith
static $httpreqwith
Definition: fogbase.class.php:229
FOGBase\parseMacList
static parseMacList( $stringlist, $image=false, $client=false)
Definition: fogbase.class.php:1696
$str
if(! $Image->isValid()) $str
Definition: progress.php:41
FOGBase
Definition: fogbase.class.php:25
FOGBase\$mySchema
static $mySchema
Definition: fogbase.class.php:286
FOGBase\redirect
static redirect($url='')
Definition: fogbase.class.php:831
FOGBase\$FOGSubMenu
static $FOGSubMenu
Definition: fogbase.class.php:211
FOGBase\$selected
static $selected
Definition: fogbase.class.php:151
FOGBase\wakeUp
static wakeUp($macs)
Definition: fogbase.class.php:2340
$path
if(!is_string($_GET['path'])) $path
Definition: getfiles.php:31
FOGBase\$ips
static $ips
Definition: fogbase.class.php:253
$count
$count
Definition: locationcheck.php:25
FOGBase\$remoteaddr
static $remoteaddr
Definition: fogbase.class.php:241
$output
$output
Definition: freespace.php:36
$interface
$interface
Definition: bandwidth.php:79
FOGBase\arrayStrpos
static arrayStrpos($haystack, $needles, $case=true)
Definition: fogbase.class.php:1876
FOGBase\createSecToken
static createSecToken()
Definition: fogbase.class.php:1445
FOGBase\getCheckedInState
static getCheckedInState()
Definition: fogbase.class.php:2124
FOGBase\getAllBlamedNodes
static getAllBlamedNodes($Host)
Definition: fogbase.class.php:618
FOGBase\$HookManager
static $HookManager
Definition: fogbase.class.php:181
FOGBase\$fogdeleteactive
static $fogdeleteactive
Definition: fogbase.class.php:43
FOGBase\$useragent
static $useragent
Definition: fogbase.class.php:79
FOGBase\$FOGPageManager
static $FOGPageManager
Definition: fogbase.class.php:199
FOGBase\$json
static $json
Definition: fogbase.class.php:109
FOGBase\$debug
static $debug
Definition: fogbase.class.php:133
FOGBase\$pendingMACs
static $pendingMACs
Definition: fogbase.class.php:55
$data
$data
Definition: freespace.php:47
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
FOGBase\getHostItem
static getHostItem( $service=true, $encoded=false, $hostnotrequired=false, $returnmacs=false, $override=false, $mac=false)
Definition: fogbase.class.php:503
FOGBase\arrayInsertAfter
static arrayInsertAfter( $key, array &$array, $new_key, $new_value)
Definition: fogbase.class.php:885
$username
$username
Definition: checkcredentials.php:25
FOGBase\$Host
static $Host
Definition: fogbase.class.php:312
$foglang
$foglang['Display']
Definition: text.php:22
FOGBase\arrayChangeKey
static arrayChangeKey(array &$array, $old_key, $new_key)
Definition: fogbase.class.php:1378
FOGBase\resetRequest
static resetRequest()
Definition: fogbase.class.php:983
FOGBase\isLoaded
isLoaded($key)
Definition: fogbase.class.php:969
FOGBase\clearMACLookupTable
static clearMACLookupTable()
Definition: fogbase.class.php:2475
$msg
if($Host->isValid()) $msg
Definition: hostnameloop.php:42
FOGBase\$foglang
static $foglang
Definition: fogbase.class.php:85
FOGBase\debug
static debug($txt, $data=array())
Definition: fogbase.class.php:742
FOGBase\byteconvert
static byteconvert($kilobytes)
Definition: fogbase.class.php:1413
FOGBase\$scriptname
static $scriptname
Definition: fogbase.class.php:217
FOGBase\getMessages
static getMessages()
Definition: fogbase.class.php:795
$url
$url
Definition: kernelvers.php:27
FOGBase\$FOGCore
static $FOGCore
Definition: fogbase.class.php:169
FOGBase\setRequest
setRequest()
Definition: fogbase.class.php:1006
FOGBase\setMessage
static setMessage($txt, $data=array())
Definition: fogbase.class.php:784
FOGBase\randWait
static randWait()
Definition: fogbase.class.php:2542
FOGBase\getCancelledState
static getCancelledState()
Definition: fogbase.class.php:2151
FOGBase\$isLoaded
$isLoaded
Definition: fogbase.class.php:121
FOGBase\arrayFind
static arrayFind( $needle, array $haystack, $ignorecase=false)
Definition: fogbase.class.php:943
FOGBase\setSetting
static setSetting($key, $value)
Definition: fogbase.class.php:2093
FOGBase\getSetting
static getSetting($key)
Definition: fogbase.class.php:2063
$ip
if(!(isset($_POST['ip']) &&is_string($_POST['ip']))) if(!(isset($_POST['file']) &&is_string($_POST['file']))) if(!(isset($_POST['lines']) &&is_numeric($_POST['lines']))) if(!(isset($_POST['reverse']) &&is_numeric($_POST['reverse']))) $ip
Definition: logtoview.php:149
$host
$host
Definition: hostnameloop.php:27
Host
Definition: host.class.php:23
FOGBase\$httpreferer
static $httpreferer
Definition: fogbase.class.php:247
FOGBase\$pendingHosts
static $pendingHosts
Definition: fogbase.class.php:61
FOGBase\$interface
static $interface
Definition: fogbase.class.php:259