<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

include('Net/SSH2.php');
include('Net/SFTP.php');
class Server {

    const ROOT_DIR = '/www/htdocs/v034011/';
    const TEMP_DIR = '/www/htdocs/v034011/_temp';

    // Settings
    private $url = 'XXXXXXX';
    private $port = 22;
    private $user = 'ssh-v034011';
    private $pass = 'XXXXXXX';

    private $ssh;
    private $sftp;

    public function __construct() {

        $this->ssh = new Net_SSH2( $this->url );
        if ( !$this->ssh->login( $this->user, $this->pass ) ) {
            throw new Exception( 'SSH Connection failed.' );
        }

        $this->sftp = new Net_SFTP( 'XXXXXXX' );
        if ( !$this->sftp->login( $this->user, $this->pass ) ) {
            throw new Exception( 'SFTP Connetion failed.' );
        }

    }

    public function exec( $cmd ) {

        return $this->ssh->exec( $cmd );

    }

    public function upload( $local, $remote ) {
        return $this->sftp->put( $remote, $local, NET_SFTP_LOCAL_FILE );
    }

    public function download( $remote, $local ) {
        return $this->sftp->get( $remote, $local );
    }
}

class Local {

    const PROJECT_ROOT = 'C:/Users/Benny/PhpstormProjects/Root';
    const TEMP_DIR = 'C:/Users/Benny/Desktop/_SYSTEM/temp';

    public function fileCopy( $src, $dest ) {
        if ( is_file( $src ) ) {
            return copy( $src, $dest );
        } else {
            return false;
        }
    }

    public function rCopy( $src, $dest ) {
        if(!is_dir($src)) return false;

        if(!is_dir($dest)) {
            if(!mkdir($dest)) {
                return false;
            }
        }

        $i = new DirectoryIterator($src);
        foreach($i as $f) {
            if($f->isFile() && substr( $f->getFilename(), 0, 1 ) != '.' ) {
                copy($f->getRealPath(), "$dest/" . $f->getFilename());
            } else if(!$f->isDot() && $f->isDir() && substr( $f->getFilename(), 0, 1 ) != '.' ) {
                $this->rCopy($f->getRealPath(), "$dest/$f");
            }
        }
    }

    public function mkDir( $src ){
        if ( is_dir( $src ) ) return false;
        if ( !mkdir( $src ) ) return false;
        return true;
    }

    public function rmDir( $dir ) {
        exec( "rd /S /Q " . escapeshellarg( $dir ) );
    }

    public function zipFolder( $folder, $zipFile = 'zipped.zip' ) {
        $rootPath = realpath( $folder );
        $zip = new ZipArchive();
        $zip->open( $zipFile, ZipArchive::CREATE);

        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($rootPath),
            RecursiveIteratorIterator::LEAVES_ONLY
        );

        foreach ($files as $name => $file) {
            // Create zip file name
            $newFileName =  str_replace( $rootPath, '', $file );
            $newFileName = ltrim( str_replace( '\\', '/', $newFileName ), '/' );

            // Allow htaccess to be added to zip
            if ( '.htaccess' !== $file->getFilename() ) {

                if ( '.' == $file->getFilename() ||
                    '.' == $file->getFilename() ||
                    '.' == substr( $file->getFilename(), 0, 1 ) ||
                    '.' == substr( $newFileName, 0, 1 ) ) {
                    continue;
                }

            }
            // If file
            if ( is_file( $file ) ) {
                $zip->addFile( $file, $newFileName );
            }
        }

        $zip->close();
    }

    public function unzipToFolder( $zipFile, $destFolder ) {
        $zip = new ZipArchive();
        $res = $zip->open( $zipFile );
        if ( true == $res ) {
            $zip->extractTo( $destFolder );
            return true;
        } else {
            return false;
        }
    }

    public function writeToFile( $file, $content ) {
        if ( is_writable( $file ) ) {
            file_put_contents( $file, $content );
            return true;
        } else {
            return false;
        }
    }

    public function exec( $cmd ) {
        return exec( $cmd );
    }
}
