Example #1
0
 protected function conn_device($id)
 {
     if ($id == 0) {
         return '-';
     }
     $dev = Model_Device::find($id);
     if ($dev) {
         return $dev->hostname;
     }
     return '-';
 }
Example #2
0
 /**
  * 
  * Update database update mac/conn device/ vlan/ configdata
  */
 public function action_index()
 {
     if (isset($this->field)) {
         $out['status'] = 'ok';
         $val = \Validation::forge();
         $val->add_field('act', 'action id', 'required|min_length[1]|max_length[20]');
         $val->add_field('val', 'value', 'required|min_length[1]');
         $val->add_field('mid', 'mac port id', 'required|min_length[1]|max_length[20]');
         if ($val->run()) {
             $new_val = $val->validated('val');
             if ($this->tmpl) {
                 $macport = Model_Device_Template_Network_Mac::find($val->validated('mid'));
             } else {
                 $macport = Model_Network_Mac::find($val->validated('mid'));
             }
             if ($macport) {
                 switch ($val->validated('act')) {
                     case 'macset':
                         if ($this->isValidMAC($new_val)) {
                             $macport->mac_address = $new_val;
                         } else {
                             $out['old'] = $macport->mac_address;
                             $out['status'] = 'no';
                         }
                         break;
                     case 'connect':
                         $dev = Model_Device::find($new_val);
                         if ($dev) {
                             $macport->conn_device = $dev->id;
                         }
                         break;
                     case 'disconnect':
                         $macport->conn_device = 0;
                         break;
                     case 'vlanset':
                         if ($new_val == 0) {
                             $macport->vlan = 0;
                         } else {
                             if ($this->tmpl) {
                                 $vlan = Model_Device_Template_Network_Vlan::find($new_val);
                             } else {
                                 $vlan = Model_Network_Vlan::find($new_val);
                             }
                             $macport->vlan = $vlan->id;
                         }
                         break;
                 }
                 $macport->save();
             }
         }
         echo json_encode($out);
     }
 }
Example #3
0
 public function action_new()
 {
     $a = array('stat' => 'no');
     if ($_POST) {
         $this->val->add_field('tid', 'template id', 'required|min_length[1]|max_length[20]');
         $this->val->add_field('name', 'element name', 'required|min_length[1]|max_length[250]');
         $this->val->add_field('type', 'element type', 'required|min_length[1]|max_length[20]');
         $this->val->add_field('tab', 'window tab', 'required|min_length[1]|max_length[20]');
         if ($this->val->run()) {
             $device = Model_Device::find($this->val->validated('tid'));
             if ($device) {
                 $field_props = array('type' => $this->elements($this->val->validated('type')), 'name' => $this->val->validated('name'), 'static' => 0, 'deviceID' => $device->id, 'tab' => $this->val->validated('tab'));
                 $field = new Model_Device_Fieldset($field_props);
                 $field->save();
                 $a = array('id' => $field->id);
             }
         }
     }
     echo json_encode($a);
 }
Example #4
0
 private function check_boundary($pdu, $size)
 {
     //template dont have boundary
     if ($this->tmpl) {
         return true;
     }
     //this dev
     $meDev = $pdu->device;
     //devices below
     $DevBelows = Model_Device::find()->where('rack', $meDev->rack)->where('cat', $meDev->cat)->where('id', '!=', $meDev->id)->where('meta_default_data', $meDev->meta_default_data)->get();
     $boundary = $pdu->pos + $size + 1;
     //get all devices in rack and see if they have pos < then boundary
     foreach ($DevBelows as $down) {
         $pdu2 = $down->power;
         if ($pdu2->pos > $pdu->pos and $pdu2->pos <= $boundary) {
             return false;
         }
     }
     return true;
 }
