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
*/

?>

<?php
include_once('auth_check.php');
?>

<div id="page_devices_modify_submit">
        <h1>
                <?php echo _('Modify 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']))
{
        $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'], VALIDATE_URL) || filter_var($_POST['form_device_url'], VALIDATE_IP)))
{
        $error->push(_('Bad URL or IP address.'));
}
/*elseif(($_POST['form_device_type'] == 'local'))
{
        $error->push(_('Bad URL or IP address.'));
}*/

elseif(strlen( $_POST['form_device_key']) > 40 || strlen($_POST['form_device_key']) < 4)
{
        $error->push(_('Key is invalid.'));
}
else
{
        // ---
        $form_device_key = filter_var($_POST['form_device_key'], FILTER_SANITIZE_STRING);
        $form_device_name= filter_var($_POST['form_device_name'], FILTER_SANITIZE_STRING);
        $form_device_type= filter_var($_POST['form_device_type'], FILTER_SANITIZE_NUMBER_INT);
        $form_device_url = filter_var($_POST['form_device_url'], FILTER_SANITIZE_URL);
        if(isset($_POST['form_device_ssl_enabled']))
        {
                $form_device_ssl_enabled = $_POST['form_device_ssl_enabled'];
        }
        else
        {
                $form_device_ssl_enabled = false;
        }

        if($form_device_type != RouteFactory::FILE)
        {
                $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  = '';
        }

        // ---
        $park   = $_SESSION['park'];
        $device = $park->getDevice($form_device_key);

        $route  = $device->getRoute();
        $route->setTarget($form_device_url);
        if($form_device_ssl_enabled)
        {
                $route->set_ssl_enabled();
                $route->set_ssl_folder($_SESSION['root_folder']['ssl']['']);
                $route->set_ssl_certificates(array('crt'=> $form_device_ssl_crt,'key'=> $form_device_ssl_key,'ca' => $form_device_ssl_ca));
        }
        else
        {
                $route->set_ssl_disabled();
        }
        $device->setRoute($route);

        try
        {
                $device->setName($form_device_name);
        }
        catch (MyException $e)
        {
                $error->push($e->getErrorReport());            
        }              
       
        $park->setDevice($form_device_key, $device);

        $error->push(_('Device updated.'));

        /*** save configuration contents ***/
        merge_trackers_and_data();
        config_save();
}

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_data_modify.php");
        $refresh = $_SESSION['refresh_redirection'];
        echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}

?>