FOG en EHU  1.0.0(r304)
FOGenEHU
packages/web/commons/functions.include.php
Ir a la documentación de este archivo.
00001 <?php
00002 /*
00003  *  FOG is a computer imaging solution.
00004 *  Copyright (C) 2007  Chuck Syperski & Jian Zhang
00005 *
00006 *   This program is free software: you can redistribute it and/or modify
00007 *   it under the terms of the GNU General Public License as published by
00008 *   the Free Software Foundation, either version 3 of the License, or
00009 *   any later version.
00010 *
00011 *   This program is distributed in the hope that it will be useful,
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *   GNU General Public License for more details.
00015 *
00016 *   You should have received a copy of the GNU General Public License
00017 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 *
00019 *
00020 */
00021 
00022 define( "WIPE_FAST", 1 );
00023 define( "WIPE_NORMAL", 2 );
00024 define( "WIPE_FULL", 3 );
00025 
00026 define( "FOG_AV_SCANONLY", 1 );
00027 define( "FOG_AV_SCANQUARANTINE", 2 );
00028 
00029 function getFirstGroupNameByHostID( $conn, $hostid )
00030 /*
00031  * getGroupNameByHostname returns the group name
00032 * written to be able to add group data to a host's quick info
00033 *
00034 * Written by Sam Wilson 09/07/09
00035 * Modified by csyperski to use host id instead of name
00036 */
00037 {
00038         if( $conn != null && is_numeric( $hostid ) )
00039         {
00040 
00041                 $sql = "select gmGroupID from groupMembers where gmHostID = '". $hostid ."' order by gmID limit 1";
00042                         
00043                 $res = mysql_query($sql, $conn) or criticalError( mysql_error(), _("FOG:: Database Error!"));
00044                 if ( $ar = mysql_fetch_array( $res ) )
00045                 {
00046                         $groupId =  $ar["gmGroupID"];
00047                         $sql = "select groupName from groups where groupID = '". mysql_real_escape_string($groupId) ."'";
00048 
00049                         $res1 = mysql_query($sql, $conn) or criticalError( mysql_error(), _("FOG:: Database Error!"));
00050 
00051                         if ( $ar1 = mysql_fetch_array( $res1 ) )
00052                         {
00053                                 return $ar1["groupName"];
00054                         }
00055                 }
00056         }
00057         return null;
00058 }
00059 
00060 function createCronScheduledPackage( $conn, $blGroup, $groupHostID, $taskType, $m, $h, $dom, $mon, $dow, $blShutdown, $blPushSnapins, &$reason, $arg2=""  )
00061 {
00062         if( $conn != null && $groupHostID != null && is_numeric( $groupHostID ) && $taskType != null && $m != null && $h != null && $dom != null && $mon != null && $dow != null )
00063         {
00064                 // check is task already exists
00065                 $sql = "SELECT
00066                 COUNT(*) as cnt
00067                 FROM
00068                 scheduledTasks
00069                 WHERE
00070                 stActive = '1' and
00071                 stIsGroup = '" . ($blGroup ? "1" : "0") . "' and
00072                 stTaskType = '" . mysql_real_escape_string( $taskType ) . "' and
00073                 stType = 'C' and
00074                 stGroupHostID = '$groupHostID' and
00075                 stMinute = '$m' and
00076                 stHour = '$h' and
00077                 stDOM = '$dom' and
00078                 stMonth = '$mon' and
00079                 stDOW = '$dow'";
00080                 $res = mysql_query( $sql, $conn ) or die( mysql_error( $conn ) );
00081                 $cnt = 0;
00082                 while( $ar = mysql_fetch_array( $res ) )
00083                 {
00084                         $cnt = $ar["cnt"];
00085                 }
00086 
00087                 if ( $cnt == 0 )
00088                 {
00089                         $sql = "INSERT INTO
00090                         scheduledTasks
00091                         (stName, stDesc, stType, stTaskType, stMinute, stHour, stDOM, stMonth, stDOW, stIsGroup, stGroupHostID, stShutDown, stOther1, stOther2, stActive)
00092                         VALUES
00093                         ( 'Scheduled Task', '', 'C', '" . mysql_real_escape_string( $taskType ) . "', '" . mysql_real_escape_string( $m ) . "', '" . mysql_real_escape_string( $h ) . "', '" . mysql_real_escape_string( $dom ) . "', '" . mysql_real_escape_string( $mon ) . "', '" . mysql_real_escape_string( $dow ) . "', '" . ($blGroup ? "1" : "0") . "', '$groupHostID', '" . ($blShutdown ? "1" : "0" ) . "', '" . ( $blPushSnapins ? "1" : "0" ) . "', '" . mysql_real_escape_string( $arg2 ) . "','1' ) ";
00094                         if ( mysql_query( $sql, $conn ) )
00095                                 return true;
00096                         else
00097                                 $reason = mysql_error( $conn );
00098                 }
00099                 else
00100                 {
00101                         $reason = _("Task already exists.");
00102                 }
00103         }
00104         else
00105                 $reason = ("General Error");
00106         return false;
00107 }
00108 
00109 
00110 function createSingleRunScheduledPackage( $conn, $blGroup, $groupHostID, $taskType, $lngTime, $blShutdown, $blPushSnapins, &$reason, $arg2="" )
00111 {
00112         if( $conn != null && $groupHostID != null && is_numeric( $groupHostID ) && $taskType != null && $lngTime != null && is_numeric( $lngTime ) )
00113         {
00114                 // check is task already exists
00115                 $sql = "SELECT
00116                 COUNT(*) as cnt
00117                 FROM
00118                 scheduledTasks
00119                 WHERE
00120                 stActive = '1' and
00121                 stIsGroup = '" . ($blGroup ? "1" : "0") . "' and
00122                 stTaskType = '" . mysql_real_escape_string( $taskType ) . "' and
00123                 stType = 'S' and
00124                 stGroupHostID = '$groupHostID' and
00125                 stDateTime = '$lngTime'";
00126                 $res = mysql_query( $sql, $conn ) or die( mysql_error( $conn ) );
00127                 $cnt = 0;
00128                 while( $ar = mysql_fetch_array( $res ) )
00129                 {
00130                         $cnt = $ar["cnt"];
00131                 }
00132 
00133                 if ( $cnt == 0 )
00134                 {
00135                         $sql = "INSERT INTO
00136                         scheduledTasks
00137                         (stName, stDesc, stType, stTaskType, stIsGroup, stGroupHostID, stDateTime, stShutDown, stOther1, stOther2, stActive)
00138                         VALUES
00139                         ( 'Scheduled Task', '', 'S', '" . mysql_real_escape_string( $taskType ) . "', '" . ($blGroup ? "1" : "0") . "', '$groupHostID', '$lngTime', '" . ($blShutdown ? "1" : "0" ) . "', '" . ( $blPushSnapins ? "1" : "0" ) . "', '" . mysql_real_escape_string( $arg2 ) . "','1' ) ";
00140                         if ( mysql_query( $sql, $conn ) )
00141                                 return true;
00142                         else
00143                                 $reason = mysql_error( $conn );
00144                 }
00145                 else
00146                 {
00147                         $reason = _("Task already exists.");
00148                 }
00149         }
00150         return false;
00151 }
00152 
00153 function sysLinuxEncrypt( $conn, $string )
00154 {
00155         if ( $conn != null && $string !== null )
00156         {
00157                 $dir = getSetting( $conn, "FOG_UTIL_DIR" );
00158                 if ( $dir != null )
00159                 {
00160                         $bin = $dir . "/md5pass";
00161                         $output = null;
00162                         $intRet = null;
00163                         exec( $bin . " " . $string, $output, $intRet );
00164                         if ( $intRet == "0" )
00165                         {
00166                                 return $output[0];
00167                         }
00168                 }
00169         }
00170         return null;
00171 }
00172 
00173 function generatePXEMenu( $conn, $type, $masterpw, $memtestpw, $reginputpw, $regpw, $quickpw, $sysinfo, $debugpw, $timeout, $blHide, $adv, &$reason )
00174 {
00175         global $currentUser;
00176         if ( $conn != null  )
00177         {
00178                 $masterpw = trim( $masterpw );
00179 
00180                 if ( ! is_numeric($timeout) || $timeout <= 0 )
00181                 {
00182                         $reason = _("Invalid Timeout Value.");
00183                         return false;
00184                 }
00185 
00186                 if (  $type == "1" || $type == "2"  )
00187                 {
00188                         $strMenu = "";
00189                         if ( $type == "1" )
00190                         {
00191                                 if ( $masterpw != null )
00192                                 {
00193                                         $masterEnc = sysLinuxEncrypt( $conn, $masterpw );
00194                                         if ( $masterEnc != null )
00195                                         {
00196                                                 $encMemTest = "";
00197                                                 $encRegInput = "";
00198                                                 $encReg = "";
00199                                                 $encDebug = "";
00200                                                 $encQuick = "";
00201                                                 $encSysinfo = "";
00202                                                         
00203                                                 if ( $memtestpw != null )
00204                                                 {
00205                                                         $encMemTest = "MENU PASSWD " . sysLinuxEncrypt( $conn, $memtestpw );
00206                                                 }
00207 
00208                                                 if ( $reginputpw != null )
00209                                                 {
00210                                                         $encRegInput = "MENU PASSWD " . sysLinuxEncrypt( $conn, $reginputpw );
00211                                                 }
00212 
00213                                                 if ( $regpw != null )
00214                                                 {
00215                                                         $encReg = "MENU PASSWD " . sysLinuxEncrypt( $conn, $regpw );
00216                                                 }
00217 
00218                                                 if ( $debugpw != null )
00219                                                 {
00220                                                         $encDebug = "MENU PASSWD " . sysLinuxEncrypt( $conn, $debugpw );
00221                                                 }
00222 
00223                                                 if ( $quickpw != null )
00224                                                 {
00225                                                         $encQuick = "MENU PASSWD " . sysLinuxEncrypt( $conn, $quickpw );
00226                                                 }
00227 
00228                                                 if ( $sysinfo != null )
00229                                                         $encSysinfo = "MENU PASSWD " . sysLinuxEncrypt( $conn, $sysinfo );
00230                                                         
00231                                                 $strMenu = "DEFAULT vesamenu.c32
00232                                                 MENU TITLE "._("FOG Computer Cloning Solution")."
00233                                                 MENU BACKGROUND fog/bg.png
00234                                                 MENU MASTER PASSWD " . $masterEnc . "
00235                                                 " . (( $blHide ) ? "MENU HIDDEN" : "") . "
00236                                                 " . (( $blHide ) ? "MENU AUTOBOOT " : "") . "
00237 
00238                                                 menu color title        1;36;44    #ffffffff #00000000 std
00239 
00240                                                 LABEL fog.local
00241                                                 localboot 0
00242                                                 MENU DEFAULT
00243                                                 MENU LABEL "._("Boot from hard disk")."
00244                                                 TEXT HELP
00245                                                 "._("Boot from the local hard drive.
00246                                                                 If you are unsure, select this option.")."
00247                                                                 ENDTEXT
00248 
00249                                                                 LABEL fog.memtest
00250                                                                 $encMemTest
00251                                                                 kernel " . getSetting( $conn, "FOG_MEMTEST_KERNEL" ) . "
00252                                                                 MENU LABEL "._("Run Memtest86+")."
00253                                                                 TEXT HELP
00254                                                                 "._("Run Memtest86+ on the client computer.")."
00255                                                                 ENDTEXT
00256 
00257                                                                 LABEL fog.reg
00258                                                                 $encRegInput
00259                                                                 kernel " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00260                                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . " root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " IP=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=autoreg keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " web=" . getSetting( $conn, "FOG_WEB_HOST" ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " loglevel=4
00261                                                                 MENU LABEL "._("Quick Host Registration and Inventory")."
00262                                                                 TEXT HELP
00263                                                                 "._("Automatically register the client computer,
00264                                                                                 and perform a hardware inventory.")."
00265                                                                                 ENDTEXT
00266 
00267                                                                                 LABEL fog.reginput
00268                                                                                 $encReg
00269                                                                                 kernel " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00270                                                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . " root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=manreg keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " web=" . getSetting( $conn, "FOG_WEB_HOST" ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " loglevel=4
00271                                                                                 MENU LABEL "._("Perform Full Host Registration and Inventory")."
00272                                                                                 TEXT HELP
00273                                                                                 "._("Perform a full host registration on the client
00274                                                                                                 computer, perform a hardware inventory, and
00275                                                                                                 optionally image the host.")."
00276                                                                                                 ENDTEXT
00277 
00278                                                                                                 LABEL fog.quickimage
00279                                                                                                 $encQuick
00280                                                                                                 kernel " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00281                                                                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=quickimage keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " web=" . getSetting( $conn, "FOG_WEB_HOST" ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " loglevel=4
00282                                                                                                 MENU LABEL "._("Quick Image")."
00283                                                                                                 TEXT HELP
00284                                                                                                 "._("This mode will allow you to image this host quickly with
00285                                                                                                                 it's default assigned image.")."
00286                                                                                                                 ENDTEXT
00287 
00288                                                                                                                 LABEL fog.sysinfo
00289                                                                                                                 $encSysinfo
00290                                                                                                                 kernel " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00291                                                                                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=sysinfo keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " loglevel=4
00292                                                                                                                 MENU LABEL "._("Client System Information")."
00293                                                                                                                 TEXT HELP
00294                                                                                                                 "._("View basic client information such as MAC address
00295                                                                                                                                 and FOG compatibility.")."
00296                                                                                                                                 ENDTEXT
00297 
00298                                                                                                                                 LABEL fog.debug
00299                                                                                                                                 $encDebug
00300                                                                                                                                 kernel " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00301                                                                                                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=onlydebug keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . "
00302                                                                                                                                 MENU LABEL "._("Debug Mode")."
00303                                                                                                                                 TEXT HELP
00304                                                                                                                                 "._("Debug mode will load the boot image and load a prompt so
00305                                                                                                                                                 you can run any commands you wish.")."
00306                                                                                                                                                 ENDTEXT
00307 
00308                                                                                                                                                 PROMPT 0
00309                                                                                                                                                 TIMEOUT " . $timeout . "0
00310                                                                                                                                                 $adv";
00311 
00312 
00313 
00314                                         }
00315                                         else
00316                                         {
00317                                                 $reason = _("Encrypted Master Password is null.");
00318                                                 return false;
00319                                         }
00320                                 }
00321                                 else
00322                                 {
00323                                         $reason = _("Master Password is null.");
00324                                         return false;
00325                                 }
00326                         }
00327                         else
00328                         {
00329                                 $strMenu = "DISPLAY boot.txt
00330                                 DEFAULT fog.local
00331 
00332                                 LABEL fog.local
00333                                 localboot 0
00334 
00335                                 LABEL fog.memtest
00336                                 kernel " . getSetting( $conn, "FOG_MEMTEST_KERNEL" ) . "
00337 
00338                                 LABEL fog.reg
00339                                 kernel  " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00340                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=autoreg keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " web=" . getSetting( $conn, "FOG_WEB_HOST" ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " loglevel=4
00341 
00342                                 LABEL fog.reginput
00343                                 kernel  " . getSetting( $conn, "FOG_TFTP_PXE_KERNEL" ) . "
00344                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting( $conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=manreg keymap=" . getSetting( $conn, "FOG_KEYMAP" ) . " web=" . getSetting( $conn, "FOG_WEB_HOST" ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " loglevel=4
00345 
00346                                 PROMPT 1
00347                                 TIMEOUT " . $timeout . "0
00348                                 $adv";
00349                         }
00350 
00351                         $tmp = createPXEFile( $strMenu );
00352                         if( $tmp !== null )
00353                         {
00354 
00355 
00356                                 $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
00357                                 $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
00358                                 if ($ftp && $ftp_loginres )
00359                                 {
00360                                         if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . "default", $tmp, FTP_ASCII ) )
00361                                         {
00362                                                 @ftp_close($ftp);
00363                                                 @unlink( $tmp );
00364 
00365                                                 setSetting( $conn, "FOG_PXE_MENU_TIMEOUT", $timeout );
00366                                                 setSetting( $conn, "FOG_PXE_ADVANCED", $adv );
00367                                                 if ( $type == "1" )
00368                                                         setSetting( $conn, "FOG_PXE_MENU_HIDDEN", ($blHide) ? "1" : "0" );
00369 
00370                                                 return true;
00371                                         }
00372                                         else
00373                                         {
00374                                                 $reason = _("Unable to upload file.");
00375                                         }
00376                                 }
00377                                 else
00378                                 {
00379                                         $reason = _("Unable to connect to tftp server.");
00380                                 }
00381                                 @ftp_close($ftp);
00382                                 @unlink( $tmp );
00383 
00384                         }
00385                         else
00386                                 $reason = _("Failed to open tmp file.");
00387 
00388                 }
00389                 else
00390                         $reason = _("Invalid PXE Menu Type.");
00391         }
00392         else
00393         {
00394                 $reason = _("Database connection was null");
00395         }
00396         return false;
00397 }
00398 
00399 function getImageName( $conn, $hostID )
00400 {
00401         if ( $conn != null && is_numeric( $hostID ) )
00402         {
00403                 $sql = "SELECT
00404                 imageName
00405                 FROM
00406                 ( SELECT * FROM hosts WHERE hostID = '$hostID') hosts
00407                 INNER JOIN images on ( hosts.hostImage = images.imageID )";
00408                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
00409                 while( $ar = mysql_fetch_array( $res ) )
00410                 {
00411                         return $ar["imageName"];
00412                 }
00413         }
00414         return null;
00415 }
00416 
00417 // Log imaging start and end times
00418 // type is either "s" for start or "e" for end
00419 function logImageTask( $conn, $type, $hostid, $imageName=null)
00420 {
00421         if ( $conn != null && is_numeric($hostid) )
00422         {
00423                 if ( $type == "dm" || $type == "su" || $type == "du" )
00424                 {
00425                         $sql = "INSERT INTO imagingLog(ilHostID, ilTime, ilText, ilType) values('$hostid', NOW(), '" . mysql_real_escape_string( $imageName ) . "', '$type')";
00426                         #echo ( $sql );
00427                         if ( ! mysql_query( $sql, $conn ) )
00428                                 criticalError( mysql_error(), _("FOG :: Database error!") );
00429                 }
00430                 else if ( $type == "en" || $type == "er" )
00431                 {
00432                         $sql = "SELECT MAX(ilID) as ilID FROM imagingLog WHERE ilHostID = '$hostid'";
00433 
00434                         $res = mysql_query( $sql, $conn );
00435 
00436                         if ( $ar = mysql_fetch_array( $res ) )
00437                         {
00438                                 #                               $sql = "UPDATE imagingLog set ilFinishTime = NOW() where ilID = '" . $ar["ilID"] . "'";
00439                                 $sql = "INSERT INTO imagingLog(ilRef, ilHostID, ilTime, ilText, ilType) values ('" . $ar["ilID"] . "','$hostid', NOW(), '" . mysql_real_escape_string( $imageName ) . "', '$type')";
00440                                 if ( ! mysql_query( $sql, $conn ) )
00441                                         criticalError( mysql_error(), _("FOG :: Database error!") );
00442                         }
00443                 }
00444         }
00445 }
00446 
00447 function setHostModuleStatus( $conn, $state, $hostid, $moduleid )
00448 {
00449         $state = mysql_real_escape_string( $state );
00450         $hostid = mysql_real_escape_string( $hostid );
00451         $moduleid = mysql_real_escape_string( $moduleid );
00452 
00453         if ( $conn != null )
00454         {
00455                 if ( is_numeric( $state ) && ( $state == "1" || $state == "0" ) )
00456                 {
00457                         if ( is_numeric( $hostid ) )
00458                         {
00459                                 $sql = "SELECT
00460                                 count(*) as cnt
00461                                 FROM
00462                                 moduleStatusByHost
00463                                 WHERE
00464                                 msHostID = '$hostid' and
00465                                 msModuleID = '$moduleid'";
00466 
00467                                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00468                                 $blExists = false;
00469                                 if ( $ar = mysql_fetch_array( $res ) )
00470                                 {
00471                                         if ( $ar["cnt"] > 0 )
00472                                                 $blExists = true;
00473                                 }
00474 
00475                                 $sql = "";
00476                                 if ( $blExists )
00477                                 {
00478                                         $sql = "UPDATE moduleStatusByHost set msState = '$state' WHERE msHostID = '$hostid' and msModuleID = '$moduleid'";
00479                                 }
00480                                 else
00481                                 {
00482                                         $sql = "INSERT INTO moduleStatusByHost(msHostID, msModuleID, msState) values('$hostid', '$moduleid', '$state')";
00483                                 }
00484 
00485                                 if ( ! mysql_query( $sql, $conn ) )
00486                                         criticalError( mysql_error(), _("FOG :: Database error!") );
00487                                 else
00488                                         return true;
00489 
00490                         }
00491                 }
00492         }
00493         return false;
00494 }
00495 
00496 function userCleanupUserExists( $conn, $name, $id=-1 )
00497 {
00498         if ( $conn != null && $name != null )
00499         {
00500                 $sql = "select count(*) as cnt from userCleanup where ucName = '" . $name . "' and ucID <> $id";
00501                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00502                 if ( $ar = mysql_fetch_array( $res ) )
00503                 {
00504                         if ( $ar["cnt"] == 0 )
00505                                 return false;
00506                 }
00507         }
00508 }
00509 
00510 function dircleanDirExists( $conn, $name, $id=-1 )
00511 {
00512         if ( $conn != null && $name != null )
00513         {
00514                 $sql = "select count(*) as cnt from dirCleaner where dcPath = '" . $name . "' and dcID <> $id";
00515                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00516                 if ( $ar = mysql_fetch_array( $res ) )
00517                 {
00518                         if ( $ar["cnt"] == 0 )
00519                                 return false;
00520                 }
00521         }
00522 }
00523 
00524 function getSettingCats( $conn )
00525 {
00526         $arCats = array();
00527         if ( $conn != null )
00528         {
00529                 $sql = "SELECT settingCategory FROM globalSettings GROUP BY settingCategory ORDER BY settingCategory";
00530                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00531                 while( $ar = mysql_fetch_array( $res ) )
00532                 {
00533                         $arCats[] = $ar["settingCategory"];
00534                 }
00535         }
00536         return $arCats;
00537 }
00538 
00539 function getStats ( $conn )
00540 {
00541         if ( $conn != null )
00542         {
00543                 $UUID = "?uuid=".getSetting ( $conn, "FOG_UUID")."&v=".FOG_VERSION;
00544 
00545                 $sql = "SELECT GROUP_CONCAT(cnt) as gc FROM (SELECT count(*) as cnt FROM users
00546                 UNION ALL SELECT count(*) FROM profiles UNION ALL SELECT count(*) FROM hosts
00547                 UNION ALL SELECT count(*) FROM groups UNION ALL SELECT count(*) FROM images
00548                 UNION ALL SELECT count(*) FROM imagingLog UNION ALL SELECT count(*) FROM imagingLog where ilType='du'
00549                 UNION ALL SELECT count(*) FROM imagingLog where ilType='dm' UNION ALL SELECT count(*) FROM multicastSessions
00550                 UNION ALL SELECT count(*) FROM snapins UNION ALL SELECT count(*) FROM snapins where sType='v'
00551                 UNION ALL SELECT count(*) FROM snapins where sType='z' UNION ALL SELECT count(*) FROM snapins where sType='r'
00552                 UNION ALL SELECT count(*) FROM snapins where sType='i'UNION ALL SELECT count(*) FROM snapinJobs) t1;";
00553                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00554                 while( $ar = mysql_fetch_array( $res ) )
00555                 {
00556                         return $UUID.",".$ar["gc"];
00557                 }
00558 
00559                 return ($UUID);
00560 
00561 
00562         }
00563         return "";
00564 }
00565 
00566 function getSetting( $conn, $key )
00567 {
00568         if ( $conn != null )
00569         {
00570                 $key = mysql_real_escape_string( $key );
00571                 $sql = "SELECT settingValue FROM globalSettings WHERE settingKey = '$key'";
00572                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00573                 while( $ar = mysql_fetch_array( $res ) )
00574                 {
00575                         return $ar["settingValue"];
00576                 }
00577         }
00578         return "";
00579 }
00580 
00581 function setSetting( $conn, $key, $value )
00582 {
00583         if ( $conn != null )
00584         {
00585                 $key = mysql_real_escape_string( $key );
00586                 $value = mysql_real_escape_string( $value );
00587                 $sql = "UPDATE globalSettings SET settingValue =  '$value' WHERE settingKey = '$key' limit 1";
00588                 if ( mysql_query( $sql, $conn ) )
00589                         return true;
00590                 else
00591                         return false;
00592         }
00593         return false;
00594 }
00595 
00596 // returns the # of tasks killed
00597 function removeAllTasksForHostMac( $conn, $mac )
00598 {
00599         if ( $conn != null )
00600         {
00601                 $macColon = str_replace ( "-", ":", strtolower($mac) );
00602 
00603                 $sql = "SELECT
00604                 hostID
00605                 FROM
00606                 hosts
00607                 WHERE
00608                 hostMAC = '$macColon'";
00609 
00610                 $res = mysql_query( $sql, $conn );
00611 
00612                 while( $ar = mysql_fetch_array( $res ) )
00613                 {
00614                         return removeAllTasksForHostID( $conn, $ar["hostID"] );
00615                 }
00616                 return $num;
00617         }
00618         return 0;
00619 }
00620 
00621 function removeAllTasksForHostID( $conn, $id )
00622 {
00623         if ( $conn != null && is_numeric( $id ) )
00624         {
00625                 $sql = "SELECT
00626                 taskID, hostMAC
00627                 FROM
00628                 tasks
00629                 inner join (SELECT * FROM hosts where hostID = '$id' ) hosts on (hosts.hostID = tasks.taskHostID)";
00630 
00631                 $res = mysql_query( $sql, $conn );
00632                 if ( $res )
00633                 {
00634                         $num = 0;
00635                         while( $ar = mysql_fetch_array( $res ) )
00636                         {
00637                                 $macDash = str_replace ( ":", "-", strtolower($ar["hostMAC"]) );
00638 
00639                                 @ftpDelete( getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . "01-" . $macDash );
00640                                         
00641                                 $sql = "delete from tasks where taskID = '" . mysql_real_escape_string( $ar["taskID"] ) . "' limit 1";
00642                                 if ( mysql_query( $sql, $conn ) )
00643                                 {
00644                                         $num++;
00645                                 }
00646                         }
00647                         return $num;
00648                 }
00649         }
00650         return 0;
00651 }
00652 
00653 function userTrackerActionToString( $code )
00654 {
00655         switch( $code )
00656         {
00657                 case "0":
00658                         return _("Logout");
00659                         break;
00660                 case "1":
00661                         return _("Login");
00662                         break;
00663                 case "99":
00664                         return _("Service Start");
00665                         break;
00666                 default:
00667                         return "N/A";
00668                         break;
00669         }
00670 }
00671 
00672 function criticalError( $description, $title="FOG :: Critical Error!")
00673 {
00674         echo "<div class=\"errorBox\">";
00675         echo "<h2>";
00676         echo $title;
00677         echo "</h2>";
00678         echo "<b>"._("Description").":</b> " . $description;
00679         echo "</div>";
00680         exit;
00681 }
00682 
00683 function isSafeHostName( $hostname )
00684 {
00685 
00686         return ( ereg( "^[0-9a-zA-Z_\-]*$", $hostname ) && strlen($hostname ) > 0 && strlen( $hostname ) <= 15  );
00687 }
00688 
00689 function isValidIPAddress( $ip )
00690 {
00691         $ar = explode( ".", $ip );
00692 
00693         if (count($ar) != 4 ) return false;
00694 
00695         for($i=0;$i<count($ar);$i++)
00696         {
00697                 if ( $ar[$i] === null || ! is_numeric( $ar[$i] ) || $ar[$i] < 0 || $ar[$i] > 255 ) return false;
00698         }
00699         return true;
00700 }
00701 
00702 function isValidMACAddress( $mac )
00703 {
00704         return ereg( "^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$", $mac );
00705 }
00706 
00707 function doAllMembersHaveSameImage( $members )
00708 {
00709         #return true;
00710         if ( $members !== null )
00711         {
00712                 $firstImageDef = null;
00713                 for( $i = 0; $i < count( $members ); $i++ )
00714                 {
00715                         $currentImageDef = $members[$i]->getImageID();
00716                         if( $i == 0 )
00717                                 $firstImageDef = $currentImageDef;
00718 
00719                         if ( $currentImageDef === null || $currentImageDef != $firstImageDef || $currentImageDef < 0 || ! is_numeric( $currentImageDef ) )
00720                                 return false;
00721                 }
00722 
00723                 if ( $firstImageDef !== null ) return true;
00724         }
00725         return false;
00726 }
00727 
00728 function getCurrentDBVersion($conn)
00729 {
00730         if ( $conn )
00731         {
00732                 @mysql_select_db( MYSQL_DATABASE );
00733                 $sql_version = "select vValue FROM schemaVersion";
00734                 $res_version = mysql_query( $sql_version, $conn );
00735                 if ( $ar = @mysql_fetch_array( $res_version ) )
00736                 {
00737                         return $ar[0];
00738                 }
00739         }
00740         return 0;
00741 }
00742 
00743 function getOSNameByID( $conn, $osid )
00744 {
00745         if ( $conn != null )
00746         {
00747                 $sql = "select * from supportedOS where osValue = '" . mysql_real_escape_string($osid) . "'";
00748                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00749                 while( $ar = mysql_fetch_array( $res ) )
00750                 {
00751                         return $ar["osName"];
00752                 }
00753         }
00754         return null;
00755 }
00756 
00757 function getImageTypeDropDown( $name="imagetype", $selected=null )
00758 {
00759 
00760         $buffer = "<select name=\"$name\" size=\"1\">\n";
00761         $buffer .= "<option value=\"-1\" label=\"Select One\">"._("Select One")."</option>\n";
00762         $tmpType = _("Single Partition (NTFS Only, Resizable)");
00763         $sel = "";
00764         if ( $selected == ImageMember::IMAGETYPE_PARTITION && $selected !== null && $selected != "")
00765                 $sel = "selected=\"selected\"";
00766         $buffer .= "<option value=\"" . ImageMember::IMAGETYPE_PARTITION . "\" label=\"$tmpType\" $sel>" . $tmpType . "</option>\n";
00767 
00768         $tmpType = _("Multiple Partition Image - Single Disk (Not Resizable)");
00769         $sel = "";
00770         if ( $selected == ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE )
00771                 $sel = "selected=\"selected\"";
00772         $buffer .= "<option value=\"" . ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE . "\" label=\"$tmpType\" $sel>" . $tmpType . "</option>\n";
00773 
00774         #               $tmpType = _("Multiple Partition Image - All Disks  (Not Resizable)");
00775         #               $sel = "";
00776         #               if ( $selected == ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES )
00777         #                       $sel = "selected=\"selected\"";
00778         #               $buffer .= "<option value=\"" . ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES . "\" label=\"$tmpType\" $sel>" . $tmpType . "</option>\n";
00779 
00780         $tmpType = _("Raw Image (Sector By Sector, DD, Slow)");
00781         $sel = "";
00782         if ( $selected == ImageMember::IMAGETYPE_DISKIMAGE )
00783                 $sel = "selected=\"selected\"";
00784         $buffer .= "<option value=\"" . ImageMember::IMAGETYPE_DISKIMAGE . "\" label=\"$tmpType\" $sel>" . $tmpType . "</option>\n";
00785         $buffer .= "</select>\n";
00786         return $buffer;
00787 
00788 }
00789 
00790 function getOSDropDown( $conn, $name="os", $selected=null )
00791 {
00792         if ( $conn != null )
00793         {
00794                 $sql = "select * from supportedOS order by osValue";
00795                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00796                 $buffer = "<select name=\"$name\" size=\"1\">\n";
00797                 $buffer .= "<option value=\"-1\" label=\"Select One\">"._("Select One")."</option>\n";
00798                 while( $ar = mysql_fetch_array( $res ) )
00799                 {
00800                         $sel = "";
00801                         if ( $selected == $ar["osValue"] )
00802                                 $sel = "selected=\"selected\"";
00803                         $buffer .= "<option value=\"" . $ar["osValue"] . "\" label=\"" . $ar["osName"] . "\" $sel>" . $ar["osName"] . " (" . $ar["osValue"] . ")</option>\n";
00804                 }
00805                 $buffer .= "</select>\n";
00806                 return $buffer;
00807         }
00808         return null;
00809 }
00810 
00811 function getImageDropDown( $conn, $name="image", $selected=null )
00812 {
00813         if ( $conn != null )
00814         {
00815                 $sql = "select * from images order by imageName";
00816                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00817                 $buffer = "<select name=\"$name\" size=\"1\">\n";
00818                 $buffer .= "<option value=\"-1\" label=\"Select One\">"._("Select One")."</option>\n";
00819                 while( $ar = mysql_fetch_array( $res ) )
00820                 {
00821                         $sel = "";
00822                         if ( $selected == $ar["imageID"] )
00823                                 $sel = "selected=\"selected\"";
00824                         $buffer .= "<option value=\"" . $ar["imageID"] . "\" label=\"" . $ar["imageName"] . " (" . $ar["imageID"] . ") \" $sel>" . $ar["imageName"] . " (" . $ar["imageID"] . ")</option>\n";
00825                 }
00826                 $buffer .= "</select>\n";
00827                 return $buffer;
00828         }
00829         return null;
00830 }
00831 
00832 function getSnapinDropDown( $conn, $name="snap", $selected=null, $stype=null, $id=null )
00833 {
00834         if ( $conn != null )
00835         {
00836                 $sql = "SELECT * FROM snapins WHERE sID>0 ORDER BY sName";
00837 
00838                 if ( $stype != null && $id != null)
00839                 {
00840                         if ( $stype == "host" )
00841                         {
00842                                 $sql = "SELECT sName, sID, saOrder, saID
00843                                 FROM snapinAssoc
00844                                 INNER JOIN snapins ON ( snapinAssoc.saSnapinID = snapins.sID )
00845                                 WHERE snapinAssoc.saHostID = '$id' ORDER BY saOrder";
00846                         }
00847                         else if ( $stype == "group" )
00848                         {
00849                                 $sql = "select sID, sName from (SELECT count(*) as cnt, sID, sName FROM snapinAssoc inner join snapins on ( snapinAssoc.saSnapinID = snapins.sID ) WHERE saHostID in (select gmHostID from groupMembers inner join hosts on (hosts.hostID=groupMembers.gmHostID) where gmGroupID ='".$id."') GROUP BY sID) T1 where cnt = (select count(*) from groupMembers inner join hosts on (hosts.hostID=groupMembers.gmHostID) where gmGroupID ='".$id."');";
00850                         }
00851 
00852                 }
00853                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00854                 $buffer = "<select name=\"$name\" size=\"1\">\n";
00855                 $buffer .= "<option value=\"-1\" label=\""._("Select One")."\">"._("Select One")."</option>\n";
00856                 while( $ar = mysql_fetch_array( $res ) )
00857                 {
00858                         $sel = "";
00859                         if ( $selected == $ar["sName"] )
00860                                 $sel = "selected=\"selected\"";
00861                         $buffer .= "<option value=\"" . $ar["sID"] . "\" label=\"" . $ar["sName"] . "\" $sel>" . $ar["sName"] . "</option>\n";
00862                 }
00863                 $buffer .= "</select>\n";
00864                 return $buffer;
00865         }
00866         return null;
00867 }
00868 
00869 function addPrinter( $conn, $hostId, $printerId )
00870 {
00871         if ( $conn != null )
00872         {
00873                 $host = mysql_real_escape_string( $hostId );
00874                 $printer = mysql_real_escape_string( $printerId );
00875 
00876                 $sql = "select count(*) as cnt from printerAssoc where paPrinterID = '$printer' and paHostID = '$host'";
00877                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00878                 if ( $ar = mysql_fetch_array( $res ) )
00879                 {
00880                         if ( $ar["cnt"] == 0 )
00881                         {
00882                                 $sql = "select count(*) as cnt from printerAssoc where paHostID = '$host' and paIsDefault = '1'";
00883                                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00884                                 if ( $ar = mysql_fetch_array( $res ) )
00885                                 {
00886                                         $default = "0";
00887                                         if ( $ar["cnt"] == 0 )
00888                                         {
00889                                                 $default = "1";
00890                                         }
00891                                         $sql = "INSERT INTO
00892                                         printerAssoc( paHostID, paPrinterID, paIsDefault )
00893                                         values( '$host', '$printer', '$default' )";
00894 
00895                                         if ( mysql_query( $sql, $conn ) )
00896                                         {
00897                                                 return true;
00898                                         }
00899                                 }
00900                         }
00901 
00902                 }
00903         }
00904         return false;
00905 }
00906 
00907 function deletePrinter( $conn, $printerAssocId )
00908 {
00909         if ( $conn != null )
00910         {
00911                 $printer = mysql_real_escape_string( $printerAssocId );
00912                 $sql = "delete from printerAssoc where paID = '$printer'";
00913                 if ( mysql_query( $sql, $conn ) )
00914                         return true;
00915         }
00916         return false;
00917 }
00918 
00919 function deletePrinterByHost( $conn, $printerId, $hostId )
00920 {
00921         if ( $conn != null )
00922         {
00923                 $printer = mysql_real_escape_string( $printerId );
00924                 $host = mysql_real_escape_string( $hostId );
00925                 $sql = "delete from printerAssoc where paPrinterID = '$printer' and paHostID = '$host'";
00926                 if ( mysql_query( $sql, $conn ) )
00927                         return true;
00928         }
00929         return false;
00930 }
00931 
00932 function setDefaultPrinter( $conn, $printerAssocId )
00933 {
00934         if ( $conn != null )
00935         {
00936                 $printer = mysql_real_escape_string( $printerAssocId );
00937                 $sql = "select paHostID from printerAssoc where paID = '$printer'";
00938                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );;
00939                 $hostid = null;
00940                 while( $ar = mysql_fetch_array( $res ) )
00941                 {
00942                         $hostid = $ar["paHostID"];
00943                         if ( $hostid !== null && is_numeric($hostid) )
00944                         {
00945                                 $sql = "update printerAssoc set paIsDefault = '0' where paHostID = '$hostid'";
00946                                 if ( mysql_query( $sql, $conn ) )
00947                                 {
00948                                         $sql = "update printerAssoc set paIsDefault = '1' where paID = '$printer'";
00949                                         return mysql_query( $sql, $conn );
00950                                 }
00951                         }
00952                 }
00953         }
00954         return false;
00955 }
00956 
00957 function getPrinterDropDown( $conn, $name="printer", $selected=null )
00958 {
00959         if ( $conn != null )
00960         {
00961                 $sql = "select * from printers order by pAlias";
00962                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
00963                 $buffer = "<select name=\"$name\" size=\"1\">\n";
00964                 $buffer .= "<option value=\"-1\" label=\""._("Select One")."\">"._("Select One")."</option>\n";
00965                 while( $ar = mysql_fetch_array( $res ) )
00966                 {
00967                         $sel = "";
00968                         if ( $selected == $ar["pID"] )
00969                                 $sel = "selected=\"selected\"";
00970                         $buffer .= "<option value=\"" . $ar["pID"] . "\" label=\"" . $ar["pAlias"] . "\" $sel>" . $ar["pAlias"] . "</option>\n";
00971                 }
00972                 $buffer .= "</select>\n";
00973                 return $buffer;
00974         }
00975         return null;
00976 }
00977 
00978 function isHostAssociatedWithSnapin( $conn, $hostid, $snapinid )
00979 {
00980         if ( $conn != null && $hostid !== null && $snapinid !== null && is_numeric( $hostid ) && is_numeric( $snapinid ) )
00981         {
00982                 $hostid = mysql_real_escape_string( $hostid );
00983                 $snapinid = mysql_real_escape_string( $snapinid );
00984                 $sql = "select count(*) as cnt from snapinAssoc where saHostID = '" . $hostid . "' and saSnapinID = '" . $snapinid . "'";
00985                 $res = mysql_query($sql, $conn) or criticalError( mysql_error(), _("FOG :: Database error!") );
00986                 while( $ar = mysql_fetch_array( $res ) )
00987                 {
00988                         return ($ar["cnt"] > 0);
00989 
00990                 }
00991                 return false;
00992         }
00993         return true;
00994 }
00995 
00996 function getHostWithSnapinAssoc( $conn, $hostid, $snapinid )
00997 {
00998         if ( $conn != null && $hostid !== null && $snapinid !== null && is_numeric( $hostid ) && is_numeric( $snapinid ) )
00999         {
01000                 $hostid = mysql_real_escape_string( $hostid );
01001                 $snapinid = mysql_real_escape_string( $snapinid );
01002                 $sql = "select saID as cnt from snapinAssoc where saHostID = '" . $hostid . "' and saSnapinID = '" . $snapinid . "'";
01003                 $res = mysql_query($sql, $conn) or criticalError( mysql_error(), _("FOG :: Database error!") );
01004                 while( $ar = mysql_fetch_array( $res ) )
01005                 {
01006                         return ($ar["cnt"]);
01007 
01008                 }
01009                 return null;
01010         }
01011         return null;
01012 }
01013 
01014 
01015 function maxHostSnapinOrder( $conn, $hostid )
01016 {
01017         if ( $conn != null && $hostid !== null && is_numeric( $hostid ) )
01018         {
01019                 $hostid = mysql_real_escape_string( $hostid );
01020                 $sql = "select max(saOrder)+1 as cnt from snapinAssoc where saHostID = '" . $hostid . "'";
01021                 $res = mysql_query($sql, $conn) or criticalError( mysql_error(), _("FOG :: Database error!") );
01022                 while( $ar = mysql_fetch_array( $res ) )
01023                 {
01024                         return ($ar["cnt"] );
01025 
01026                 }
01027                 return false;
01028         }
01029         return true;
01030 }
01031 
01032 
01033 function addSnapinToHost( $conn, $hostid, $snapinid, &$reason )
01034 {
01035         if ( $conn != null && $hostid !== null && $snapinid !== null && is_numeric( $hostid ) && is_numeric( $snapinid ) )
01036         {
01037                 if ( ! isHostAssociatedWithSnapin( $conn, $hostid, $snapinid ) )
01038                 {
01039                         $hostid = mysql_real_escape_string( $hostid );
01040                         $snapinid = mysql_real_escape_string( $snapinid );
01041                         $saorder =  maxHostSnapinOrder( $conn, $hostid );
01042                         $sql = "insert into snapinAssoc(saHostID, saSnapinID, saOrder) values('$hostid','$snapinid','$saorder')";
01043                         if( mysql_query( $sql, $conn ) )
01044                         {
01045                                 $reason = _("Snapin added to host.");
01046                                 return true;
01047                         }
01048                         else
01049                                 $reason = _("Database error").": " . mysql_error() ;
01050                 }
01051                 else
01052                         $reason = _("Snapin is already linked with this host.");
01053         }
01054         else
01055                 $reason = _("Either the database connection, snapid ID, or host ID was null.");
01056 
01057         return false;
01058 }
01059 
01060 function deleteSnapinFromHost( $conn, $hostid, $snapinid, &$reason )
01061 {
01062         if ( $conn != null && $hostid !== null && $snapinid !== null && is_numeric( $hostid ) && is_numeric( $snapinid ) )
01063         {
01064                 if ( isHostAssociatedWithSnapin( $conn, $hostid, $snapinid ) )
01065                 {
01066                         $hostid = mysql_real_escape_string( $hostid );
01067                         $snapinid = mysql_real_escape_string( $snapinid );
01068                         $sql = "delete from snapinAssoc where saHostID = '$hostid' and saSnapinID = '$snapinid'";
01069                         if( mysql_query( $sql, $conn ) )
01070                         {
01071                                 $reason = _("Snapin removed from host.");
01072                                 return true;
01073                         }
01074                         else
01075                                 $reason = _("Database error").": " . mysql_error() ;
01076                 }
01077                 else
01078                         $reason = _("Snapin is not linked with this host.");
01079         }
01080         else
01081                 $reason = _("Either the database connection, snapid ID, or host ID was null.");
01082 
01083         return false;
01084 }
01085 
01086 function upSnapinFromHost( $conn, $saID1, $saID2, &$reason )
01087 {
01088         if ( $conn != null && $saID1 !== null && $saID2 !== null && is_numeric( $saID1 ) && is_numeric( $saID2 ) )
01089         {
01090                 $saID1 = mysql_real_escape_string( $saID1 );
01091                 $saID2 = mysql_real_escape_string( $saID2 );
01092 
01093                 $sql = "SELECT
01094                 saOrder
01095                 FROM
01096                 snapinAssoc
01097                 WHERE
01098                 saID = '$saID1' OR
01099                 saID = '$saID2'
01100                 ORDER BY
01101                 saOrder;";
01102 
01103                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01104                 if ( $ar = mysql_fetch_array( $res ) )
01105                 {
01106                         $sql = "UPDATE snapinAssoc SET saOrder='" . $ar["saOrder"] . "' WHERE saID='$saID1'";
01107                         msgBox( $sql );
01108                         if (!mysql_query( $sql, $conn ) )
01109                                 die( mysql_error() );
01110                 }
01111                 if ( $ar = mysql_fetch_array( $res ) )
01112                 {
01113                         $sql = "UPDATE snapinAssoc SET saOrder='" . $ar["saOrder"] . "' WHERE saID='$saID2'";
01114                         if (!mysql_query( $sql, $conn ) )
01115                                 die( mysql_error() );
01116                 }
01117                 return true;
01118         }
01119         else
01120                 $reason = _("Either the database connection, snapid ID, or host ID was null.");
01121 
01122         return false;
01123 }
01124 
01135 function cancelSnapins( $conn, $arSnapinAndHostId )
01136 {
01137         if ( $conn != null  )
01138         {
01139                 for( $i=0; $i<count($arSnapinAndHostId); $i++)
01140                 {
01141 
01142                         $ar=explode("-",$arSnapinAndHostId[$i]);
01143                         $snapinid=$ar[0];
01144                         $hostid=$ar[1];
01145 
01146                         if ( !cancelSnapinsForHost( $conn, $hostid, $snapinid ) )
01147                                 //return "No se borran: " .$stDebug;
01148                                 return true;
01149                         lg( _("Snapin Task deleted")." :: " . $arSnapinAndHostId );
01150                 }
01151                 return true;
01152 
01153         }
01154         else
01155                 return "sin conexion";
01156 }
01157 
01158 
01159 function cancelSnapinsForHost( $conn, $hostid, $snapID = -1 )
01160 {
01161         if ( $conn != null && $hostid !== null && is_numeric( $hostid )  )
01162         {
01163                 $hostid = mysql_real_escape_string( $hostid );
01164                 $snapID = mysql_real_escape_string( $snapID );
01165                 $where = "";
01166                 if ( $snapID != -1 )
01167                 {
01168                         $where = " and stSnapinID = '$snapID' ";
01169                 }
01170 
01171                 $sql = "SELECT
01172                 stID
01173                 FROM
01174                 snapinTasks
01175                 inner join snapinJobs on ( snapinTasks.stJobID = snapinJobs.sjID )
01176                 WHERE
01177                 sjHostID = '$hostid' and
01178                 stState in ( '0', '1' )
01179                 $where";
01180 
01181                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01182                 while( $ar = mysql_fetch_array( $res ) )
01183                 {
01184                         $sql = "update snapinTasks set stState = '-1' where stID = '" . $ar["stID"] . "'";
01185                         if (!mysql_query( $sql, $conn ) )
01186                                 die( mysql_error() );
01187                 }
01188                 return true;
01189         }
01190         return false;
01191 }
01192 
01193 function deploySnapinsForHost( $conn, $hostid, $snapID = -1 )
01194 {
01195         if ( $conn != null && $hostid !== null && is_numeric( $hostid )  )
01196         {
01197                 $hostid = mysql_real_escape_string( $hostid );
01198                 $snap = mysql_real_escape_string( $snapID );
01199                 $where = "";
01200                 if ( $snapID != -1 )
01201                 {
01202                         $where = " and sID = '$snap' ";
01203                 }
01204                 $sql = "SELECT
01205                 count(*) as cnt
01206                 FROM
01207                 snapinAssoc
01208                 inner join snapins on ( snapinAssoc.saSnapinID = snapins.sID )
01209                 WHERE
01210                 snapinAssoc.saHostID = '$hostid'
01211                 $where";
01212 
01213                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01214                 if ( $ar = mysql_fetch_array($res) )
01215                 {
01216                         if($ar["cnt"] > 0 )
01217                         {
01218                                 // create job record
01219                                 // todo: make transactional
01220                                 $sql = "insert into snapinJobs(sjHostID, sjCreateTime) values( '$hostid', NOW())";
01221                                 if ( mysql_query( $sql, $conn ) )
01222                                 {
01223                                         $insertedID = mysql_insert_id( $conn );
01224                                         if ( $insertedID !== false )
01225                                         {
01226                                                 // create job items
01227                                                 $suc = 0;
01228                                                 $sql = "SELECT
01229                                                 sID
01230                                                 FROM
01231                                                 snapinAssoc
01232                                                 inner join snapins on ( snapinAssoc.saSnapinID = snapins.sID )
01233                                                 WHERE
01234                                                 snapinAssoc.saHostID = '$hostid'
01235                                                 ORDER BY
01236                                                 snapins.sName";
01237 
01238                                                 $resS = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01239                                                 while( $arS = mysql_fetch_array( $resS ) )
01240                                                 {
01241 
01242                                                         if ( $snap == -1 || $arS["sID"] == $snap )
01243                                                         {
01244                                                                 $sql = "insert into
01245                                                                 snapinTasks(stJobID, stState, stSnapinID)
01246                                                                 values('$insertedID', '0', '" . $arS["sID"] . "')";
01247 
01248                                                                 if ( mysql_query( $sql, $conn ) )
01249                                                                         $suc++;
01250                                                         }
01251                                                 }
01252                                                 return $suc;
01253                                         }
01254                                 }
01255                         }
01256                         else
01257                                 return 0;
01258                 }
01259                         
01260         }
01261         return -1;
01262 }
01263 
01264 function getImageAction( $char )
01265 {
01266         if ( strtolower( $char ) == "u" )
01267                 return _("Upload");
01268         else if ( strtolower( $char ) == "d" )
01269                 return _("Download");
01270         else if ( strtolower( $char ) == "w" )
01271                 return _("Wipe");
01272         else if ( strtolower( $char ) == "x" )
01273                 return _("Debug");
01274         else if ( strtolower( $char ) == "m" )
01275                 return _("Memtest");
01276         else if ( strtolower( $char ) == "t" )
01277                 return _("Testdisk");
01278         else if ( strtolower( $char ) == "r" )
01279                 return _("PhotoRec");
01280         else if ( strtolower( $char ) == "c" )
01281                 return _("Multicast");
01282         else if ( strtolower( $char ) == "v" )
01283                 return _("Virus Scan");
01284         else if ( strtolower( $char ) == "i" )
01285                 return _("Inventory");
01286         else if ( strtolower( $char ) == "j" )
01287                 return _("Pass Reset");
01288         else if ( strtolower( $char ) == "s" )
01289                 return _("All Snapins");
01290         else if ( strtolower( $char ) == "l" )
01291                 return _("Single Snapin");
01292         else if ( strtolower( $char ) == "o" )
01293                 return _("Wake up");
01294         else if ( strtolower( $char ) == "a" )
01295                 return _("Switch Off");
01296         else if ( strtolower( $char ) == "b" )
01297                 return _("Boot");
01298         else
01299                 return _("N/A");
01300 }
01301 
01302 // new method to handle deletimg from master
01303 function ftpDeleteImage($conn, $imageid)
01304 {
01305         if ( $conn != null && $imageid !== null && is_numeric( $imageid ) )
01306         {
01307                 $sql = "SELECT
01308                 *
01309                 FROM
01310                 ( SELECT * FROM images WHERE imageID = '$imageid' ) images
01311                 INNER JOIN nfsGroups on ( images.imageNFSGroupID = nfsGroups.ngID )
01312                 INNER JOIN nfsGroupMembers on ( nfsGroupMembers.ngmGroupID = nfsGroups.ngID )
01313                 WHERE
01314                 nfsGroupMembers.ngmIsMasterNode = '1' and
01315                 nfsGroupMembers.ngmIsEnabled = '1'";
01316                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
01317                 if ( mysql_num_rows( $res ) == 1 )
01318                 {
01319 
01320                         while( $ar = mysql_fetch_array( $res ) )
01321                         {
01322 
01323                                 $path = $ar["ngmRootPath"] . $ar["imagePath"];
01324                                 $user = $ar["ngmUser"];
01325                                 $pass = $ar["ngmPass"];
01326                                 $server = $ar["ngmHostname"];
01327 
01328                                 if ( $path != null && $user != null && $pass != null && $server != null )
01329                                 {
01330 
01331                                         $ftp = ftp_connect($server);
01332                                         $ftp_loginres = ftp_login($ftp, $user, $pass);
01333                                         if ($ftp && $ftp_loginres )
01334                                         {
01335                                                 return ftp_delete( $ftp, $path );
01336                                         }
01337                                         @ftp_close($ftp);
01338                                 }
01339 
01340                         }
01341                 }
01342         }
01343         return false;
01344 }
01345 
01346 function ftpDeleteImageDir($conn, $imageid)
01347 {
01348         if ( $conn != null && $imageid !== null && is_numeric( $imageid ) )
01349         {
01350                 $sql = "SELECT
01351                 *
01352                 FROM
01353                 ( SELECT * FROM images WHERE imageID = '$imageid' ) images
01354                 INNER JOIN nfsGroups on ( images.imageNFSGroupID = nfsGroups.ngID )
01355                 INNER JOIN nfsGroupMembers on ( nfsGroupMembers.ngmGroupID = nfsGroups.ngID )
01356                 WHERE
01357                 nfsGroupMembers.ngmIsMasterNode = '1' and
01358                 nfsGroupMembers.ngmIsEnabled = '1'";
01359                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
01360                 if ( mysql_num_rows( $res ) == 1 )
01361                 {
01362                         while( $ar = mysql_fetch_array( $res ) )
01363                         {
01364                                 $path = $ar["ngmRootPath"] . $ar["imagePath"];
01365                                 $user = $ar["ngmUser"];
01366                                 $pass = $ar["ngmPass"];
01367                                 $server = $ar["ngmHostname"];
01368 
01369                                 if ( $path != null && $user != null && $pass != null && $server != null )
01370                                 {
01371                                         $ftp = ftp_connect($server);
01372                                         $ftp_loginres = ftp_login($ftp, $user, $pass);
01373                                         if ($ftp && $ftp_loginres )
01374                                         {
01375                                                 $arFiles = ftp_nlist($ftp, $path);
01376                                                 for( $i = 0; $i < count( $arFiles ); $i++ )
01377                                                 {
01378                                                         if ( $arFiles[$i] != "." && $arFiles[$i] != ".." )
01379                                                                 @ftp_delete( $ftp, $arFiles[$i] );
01380                                                 }
01381                                                 return ftp_rmdir( $ftp, $path );
01382                                         }
01383                                         @ftp_close($ftp);
01384                                 }
01385 
01386                         }
01387                 }
01388         }
01389         return false;
01390 }
01391 
01392 
01393 function ftpDelete( $remotefile )
01394 {
01395         global $conn;
01396 
01397         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
01398         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
01399         if ($ftp && $ftp_loginres )
01400         {
01401                 return ftp_delete( $ftp, $remotefile );
01402         }
01403         @ftp_close($ftp);
01404         return false;
01405 }
01406 
01407 function ftpDeleteDir( $dir )
01408 {
01409         global $conn;
01410 
01411         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
01412         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
01413         if ($ftp && $ftp_loginres )
01414         {
01415                 $arFiles = ftp_nlist($ftp, $dir);
01416                 for( $i = 0; $i < count( $arFiles ); $i++ )
01417                 {
01418                         if ( $arFiles[$i] != "." && $arFiles[$i] != ".." )
01419                                 @ftp_delete( $ftp, $arFiles[$i] );
01420                 }
01421                 return ftp_rmdir( $ftp, $dir );
01422         }
01423         @ftp_close($ftp);
01424         return false;
01425 }
01426 
01427 function hasCheckedIn( $conn, $jobid )
01428 {
01429         if ( $conn && $jobid )
01430         {
01431                 $sql = "select (UNIX_TIMESTAMP(taskCheckIn) - UNIX_TIMESTAMP(taskCreateTime) ) as diff from tasks where taskID = '" . mysql_real_escape_string( $jobid ) . "'";
01432                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01433                 if ( $ar = mysql_fetch_array( $res ) )
01434                 {
01435                         if ( $ar["diff"] > 2 ) return true;
01436                 }
01437         }
01438         return false;
01439 }
01440 
01441 function state2text( $intState )
01442 {
01443         if ( $intState == 0 )
01444                 return _("Queued");
01445         else if ( $intState == 1 )
01446                 return _("In progress");
01447         else if ( $intState == 2 )
01448                 return _("complete");
01449         else if ( $intState == 3 )
01450                 return _("Error");
01451         else
01452                 return _("unknown");
01453 }
01454 
01455 function getNumberOfTasks($conn, $intState, $taskType="D" )
01456 {
01457         if ( $conn != null )
01458         {
01459                 if ($taskType == "C")
01460                         $sql = "select count(*) as cnt from
01461                         (select distinct(msID) from tasks,multicastSessionsAssoc where tasks.taskID=multicastSessionsAssoc.tID AND
01462                         taskState = '" . mysql_real_escape_string( $intState ) . "' and taskType ='" . mysql_real_escape_string( $taskType ) . "'
01463                         ) tasks";
01464                 else
01465                         $sql = "select count(*) as cnt
01466                         from (select * from tasks where taskState = '" . mysql_real_escape_string( $intState ) . "' AND
01467                         taskType ='" . mysql_real_escape_string( $taskType ) . "') tasks inner join hosts on ( tasks.taskHostID = hosts.hostID )";
01468 
01469                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01470 
01471                 if ( $ar = mysql_fetch_array( $res ) )
01472                 {
01473                         return $ar["cnt"];
01474                 }
01475         }
01476         return 0;
01477 }
01478 
01479 
01480 function snapinExists( $conn, $name, $id=-1 )
01481 {
01482         if ( $conn != null && $name != null )
01483         {
01484                 $sql = "select count(*) as cnt from snapins where sName = '" . mysql_real_escape_string( $name ) . "' and sID <> $id";
01485                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01486                 if ( $ar = mysql_fetch_array( $res ) )
01487                 {
01488                         if ( $ar["cnt"] == 0 )
01489                                 return false;
01490                 }
01491         }
01492 }
01493 
01494 function imageDefExists( $conn, $name, $id=-1 )
01495 {
01496         if ( $conn != null && $name != null )
01497         {
01498                 $sql = "select count(*) as cnt from images where imageName = '" . mysql_real_escape_string( $name ) . "' and imageID <> $id";
01499                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01500                 if ( $ar = mysql_fetch_array( $res ) )
01501                 {
01502                         if ( $ar["cnt"] == 0 )
01503                                 return false;
01504                 }
01505         }
01506         return true;
01507 }
01508 
01509 function msgBox( $msg )
01510 {
01511         global $conn;
01512 
01513         $action = "rollDownElement('msgBox', 'alert');";
01514         if ( $conn != null )
01515         {
01516                 $ret = getSetting(  $conn, "FOG_USE_ANIMATION_EFFECTS");
01517                 if ( $ret != "1" )
01518                         $action = "rollDownNoEffect( 'msgBox', 'alert' )";
01519         }
01520 
01521         echo ( "<script type=\"text/javascript\">" );
01522         echo ( "document.getElementById('msgboxmsg').innerHTML='$msg';" );
01523         echo ( $action );
01524         echo ( "</script>" );
01525 }
01526 
01527 function lg( $string )
01528 {
01529         global $conn, $currentUser;
01530         $uname = "";
01531         if ( $currentUser != null )
01532                 $uname = mysql_real_escape_string( $currentUser->getUserName() );
01533 
01534         $sql = "insert into history( hText, hUser, hTime, hIP ) values( '" . mysql_real_escape_string( $string ) . "', '" . $uname . "', NOW(), '" . $_SERVER["REMOTE_ADDR"] . "')";
01535         @mysql_query( $sql, $conn );
01536 }
01537 
01538 function groupExists( $conn, $groupName, $id=-1 )
01539 {
01540         if ( $conn != null && $groupName != null )
01541         {
01542                 $sql = "select count(*) as cnt from groups where groupName = '" . mysql_real_escape_string( $groupName ) . "' and groupID <> $id";
01543                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01544                 if ( $ar = mysql_fetch_array( $res ) )
01545                 {
01546                         if ( $ar["cnt"] == 0 )
01547                                 return false;
01548                 }
01549         }
01550         return true;
01551 }
01552 
01553 function profileExists( $conn, $profileName, $id=-1 )
01554 {
01555         if ( $conn != null && $profileName != null )
01556         {
01557                 $sql = "select count(*) as cnt from profiles where profileName = '" . mysql_real_escape_string( $profileName ) . "' and profileID <> $id";
01558                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01559                 if ( $ar = mysql_fetch_array( $res ) )
01560                 {
01561                         if ( $ar["cnt"] == 0 )
01562                                 return false;
01563                 }
01564         }
01565         return true;
01566 }
01567 
01568 
01569 function hostsExists( $conn, $mac, $id=-1 )
01570 {
01571         if ( $conn != null && $mac != null )
01572         {
01573                 $sql = "select count(*) as cnt from hosts where hostMAC = '" . mysql_real_escape_string( $mac ) . "' and hostID <> $id";
01574                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01575                 if ( $ar = mysql_fetch_array( $res ) )
01576                 {
01577                         if ( $ar["cnt"] == 0 )
01578                                 return false;
01579                 }
01580         }
01581         return true;
01582 }
01583 
01584 function createHost( $conn, $mac, $hostname, $hostdomain, $ip="" , $desc="", $createby=null, $osid=null, $imageid=null, $binUseAD=0, $adDomain=null, $adOU=null, $adUser=null, $adPass=null, $kernelargs="", $kernel="", $device="")
01585 {
01586         if ( ! hostsExists( $conn, $mac ) )
01587         {
01588                 if ( isValidMACAddress( $mac ) )
01589                 {
01590                         $ip = mysql_real_escape_string( $ip );
01591                         $desc = mysql_real_escape_string( $desc );
01592                         $imageid = mysql_real_escape_string( $imageid );
01593                         $mac = mysql_real_escape_string( $mac );
01594                         $hostname = mysql_real_escape_string( $hostname );
01595                         $hostdomain = mysql_real_escape_string( $hostdomain );
01596                         $osid = mysql_real_escape_string( $osid );
01597                         $createby = mysql_real_escape_string( $createby );
01598                         $kernelargs = mysql_real_escape_string($kernelargs);
01599                         $kernel = mysql_real_escape_string($kernel);
01600                         $device = mysql_real_escape_string($device);
01601 
01602                         $useAD = "0";
01603                         if ( $binUseAD == "1" )
01604                                 $useAD = "1";
01605 
01606                         $adDomain = mysql_real_escape_string( $adDomain );
01607                         $adOU = mysql_real_escape_string( $adOU );
01608                         $adUser = mysql_real_escape_string( $adUser );
01609                         $adPass = mysql_real_escape_string( $adPass );
01610 
01611                         if ( $mac != null && $hostname != null && $os != "-1" )
01612                         {
01613                                 $sql = "insert into hosts(hostMAC, hostIP, hostName, hostDomain, hostDesc, hostCreateDate, hostImage, hostCreateBy, hostUseAD, hostADDomain, hostADOU, hostADUser, hostADPass, hostKernelArgs, hostKernel, hostDevice)
01614                                 values('$mac','$ip', '$hostname', '$hostdomain', '$desc', NOW(), '$imageid', '$createby', '$useAD', '$adDomain', '$adOU', '$adUser', '$adPass', '$kernelargs', '$kernel', '$device' )";
01615                                 if ( mysql_query( $sql, $conn ) )
01616                                         return true;
01617                         }
01618                 }
01619         }
01620         return false;
01621 }
01622 
01623 function trimString( $string, $len )
01624 {
01625         if ( strlen($string) > $len )
01626         {
01627                 return substr( trim($string), 0, $len ) . "...";
01628         }
01629 
01630         return $string;
01631 }
01632 
01633 function endsWith( $str, $sub )
01634 {
01635         return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
01636 }
01637 
01638 function getCheckedItems( $post )
01639 {
01640         $retAr = array();
01641         foreach ($post as $key => $value)
01642         {
01643                 if ( substr( trim($key), 0, 3 ) == "HID" && $value == "on" )
01644                 {
01645                         $retAr[] = substr(trim($key),3 );
01646                 }
01647         }
01648         return $retAr;
01649 
01650 }
01651 
01652 function addMembersToGroup( $conn, $grpID, $arMembers )
01653 {
01654 
01655         if ( $conn != null && $grpID != null && $arMembers != null )
01656         {
01657                 for( $i = 0; $i < count( $arMembers ); $i++ )
01658                 {
01659                         if ( $arMembers[$i] != null && is_numeric($arMembers[$i]) )
01660                         {
01661                                 $sql = "select count(*) from groupMembers where gmGroupID = '$grpID' and gmHostID = '$arMembers[$i]'";
01662                                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01663                                 $num = -1;
01664                                 while( $ar = mysql_fetch_array( $res ) )
01665                                 {
01666                                         $num = $ar[0];
01667                                 }
01668                                         
01669                                 if ( $num == 0 )
01670                                 {
01671                                         $sql = "insert into groupMembers(gmGroupID, gmHostID) values('$grpID', '$arMembers[$i]')";
01672                                         mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01673                                         lg( _("Host")." [$arMembers[$i]] "._("added to group")." [$grpID] " );
01674                                 }
01675                         }
01676                 }
01677                 return true;
01678         }
01679         return false;
01680 }
01681 
01682 function addMembersToProfile( $conn, $prfID, $arMembers )
01683 {
01684 
01685         if ( $conn != null && $prfID != null && $arMembers != null )
01686         {
01687                 for( $i = 0; $i < count( $arMembers ); $i++ )
01688                 {
01689                         if ( $arMembers[$i] != null && is_numeric($arMembers[$i]) )
01690                         {
01691                                 $sql = "select count(*) from profileMembers where pmProfileID = '$prfID' and pmUserID = '$arMembers[$i]'";
01692                                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01693                                 $num = -1;
01694                                 while( $ar = mysql_fetch_array( $res ) )
01695                                 {
01696                                         $num = $ar[0];
01697                                 }
01698 
01699                                 if ( $num == 0 )
01700                                 {
01701                                         $sql = "insert into profileMembers(pmProfileID, pmUserID) values('$prfID', '$arMembers[$i]')";
01702                                         mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01703                                         lg( _("Usuario")." [$arMembers[$i]] "._("asociado al perfil")." [$prfID] " );
01704                                 }
01705                         }
01706                 }
01707                 return true;
01708         }
01709         return false;
01710 }
01711 
01712 
01713 function getImageMembersByGroupID( $conn, $groupID )
01714 {
01715         $arM = array();
01716         if ( $conn != null && $groupID != null )
01717         {
01718                 $sql = "select
01719                 *
01720                 from groups
01721                 inner join groupMembers on ( groups.groupID = groupMembers.gmGroupID )
01722                 where groupID = $groupID";
01723                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01724                 $arHosts = array();
01725                 while( $ar = mysql_fetch_array( $res ) )
01726                 {
01727                         $arHosts[] = $ar["gmHostID"];
01728                 }
01729 
01730                 for( $i = 0; $i < count( $arHosts ); $i++ )
01731                 {
01732                         $tmpMember = getImageMemberFromHostID( $conn, $arHosts[$i] );
01733                         if( $tmpMember != null )
01734                                 $arM[] = $tmpMember;
01735                 }
01736         }
01737         return $arM;
01738 }
01739 
01740 function getMembersByProfileID( $conn, $profileID )
01741 {
01742         $arM = array();
01743         if ( $conn != null && $profileID != null )
01744         {
01745                 $sql = "select
01746                 uId, uName, uIdentity, uEmail
01747                 from profiles
01748                 inner join profileMembers on ( profiles.profileID = profileMembers.pmProfileID )
01749                 inner join users on ( users.uID = profileMembers.pmUserID )
01750                 where profileID = $profileID";
01751                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01752                 $arMembers = array();
01753                 while( $ar = mysql_fetch_array( $res ) )
01754                 {
01755                         $arMembers[] = new User( $ar["uId"], $ar["uName"], null, null, null, $ar["uIdentity"], $ar["uEmail"] );
01756 
01757                 }
01758 
01759                 #                for( $i = 0; $i < count( $arProfiles ); $i++ )
01760                 #               {
01761                 #                      $tmpMember = getUserFromUserID( $conn, $arProfiles[$i] );
01762                 #                     if( $tmpMember != null )
01763                 #                            $arM[] = $tmpMember;
01764                         #           }
01765         }
01766         #        return $arM;
01767         #       return $res;
01768         return $arMembers;
01769 }
01770 
01771 
01772 function getGroupNameByID( $conn, $id )
01773 {
01774         if ( $conn != null && $id != null && is_numeric( $id ) )
01775         {
01776                 $id = mysql_real_escape_string( $id );
01777                 $sql = "select * from groups where groupID = '$id'";
01778 
01779                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01780                 if ( mysql_num_rows( $res ) == 1 )
01781                 {
01782                         if( $ar = mysql_fetch_array( $res ) )
01783                         {
01784                                 return $ar["groupName"];
01785                         }
01786                 }
01787         }
01788         return null;
01789 }
01790 
01791 function getProfileNameByID( $conn, $id )
01792 {
01793         if ( $conn != null && $id != null && is_numeric( $id ) )
01794         {
01795                 $id = mysql_real_escape_string( $id );
01796                 $sql = "select profileName from profiles where profileID = '$id'";
01797 
01798                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01799                 if ( mysql_num_rows( $res ) == 1 )
01800                 {
01801                         if( $ar = mysql_fetch_array( $res ) )
01802                         {
01803                                 return $ar["profileName"];
01804                         }
01805                 }
01806         }
01807         return null;
01808 }
01809 
01810 
01811 function getGroupIDByName( $conn, $name )
01812 {
01813         if ( $conn != null && $name != null )
01814         {
01815                 $name = mysql_real_escape_string( $name );
01816                 $sql = "select groupID from groups where groupName = '$name'";
01817                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01818                 if ( mysql_num_rows( $res ) == 1 )
01819                 {
01820                         if( $ar = mysql_fetch_array( $res ) )
01821                         {
01822                                 return $ar["groupID"];
01823                         }
01824                 }
01825         }
01826         return -1;
01827 }
01828 
01829 function getProfileIDByName( $conn, $name )
01830 {
01831         if ( $conn != null && $name != null )
01832         {
01833                 $name = mysql_real_escape_string( $name );
01834                 $sql = "select profileID from profiles where profileName = '$name'";
01835                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01836                 if ( mysql_num_rows( $res ) == 1 )
01837                 {
01838                         if( $ar = mysql_fetch_array( $res ) )
01839                         {
01840                                 return $ar["profileID"];
01841                         }
01842                 }
01843         }
01844         return -1;
01845 }
01846 
01847 
01848 function getImageMemberFromHostID( $conn, $hostid )
01849 {
01850         $member = null;
01851         if ( $conn != null && $hostid != null )
01852         {
01853                 $hostid = mysql_real_escape_string( $hostid );
01854                 $sql = "select
01855                 *
01856                 from hosts
01857                 left outer join images on ( hostImage = imageID )
01858                 where hostID = '$hostid'";
01859 
01860                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
01861                 while( $ar = mysql_fetch_array( $res ) )
01862                 {
01863                         $imgType = ImageMember::IMAGETYPE_PARTITION;
01864                         if ( $ar["imageDD"] == "1" )
01865                                 $imgType = ImageMember::IMAGETYPE_DISKIMAGE;
01866                         else if ( $ar["imageDD"] == "2" )
01867                                 $imgType = ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE;
01868                         else if ( $ar["imageDD"] == "3" )
01869                                 $imgType = ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES;
01870 
01871                         $imageServer = null;
01872                         $imagePath = null;
01873                         $groupid = -1;
01874                         $nodeid = -1;
01875 
01876                         $arNodes = array();
01877                         if ( $ar["imageNFSGroupID"] != null && is_numeric( $ar["imageNFSGroupID"] ) )
01878                         {
01879                                 $gid = $ar["imageNFSGroupID"];
01880                                 $sql = "SELECT
01881                                 *
01882                                 FROM
01883                                 (SELECT * FROM nfsGroups WHERE ngID = '$gid' ) nfsGroups
01884                                 INNER JOIN nfsGroupMembers on ( nfsGroupMembers.ngmGroupID = nfsGroups.ngID )
01885                                 WHERE
01886                                 nfsGroupMembers.ngmIsEnabled = '1' and
01887                                 trim(ngmRootPath) <> '' and
01888                                 trim(ngmHostname) <> '' and
01889                                 ngmMaxClients > 0 ";
01890                                 $resn = mysql_query( $sql, $conn ) or die( mysql_error() );
01891                                 $cnt = 0;
01892                                 while( $arn = mysql_fetch_array( $resn ) )
01893                                 {
01894                                         $arNodes[$cnt]["path"] = $arn["ngmRootPath"];
01895                                         $arNodes[$cnt]["group"] = $arn["ngID"];
01896                                         $arNodes[$cnt]["node"] = $arn["ngmID"];
01897                                         $arNodes[$cnt++]["server"] = $arn["ngmHostname"];
01898                                 }
01899                         }
01900 
01901                         if ( count( $arNodes ) > 0 )
01902                         {
01903                                 $random = rand( 0, count( $arNodes ) - 1 );
01904                                 $imageServer = $arNodes[$random]["server"];
01905                                 $imagePath = $arNodes[$random]["path"];
01906                                 $groupid = $arNodes[$random]["group"];
01907                                 $nodeid = $arNodes[$random]["node"];
01908                         }
01909 
01910                         $member = new ImageMember( $ar["hostName"], $ar["hostIP"], $ar["hostMAC"], $ar["imagePath"], $imagePath, $imageServer, $groupid, $nodeid, $ar["hostID"], 0, $ar["imageID"], false, $imgType, $ar["hostKernelArgs"], $ar["hostKernel"], $ar["hostDevice"] );
01911                 }
01912         }
01913         return $member;
01914 }
01915 
01916 function wakeUp( $mac, $ip)
01917 {
01918         global $conn;
01919 
01920         if ($ip != null)
01921         {
01922                 list($ip1, $ip2, $ip3, $ip4) = explode(".", $ip);
01923                 $subred=$ip1 . "." . $ip2 . "." . $ip3;
01924 
01925                 if ( getSetting( $conn, "FOG_WOL_BCAST" ) == "" )
01926                         switch ($subred) {
01927                         # ejemplo, las direcciones broadcast de subrredes que no resulten de sustituir el ultimo octeto de la direccion ip por 255
01928                 # deberan defirse como en los siguientes ejemplos
01929 
01930                                 case "10.227.34": $ip = "10.227.35.255"; break;
01931                                 case "10.227.28": $ip = "10.227.31.255"; break;
01932                                 case "10.227.15": $ip = "10.227.15.31"; break;
01933 
01934                                 default: $ip = $ip1 . "." . $ip2 . "." . $ip3 . "." . "255";
01935                                 //                      default: $ip = "255.255.255.255";
01936                 } else $ip = getSetting( $conn, "FOG_WOL_BCAST" );
01937 
01938         }
01939 
01940         /*      if ( $mac != null )
01941          {
01942         $ch = curl_init();
01943 
01944         curl_setopt($ch, CURLOPT_URL, "http://" . getSetting( $conn, "FOG_WOL_HOST" ) . getSetting( $conn, "FOG_WOL_PATH" ) . "?wakeonlan=$mac&ip=" . $ip);
01945         curl_setopt($ch, CURLOPT_HEADER, 0);
01946         curl_exec($ch);
01947         curl_close($ch);
01948         lg( "WOL enviado [$mac] :: $ip" );
01949         }
01950         */
01954         if ( $mac != null )
01955         {
01956                 $URL = "http://" . getSetting( $conn, "FOG_WOL_HOST" ) . getSetting( $conn, "FOG_WOL_PATH" ) . "?wakeonlan=$mac&ip=" . $ip;
01957                 $contextOptions = array(
01958                                 'ssl'   => array(
01959                                                 'allow_self_signed' => true
01960                                 ),
01961                                 'http'  => array(
01962                                                 'method'        => 'GET',
01963                                                 'user_agent'    => $userAgent,
01964                                                 'timeout'       => $timeout,
01965                                                 'max_redirects' => $maxRedirects,
01966                                                 'header'        => array(
01967                                                                 'Accept-language: en',
01968                                                                 'Pragma: no-cache'
01969                                                 )
01970                                 )
01971                 );
01972                 $response = trim(@file_get_contents($URL, false, stream_context_create($contextOptions)));
01973 
01974         }
01975 }
01976 
01977 function switchOff( $ip, $reboot=false, $message="") {
01978         global $conn;
01979 
01980         if ($ip != null)
01981         {
01982                 if ( !empty($message)) $message = " \"".$message."\"";
01983 
01984                 if ($reboot) $rc = feSendMsg($ip, 'fogehu.Sistema comando reboot'.$message);
01985                 else $rc = feSendMsg($ip, 'fogehu.Sistema comando shutdown'.$message);
01986 
01987                 if (strpos($rc,"Unable") === 0) $rc="Imposible conectar con el agente";
01988                 else if (strpos($rc,"process has not exited") > 0) $rc="Apagando";
01989                 lg( "SwitchOff enviado [$ip] :: $rc" );
01990         }
01991 }
01992 
01993 
01994 function feGetOS($host) {
01995         $data = feSendMsg($host, "os");
01996         if (strpos($data,"Unable")===0) return "0";
01997         if ($data == "#nc") return "#nc";
01998         $task = strtok($data, ":");
01999         #        $ver = strtok(":");
02000         #       $proc = strtok (":");
02001         #       $mem = strtok (":");
02002         $os = strtok(":");
02003         $osver = strtok(":");
02004         return $os;
02005 }
02006 function feGetVer($host) {
02007         $data = feSendMsg($host, "ver");
02008         if (strpos($data,"Unable")===0) return "0";
02009         if ($data == "#nc") return "#nc";
02010         $task = strtok($data, ":");
02011         $ver = strtok(":");
02012         return $ver;
02013 }
02014 
02015 function feDeploySnapin( $conn, $hostid, $snapid ) {
02016         error_log ("paso por aqui");
02017         if ( $conn != null && $hostid != null && $snapid!=null) {
02018                 $sql="SELECT stSnapinID as snapin, sjHostID as host, saOrder as orden, hostIP,
02019                 (SELECT COUNT(*) FROM snapinTasks a LEFT JOIN snapinJobs b ON (a.stJobID=b.sjID) LEFT JOIN snapinAssoc c ON (c.saHostID=b.sjHostID AND c.saSnapinID=a.stSnapinID)
02020                 WHERE stState='0' AND a.stSnapinID=snapin) as c
02021                 FROM (SELECT stSnapinID, sjHostID, saOrder, hostIP FROM snapinTasks a LEFT JOIN snapinJobs b ON (a.stJobID=b.sjID) LEFT JOIN hosts d ON (b.sjHostID=d.hostID)
02022                 LEFT JOIN snapinAssoc c ON (c.saHostID=b.sjHostID AND c.saSnapinID=a.stSnapinID) WHERE stState='0') t1 ORDER BY hostIP;";
02023 
02024                 $res0 = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02025                 //while
02026                 if( $ar0 = mysql_fetch_array( $res0) ) {
02027                         $hostid=$ar0["host"];
02028                         $snapid=$ar0["snapin"];
02029                         $c=$ar0["c"];
02030                 }
02031 
02032                 // localizado snapin a tratar=$snapinid y forma de envío
02033                 if ($c == "1") {
02034                         $sql="SELECT
02035                         (SELECT settingValue from globalSettings where settingKey='FOG_WEB_HOST') as web,
02036                         (SELECT hostMAC from hosts where hostID=$hostid) as mac,
02037                         (SELECT hostIP from hosts where hostID=$hostid) as ip,
02038                         (SELECT MAX(a.stID) FROM snapinTasks a, snapinJobs b WHERE a.stJobID=b.sjID AND b.sjHostID=$hostid AND a.stSnapinID=$snapid AND stState>-1) as task,
02039                         (SELECT CONCAT(sName,'.exe') FROM snapins WHERE sID=$snapid) as fichero,
02040                         (SELECT sType FROM snapins WHERE sID=$snapid) as sType,
02041                         (SELECT sArgs FROM snapins WHERE sID=$snapid) as sArgs
02042                         FROM DUAL;";
02043                         error_log($sql);
02044                         $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02045                         if( $ar = mysql_fetch_array( $res ) ) {
02046                                 $source="http://".$ar["web"]."/fog/service/snapins.file.php?mac=".$ar["mac"]."&taskid=".$ar["task"];
02047                                 $logger="http://".$ar["web"]."/fog/service/snapins.checkin.php?mac=";
02048                                 if ( $ar["sType"] == "v" )
02049                                         return (feSendMsg ($ar["ip"], "fogehu.FileUtil getFile ".$source." ".str_replace(' ','_',$ar["fichero"])." cameyoInt ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc="));
02050                                 else if ( $ar["sType"] == "z")
02051                                         return (feSendMsg ($ar["ip"], "fogehu.FileUtil unzipFile ".$source." ".$ar["sArgs"]." Usuarios:F ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc="));
02052                         } // end if $ar
02053                 } else if ($c > "1") {
02054                         $source=""; $logger="";
02055                         while ($ar1 = mysql_fetch_array( $res0) ) {
02056                                 $hostid1=$ar1["host"];
02057                                 $snapid1=$ar1["snapin"];
02058                                 if ($snapid1 != $snapid) continue;
02059 
02060                                 $sql="SELECT
02061                                 (SELECT settingValue from globalSettings where settingKey='FOG_WEB_HOST') as web,
02062                                 (SELECT hostMAC from hosts where hostID=$hostid1) as mac,
02063                                 (SELECT hostIP from hosts where hostID=$hostid1) as ip,
02064                                 (SELECT MAX(a.stID) FROM snapinTasks a, snapinJobs b WHERE a.stJobID=b.sjID AND b.sjHostID=$hostid1 AND a.stSnapinID=$snapid1 AND stState>-1) as task,
02065                                 (SELECT CONCAT(sName,'.exe') FROM snapins WHERE sID=$snapid1) as fichero,
02066                                 (SELECT sType FROM snapins WHERE sID=$snapid1) as sType,
02067                                 (SELECT sArgs FROM snapins WHERE sID=$snapid1) as sArgs
02068                                 FROM DUAL;";
02069                                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02070 
02071                                 if ( $ar = mysql_fetch_array( $res ) ) {
02072                                         $ip = $ar["ip"];
02073                                         if (empty($source)) $source="http://".$ar["web"]."/fog/service/snapins.file.php?mac=".$ar["mac"]."&taskid=".$ar["task"];
02074                                         if (empty($logger)) $logger="http://".$ar["web"]."/fog/service/snapins.checkin.php?mac=";
02075 
02076                                         if ( $ar["sType"] == "v" )
02077                                                 $port=feSendMsg ($ar["ip"], "fogehu.FileUtil getServeFile ".$source." ".str_replace(' ','_',$ar["fichero"])." cameyoInt ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc=");
02078                                         else if ( $ar["sType"] == "z")
02079                                                 $port = feSendMsg ($ar["ip"], "fogehu.FileUtil unzipServeFile ".$source." ".$ar["sArgs"]." Usuarios:F ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc=");
02080 
02081                                         $port = substr($port,strpos($port,"=")+1);
02082                                         $port= substr ($port,0,strpos($port,"\r"));
02083                                         $source=$ip.":".$port;
02084                                         error_log("source=".$source);
02085                                 }
02086                         } // end while $ar1
02087                         //cierro ciclo
02088                         $sql="SELECT
02089                         (SELECT settingValue from globalSettings where settingKey='FOG_WEB_HOST') as web,
02090                         (SELECT hostMAC from hosts where hostID=$hostid) as mac,
02091                         (SELECT hostIP from hosts where hostID=$hostid) as ip,
02092                         (SELECT MAX(a.stID) FROM snapinTasks a, snapinJobs b WHERE a.stJobID=b.sjID AND b.sjHostID=$hostid AND a.stSnapinID=$snapid AND stState>-1) as task,
02093                         (SELECT CONCAT(sName,'.exe') FROM snapins WHERE sID=$snapid) as fichero,
02094                         (SELECT sType FROM snapins WHERE sID=$snapid) as sType,
02095                         (SELECT sArgs FROM snapins WHERE sID=$snapid) as sArgs
02096                         FROM DUAL;";
02097                         $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02098                         if( $ar = mysql_fetch_array( $res ) )
02099                                 if ( $ar["sType"] == "v" )
02100                                 $msg=feSendMsg ($ar["ip"], "fogehu.FileUtil getFile ".$source." ".str_replace(' ','_',$ar["fichero"])." cameyoInt ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc=");
02101                         else if ( $ar["sType"] == "z")
02102                                 $msg = feSendMsg ($ar["ip"], "fogehu.FileUtil unzipFile ".$source." ".$ar["sArgs"]." Usuarios:F ".$logger.$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc=");
02103                 }
02104 
02105 
02106         } // end if !=null
02107 }
02108 
02109 
02110 function feDeploySnapinBak( $conn, $hostid, $snapid ) {
02111         if ( $conn != null && $hostid != null && $snapid!=null) {
02112                 $sql="SELECT
02113                 (SELECT settingValue from globalSettings where settingKey='FOG_WEB_HOST') as web,
02114                 (SELECT hostMAC from hosts where hostID=$hostid) as mac,
02115                 (SELECT hostIP from hosts where hostID=$hostid) as ip,
02116                 (SELECT MAX(a.stID) FROM snapinTasks a, snapinJobs b WHERE a.stJobID=b.sjID AND b.sjHostID=$hostid AND a.stSnapinID=$snapid AND stState>-1) as task,
02117                 (SELECT CONCAT(sName,'.exe') FROM snapins WHERE sID=$snapid) as fichero,
02118                 (SELECT sType FROM snapins WHERE sID=$snapid) as sType,
02119                 (SELECT sArgs FROM snapins WHERE sID=$snapid) as sArgs
02120                 FROM DUAL;";
02121                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02122                 if( $ar = mysql_fetch_array( $res ) )
02123                         if ( $ar["sType"] == "v" )
02124                         return (feSendMsg ($ar["ip"], "fogehu.FileUtil getFile http://".$ar["web"]."/fog/service/snapins.file.php?mac=".$ar["mac"]."&taskid=".$ar["task"]." ".str_replace(' ','_',$ar["fichero"])." cameyoInt http://".$ar["web"]."/fog/service/snapins.checkin.php?mac=".$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc="));
02125                 else if ( $ar["sType"] == "z")
02126                         return (feSendMsg ($ar["ip"], "fogehu.FileUtil unzipFile http://".$ar["web"]."/fog/service/snapins.file.php?mac=".$ar["mac"]."&taskid=".$ar["task"]." ".$ar["sArgs"]." \"\" http://".$ar["web"]."/fog/service/snapins.checkin.php?mac=".$ar["mac"]."&taskid=".$ar["task"]."&exitcode=fe&exitdesc="));
02127 
02128         } // end if !=null
02129 }
02130 
02131 
02132 
02133 function feUndeploySnapin( $conn, $hostid, $snapid ) {
02134         if ( $conn != null && $hostid != null && $snapid!=null) {
02135                 $sql="SELECT
02136                 (SELECT CONCAT(sName,'.exe') FROM snapins WHERE sID=$snapid) as fichero,
02137                 (SELECT hostIP from hosts where hostID=$hostid) as ip,
02138                 (SELECT sType FROM snapins WHERE sID=$snapid) as sType,
02139                 (SELECT sArgs FROM snapins WHERE sID=$snapid) as sArgs
02140                 FROM DUAL;";
02141                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02142                 if( $ar = mysql_fetch_array( $res ) ){
02143                         if ( $ar["sType"] == "v" ) return (feSendMsg ($ar["ip"], "fogehu.Sistema comando ".str_replace(' ','_',$ar["fichero"])." -Quiet -Unintegrate"));
02144                         else if ( $ar["sType"] == "z") return (feSendMsg ($ar["ip"], "fogehu.Sistema deleteDir ".$ar["sArgs"]));
02145                 }
02146         } // end if !=null
02147 }
02148 
02149 
02150 function feCheckSnapins(){
02151         return feSendMsg ("192.168.0.100", "fogehu.FileUtil getFile http://192.168.0.128/fog/service/snapins.file.php?mac=00:0c:29:84:5b:5a&taskid=18 geogebra.exe");
02152         //      "http://192.168.0.128/fog/service/snapins.file.php?mac=00:22:33:44:55:66&taskid=53"
02153 
02154 
02155         if ( $conn != null && $hostid != null )
02156         {
02157                 $sql = "SELECT sID,hostIP,sType,sArgs,hostMAC,stID,saOrder,stState
02158                 FROM snapinTasks
02159                 LEFT JOIN snapinJobs ON snapinTasks.stJobID=snapinJobs.sjID
02160                 LEFT JOIN hosts ON hosts.hostID=snapinJobs.sjHostID
02161                 LEFT JOIN snapins ON snapins.sID=snapinTasks.stSnapinID
02162                 LEFT JOIN snapinAssoc ON snapins.sID=snapinAssoc.saID
02163                 WHERE stState=0 AND sType='z' ORDER BY snapinAssoc.saOrder,sID";
02164 
02165                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
02166                 while( $ar = mysql_fetch_array( $res ) )
02167                 {
02168                         feSendMsg ("192.168.0.104", "fogehu.FileUtil unzipFile http://158.227.4.36/m.zip c:/user");
02169 
02170                 }
02171         }
02172 }
02173 
02174 function feSendMsg($host, $data) {
02175         //        $xdata = '';
02176         //        $key = 'G4E';
02177         //        for($i=0;$i<strlen($data);) for($j=0;$j<strlen($key);$j++,$i++) $xdata .= $data{$i} ^ $key{$j};
02178         //        $edata = base64_encode($xdata)."\n\0";
02179         //      $edata = $data;
02180         error_log("".$host." ".$data);
02181         $data=$data."\n";
02182         $socket = null;  $reply = "";
02183         try {
02184                 $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
02185                 if ($sock == FALSE){
02186                         error_log("#ucs"); throw new Exception("Unable to create socket: " . socket_strerror(socket_last_error()));
02187                 }
02188 
02189                 socket_set_option($sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
02190                 socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0));
02191 
02192                 if (!socket_connect($sock, $host, 12025)){
02193                         error_log("#nc"); return "#nc";
02194                 }
02195 
02196                 $length = strlen($data);
02197                 $retval = array();
02198                 $sent = socket_write ($sock, $data, $length);
02199 
02200                 if($sent == false) {
02201                         throw Exception("Unable to write to socket: " .socket_strerror(socket_last_error())); error_log("#uws"); return false;
02202                 }
02203 
02204                 if($sent < $length) {
02205                         $edata = substr($edata, $sent); $length -= $sent; print("Message truncated: Resending: $edata");
02206                 }
02207                 else do {
02208                         $recv = "";
02209                         $recv = socket_read($sock, '1400');
02210                         if($recv != "") {
02211                                 $reply .= $recv;
02212                         }
02213                 } while($recv != "");
02214                         
02215                 socket_close($sock);
02216         } catch (Exception $e) {
02217                 return "";
02218         }
02219 
02220         error_log("reply:".$reply);
02221         return $reply;
02222 }
02223 
02224 function feSendMsg3 ($host, $data) {
02225         $argss = "export DYLD_LIBRARY_PATH=\"\"; /usr/bin/java -cp /opt/fog/utils/fogehu.jar EchoClient $host $data";
02226         $result = exec($argss, $output);
02227         return $result;
02228 }
02229 
02230 function feSendMsg2 ($host, $data){
02231 
02232         $reply = "";
02233         $sk = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
02234         socket_set_option($sk, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
02235         socket_set_option($sk, SOL_SOCKET, SO_REUSEADDR, 1);
02236 
02237         if ( !socket_connect($sk, $host, 12025) ) {
02238                 return "#nc";
02239         };
02240 
02241         $data = $data."\n"; $length = strlen($data);
02242         $sent = socket_write($sk, $data, $length );
02243 
02244         if ($sent < $length) {
02245                 $edata = substr($edata, $sent); $length -= $sent;
02246                 $sent = socket_write($sk, $data, $length );
02247         } else do {
02248                 $recv = "";
02249                 $recv = socket_read($sk, '1');
02250                 if($recv != "") {
02251                         $reply .= $recv;
02252                 }
02253         } while ($recv != "");
02254         socket_close($sk);
02255 
02256         return $reply;
02257 
02258 }
02259 
02260 
02261 
02262 
02263 function createGroup( $conn, $name )
02264 {
02265         global $currentUser;
02266 
02267         if ( $conn != null && $name != null )
02268         {
02269                 $name = mysql_real_escape_string( $name );
02270                 $sql = "select * from groups where groupName = '$name'";
02271                 $res = mysql_query( $sql, $conn );
02272                 if ( mysql_num_rows( $res ) == 0 )
02273                 {
02274                         $sql = "insert into groups(groupName, groupCreateBy, groupDateTime) values( '$name', '" . mysql_real_escape_string($currentUser->getUserName()) . "', NOW() )";
02275                         if( mysql_query( $sql, $conn ) )
02276                         {
02277                                 lg( _("Group created")." :: $name" );
02278                                 return true;
02279                         }
02280                 }
02281         }
02282         return false;
02283 }
02284 
02285 function createPXEFile( $contents )
02286 {
02287         $tmp = "/tmp/fog-" . rand ( 0, 999999999 ) . ".pxe";
02288         $hndl = fopen( $tmp, "w" );
02289         if( $hndl )
02290         {
02291                 if ( fwrite( $hndl, $contents ) )
02292                 {
02293                         return $tmp;
02294                 }
02295                 fclose( $hndl );
02296         }
02297         return null;
02298 }
02299 
02300 /*
02301  *  Until we move to a busy box based client image
02302 *  that can handle dns lookups, this is the poor mans
02303 *  name resolution.
02304 */
02305 
02306 function sloppyNameLookup( $host )
02307 {
02308         global $conn;
02309 
02310         if ( getSetting( $conn, "FOG_USE_SLOPPY_NAME_LOOKUPS" ) )
02311                 return gethostbyname( $host );
02312 
02313         return $host;
02314 }
02315 
02316 function createInventoryPackage( $conn, $member, &$reason, $shutdown="", $kernel="", $otherargs="" )
02317 {
02318         global $currentUser;
02319         if ( $conn != null && $member != null )
02320         {
02321                 if (  $member->getMACDash() != null  )
02322                 {
02323 
02324                         $mac = strtolower( $member->getMACImageReady() );
02325 
02326                         $keymapapp = "";
02327                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02328                         if ( $keymap != null && $keymap != "" )
02329                                 $keymapapp = "keymap=$keymap";
02330 
02331                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02332                         if ( $kernel != "" )
02333                                 $strKern = $kernel;
02334                         $output = "# "._("Created by FOG Imaging System")."\n\n
02335                         DEFAULT send\n
02336                         LABEL send\n
02337                         kernel " . $strKern . "\n
02338                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac_deployed=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " mode=autoreg deployed=1 $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02339                         $tmp = createPXEFile( $output );
02340                         if( $tmp !== null )
02341                         {
02342                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02343                                         
02344                                 if ( $num == 0 )
02345                                 {
02346 
02347                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02348                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02349                                         if ($ftp && $ftp_loginres )
02350                                         {
02351                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02352                                                 {
02353                                                         $sql = "insert into
02354                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02355                                                         values('" . mysql_real_escape_string($member->getHostName() . " Inventory") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'I' )";
02356                                                         if ( mysql_query( $sql, $conn ) )
02357                                                         {
02358                                                                 wakeUp( $member->getMACColon() , $member->getIPAddress() );
02359                                                                 lg( _("Inventory package created for host")." ". $member->getHostName() . " [" . $member->getMACDash() . "]" );
02360                                                                 @ftp_close($ftp);
02361                                                                 @unlink( $tmp );
02362                                                                 return true;
02363                                                         }
02364                                                         else
02365                                                         {
02366                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02367                                                                 $reason = mysql_error();
02368                                                         }
02369                                                 }
02370                                                 else
02371                                                         $reason = _("Unable to upload file.");
02372                                         }
02373                                         else
02374                                                 $reason = _("Unable to connect to tftp server.");
02375 
02376                                         @ftp_close($ftp);
02377                                         @unlink( $tmp );
02378                                 }
02379                                 else
02380                                         $reason = _("This host is already a member of a task!");
02381                         }
02382                         else
02383                                 $reason = _("Failed to open tmp file.");
02384 
02385                 }
02386                 else
02387                         $reason = _("MAC is null.");
02388         }
02389         else
02390         {
02391                 $reason = _("Either member of database connection was null");
02392         }
02393         return false;
02394 }
02395 
02396 function createDiskSufaceTestPackage( $conn, $member, &$reason, $shutdown="", $kernel="", $otherargs="" )
02397 {
02398         global $currentUser;
02399         if ( $conn != null && $member != null )
02400         {
02401                 if (  $member->getMACDash() != null  )
02402                 {
02403 
02404                         $mac = strtolower( $member->getMACImageReady() );
02405 
02406                         $keymapapp = "";
02407                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02408                         if ( $keymap != null && $keymap != "" )
02409                                 $keymapapp = "keymap=$keymap";
02410 
02411                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02412                         if ( $kernel != "" )
02413                                 $strKern = $kernel;
02414                         $output = "# "._("Created by FOG Imaging System")."\n\n
02415                         DEFAULT send\n
02416                         LABEL send\n
02417                         kernel " . $strKern . "\n
02418                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " mode=badblocks $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02419                         $tmp = createPXEFile( $output );
02420                         if( $tmp !== null )
02421                         {
02422                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02423                                         
02424                                 if ( $num == 0 )
02425                                 {
02426 
02427                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02428                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02429                                         if ($ftp && $ftp_loginres )
02430                                         {
02431                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02432                                                 {
02433                                                         $sql = "insert into
02434                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02435                                                         values('" . mysql_real_escape_string($member->getHostName() . " Testdisk") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'T' )";
02436                                                         if ( mysql_query( $sql, $conn ) )
02437                                                         {
02438                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress()  );
02439                                                                 lg( _("Testdisk package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02440                                                                 @ftp_close($ftp);
02441                                                                 @unlink( $tmp );
02442                                                                 return true;
02443                                                         }
02444                                                         else
02445                                                         {
02446                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02447                                                                 $reason = mysql_error();
02448                                                         }
02449                                                 }
02450                                                 else
02451                                                         $reason = _("Unable to upload file.");
02452                                         }
02453                                         else
02454                                                 $reason = _("Unable to connect to tftp server.");
02455 
02456                                         @ftp_close($ftp);
02457                                         @unlink( $tmp );
02458                                 }
02459                         }
02460                         else
02461                                 $reason = _("Failed to open tmp file.");
02462 
02463                 }
02464                 else
02465                         $reason = _("MAC is null.");
02466         }
02467         else
02468         {
02469                 $reason = _("Either member of database connection was null");
02470         }
02471         return false;
02472 }
02473 
02474 
02475 function createPassResetPackage( $conn, $member, &$reason, $shutdown="", $kernel="", $otherargs="" )
02476 {
02477         global $currentUser;
02478         if ( $conn != null && $member != null )
02479         {
02480                 if (  $member->getMACDash() != null  )
02481                 {
02482 
02483                         $mac = strtolower( $member->getMACImageReady() );
02484 
02485                         $keymapapp = "";
02486                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02487                         if ( $keymap != null && $keymap != "" )
02488                                 $keymapapp = "keymap=$keymap";
02489 
02490                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02491                         if ( $kernel != "" )
02492                                 $strKern = $kernel;
02493                         $output = "# "._("Created by FOG Imaging System")."\n\n
02494                         DEFAULT send\n
02495                         LABEL send\n
02496                         kernel " . $strKern . "\n
02497                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  storage=" . sloppyNameLookup(getSetting( $conn, "FOG_NFS_HOST" )) . ":" . getSetting($conn, "FOG_NFS_DATADIR_UPLOAD") . " root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " mode=winpassreset $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02498                         $tmp = createPXEFile( $output );
02499                         if( $tmp !== null )
02500                         {
02501                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02502                                         
02503                                 if ( $num == 0 )
02504                                 {
02505 
02506                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02507                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02508                                         if ($ftp && $ftp_loginres )
02509                                         {
02510                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02511                                                 {
02512                                                         $sql = "insert into
02513                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02514                                                         values('" . mysql_real_escape_string($member->getHostName() . " pass reset") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'J' )";
02515                                                         if ( mysql_query( $sql, $conn ) )
02516                                                         {
02517                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
02518                                                                 lg( _("Password reset package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02519                                                                 @ftp_close($ftp);
02520                                                                 @unlink( $tmp );
02521                                                                 return true;
02522                                                         }
02523                                                         else
02524                                                         {
02525                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02526                                                                 $reason = mysql_error();
02527                                                         }
02528                                                 }
02529                                                 else
02530                                                         $reason = _("Unable to upload file.");
02531                                         }
02532                                         else
02533                                                 $reason = _("Unable to connect to tftp server.");
02534 
02535                                         @ftp_close($ftp);
02536                                         @unlink( $tmp );
02537 
02538                                 }
02539                         }
02540                         else
02541                                 $reason = _("Failed to open tmp file.");
02542 
02543                 }
02544                 else
02545                         $reason = _("MAC is null.");
02546         }
02547         else
02548         {
02549                 $reason = _("Either member of database connection was null");
02550         }
02551         return false;
02552 }
02553 
02554 // create photorec package
02555 function createPhotoRecPackage( $conn, $member, &$reason, $shutdown="", $kernel="", $otherargs="" )
02556 {
02557         global $currentUser;
02558         if ( $conn != null && $member != null )
02559         {
02560                 if (  $member->getMACDash() != null  )
02561                 {
02562 
02563                         $mac = strtolower( $member->getMACImageReady() );
02564 
02565                         $keymapapp = "";
02566                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02567                         if ( $keymap != null && $keymap != "" )
02568                                 $keymapapp = "keymap=$keymap";
02569 
02570                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02571                         if ( $kernel != "" )
02572                                 $strKern = $kernel;
02573                         $output = "# "._("Created by FOG Imaging System")."\n\n
02574                         DEFAULT send\n
02575                         LABEL send\n
02576                         kernel " . $strKern . "\n
02577                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  storage=" . sloppyNameLookup(getSetting( $conn, "FOG_NFS_HOST" )) . ":" . getSetting($conn, "FOG_NFS_DATADIR_UPLOAD") . " root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " mode=photorec $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02578                         $tmp = createPXEFile( $output );
02579                         if( $tmp !== null )
02580                         {
02581                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02582                                         
02583                                 if ( $num == 0 )
02584                                 {
02585 
02586                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02587                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02588                                         if ($ftp && $ftp_loginres )
02589                                         {
02590                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02591                                                 {
02592                                                         $sql = "insert into
02593                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02594                                                         values('" . mysql_real_escape_string($member->getHostName() . " photorec") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'R' )";
02595                                                         if ( mysql_query( $sql, $conn ) )
02596                                                         {
02597                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
02598                                                                 lg( _("Testdisk package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02599                                                                 @ftp_close($ftp);
02600                                                                 @unlink( $tmp );
02601                                                                 return true;
02602                                                         }
02603                                                         else
02604                                                         {
02605                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02606                                                                 $reason = mysql_error();
02607                                                         }
02608                                                 }
02609                                                 else
02610                                                         $reason = _("Unable to upload file.");
02611                                         }
02612                                         else
02613                                                 $reason = _("Unable to connect to tftp server.");
02614 
02615                                         @ftp_close($ftp);
02616                                         @unlink( $tmp );
02617 
02618                                 }
02619                         }
02620                         else
02621                                 $reason = _("Failed to open tmp file.");
02622 
02623                 }
02624                 else
02625                         $reason = _("MAC is null.");
02626         }
02627         else
02628         {
02629                 $reason = _("Either member of database connection was null");
02630         }
02631         return false;
02632 }
02633 
02634 
02635 function createTestDiskPackage( $conn, $member, &$reason, $shutdown="", $kernel="", $otherargs="" )
02636 {
02637         global $currentUser;
02638         if ( $conn != null && $member != null )
02639         {
02640                 if (  $member->getMACDash() != null  )
02641                 {
02642 
02643                         $mac = strtolower( $member->getMACImageReady() );
02644 
02645                         $keymapapp = "";
02646                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02647                         if ( $keymap != null && $keymap != "" )
02648                                 $keymapapp = "keymap=$keymap";
02649 
02650                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02651                         if ( $kernel != "" )
02652                                 $strKern = $kernel;
02653                         $output = "# "._("Created by FOG Imaging System")."\n\n
02654                         DEFAULT send\n
02655                         LABEL send\n
02656                         kernel " . $strKern . "\n
02657                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " mode=checkdisk $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02658                         $tmp = createPXEFile( $output );
02659                         if( $tmp !== null )
02660                         {
02661                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02662                                         
02663                                 if ( $num == 0 )
02664                                 {
02665 
02666                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02667                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02668                                         if ($ftp && $ftp_loginres )
02669                                         {
02670                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02671                                                 {
02672                                                         $sql = "insert into
02673                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02674                                                         values('" . mysql_real_escape_string($member->getHostName() . " Testdisk") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'T' )";
02675                                                         if ( mysql_query( $sql, $conn ) )
02676                                                         {
02677                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
02678                                                                 lg( _("Testdisk package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02679                                                                 @ftp_close($ftp);
02680                                                                 @unlink( $tmp );
02681                                                                 return true;
02682                                                         }
02683                                                         else
02684                                                         {
02685                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02686                                                                 $reason = mysql_error();
02687                                                         }
02688                                                 }
02689                                                 else
02690                                                         $reason = _("Unable to upload file.");
02691                                         }
02692                                         else
02693                                                 $reason = _("Unable to connect to tftp server.");
02694 
02695                                         @ftp_close($ftp);
02696                                         @unlink( $tmp );
02697 
02698                                 }
02699                         }
02700                         else
02701                                 $reason = _("Failed to open tmp file.");
02702 
02703                 }
02704                 else
02705                         $reason = _("MAC is null.");
02706         }
02707         else
02708         {
02709                 $reason = _("Either member of database connection was null");
02710         }
02711         return false;
02712 }
02713 
02714 function createWipePackage( $conn, $member, &$reason, $mode=WIPE_NORMAL, $shutdown="", $kernel="", $otherargs="" )
02715 {
02716         global $currentUser;
02717         if ( $conn != null && $member != null )
02718         {
02719                 if ( $member->getMACDash() != null  )
02720                 {
02721 
02722                         $mac = strtolower( $member->getMACImageReady() );
02723 
02724                         $wipemode="wipemode=full";
02725                         if ( $mode ==  WIPE_FAST )
02726                                 $wipemode="wipemode=fast";
02727                         else if ( $mode ==  WIPE_NORMAL )
02728                                 $wipemode="wipemode=normal";
02729                         else if ( $mode ==  WIPE_FULL )
02730                                 $wipemode="wipemode=full";
02731 
02732                         $keymapapp = "";
02733                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02734                         if ( $keymap != null && $keymap != "" )
02735                                 $keymapapp = "keymap=$keymap";
02736 
02737                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02738                         if ( $kernel != "" )
02739                                 $strKern = $kernel;
02740                         $output = "# "._("Created by FOG Imaging System")."\n\n
02741                         DEFAULT send\n
02742                         LABEL send\n
02743                         kernel " . $strKern . "\n
02744                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup( getSetting($conn, "FOG_WEB_HOST") ) . getSetting( $conn, "FOG_WEB_ROOT" ) . " osid=" . $member->getOSID() . " $wipemode mode=wipe $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02745                         $tmp = createPXEFile( $output );
02746                         if( $tmp !== null )
02747                         {
02748                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02749                                         
02750                                 if ( $num == 0 )
02751                                 {
02752 
02753                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02754                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02755                                         if ($ftp && $ftp_loginres )
02756                                         {
02757                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02758                                                 {
02759                                                         $sql = "insert into
02760                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02761                                                         values('" . mysql_real_escape_string($member->getHostName() . " Wipe") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'W' )";
02762                                                         if ( mysql_query( $sql, $conn ) )
02763                                                         {
02764                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
02765                                                                 lg( "Wipe package created for host " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02766                                                                 @ftp_close($ftp);
02767                                                                 @unlink( $tmp );
02768                                                                 return true;
02769                                                         }
02770                                                         else
02771                                                         {
02772                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02773                                                                 $reason = mysql_error();
02774                                                         }
02775                                                 }
02776                                                 else
02777                                                         $reason = _("Unable to upload file.");
02778                                         }
02779                                         else
02780                                                 $reason = _("Unable to connect to tftp server.");
02781 
02782                                         @ftp_close($ftp);
02783                                         @unlink( $tmp );
02784 
02785                                 }
02786                         }
02787                         else
02788                                 $reason = _("Failed to open tmp file.");
02789 
02790                 }
02791                 else
02792                         $reason = _("MAC is null.");
02793         }
02794         else
02795         {
02796                 $reason = _("Either member of database connection was null");
02797         }
02798         return false;
02799 }
02800 
02801 function avModeToString( $avMode )
02802 {
02803         if ( $avMode == "q" )
02804                 return _("Quarantine");
02805         else if ( $avMode == "s" )
02806                 return _("Report");
02807 }
02808 
02809 function clearAVRecord( $conn, $avID )
02810 {
02811         if ( $conn != null && $avID != null && is_numeric( $avID ) )
02812         {
02813                 $vid = mysql_real_escape_string( $avID );
02814                 $sql = "delete from virus where vID = '$vid'";
02815                 return mysql_query( $sql, $conn );
02816         }
02817         return false;
02818 }
02819 
02820 function clearAVRecordsForHost( $conn, $mac )
02821 {
02822         if ( $conn != null && $mac != null  )
02823         {
02824                 $mac = mysql_real_escape_string( $mac );
02825                 $sql = "delete from virus where vHostMAC = '$mac'";
02826                 return mysql_query( $sql, $conn );
02827         }
02828         return false;
02829 }
02830 
02831 function clearAllAVRecords( $conn )
02832 {
02833         if ( $conn != null  )
02834         {
02835                 $sql = "delete from virus";
02836                 return mysql_query( $sql, $conn );
02837         }
02838         return false;
02839 }
02840 
02841 function createAVPackage( $conn, $member, &$reason, $mode=FOG_AV_SCANONLY, $shutdown="", $kernel="", $otherargs="" )
02842 {
02843         global $currentUser;
02844         if ( $conn != null && $member != null )
02845         {
02846                 if ( $member->getMACDash() != null  )
02847                 {
02848 
02849                         $mac = strtolower( $member->getMACImageReady() );
02850 
02851                         $scanmode="avmode=s";
02852                         if ( $mode ==  FOG_AV_SCANQUARANTINE )
02853                                 $scanmode="avmode=q";
02854 
02855                         $keymapapp = "";
02856                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02857                         if ( $keymap != null && $keymap != "" )
02858                                 $keymapapp = "keymap=$keymap";
02859 
02860                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
02861                         if ( $kernel != "" )
02862                                 $strKern = $kernel;
02863                         $output = "# "._("Created by FOG Imaging System")."\n\n
02864                         DEFAULT send\n
02865                         LABEL send\n
02866                         kernel " . $strKern . "\n
02867                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mac=" . $member->getMACColon() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " osid=" . $member->getOSID() . " $scanmode mode=clamav $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
02868                         $tmp = createPXEFile( $output );
02869                         if( $tmp !== null )
02870                         {
02871                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
02872                                         
02873                                 if ( $num == 0 )
02874                                 {
02875 
02876                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
02877                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
02878                                         if ($ftp && $ftp_loginres )
02879                                         {
02880                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
02881                                                 {
02882                                                         $sql = "insert into
02883                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
02884                                                         values('" . mysql_real_escape_string($member->getHostName() . " ClamScan") . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'v' )";
02885                                                         if ( mysql_query( $sql, $conn ) )
02886                                                         {
02887                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
02888                                                                 lg( _("ClamAV package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
02889                                                                 @ftp_close($ftp);
02890                                                                 @unlink( $tmp );
02891                                                                 return true;
02892                                                         }
02893                                                         else
02894                                                         {
02895                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
02896                                                                 $reason = mysql_error();
02897                                                         }
02898                                                 }
02899                                                 else
02900                                                         $reason = _("Unable to upload file.");
02901                                         }
02902                                         else
02903                                                 $reason = _("Unable to connect to tftp server.");
02904 
02905                                         @ftp_close($ftp);
02906                                         @unlink( $tmp );
02907 
02908                                 }
02909                                 else
02910                                         $reason = _("Ya existe una tarea activa para este equipo!");
02911                         }
02912                         else
02913                                 $reason = _("Failed to open tmp file.");
02914 
02915                 }
02916                 else
02917                         $reason = _("MAC is null.");
02918         }
02919         else
02920         {
02921                 $reason = _("Either member of database connection was null");
02922         }
02923         return false;
02924 }
02925 
02926 
02927 function createUploadImagePackage( $conn, $member, &$reason, $debug=false, $shutdown="", $kernel="", $otherargs="" )
02928 {
02929         global $currentUser;
02930         if ( $conn != null && $member != null )
02931         {
02932                 if (  $member->getImage() != null && $member->getMACDash() != null  )
02933                 {
02934 
02935                         $mac = strtolower( $member->getMACImageReady() );
02936 
02937                         $image = $member->getImage();
02938                         $imageid = $member->getImageID();
02939                         $building = $member->getBuilding();
02940 
02941                         $mode = "";
02942                         if ($debug)
02943                                 $mode = "mode=debug";
02944 
02945                         // since this is an upload, we need to get the master server for the NFS Group
02946                         if( $member->getStorageGroup() !== null  && is_numeric( $member->getStorageGroup()  ) )
02947                         {
02948 
02949                                 $sql = "SELECT
02950                                 *
02951                                 FROM
02952                                 nfsGroups
02953                                 INNER JOIN nfsGroupMembers on ( nfsGroups.ngID = nfsGroupMembers.ngmGroupID )
02954                                 WHERE
02955                                 ngID = '"  . $member->getStorageGroup() . "' and
02956                                 ngmIsEnabled = '1' and
02957                                 ngmIsMasterNode = '1'";
02958 
02959                                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
02960                                 if ( mysql_num_rows( $res ) == 1 )
02961                                 {
02962 
02963                                         while( $ar = mysql_fetch_array( $res ) )
02964                                         {
02965                                                 $nfsip = trim(mysql_real_escape_string($ar["ngmHostname"]));
02966                                                 $nfsroot = trim(mysql_real_escape_string($ar["ngmRootPath"]));
02967                                                 $groupid = trim(mysql_real_escape_string( $ar["ngID"] ));
02968                                                 $nodeid  = trim(mysql_real_escape_string( $ar["ngmID"] ));
02969 
02970 
02971                                                 if ( $nfsip != null && $nfsroot != null && is_numeric( $groupid ) && is_numeric( $nodeid ) )
02972                                                 {
02973                                                         if ( endsWith( $nfsroot, "/" )  )
02974                                                                 $nfsroot .= "dev/";
02975                                                         else
02976                                                                 $nfsroot .= "/dev/";
02977 
02978                                                         $imgType = "imgType=n";
02979                                                         if ( $member->getImageType() == ImageMember::IMAGETYPE_DISKIMAGE )
02980                                                                 $imgType = "imgType=dd";
02981                                                         else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE )
02982                                                                 $imgType = "imgType=mps";
02983                                                         else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES )
02984                                                                 $imgType = "imgType=mpa";
02985 
02986                                                         $pct = "pct=5"; // default percentage
02987 
02988                                                         if ( is_numeric(getSetting($conn, "FOG_UPLOADRESIZEPCT") ) && getSetting($conn, "FOG_UPLOADRESIZEPCT") >= 5 && getSetting($conn, "FOG_UPLOADRESIZEPCT") < 100 )
02989                                                                 $pct = "pct=" . getSetting($conn, "FOG_UPLOADRESIZEPCT");
02990 
02991                                                         $ignorepg = "0";
02992 
02993                                                         if ( getSetting( $conn, "FOG_UPLOADIGNOREPAGEHIBER" ) )
02994                                                                 $ignorepg = "1";
02995 
02996                                                         $keymapapp = "";
02997                                                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
02998                                                         if ( $keymap != null && $keymap != "" )
02999                                                                 $keymapapp = "keymap=$keymap";
03000 
03001                                                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
03002                                                         if ( $kernel != "" )
03003                                                                 $strKern = $kernel;
03004                                                         $output = "# "._("Created by FOG Imaging System")."\n\n
03005                                                         DEFAULT send\n
03006                                                         LABEL send\n
03007                                                         kernel " . $strKern . "\n
03008                                                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " type=up img=$image imgid=$imageid mac=" . $member->getMACColon() . " storage=" . $nfsip . ":" . $nfsroot . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " ignorepg=$ignorepg osid=" . $member->getOSID() . " $mode $pct $imgType $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
03009                                                         $tmp = createPXEFile( $output );
03010                                                         if( $tmp !== null )
03011                                                         {
03012                                                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03013                                                                         
03014                                                                 if ( $num == 0 )
03015                                                                 {
03016 
03017                                                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03018                                                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
03019                                                                         if ($ftp && $ftp_loginres )
03020                                                                         {
03021                                                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03022                                                                                 {
03023                                                                                         $sql = "insert into
03024                                                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType, taskNFSGroupID, taskNFSMemberID )
03025                                                                                         values('" . mysql_real_escape_string($member->getHostName()) . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'U', '$groupid', '$nodeid' )";
03026                                                                                         if ( mysql_query( $sql, $conn ) )
03027                                                                                         {
03028                                                                                                 switchOff($member->getIPAddress(), true, "FOG@ehu:%20iniciada%20tarea%20clonar%20equipo");
03029                                                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03030                                                                                                 lg( _("Image upload package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03031                                                                                                 @ftp_close($ftp);
03032                                                                                                 @unlink( $tmp );
03033                                                                                                 return true;
03034                                                                                         }
03035                                                                                         else
03036                                                                                         {
03037                                                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03038                                                                                                 $reason = mysql_error();
03039                                                                                         }
03040                                                                                 }
03041                                                                                 else
03042                                                                                         $reason = _("Unable to upload file.");
03043                                                                         }
03044                                                                         else
03045                                                                                 $reason = _("Unable to connect to tftp server.");
03046 
03047                                                                         @ftp_close($ftp);
03048                                                                         @unlink( $tmp );
03049 
03050                                                                 }
03051                                                         }
03052                                                         else
03053                                                                 $reason = _("Failed to open tmp file.");
03054                                                 }
03055                                                 else
03056                                                         $reason = _("Unable to determine valid settings for NFS Server.");
03057                                         }
03058                                 }
03059                                 else
03060                                         $reason = _("Unable to located master node from storage group.");
03061                         }
03062                         else
03063                                 $reason = _("Unable to determine the Group ID number.");
03064 
03065                 }
03066                 else
03067                 {
03068                         if( $member->getImage() == null )
03069                                 $reason = _("No se ha asociado ninguna imagen a este equipo.");
03070 
03071                         if ( $member->getMACDash() == null )
03072                                 $reason = _("MAC Address is null");
03073                 }
03074         }
03075         else
03076         {
03077                 $reason = _("Either member of database connection was null");
03078         }
03079         return false;
03080 }
03081 
03082 function getCountOfActiveTasksWithMAC( $conn, $mac )
03083 {
03084         if ( $conn != null && $mac != null )
03085         {
03086                 $sql = "select count(*) as cnt
03087                 from tasks
03088                 inner join hosts on ( tasks.taskHostID = hostID )
03089                 where hostMAC = '" . mysql_real_escape_string($mac) . "' and tasks.taskState in (0,1)";
03090                 $res = mysql_query( $sql, $conn );
03091                 if ( $res )
03092                 {
03093                         if ( $ar = mysql_fetch_array( $res ) )
03094                         {
03095                                 return $ar["cnt"];
03096                         }
03097                 }
03098         }
03099         return -1;
03100 }
03101 
03102 function createMemTestPackage($conn, $member, &$reason)
03103 {
03104         // Load memtest86+
03105         global $currentUser;
03106         if ( $conn != null && $member != null )
03107         {
03108                 if ( $member->getMACDash() != null )
03109                 {
03110                         $mac = strtolower( $member->getMACImageReady() );
03111 
03112                         $output = "# "._("Created by FOG Imaging System")."\n\n
03113                         DEFAULT fog\n
03114                         LABEL fog\n
03115                         kernel " . getSetting($conn, "FOG_MEMTEST_KERNEL" ) . "\n";
03116 
03117                         $tmp = createPXEFile( $output );
03118 
03119                         if( $tmp !== null )
03120                         {
03121                                 // make sure there is no active task for this mac address
03122                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03123 
03124                                 if ( $num == 0 )
03125                                 {
03126                                         // attempt to ftp file
03127 
03128                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03129                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ) );
03130                                         if ($ftp && $ftp_loginres )
03131                                         {
03132                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03133                                                 {
03134                                                         $sql = "insert into
03135                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
03136                                                         values('" . mysql_real_escape_string('MEMTEST') . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'M' )";
03137                                                         if ( mysql_query( $sql, $conn ) )
03138                                                         {
03139                                                                 // lets try to wake the computer up!
03140                                                                         
03141                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03142                                                                 lg( _("memtest package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03143                                                                 @ftp_close($ftp);
03144                                                                 @unlink( $tmp );
03145                                                                 return true;
03146                                                         }
03147                                                         else
03148                                                         {
03149                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03150                                                                 $reason = mysql_error();
03151                                                         }
03152                                                 }
03153                                                 else
03154                                                         $reason = _("Unable to upload file.");
03155                                         }
03156                                         else
03157                                                 $reason = _("Unable to connect to tftp server.");
03158 
03159                                         @ftp_close($ftp);
03160                                         @unlink( $tmp );
03161                                 }
03162                                 else
03163                                         $reason = _("Host is already a member of a active task.");
03164                         }
03165                         else
03166                                 $reason = _("Failed to open tmp file.");
03167 
03168                 }
03169                 else
03170                 {
03171                         if ( $member->getMACDash() == null )
03172                                 $reason = _("MAC Address is null");
03173                 }
03174         }
03175         else
03176         {
03177                 $reason = _("Either member of database connection was null");
03178         }
03179         return false;
03180 }
03181 
03182 
03183 function createDebugPackage($conn, $member, &$reason, $kernel="", $otherargs="")
03184 {
03185         // Just load image
03186         global $currentUser;
03187         if ( $conn != null && $member != null )
03188         {
03189                 if ( $member->getMACDash() != null )
03190                 {
03191                         $mac = strtolower( $member->getMACImageReady() );
03192 
03193                         $keymapapp = "";
03194                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
03195                         if ( $keymap != null && $keymap != "" )
03196                                 $keymapapp = "keymap=$keymap";
03197 
03198                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
03199                         if ( $kernel != "" )
03200                                 $strKern = $kernel;
03201                         $output = "# "._("Created by FOG Imaging System")."\n\n
03202                         DEFAULT fog\n
03203                         LABEL fog\n
03204                         kernel " . $strKern . "\n
03205                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=onlydebug $keymapapp " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
03206 
03207                         $tmp = createPXEFile( $output );
03208 
03209                         if( $tmp !== null )
03210                         {
03211                                 // make sure there is no active task for this mac address
03212                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03213 
03214                                 if ( $num == 0 )
03215                                 {
03216                                         // attempt to ftp file
03217 
03218                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03219                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
03220                                         if ($ftp && $ftp_loginres )
03221                                         {
03222                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03223                                                 {
03224                                                         $sql = "insert into
03225                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
03226                                                         values('" . mysql_real_escape_string('DEBUG') . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'X' )";
03227                                                         if ( mysql_query( $sql, $conn ) )
03228                                                         {
03229                                                                 // lets try to wake the computer up!
03230                                                                 switchOff($member->getIPAddress(), true, "FOG@ehu:%20iniciando%20tarea%20depurar%20equipo");
03231                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03232                                                                 lg( _("debug package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03233                                                                 @ftp_close($ftp);
03234                                                                 @unlink( $tmp );
03235                                                                 return true;
03236                                                         }
03237                                                         else
03238                                                         {
03239                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03240                                                                 $reason = mysql_error();
03241                                                         }
03242                                                 }
03243                                                 else
03244                                                         $reason = _("Unable to upload file.");
03245                                         }
03246                                         else
03247                                                 $reason = _("Unable to connect to tftp server.");
03248 
03249                                         @ftp_close($ftp);
03250                                         @unlink( $tmp );
03251                                 }
03252                                 else
03253                                         $reason = _("Host is already a member of a active task.");
03254                         }
03255                         else
03256                                 $reason = _("Failed to open tmp file.");
03257 
03258                 }
03259                 else
03260                 {
03261                         if ( $member->getMACDash() == null )
03262                                 $reason = _("MAC Address is null");
03263                 }
03264         }
03265         else
03266         {
03267                 $reason = _("Either member of database connection was null");
03268         }
03269         return false;
03270 }
03271 
03272 function getMulticastPort( $conn )
03273 {
03274         $endingPort = getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" ) + (getSetting( $conn, "FOG_MULTICAST_MAX_SESSIONS" ) * 2);
03275         if ( $conn !== null && getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" ) !== null && isValidPortNumber( getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" ) ) && isValidPortNumber( $endingPort ) )
03276         {
03277                 $sql = "select msBasePort from multicastSessions order by msID desc limit 1";
03278                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03279 
03280                 $recPort = getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" );
03281 
03282                 if ( $ar = mysql_fetch_array( $res ) )
03283                 {
03284                         $potPort = ($ar["msBasePort"]  + 2);
03285                         if ( $potPort >= getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" ) && ($potPort + 1) < $endingPort )
03286                         {
03287                                 $recPort = $potPort;
03288                         }
03289                 }
03290 
03291                 if ( ( ( $recPort % 2 ) == 0 ) && $recPort >= getSetting( $conn, "FOG_UDPCAST_STARTINGPORT" ) && $recPort + 1 < $endingPort )
03292                 {
03293                         return $recPort;
03294                 }
03295         }
03296         return -1;
03297 }
03298 
03299 function isValidPortNumber( $port )
03300 {
03301         if ( $port <= 65535 && $port > 0 && is_numeric($port) )
03302                 return true;
03303 
03304         return false;
03305 }
03306 
03307 
03308 function deleteMulticastJob( $conn, $mcid )
03309 {
03310         // first pulls all the associations, delete the jobs in task table, delete associations, then deletes mc task.
03311         if ( $conn != null && is_numeric( $mcid ) && $mcid !== null )
03312         {
03313                 $mcid = mysql_real_escape_string( $mcid );
03314                 $sql = "select tID from multicastSessionsAssoc where msID = '$mcid'";
03315                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03316                 while( $ar = mysql_fetch_array( $res ) )
03317                 {
03318                         $sql = "select taskHostID from tasks where taskID = '" . mysql_real_escape_string( $ar["tID"] ) . "'";
03319                         $res_hostid = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03320                         if ( $ar_hid = mysql_fetch_array( $res_hostid ) )
03321                         {
03322                                 $im = getImageMemberFromHostID( $conn, $ar_hid["taskHostID"] );
03323                                 if ( $im != null )
03324                                 {
03325                                         if ( ! ftpDelete( getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $im->getMACImageReady() ) )
03326                                         {
03327                                                 msgBox( _("Unable to delete PXE file") );
03328                                         }
03329                                 }
03330                                 cancelSnapinsForHost( $conn, $ar_hid["taskHostID"] );
03331                         }
03332                         $sql = "delete from tasks where taskID = '" . mysql_real_escape_string( $ar["tID"] ) . "'";
03333                         mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03334 
03335                 }
03336 
03337                 // now remove all the associations
03338                 $sql = "delete from multicastSessionsAssoc where msID = '$mcid'";
03339                 mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03340 
03341                 // now remove the multicast task
03342                 $sql = "update multicastSessions set msState = '2' where msID = '" . $mcid . "'";
03343                 mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03344                 return true;
03345         }
03346         return false;
03347 }
03348 
03349 // returns the parent job id
03350 function createMulticastJob( $conn, $name, $port, $path, $eth, $imagetype, $nfsGroupID )
03351 {
03352         if ( $conn != null && isValidPortNumber($port) && $path !== null  )
03353         {
03354                 $name = mysql_real_escape_string( $name );
03355                 $port = mysql_real_escape_string( $port );
03356                 $path = mysql_real_escape_string( $path );
03357                 $eth  = mysql_real_escape_string( $eth );
03358                 $dd = mysql_real_escape_string($imagetype);
03359                 $nfsGroupID = mysql_real_escape_string( $nfsGroupID );
03360 
03361                 $sql = "insert
03362                 into multicastSessions
03363                 (msName, msBasePort, msImage, msInterface, msStartDateTime, msPercent, msState, msIsDD, msNFSGroupID )
03364                 values
03365                 ('$name', '$port', '$path', '$eth', NOW(), '0', '-1', '$dd', '$nfsGroupID')";
03366                 mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03367                 $id = mysql_insert_id( $conn );
03368                 if ( $id !== null )
03369                         return $id;
03370         }
03371         return -1;
03372 }
03373 
03374 function activateMulticastJob( $conn, $mcid )
03375 {
03376         if ( $conn != null && is_numeric( $mcid ) )
03377         {
03378                 $sql = "UPDATE
03379                 multicastSessions
03380                 SET
03381                 msState = '0'
03382                 WHERE
03383                 msID = '$mcid'";
03384                 if ( mysql_query( $sql, $conn ) )
03385                         return true;
03386         }
03387         return false;
03388 }
03389 
03390 function linkTaskToMultitaskJob( $conn, $taskid, $mcid )
03391 {
03392         if ( $conn != null && $taskid !== null && $mcid !== null && is_numeric( $taskid) && is_numeric( $mcid ) )
03393         {
03394                 $taskid = mysql_real_escape_string( $taskid );
03395                 $mcid = mysql_real_escape_string( $mcid );
03396 
03397                 $sql = "insert into multicastSessionsAssoc(msID, tID) values('$mcid', '$taskid')";
03398 
03399                 mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03400                 return true;
03401         }
03402         return false;
03403 }
03404 
03405 // this function return the insert id so the multicast session can be linked with the single tasks
03406 function createImagePackageMulticast($conn, $member, $taskName, $port, &$reason, $debug=false, $deploySnapins=true, $shutdown="", $kernel="", $otherargs="hola=1", $bashscript="", $type="C", $taskPCT=0)
03407 {
03408         global $currentUser;
03409 
03410         if ( $conn != null && $member != null  )
03411         {
03412                 if ( $port !== null && is_numeric( $port ) )
03413                 {
03414                         if ( $member->getImage() != null && $member->getMACDash() != null )
03415                         {
03416                                 if ( $member->getStorageGroup() != null && is_numeric( $member->getStorageGroup() ) )
03417                                 {
03418 
03419                                         $blStorageOk = false;
03420                                         $snIP = null;
03421                                         $snRoot = null;
03422                                         $sql = "SELECT * FROM nfsGroupMembers WHERE ngmGroupID = '" . $member->getStorageGroup() . "' and ngmIsMasterNode = '1' and ngmIsEnabled = '1'";
03423                                         $res = mysql_query( $sql, $conn ) or die( mysql_error() );
03424                                         if ( mysql_num_rows( $res ) == 1 )
03425                                         {
03426                                                 while( $ar = mysql_fetch_array( $res ) )
03427                                                 {
03428                                                         $snIP = $ar["ngmHostname"];
03429                                                         $snRoot = $ar["ngmRootPath"];
03430                                                         $blStorageOk = true;
03431                                                 }
03432                                         }
03433 
03434                                         if ( $blStorageOk )
03435                                         {
03436                                                 $mac = strtolower( $member->getMACImageReady() );
03437 
03438                                                 $image = $member->getImage();
03439                                                 $mode = "";
03440                                                 if ($debug)
03441                                                         $mode = "mode=debug";
03442 
03443                                                 $imgType = "imgType=n";
03444                                                 #bcsgaguu
03445                                                 #if ( $member->getImageType() == ImageMember::IMAGETYPE_DISKIMAGE )
03446                                                 #       $imgType = "imgType=dd";
03447                                                 #else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE )
03448                                                 #       $imgType = "imgType=mps";
03449                                                 #else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES )
03450                                                 #       $imgType = "imgType=mpa";
03451                                                 #else
03452                                                 #{
03453                                                 #       if ( $member->getOSID() == "99" )
03454                                                 #       {
03455                                                 #               $reason = _("Invalid OS type, unable to determine MBR.");
03456                                                 #               return -1;
03457                                                 #       }
03458                                                         
03459                                                 #       if ( strlen( trim($member->getOSID()) ) == 0 )
03460                                                 #       {
03461                                                 #               $reason = _("Invalid OS type, you must specify an OS Type to image.");
03462                                                 #               return -1;
03463                                                 #       }
03464                                                         
03465                                                 #       if ( trim($member->getOSID()) != "1" && trim($member->getOSID()) != "2" && trim($member->getOSID()) != "5")
03466                                                 #       {
03467                                                 #               $reason = _("Unsupported OS detected in host!");
03468                                                 #               return -1;
03469                                                 #       }
03470                                                 #}
03471                                                 $imageid = $member->getImageID();
03472                                                 $keymapapp = "";
03473                                                 $keymap = getSetting( $conn, "FOG_KEYMAP" );
03474                                                 if ( $keymap != null && $keymap != "" )
03475                                                         $keymapapp = "keymap=$keymap";
03476 
03477                                                 $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
03478                                                 if ( $kernel != "" )
03479                                                         $strKern = $kernel;
03480                                                 $output = "# "._("Created by FOG Imaging System, ").$kernel."\n\n
03481                                                 DEFAULT fog\n
03482                                                 LABEL fog\n
03483                                                 kernel " . $strKern . "\n
03484                                                 append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . " root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " type=down img=$image imgid=$imageid mc=yes port=" . $port . " storageip=" . $snIP . " storage=" . $snIP . ":" . $snRoot . " mac=" . $member->getMACColon() . " ftp=" . sloppyNameLookup(getSetting( $conn, "FOG_TFTP_HOST" )) . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " osid=" . $member->getOSID() . " $mode $imgType $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
03485                                                         
03486                                                 $tmp = createPXEFile( $output );
03487 
03488                                                 if( $tmp !== null )
03489                                                 {
03490                                                         // make sure there is no active task for this mac address
03491                                                         $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03492 
03493                                                         if ( $num == 0 )
03494                                                         {
03495                                                                 // attempt to ftp file
03496                                                                         
03497                                                                 $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03498                                                                 $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
03499                                                                 if ($ftp && $ftp_loginres )
03500                                                                 {
03501                                                                         if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03502                                                                         {
03503                                                                                 $sql = "insert into
03504                                                                                 tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType, taskScript, taskNFSGroupID, taskNFSMemberID, taskPCT )
03505                                                                                 values('" . mysql_real_escape_string($taskName) . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', '$type', '$bashscript', '1', '1', $taskPCT  )";
03506                                                                                 if ( mysql_query( $sql, $conn ) )
03507                                                                                 {
03508                                                                                         $insertId = mysql_insert_id( $conn );
03509                                                                                         if ( $insertId !== null && $insertId >= 0 )
03510                                                                                         {
03511                                                                                                 if ( $deploySnapins )
03512                                                                                                 {
03513                                                                                                         // Remove any exists snapin tasks
03514                                                                                                         cancelSnapinsForHost( $conn, $member->getID() );
03515 
03516                                                                                                         // now do a clean snapin deploy
03517                                                                                                         deploySnapinsForHost( $conn, $member->getID() );
03518                                                                                                 }
03519 
03520                                                                                                 // lets try to wake the computer up!
03521                                                                                                 switchOff($member->getIPAddress(), true, "FOG@ehu:%20iniciando%20tarea%20de%20despliegue%20multicast");
03522                                                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03523                                                                                                 lg( _("Image push multicast package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03524                                                                                                 @ftp_close($ftp);
03525                                                                                                 @unlink( $tmp );
03526                                                                                                 return $insertId;
03527                                                                                         }
03528                                                                                 }
03529                                                                                 else
03530                                                                                 {
03531                                                                                         ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03532                                                                                         $reason = mysql_error();
03533                                                                                 }
03534                                                                         }
03535                                                                         else
03536                                                                                 $reason = _("Unable to upload file.");
03537                                                                 }
03538                                                                 else
03539                                                                         $reason = _("Unable to connect to tftp server.");
03540                                                                         
03541                                                                 @ftp_close($ftp);
03542                                                                 @unlink( $tmp );
03543                                                         }
03544                                                         else
03545                                                                 $reason = _("Host is already a member of a active task.");
03546                                                 }
03547                                                 else
03548                                                         $reason = _("Failed to open tmp file.");
03549                                         }
03550                                         else
03551                                                 $reason = _("Unable to locate master storage node.");
03552                                 }
03553                                 else
03554                                         $reason = _("Invalid NFS Group ID");
03555                         }
03556                         else
03557                         {
03558                                 if( $member->getImage() == null )
03559                                         $reason = _("Image assocation is null, please define an image for this host.");
03560 
03561                                 if ( $member->getMACDash() == null )
03562                                         $reason = _("MAC Address is null");
03563                         }
03564 
03565                 }
03566                 else
03567                 {
03568                         $reason = _("Invalid port number".", $port");
03569                 }
03570         }
03571         else
03572         {
03573                 $reason = _("Either member or database connection was null");
03574         }
03575         return -1;
03576 }
03577 
03578 function createImagePackage($conn, $member, $taskName, &$reason, $debug=false, $deploySnapins=true, $shutdown="", $kernel="", $otherargs="" , $bashscript="")
03579 {
03580         global $currentUser;
03581         if ( $conn != null && $member != null )
03582         {
03583                 if ( $member->getImage() != null && $member->getMACDash() != null )
03584                 {
03585                         $mac = strtolower( $member->getMACImageReady() );
03586 
03587                         if ( $member->getNFSRoot() == null || $member->getNFSServer() == null || $member->getStorageNode() === null || $member->getStorageGroup() === null)
03588                         {
03589                                 $reason = _("Unable to determine a valid storage server root path or server IP.");
03590                                 return false;
03591                         }
03592 
03593                         $image = $member->getImage();
03594                         $imageid = $member->getImageID();
03595                         $mode = "";
03596                         if ($debug)
03597                                 $mode = "mode=debug";
03598 
03599                         $imgType = "imgType=n";
03600                         if ( $member->getImageType() == ImageMember::IMAGETYPE_DISKIMAGE )
03601                                 $imgType = "imgType=dd";
03602                         else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_SINGLEDRIVE )
03603                                 $imgType = "imgType=mps";
03604                         else if ( $member->getImageType() == ImageMember::IMAGETYPE_MULTIPART_ALLDRIVES )
03605                                 $imgType = "imgType=mpa";
03606                         else
03607                         {
03608                                 if ( $member->getOSID() == "99" )
03609                                 {
03610                                         $reason = _("Invalid OS type, unable to determine MBR.");
03611                                         return false;
03612                                 }
03613 
03614                                 if ( strlen( trim($member->getOSID()) ) == 0 )
03615                                 {
03616                                         $reason = _("Invalid OS type, you must specify an OS Type to image.");
03617                                         return false;
03618                                 }
03619 
03620                                 if ( trim($member->getOSID()) != "1" && trim($member->getOSID()) != "2" && trim($member->getOSID()) != "5" )
03621                                 {
03622                                         $reason = _("Unsupported OS detected in host!");
03623                                         return false;
03624                                 }
03625                         }
03626 
03627                         $keymapapp = "";
03628                         $keymap = getSetting( $conn, "FOG_KEYMAP" );
03629                         if ( $keymap != null && $keymap != "" )
03630                                 $keymapapp = "keymap=$keymap";
03631 
03632                         $strKern = getSetting($conn, "FOG_TFTP_PXE_KERNEL" );
03633                         if ( $kernel != "" )
03634                                 $strKern = $kernel;
03635                         $output = "# "._("Created by FOG Imaging System")."\n\n
03636                         DEFAULT fog\n
03637                         LABEL fog\n
03638                         kernel " . $strKern . "\n
03639                         append initrd=" . getSetting( $conn, "FOG_PXE_BOOT_IMAGE" ) . "  root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " type=down img=$image imgid=$imageid mac=" . $member->getMACColon() . " ftp=" . sloppyNameLookup(getSetting( $conn, "FOG_TFTP_HOST" )) . " storage=" . $member->getNFSServer() . ":" . $member->getNFSRoot() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " osid=" . $member->getOSID() . " $mode $imgType $keymapapp shutdown=$shutdown loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
03640 
03641                         $tmp = createPXEFile( $output );
03642 
03643                         if( $tmp !== null )
03644                         {
03645                                 // make sure there is no active task for this mac address
03646                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03647 
03648                                 if ( $num == 0 )
03649                                 {
03650                                         // attempt to ftp file
03651 
03652                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03653                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ));
03654                                         if ($ftp && $ftp_loginres )
03655                                         {
03656                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03657                                                 {
03658                                                         $uname = "";
03659                                                         if ( $currentUser != null )
03660                                                                 $uname = mysql_real_escape_string( $currentUser->getUserName() );
03661 
03662                                                         $sql = "insert into
03663                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType, taskNFSGroupID, taskNFSMemberID, taskScript )
03664                                                         values('" . mysql_real_escape_string($taskName) . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . $uname . "', '0', 'D', '" . $member->getStorageGroup() . "', '" . $member->getStorageNode() . "', '$bashscript' )";
03665                                                         if ( mysql_query( $sql, $conn ) )
03666                                                         {
03667                                                                 if ( $deploySnapins )
03668                                                                 {
03669                                                                         // Remove any exists snapin tasks
03670                                                                         cancelSnapinsForHost( $conn, $member->getID() );
03671 
03672                                                                         // now do a clean snapin deploy
03673                                                                         deploySnapinsForHost( $conn, $member->getID() );
03674                                                                 }
03675 
03676                                                                 // lets try to wake the computer up!
03677                                                                 switchOff($member->getIPAddress(), true, "FOG@ehu:%20iniciando%20tarea%20de%20descarga%20unicast");
03678                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03679                                                                 lg( _("Image push package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03680                                                                 @ftp_close($ftp);
03681                                                                 @unlink( $tmp );
03682                                                                 return true;
03683                                                         }
03684                                                         else
03685                                                         {
03686                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03687                                                                 $reason = mysql_error();
03688                                                         }
03689                                                 }
03690                                                 else
03691                                                         $reason = _("Unable to upload file.");
03692                                         }
03693                                         else
03694                                                 $reason = _("Unable to connect to tftp server.");
03695 
03696                                         @ftp_close($ftp);
03697                                         @unlink( $tmp );
03698                                 }
03699                                 else
03700                                         $reason = _("Host is already a member of a active task.");
03701                         }
03702                         else
03703                                 $reason = _("Failed to open tmp file.");
03704 
03705                 }
03706                 else
03707                 {
03708                         if( $member->getImage() == null )
03709                                 $reason = _("Image assocation is null, please define an image for this host.");
03710 
03711                         if ( $member->getMACDash() == null )
03712                                 $reason = _("MAC Address is null");
03713                 }
03714         }
03715         else
03716         {
03717                 $reason = _("Either member of database connection was null");
03718         }
03719         return false;
03720 }
03721 
03734 function createBootPackage($conn, $member, $otherargs="", &$reason)
03735 {
03736         global $currentUser;
03737         if ( $conn != null && $member != null )
03738         {
03739                 if ( $member->getMACDash() != null )
03740                 {
03741                         $mac = strtolower( $member->getMACImageReady() );
03742 
03743                         $output = "# "._("Created by FOG Imaging System")."\n\n
03744                         DEFAULT fog\n
03745                         LABEL fog\n
03746                         kernel fog/kernel/bzImage\n
03747                         append initrd=fog/images/init.gz root=/dev/ram0 rw ramdisk_size=" . getSetting( $conn, "FOG_KERNEL_RAMDISK_SIZE" ) . " ip=dhcp dns=" . getSetting($conn, "FOG_PXE_IMAGE_DNSADDRESS" ) . " mode=boot mac_deployed=" . $member->getMACColon() . " ftp=" . sloppyNameLookup(getSetting( $conn, "FOG_TFTP_HOST" )) . " storage=" . $member->getNFSServer() . ":" . $member->getNFSRoot() . " web=" . sloppyNameLookup(getSetting($conn, "FOG_WEB_HOST")) . getSetting( $conn, "FOG_WEB_ROOT" ) . " shutdown= loglevel=4 " . getSetting( $conn, "FOG_KERNEL_ARGS" ) . " " . $member->getKernelArgs() . " " . $otherargs;
03748 
03749                         $tmp = createPXEFile( $output );
03750 
03751                         if( $tmp !== null )
03752                         {
03753                                 // make sure there is no active task for this mac address
03754                                 $num = getCountOfActiveTasksWithMAC( $conn, $member->getMACColon());
03755 
03756                                 if ( $num == 0 )
03757                                 {
03758                                         // attempt to ftp file
03759                                         $taskName = $member->getHostName(). " Boot";
03760                                         $ftp = ftp_connect(getSetting( $conn, "FOG_TFTP_HOST" ));
03761                                         $ftp_loginres = ftp_login($ftp, getSetting( $conn, "FOG_TFTP_FTP_USERNAME" ), getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" ) );
03762                                         if ($ftp && $ftp_loginres )
03763                                         {
03764                                                 if ( ftp_put( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac, $tmp, FTP_ASCII ) )
03765                                                 {
03766                                                         $sql = "insert into
03767                                                         tasks(taskName, taskCreateTime, taskCheckIn, taskHostID, taskState, taskCreateBy, taskForce, taskType )
03768                                                         values('" . mysql_real_escape_string($taskName) . "', NOW(), NOW(), '" . $member->getID() . "', '0', '" . mysql_real_escape_string( $currentUser->getUserName() ) . "', '0', 'B' )";
03769                                                         if ( mysql_query( $sql, $conn ) )
03770                                                         {
03771                                                                 // lets try to wake the computer up!
03772                                                                         
03773                                                                 wakeUp( $member->getMACColon(), $member->getIPAddress() );
03774                                                                 lg( _("boot package created for host")." " . $member->getHostName() . " [" . $member->getMACDash() . "]" );
03775                                                                 @ftp_close($ftp);
03776                                                                 @unlink( $tmp );
03777                                                                 return true;
03778                                                         }
03779                                                         else
03780                                                         {
03781                                                                 ftp_delete( $ftp, getSetting( $conn, "FOG_TFTP_PXE_CONFIG_DIR" ) . $mac );
03782                                                                 $reason = mysql_error();
03783                                                         }
03784                                                 }
03785                                                 else
03786                                                         $reason = _("Unable to upload file.");
03787                                         }
03788                                         else
03789                                                 $reason = _("Unable to connect to tftp server.");
03790 
03791                                         @ftp_close($ftp);
03792                                         @unlink( $tmp );
03793                                 }
03794                                 else
03795                                         $reason = _("Host is already a member of a active task.");
03796                         }
03797                         else
03798                                 $reason = _("Failed to open tmp file.");
03799 
03800                 }
03801                 else
03802                 {
03803                         if ( $member->getMACDash() == null )
03804                                 $reason = _("MAC Address is null");
03805                 }
03806         }
03807         else
03808         {
03809                 $reason = _("Either member of database connection was null");
03810         }
03811         return false;
03812 }
03813 
03814 /*
03815  *
03816 *    Below are functions that are used in the service scripts
03817 *
03818 *
03819 */
03820 
03821 function cleanIncompleteTasks( $conn, $hostid )
03822 {
03823         if ( $conn != null && $hostid != null )
03824         {
03825                 $sql = "update tasks set taskState = '0' where taskHostID = '" . mysql_real_escape_string($hostid) . "' and taskState = '1'";
03826                 mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03827         }
03828 }
03829 
03830 function queuedTaskExists( $conn, $mac )
03831 {
03832         if ( $conn != null && $mac != null )
03833         {
03834                 if ( getTaskIDByMac( $conn, $mac ) != null ) return true;
03835         }
03836         return false;
03837 }
03838 
03839 function getTaskIDByMac( $conn, $mac, $state=null )
03840 {
03841         if ( $state == null)
03842                 $state = "(taskState = '0' OR taskState = '1')";
03843         else $state = "taskState = '$state'";
03844         if ( $conn != null && $mac != null )
03845         {
03846                 $sql = "select
03847                 taskID
03848                 from hosts
03849                 inner join tasks on ( hosts.hostID = tasks.taskHostID ) where hostMAC = '" . mysql_real_escape_string($mac) . "' and $state";
03850 
03851                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03852                 while( $ar = mysql_fetch_array( $res ) )
03853                 {
03854                         return $ar["taskID"];
03855                 }
03856         }
03857         return null;
03858 }
03859 
03860 /* Deprecated as of version 0.24 */
03861 function getNumberInQueue( $conn, $state )
03862 {
03863         if ( $conn != null && $state != null )
03864         {
03865                 $sql = "select count(*) as cnt from tasks where taskState = '" . mysql_real_escape_string($state) . "' and taskType in ('U', 'D')";
03866                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03867 
03868                 if ( $ar = mysql_fetch_array( $res ) )
03869                 {
03870                         return $ar["cnt"];
03871                 }
03872         }
03873         return null;
03874 }
03875 
03876 /*
03877  * Replaces function getNumber InQueue
03878 * Handles Queue by NFS Server
03879 *
03880 */
03881 function getNumberInQueueByNFSServer( $conn, $state, $nodeid )
03882 {
03883         if ( $conn != null && $state != null && $nodeid != null )
03884         {
03885                 $sql = "SELECT
03886                 COUNT(*) as cnt
03887                 FROM
03888                 tasks
03889                 WHERE
03890                 taskState = '" . mysql_real_escape_string( $state ) . "' and
03891                 taskType in ( 'U', 'D' ) and
03892                 taskNFSMemberID = '" . mysql_real_escape_string( $nodeid ) . "'";
03893                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03894 
03895                 if ( $ar = mysql_fetch_array( $res ) )
03896                 {
03897                         return $ar["cnt"];
03898                 }
03899         }
03900         return null;
03901 }
03902 
03903 
03904 // global queue stuff for
03905 // the dashboard
03906 function getGlobalQueueSize( $conn )
03907 {
03908         if ( $conn != null )
03909         {
03910                 $sql = "SELECT
03911                 SUM(ngmMaxClients) as cnt
03912                 FROM
03913                 nfsGroupMembers
03914                 WHERE
03915                 ngmIsEnabled = '1'";
03916                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
03917                 while( $ar = mysql_fetch_array( $res ) )
03918                 {
03919                         return $ar["cnt"];
03920                 }
03921         }
03922         return null;
03923 }
03924 
03925 
03926 /*
03927  * Replaces function getNumber InQueue
03928 * Handles Queue by NFS Server Group
03929 *
03930 */
03931 function getNumberInQueueByNFSGroup( $conn, $state, $groupid )
03932 {
03933         if ( $conn != null && $state != null && $groupid != null )
03934         {
03935                 $sql = "SELECT
03936                 COUNT(*) as cnt
03937                 FROM
03938                 tasks
03939                 WHERE
03940                 taskState = '" . mysql_real_escape_string( $state ) . "' and
03941                 taskType in ( 'U', 'D' ) and
03942                 taskNFSGroupID = '" . mysql_real_escape_string( $groupid ) . "'";
03943 
03944                 $sql = "SELECT count(*) as cnt from
03945                 ( SELECT taskId as ti FROM tasks WHERE
03946                 taskState = '" . mysql_real_escape_string( $state ) . "' and
03947                 taskType in ( 'U', 'D' ) and
03948                 taskNFSGroupID = '" . mysql_real_escape_string( $groupid ) . "'
03949                 UNION
03950                 select distinct(msID) as ti from tasks,multicastSessionsAssoc
03951                 where tasks.taskID=multicastSessionsAssoc.tID AND
03952                 taskState = '" . mysql_real_escape_string( $state ) . "' and taskType ='C'
03953                 and taskNFSGroupID='" . mysql_real_escape_string( $groupid ) . "'
03954                 ) t1";
03955 
03956 
03957                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03958 
03959                 if ( $ar = mysql_fetch_array( $res ) )
03960                 {
03961                         return $ar["cnt"];
03962                 }
03963         }
03964         return null;
03965 }
03966 
03967 function getNFSGroupIDByTaskID( $conn, $taskID )
03968 {
03969         if ( $conn != null && $taskID != null )
03970         {
03971                 $sql = "SELECT
03972                 taskNFSGroupID
03973                 FROM
03974                 tasks
03975                 WHERE
03976                 taskID = '" . mysql_real_escape_string( $taskID ) . "'";
03977 
03978                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
03979 
03980                 if ( $ar = mysql_fetch_array( $res ) )
03981                 {
03982                         return $ar["taskNFSGroupID"];
03983                 }
03984         }
03985         return null;
03986 }
03987 
03988 function getAllBlamedNodes( $conn, $taskid, $hostid )
03989 {
03990         $arRet = array();
03991 
03992         if ( $conn != null && is_numeric( $taskid ) && is_numeric( $hostid ) )
03993         {
03994                 $sql = "SELECT
03995                 nfNodeID
03996                 FROM
03997                 nfsFailures
03998                 WHERE
03999                 nfTaskID = '$taskid' and
04000                 nfHostID = '$hostid' and
04001                 TIMESTAMP(nfDateTime) BETWEEN TIMESTAMP(DATE_ADD(NOW(), INTERVAL -5 MINUTE)) and TIMESTAMP(NOW())";
04002 
04003                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04004                 while( $ar = mysql_fetch_array( $res ) )
04005                 {
04006                         $node = $ar["nfNodeID"];
04007 
04008                         if ( ! in_array  ( $node, $arRet ) )
04009                                 $arRet[] = $node;
04010                 }
04011         }
04012         return $arRet;
04013 }
04014 
04015 // Returns the node id for all nodes in a given group (id)
04016 function getAllNodeInNFSGroup( $conn, $groupid )
04017 {
04018         $arRet = array();
04019         if ( $conn != null && $groupid != null )
04020         {
04021                 $sql = "SELECT
04022                 ngmID
04023                 FROM
04024                 nfsGroupMembers
04025                 WHERE
04026                 ngmGroupID = '" . $groupid . "' and
04027                 ngmIsEnabled = '1'";
04028                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04029                 while ( $ar = mysql_fetch_array( $res ) )
04030                 {
04031                         $arRet[] = $ar["ngmID"];
04032                 }
04033         }
04034         return $arRet;
04035 }
04036 
04037 function getNodeQueueSize( $conn, $nodeid )
04038 {
04039         if ( $conn != null && $nodeid != null )
04040         {
04041                 $sql = "SELECT
04042                 ngmMaxClients
04043                 FROM
04044                 nfsGroupMembers
04045                 WHERE
04046                 ngmID = '" . $nodeid . "' and
04047                 ngmIsEnabled = '1'";
04048                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04049                 while ( $ar = mysql_fetch_array( $res ) )
04050                 {
04051                         return $ar["ngmMaxClients"];
04052                 }
04053         }
04054         return null;
04055 }
04056 
04057 function getNFSNodeNameById( $conn, $nodeid )
04058 {
04059         if ( $conn != null && $nodeid != null )
04060         {
04061                 $sql = "SELECT
04062                 ngmMemberName
04063                 FROM
04064                 nfsGroupMembers
04065                 WHERE
04066                 ngmID = '" . $nodeid . "' and
04067                 ngmIsEnabled = '1'";
04068                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04069                 while ( $ar = mysql_fetch_array( $res ) )
04070                 {
04071                         return $ar["ngmMemberName"];
04072                 }
04073         }
04074         return null;
04075 }
04076 
04077 function checkIn( $conn, $jobid )
04078 {
04079         if ( $conn != null && $jobid != null )
04080         {
04081                 $sql = "update tasks set taskCheckIn = NOW() where taskID = '" . mysql_real_escape_string( $jobid ) . "'";
04082                 if ( mysql_query( $sql, $conn ) )
04083                         return true;
04084         }
04085         return false;
04086 }
04087 
04088 function isForced( $conn, $jobid )
04089 {
04090         if ( $conn != null && $jobid != null )
04091         {
04092                 #               $sql = "select count(*) as c from tasks where taskID = '" . mysql_real_escape_string( $jobid ) . "' and taskForce = 1";
04093                 $sql = "select count(*) as c from
04094                 ( SELECT taskID from tasks where taskID = '" . mysql_real_escape_string( $jobid ) . "' and taskForce = 1
04095                 UNION
04096                 SELECT taskID from tasks,multicastSessionsAssoc where multicastSessionsAssoc.tID=tasks.taskID AND tasks.taskState='1' AND
04097                 msID in (SELECT msID from tasks, multicastSessionsAssoc where tasks.taskID='" . mysql_real_escape_string( $jobid ) . "' and multicastSessionsAssoc.tID=tasks.taskID)
04098                 ) t1";
04099                 #$sql = "select count(*) as c from tasks where taskID = '" . mysql_real_escape_string( $jobid ) . "' and taskForce = 1";
04100 
04101                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04102                 if ( $ar = mysql_fetch_array( $res ) )
04103                 {
04104                         if ( $ar["c"] != "0" ) return true;
04105 
04106                 }
04107         }
04108         return false;
04109 }
04110 
04111 function getNewStorageStringForImage( $conn, $nodeid )
04112 {
04113         if ( $conn != null && $nodeid != null )
04114         {
04115                 $sql = "SELECT
04116                 ngmHostName,
04117                 ngmRootPath,
04118                 ngmMemberName
04119                 FROM
04120                 nfsGroupMembers
04121                 WHERE
04122                 ngmID = '" . mysql_real_escape_string( $nodeid ) . "' and
04123                 ngmIsEnabled = '1'";
04124                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04125                 if ( $ar = mysql_fetch_array( $res ) )
04126                 {
04127                         return  sloppyNameLookup( $ar["ngmHostName"] ) . "@" . sloppyNameLookup($ar["ngmHostName"]) . ":" . $ar["ngmRootPath"] . "@" . base64_encode( $ar["ngmMemberName"] );
04128                 }
04129         }
04130         return false;
04131 }
04132 
04133 function doesStorageGroupExist( $conn, $name, $id=-1 )
04134 {
04135         if ( $conn != null && $name != null && is_numeric( $id ) )
04136         {
04137                 $sql = "SELECT COUNT(*) as cnt FROM nfsGroups WHERE ngName = '" . mysql_real_escape_string( $name ) . "' and ngID <> $id";
04138                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04139                 while( $ar = mysql_fetch_array( $res ) )
04140                 {
04141                         return ( $ar["cnt"] > 0 );
04142                 }
04143         }
04144         // play it safe
04145         return true;
04146 }
04147 
04148 function doesStorageNodeExist( $conn, $name, $id=-1 )
04149 {
04150         if ( $conn != null && $name != null && is_numeric( $id ) )
04151         {
04152                 $sql = "SELECT COUNT(*) as cnt FROM nfsGroupMembers WHERE ngmMemberName = '" . mysql_real_escape_string( $name ) . "' and ngmID <> $id";
04153                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04154                 while( $ar = mysql_fetch_array( $res ) )
04155                 {
04156                         return ( $ar["cnt"] > 0 );
04157                 }
04158         }
04159         // play it safe
04160         return true;
04161 }
04162 
04163 function getNFSGroupDropDown( $conn, $name="storagegroup", $selected=null )
04164 {
04165         if ( $conn != null )
04166         {
04167                 $sql = "select * from nfsGroups order by ngName";
04168                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04169                 $buffer = "<select name=\"$name\" size=\"1\">\n";
04170                 $buffer .= "<option value=\"-1\" label=\"Select One\">"._("Select One")."</option>\n";
04171                 while( $ar = mysql_fetch_array( $res ) )
04172                 {
04173                         $sel = "";
04174                         if ( $selected == $ar["ngID"] )
04175                                 $sel = "selected=\"selected\"";
04176                         $buffer .= "<option value=\"" . $ar["ngID"] . "\" label=\"" . $ar["ngName"] . "\" $sel>" . $ar["ngName"] . "</option>\n";
04177                 }
04178                 $buffer .= "</select>\n";
04179                 return $buffer;
04180         }
04181         return null;
04182 }
04183 
04184 function getStorageRootByGroupID( $conn, $groupid )
04185 {
04186         if ( $conn != null && $groupid != null && is_numeric( $groupid ) )
04187         {
04188                 $sql = "SELECT
04189                 ngmRootPath
04190                 FROM
04191                 (SELECT * FROM nfsGroups WHERE ngID = '$groupid') nfsGroups
04192                 INNER JOIN nfsGroupMembers on ( nfsGroups.ngID = nfsGroupMembers.ngmGroupID )
04193                 WHERE
04194                 ngmIsMasterNode = '1' and
04195                 ngmIsEnabled = '1'";
04196 
04197                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04198                 if ( mysql_num_rows( $res ) == 1 )
04199                 {
04200                         while( $ar = mysql_fetch_array( $res ) )
04201                                 return $ar["ngmRootPath"];
04202                 }
04203         }
04204         return null;
04205 }
04206 
04207 function doImage( $conn, $jobid, $blUpdateNFS=false, $nodeid=null )
04208 {
04209         if ( $conn != null && $jobid != null )
04210         {
04211                 $set = "";
04212                 if ( $blUpdateNFS ) {
04213                         $set = "taskNFSMemberID = '" . mysql_real_escape_string( $nodeid ) . "', ";
04214                 }
04215 
04216                 $sql = "UPDATE tasks SET $set taskState = '1' WHERE taskID = '" . mysql_real_escape_string($jobid) . "'";
04217                 if ( mysql_query( $sql, $conn ) )
04218                         return true;
04219                 else
04220                 {
04221                         die( mysql_error() );
04222                 }
04223         }
04224         return false;
04225 }
04226 
04227 function isRunning ($conn, $jobid)
04228 {
04229         if ( $conn != null && $jobid != null )
04230         {
04231                 $sql = "SELECT multicastSessions.msPercent FROM multicastSessions,multicastSessionsAssoc
04232                 WHERE multicastSessionsAssoc.tID = '" . mysql_real_escape_string($jobid) . "'
04233                 AND multicastSessionsAssoc.msID = multicastSessions.msID AND multicastSessions.msPercent<>0;";
04234                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04235                 if ( $ar = mysql_fetch_array( $res ) )
04236                         return true;
04237                 //  }   else {
04238                 //              die( mysql_error());
04239                 //      }
04240         }
04241         return false;
04242 }
04243 
04244 function getTotalClusteredQueueSize( $conn, $groupid )
04245 {
04246         if ( $conn != null && $groupid != null )
04247         {
04248                 #               $sql = "SELECT
04249                 #                               SUM(ngmMaxClients) as max
04250                 #                       FROM
04251                 #                               nfsGroupMembers
04252                 #                       WHERE
04253                 #                               ngmGroupID = '" . mysql_real_escape_string( $groupid ) . "' and
04254                 #                               ngmIsEnabled = '1'";
04255                 $sql = "SELECT settingValue FROM globalSettings WHERE settingKey='FOG_QUEUESIZE'";
04256                 $res = mysql_query( $sql, $conn ) or die( mysql_error() );
04257 
04258                 while( $ar = mysql_fetch_array( $res ) )
04259                 {
04260                         return $ar["settingValue"];
04261                 }
04262         }
04263         return null;
04264 }
04265 
04266 
04267 function getNumberInFrontOfMe( $conn, $jobid, $groupid=-1 )
04268 {
04269         if ( $conn != null && $jobid != null )
04270         {
04271                 $where = "";
04272                 if ( $groupid != -1 )
04273                 {
04274                         $where = " taskNFSGroupID = '" . mysql_real_escape_string( $groupid ) . "' and ";
04275                 }
04276                 #$sql = "SELECT COUNT(*) AS c FROM tasks WHERE $where taskState = '0' AND taskID < " . mysql_real_escape_string($jobid) .
04277                 #       " AND (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(taskCheckIn)) < " . getSetting( $conn, "FOG_CHECKIN_TIMEOUT" );
04278 
04279                 $sql = "SELECT COUNT(*)-1 as c FROM (SELECT IFNULL(multicastSessions.msName, taskID) as msName FROM tasks
04280                 LEFT JOIN multicastSessionsAssoc ON (tasks.taskID=multicastSessionsAssoc.tID)
04281                 LEFT JOIN multicastSessions ON (multicastSessionsAssoc.msID=multicastSessions.msID)
04282                 WHERE $where taskState = '0' AND taskID<=". mysql_real_escape_string($jobid) ." GROUP BY msName) t1;";
04283 
04284                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04285 
04286                 if ( $ar = mysql_fetch_array( $res ) )
04287                         return $ar["c"];
04288         }
04289         return null;
04290 }
04291 
04292 function getHostID( $conn, $mac )
04293 {
04294         if ( $conn != null && $mac != null )
04295         {
04296                 $sql = "select hostID from hosts where hostMAC = '" . mysql_real_escape_string($mac) . "'";
04297                 $res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") );
04298                 while( $ar = mysql_fetch_array( $res ) )
04299                 {
04300                         return $ar["hostID"];
04301                 }
04302         }
04303         return null;
04304 }
04305 
04306 function checkOut( $conn, $jobid , $msg=null, $hostid=0 )
04307 {
04308         if ( $conn != null && $jobid != null )
04309         {
04310                 if ( $msg != null ) {
04311                         $sql = "update tasks set taskState = '3' where taskID = '" . mysql_real_escape_string($jobid). "'";
04312                         cancelSnapinsForHost( $conn, $hostid );
04313                 }
04314                 else
04315                         $sql = "update tasks set taskState = '2' where taskID = '" . mysql_real_escape_string($jobid). "'";
04316                 if ( mysql_query( $sql, $conn ) )
04317                         return true;
04318         }
04319         return false;
04320 }
04321 
04331 function getLanguageName( $file )
04332 {
04333         switch ($file) {
04334                 case "en_US.UTF-8":
04335                         return "English";
04336                         break;
04337                 case "es_ES.UTF-8":
04338                         return "Castellano";
04339                         break;
04340                 case "eu_ES.UTF-8":
04341                         return "Euskara";
04342                         break;
04343                 case "zh_CN.UTF-8":
04344                         return "Chinese";
04345                         break;
04346                 case "it_IT.UTF-8/":
04347                         return "Italian";
04348                         break;
04349                 default:
04350                         return "unidentified";
04351         }
04352 }
04353 
04369 function doPartitionBackup( $conn,$ftp, $nodeip, $dest, $fileName, $linkTo=null){
04370 
04371         if(!ftp_chdir($ftp,$dest) || ftp_size($ftp, $dest."/".$fileName)!=-1){
04372                 if(!ftp_chdir($ftp,$dest."_bak"))
04373                 {
04374                         @ftp_mkdir($ftp,$dest."_bak");
04375                         @ftp_chmod($ftp,0777,$dest."_bak");
04376                         @ftp_chdir($ftp,$dest."_bak");
04377                         $arFilesBak=ftp_nlist($ftp,$dest);
04378                         for( $i = 0; $i < count( $arFilesBak ); $i++ )
04379                         {
04380                                 if ( $arFilesBak[$i] !=$dest."/".$fileName){
04381                                         $password=getSetting( $conn, "FOG_TFTP_FTP_PASSWORD" );
04382                                         $command="echo $password > ssh fog@".$nodeip.";cd ".$dest."_bak;ln ".$arFilesBak[$i]." .";
04383                                         @shell_exec($command);
04384                                 }
04385                         }
04386                 }
04387                 else{
04388                         @ftp_delete( $ftp, $dest."_bak/".$fileName );
04389                 }
04390                 @ftp_rename($ftp,$dest."/".$fileName,$dest."_bak/".$fileName);
04391                 if($linkTo!=null){
04392                         $command="echo $password > ssh fog@".$nodeip.";cd ".$dest.";ln ".$linkTo."/".$fileName." .";
04393                         @shell_exec($command);
04394                 }
04395 
04396         }
04397 }
 Todo Clases Archivos Funciones Variables Enumeraciones