Example #5
0
 public function action_get()
 {
     if ($_POST) {
         $val = \Validation::forge();
         $val->add_field('did', 'Device id', 'required|min_length[1]|max_length[18]');
         if ($val->run()) {
             $device = Model_Device::find($val->validated('did'));
             $rack = Model_Rack::find($device->rack);
             $room = Model_Room::find($rack->room);
             $floor = Model_Floor::find($room->floor);
             $build = Model_Building::find($floor->building);
             $data = array('id' => $device->id, 'hostname' => $device->hostname, 'rack' => $device->rack, 'rack_pos' => $device->rack_pos, 'rack_units' => $device->rack_units, 'cat' => $device->cat, 'type' => $device->type, 'room' => $room->id, 'floor' => $floor->id, 'build' => $build->id);
             echo json_encode($data);
         }
     }
 }
Example #6
0
 public function action_new()
 {
     $out = 0;
     if ($_POST) {
         $val = \Validation::forge();
         $val->add_field('rack', 'rack id', 'required|min_length[1]|max_length[20]');
         $val->add_field('p', 'position left|right', 'required|min_length[1]|max_length[20]');
         if ($val->run()) {
             $rack = Model_Rack::find($val->validated('rack'));
             if ($rack) {
                 $size = $rack->size;
                 //left or right side pdu
                 if ($val->validated('p') == 'l') {
                     $m = 1;
                 } else {
                     $m = 2;
                 }
                 $devs = Model_Device::find()->where('rack', $rack->id)->where('cat', 4)->where('meta_default_data', $m)->get();
                 $pos = $this->empty_space($devs, $rack->size);
                 if ($pos >= 0) {
                     $props = array('hostname' => 'vertical pdu', 'type' => 0, 'cat' => 4, 'rack' => $rack->id, 'rack_pos' => 0, 'rack_units' => 0, 'parent_device' => 0, 'meta_default_data' => $m, 'meta_update_time' => time(), 'meta_update_user' => $this->user);
                     //print_r($props);
                     $dev = new Model_Device($props);
                     $dev->save();
                     $this->add_device_default_fields($dev);
                     $power = Model_Device_Power::find()->where('deviceID', $dev->id)->get_one();
                     $this->tmpl = false;
                     $power->pos = $pos;
                     $power->input = 1;
                     $power->output = 8;
                     $power->save();
                     for ($i = 1; $i <= 7; $i++) {
                         $prop = array('powerID' => $power->id, 'conn_type' => 1, 'type' => 2);
                         if ($this->tmpl) {
                             $socket = new Model_Device_Template_Pdu_Socket($prop);
                         } else {
                             $socket = new Model_Device_Power_Socket($prop);
                         }
                         $socket->save();
                     }
                     $out = array('id' => $power->id, 'ru' => 8, 'pos' => $pos, 'devid' => $dev->id, 'name' => $dev->hostname, 'parent_device' => 0, 'out' => $power['output'], 'cur' => $power['current']);
                 }
             }
         }
     }
     echo json_encode(array('data' => $out));
 }
Example #7
0
 /**
  * 
  * frontend device CRUD connector 
  */
 public function action_device($id = null)
 {
     if ($id) {
         if ($id != 'new') {
         } else {
             //create new device in rack, return value
             if ($id == 'new') {
                 $val = \Validation::forge();
                 $val->add_field('rack', 'rack id', 'required|min_length[1]|max_length[20]');
                 //validate rack
                 if ($val->run()) {
                     $rack = Model_Rack::find($val->validated('rack'));
                     //rack exist
                     if ($rack) {
                         //check position
                         $pos = $this->check_device_position($rack);
                         if ($pos) {
                             $props = array('hostname' => 'new device', 'type' => 0, 'cat' => 0, 'rack' => $rack->id, 'rack_pos' => $pos, 'rack_units' => 1, 'parent_device' => 0, 'meta_default_data' => 0, 'meta_update_time' => time(), 'meta_update_user' => $this->user);
                             //print_r($props);
                             $new = new Model_Device($props);
                             $new->save();
                             $dev = array("id" => $new['id'], "position" => $new['rack_pos'], "units" => $new['rack_units'], "type" => $new['type'], "cat" => 0, "host" => $new['hostname'], "parent" => $new['parent_device']);
                         }
                         echo json_encode($dev);
                     }
                 } else {
                     $error = $val->error();
                     print_r($error);
                 }
             }
         }
     }
 }
