~tim-alwaysreformed/+junk/md5xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

clearstatcache();

$sourcepath = ".";

$dirs = array();
$files = array();

// Walk a directory recursivelly, and apply a callback on each file
function m_walk_dir( $root, $callback, $recursive = true ) {
    $dh = @opendir( $root );
    if( false === $dh ) {
        return false;
    }
    while( $file = readdir( $dh )) {
        if( "." == $file || ".." == $file ){
            continue;
        }
        call_user_func( $callback, "{$root}/{$file}" );
        if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
            m_walk_dir( "{$root}/{$file}", $callback, $recursive );
        }
    }
    closedir( $dh );
    return true;
}

function append_contents( $path ) {
    global $dirs;
    global $files;
    // Add some time since we're listing a lot of files
    set_time_limit(1);
    if (is_dir($path)){
        $dirs[] = $path;
        /*
        echo $path . "\n";
        echo count($dirs);
        echo "\n\n";
        */
    }else{
        $files[] = $path;
        /*
        echo $path . "\n";
        echo count($files);
        echo "\n\n";
        */
    }
}

// Replace \ by / and remove the final / if any
$root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/", $sourcepath ));
// Touch all the files from the $root directory
if( false === m_walk_dir( $root, "append_contents", true )) {
    echo "'{$root}' is not a valid directory\n";
}else{
    $out[] = '<?xml version="1.0"?>';
    $out[] = '<md5Xml>';
    $out[] = '<folders>';
    sort($dirs);
    foreach($dirs as $dir){
        $out[] = "<folder name=\"$dir\" />";
    }
    $out[] = '</folders>';
    $out[] = '<files>';
    sort($files);
    foreach($files as $file){
        // Add some time since opening & hashing mp3s takes some time.
        set_time_limit(10);
        $hash = md5_file($sourcepath . "/" . $file);
        $out[] = "<file name=\"$file\" md5=\"$hash\" />";
    }
    $out[] = '</files>';
    $out[] = '</md5Xml>';

    // write XML to file
    //echo implode("\n", $out);
    $fh = fopen("out.xml", 'w');
    fwrite($fh, implode("\n", $out));
    fclose($fh);
    
}

echo "<br />Done!";

?>