"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/packages/web/lib/pages/snapinmanagementpage.class.php" (13 Sep 2020, 58831 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


The requested HTML page contains a <FORM> tag that is unusable on "Fossies" in "automatic" (rendered) mode so that page is shown as HTML source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "snapinmanagementpage.class.php": 1.5.8_vs_1.5.9.

    1 <?php
    2 /**
    3  * Snapin management page
    4  *
    5  * PHP version 5
    6  *
    7  * @category SnapinManagementPage
    8  * @package  FOGProject
    9  * @author   Tom Elliott <tommygunsster@gmail.com>
   10  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   11  * @link     https://fogproject.org
   12  */
   13 /**
   14  * Snapin management page
   15  *
   16  * @category SnapinManagementPage
   17  * @package  FOGProject
   18  * @author   Tom Elliott <tommygunsster@gmail.com>
   19  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   20  * @link     https://fogproject.org
   21  */
   22 class SnapinManagementPage extends FOGPage
   23 {
   24     /**
   25      * Arg types for snapin template
   26      *
   27      * @var array
   28      */
   29     private static $_argTypes = array(
   30         'MSI' => array('msiexec.exe','/i','/quiet'),
   31         'Batch Script' => array('cmd.exe','/c'),
   32         'Bash Script' => array('/bin/bash'),
   33         'VB Script' => array('cscript.exe'),
   34         'Powershell (default)' => array(
   35             'powershell.exe',
   36             '-ExecutionPolicy Bypass -NoProfile -File'
   37         ),
   38         'Powershell x64' => array(
   39             '&quot;%SYSTEMROOT%\\sysnative\\windowspowershell'
   40             . '\\v1.0\\powershell.exe',
   41             '-ExecutionPolicy Bypass -NoProfile -File'
   42         ),
   43         'Mono' => array('mono'),
   44     );
   45     /**
   46      * Template for non-pack.
   47      *
   48      * @var string
   49      */
   50     private static $_template1;
   51     /**
   52      * Template for pack.
   53      *
   54      * @var string
   55      */
   56     private static $_template2;
   57     /**
   58      * The node this page operates off of.
   59      *
   60      * @var string
   61      */
   62     public $node = 'snapin';
   63     /**
   64      * Initializes the snapin page class
   65      *
   66      * @param string $name the name to pass
   67      *
   68      * @return void
   69      */
   70     public function __construct($name = '')
   71     {
   72         /**
   73          * The real name not using our name passer.
   74          */
   75         $this->name = 'Snapin Management';
   76         /**
   77          * Pull in the FOG Page class items.
   78          */
   79         parent::__construct($name);
   80         /**
   81          * Generate our snapin arg templates.
   82          */
   83         /**
   84          * Start a new buffer (last one ended anyway)
   85          * to create our template non-pack.
   86          */
   87         ob_start();
   88         printf(
   89             '<select name="argTypes" id="argTypes">'
   90             . '<option value="">- %s -</option>',
   91             _('Please select an option')
   92         );
   93         foreach (self::$_argTypes as $type => &$cmd) {
   94             printf(
   95                 '<option value="%s" rwargs="%s" args="%s">%s</option>',
   96                 $cmd[0],
   97                 $cmd[1],
   98                 $cmd[2],
   99                 $type
  100             );
  101             unset($cmd);
  102         }
  103         echo '</select>';
  104         self::$_template1 = ob_get_clean();
  105         /**
  106          * Store the pack based template.
  107          */
  108         self::$_template2 = $this->_maker();
  109         /**
  110          * Get our nicer names.
  111          */
  112         global $id;
  113         global $sub;
  114         if ($id) {
  115             /**
  116              * The other sub menu items.
  117              */
  118             $this->subMenu = array(
  119                 "$this->linkformat#snap-gen" => self::$foglang['General'],
  120                 "$this->linkformat#snap-storage" => sprintf(
  121                     '%s %s',
  122                     self::$foglang['Storage'],
  123                     self::$foglang['Group']
  124                 ),
  125                 $this->membership => self::$foglang['Membership'],
  126                 $this->delformat => self::$foglang['Delete'],
  127             );
  128             /**
  129              * The notes for this item.
  130              */
  131             $this->notes = array(
  132                 self::$foglang['Snapin'] => $this->obj->get('name'),
  133                 self::$foglang['File'] => $this->obj->get('file'),
  134                 _('Filesize') => self::formatByteSize($this->obj->get('size')),
  135             );
  136         }
  137         /**
  138          * Allow custom hooks/changes to: Submenu data via.
  139          *
  140          * Menu, submenu, id, notes, the main object,
  141          * linkformat, delformat, and membership information.
  142          */
  143         self::$HookManager->processEvent(
  144             'SUB_MENULINK_DATA',
  145             array(
  146                 'menu' => &$this->menu,
  147                 'submenu' => &$this->subMenu,
  148                 'id' => &$this->id,
  149                 'notes' => &$this->notes,
  150                 'object' => &$this->obj,
  151                 'linkformat' => &$this->linkformat,
  152                 'delformat' => &$this->delformat,
  153                 'membership' => &$this->membership
  154             )
  155         );
  156         /**
  157          * The header data for list/search.
  158          */
  159         $this->headerData = array(
  160             '',
  161             '',
  162             '<label for="toggler">'
  163             . '<input type="checkbox" name="toggle-checkbox" '
  164             . 'class="toggle-checkboxAction" id="toggler"/>'
  165             . '</label>',
  166             _('Snapin Name'),
  167             _('Is Pack'),
  168             _('Storage Group'),
  169         );
  170         /**
  171          * The template for the list/search elements.
  172          */
  173         $this->templates = array(
  174             '${protected}',
  175             '${enabled}',
  176             '<label for="toggler1">'
  177             . '<input type="checkbox" name="snapin[]" '
  178             . 'value="${id}" class="toggle-action" id="'
  179             . 'toggler1"/></label>',
  180             '<a href="?node='
  181             . $this->node
  182             . '&sub=edit&id=${id}" '
  183             . 'data-toggle="tooltip" data-placement="right" '
  184             . 'title="'
  185             . _('Edit')
  186             . ': ${name}">${name} - ${id}</a>',
  187             '${packtype}',
  188             '${storageGroup}',
  189         );
  190         /**
  191          * The attributes for the table items.
  192          */
  193         $this->attributes = array(
  194             array(
  195                 'class' => 'filter-false',
  196                 'width' => 5
  197             ),
  198             array(
  199                 'class' => 'filter-false',
  200                 'width' => 5
  201             ),
  202             array(
  203                 'class' => 'filter-false',
  204                 'width' => 16
  205             ),
  206             array(),
  207             array(
  208                 'width' => 50
  209             ),
  210             array()
  211         );
  212         /**
  213          * Lamda function to return data either by list or search.
  214          *
  215          * @param object $Snapin the object to use.
  216          *
  217          * @return void
  218          */
  219         self::$returnData = function (&$Snapin) {
  220             /**
  221              * Stores the items in a nicer name.
  222              */
  223             /**
  224              * The id.
  225              */
  226             $id = $Snapin->id;
  227             /**
  228              * The name.
  229              */
  230             $name = $Snapin->name;
  231             /**
  232              * The description.
  233              */
  234             $description = $Snapin->description;
  235             /**
  236              * The file name.
  237              */
  238             $file = $Snapin->file;
  239             /**
  240              * Tell if packtype is true or not.
  241              */
  242             if ($Snapin->packtype <= 0) {
  243                 $packtype = _('No');
  244             } else {
  245                 $packtype = _('Yes');
  246             }
  247             /**
  248              * The storage group name.
  249              */
  250             $storageGroup = $Snapin->storagegroupname;
  251             /**
  252              * If the snapin is not protected show
  253              * the unlocked symbol and title of not protected
  254              * otherwise set as is protected.
  255              */
  256             if ($Snapin->protected < 1) {
  257                 $protected = '<i class="fa fa-unlock fa-1x icon hand" '
  258                     . 'data-toggle="tooltip" data-placement="right" '
  259                     . 'title="'
  260                     . _('Not protected')
  261                     . '"></i>';
  262             } else {
  263                 $protected = '<i class="fa fa-lock fa-1x icon hand" '
  264                     . 'data-toggle="tooltip" data-placement="right" '
  265                     . 'title="'
  266                     . _('Protected')
  267                     . '"></i>';
  268             }
  269             /**
  270              * If the snapin is enabled or not.
  271              */
  272             if ($Snapin->isEnabled) {
  273                 $enabled = '<i class="fa fa-check-circle green" '
  274                     . 'title="'
  275                     . _('Enabled')
  276                     . '" data-toggle="tooltip" data-placement="top">'
  277                     . '</i>';
  278             } else {
  279                 $enabled = '<i class="fa fa-times-circle red" '
  280                     . 'title="'
  281                     . _('Disabled')
  282                     . '" data-toggle="tooltip" data-placement="top">'
  283                     . '</i>';
  284             }
  285             /**
  286              * Store the data.
  287              */
  288             $this->data[] = array(
  289                 'id' => $id,
  290                 'name' => $name,
  291                 'description' => $description,
  292                 'file' => $file,
  293                 'packtype' => $packtype,
  294                 'storageGroup' => $storageGroup,
  295                 'protected' => $protected,
  296                 'enabled' => $enabled
  297             );
  298             /**
  299              * Cleanup.
  300              */
  301             unset(
  302                 $id,
  303                 $name,
  304                 $description,
  305                 $file,
  306                 $packtype,
  307                 $Snapin
  308             );
  309         };
  310     }
  311     /**
  312      * Generates the selector for Snapin Packs.
  313      *
  314      * @return void
  315      */
  316     private function _maker()
  317     {
  318         $args = array(
  319             'MSI' => array(
  320                 'msiexec.exe',
  321                 '/i &quot;[FOG_SNAPIN_PATH]\\MyMSI.msi&quot;'
  322             ),
  323             'MSI + MST' => array(
  324                 'msiexec.exe',
  325                 '/i &quot;[FOG_SNAPIN_PATH]\\MyMST.mst&quot;'
  326             ),
  327             'Batch Script' => array(
  328                 'cmd.exe',
  329                 '/c &quot;[FOG_SNAPIN_PATH]\\MyScript.bat&quot;'
  330             ),
  331             'Bash Script' => array(
  332                 '/bin/bash',
  333                 '&quot;[FOG_SNAPIN_PATH]/MyScript.sh&quot;'
  334             ),
  335             'VB Script' => array(
  336                 'cscript.exe',
  337                 '&quot;[FOG_SNAPIN_PATH]\\MyScript.vbs&quot;'
  338             ),
  339             'PowerShell Script' => array(
  340                 'powershell.exe',
  341                 '-ExecutionPolicy Bypass -File &quot;'
  342                 .'[FOG_SNAPIN_PATH]\\MyScript.ps1&quot;'
  343             ),
  344             'PowerShell x64 Script' => array(
  345                 '&quot;%SYSTEMROOT%\\sysnative\\windowspowershell'
  346                 . '\\v1.0\\powershell.exe&quot;',
  347                 '-ExecutionPolicy Bypass -File &quot;'
  348                 .'[FOG_SNAPIN_PATH]\\MyScript.ps1&quot;'
  349             ),
  350             'EXE' => array(
  351                 '[FOG_SNAPIN_PATH]\\MyFile.exe'
  352             ),
  353             'Mono' => array(
  354                 'mono',
  355                 '&quot;[FOG_SNAPIN_PATH]/MyFile.exe&quot;'
  356             ),
  357         );
  358         ob_start();
  359         printf(
  360             '<select id="packTypes"><option value="">- %s -</option>',
  361             _('Please select an option')
  362         );
  363         foreach ($args as $type => &$cmd) {
  364             printf(
  365                 '<option file="%s" args="%s">%s</option>',
  366                 $cmd[0],
  367                 (
  368                     isset($cmd[1]) ?
  369                     $cmd[1] :
  370                     ''
  371                 ),
  372                 $type
  373             );
  374             unset($cmd);
  375         }
  376         echo '</select>';
  377         return ob_get_clean();
  378     }
  379     /**
  380      * The form to display when adding a new snapin
  381      * definition.
  382      *
  383      * @return void
  384      */
  385     public function add()
  386     {
  387         unset(
  388             $this->data,
  389             $this->form,
  390             $this->headerData,
  391             $this->attributes,
  392             $this->templates
  393         );
  394         /**
  395          * Title of inital/general element.
  396          */
  397         $this->title = _('New Snapin');
  398         /**
  399          * The table attributes.
  400          */
  401         $this->attributes = array(
  402             array('class' => 'col-xs-4'),
  403             array('class' => 'col-xs-8 form-group'),
  404         );
  405         /**
  406          * The table template.
  407          */
  408         $this->templates = array(
  409             '${field}',
  410             '${input}',
  411         );
  412         $storagegroup = filter_input(INPUT_POST, 'storagegroup');
  413         $snapinfileexist = basename(
  414             filter_input(INPUT_POST, 'snapinfileexist')
  415         );
  416         $name = filter_input(INPUT_POST, 'name');
  417         $desc = filter_input(INPUT_POST, 'description');
  418         $packtype = (int)filter_input(INPUT_POST, 'packtype');
  419         $rw = filter_input(INPUT_POST, 'rw');
  420         $rwa = filter_input(INPUT_POST, 'rwa');
  421         $args = filter_input(INPUT_POST, 'args');
  422         $timeout = filter_input(INPUT_POST, 'timeout');
  423         /**
  424          * Set the storage group to pre-select.
  425          */
  426         if ($storagegroup > 0) {
  427             $sgID = $storagegroup;
  428         } else {
  429             $sgID = @min(self::getSubObjectIDs('StorageGroup'));
  430         }
  431         /**
  432          * Set our storage group object.
  433          */
  434         $StorageGroup = new StorageGroup($sgID);
  435         $StorageGroups = self::getClass('StorageGroupManager')
  436             ->buildSelectBox(
  437                 $sgID,
  438                 '',
  439                 'id'
  440             );
  441         /**
  442          * We'll get the files associated with the selected
  443          * group.
  444          */
  445         /**
  446          * Reset our "selected" item for our option list.
  447          */
  448         self::$selected = '';
  449         /**
  450          * If the snapin file exists, set selected item
  451          * to this value.
  452          */
  453         self::$selected = $snapinfileexist;
  454         /**
  455          * Initialize the filelist.
  456          */
  457         $filelist = array();
  458         /**
  459          * Get the master storage node.
  460          */
  461         $StorageNode = $StorageGroup->getMasterStorageNode();
  462         /**
  463          * Get the files on this node.
  464          */
  465         $filelist = $StorageNode->get('snapinfiles');
  466         /**
  467          * Sort our files nicely.
  468          *
  469          * Naturally sort as snapins in form:
  470          * 03x, 01x, 02x, or only numerically named
  471          * should present in human natural order.
  472          */
  473         natcasesort($filelist);
  474         /**
  475          * Filter the list.
  476          */
  477         $filelist = array_values(
  478             array_unique(
  479                 array_filter($filelist)
  480             )
  481         );
  482         /**
  483          * Buffer the select box.
  484          */
  485         ob_start();
  486         /**
  487          * Build our select box based on this file list.
  488          */
  489         array_map(self::$buildSelectBox, $filelist);
  490         /**
  491          * Create our listing and store in a variable.
  492          */
  493         $selectFiles = '<select class='
  494             . '"snapinfileexist-input cmdlet3 form-control" '
  495             . 'name="snapinfileexist" id="snapinfileexist">'
  496             . '<option value="">- '
  497             . _('Please select an option')
  498             . ' -</option>'
  499             . ob_get_clean()
  500             . '</select>';
  501         /**
  502          * Setup the fields to be used to display.
  503          */
  504         $fields = array(
  505             '<label for="name">'
  506             . _('Snapin Name')
  507             . '</label>' => '<div class="input-group">'
  508             . '<input type="text" name="name" id="name" value="'
  509             . $name
  510             . '" class="snapinname-input form-control"/>'
  511             . '</div>',
  512             '<label for="desc">'
  513             . _('Snapin Description')
  514             . '</label>' => '<div class="input-group">'
  515             . '<textarea name="description" id="desc" class='
  516             . '"form-control snapindescription-input">'
  517             . $desc
  518             . '</textarea>'
  519             . '</div>',
  520             '<label for="storagegroup">'
  521             . _('Storage Group')
  522             . '</label>' => $StorageGroups,
  523             '<label for="snapinpack">'
  524             . _('Snapin Type')
  525             . '</label>' => '<select class="snapinpack-input form-control" '
  526             . 'name="packtype" id="snapinpack">'
  527             . '<option value="0"'
  528             . (
  529                 $packtype == 0 ?
  530                 ' selected' :
  531                 ''
  532             )
  533             . '>'
  534             . _('Normal Snapin')
  535             . '</option>'
  536             . '<option value="1"'
  537             . (
  538                 $packtype > 0 ?
  539                 ' selected' :
  540                 ''
  541             )
  542             . '>'
  543             . _('Snapin Pack')
  544             . '</option>'
  545             . '</select>',
  546             '<span class="hiddeninitially packnotemplate">'
  547             . '<label for="argTypes">'
  548             . _('Snapin Template')
  549             . '</label>'
  550             . '</span>'
  551             . '<span class="hiddeninitially packtemplate">'
  552             . '<label for="packTypes">'
  553             . _('Snapin Pack Template')
  554             . '</label>'
  555             . '</span>' => '<span class="hiddeninitially packnotemplate">'
  556             . self::$_template1
  557             . '</span>'
  558             . '<span class="hiddeninitially packtemplate">'
  559             . self::$_template2
  560             . '</span>',
  561             '<span class="hiddeninitially packnochangerw">'
  562             . '<label for="snaprw">'
  563             . _('Snapin Run With')
  564             . '</label>'
  565             . '</span>'
  566             . '<span class="hiddeninitially packchangerw">'
  567             . '<label for="snaprw">'
  568             . _('Snapin Pack File')
  569             . '</label>'
  570             . '</span>' => '<div class="input-group">'
  571             . '<input type="text" class="snapinrw-input cmdlet1 form-control" '
  572             . 'name="rw" id="snaprw" value="'
  573             . $rw
  574             . '"/>'
  575             . '</div>',
  576             '<span class="hiddeninitially packnochangerwa">'
  577             . '<label for="snaprwa">'
  578             . _('Snapin Run With Argument')
  579             . '</label>'
  580             . '</span>'
  581             . '<span class="hiddeninitially packchangerwa">'
  582             . '<label for="snaprwa">'
  583             . _('Snapin Pack Arguments')
  584             . '</label>'
  585             . '</span>' => '<div class="input-group">'
  586             . '<input type="text" class="snapinrwa-input cmdlet2 form-control" '
  587             . 'name="rwa" id="snaprwa" value="'
  588             . $rwa
  589             . '"/>'
  590             . '</div>',
  591             '<label for="snapinfile">'
  592             . _('Snapin File')
  593             . '<br/>'
  594             . _('Max Size')
  595             . ': '
  596             . ini_get('post_max_size')
  597             . '</label>' => '<div class="input-group">'
  598             . '<label class="input-group-btn">'
  599             . '<span class="btn btn-info">'
  600             . _('Browse')
  601             . '<input type="file" class="hidden cmdlet3" name="snapin" '
  602             . 'id="snapinfile"/>'
  603             . '</span>'
  604             . '</label>'
  605             . '<input type="text" class="form-control filedisp cmdlet3" readonly/>'
  606             . '</div>',
  607             (
  608                 count($filelist) > 0 ?
  609                 '<label for="snapinfileexist">'
  610                 . _('Snapin File (exists)')
  611                 . '</label>' :
  612                 ''
  613             ) => (
  614                 count($filelist) > 0 ?
  615                 $selectFiles :
  616                 ''
  617             ),
  618             '<span class="hiddeninitially packhide">'
  619             . '<label for="args">'
  620             . _('Snapin Arguments')
  621             . '</label>'
  622             . '</span>' => '<span class="hiddeninitially packhide">'
  623             . '<div class="input-group">'
  624             . '<input type="text" name="args" id="args" class='
  625             . '"snapinargs-input cmdlet4 form-control" value="'
  626             . $args
  627             . '"/>'
  628             . '</div>'
  629             . '</span>',
  630             '<label for="isen">'
  631             . _('Snapin Enabled')
  632             . '</label>' => '<input type="checkbox" name="isEnabled" '
  633             . 'class="snapinenabled-input" id="isen" checked/>',
  634             '<label for="isHidden">'
  635             . _('Snapin Arguments Hidden')
  636             . '</label>' => '<input type="checkbox" name="isHidden" '
  637             . 'class="snapinhidden-input" id="isHidden"/>',
  638             '<label for="timeout">'
  639             . _('Snapin Timeout (seconds)')
  640             . '</label>' => '<div class="input-group">'
  641             . '<input type="number" class='
  642             . '"snapintimeout-input form-control" name="timeout" '
  643             . 'id="timeout" value="'
  644             . $timeout
  645             . '"/>'
  646             . '</div>',
  647             '<label for="toRep">'
  648             . _('Replicate?')
  649             . '</label>' => '<input type="checkbox" name="toReplicate" '
  650             . 'class="snapinreplicate-input" id="toRep" checked/>',
  651             '<label for="reboot">'
  652             . _('Reboot after install')
  653             . '</label>' => '<input type="radio" name="action" '
  654             . 'class="snapinreboot-input action" id="reboot" value="reboot" '
  655             . 'checked/>',
  656             '<label for="shutdown">'
  657             . _('Shutdown after install')
  658             . '</label>' => '<input type="radio" name="action" '
  659             . 'class="snapinshutdown-input action" id="shutdown" value="shutdown"/>',
  660             '<label for="cmdletin">'
  661             . _('Snapin Command')
  662             . '<br/>'
  663             . _('read-only')
  664             . '</label>' => '<div class="input-group">'
  665             . '<textarea class="form-control snapincmd" name="snapincmd" '
  666             . 'id="cmdletin" readonly></textarea>',
  667             '<label for="add">'
  668             . _('Create New Snapin')
  669             . '</label>' => '<button type="submit" name="add" id="add" '
  670             . 'class="btn btn-info btn-block">'
  671             . _('Add')
  672             . '</button>'
  673         );
  674         array_walk($fields, $this->fieldsToData);
  675         self::$HookManager
  676             ->processEvent(
  677                 'SNAPIN_ADD',
  678                 array(
  679                     'headerData' => &$this->headerData,
  680                     'data' => &$this->data,
  681                     'templates' => &$this->templates,
  682                     'attributes' => &$this->attributes
  683                 )
  684             );
  685         echo '<div class="col-xs-9">';
  686         echo '<form class="form-horizontal" method="post" action="'
  687             . $this->formAction
  688             . '" enctype="multipart/form-data">';
  689         $this->indexDivDisplay();
  690         echo '</form>';
  691         echo '</div>';
  692         unset(
  693             $fields,
  694             $this->data,
  695             $this->form,
  696             $this->headerData,
  697             $this->attributes,
  698             $this->templates
  699         );
  700     }
  701     /**
  702      * Actually sibmit the creation of the snapin.
  703      *
  704      * @return void
  705      */
  706     public function addPost()
  707     {
  708         self::$HookManager->processEvent('SNAPIN_ADD_POST');
  709         $name = filter_input(INPUT_POST, 'name');
  710         $desc = filter_input(INPUT_POST, 'description');
  711         $packtype = filter_input(INPUT_POST, 'packtype');
  712         $runWith = filter_input(INPUT_POST, 'rw');
  713         $runWithArgs = filter_input(INPUT_POST, 'rwa');
  714         $storagegroup = (int)filter_input(INPUT_POST, 'storagegroup');
  715         $snapinfile = basename(
  716             filter_input(INPUT_POST, 'snapinfileexist')
  717         );
  718         $uploadfile = basename(
  719             $_FILES['snapin']['name']
  720         );
  721         if ($uploadfile) {
  722             $snapinfile = $uploadfile;
  723         }
  724         $isEnabled = (int)isset($_POST['isEnabled']);
  725         $toReplicate = (int)isset($_POST['toReplicate']);
  726         $hide = (int)isset($_POST['isHidden']);
  727         $timeout = (int)filter_input(INPUT_POST, 'timeout');
  728         $action = filter_input(INPUT_POST, 'action');
  729         $args = filter_input(INPUT_POST, 'args');
  730         try {
  731             $phpPostMaxSize = ini_get('post_max_size');
  732             switch (strtolower(substr($phpPostMaxSize, -1))) {
  733                 case 'g':
  734                     $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
  735                     // no break
  736                 case 'm':
  737                     $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
  738                     // no break
  739                 case 'k':
  740                     $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
  741             }
  742             if (isset($_SERVER['CONTENT_LENGTH']) && $phpPostMaxSize > 0 &&
  743                     $_SERVER['CONTENT_LENGTH'] > $phpPostMaxSize) {
  744                 throw new Exception(
  745                     _('Snapin file is too big, increase post_max_size in php.ini.')
  746                 );
  747             }
  748             if (empty($_POST) && isset($_SERVER['CONTENT_LENGTH']) &&
  749                     $_SERVER['CONTENT_LENGTH'] > 0) {
  750                 throw new Exception(
  751                     _('$_POST variable is empty, check apache error log.')
  752                 );
  753             }
  754             if (!$name) {
  755                 throw new Exception(
  756                     _('A snapin name is required!')
  757                 );
  758             }
  759             if (self::getClass('SnapinManager')->exists($name)) {
  760                 throw new Exception(
  761                     _('A snapin already exists with this name!')
  762                 );
  763             }
  764             if (!$snapinfile) {
  765                 throw new Exception(
  766                     sprintf(
  767                         '%s, %s, %s!',
  768                         _('A file'),
  769                         _('either already selected or uploaded'),
  770                         _('must be specified')
  771                     )
  772                 );
  773             }
  774             if (preg_match('#ssl#i', $snapinfile)) {
  775                 throw new Exception(
  776                     sprintf(
  777                         '%s, %s.',
  778                         _('Please choose a different name'),
  779                         _('this one is reserved for FOG')
  780                     )
  781                 );
  782             }
  783             $snapinfile = preg_replace('/[^-\w\.]+/', '_', $snapinfile);
  784             $StorageGroup = new StorageGroup($storagegroup);
  785             $StorageNode = $StorageGroup->getMasterStorageNode();
  786             if (!$snapinfile && $_FILES['snapin']['error'] > 0) {
  787                 throw new UploadException($_FILES['snapin']['error']);
  788             }
  789             $src = sprintf(
  790                 '%s/%s',
  791                 dirname($_FILES['snapin']['tmp_name']),
  792                 basename($_FILES['snapin']['tmp_name'])
  793             );
  794             $dest = sprintf(
  795                 '/%s/%s',
  796                 trim(
  797                     $StorageNode->get('snapinpath'),
  798                     '/'
  799                 ),
  800                 $snapinfile
  801             );
  802             set_time_limit(0);
  803             $hash = '';
  804             $size = 0;
  805             if ($uploadfile && file_exists($src)) {
  806                 $hash = hash_file('sha512', $src);
  807                 $size = self::getFilesize($src);
  808                 self::$FOGFTP
  809                     ->set('host', $StorageNode->get('ip'))
  810                     ->set('username', $StorageNode->get('user'))
  811                     ->set('password', $StorageNode->get('pass'));
  812                 if (!self::$FOGFTP->connect()) {
  813                     throw new Exception(
  814                         sprintf(
  815                             '%s: %s: %s.',
  816                             _('Storage Node'),
  817                             $StorageNode->get('ip'),
  818                             _('FTP Connection has failed')
  819                         )
  820                     );
  821                 }
  822                 if (!self::$FOGFTP->chdir($StorageNode->get('snapinpath'))) {
  823                     if (!self::$FOGFTP->mkdir($StorageNode->get('snapinpath'))) {
  824                         throw new Exception(
  825                             _('Failed to add snapin')
  826                         );
  827                     }
  828                 }
  829                 self::$FOGFTP->delete($dest);
  830                 if (!self::$FOGFTP->put($dest, $src)) {
  831                     throw new Exception(
  832                         _('Failed to add/update snapin file')
  833                     );
  834                 }
  835                 self::$FOGFTP
  836                     ->chmod(0777, $dest)
  837                     ->close();
  838             }
  839             $Snapin = self::getClass('Snapin')
  840                 ->set('name', $name)
  841                 ->set('packtype', $packtype)
  842                 ->set('description', $desc)
  843                 ->set('file', $snapinfile)
  844                 ->set('hash', $hash)
  845                 ->set('size', $size)
  846                 ->set('args', $args)
  847                 ->set('reboot', $action == 'reboot')
  848                 ->set('shutdown', $action == 'shutdown')
  849                 ->set('runWith', $runWith)
  850                 ->set('runWithArgs', $runWithArgs)
  851                 ->set('isEnabled', $isEnabled)
  852                 ->set('toReplicate', $toReplicate)
  853                 ->set('hide', $hide)
  854                 ->set('timeout', $timeout)
  855                 ->addGroup($storagegroup);
  856             if (!$Snapin->save()) {
  857                 throw new Exception(_('Add snapin failed!'));
  858             }
  859             /**
  860              * During snapin creation we only allow a single group anyway.
  861              * This will set it to be the primary master.
  862              */
  863             $Snapin->setPrimaryGroup($storagegroup);
  864             $hook = 'SNAPIN_ADD_SUCCESS';
  865             $msg = json_encode(
  866                 array(
  867                     'msg' => _('Snapin added!'),
  868                     'title' => _('Snapin Create Success')
  869                 )
  870             );
  871         } catch (Exception $e) {
  872             self::$FOGFTP->close();
  873             $hook = 'SNAPIN_ADD_FAIL';
  874             $msg = json_encode(
  875                 array(
  876                     'error' => $e->getMessage(),
  877                     'title' => _('Snapin Create Fail')
  878                 )
  879             );
  880         }
  881         self::$HookManager
  882             ->processEvent(
  883                 $hook,
  884                 array('Snapin' => &$Snapin)
  885             );
  886         unset($Snapin);
  887         echo $msg;
  888         exit;
  889     }
  890     /**
  891      * Display snapin general edit elements.
  892      *
  893      * @return void
  894      */
  895     public function snapinGeneral()
  896     {
  897         unset(
  898             $this->data,
  899             $this->form,
  900             $this->headerData,
  901             $this->attributes,
  902             $this->templates
  903         );
  904         $this->title = _('Snapin General');
  905         $this->attributes = array(
  906             array('class' => 'col-xs-4'),
  907             array('class' => 'col-xs-8 form-group')
  908         );
  909         $this->templates = array(
  910             '${field}',
  911             '${input}'
  912         );
  913         $snapinfileexists = basename(
  914             filter_input(INPUT_POST, 'snapinfileexist')
  915         );
  916         if (!$snapinfileexists) {
  917             $snapinfileexists = $this->obj->get('file');
  918         }
  919         $name = filter_input(INPUT_POST, 'name') ?:
  920             $this->obj->get('name');
  921         $desc = filter_input(INPUT_POST, 'description') ?:
  922             $this->obj->get('description');
  923         $packtype = (int)filter_input(INPUT_POST, 'packtype') ?:
  924             (int)$this->obj->get('packtype');
  925         $rw = filter_input(INPUT_POST, 'rw') ?:
  926             $this->obj->get('runWith');
  927         $rwa = filter_input(INPUT_POST, 'rwa') ?:
  928             $this->obj->get('runWithArgs');
  929         $protected = (int)isset($_POST['protected_snapin']) ?:
  930             (int)$this->obj->get('protected');
  931         $toReplicate = (int)isset($_POST['toReplicate']) ?:
  932             (int)$this->obj->get('toReplicate');
  933         $isEnabled = (int)isset($_POST['isEnabled']) ?:
  934             (int)$this->obj->get('isEnabled');
  935         $isHidden = (int)isset($_POST['isHidden']) ?:
  936             (int)$this->obj->get('hide');
  937         $timeout = (int)filter_input(INPUT_POST, 'timeout') ?:
  938             (int)$this->obj->get('timeout');
  939         $ishid = (
  940             $isHidden > 0 ? ' checked' : ''
  941         );
  942         $isprot = (
  943             $protected > 0 ? ' checked' : ''
  944         );
  945         $isen = (
  946             $isEnabled > 0 ? ' checked' : ''
  947         );
  948         $isrep = (
  949             $toReplicate > 0 ? ' checked' : ''
  950         );
  951         $action = filter_input(INPUT_POST, 'action');
  952         if (!$action) {
  953             $action = (
  954                 $this->obj->get('shutdown') ?
  955                 'shutdown' : (
  956                     $this->obj->get('reboot') ?
  957                     'reboot' :
  958                     ''
  959                 )
  960             );
  961         }
  962         $reboot = (
  963             $action == 'reboot' ? ' checked' : ''
  964         );
  965         $shutdown = (
  966             $action == 'shutdown' ? ' checked' : ''
  967         );
  968         $args = filter_input(INPUT_POST, 'args') ?:
  969             $this->obj->get('args');
  970         self::$selected = $snapinfileexists;
  971         $filelist = self::getSubObjectIDs(
  972             'Snapin',
  973             '',
  974             'file'
  975         );
  976         $filelist = array_values(
  977             array_unique(
  978                 array_filter(
  979                     $filelist
  980                 )
  981             )
  982         );
  983         natcasesort($filelist);
  984         ob_start();
  985         array_map(
  986             self::$buildSelectBox,
  987             $filelist
  988         );
  989         $selectFiles = '<select class='
  990             . '"snapinfileexist-input cmdlet3 form-control" '
  991             . 'name="snapinfileexist" id="snapinfileexist">'
  992             . '<option value="">- '
  993             . _('Please select an option')
  994             . ' -</option>'
  995             . ob_get_clean()
  996             . '</select>';
  997         $fields = array(
  998             '<label for="name">'
  999             . _('Snapin Name')
 1000             . '</label>' => '<div class="input-group">'
 1001             . '<input type="text" name="name" id="name" value="'
 1002             . $name
 1003             . '" class="snapinname-input form-control"/>'
 1004             . '</div>',
 1005             '<label for="desc">'
 1006             . _('Snapin Description')
 1007             . '</label>' => '<div class="input-group">'
 1008             . '<textarea name="description" id="desc" class='
 1009             . '"form-control snapindescription-input">'
 1010             . $desc
 1011             . '</textarea>'
 1012             . '</div>',
 1013             '<label for="snapinpack">'
 1014             . _('Snapin Type')
 1015             . '</label>' => '<select class="snapinpack-input form-control" '
 1016             . 'name="packtype" id="snapinpack">'
 1017             . '<option value="0"'
 1018             . (
 1019                 $packtype == 0 ?
 1020                 ' selected' :
 1021                 ''
 1022             )
 1023             . '>'
 1024             . _('Normal Snapin')
 1025             . '</option>'
 1026             . '<option value="1"'
 1027             . (
 1028                 $packtype > 0 ?
 1029                 ' selected' :
 1030                 ''
 1031             )
 1032             . '>'
 1033             . _('Snapin Pack')
 1034             . '</option>'
 1035             . '</select>',
 1036             '<span class="hiddeninitially packnotemplate">'
 1037             . '<label for="argTypes">'
 1038             . _('Snapin Template')
 1039             . '</label>'
 1040             . '</span>'
 1041             . '<span class="hiddeninitially packtemplate">'
 1042             . '<label for="packTypes">'
 1043             . _('Snapin Pack Template')
 1044             . '</label>'
 1045             . '</span>' => '<span class="hiddeninitially packnotemplate">'
 1046             . self::$_template1
 1047             . '</span>'
 1048             . '<span class="hiddeninitially packtemplate">'
 1049             . self::$_template2
 1050             . '</span>',
 1051             '<span class="hiddeninitially packnochangerw">'
 1052             . '<label for="snaprw">'
 1053             . _('Snapin Run With')
 1054             . '</label>'
 1055             . '</span>'
 1056             . '<span class="hiddeninitially packchangerw">'
 1057             . '<label for="snaprw">'
 1058             . _('Snapin Pack File')
 1059             . '</label>'
 1060             . '</span>' => '<div class="input-group">'
 1061             . '<input type="text" class="snapinrw-input cmdlet1 form-control" '
 1062             . 'name="rw" id="snaprw" value="'
 1063             . $rw
 1064             . '"/>'
 1065             . '</div>',
 1066             '<span class="hiddeninitially packnochangerwa">'
 1067             . '<label for="snaprwa">'
 1068             . _('Snapin Run With Argument')
 1069             . '</label>'
 1070             . '</span>'
 1071             . '<span class="hiddeninitially packchangerwa">'
 1072             . '<label for="snaprwa">'
 1073             . _('Snapin Pack Arguments')
 1074             . '</label>'
 1075             . '</span>' => '<div class="input-group">'
 1076             . '<input type="text" class="snapinrwa-input cmdlet2 form-control" '
 1077             . 'name="rwa" id="snaprwa" value="'
 1078             . $rwa
 1079             . '"/>'
 1080             . '</div>',
 1081             '<label for="snapinfile">'
 1082             . _('Snapin File')
 1083             . '<br/>'
 1084             . _('Max Size')
 1085             . ': '
 1086             . ini_get('post_max_size')
 1087             . '</label>' => '<div class="input-group">'
 1088             . '<label class="input-group-btn">'
 1089             . '<span class="btn btn-info">'
 1090             . _('Browse')
 1091             . '<input type="file" class="hidden cmdlet3" name="snapin" '
 1092             . 'id="snapinfile"/>'
 1093             . '</span>'
 1094             . '</label>'
 1095             . '<input type="text" class="form-control filedisp cmdlet3" value="'
 1096             . $snapinfileexists
 1097             . '" readonly/>'
 1098             . '</div>',
 1099             (
 1100                 count($filelist) > 0 ?
 1101                 '<label for="snapinfileexist">'
 1102                 . _('Snapin File (exists)')
 1103                 . '</label>' :
 1104                 ''
 1105             ) => (
 1106                 count($filelist) > 0 ?
 1107                 $selectFiles :
 1108                 ''
 1109             ),
 1110             '<span class="hiddeninitially packhide">'
 1111             . '<label for="args">'
 1112             . _('Snapin Arguments')
 1113             . '</label>'
 1114             . '</span>' => '<span class="hiddeninitially packhide">'
 1115             . '<div class="input-group">'
 1116             . '<input type="text" name="args" id="args" class='
 1117             . '"snapinargs-input cmdlet4 form-control" value="'
 1118             . $args
 1119             . '"/>'
 1120             . '</div>'
 1121             . '</span>',
 1122             '<label for="isprot">'
 1123             . _('Snapin Protected')
 1124             . '</label>' => '<input type="checkbox" name="protected_snapin" '
 1125             . 'class="snapinprotected-input" id="isprot"'
 1126             . $isprot
 1127             . '/>',
 1128             '<label for="isen">'
 1129             . _('Snapin Enabled')
 1130             . '</label>' => '<input type="checkbox" name="isEnabled" '
 1131             . 'class="snapinenabled-input" id="isen"'
 1132             . $isen
 1133             . '/>',
 1134             '<label for="isHidden">'
 1135             . _('Snapin Arguments Hidden')
 1136             . '</label>' => '<input type="checkbox" name="isHidden" '
 1137             . 'class="snapinhidden-input" id="isHidden"'
 1138             . $ishid
 1139             . '/>',
 1140             '<label for="timeout">'
 1141             . _('Snapin Timeout (seconds)')
 1142             . '</label>' => '<div class="input-group">'
 1143             . '<input type="number" class='
 1144             . '"snapintimeout-input form-control" name="timeout" '
 1145             . 'id="timeout" value="'
 1146             . $timeout
 1147             . '"/>'
 1148             . '</div>',
 1149             '<label for="toRep">'
 1150             . _('Replicate?')
 1151             . '</label>' => '<input type="checkbox" name="toReplicate" '
 1152             . 'class="snapinreplicate-input" id="toRep"'
 1153             . $isrep
 1154             . '/>',
 1155             '<label for="reboot">'
 1156             . _('Reboot after install')
 1157             . '</label>' => '<input type="radio" name="action" '
 1158             . 'class="snapinreboot-input action" id="reboot" value="reboot"'
 1159             . $reboot
 1160             . '/>',
 1161             '<label for="shutdown">'
 1162             . _('Shutdown after install')
 1163             . '</label>' => '<input type="radio" name="action" '
 1164             . 'class="snapinshutdown-input action" id="shutdown" value="shutdown"'
 1165             . $shutdown
 1166             . '/>',
 1167             '<label for="cmdletin">'
 1168             . _('Snapin Command')
 1169             . '<br/>'
 1170             . _('read-only')
 1171             . '</label>' => '<div class="input-group">'
 1172             . '<textarea class="form-control snapincmd" name="snapincmd" '
 1173             . 'id="cmdletin" readonly></textarea>',
 1174             '<label for="update">'
 1175             . _('Make Changes?')
 1176             . '</label>' => '<button type="submit" name="update" id="update" '
 1177             . 'class="btn btn-info btn-block">'
 1178             . _('Update')
 1179             . '</button>'
 1180         );
 1181         $fields = array_filter(
 1182             $fields
 1183         );
 1184         array_walk($fields, $this->fieldsToData);
 1185         self::$HookManager
 1186             ->processEvent(
 1187                 'SNAPIN_EDIT',
 1188                 array(
 1189                     'data' => &$this->data,
 1190                     'headerData' => &$this->headerData,
 1191                     'attributes' => &$this->attributes,
 1192                     'templates' => &$this->templates
 1193                 )
 1194             );
 1195         echo '<!-- General -->';
 1196         echo '<div class="tab-pane fade in active" id="snap-gen">';
 1197         echo '<div class="panel panel-info">';
 1198         echo '<div class="panel-heading text-center">';
 1199         echo '<h4 class="title">';
 1200         echo $this->title;
 1201         echo '</h4>';
 1202         echo '</div>';
 1203         echo '<div class="panel-body">';
 1204         echo '<form class="form-horizontal" method="post" action="'
 1205             . $this->formAction
 1206             . '&tab=snap-gen">';
 1207         $this->render(12);
 1208         echo '</form>';
 1209         echo '</div>';
 1210         echo '</div>';
 1211         echo '</div>';
 1212         unset(
 1213             $this->data,
 1214             $this->form,
 1215             $this->headerData,
 1216             $this->attributes,
 1217             $this->templates
 1218         );
 1219     }
 1220     /**
 1221      * Display snapin storage groups.
 1222      *
 1223      * @return void
 1224      */
 1225     public function snapinStoragegroups()
 1226     {
 1227         unset(
 1228             $this->data,
 1229             $this->form,
 1230             $this->templates,
 1231             $this->attributes,
 1232             $this->headerData
 1233         );
 1234         $this->headerData = array(
 1235             '<label for="toggler2">'
 1236             . '<input type="checkbox" name="toggle-checkboxgroup1" '
 1237             . 'class="toggle-checkbox1" id="toggler2"/>'
 1238             . '</label>',
 1239             _('Storage Group Name')
 1240         );
 1241         $this->templates = array(
 1242             '<label for="sg-${storageGroup_id}">'
 1243             . '<input type="checkbox" name="storagegroup[]" class='
 1244             . '"toggle-group" id="sg-${storageGroup_id}" '
 1245             . 'value="${storageGroup_id}"/>'
 1246             . '</label>',
 1247             '<a href="?node=storage&editStorageGroup&id=${storageGroup_id}">'
 1248             . '${storageGroup_name}'
 1249             . '</a>'
 1250         );
 1251         $this->attributes = array(
 1252             array(
 1253                 'class' => 'filter-false',
 1254                 'width' => 16
 1255             ),
 1256             array(),
 1257         );
 1258         Route::listem('storagegroup');
 1259         $StorageGroups = json_decode(
 1260             Route::getData()
 1261         );
 1262         $StorageGroups = $StorageGroups->storagegroups;
 1263         foreach ((array)$StorageGroups as &$StorageGroup) {
 1264             $groupinme = in_array(
 1265                 $StorageGroup->id,
 1266                 $this->obj->get('storagegroups')
 1267             );
 1268             if ($groupinme) {
 1269                 continue;
 1270             }
 1271             $this->data[] = array(
 1272                 'storageGroup_id' => $StorageGroup->id,
 1273                 'storageGroup_name' => $StorageGroup->name,
 1274             );
 1275             unset($StorageGroup);
 1276         }
 1277         self::$HookManager->processEvent(
 1278             'SNAPIN_ADD_STORAGE_GROUP',
 1279             array(
 1280                 'data' => &$this->data,
 1281                 'headerData' => &$this->headerData,
 1282                 'templates' => &$this->templates,
 1283                 'attributes' => &$this->attributes
 1284             )
 1285         );
 1286         echo '<!-- Storage Groups -->';
 1287         echo '<div class="tab-pane fade" id="snap-storage">';
 1288         echo '<div class="panel panel-info">';
 1289         echo '<div class="panel-heading text-center">';
 1290         echo '<h4 class="title">';
 1291         echo _('Snapin Storage Groups');
 1292         echo '</h4>';
 1293         echo '</div>';
 1294         echo '<div class="panel-body">';
 1295         echo '<form class="form-horizontal" method="post" action="'
 1296             . $this->formAction
 1297             . '&tab=snap-storage" enctype="multipart/form-data">';
 1298         if (is_array($this->data) && count($this->data)) {
 1299             echo '<div class="text-center">';
 1300             echo '<div class="checkbox">';
 1301             echo '<label for="groupMeShow">';
 1302             echo '<input type="checkbox" name="groupMeShow" '
 1303                 . 'id="groupMeShow"/>';
 1304             echo _('Check here to see what storage groups can be added');
 1305             echo '</label>';
 1306             echo '</div>';
 1307             echo '</div>';
 1308             echo '<br/>';
 1309             echo '<div class="hiddeninitially groupNotInMe panel panel-info" '
 1310                 . 'id="groupNotInMe">';
 1311             echo '<div class="panel-heading text-center">';
 1312             echo '<h4 class="title">';
 1313             echo _('Add Storage Groups');
 1314             echo '</h4>';
 1315             echo '</div>';
 1316             echo '<div class="panel-body">';
 1317             $this->render(12);
 1318             echo '<div class="form-group">';
 1319             echo '<label for="updategroups" class="control-label col-xs-4">';
 1320             echo _('Add selected storage groups');
 1321             echo '</label>';
 1322             echo '<div class="col-xs-8">';
 1323             echo '<button type="submit" name="updategroups" class='
 1324                 . '"btn btn-info btn-block" id="updategroups">'
 1325                 . _('Add')
 1326                 . '</button>';
 1327             echo '</div>';
 1328             echo '</div>';
 1329             echo '</div>';
 1330             echo '</div>';
 1331         }
 1332         unset(
 1333             $this->data,
 1334             $this->headerData,
 1335             $this->templates,
 1336             $this->attributes
 1337         );
 1338         $this->headerData = array(
 1339             '<label for="toggler3">'
 1340             . '<input type="checkbox" name="toggle-checkbox" '
 1341             . 'class="toggle-checkboxAction" id="toggler3"/>'
 1342             . '</label>',
 1343             '',
 1344             _('Storage Group Name')
 1345         );
 1346         $this->templates = array(
 1347             '<label for="sg1-${storageGroup_id}">'
 1348             . '<input type="checkbox" name="storagegroup-rm[]" class='
 1349             . '"toggle-group" id="sg1-${storageGroup_id}" '
 1350             . 'value="${storageGroup_id}"/>'
 1351             . '</label>',
 1352             '<div class="radio">'
 1353             . '<input type="radio" class="default" '
 1354             . 'name="primary" id="group${storageGroup_id}" '
 1355             . 'value="${storageGroup_id}" ${is_primary}/>'
 1356             . '<label for="group${storageGroup_id}">'
 1357             . '</label>'
 1358             . '</div>',
 1359             '<a href="?node=storage&editStorageGroup&id=${storageGroup_id}">'
 1360             . '${storageGroup_name}'
 1361             . '</a>'
 1362         );
 1363         $this->attributes = array(
 1364             array(
 1365                 'class' => 'filter-false',
 1366                 'width' => 16
 1367             ),
 1368             array(
 1369                 'class' => 'filter-false',
 1370                 'width' => 16
 1371             ),
 1372             array(),
 1373         );
 1374         foreach ((array)$StorageGroups as &$StorageGroup) {
 1375             $groupinme = in_array(
 1376                 $StorageGroup->id,
 1377                 $this->obj->get('storagegroups')
 1378             );
 1379             if (!$groupinme) {
 1380                 continue;
 1381             }
 1382             $this->data[] = array(
 1383                 'storageGroup_id' => $StorageGroup->id,
 1384                 'storageGroup_name' => $StorageGroup->name,
 1385                 'is_primary' => (
 1386                     $this->obj->getPrimaryGroup($StorageGroup->id) ?
 1387                     ' checked' :
 1388                     ''
 1389                 )
 1390             );
 1391             unset($StorageGroup);
 1392         }
 1393         if (is_array($this->data) && count($this->data) > 0) {
 1394             self::$HookManager->processEvent(
 1395                 'SNAPIN_EDIT_STORAGE_GROUP',
 1396                 array(
 1397                     'data' => &$this->data,
 1398                     'headerData' => &$this->headerData,
 1399                     'templates' => &$this->templates,
 1400                     'attributes' => &$this->attributes
 1401                 )
 1402             );
 1403             echo '<div class="panel panel-info">';
 1404             echo '<div class="panel-heading text-center">';
 1405             echo '<h4 class="title">';
 1406             echo _('Update/Remove Storage Groups');
 1407             echo '</h4>';
 1408             echo '</div>';
 1409             echo '<div class="panel-body">';
 1410             $this->render(12);
 1411             echo '<div class="form-group">';
 1412             echo '<label for="primarysel" class="control-label col-xs-4">';
 1413             echo _('Update primary group');
 1414             echo '</label>';
 1415             echo '<div class="col-xs-8">';
 1416             echo '<button type="submit" name="primarysel" class='
 1417                 . '"btn btn-info btn-block" id="primarysel">'
 1418                 . _('Update')
 1419                 . '</button>';
 1420             echo '</div>';
 1421             echo '</div>';
 1422             echo '<div class="form-group">';
 1423             echo '<label for="groupdel" class="control-label col-xs-4">';
 1424             echo _('Remove selected groups');
 1425             echo '</label>';
 1426             echo '<div class="col-xs-8">';
 1427             echo '<button type="submit" name="groupdel" class='
 1428                 . '"btn btn-danger btn-block" id="groupdel">'
 1429                 . _('Remove')
 1430                 . '</button>';
 1431             echo '</div>';
 1432             echo '</div>';
 1433             echo '</div>';
 1434             echo '</div>';
 1435         }
 1436         echo '</form>';
 1437         echo '</div>';
 1438         echo '</div>';
 1439         echo '</div>';
 1440         unset(
 1441             $this->data,
 1442             $this->form,
 1443             $this->templates,
 1444             $this->attributes,
 1445             $this->headerData
 1446         );
 1447     }
 1448     /**
 1449      * Edit this snapin
 1450      *
 1451      * @return void
 1452      */
 1453     public function edit()
 1454     {
 1455         echo '<div class="col-xs-9 tab-content">';
 1456         $this->snapinGeneral();
 1457         $this->snapinStoragegroups();
 1458         echo '</div>';
 1459     }
 1460     /**
 1461      * Snapin General Post
 1462      *
 1463      * @return void
 1464      */
 1465     public function snapinGeneralPost()
 1466     {
 1467         $name = filter_input(INPUT_POST, 'name');
 1468         $desc = filter_input(INPUT_POST, 'description');
 1469         $packtype = filter_input(INPUT_POST, 'packtype');
 1470         $runWith = filter_input(INPUT_POST, 'rw');
 1471         $runWithArgs = filter_input(INPUT_POST, 'rwa');
 1472         $snapinfile = basename(
 1473             filter_input(INPUT_POST, 'snapinfileexist')
 1474         );
 1475         $uploadfile = basename(
 1476             $_FILES['snapin']['name']
 1477         );
 1478         if ($uploadfile) {
 1479             $snapinfile = $uploadfile;
 1480         }
 1481         $protected = (int)isset($_POST['protected_snapin']);
 1482         $isEnabled = (int)isset($_POST['isEnabled']);
 1483         $toReplicate = (int)isset($_POST['toReplicate']);
 1484         $hide = (int)isset($_POST['isHidden']);
 1485         $timeout = (int)filter_input(INPUT_POST, 'timeout');
 1486         $action = filter_input(INPUT_POST, 'action');
 1487         $args = filter_input(INPUT_POST, 'args');
 1488         $phpPostMaxSize = ini_get('post_max_size');
 1489         switch (strtolower(substr($phpPostMaxSize, -1))) {
 1490             case 'g':
 1491                 $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
 1492                 // no break
 1493             case 'm':
 1494                 $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
 1495                 // no break
 1496             case 'k':
 1497                 $phpPostMaxSize = ((int)$phpPostMaxSize) * 1024;
 1498         }
 1499         if (isset($_SERVER['CONTENT_LENGTH']) && $phpPostMaxSize > 0 &&
 1500                 $_SERVER['CONTENT_LENGTH'] > $phpPostMaxSize) {
 1501             throw new Exception(
 1502                 _('Snapin file is too big, increase post_max_size in php.ini.')
 1503             );
 1504         }
 1505         if (empty($_POST) && isset($_SERVER['CONTENT_LENGTH']) &&
 1506                 $_SERVER['CONTENT_LENGTH'] > 0) {
 1507             throw new Exception(
 1508                 _('$_POST variable is empty, check apache error log.')
 1509             );
 1510         }
 1511         if (!$name) {
 1512             throw new Exception(
 1513                 _('A snapin name is required!')
 1514             );
 1515         }
 1516         if ($this->obj->get('name') != $name
 1517             && self::getClass('SnapinManager')->exists(
 1518                 $name,
 1519                 $this->obj->get('id')
 1520             )
 1521         ) {
 1522             throw new Exception(
 1523                 _('A snapin already exists with this name!')
 1524             );
 1525         }
 1526         if (!$snapinfile) {
 1527             throw new Exception(
 1528                 sprintf(
 1529                     '%s, %s, %s!',
 1530                     _('A file'),
 1531                     _('either already selected or uploaded'),
 1532                     _('must be specified')
 1533                 )
 1534             );
 1535         }
 1536         if (preg_match('#ssl#i', $snapinfile)) {
 1537             throw new Exception(
 1538                 sprintf(
 1539                     '%s, %s.',
 1540                     _('Please choose a different name'),
 1541                     _('this one is reserved for FOG')
 1542                 )
 1543             );
 1544         }
 1545         $snapinfile = preg_replace('/[^-\w\.]+/', '_', $snapinfile);
 1546         $StorageNode = $this
 1547             ->obj
 1548             ->getStorageGroup()
 1549             ->getMasterStorageNode();
 1550         if (!$snapinfile && $_FILES['snapin']['error'] > 0) {
 1551             throw new UploadException($_FILES['snapin']['error']);
 1552         }
 1553         $src = sprintf(
 1554             '%s/%s',
 1555             dirname($_FILES['snapin']['tmp_name']),
 1556             basename($_FILES['snapin']['tmp_name'])
 1557         );
 1558         set_time_limit(0);
 1559         if ($uploadfile && file_exists($src)) {
 1560             $hash = hash_file('sha512', $src);
 1561             $size = self::getFilesize($src);
 1562         } else {
 1563             if ($snapinfile == $this->obj->get('file')) {
 1564                 $hash = $this->obj->get('hash');
 1565                 $size = $this->obj->get('size');
 1566             } else {
 1567                 $hash = '';
 1568                 $size = 0;
 1569             }
 1570         }
 1571         $dest = sprintf(
 1572             '/%s/%s',
 1573             trim(
 1574                 $StorageNode->get('snapinpath'),
 1575                 '/'
 1576             ),
 1577             $snapinfile
 1578         );
 1579         if ($uploadfile) {
 1580             self::$FOGFTP
 1581                 ->set('host', $StorageNode->get('ip'))
 1582                 ->set('username', $StorageNode->get('user'))
 1583                 ->set('password', $StorageNode->get('pass'));
 1584             if (!self::$FOGFTP->connect()) {
 1585                 throw new Exception(
 1586                     sprintf(
 1587                         '%s: %s: %s.',
 1588                         _('Storage Node'),
 1589                         $StorageNode->get('ip'),
 1590                         _('FTP Connection has failed')
 1591                     )
 1592                 );
 1593             }
 1594             if (!self::$FOGFTP->chdir($StorageNode->get('snapinpath'))) {
 1595                 if (!self::$FOGFTP->mkdir($StorageNode->get('snapinpath'))) {
 1596                     throw new Exception(
 1597                         _('Failed to add snapin')
 1598                     );
 1599                 }
 1600             }
 1601             self::$FOGFTP->delete($dest);
 1602             if (!self::$FOGFTP->put($dest, $src)) {
 1603                 throw new Exception(
 1604                     _('Failed to add/update snapin file')
 1605                 );
 1606             }
 1607             self::$FOGFTP
 1608                 ->chmod(0777, $dest)
 1609                 ->close();
 1610         }
 1611         $this->obj
 1612             ->set('name', $name)
 1613             ->set('packtype', $packtype)
 1614             ->set('description', $desc)
 1615             ->set('file', $snapinfile)
 1616             ->set('args', $args)
 1617             ->set('hash', $hash)
 1618             ->set('size', $size)
 1619             ->set('reboot', $action == 'reboot')
 1620             ->set('shutdown', $action == 'shutdown')
 1621             ->set('runWith', $runWith)
 1622             ->set('runWithArgs', $runWithArgs)
 1623             ->set('protected', $protected)
 1624             ->set('isEnabled', $isEnabled)
 1625             ->set('toReplicate', $toReplicate)
 1626             ->set('hide', $hide)
 1627             ->set('timeout', $timeout);
 1628     }
 1629     /**
 1630      * Snapin Storage Group Post
 1631      *
 1632      * @return void
 1633      */
 1634     public function snapinStoragegroupsPost()
 1635     {
 1636         $items = filter_input_array(
 1637             INPUT_POST,
 1638             array(
 1639                 'storagegroup' => array(
 1640                     'flags' => FILTER_REQUIRE_ARRAY
 1641                 ),
 1642                 'storagegroup-rm' => array(
 1643                     'flags' => FILTER_REQUIRE_ARRAY
 1644                 )
 1645             )
 1646         );
 1647         $storagegroup = $items['storagegroup'];
 1648         $storagegrouprm = $items['storagegroup-rm'];
 1649         $primary = (int)filter_input(
 1650             INPUT_POST,
 1651             'primary'
 1652         );
 1653         if (isset($_POST['updategroups'])) {
 1654             $this->obj->addGroup($storagegroup);
 1655         } elseif (isset($_POST['primarysel'])) {
 1656             $this->obj->setPrimaryGroup($primary);
 1657         } elseif (isset($_POST['groupdel'])) {
 1658             $groupdel = count($storagegrouprm);
 1659             $ingroups = count($this->obj->get('storagegroups'));
 1660             if ($groupdel < 1) {
 1661                 throw new Exception(
 1662                     _('No groups selected to be removed')
 1663                 );
 1664             }
 1665             if ($ingroups < 2) {
 1666                 throw new Exception(
 1667                     _('You must have at least one group associated')
 1668                 );
 1669             }
 1670             $this
 1671                 ->obj
 1672                 ->removeGroup(
 1673                     $storagegrouprm
 1674                 );
 1675         }
 1676     }
 1677     /**
 1678      * Submit for update.
 1679      *
 1680      * @return void
 1681      */
 1682     public function editPost()
 1683     {
 1684         self::$HookManager->processEvent(
 1685             'SNAPIN_EDIT_POST',
 1686             array(
 1687                 'Snapin' => &$this->obj
 1688             )
 1689         );
 1690         global $tab;
 1691         try {
 1692             switch ($tab) {
 1693             case 'snap-gen':
 1694                 $this->snapinGeneralPost();
 1695                 break;
 1696             case 'snap-storage':
 1697                 $this->snapinStoragegroupsPost();
 1698                 break;
 1699             }
 1700             if (!$this->obj->save()) {
 1701                 throw new Exception(_('Snapin update failed!'));
 1702             }
 1703             $hook = 'SNAPIN_UPDATE_SUCCESS';
 1704             $msg = json_encode(
 1705                 array(
 1706                     'msg' => _('Snapin updated!'),
 1707                     'title' => _('Snapin Update Success')
 1708                 )
 1709             );
 1710         } catch (Exception $e) {
 1711             self::$FOGFTP->close();
 1712             $hook = 'SNAPIN_UPDATE_FAIL';
 1713             $msg = json_encode(
 1714                 array(
 1715                     'error' => $e->getMessage(),
 1716                     'title' => _('Snapin Update Fail')
 1717                 )
 1718             );
 1719         }
 1720         self::$HookManager
 1721             ->processEvent(
 1722                 $hook,
 1723                 array('Snapin' => &$this->obj)
 1724             );
 1725         echo $msg;
 1726         exit;
 1727     }
 1728 }