Example #8
0
 private function add_switch($rack, $user, $pos, $name)
 {
     $props = array('hostname' => $name, 'type' => 0, 'cat' => 2, 'rack' => $rack->id, 'rack_pos' => $pos, 'rack_units' => 1, 'parent_device' => 0, 'meta_default_data' => 0, 'meta_update_time' => time(), 'meta_update_user' => $user);
     //print_r($props);
     $this->net_type = 2;
     $sw = new Model_Device($props);
     $sw->save();
     $this->add_device_default_fields($sw);
     $network = Model_Device_Network::find()->where('deviceID', $sw->id)->get_one();
     $network->ports = 48;
     $network->save();
     $macc = array();
     for ($i = 1; $i <= 48; $i++) {
         $prop = array('networkID' => $network->id, 'mac_address' => '', 'conn_device' => 0, 'vlan' => 0, 'type' => 1);
         $macs = new Model_Network_Mac($prop);
         $macs->save();
         array_push($macc, $macs->id);
     }
     return array('id' => $sw->id, 'macs' => $macc);
 }
Example #9
0
 public function action_index()
 {
     $out = array();
     if (count($_GET) > 0) {
         /*
                       'buildings'=>Array(),
                       'rooms'=>Array(),
                       'floors'=>Array(),
                       'buildings'=>Array(),
                       'fields'=>Array() */
         $key = false;
         if (isset($_GET['key'])) {
             $key = $_GET['key'];
         }
         if ($key) {
             $key_right = $key . '%';
             $key = '%' . $key . '%';
             //device hostname
             $device = Model_Device::find()->where('hostname', 'like', $key)->limit(5)->where('meta_update_user', $this->user)->get();
             $m = array('type' => 'Device', 'tn' => 1, 'items' => array());
             foreach ($device as $dev) {
                 $rack = $dev->racks;
                 $room = $rack->rooms;
                 array_push($m['items'], array('id' => $dev->id, 'name' => $dev->hostname, 'room' => $room->id, 'rack' => $rack->id, 'floor' => $room->floors->id, 'building' => $room->floors->buildings->id));
             }
             array_push($out, $m);
             //buildings
             $buildings = Model_Building::find()->where('name', 'like', $key)->where('meta_update_user', $this->user)->limit(5)->get();
             $m = array('type' => 'Building', 'tn' => 2, 'items' => array());
             foreach ($buildings as $bui) {
                 array_push($m['items'], array('id' => $bui->id, 'name' => $bui->name));
             }
             array_push($out, $m);
             //floors
             $floors = Model_Floor::find()->where('name', 'like', $key)->where('meta_update_user', $this->user)->limit(5)->get();
             $m = array('type' => 'Floor', 'tn' => 3, 'items' => array());
             foreach ($floors as $fl) {
                 array_push($m['items'], array('id' => $fl->id, 'name' => $fl->name, 'building' => $fl->buildings->id));
             }
             array_push($out, $m);
             //rooms
             $rooms = Model_Room::find()->where('name', 'like', $key)->where('meta_update_user', $this->user)->limit(5)->get();
             $m = array('type' => 'Room', 'tn' => 4, 'items' => array());
             foreach ($rooms as $rm) {
                 array_push($m['items'], array('id' => $rm->id, 'name' => $rm->name, 'floor' => $rm->floors->id, 'building' => $rm->floors->buildings->id));
             }
             array_push($out, $m);
             //racks
             $rack = Model_Rack::find()->where('name', 'like', $key)->where('meta_update_user', $this->user)->limit(5)->get();
             $m = array('type' => 'Rack', 'tn' => 5, 'items' => array());
             foreach ($rack as $rm) {
                 array_push($m['items'], array('id' => $rm->id, 'name' => $rm->name, 'floor' => $rm->rooms->floors->id, 'room' => $rm->rooms->id, 'building' => $rm->rooms->floors->buildings->id));
             }
             array_push($out, $m);
             //ip address
             $query = \DB::query('select distinct * from network_ip_ports where ipv4 like "' . $key_right . '" group by networkID limit 0,5');
             $ips = $query->as_object()->execute();
             foreach ($ips as $ip) {
                 $tab = $ip->type == 3 ? 6 : 2;
                 $network = Model_Device_Network::find()->where('id', $ip->networkID)->get_one();
                 $device = $network->device;
                 $m = array('type' => 'IPv4', 'tn' => 6, 'items' => array());
                 $room = $device->racks->rooms;
                 array_push($m['items'], array('id' => $ip->id, 'name' => $ip->ipv4, 'room' => $device->racks->rooms->id, 'rack' => $device->racks->id, 'building' => $room->floors->buildings->id, 'floor' => $room->floors->id, 'tab' => $tab, 'dev' => $device->id));
                 array_push($out, $m);
             }
             //raid
             $query = \DB::query('select df.id, df.name, df.tab, df.deviceID, d.hostname, df.value, hrd.model, hrd.serial_number, hrd.id as "rid" from device as d join device_fieldset as df on df.deviceID = d.id join hardware_raid AS hr on hr.fieldsetID = df.id join hardware_raid_data as hrd on hrd.hardware_raid = hr.id where hrd.model like "' . $key . '" or hrd.serial_number like "' . $key . '" group by hrd.serial_number, hrd.model');
             $raid = $query->as_object()->execute();
             foreach ($raid as $fd) {
                 $device = Model_Device::find($fd->deviceID);
                 $m = array('type' => $fd->name, 'tn' => 6, 'items' => array());
                 $room = $device->racks->rooms;
                 array_push($m['items'], array('id' => $fd->id, 'name' => $fd->value, 'room' => $device->racks->rooms->id, 'rack' => $device->racks->id, 'building' => $room->floors->buildings->id, 'floor' => $room->floors->id, 'tab' => $fd->tab, 'dev' => $fd->deviceID, 'hostname' => $fd->hostname, 'model' => $fd->model, 'serial' => $fd->serial_number, 'rid' => $fd->rid));
                 array_push($out, $m);
             }
             $notes = Model_Notes::find()->where('txt', 'like', $key)->where('meta_update_user', $this->user)->limit(5)->get();
             $m = array('type' => 'Device Notes', 'tn' => 7, 'items' => array());
             foreach ($notes as $note) {
                 $device = $note->device;
                 $room = $device->racks->rooms;
                 array_push($m['items'], array('id' => $note->id, 'name' => substr($note->txt, -100), 'room' => $device->racks->rooms->id, 'rack' => $device->racks->id, 'building' => $room->floors->buildings->id, 'floor' => $room->floors->id, 'tab' => 4, 'dev' => $device->id));
             }
             array_push($out, $m);
             //ram
             $query = \DB::query('select df.id, df.name, df.tab, df.deviceID, d.hostname, df.value, hrd.model, hrd.serial_number, hrd.id as "rid" from device as d join device_fieldset as df on df.deviceID = d.id join hardware_ram AS hr on hr.fieldsetID = df.id join hardware_ram_data as hrd on hrd.hardware_ram = hr.id where hrd.model like "' . $key . '" or hrd.serial_number like "' . $key . '" group by hrd.serial_number, hrd.model');
             $ram = $query->as_object()->execute();
             foreach ($ram as $fd) {
                 $device = Model_Device::find($fd->deviceID);
                 $m = array('type' => $fd->name, 'tn' => 8, 'items' => array());
                 $room = $device->racks->rooms;
                 array_push($m['items'], array('id' => $fd->id, 'name' => $fd->value, 'room' => $device->racks->rooms->id, 'rack' => $device->racks->id, 'building' => $room->floors->buildings->id, 'floor' => $room->floors->id, 'tab' => $fd->tab, 'dev' => $fd->deviceID, 'hostname' => $fd->hostname, 'model' => $fd->model, 'serial' => $fd->serial_number, 'rid' => $fd->rid));
                 array_push($out, $m);
             }
         }
         echo json_encode($out);
     }
 }
