code/zipdir.php


Home Back
<!--
   PobCode
   
   Copyright 2022 Fabio DM <fadimatteo@gmail.com>
   
   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 2 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
    require("conf.php");
    $projectName=$_GET['p'];
    $file=$ProjectsRoot.'/'.$projectName;
    $realPath=realpath($ProjectsRoot.'/'.$projectName);
    
    $isProjectRoot=true;
    
    if (strpos($projectName, '/') !== false) 
    {
        $isProjectRoot=false;
        echo 'Ops you are bad!';
        die();
    }
    
    if (strpos($realPath, $ProjectsRoot) === FALSE) 
    {
        echo 'Ops you are bad!';
        die();
    }
    
    
    if(! isset($_GET['p']))
    {
        echo "Ops...";
        die();
    }
    
    if (realpath($ProjectsRoot.'/'.$projectName)==false)
    {
        echo "Ops...file not found!";
        die();
    }
    
    if(empty($_GET['p']))
    {
        echo "Ops...";
        die();
    }
    
    
    if (strpos($projectName, '..') !== false) 
    {
        echo "Ops...";
        die();
    }
    
    if (strpos($projectName, './') !== false)
    {
        echo "Ops...";
        die();
    }
    
    if (strpos($projectName, ',') !== false) 
    {
        echo "Ops...";
        die();
    }
    
    
    

        function Zip($source, $destination)
        {
            
            if (file_exists($destination)) {
                unlink($destination);
            }
            
            if (!extension_loaded('zip') || !file_exists($source)) {
                return false;
            }

            $zip = new ZipArchive();
            if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
                return false;
            }

            $source = str_replace('\\', '/', realpath($source));

            if (is_dir($source) === true)
            {
                $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

                foreach ($files as $file)
                {
                    $file = str_replace('\\', '/', $file);

                    // Ignore "." and ".." folders
                    if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                        continue;

                    $file = realpath($file);

                    if (is_dir($file) === true)
                    {
                        $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                    }
                    else if (is_file($file) === true)
                    {
                        $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                    }
                }
            }
            else if (is_file($source) === true)
            {
                $zip->addFromString(basename($source), file_get_contents($source));
            }

            return $zip->close();
        }
        
        
        
include ('header.php');        
?>

    <div id="main" class="container">
    <p class="h1"><?php echo $projectName ?></p>
    <hr/>
    <div class="container" style="margin-bottom:10px">
    <a class="btn btn-success" href="index.php" role="button">Home</a>    
    <a class="btn btn-success" href="javascript:window.history.back()" role="button">Back</a>
    </div>
    
    <?php 
        $zipFileName = str_replace('/', '-', $projectName);
        Zip($ProjectsRoot.'/'.$projectName.'/', 'tmp/'.$zipFileName.'.zip');
        
    ?>
    <div class="container" style="margin-bottom:10px;text-align:center">
    <h3>download the file:</h3>
    <p><a class="btn btn-info" href="<?php echo $weburl.'/tmp/'.$zipFileName.'.zip' ?>" role="button">Download <?php echo $zipFileName.'.zip'?></a></p>
    <p style="margin-top:55px">With pobsrc from terminal (click to copy): </p>
    <p><input id="txtClone" type="text" value="pobsrc clone <?php echo $weburl.'/tmp/'.$zipFileName.'.zip' ?>"></p>
    </div>
    <script>
        $(document).ready(function(){
            $("#txtClone").on("click", function () {
               $(this).select();
               document.execCommand('copy');
            });
            
            });
        </script>
    
    </div>
<?php include('footer.php');?>
</body>

</html>

Powered by Code, a simple repository browser by Fabio Di Matteo