<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# POSH Widgets management mobile - XML widget informations
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

$folder="";
$not_access=1;
$pagename="includes/upload_component.php";
$granted="I";
//includes
require_once('../../../config.inc.php');
require_once('../../../connection_'.__DBTYPE.'.inc.php');
require_once('../../../session.inc.php');
require_once('../db_layer/'.__DBTYPE.'/mobile_admin.php');

global $DB;
$DB = new connection(__SERVER,__LOGIN,__PASS,__DB);

require_once('../../../file.inc.php');
require_once('../../../image.inc.php');
require_once('../l10n/'.__LANG.'/lang.php');
$error = "";
$nbicons  = (isset($_REQUEST["nbicons"])) ? $_REQUEST["nbicons"] : exit();
$modid  = (isset($_REQUEST["modid"])) ? $_REQUEST["modid"] : 0;

?>
    <!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" xml:lang="en" lang="en">
    <head>
    <style>
    input[type="file"] {
	border:1px solid #efefef;
	font-size:7pt;
	font-weight:bold;
	background-color:#aaaaaa;
	height:20px;
	color:#ffffff;
	cursor:hand;
	cursor:pointer;
    }
    input.btn {
        border:1px solid #efefef;
        font-size:7pt;
        font-weight:bold;
        background-color:#aaaaaa;
        height:20px;
        color:#ffffff;
        cursor:hand;
        cursor:pointer;
    }
    </style>
    </head>
    <body>
   
<?php   
if (isset($_FILES['file']))
{
    $fileallowext = array();
    $allowext  = 'png';
    $fileallowext = explode(',',$allowext);

    if (isset($_FILES["file"]["name"]) AND !empty($_FILES["file"]["name"])) {
        //control path directory
        $uploadFile = $_FILES['file']['tmp_name'];
        $fileName = $_FILES["file"]["name"] ;
        $fileSize = $_FILES["file"]["size"];
        if ($fileSize > __uploadMaxFileSize) $error = 'fileTooBig'.'('.__uploadMaxFileSize.')';
        if ($_FILES["file"]["error"] > 0) $error = $_FILES["file"]["error"];
        if ($error == "") {
            //Check file extension
            $ext = strtolower(substr($fileName,-3));
            if (count($fileallowext)>0) {
                if (!in_array($ext, $fileallowext)) {
                    $error = 'extensionNotAllowed';
                }
            }
            else if (strpos(__uploadAllowedExtensions,$ext) === false) {
                $error = 'extensionNotAllowed';
            }
            else {
                if (is_uploaded_file($_FILES['file']['tmp_name'])) {
                    $uploadFile = $_FILES['file']['tmp_name'];
                }
                else {
                    $error = 'uploadNotSuccessful';
                }
            }
        }
    }
    else $error = 'uploadNotSuccessful';

    //In case everything is fine
    if ($error != "") {
        ?><script>parent.$p.mobile.widgets.errorUploading("<?php echo $error; ?>");</script><?php
    }
    else {
        $completeFolder = "../images/icons";
        $num = $nbicons+1;
        $newFileName = 'mob_ico_'.$num.'.png';
        $fileLocation = $completeFolder.'/'.$newFileName;
        $img = new img($uploadFile);
        if ($img->height != 48 || $img->width != 48) {
            if ($img->height > $img->width) {
                $decx=0;
                $decy=($img->height - $img->width)/2;
                $img->height = $img->width;
            }
            else {
                $decx = ($img->width - $img->height)/2;
                $decy = 0;
                $img->width = $img->height;
            }
            if ( $ext == "png" ) {
                $img->createPng();
            }
            $img->resize($fileLocation,'48','48',$decx,$decy);
        }
        else{
            copy($uploadFile,$fileLocation);
        }
        ?><script>parent.$p.mobile.widgets.refreshAfterUpload("<?php echo $modid; ?>");</script><?php
    }
}
?>
<form name="f" action="" method="post" enctype="multipart/form-data" style="margin-top: 0px;">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo __uploadMaxFileSize;?>" />
	<input type="hidden" name="nbicons" value="<?php echo $nbicons;?>" />
	<input type="hidden" name="modid" value="<?php echo $modid;?>" />
	<input type="file" class="btn" name="file" />
    <input type="submit" class="btn" value="<?php echo lg("lblUpload");?>" />
</form>