Subversion Repositories portal2

Rev

Rev 714 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*  emg - Energy Monitoring & Graphing
*  Copyright (C) 2006-2012 Romain Lievin
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software Foundation,
*  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

?>

<div id="page_form_devices_add_submit">
        <h1>
                <?php echo _('Add device'); ?>
        </h1>
</div>

<?php

$error = New error();
$ok = false;

if(isset($_POST['form_device_cancel']))
{
        $error->push(_('Operation aborted.'));
}
else
if(!isset( $_POST['form_device_name'], $_POST['form_device_url'], $_POST['form_token'], $_POST['form_device_type']))
{
        $error->push(_('One or more fields are missing.'));
}
elseif( $_POST['form_token'] != $_SESSION['form_token'])
{
        $error->push(_('Internal error: invalid form token.'));
}
elseif(strlen( $_POST['form_device_name']) > 64 || strlen($_POST['form_device_name']) < 4)
{
        $error->push(_('Name must have [4-64] characters).'));
}
elseif(($_POST['form_device_type'] == 'remote') &&
          !(filter_var($_POST['form_device_url'], FILTER_VALIDATE_URL) || filter_var($_POST['form_device_url'], FILTER_VALIDATE_IP)))
{
        $error->push(_('Bad IP or URL address.'));
}
/*elseif(($_POST['form_device_type'] == 'local'))
{
        $error->push(_('Bad IP or URL address.'));
}*/

else
{
        $form_device_type       = filter_var($_POST['form_device_type'], FILTER_SANITIZE_STRING);
        $form_device_name       = filter_var($_POST['form_device_name'], FILTER_SANITIZE_STRING);
       
        if(filter_var($_POST['form_device_url'], FILTER_VALIDATE_IP) !== false)
        {
                $form_device_url        = filter_var($_POST['form_device_url'], FILTER_VALIDATE_IP);
        }
        else
        {
                $form_device_url        = filter_var($_POST['form_device_url'], FILTER_SANITIZE_URL);
        }
       
        $form_device_url        = filter_var($_POST['form_device_url'], FILTER_SANITIZE_URL);
        $form_device_ssl_folder = $_SESSION['root_folder']['ssl'][''];
        if(isset($_POST['form_device_ssl_enabled']))
        {
                if($_POST['form_device_ssl_enabled'] == 'on')
                $form_device_ssl_enabled = true;
                else
                $form_device_ssl_enabled = false;
        }
        else
        {
                $form_device_ssl_enabled = false;
        }

        if($form_device_ssl_enabled === true)
        {
                $form_device_ssl_crt = filter_var($_POST['form_device_ssl_crt'], FILTER_SANITIZE_STRING);
                $form_device_ssl_key = filter_var($_POST['form_device_ssl_key'], FILTER_SANITIZE_STRING);
                $form_device_ssl_ca  = filter_var($_POST['form_device_ssl_ca'], FILTER_SANITIZE_STRING);
        }
        else
        {
                $form_device_ssl_crt = '';
                $form_device_ssl_key = '';
                $form_device_ssl_ca  = '';
        }

        if($form_device_type == 'remote')
        {
                $model = RouteFactory::HTTP;
        }
        elseif($form_device_type == 'local')
        {
                $model           = RouteFactory::FILE;
                $form_device_url = '.';
        }
        else
        {
                $model = RouteFactory::FILE;
        }

        $route = RouteFactory::createRoute($model);
        $route->setTarget($form_device_url);

        if($form_device_ssl_enabled === true)
        {
                $route->set_ssl_enabled();
                $route->set_ssl_folder($form_device_ssl_folder);
                $route->set_ssl_certificates(array('crt'=> $form_device_ssl_crt,'key'=> $form_device_ssl_key,'ca' => $form_device_ssl_ca));
        }

        $park   = $_SESSION['park'];
        $device = DeviceFactory::createDevice($form_device_name);
        $device->setRoute($route);


        try
        {
                $uuid   = $device->queryUuid();
        }      
        catch (MyException $e)
        {
                $error->push($e->getErrorReport());            
                $uuid = null;
        }
        if($uuid == null)
        {
                $error->push(_('Wrong answer from device. Either device is faulty, or you attempt to establish communication with an unknown IP device.'));
        }
        else
        {
                try
                {
                        if($device->queryTimeZone() === false)
                        {
                                //load uuid into class only; does nothing
                        }
                }              
                catch (MyExpcetion $e)
                {
                        $error->push($e->getErrorReport());    
                }

                try
                {
                        $device->querySensors();
                        $device->loadTrackers();
                        $device->setName($form_device_name);   
                }              
                catch (MyException $e)
                {
                        $error->push($e->getErrorReport());            
                }              

                $result = $park->addDevice($uuid, $device);
                if($result == true)
                {
                        $error->push(_('Device added.'));
                        $ok      = true;

                        /*** save configuration contents ***/
                        merge_trackers_and_data();
                        config_save();
                }
                else
                {
                        var_dump($uuid);
                        $error->push(_('A device with this UUID already exists.'));
                }
        }
}

echo $error->display();

/* Redirection */
if(isset($_POST['form_cancel']) || ($ok === true))
{
        $id = link_get_id_from_filename("form_devices_list.php");
        $refresh = $_SESSION['refresh_redirection'];
        echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}
else if($ok == false)
{
        $id = link_get_id_from_filename("form_devices_add.php");
        $refresh = $_SESSION['refresh_redirection'];
        echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}

?>