<?php 
/**
 * Convertidor de bytes - repaso_1.php
 * 
 * @author    Bartolomé Sintes Marco <bartolome.sintes+mclibre@gmail.com>
 * @copyright 2011 Bartolomé Sintes Marco
 * @license   http://www.gnu.org/licenses/agpl.txt AGPL 3 or later
 * @version   2011-04-07
 * @link      http://www.mclibre.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">\n"; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Convertidor de bytes (Resultado).
  Repaso 1. Ejercicios. PHP. Bartolomé Sintes Marco</title>
  <link href="mclibre_php_soluciones.css" rel="stylesheet" type="text/css" 
  title="Color" />
</head>

<body>
<h1>Convertidor de bytes (Resultado)</h1>
<?php

function recoge($var)
{ 
    $tmp = (isset($_REQUEST[$var])) ? trim(strip_tags($_REQUEST[$var])) : '';
    if (get_magic_quotes_gpc()) {
        $tmp = stripslashes($tmp);
    }
    $tmp = str_replace('&', '&amp;',  $tmp);
    $tmp = str_replace('"', '&quot;', $tmp);
    return $tmp;
}

define('NUM_MAXIMO', 2000000000);

$bytes   = recoge('bytes');
$bytesOk = FALSE;

if ($bytes=="") {
    print "<p style=\"color: red\">No ha escrito los bytes.</p>\n";
} elseif (!is_numeric($bytes)) { 
    print "<p style=\"color: red\">No ha escrito los bytes como número.</p>\n";
} elseif (!ctype_digit($bytes)) { 
    print "<p style=\"color: red\">Los bytes deben ser un número entero positivo (sin parte decimal)</p>\n";
} elseif ($bytes>=NUM_MAXIMO) {
    // No se puede utilizar NUM_MAXIMO porque es superior a PHP_INT_MAX y se convierte a float
    print "<p style=\"color: red\">Los bytes deben ser inferiores a "
        .number_format(NUM_MAXIMO, 0, ',', '.').".</p>\n";
} else {
    $bytesOk = TRUE;
}
    
if ($bytesOk) {
    $bytesOriginal = $bytes;
    if ($bytes>=1024*1024*1024) {
        $gb = floor($bytes/(1024*1024*1024));
        $bytes = $bytes%(1024*1024*1024);
    }
    if ($bytes>=1024*1024) {
        $mb = floor($bytes/(1024*1024));
        $bytes = $bytes%(1024*1024);
    }
    if ($bytes>=1024) {
        $kb = floor($bytes/1024);
        $bytes = $bytes%1024;
    }
    
    print "<p>".number_format($bytesOriginal, 0, ',', '.')." bytes son ";
    if ($bytesOriginal==0) {
    	print "0 bytes.";
    }
    if (isset($gb)) {
        print number_format($gb, 0, ',', '.')." GB";
        if ((isset($mb) && isset($kb)) || (isset($mb) && ($bytes!=0))||
            (isset($kb) && ($bytes!=0))) {
            print ", ";
        } elseif (isset($mb) || isset($kb) || ($bytes!=0)) {
            print " y ";
        }
    }
    if (isset($mb)) {
        print number_format($mb, 0, ',', '.')." MB";
        if (isset($kb) && ($bytes!=0)) {
            print ", ";
        } elseif (isset($kb) || ($bytes!=0)) {
            print " y ";
        }
    }
    if (isset($kb)) {
        print number_format($kb, 0, ',', '.')." KB";
        if ($bytes!=0) {
            print " y ";
        }
    }
    if ($bytes==1) {
        print "1 byte";
    } elseif ($bytes!=0) {
        print number_format($bytes, 0, ',', '.')." bytes";
    }
    print ".<p>\n";
}

print "<p><a href=\"repaso_1.html\">Volver al formulario.</a></p>\n";
?>

<address>
  Esta página forma parte del curso "Páginas web con PHP" disponible en <a
  href="http://www.mclibre.org/">http://www.mclibre.org</a><br />
  Autor: Bartolomé Sintes Marco<br />
  Última modificación de esta página: 7 de abril de 2011
</address>
<p class="licencia">El programa PHP que genera esta página está bajo 
<a rel="license" href="http://www.gnu.org/licenses/agpl.txt">licencia AGPL 3 o
posterior</a>.</p>
</body>
</html>