Example #10
0
 private function vpsdata($id)
 {
     $device = Model_Device::find($id);
     $this->id = $device->id;
     /*
      * SUMMARY
      * */
     $sockets = $this->get_field('Processor Sockets');
     $cores = $this->get_field('Cores per Socket');
     $hyperthreading = $this->get_field('hyperthreading');
     $hyper = 'No';
     $hyperNum = 1;
     if ($hyperthreading == 1) {
         $hyper = 'Yes';
         $hyperNum = 2;
     }
     $logic = $sockets * $cores * $hyperNum;
     $summary = array('manufacturer' => $this->get_field('Manufacturer'), 'model' => $this->get_field('Model'), 'speed' => $this->get_field('Core speed (GHz)'), 'processor' => $this->get_field('Processor Type'), 'sockets' => $sockets, 'cores' => $cores, 'hyperthreading' => $hyper, 'logic' => $logic, 'memory' => $this->get_memory(), 'localStorage' => $this->get_storage(), 'vps' => array(), 'ram_used' => 0, 'hdd_used' => 0, 'cpu_used' => 0);
     $vpss = $device->vps;
     $ram = 0;
     $cpu = 0;
     $hdd = 0;
     foreach ($vpss as $vps) {
         $ips = $vps->ips;
         $ip_data = array();
         foreach ($ips as $ip) {
             $ipv4 = $this->vps_port($ip);
             array_push($ip_data, array('id' => $ip->portID, 'data' => $ipv4));
         }
         array_push($summary['vps'], array('id' => $vps->id, 'hostname' => $vps->hostname, 'cpu' => $vps->cpu, 'ram' => $vps->ram, 'hdd' => $vps->storage, 'ip' => $ip_data));
         $ram += $vps->ram;
         $cpu += $vps->cpu;
         $hdd += $vps->storage;
     }
     $summary['ram_used'] = $ram;
     $summary['hdd_used'] = $hdd;
     $summary['cpu_used'] = $cpu;
     $debug = array('debug' => 'Page rendered in {exec_time}s using {mem_usage}mb of memory.');
     array_push($summary, $debug);
     return $summary;
 }
