Beispiel #1
0
function scandirr($aDir)
{
    $root = scandir($aDir);
    $results = array();
    foreach ($root as $node) {
        $nodePath = $aDir . "/" . $node;
        if (strpos($node, ".") === 0) {
            continue;
        }
        if (is_file($nodePath)) {
            $result[] = $nodePath;
            continue;
        }
        foreach (scandirr($nodePath) as $subNodes) {
            $result[] = $subNodes;
        }
    }
    return $result;
}
Beispiel #2
0
/**
 * function scandirr
 * scans a directory just like scandir(), only recursively
 * returns a hierarchical array representing the directory structure
 *
 * @pram string - directory to scan
 * @pram strin - retirn absolute paths
 * @returns array
 *
 * @author Moshe Brevda mbrevda => gmail ~ com
 */
function scandirr($dir, $absolute = false)
{
    $list = array();
    if ($absolute) {
        global $list;
    }
    //get directory contents
    foreach (scandir($dir) as $d) {
        //ignore any of the files in the array
        if (in_array($d, array('.', '..'))) {
            continue;
        }
        //if current file ($d) is a directory, call scandirr
        if (is_dir($dir . '/' . $d)) {
            if ($absolute) {
                scandirr($dir . '/' . $d, $absolute);
            } else {
                $list[$d] = scandirr($dir . '/' . $d, $absolute);
            }
            //otherwise, add the file to the list
        } elseif (is_file($dir . '/' . $d) || is_link($dir . '/' . $d)) {
            if ($absolute) {
                $list[] = $dir . '/' . $d;
            } else {
                $list[] = $d;
            }
        }
    }
    return $list;
}
Beispiel #3
0
function backup_migrate_legacy($bu)
{
    global $amp_conf;
    $legacy_name = '';
    $name = pathinfo($bu, PATHINFO_BASENAME);
    if (substr($name, -7) != '.tar.gz') {
        return false;
    }
    //get legacy name based on the directory the legacy backup was origionally created in
    //were expcecting to see something like: /tmp/ampbackups.20110310.16.00.00/
    //in the tarball
    $cmd[] = fpbx_which('tar');
    $cmd[] = 'tf';
    $cmd[] = $bu;
    exec(implode(' ', $cmd), $res);
    unset($cmd);
    foreach ($res as $r) {
        if (preg_match('/\\/tmp\\/ampbackups\\.([\\d]{8}(\\.[\\d]{2}){3})\\//', $r, $legacy_name)) {
            if (isset($legacy_name[1])) {
                $legacy_name = $legacy_name[1];
                break;
            }
        }
    }
    if (!$legacy_name) {
        return false;
    }
    //create directory where tarball will be exctracted to
    $dir = $amp_conf['ASTSPOOLDIR'] . '/tmp/' . $legacy_name;
    mkdir($dir, 0755, true);
    $cmd[] = fpbx_which('tar');
    $cmd[] = '-zxf';
    $cmd[] = $bu;
    $cmd[] = ' -C ' . $dir;
    exec(implode(' ', $cmd));
    unset($cmd);
    $dir2 = $dir . '/tmp/ampbackups.' . $legacy_name;
    //exctract sub tarballs
    foreach (scandir($dir2) as $file) {
        if (substr($file, -7) == '.tar.gz') {
            $cmd[] = fpbx_which('tar');
            $cmd[] = '-zxf';
            $cmd[] = $dir2 . '/' . $file;
            $cmd[] = ' -C ' . $dir2;
            exec(implode(' ', $cmd));
            unset($cmd);
            unlink($dir2 . '/' . $file);
        }
    }
    //add files to manifest
    $ret['file_list'] = scandirr($dir2);
    $ret['file_count'] = count($ret['file_list'], COUNT_RECURSIVE);
    $ret['fpbx_db'] = '';
    $ret['fpbx_cdrdb'] = '';
    //format db's + add to manifest
    if (is_file($dir2 . '/astdb.dump')) {
        //rename file
        rename($dir2 . '/astdb.dump', $dir2 . '/astdb');
        //remove it from the file_list
        unset($ret['file_list'][array_search('astdb.dump', $ret['file_list'])]);
        //set the manifest
        $ret['astdb'] = 'astdb';
    } elseif (is_file($dir2 . '/tmp/ampbackups.' . $legacy_name . '/astdb.dump')) {
        rename($dir2 . '/tmp/ampbackups.' . $legacy_name . '/astdb.dump', $dir2 . '/astdb');
        $ret['astdb'] = 'astdb';
    }
    //serialize the astdb
    if (!empty($ret['astdb'])) {
        $astdb = array();
        foreach (file($dir2 . '/astdb') as $line) {
            $line = explode('] [', trim($line, '[]/'));
            //chuck the bad values
            if ($line[1] == '<bad value>') {
                continue;
            }
            //expldoe the key
            list($family, $key) = explode('/', $line[0], 2);
            //add to astdb array
            $astdb[$family][$key] = trim(trim($line[1]), ']');
        }
        file_put_contents($dir2 . '/astdb', serialize($astdb));
    }
    //migrate mysql files to a format that we can restore from
    $src = $dir2 . '/asterisk.sql';
    if (is_file($src)) {
        $dst = $dir2 . '/mysql-db.sql';
        unset($ret['file_list'][array_search('asterisk.sql', $ret['file_list'])]);
        //remove from manifest
        $ret['fpbx_db'] = 'mysql-db';
        $ret['mysql']['db'] = array('file' => 'mysql-db.sql');
        // remove SET and comments that later break restores when using pear
        $cmd[] = fpbx_which('grep');
        $cmd[] = "-v '^\\/\\*\\|^SET\\|^--'";
        $cmd[] = $src;
        $cmd[] = ' > ' . $dst;
        exec(implode(' ', $cmd), $file, $status);
        if ($status) {
            // The grep failed, if there is a $dst file remove it and either way rename the $src
            freepbx_log(FPBX_LOG_ERROR, _("Failed converting asterisk.sql to proper format, renaming to mysql-db.sql in current state"));
            if (is_file($dst)) {
                unlink($dst);
            }
            rename($src, $dst);
        } else {
            unlink($src);
        }
        unset($cmd, $file);
    }
    $src = $dir2 . '/asteriskcdr.sql';
    if (is_file($src)) {
        $dst = $dir2 . '/mysql-cdr.sql';
        unset($ret['file_list'][array_search('asteriskcdr.sql', $ret['file_list'])]);
        //remove from manifest
        $ret['fpbx_cdrdb'] = 'mysql-cdr';
        $ret['mysql']['cdr'] = array('file' => 'mysql-cdr.sql');
        // remove SET and comments that later break restores when using pear
        $cmd[] = fpbx_which('grep');
        $cmd[] = "-v '^\\/\\*\\|^SET\\|^--'";
        $cmd[] = $src;
        $cmd[] = ' > ' . $dst;
        exec(implode(' ', $cmd), $file, $status);
        if ($status) {
            // The grep failed, if there is a $dst file remove it and either way rename the $src
            freepbx_log(FPBX_LOG_ERROR, _("Failed converting asteriskcdr.sql to proper format, renaming to mysql-cdr.sql in current state"));
            if (is_file($dst)) {
                unlink($dst);
            }
            rename($src, $dst);
        } else {
            unlink($src);
        }
        unset($cmd, $file);
    }
    $ret['mysql_count'] = $ret['mysql'] ? count($ret['mysql']) : 0;
    $ret['astdb_count'] = $ret['astdb'] ? count($ret['astdb']) : 0;
    //delete legacy's tmp dir
    system('rm -rf ' . $dir2 . '/tmp');
    unset($ret['file_list']['tmp']);
    //store manifest
    file_put_contents($dir2 . '/manifest', serialize($ret));
    //build new tarball
    $dest = $amp_conf['ASTSPOOLDIR'] . '/tmp/' . 'backuptmp-slegacy-' . time() . '-' . $legacy_name . '.tgz';
    $cmd[] = fpbx_which('tar');
    $cmd[] = '-zcf';
    $cmd[] = $dest;
    $cmd[] = '-C ' . $dir2;
    $cmd[] = '.';
    exec(implode(' ', $cmd));
    unset($cmd);
    //remove tmp working dir
    system('rm -rf ' . $dir);
    return $dest;
}
Beispiel #4
0
/**
 * Get list of files in log directory
 */
