function openqrm_puppet_remove_cloud_applications($appliance_name)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    $puppet = new puppet();
    $puppet->remove_appliance($appliance_name);
}
function openqrm_puppet_cloud_product($cmd, $cloud_hook_config)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    global $RootDir;
    $openqrm_server = new openqrm_server();
    $event->log("openqrm_puppet_cloud_product", $_SERVER['REQUEST_TIME'], 5, "openqrm-puppet-cloud-product-hook.php", "Handling " . $cmd . " event", "", "", 0, 0, 0);
    switch ($cmd) {
        case "add":
            $event->log("openqrm_puppet_cloud_product", $_SERVER['REQUEST_TIME'], 5, "openqrm-puppet-cloud-product-hook.php", "Handling cloud-product " . $cmd . " event", "", "", 0, 0, 0);
            // create application products
            $next_sort_id = 0;
            $db = openqrm_get_db_connection();
            $puppet = new puppet();
            $puppet_group_array = $puppet->get_available_groups();
            foreach ($puppet_group_array as $index => $puppet_app) {
                $event->log("openqrm_puppet_cloud_product", $_SERVER['REQUEST_TIME'], 5, "openqrm-puppet-cloud-product-hook.php", "Adding application " . $puppet_app . " as cloud-product", "", "", 0, 0, 0);
                $cloud_product_id = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                $create_application_cloudselector_config = "insert into cloud_selector (id, type, sort_id, quantity, price, name, description, state) VALUES (" . $cloud_product_id . ", 'application', " . $next_sort_id . ", 'puppet/" . $puppet_app . "', 1, '" . $puppet_app . "', '" . $puppet_app . " via puppet', 1);";
                $recordSet = $db->Execute($create_application_cloudselector_config);
                $next_sort_id++;
            }
            break;
        case "remove":
            $cloud_product_class = $RootDir . "/plugins/cloud/class/cloudselector.class.php";
            if (file_exists($cloud_product_class)) {
                require_once $cloud_product_class;
                $cloud_selector = new cloudselector();
                $cloud_selector_id_ar = $cloud_selector->get_all_ids();
                foreach ($cloud_selector_id_ar as $key => $value) {
                    $id = $value['id'];
                    $cloud_selector->get_instance_by_id($id);
                    $pos = strpos($cloud_selector->quantity, 'puppet/');
                    if ($pos !== false) {
                        $event->log("openqrm_puppet_cloud_product", $_SERVER['REQUEST_TIME'], 5, "openqrm-puppet-cloud-product-hook.php", "Removing application " . $cloud_selector->quantity . " from cloud-products", "", "", 0, 0, 0);
                        $cloud_selector->remove($id);
                    }
                }
            }
            break;
    }
}
 function get_response()
 {
     $response = $this->response;
     $form = $response->get_form($this->actions_name, "create");
     if ($response->html->request()->get('profile')) {
         require_once $this->rootdir . "/plugins/cloud/class/cloudprofile.class.php";
         $this->cloudprofile = new cloudprofile();
         $this->cloudprofile->get_instance_by_id($response->html->request()->get('profile'));
         $msg = $response->html->request()->get($this->message_param);
         if ($msg !== '') {
             $msg = $msg . '<br>';
         }
         $_REQUEST[$this->message_param] = $msg . sprintf($this->lang['msg_loading_profile'], $this->cloudprofile->name);
     }
     // pre-define select arrays
     $kernel_list = array();
     $cloud_image_select_arr = '';
     $cloud_virtualization_select_arr = '';
     $virtualization_list_select = array();
     $cloud_memory_select_arr = '';
     $cloud_cpu_select_arr = '';
     $cloud_disk_select_arr = '';
     $cloud_network_select_arr = '';
     $cloud_ha_select_arr = '';
     $cloud_application_select_arr = array();
     $product_application_description_arr = array();
     $ip_mgmt_list_per_user_arr = array();
     // global limits
     $max_resources_per_cr = 1;
     // big switch ##############################################################
     //  : either show what is provided in the cloudselector
     //  : or show what is available
     // check if cloud_selector feature is enabled
     $cloud_selector_enabled = $this->cloudconfig->get_value_by_key('cloud_selector');
     // cloud_selector
     $virt_types = array();
     if (!strcmp($cloud_selector_enabled, "true")) {
         // show what is provided by the cloudselectors
         // cpus
         $product_array = $this->cloudselector->display_overview_per_type("cpu");
         $available_cpunumber = array();
         foreach ($product_array as $index => $cloudproduct) {
             // is product enabled ?
             if ($cloudproduct["state"] == 1) {
                 $cs_cpu = $cloudproduct["quantity"];
                 if ($cs_cpu <= $this->cloud_limits->free('cpu')) {
                     $available_cpunumber[] = array("value" => $cloudproduct["quantity"], "label" => $cloudproduct["name"]);
                 }
             }
         }
         // disk size
         $disk_size_select = array();
         $product_array = $this->cloudselector->display_overview_per_type("disk");
         foreach ($product_array as $index => $cloudproduct) {
             // is product enabled ?
             if ($cloudproduct["state"] == 1) {
                 $cs_disk = $cloudproduct["quantity"];
                 if ($cs_disk <= $this->cloud_limits->free('disk')) {
                     $disk_size_select[] = array("value" => $cloudproduct["quantity"], "label" => $cloudproduct["name"]);
                 }
             }
         }
         // kernel
         $product_array = $this->cloudselector->display_overview_per_type("kernel");
         foreach ($product_array as $index => $cloudproduct) {
             // is product enabled ?
             if ($cloudproduct["state"] == 1) {
                 $kernel_list[] = array("value" => $cloudproduct["quantity"], "label" => $cloudproduct["name"]);
             }
         }
         // memory sizes
         $available_memtotal = array();
         $product_array = $this->cloudselector->display_overview_per_type("memory");
         foreach ($product_array as $index => $cloudproduct) {
             // is product enabled ?
             if ($cloudproduct["state"] == 1) {
                 $cs_memory = $cloudproduct["quantity"];
                 if ($cs_memory <= $this->cloud_limits->free('memory')) {
                     $available_memtotal[] = array("value" => $cloudproduct["quantity"], "label" => $cloudproduct["name"]);
                 }
             }
         }
         // network cards
         $max_network_interfaces_select = array();
         $product_array = $this->cloudselector->display_overview_per_type("network");
         foreach ($product_array as $index => $cloudproduct) {
             // is product enabled ?
             if ($cloudproduct["state"] == 1) {
                 $cs_metwork = $cloudproduct["quantity"];
                 if ($cs_metwork <= $this->cloud_limits->free('network')) {
                     $max_network_interfaces_select[] = array("value" => $cloudproduct["quantity"], "label" => $cloudproduct["name"]);
                 }
             }
         }
         // application classes
         // check if to show application
         $show_puppet_groups = $this->cloudconfig->get_value_by_key('show_puppet_groups');
         // show_puppet_groups
         if (!strcmp($show_puppet_groups, "true")) {
             $product_array = $this->cloudselector->display_overview_per_type("application");
             foreach ($product_array as $index => $cloudproduct) {
                 // is product enabled ?
                 if ($cloudproduct["state"] == 1) {
                     $application_product_name = $cloudproduct["description"];
                     $application_class_name = $cloudproduct["quantity"];
                     $cloud_application_select_arr[] = array("value" => $application_class_name, "label" => $application_product_name);
                     $product_application_description_arr[$application_product_name] = $cloudproduct["name"];
                 }
             }
         }
         // virtualization types
         $product_array = $this->cloudselector->display_overview_per_type("resource");
         $virt = $this->openqrm->virtualization();
         foreach ($product_array as $key => $product) {
             // is product enabled ?
             if ($product["state"] == 1) {
                 $virt->get_instance_by_id($product["quantity"]);
                 $tmp = $virt->type;
                 // tag network vms
                 if (strstr($tmp, "-net")) {
                     $tmp = 'vm-net';
                 }
                 // store virtualization type
                 $virt_types[$tmp] = $virt->get_plugin_name();
                 $str = $this->__get_virt_tag($tmp, $virt_types);
                 $virtualization_list_select[] = array("value" => $product["quantity"], "label" => $product["description"] . '  ' . $str);
                 $js_resources[] = array($product["quantity"], $product["description"], $str, $tmp);
             }
         }
     } else {
         // show what is available in openQRM
         $kernel = $this->openqrm->kernel();
         $kernel_list = array();
         $kernel_list = $kernel->get_list();
         // remove the openqrm kernelfrom the list
         array_shift($kernel_list);
         // virtualization types
         $virt = $this->openqrm->virtualization();
         $virtualization_list_select = array();
         $virt_list = $virt->get_list();
         // check if to show physical system type
         $cc_request_physical_systems = $this->cloudconfig->get_value_by_key('request_physical_systems');
         // request_physical_systems
         if (!strcmp($cc_request_physical_systems, "false")) {
             array_shift($virt_list);
         }
         // filter out the virtualization hosts
         foreach ($virt_list as $id => $product) {
             if (!strstr($product['label'], "Host")) {
                 $virt->get_instance_by_id($product['value']);
                 $tmp = $virt->type;
                 // tag network vms
                 if (strstr($tmp, "-net")) {
                     $tmp = 'vm-net';
                 }
                 // store virtualization type
                 $virt_types[$tmp] = $virt->get_plugin_name();
                 $str = $this->__get_virt_tag($tmp, $virt_types);
                 $js_resources[] = array($product['value'], $product['label'], $str, $tmp);
                 $virtualization_list_select[] = array("value" => $product['value'], "label" => $product['label'] . ' ' . $str);
             }
         }
         // prepare the array for the network-interface select
         $max_network_interfaces_select = array();
         $max_network_interfaces = $this->cloudconfig->get_value_by_key('max_network_interfaces');
         // max_network_interfaces
         for ($mnet = 1; $mnet <= $max_network_interfaces; $mnet++) {
             $max_network_interfaces_select[] = array("value" => $mnet, "label" => $mnet);
         }
         # TODO
         # better cpu and memory generation
         // get list of available resource parameters
         $resource_p = $this->openqrm->resource();
         $resource_p_array = $resource_p->get_list();
         // remove openQRM resource
         array_shift($resource_p_array);
         // gather all available values in arrays
         $available_cpunumber_uniq = array();
         $available_cpunumber = array();
         $available_cpunumber[] = array("value" => "0", "label" => "Auto");
         $available_memtotal_uniq = array();
         $available_memtotal = array();
         $available_memtotal[] = array("value" => "0", "label" => "Auto");
         foreach ($resource_p_array as $res) {
             $res_id = $res['resource_id'];
             $tres = $this->openqrm->resource();
             $tres->get_instance_by_id($res_id);
             if (strlen($tres->cpunumber) && intval($tres->cpunumber) !== 0 && !in_array($tres->cpunumber, $available_cpunumber_uniq)) {
                 $available_cpunumber[] = array("value" => $tres->cpunumber, "label" => $tres->cpunumber);
                 $available_cpunumber_uniq[] .= $tres->cpunumber;
             }
             if (strlen($tres->memtotal) && !in_array($tres->memtotal, $available_memtotal_uniq)) {
                 if ($tres->memtotal < 1000) {
                     $size = $tres->memtotal . " MB";
                 } else {
                     $size = $tres->memtotal / 1000 . " GB";
                 }
                 $available_memtotal[] = array("value" => $tres->memtotal, "label" => $size);
                 $available_memtotal_uniq[] .= $tres->memtotal;
             }
         }
         // disk size select
         $disk_size_select = array();
         $max_disk_size = $this->cloud_limits->free('disk');
         if (1000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 1000, "label" => '1 GB');
         }
         if (2000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 2000, "label" => '2 GB');
         }
         if (3000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 3000, "label" => '3 GB');
         }
         if (4000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 4000, "label" => '4 GB');
         }
         if (5000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 5000, "label" => '5 GB');
         }
         if (10000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 10000, "label" => '10 GB');
         }
         if (20000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 20000, "label" => '20 GB');
         }
         if (50000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 50000, "label" => '50 GB');
         }
         if (100000 <= $max_disk_size) {
             $disk_size_select[] = array("value" => 100000, "label" => '100 GB');
         }
         // check if to show puppet
         $show_puppet_groups = $this->cloudconfig->get_value_by_key('show_puppet_groups');
         // show_puppet_groups
         if (!strcmp($show_puppet_groups, "true")) {
             // is puppet enabled ?
             if (file_exists($this->rootdir . "/plugins/puppet/.running")) {
                 require_once $this->rootdir . "/plugins/puppet/class/puppet.class.php";
                 $puppet_group_dir = $this->rootdir . "/plugins/puppet/puppet/manifests/groups";
                 global $puppet_group_dir;
                 $puppet_group_array = array();
                 $puppet = new puppet();
                 $puppet_group_array = $puppet->get_available_groups();
                 foreach ($puppet_group_array as $index => $puppet_g) {
                     $puid = $index + 1;
                     $puppet_info = $puppet->get_group_info($puppet_g);
                     $cloud_application_select_arr[] = array("value" => "puppet/" . $puppet_g, "label" => $puppet_g);
                     $product_application_description_arr[$puppet_g] = $puppet_info;
                 }
             }
         }
     }
     // show available images or private images which are enabled
     $img = $this->openqrm->image();
     $image_list = array();
     $image_list_tmp = array();
     $image_list_tmp = $img->get_list();
     // remove the openqrm + idle image from the list
     array_shift($image_list_tmp);
     array_shift($image_list_tmp);
     // check if private image feature is enabled
     $show_private_image = $this->cloudconfig->get_value_by_key('show_private_image');
     // show_private_image
     if (!strcmp($show_private_image, "true")) {
         // private image feature enabled
         $private_image_list = $this->cloudprivateimage->get_all_ids();
         foreach ($private_image_list as $index => $cpi) {
             $cpi_id = $cpi["co_id"];
             $this->cloudprivateimage->get_instance_by_id($cpi_id);
             if ($this->clouduser->id == $this->cloudprivateimage->cu_id) {
                 $img = $this->openqrm->image();
                 $img->get_instance_by_id($this->cloudprivateimage->image_id);
                 // do not show active images
                 if ($img->isactive == 1) {
                     continue;
                 }
                 // only show the non-shared image to the user if it is not attached to a resource
                 // because we don't want users to assign the same image to two appliances
                 $priv_cloud_im = new cloudimage();
                 $priv_cloud_im->get_instance_by_image_id($this->cloudprivateimage->image_id);
                 if ($priv_cloud_im->resource_id == 0 || $priv_cloud_im->resource_id == -1) {
                     // get virtualization tag
                     $str = $this->__get_virt_tag($img, $virt_types);
                     // only show images for available virtualizations
                     if ($str !== '') {
                         $image_list[] = array("value" => $img->id, "label" => $img->name . ' ' . $str);
                         $js_images[] = array($img->id, $img->name, $str);
                     }
                 }
             } else {
                 if ($this->cloudprivateimage->cu_id == 0) {
                     $img = $this->openqrm->image();
                     $img->get_instance_by_id($this->cloudprivateimage->image_id);
                     if ($img->isactive == 1) {
                         continue;
                     }
                     // get virtualization tag
                     $str = $this->__get_virt_tag($img, $virt_types);
                     // only show images for available virtualizations
                     if ($str !== '') {
                         $image_list[] = array("value" => $img->id, "label" => $img->name . ' ' . $str);
                         $js_images[] = array($img->id, $img->name, $str);
                     }
                 }
             }
         }
     } else {
         // private image feature is not enabled
         // do not show the image-clones from other requests
         foreach ($image_list_tmp as $list) {
             $iname = $list['label'];
             $iid = $list['value'];
             $img = $this->openqrm->image();
             $img->get_instance_by_id($iid);
             // do not show active images
             if ($img->isactive == 1) {
                 continue;
             }
             if (!strstr($iname, ".cloud_")) {
                 // get virtualization tag
                 $str = $this->__get_virt_tag($img, $virt_types);
                 // only show images for available virtualizations
                 if ($str !== '') {
                     $image_list[] = array("value" => $iid, "label" => $iname . ' ' . $str);
                     $js_images[] = array($img->id, $img->name, $str);
                 }
             }
         }
     }
     // check ip-mgmt
     $show_ip_mgmt = $this->cloudconfig->get_value_by_key('ip-management');
     // ip-mgmt enabled ?
     $ip_mgmt_select = '';
     $ip_mgmt_title = '';
     $ip_mgmt_list_per_user_arr[] = array("value" => -2, "label" => "Auto");
     $ip_mgmt_list_per_user_arr[] = array("value" => -1, "label" => "None");
     if (!strcmp($show_ip_mgmt, "true")) {
         if (file_exists($this->rootdir . "/plugins/ip-mgmt/.running")) {
             require_once $this->rootdir . "/plugins/ip-mgmt/class/ip-mgmt.class.php";
             $ip_mgmt = new ip_mgmt();
             $ip_mgmt_list_per_user = $ip_mgmt->get_list_by_user($this->clouduser->cg_id);
             foreach ($ip_mgmt_list_per_user as $list) {
                 $ip_mgmt_id = $list['ip_mgmt_id'];
                 $ip_mgmt_name = trim($list['ip_mgmt_name']);
                 $ip_mgmt_address = trim($list['ip_mgmt_address']);
                 $ip_mgmt_list_per_user_arr[] = array("value" => $ip_mgmt_id, "label" => $ip_mgmt_address . ' (' . $ip_mgmt_name . ')');
             }
         }
     }
     // check if cloud_selector feature is enabled
     $cloud_appliance_hostname = '';
     $cloud_appliance_hostname_input = '';
     $cloud_appliance_hostname_help = '';
     $cloud_appliance_hostname_enabled = $this->cloudconfig->get_value_by_key('appliance_hostname');
     // appliance_hostname
     if (!strcmp($cloud_appliance_hostname_enabled, "true")) {
         $cloud_appliance_hostname = 'Hostname setup';
         $cloud_appliance_hostname_help = '<small>Multiple appliances get the postfix <b>_[#no]</b></small>';
     }
     $cloud_memory_select_arr = array();
     if (isset($available_memtotal)) {
         $cloud_memory_select_arr = $available_memtotal;
     }
     $cloud_disk_select_arr = array();
     if (isset($disk_size_select)) {
         $cloud_disk_select_arr = $disk_size_select;
     }
     // Sort Image List
     if (count($image_list) > 0) {
         foreach ($image_list as $key => $row) {
             $label[$key] = strtolower($row['label']);
         }
         array_multisort($label, SORT_ASC, SORT_STRING, $image_list);
     }
     $cloud_image_select_arr = $image_list;
     $cloud_virtualization_select_arr = $virtualization_list_select;
     $cloud_cpu_select_arr = $available_cpunumber;
     $cloud_network_select_arr = $max_network_interfaces_select;
     #$cloud_ha_select_arr = $show_ha;
     $cloud_kernel_select_arr = $kernel_list;
     $d = array();
     $d['cloud_virtualization_select']['label'] = $this->lang['type'];
     $d['cloud_virtualization_select']['required'] = true;
     $d['cloud_virtualization_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_virtualization_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_virtualization_select']['object']['attrib']['id'] = 'cloud_virtualization_select';
     $d['cloud_virtualization_select']['object']['attrib']['name'] = 'cloud_virtualization_select';
     $d['cloud_virtualization_select']['object']['attrib']['options'] = $cloud_virtualization_select_arr;
     if (isset($this->cloudprofile->resource_type_req)) {
         $d['cloud_virtualization_select']['object']['attrib']['selected'] = array($this->cloudprofile->resource_type_req);
     }
     $d['cloud_kernel_select']['label'] = $this->lang['kernel'];
     $d['cloud_kernel_select']['required'] = true;
     $d['cloud_kernel_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_kernel_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_kernel_select']['object']['attrib']['id'] = 'cloud_kernel_select';
     $d['cloud_kernel_select']['object']['attrib']['name'] = 'cloud_kernel_select';
     $d['cloud_kernel_select']['object']['attrib']['options'] = $cloud_kernel_select_arr;
     if (isset($this->cloudprofile->kernel_id)) {
         $d['cloud_kernel_select']['object']['attrib']['selected'] = array($this->cloudprofile->kernel_id);
     }
     $d['cloud_image_select']['label'] = $this->lang['image'];
     $d['cloud_image_select']['required'] = true;
     $d['cloud_image_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_image_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_image_select']['object']['attrib']['id'] = 'cloud_image_select';
     $d['cloud_image_select']['object']['attrib']['name'] = 'cloud_image_select';
     $d['cloud_image_select']['object']['attrib']['options'] = $cloud_image_select_arr;
     if (isset($this->cloudprofile->image_id)) {
         $d['cloud_image_select']['object']['attrib']['selected'] = array($this->cloudprofile->image_id);
     }
     $d['cloud_memory_select']['label'] = $this->lang['ram'];
     $d['cloud_memory_select']['required'] = true;
     $d['cloud_memory_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_memory_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_memory_select']['object']['attrib']['id'] = 'cloud_memory_select';
     $d['cloud_memory_select']['object']['attrib']['name'] = 'cloud_memory_select';
     $d['cloud_memory_select']['object']['attrib']['options'] = $cloud_memory_select_arr;
     if (isset($this->cloudprofile->ram_req)) {
         $d['cloud_memory_select']['object']['attrib']['selected'] = array($this->cloudprofile->ram_req);
     }
     $d['cloud_cpu_select']['label'] = $this->lang['cpu'];
     $d['cloud_cpu_select']['required'] = true;
     $d['cloud_cpu_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_cpu_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_cpu_select']['object']['attrib']['id'] = 'cloud_cpu_select';
     $d['cloud_cpu_select']['object']['attrib']['name'] = 'cloud_cpu_select';
     $d['cloud_cpu_select']['object']['attrib']['options'] = $cloud_cpu_select_arr;
     if (isset($this->cloudprofile->cpu_req)) {
         $d['cloud_cpu_select']['object']['attrib']['selected'] = array($this->cloudprofile->cpu_req);
     }
     $d['cloud_disk_select']['label'] = $this->lang['disk'];
     $d['cloud_disk_select']['required'] = true;
     $d['cloud_disk_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_disk_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_disk_select']['object']['attrib']['id'] = 'cloud_disk_select';
     $d['cloud_disk_select']['object']['attrib']['name'] = 'cloud_disk_select';
     $d['cloud_disk_select']['object']['attrib']['options'] = $cloud_disk_select_arr;
     if (isset($this->cloudprofile->disk_req)) {
         $d['cloud_disk_select']['object']['attrib']['selected'] = array($this->cloudprofile->disk_req);
     }
     $d['cloud_network_select']['label'] = $this->lang['network'];
     $d['cloud_network_select']['required'] = true;
     $d['cloud_network_select']['object']['type'] = 'htmlobject_select';
     $d['cloud_network_select']['object']['attrib']['index'] = array('value', 'label');
     $d['cloud_network_select']['object']['attrib']['id'] = 'cloud_network_select';
     $d['cloud_network_select']['object']['attrib']['name'] = 'cloud_network_select';
     $d['cloud_network_select']['object']['attrib']['options'] = $cloud_network_select_arr;
     if (isset($this->cloudprofile->network_req)) {
         $d['cloud_network_select']['object']['attrib']['selected'] = array($this->cloudprofile->network_req);
     }
     // ips
     $ip_loop = 0;
     if (count($ip_mgmt_list_per_user_arr) > 0) {
         $max = 0;
         foreach ($cloud_network_select_arr as $v) {
             if ($v['value'] > $max) {
                 $max = $v['value'];
             }
         }
         for ($i = 0; $i < $max; $i++) {
             $nic_no = $ip_loop + 1;
             $d['cloud_ip_select_' . $ip_loop]['label'] = 'IP ' . $nic_no;
             $d['cloud_ip_select_' . $ip_loop]['object']['type'] = 'htmlobject_select';
             $d['cloud_ip_select_' . $ip_loop]['object']['attrib']['index'] = array('value', 'label');
             $d['cloud_ip_select_' . $ip_loop]['object']['attrib']['id'] = 'cloud_ip_select_' . $ip_loop;
             $d['cloud_ip_select_' . $ip_loop]['object']['attrib']['name'] = 'cloud_ip_select_' . $ip_loop;
             $d['cloud_ip_select_' . $ip_loop]['object']['attrib']['options'] = $ip_mgmt_list_per_user_arr;
             if ($i === $max - 1) {
                 $d['cloud_ip_select_' . $ip_loop]['css'] = 'last';
             }
             $ip_loop++;
         }
         for ($f = $ip_loop; $f < 4; $f++) {
             $d['cloud_ip_select_' . $f] = ' ';
         }
     } else {
         for ($f = $ip_loop; $f < 4; $f++) {
             $d['cloud_ip_select_' . $f] = ' ';
         }
     }
     // application
     $apps_selected = explode(',', $this->cloudprofile->puppet_groups);
     $product_loop = 0;
     if (count($cloud_application_select_arr) > 0) {
         foreach ($cloud_application_select_arr as $application) {
             $product_name = $application['label'];
             $product_description = $application['value'];
             $d['cloud_application_select_' . $product_loop]['label'] = $product_name;
             $d['cloud_application_select_' . $product_loop]['object']['type'] = 'htmlobject_input';
             $d['cloud_application_select_' . $product_loop]['object']['attrib']['type'] = 'checkbox';
             $d['cloud_application_select_' . $product_loop]['object']['attrib']['id'] = 'cloud_application_select' . $product_loop;
             $d['cloud_application_select_' . $product_loop]['object']['attrib']['name'] = 'cloud_application_select_' . $product_loop;
             $d['cloud_application_select_' . $product_loop]['object']['attrib']['value'] = $product_description;
             $d['cloud_application_select_' . $product_loop]['object']['attrib']['title'] = $product_application_description_arr[$product_name];
             if (in_array($product_description, $apps_selected)) {
                 $d['cloud_application_select_' . $product_loop]['object']['attrib']['checked'] = true;
             }
             $product_loop++;
         }
         for ($f = $product_loop; $f < $this->cloud_max_applications; $f++) {
             $d['cloud_application_select_' . $f] = ' ';
         }
     } else {
         for ($f = $product_loop; $f < $this->cloud_max_applications; $f++) {
             $d['cloud_application_select_' . $f] = ' ';
         }
     }
     // ha
     $ha = false;
     $d['cloud_ha_select'] = '';
     if ($this->cloudconfig->get_value_by_key('cloud_selector') === 'true') {
         if (count($this->cloudselector->display_overview_per_type("ha")) > 0) {
             $ha = true;
         }
     } else {
         if ($this->cloudconfig->get_value_by_key('show_ha_checkbox') === 'true') {
             $ha = true;
         }
     }
     if ($ha === true) {
         $d['cloud_ha_select']['label'] = $this->lang['ha'];
         $d['cloud_ha_select']['object']['type'] = 'htmlobject_input';
         $d['cloud_ha_select']['object']['attrib']['type'] = 'checkbox';
         $d['cloud_ha_select']['object']['attrib']['id'] = 'cloud_ha_select';
         $d['cloud_ha_select']['object']['attrib']['name'] = 'cloud_ha_select';
         $d['cloud_ha_select']['object']['attrib']['value'] = 'ha';
         if ($this->cloudprofile->ha_req === '1') {
             $d['cloud_ha_select']['object']['attrib']['checked'] = true;
         }
     }
     // capabilities
     $d['cloud_appliance_capabilities']['label'] = $this->lang['capabilities'];
     $d['cloud_appliance_capabilities']['object']['type'] = 'htmlobject_textarea';
     $d['cloud_appliance_capabilities']['object']['attrib']['id'] = 'cloud_appliance_capabilities';
     $d['cloud_appliance_capabilities']['object']['attrib']['name'] = 'cloud_appliance_capabilities';
     $d['cloud_appliance_capabilities']['object']['attrib']['maxlength'] = 1000;
     $d['cloud_appliance_capabilities']['object']['attrib']['value'] = $this->cloudprofile->appliance_capabilities;
     // check if user are allowed to set the hostname
     $d['cloud_hostname_input'] = '';
     if ($this->cloudconfig->get_value_by_key('appliance_hostname') === "true") {
         $d['cloud_hostname_input']['label'] = $this->lang['hostname'];
         $d['cloud_hostname_input']['validate']['regex'] = $this->openqrm->get('regex', 'hostname');
         $d['cloud_hostname_input']['validate']['errormsg'] = 'Hostname must be ' . $this->openqrm->get('regex', 'hostname') . ' only';
         $d['cloud_hostname_input']['object']['type'] = 'htmlobject_input';
         $d['cloud_hostname_input']['object']['attrib']['type'] = 'text';
         $d['cloud_hostname_input']['object']['attrib']['id'] = 'cloud_hostname_input';
         $d['cloud_hostname_input']['object']['attrib']['name'] = 'cloud_hostname_input';
         $d['cloud_hostname_input']['object']['attrib']['maxlength'] = 255;
         $d['cloud_hostname_input']['object']['attrib']['value'] = $this->cloudprofile->appliance_hostname;
     }
     // save as profile
     $d['cloud_profile_name']['label'] = $this->lang['save_as_profile'];
     $d['cloud_profile_name']['required'] = false;
     $d['cloud_profile_name']['validate']['regex'] = '~^[a-z0-9]+$~i';
     $d['cloud_profile_name']['validate']['errormsg'] = sprintf($this->lang['error_save_as_profile'], '[a-z0-9]');
     $d['cloud_profile_name']['object']['type'] = 'htmlobject_input';
     $d['cloud_profile_name']['object']['attrib']['type'] = 'text';
     $d['cloud_profile_name']['object']['attrib']['id'] = 'cloud_profile_name';
     $d['cloud_profile_name']['object']['attrib']['name'] = 'cloud_profile_name';
     $d['cloud_profile_name']['object']['attrib']['maxlength'] = 15;
     $str = 'var formbuilder = {' . "\n";
     $str .= 'resources:[' . "\n";
     $i = 1;
     if (isset($js_resources)) {
         foreach ($js_resources as $k => $v) {
             $str .= '[' . $v[0] . ',"' . $v[1] . '","' . $v[2] . '","' . $v[3] . '"]';
             if ($i < count($js_resources)) {
                 $str .= ',' . "\n";
             }
             $i++;
         }
     }
     $str .= '],' . "\n";
     $str .= 'images:[' . "\n";
     $i = 1;
     if (isset($js_images)) {
         // Sort Image List
         if (count($js_images) > 0) {
             $label = array();
             foreach ($js_images as $key => $row) {
                 $label[$key] = $row[1];
             }
             array_multisort($label, SORT_ASC, SORT_STRING, $js_images);
         }
         // build json
         foreach ($js_images as $k => $v) {
             $str .= '[' . $v[0] . ',"' . $v[1] . '","' . $v[2] . '"]';
             if ($i < count($js_images)) {
                 $str .= ',' . "\n";
             }
             $i++;
         }
     }
     $str .= ']' . "\n";
     $str .= '};' . "\n";
     $this->js_resources = $str;
     $form->add($d);
     // check profiles
     $profile = $this->response->html->request()->get('profile');
     if ($profile !== '') {
         $profile = $this->cloudprofile->name;
         if (isset($this->cloudprofile->resource_type_req)) {
             $p = false;
             foreach ($cloud_virtualization_select_arr as $v) {
                 if ($v['value'] === $this->cloudprofile->resource_type_req) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_virtualization_select', sprintf($this->lang['error_profile'], $this->lang['type'], $profile));
             }
         }
         if (isset($this->cloudprofile->image_id)) {
             $p = false;
             foreach ($cloud_image_select_arr as $v) {
                 if ($v['value'] === $this->cloudprofile->image_id) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_image_select', sprintf($this->lang['error_profile'], $this->lang['image'], $profile));
             }
         }
         if (isset($this->cloudprofile->kernel_id)) {
             $p = false;
             foreach ($cloud_kernel_select_arr as $v) {
                 if ($v['value'] === $this->cloudprofile->kernel_id) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_kernel_select', sprintf($this->lang['error_profile'], $this->lang['kernel'], $profile));
             }
         }
         if (isset($this->cloudprofile->disk_req)) {
             $p = false;
             foreach ($cloud_disk_select_arr as $v) {
                 if ($v['value'] == $this->cloudprofile->disk_req) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_disk_select', sprintf($this->lang['error_profile'], $this->lang['disk'] . ' (' . $this->cloudprofile->disk_req . ' MB)', $profile));
             }
         }
         if (isset($this->cloudprofile->ram_req)) {
             $p = false;
             foreach ($cloud_memory_select_arr as $v) {
                 if ($v['value'] == $this->cloudprofile->ram_req) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_memory_select', sprintf($this->lang['error_profile'], $this->lang['ram'] . ' (' . $this->cloudprofile->ram_req . ' MB)', $profile));
             }
         }
         if (isset($this->cloudprofile->cpu_req)) {
             $p = false;
             foreach ($cloud_cpu_select_arr as $v) {
                 if ($v['value'] == $this->cloudprofile->cpu_req) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_cpu_select', sprintf($this->lang['error_profile'], $this->lang['cpu'] . ' (' . $this->cloudprofile->cpu_req . ')', $profile));
             }
         }
         if (isset($this->cloudprofile->network_req)) {
             $p = false;
             foreach ($cloud_network_select_arr as $v) {
                 if ($v['value'] == $this->cloudprofile->network_req) {
                     $p = true;
                     break;
                 }
             }
             if ($p === false) {
                 $form->set_error('cloud_network_select', sprintf($this->lang['error_profile'], $this->lang['network'] . ' (' . $this->cloudprofile->network_req . ')', $profile));
             }
         }
     }
     $form->display_errors = false;
     $response->form = $form;
     return $response;
 }