Example #11
0
 public function action_devices($id = null, $name = null)
 {
     if ($id and $name) {
         $device = Model_Device::find($id);
         if ($device) {
             $fields = $device->fields;
             $h = array('title' => $device->hostname . 'X', 'category' => $device->cat, 'items' => array());
             $a = array();
             $file = tempnam(DOCROOT . "images", "zip");
             $zip = new \ZipArchive();
             $m = $zip->open($file, \ZipArchive::OVERWRITE);
             //images
             $zip->addEmptyDir('images');
             $zip->addEmptyDir('images/tumb');
             foreach ($fields as $field) {
                 $val = '';
                 if ($field['value'] != null) {
                     $val = htmlspecialchars_decode($field['value']);
                 }
                 $a = array('name' => $field['name'], 'tab' => $field['tab'], 'value' => $val, 'element' => $field['type'], 'static' => $field['static'], 'extra' => $field['extra'], 'class' => "", 'items' => null);
                 //get hdd into array
                 if ($field['type'] == 'hdd') {
                     $a['items'] = array();
                     $hdds = $field->hdd;
                     $a['raid_type'] = $hdds->raid_type;
                     $a['size'] = $hdds->size;
                     $a['total'] = $hdds->total;
                     foreach ($hdds->rows as $hdd) {
                         $m = array('model' => $hdd['model'], 'size' => $hdd['size'], 'vport' => $hdd['vport'], 'serial_number' => $hdd['serial_number']);
                         array_push($a['items'], $m);
                     }
                 }
                 //get ram into array
                 if ($field['type'] == 'ram') {
                     $a['items'] = array();
                     $rams = $field->ram;
                     $a['ram_type'] = $rams->ram_type;
                     $a['size'] = $rams->size;
                     $a['total'] = $rams->total;
                     foreach ($rams->rows as $ram) {
                         $m = array('model' => $ram['model'], 'size' => $ram['size'], 'port' => $ram['port'], 'serial_number' => $ram['serial_number']);
                         array_push($a['items'], $m);
                     }
                 }
                 //get images into array
                 if ($field['type'] == 'img') {
                     $imagedir = DOCROOT . 'images/';
                     $a['items'] = array();
                     $imgs = $field->images;
                     foreach ($imgs as $img) {
                         if (is_file($imagedir . $img->id . '.png')) {
                             $zip->addFile($imagedir . $img->id . '.png', 'images/' . $img->id . '.png');
                         }
                         if (is_file($imagedir . 'tumb/' . $img->id . '.png')) {
                             $zip->addFile($imagedir . 'tumb/' . $img->id . '.png', 'images/tumb/' . $img->id . '.png');
                         }
                         $im = array('type' => $img->type, 'tmpl' => 'true', 'w' => $img->width, 'h' => $img->height);
                         array_push($a['items'], $im);
                     }
                 }
                 if ($field['type'] == 'power_in') {
                     $a['element'] = 'none';
                     $a['nolegend'] = 'ok';
                     $a['special'] = 'inpowerfield';
                     $pdu = $field->power;
                     $a['data'] = array('input' => $pdu->input, 'current' => $pdu->current, 'output' => $pdu->output, 'type' => $pdu->type, 'sockets' => array());
                     foreach ($pdu->socket as $socket) {
                         array_push($a['data']['sockets'], array('type' => $socket->type, 'conn_type' => $socket->conn_type));
                     }
                 }
                 if ($field['type'] == 'power_out') {
                     $a['element'] = 'none';
                     $a['nolegend'] = 'ok';
                     $a['special'] = 'powerfield';
                     $pdu = $field->power;
                     $a['data'] = array('input' => $pdu->input, 'current' => $pdu->current, 'output' => $pdu->output, 'type' => $pdu->type, 'sockets' => array());
                     foreach ($pdu->socket as $socket) {
                         array_push($a['data']['sockets'], array('type' => $socket->type, 'conn_type' => $socket->conn_type));
                     }
                 }
                 /*
                  * KVM distribution
                  * */
                 if ($field['type'] == 'kvm_in') {
                     $a['element'] = 'none';
                     $a['nolegend'] = 'ok';
                     $a['special'] = 'inkvmfield';
                     $kvm = $field->kvm;
                     $a['data'] = array('input' => $kvm->input, 'output' => $kvm->output, 'sockets' => array());
                     foreach ($kvm->socket as $socket) {
                         array_push($a['data']['sockets'], array('id' => $socket->id, 'type' => $socket->type, 'conn_type' => $socket->conn_type));
                     }
                 }
                 if ($field['type'] == 'kvm_out') {
                     $a['element'] = 'none';
                     $a['nolegend'] = 'ok';
                     $a['special'] = 'kvmfield';
                     $kvm = $field->kvm;
                     $a['data'] = array('input' => $kvm->input, 'output' => $kvm->output, 'sockets' => array());
                     foreach ($kvm->socket as $socket) {
                         array_push($a['data']['sockets'], array('type' => $socket->type, 'conn_type' => $socket->conn_type));
                     }
                 }
                 if ($field['type'] == 'network') {
                     $a['element'] = 'none';
                     $a['data'] = array();
                     echo $field['type'];
                     if ($field->network) {
                         $a['extra'] = array('type' => $field->network->type, 'nics' => $field->network->nics, 'vports' => $field->network->vports, 'ports' => $field->network->ports, 'uplinks' => $field->network->uplinks, 'config_data' => $field->network->config_data);
                         $a['type'] = $field->network->type;
                         switch ($field->network->type) {
                             case 1:
                                 //server ip tables
                                 $a['special'] = 'ipfield';
                                 $i = 1;
                                 foreach ($field->network->ip as $ip) {
                                     array_push($a['data'], array('port' => array('n' => $i, 'name' => $ip->nic_name, 'ipv4' => $ip->ipv4, 'ipv6' => $ip->ipv6, 'conn_type' => $ip->conn_type, 'conn_speed' => $ip->conn_speed, 'type' => $ip->type)));
                                     $i++;
                                 }
                                 break;
                             case 2:
                                 $a['special'] = 'macfield';
                                 $a['vlans'] = array();
                                 $i = 1;
                                 foreach ($field->network->mac as $mac) {
                                     $d = array('n' => $i, 'mac_addr' => $mac->mac_address, 'conn_dev' => $mac->conn_device, 'vlan' => $mac->vlan, 'type' => $mac->type);
                                     array_push($a['data'], $d);
                                     $i++;
                                 }
                                 foreach ($field->network->vlan as $vlan) {
                                     $d = array('id' => $vlan->id, 'name' => $vlan->name);
                                     array_push($a['vlans'], $d);
                                 }
                                 break;
                             case 3:
                                 $a['special'] = 'panelfield';
                                 $i = 1;
                                 foreach ($field->network->mac as $mac) {
                                     $d = array('n' => $i, 'id' => $mac->id, 'mac_addr' => $mac->mac_address, 'conn_dev' => $mac->conn_device, 'vlan' => $mac->vlan, 'type' => $mac->type);
                                     array_push($a['data'], $d);
                                     $i++;
                                 }
                                 break;
                         }
                     }
                 }
                 array_push($h['items'], $a);
             }
             $json = json_encode($h);
             //data
             $zip->addFromString('data.json', $json);
             $zip->close();
             ob_clean();
             header('Content-Type: application/zip');
             header("Content-Length: " . filesize($file));
             header('Content-Disposition: attachment; filename="' . str_replace(' ', '_', $device->hostname) . '.zip"');
             readfile($file);
             exit;
             //unlink($file);
         }
     }
 }
