<?php
function grep_string($file_formats)
{
$grep_string = str_replace(" ","$ -e ",$file_formats);
$grep_string = $grep_string."$";
return $grep_string;
}
function get_list_from_db($mode,$repository,$subdir)
{
global $musiclib;
echo "Getting list from database ...<br>";
$query = construct_song_select("WHERE repository=".$repository);
$result = $GLOBALS['phpgw']->db->query($query,__LINE__,__FILE__);
$x=0;
while ($GLOBALS['phpgw']->db->next_record())
{
$row = $GLOBALS['phpgw']->db->Record;
# print_r($row);
$files_from_db_array[$x] = $musiclib['config']['music_path'].'/'.$subdir
.generate_filename($mode,$row);
# echo "$filename<BR>";
$x++;
}
return $files_from_db_array;
}
function get_list_from_fs($subdir,$grep_string)
{
global $musiclib;
$music_list = $musiclib['config']['music_list'].'.'.strtolower($subdir);
if ($musiclib['config']['remote_library'] == '1')
{
if (file_exists($music_list))
{
echo "Getting list from remote filesystem ".$music_list."<br />";
$filelistFromFs = $music_list;
}
else
{
echo "File list not found. Script Terminating.<br />";
exit;
}
}
else
{
echo "Getting list from local filesystem ".$musiclib['config']['music_path'].'/'.$subdir."<br />";
$filelistFromFs = $GLOBALS['phpgw_info']['server']['temp_dir'].'/'.$subdir."_filelistFromFs.txt";
$command = "find ".$musiclib['config']['music_path'].'/'.$subdir
." -type f | grep -i -e ".$grep_string
." > ".$filelistFromFs;
echo $command."<br />";
shell_exec($command);
}
// Read files from filesystem into an array
$fp = fopen($filelistFromFs, "r");
$x=0;
while ($line = fgets($fp, 1024))
{
$files_from_fs_array[$x] = trim($line);
$x++;
}
fclose ($fp);
if (!count($files_from_fs_array) or count($files_from_fs_array) < 1)
{
echo "No files to process. Script Terminating. <br />";
exit;
}
return $files_from_fs_array;
}
function music_to_add($files_from_db_array,$files_from_fs_array)
{
if (count($files_from_db_array) > 0 and count($files_from_fs_array) > 0)
{
$to_be_added = array_diff($files_from_fs_array, $files_from_db_array);
# if db is empty and fs is not
}
else if (count($files_from_db_array) == 0 and count($files_from_fs_array) > 0)
{
$to_be_added = $files_from_fs_array;
}
else
{
$to_be_added = array();
}
sort($to_be_added);
return $to_be_added;
}
function music_to_del($files_from_db_array,$files_from_fs_array)
{
if (count($files_from_db_array) > 0 and count($files_from_fs_array) > 0)
{
$to_be_deleted = array_diff($files_from_db_array, $files_from_fs_array);
# if fs is empty and db is not
}
else if (count($files_from_db_array) > 0 and count($files_from_fs_array) == 0)
{
$to_be_deleted = $files_from_db_array;
}
else
{
$to_be_deleted = array();
}
return $to_be_deleted;
}
function del_tmp_files()
{
shell_exec ("rm -f ".$GLOBALS['phpgw_info']['server']['temp_dir']."/*_filelistFromFs.txt");
}
?>