Ejemplo n.º 1
0
 public function action_duplicate()
 {
     $val = \Validation::forge();
     $val->add_field('tid', 'Template id', 'required|min_length[1]|max_length[25]');
     $val->add_field('val', 'new duplicate name', 'min_length[0]');
     if ($val->run()) {
         $json = array();
         $template = Model_Device_Template::find($val->validated('tid'));
         $iscopy = Model_Device_Template::find()->where('name', $val->validated('val'))->get_one();
         if ($iscopy) {
             $status = 'Template with this name already exist!';
         } else {
             $status = 'Strange error!';
             if ($template) {
                 $status = 'ok';
                 //device_template
                 $prop = array('name' => $val->validated('val'), 'categoryID' => $template->categoryID, 'hidden' => 0, 'meta_update_user' => $this->user, 'rack_unit' => 0);
                 $new = new Model_Device_Template($prop);
                 $new->save();
                 //get device_temp_field
                 foreach ($template->field as $field) {
                     //basic data for field
                     $prop = array('templateID' => $new->id, 'tab' => $field->tab, 'name' => $field->name, 'type' => $field->type, 'static' => $field->static, 'value' => $field->value, 'extra' => $field->extra);
                     //insert into temp field
                     $new_field = new Model_Device_Template_Field($prop);
                     $new_field->save();
                     #hdd element data
                     #####################################################################################
                     if ($field->type == 'hdd') {
                         //get raid data
                         $old_raid = Model_Raid::find()->where('tempfieldID', $field->id)->get_one();
                         //create new hardware_raid
                         $prop = array('raid_type' => $old_raid->raid_type, 'size' => $old_raid->size, 'total' => $old_raid->total, 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'tempfieldID' => $new_field->id);
                         $hdd = new Model_Raid($prop);
                         $hdd->save();
                         //get all discs from array
                         foreach ($old_raid->rows as $disc) {
                             $m = array('model' => $disc->model, 'size' => $disc->size, 'vport' => $disc->vport, 'serial_number' => $disc->serial_number, 'hardware_raid' => $hdd->id, 'meta_update_time' => time(), 'meta_update_user' => $this->user);
                             $discs = new Model_Raid_Data($m);
                             $discs->save();
                         }
                     }
                     //ram
                     #####################################################################################
                     if ($field->type == 'ram') {
                         //get ram data
                         $old_ram = Model_Ram::find()->where('tempfieldID', $field->id)->get_one();
                         //create new hardware_ram
                         $prop = array('ram_type' => $old_ram->ram_type, 'size' => $old_ram->size, 'total' => $old_ram->total, 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'tempfieldID' => $new_field->id);
                         $ram = new Model_Ram($prop);
                         $ram->save();
                         //get all modules from array
                         foreach ($old_ram->rows as $modul) {
                             $m = array('hardware_ram' => $ram->id, 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'model' => $modul->model, 'size' => $modul->size, 'port' => $modul->port, 'serial_number' => $modul->serial_number);
                             $rams = new Model_Ram_Data($m);
                             $rams->save();
                         }
                     }
                     //images
                     #####################################################################################
                     if ($field->type == 'img') {
                         //get field images
                         $temp_imags = Model_Device_Template_Image::find()->where('elementID', $field->id)->get();
                         foreach ($temp_imags as $img) {
                             $props = array('name' => $img->name, 'elementID' => $new_field->id, 'type' => $img->type, 'width' => $img->width, 'height' => $img->height);
                             $imgs = new Model_Device_Template_Image($props);
                             $imgs->save();
                             copy(DOCROOT . 'images/temp' . $img->id . '.png', DOCROOT . 'images/temp' . $imgs->id . '.png');
                             copy(DOCROOT . 'images/tumb/temp' . $img->id . '.png', DOCROOT . 'images/tumb/temp' . $imgs->id . '.png');
                         }
                     }
                     //power out (PDU,ATS,UPS)
                     #####################################################################################
                     if ($field->type == 'power_out') {
                         //power is set automatic as field saved
                         $power = Model_Device_Template_Power::find()->where('fieldsetID', $new_field->id)->get_one();
                         $extra = $field->power;
                         //update default data with template power data
                         $power->input = $extra->input;
                         $power->output = $extra->output;
                         $power->type = $extra->type;
                         $power->current = $extra->current;
                         $power->ru = $extra->ru;
                         $power->pos = $extra->pos;
                         $power->save();
                         //delete any default socket
                         $defsockets = Model_Device_Template_Power_Socket::find()->where('powerID', $power->id)->get();
                         foreach ($defsockets as $ds) {
                             $ds->delete();
                         }
                         foreach ($field->power->socket as $socket) {
                             $prop = array('powerID' => $power->id, 'conn_type' => $socket->conn_type, 'type' => $socket->type);
                             $newsocket = new Model_Device_Template_Power_Socket($prop);
                             $newsocket->save();
                         }
                     }
                     //power in (Server,Switch,Router,...)
                     #####################################################################################
                     if ($field->type == 'power_in') {
                         //power is set automatic as field saved
                         $power = Model_Device_Template_Power::find()->where('fieldsetID', $new_field->id)->get_one();
                         $extra = $field->power;
                         //update default data with template power data
                         $power->input = $extra->input;
                         $power->output = $extra->output;
                         $power->type = $extra->type;
                         $power->current = $extra->current;
                         $power->ru = $extra->ru;
                         $power->pos = $extra->pos;
                         $power->save();
                         //delete any default socket
                         $defsockets = Model_Device_Template_Power_Socket::find()->where('powerID', $power->id)->get();
                         foreach ($defsockets as $ds) {
                             $ds->delete();
                         }
                         foreach ($field->power->socket as $socket) {
                             $prop = array('powerID' => $power->id, 'conn_type' => $socket->conn_type, 'type' => $socket->type);
                             $newsocket = new Model_Device_Template_Power_Socket($prop);
                             $newsocket->save();
                         }
                     }
                     //kvm out (KVM switch)
                     #####################################################################################
                     if ($field->type == 'kvm_out') {
                         //power is set automatic as field saved
                         $kvm = Model_Device_Template_Kvm::find()->where('fieldsetID', $new_field->id)->get_one();
                         $extra = $field->kvm;
                         //update default data with template power data
                         $kvm->input = $extra->input;
                         $kvm->output = $extra->output;
                         $kvm->type = $extra->type;
                         $kvm->save();
                         //delete any default socket
                         $defsockets = Model_Device_Template_Kvm_Socket::find()->where('kvmID', $kvm->id)->get();
                         foreach ($defsockets as $ds) {
                             $ds->delete();
                         }
                         foreach ($field->kvm->socket as $socket) {
                             $prop = array('kvmID' => $kvm->id, 'conn_type' => $socket->conn_type, 'type' => $socket->type);
                             $newsocket = new Model_Device_Template_Kvm_Socket($prop);
                             $newsocket->save();
                         }
                     }
                     //KVM in (Server,Switch,Router,...)
                     #####################################################################################
                     if ($field->type == 'kvm_in') {
                         //kvm is set automatic as field saved
                         $kvm = Model_Device_Template_Kvm::find()->where('fieldsetID', $new_field->id)->get_one();
                         $extra = $field->kvm;
                         //update default data with template kvm data
                         $kvm->input = $extra->input;
                         $kvm->output = $extra->output;
                         $kvm->type = $extra->type;
                         $kvm->save();
                         //delete any default socket
                         $defsockets = Model_Device_Template_Kvm_Socket::find()->where('kvmID', $kvm->id)->get();
                         foreach ($defsockets as $ds) {
                             $ds->delete();
                         }
                         foreach ($field->kvm->socket as $socket) {
                             $prop = array('kvmID' => $kvm->id, 'conn_type' => $socket->conn_type, 'type' => $socket->type);
                             $newsocket = new Model_Device_Template_Kvm_Socket($prop);
                             $newsocket->save();
                         }
                     }
                     //network
                     #####################################################################################
                     if ($field->type == 'network') {
                         //newtwork is set automatic as field saved
                         $network = Model_Device_Template_Network::find()->where('fieldID', $new_field->id)->get_one();
                         $extra = $field->network;
                         //update default data with old template network data
                         $network->type = $extra->type;
                         $network->nics = $extra->nics;
                         $network->vports = $extra->vports;
                         $network->ports = $extra->ports;
                         $network->uplinks = $extra->uplinks;
                         $network->save();
                         switch ($network->type) {
                             case 1:
                                 //IP addresses
                                 foreach ($field->network->ip as $ip) {
                                     $prop = array('tempnetID' => $network->id, 'nic_name' => $ip->nic_name, 'ipv4' => $ip->ipv4, 'ipv6' => $ip->ipv6, 'conn_type' => $ip->conn_type, 'conn_speed' => $ip->conn_speed, 'type' => $ip->type);
                                     $ips = new Model_Device_Template_Network_Ip($prop);
                                     $ips->save();
                                 }
                                 break;
                                 //MAC and VLAN
                             //MAC and VLAN
                             case 2:
                                 $vlan2port = array();
                                 //if no vlan is set to mac
                                 $vlan2port[0] = 0;
                                 foreach ($field->network->vlan as $vlan) {
                                     $prop = array('tempnetID' => $network->id, 'name' => $vlan->name);
                                     $vlans = new Model_Device_Template_Network_Vlan($prop);
                                     $vlans->save();
                                     $vlan2port[$vlan->id] = $vlans->id;
                                 }
                                 foreach ($field->network->mac as $mac) {
                                     $prop = array('tempnetID' => $network->id, 'mac_address' => $mac->mac_address, 'conn_device' => $mac->conn_device, 'vlan' => $vlan2port[$mac->vlan], 'type' => $mac->type);
                                     $macs = new Model_Device_Template_Network_Mac($prop);
                                     $macs->save();
                                 }
                                 break;
                                 //patch panels
                             //patch panels
                             case 3:
                                 $vlan2port = array();
                                 //if no vlan is set to mac
                                 $vlan2port[0] = 0;
                                 foreach ($field->network->vlan as $vlan) {
                                     $prop = array('tempnetID' => $network->id, 'name' => $vlan->name);
                                     $vlans = new Model_Device_Template_Network_Vlan($prop);
                                     $vlans->save();
                                     $vlan2port[$vlan->id] = $vlans->id;
                                 }
                                 foreach ($field->network->mac as $mac) {
                                     $prop = array('tempnetID' => $network->id, 'mac_address' => $mac->mac_address, 'conn_device' => $mac->conn_device, 'vlan' => $mac->vlan, 'type' => $mac->type);
                                     $macs = new Model_Device_Template_Network_Mac($prop);
                                     $macs->save();
                                 }
                                 break;
                         }
                     }
                 }
                 $d = array();
                 $d['templates'] = $new;
                 $d['import'] = true;
                 $d['alltemplates'] = Model_Device_Template::find()->where('hidden', 0)->where('categoryID', $template->categoryID)->get();
                 $d['category'] = $template->categoryID;
                 $json = json_decode(\View::forge('template/data', $d));
             }
         }
     }
     echo json_encode(array('status' => $status, 'data' => $json));
 }