Example #12
0
 public function action_device($id = null)
 {
     if ($_POST) {
         $val = \Validation::forge('templates');
         if ($id == 'edit') {
             $val->add_field('did', 'device id', 'required|min_length[1]|max_length[20]');
             $val->add_field('type', 'device new type', 'required|min_length[1]|max_length[10]');
             if ($val->run()) {
                 //get device current template type
                 $dev = Model_Device::find($val->validated('did'));
                 //set template
                 $template = Model_Device_Template::find($val->validated('type'));
                 //category
                 $cat = $template->category;
                 //store position of vertical pdu
                 $power_temp = false;
                 if ($cat->id == 4) {
                     if ($dev->meta_default_data > 0) {
                         $power_temp = $dev->power;
                     }
                 }
                 $pdu_size_wrong = $this->check_for_PDU($template, $dev);
                 if ($pdu_size_wrong) {
                     echo json_encode(array('error' => 'You cant use this template, no enough space in rack'));
                 } else {
                     $dev_fields = Model_Device_Fieldset::find()->where('deviceID', $dev->id)->get();
                     //delete all data from device
                     foreach ($dev_fields as $dev_field) {
                         $dev_field->delete();
                     }
                     $fields = $template->field;
                     $dev->type = $template->id;
                     $dev->save();
                     foreach ($fields as $field) {
                         $data = array('name' => $field['name'], 'static' => $field['static'], 'value' => htmlspecialchars_decode($field['value']), 'type' => $field['type'], 'deviceID' => $dev->id, 'tab' => $field['tab'], 'extra' => $field['extra']);
                         $new_field = new Model_Device_Fieldset($data);
                         $new_field->save();
                         $this->insert_template_to_device_data($field, $new_field);
                     }
                     $out = false;
                     if ($cat->id == 4) {
                         $dev = Model_Device::find($val->validated('did'));
                         //check if vertical PDU , and echo number of output pins
                         if ($dev->meta_default_data > 0) {
                             $po = Model_Device_Power::find()->where('deviceID', $val->validated('did'))->get_one();
                             $po->pos = $power_temp->pos;
                             $po->ru = $power_temp->ru;
                             $po->save();
                             $out = $po->output;
                             echo json_encode(array('type' => $template->id, 'id' => $dev->id, 'cat' => $cat->id, 'out' => $out));
                         }
                     }
                     if (!$out) {
                         echo json_encode(array('type' => $template->id, 'id' => $dev->id, 'cat' => $cat->id));
                     }
                 }
             }
         }
     }
 }