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

// Configs

// SSH
$ssh_url = "XXXXXXXXXXX";
$ssh_port = 22;
$ssh_user = "ssh-v034011";
$ssh_pass = "XXXXXXXXXXX";
$ssh_session = NULL;

// Remote
$remote_temp_dir = "/www/htdocs/v034011/_temp";
$remote_backup_dir = '/www/htdocs/v034011/_backup';
$remote_dest_dir = "teambuddy";
$remote_dest_dir_teambuddy = $remote_dest_dir . '/www/htdocs/v034011/projects/apps/teambuddy';
$remote_dest_dir_dependencies = $remote_dest_dir . '/dependencies';
$remote_dest_dir_francis = $remote_dest_dir . '/dependencies/Francis';
$remote_db_core_name= "d01c327e";
$remote_db_core_user = "d01c327e";
$remote_db_core_pass = "p4QQMb4cGhWqfftQ";
$remote_db_teamdata_name = "d01c3281";
$remote_db_teamdata_user = "d01c3281";
$remote_db_teamdata_pass = "5hWB5aFNBAzuvE2m";

// Local
$local_temp_dir = "C:/Users/Benny/Desktop/_SYSTEM/temp";
$local_source_dir = "C:/Users/Benny/PhpstormProjects/Root";
$local_path_mysql = "C:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe";
$local_path_mysqldump = "C:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump.exe";

// General
$general_verbose = true;

// Functions
function sshConnect() {
    global $ssh_session;
    $session = ssh2_connect( "xxxxxxxx", 22 );
    if ( ssh2_auth_password( $session, 'ssh-v034011', 'XXXXXXXX' ) ) {
        $ssh_session = $session;
    }
}

function sshExec( $cmd, $verbose = true ) {
    global $ssh_session;
    $stream = ssh2_exec( $ssh_session, $cmd );
    if ( $verbose ) {
        stream_set_blocking( $stream, true );
        $data = "";
        while( $buffer = fread( $stream,4096 ) ) {
            $data .= $buffer;
        }
        fclose($stream);

        debug( $data );
    }
}

function sshFileCopy( $local, $remote ) {
    global $ssh_session;
    return ssh2_scp_send( $ssh_session, $local, $remote );
}

function sshFileDownload( $remote, $local ) {
    global $ssh_session;
    return ssh2_scp_recv( $ssh_session, $remote, $local );
}

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

function localRCopy($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 ) != '.' ) {
            localRCopy($f->getRealPath(), "$dest/$f");
        }
    }
}

function localMkdir( $src ) {
    debug( "Creating local directory: $src..." );
    if ( is_dir( $src ) ) return false;
    if ( !mkdir( $src ) ) return false;
    return true;
}


function localZipFolder( $folder, $zipFile = 'packed.zip' ) {
    $rootPath = realpath( $folder );
    $zip = new ZipArchive();
    $zip->open( $zipFile, ZipArchive::CREATE);
    debug ( "Creating zip archive: $zipFile..." );

    $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 ), '/' );
        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();
    debug( "Creation of zip archive successful." );
}

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

function windowsRemoveDir( $dir ) {
    windowsCmd( "rd /S /Q " . escapeshellarg( $dir ) );
}

function debug( $txt ) {
    global $general_verbose;
    if ( !$general_verbose ) return;
    print $txt . "\n";
}