function logfiles_list()
{
    global $amp_conf;
    $dir = scandirr($amp_conf['ASTLOGDIR'], true);
    //only show files, relative to $amp_conf['ASTLOGDIR']
    foreach ($dir as $k => $v) {
        if (!is_file($v)) {
            unset($dir[$k]);
        } else {
            $dir[$k] = str_replace($amp_conf['ASTLOGDIR'] . '/', '', $v);
            //relative paths only
        }
    }
    return array_values($dir);
}
Beispiel #5
0
/**
 * Returns files & directories in a given directory recursively.
 *
 * Returned array is multi-dimensional with directory/file names used as keys.
 * 
 * @param string $dir Directory to scan.
 * @param int $levels Max directory depth level.
 * @param int $level Current depth level.
 * @return array Multi-dimensional array of files and directories.
 */
function scandirr($dir, $levels = 5, $level = 0)
{
    $dir = rtrim($dir, '/\\') . '/';
    $dirs = array();
    foreach (scandir($dir) as $item) {
        if ('.' !== $item && '..' !== $item) {
            if ($level < $levels && is_dir($dir . $item)) {
                $level++;
                $dirs[$item] = scandirr($dir . $item, $levels, $level);
            } else {
                $dirs[$item] = $dir . $item;
            }
        }
    }
    return $dirs;
}