Subversion Repositories portal2

Rev

Rev 690 | 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_data_submit">
        <h1>
                <?php echo _('Save/load configuration'); ?>
        </h1>
</div>

<?php

// code from < http://php.net / manual / fr / function.header.php >
function downloadFile( $fullPath )
{
        // Must be fresh start
        if(headers_sent())
        {
                die('Headers Sent');
        }

        // Required for some browsers
        if(ini_get('zlib.output_compression'))
        {
                ini_set('zlib.output_compression', 'Off');
        }

        // File Exists?
        if( file_exists($fullPath) )
        {
                // Parse Info / Get Extension
                $fsize      = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext        = strtolower($path_parts["extension"]);

                // Determine Content Type
                switch($ext)
                {
                        case "pdf": $ctype = "application/pdf"; break;
                        case "exe": $ctype = "application/octet-stream"; break;
                        case "zip": $ctype = "application/zip"; break;
                        case "doc": $ctype = "application/msword"; break;
                        case "xls": $ctype = "application/vnd.ms-excel"; break;
                        case "ppt": $ctype = "application/vnd.ms-powerpoint"; break;
                        case "gif": $ctype = "image/gif"; break;
                        case "png": $ctype = "image/png"; break;
                        case "jpeg":
                        case "jpg": $ctype = "image/jpg"; break;
                        default: $ctype = "application/force-download";
                }

                header("Pragma: public"); // required
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: private",false); // required for certain browsers
                header("Content-Type: $ctype");
                header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: ".$fsize);
                ob_clean();
                flush();
                readfile( $fullPath );
        }
        else
        {
                die('File Not Found');
        }
}

print_r($_POST);

$error = new Error();
$ok    = false;

if(!isset($_POST['form_device_key']))
{
        /// nothing to do
}
else
{
        $form_device_key = $_POST['form_device_key'];
}
if(!isset($_POST['form_token'], $_POST['form_data']))
{
        $error->push(_('One or more fields are missing.'));
}
elseif( $_POST['form_token'] != $_SESSION['form_token'])
{
        $error->push(_('Internal error: invalid form token.'));
}
else
if($_POST['form_data'] != 'download')
{
        $error->push(_('Internal error: invalid form token.'));
}

if(isset($_SESSION['park']))
{
        $park = $_SESSION['park'];
        if($park)
        {
                $device      = $park->getDevice($form_device_key);
                $device_name = $device->getName();

                $route       = $device->getRoute();
                $db          = new SqliteEngine($route);

                $target      = $_SESSION['root_folder']['tmp'][''] . '/' . $device_name . '.sqlite';
               
                try
                {
                        $ret         = $db->getCopy($target);
                }
                catch (MyException $e)
                {
                        $error->push($e->getErrorReport());            
                }      

                if($ret === false)
                {
                        $error->push(_('Database download failed.'));
                }
                else
                {
                        $error->push(_('Database download successfully.'));
                        downloadFile($target);
                }
        }
}

echo $error->display();

$id      = link_get_id_from_filename("form_config.php");
$refresh = $_SESSION['refresh_redirection'];
echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
?>