<?php 
        // Load configuration
        include 'conf/config.php';

        // Delete directory recursively
        function delTree($dir) {
                $files = array_diff(scandir($dir), array('.','..'));
                foreach ($files as $file) {
                        (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
                }
                return rmdir($dir);
        } 

        // Check if file have to be deleted
        $data_dir = array_diff(scandir(_DATA_DIR), array('..', '.', '.gitkeep'));
        foreach ($data_dir as $data_dir_content)
        {
                $data_conf = json_decode(file_get_contents(_DATA_DIR . "/" . $data_dir_content . "/info.json"),true);
                if ($data_conf['expire'] < time()) 
                {
                        delTree(_DATA_DIR . "/" . $data_dir_content);
                } 

        }

        if (_PRETTY_URL === true)
        {
                $downloadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH   ;
                $uploadurl = $downloadurl ;  
        }
        else
        {
                $downloadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . 'index.php?id=' ;
                $uploadurl = _HTTP_PROTO . '://' . _HTTP_DOMAIN . _HTTP_PATH . 'index.php?name=my_file' ;
        }

        // Upload File
        if ($_SERVER['REQUEST_METHOD'] === 'PUT') 
        {
                $rnd_data = mt_rand(10000,99999);
                $filesize = 0 ;
                
                mkdir(_DATA_DIR . "/" . $rnd_data);
                $info_json = new stdClass(); 
                $info_json->expire = time() + (7 * 24 * 60 * 60);
                if ($_GET['name'])
                {
                        $info_json->name = $_GET['name'];
                }
                file_put_contents(_DATA_DIR . "/" . $rnd_data . "/info.json", json_encode($info_json));

                $putdata = fopen("php://input", "r");
                $fp = fopen(_DATA_DIR . "/" . $rnd_data . "/file", "w");
                while ($data = fread($putdata, 1024) and $filesize < _SIZE_LIMIT )
                {
                        $filesize += 1024 ;
                        fwrite($fp, $data);
                }
                fclose($putdata);
                fclose($fp);

                if ($filesize < _SIZE_LIMIT )
                {
                        print($downloadurl . $rnd_data );
                }
                else
                {
                        delTree(_DATA_DIR . "/" . $rnd_data);
                        print("File size exceeded (Max " . _SIZE_LIMIT . " bytes)");
                }
                

        }

        // Informations for user
        if ($_SERVER['REQUEST_METHOD'] === 'GET')
        {
                if (isset($_GET['id']))
                {
                        if (is_dir(_DATA_DIR . "/" . $_GET['id']))
                        {
                                $data_conf = json_decode(file_get_contents(_DATA_DIR . "/" . $_GET['id'] . "/info.json"),true);
                                if ($data_conf['name']) 
                                {
                                        $filename = $data_conf['name'];
                                }
                                else
                                {
                                        $filename = "file";
                                }
                                $file = _DATA_DIR . "/" . $_GET['id'] . "/file" ;
                                header('Content-Type: application/octet-stream');
                                header("Content-Transfer-Encoding: Binary"); 
                                header('Content-Length: ' . filesize($file));
                                header("Content-disposition: attachment; filename=\"" . $filename . "\""); 
                                readfile($file);
                        }
                        else
                        {
                                http_response_code(404);
                        }
                }
                else
                {
                        if (stristr($_SERVER["HTTP_USER_AGENT"], 'curl') or stristr($_SERVER["HTTP_USER_AGENT"], 'Wget'))
                        {
                                print("To upload file, use # curl --upload-file my_file " . $uploadurl);
                        }
                        else
                        {
                                include("front.php" );
                        }
                }
        }

?>