Ejemplo n.º 2
0
 public function action_index()
 {
     ob_start();
     $out = array('status' => 'no');
     $uploaddir = DOCROOT . 'images/uploads/';
     $filename = $_FILES['image']['tmp_name'];
     $zip = new \ZipArchive();
     if ($zip->open($filename) === true) {
         $json = $zip->getFromName('data.json');
         $data = json_decode($json);
         //check if that template already exist
         $istempl = Model_Device_Template::find()->where('name', $data->title)->where('categoryID', $data->category)->get_one();
         if (!$istempl) {
             $prop = array('name' => $data->title, 'categoryID' => $data->category, 'hidden' => 0, 'meta_update_user' => $this->user, 'rack_unit' => 1);
             $template = new Model_Device_Template($prop);
             $template->save();
             foreach ($data->items as $item) {
                 //basic data for field
                 $prop = array('templateID' => $template->id, 'tab' => $item->tab, 'name' => $item->name, 'type' => $item->element, 'static' => $item->static, 'extra' => htmlspecialchars_decode($item->extra), 'value' => htmlspecialchars_decode($item->value));
                 //echo $item->value;
                 //insert into temp field
                 $field = new Model_Device_Template_Field($prop);
                 $field->save();
                 //hdd element data
                 if ($item->element == 'hdd') {
                     //create new hardware_raid
                     $prop = array('raid_type' => $item->raid_type, 'size' => $item->size, 'total' => floor($item->total), 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'tempfieldID' => $field->id);
                     $hdd = new Model_Raid($prop);
                     $hdd->save();
                     //get all discs from array
                     foreach ($item->items as $disc) {
                         $m = array('model' => $disc->model, 'size' => $disc->size, 'vport' => $disc->vport, 'serial_number' => $disc->serial_number, 'hardware_raid' => $hdd->id, 'meta_update_time' => time(), 'meta_update_user' => $this->user);
                         $discs = new Model_Raid_Data($m);
                         $discs->save();
                     }
                 }
                 //ram element data
                 if ($item->element == 'ram') {
                     //create new hardware_ram
                     $prop = array('ram_type' => $item->ram_type, 'size' => $item->size, 'total' => floor($item->total), 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'tempfieldID' => $field->id);
                     $ram = new Model_Ram($prop);
                     $ram->save();
                     //get all modules from array
                     foreach ($item->items as $modul) {
                         $m = array('hardware_ram' => $ram->id, 'meta_update_time' => time(), 'meta_update_user' => $this->user, 'model' => $modul->model, 'size' => $modul->size, 'port' => $modul->port, 'serial_number' => $modul->serial_number);
                         $rams = new Model_Ram_Data($m);
                         $rams->save();
                     }
                 }
                 //images element
                 if ($item->element == 'img') {
                     //import images from zip
                     foreach ($item->items as $img) {
                         $props = array('name' => 'none', 'elementID' => $field->id, 'type' => $img->type, 'width' => $img->w, 'height' => $img->h);
                         $imgs = new Model_Device_Template_Image($props);
                         $imgs->save();
                         copy('zip://' . $filename . '#images/' . $img->id . '.png', DOCROOT . 'images/temp' . $imgs->id . '.png');
                         copy('zip://' . $filename . '#images/tumb/' . $img->id . '.png', DOCROOT . 'images/tumb/temp' . $imgs->id . '.png');
                     }
                 }
                 //network element data
                 if ($item->element == 'none') {
                     switch ($item->special) {
                         //ip ports for server
                         case 'ipfield':
                             $field->type = 'network';
                             $field->save();
                             $extra = $item->extra;
                             $network = Model_Device_Template_Network::find()->where('fieldID', $field->id)->get_one();
                             $network->type = $extra->type;
                             $network->nics = $extra->nics;
                             $network->vports = $extra->vports;
                             $network->ports = $extra->ports;
                             $network->uplinks = $extra->uplinks;
                             $network->save();
                             foreach ($item->data as $ips) {
                                 $ip = $ips->port;
                                 $prop = array('tempnetID' => $network->id, 'nic_name' => $ip->name, 'ipv4' => $ip->ipv4, 'ipv6' => $ip->ipv6, 'conn_type' => $ip->conn_type, 'conn_speed' => $ip->conn_speed, 'type' => $ip->type);
                                 $ips = new Model_Device_Template_Network_Ip($prop);
                                 $ips->save();
                             }
                             break;
                             // mac for switch, router
                         // mac for switch, router
                         case 'macfield':
                             $field->type = 'network';
                             $field->save();
                             $extra = $item->extra;
                             $network = Model_Device_Template_Network::find()->where('fieldID', $field->id)->get_one();
                             $network->type = $extra->type;
                             $network->nics = $extra->nics;
                             $network->vports = $extra->vports;
                             $network->ports = $extra->ports;
                             $network->uplinks = $extra->uplinks;
                             $network->save();
                             $vlan2port = array();
                             //if no vlan is set to mac
                             $vlan2port[0] = 0;
                             //vlans
                             foreach ($item->vlans as $vlan) {
                                 $prop = array('tempnetID' => $network->id, 'name' => $vlan->name);
                                 $vlans = new Model_Device_Template_Network_Vlan($prop);
                                 $vlans->save();
                                 $vlan2port[$vlan->id] = $vlans->id;
                             }
                             //print_r($item->data);
                             //mac addresses
                             foreach ($item->data as $mac) {
                                 $prop = array('tempnetID' => $network->id, 'mac_address' => $mac->mac_addr, 'conn_device' => $mac->conn_dev, 'vlan' => $vlan2port[$mac->vlan], 'type' => $mac->type);
                                 $macs = new Model_Device_Template_Network_Mac($prop);
                                 $macs->save();
                             }
                             break;
                         case 'powerfield':
                             $field->type = 'power_out';
                             $field->save();
                             $extra = $item->data;
                             $power = Model_Device_Template_Power::find()->where('fieldsetID', $field->id)->get_one();
                             //update device power field
                             $power->input = $extra->input;
                             $power->current = $extra->current;
                             $power->output = $extra->output;
                             $power->save();
                             //delete default stored sockets
                             foreach ($power->socket as $tsoc) {
                                 $tsoc->delete();
                             }
                             //loop from exportet socket, put them into database
                             foreach ($extra->sockets as $socket) {
                                 $prop = array('powerID' => $power->id, 'type' => $socket->type, 'conn_type' => $socket->conn_type);
                                 $soc = new Model_Device_Template_Power_Socket($prop);
                                 $soc->save();
                             }
                             break;
                         case 'inpowerfield':
                             $field->type = 'power_in';
                             $field->save();
                             $extra = $item->data;
                             $power = Model_Device_Template_Power::find()->where('fieldsetID', $field->id)->get_one();
                             //update device power field
                             $power->input = $extra->input;
                             $power->save();
                             //delete default stored sockets
                             foreach ($power->socket as $tsoc) {
                                 $tsoc->delete();
                             }
                             //loop from exportet socket, put them into database
                             foreach ($extra->sockets as $socket) {
                                 $prop = array('powerID' => $power->id, 'type' => $socket->type, 'conn_type' => $socket->conn_type);
                                 $soc = new Model_Device_Template_Power_Socket($prop);
                                 $soc->save();
                             }
                             break;
                         case 'panelfield':
                             $field->type = 'network';
                             $field->save();
                             $extra = $item->extra;
                             $network = Model_Device_Template_Network::find()->where('fieldID', $field->id)->get_one();
                             $network->type = $extra->type;
                             $network->nics = $extra->nics;
                             $network->vports = $extra->vports;
                             $network->ports = $extra->ports;
                             $network->uplinks = $extra->uplinks;
                             $network->save();
                             //print_r($item->data);
                             //mac addresses
                             foreach ($item->data as $mac) {
                                 $prop = array('tempnetID' => $network->id, 'mac_address' => $mac->mac_addr, 'conn_device' => $mac->conn_dev, 'vlan' => $mac->vlan, 'type' => $mac->type);
                                 $macs = new Model_Device_Template_Network_Mac($prop);
                                 $macs->save();
                             }
                             break;
                         case 'inkvmfield':
                             $field->type = 'kvm_in';
                             $field->save();
                             $extra = $item->data;
                             $kvm = Model_Device_Template_Kvm::find()->where('fieldsetID', $field->id)->get_one();
                             //update device power field
                             $kvm->input = $extra->input;
                             $kvm->output = 0;
                             $kvm->save();
                             if ($kvm) {
                                 //delete default stored sockets
                                 $defSoc = Model_Device_Template_Kvm_Socket::find()->where('kvmID', $kvm->id)->get();
                                 foreach ($defSoc as $tsoc) {
                                     $tsoc->delete();
                                 }
                                 //loop from exportet socket, put them into database
                                 $esoc = $extra->sockets;
                                 foreach ($esoc as $socket) {
                                     $prop = array('kvmID' => $kvm['id'], 'type' => $socket->type, 'conn_type' => $socket->conn_type);
                                     $soc = new Model_Device_Template_Kvm_Socket($prop);
                                     $soc->save();
                                 }
                             }
                             break;
                         case 'kvmfield':
                             $field->type = 'kvm_out';
                             $field->save();
                             $extra = $item->data;
                             $kvm = Model_Device_Template_Kvm::find()->where('fieldsetID', $field->id)->get_one();
                             //update device power field
                             $kvm->input = $extra->input;
                             $kvm->output = $extra->output;
                             $kvm->save();
                             //delete default stored sockets
                             foreach ($kvm->socket as $tsoc) {
                                 $tsoc->delete();
                             }
                             //loop from exportet socket, put them into database
                             foreach ($extra->sockets as $socket) {
                                 $prop = array('kvmID' => $kvm->id, 'type' => $socket->type, 'conn_type' => $socket->conn_type);
                                 $soc = new Model_Device_Template_Kvm_Socket($prop);
                                 $soc->save();
                             }
                             break;
                     }
                 }
             }
             ob_end_clean();
             $d = array();
             $d['templates'] = Model_Device_Template::find($template->id);
             $d['import'] = true;
             $d['alltemplates'] = Model_Device_Template::find()->where('hidden', 0)->where('categoryID', $template->categoryID)->get();
             $d['category'] = $template->categoryID;
             echo $json = \View::forge('template/data', $d);
         } else {
             ob_end_clean();
             echo json_encode(array('err' => 'template already exist!'));
         }
         $zip->close();
     }
     unlink($filename);
 }