Ejemplo n.º 1
0
function fileStructure($dir, $file, $filename, $album_id, $album_add_time)
{
    global $cfg, $db, $lastGenre_id, $getID3, $dirsCounter, $filesCounter, $curFilesCounter, $curDirsCounter, $prevDirsCounter;
    /* unused because of writing ID into album_id table 
    	if ($album_id == '') {
    		$album_id = base_convert(uniqid(), 16, 36);
    		$album_add_time = time();
    		if (file_put_contents($dir . $album_id . '.id', '') === false)
    			message(__FILE__, __LINE__, 'error', '[b]Failed to write file:[/b][br]' . $dir . $album_id . '.id[list][*]Check file/directory permission.[/list]');
    	}
    	elseif (preg_match('#^[a-z0-9]{10,11}$#', $album_id) == false)
    		message(__FILE__, __LINE__, 'error', '[b]This is not a valid id:[/b][br]' . $dir . $album_id . '.id[list][*]Remove this id and update again.[/list]');
    	else
    		$album_add_time = filemtime($dir . $album_id . '.id');
    	 */
    // Also needed for track update!
    $discs = 1;
    $disc_digits = 0;
    $track_digits = 0;
    $year = NULL;
    $month = NULL;
    $artist = 'Unknown AlbumArtist';
    $aGenre = 'Unknown Genre';
    $album_dr = NULL;
    if ($cfg['name_source'] != 'tags') {
        if (preg_match('#^(0{0,1}1)(0{1,3}1)+\\.\\s+.+#', $filename[0], $match) && preg_match('#^(\\d{' . strlen($match[1] . $match[2]) . '})+\\.\\s+.+#', $filename[count($filename) - 1])) {
            // Multi disc
            $disc_digits = strlen($match[1]);
            $track_digits = strlen($match[2]);
            preg_match('#^(\\d{' . $disc_digits . '})\\d{' . $track_digits . '}+\\.\\s+#', $filename[count($filename) - 1], $match);
            $discs = $match[1];
        } elseif (preg_match('#^(\\d{2,4})+\\.\\s+.+#', $filename[0], $match)) {
            // Single disc
            $track_digits = strlen($match[1]);
        }
        $temp = decodeEscapeChar($dir);
        $temp = explode('/', $temp);
        $n = count($temp);
        $artist_alphabetic = $temp[$n - 3];
        $artist = $artist_alphabetic;
        $album = $temp[$n - 2];
        if (preg_match('#^(\\d{4})\\s+-\\s+(.+)#', $album, $match)) {
            $year = $match[1];
            $album = $match[2];
        } elseif (preg_match('#^(\\d{4})(0[1-9]|1[012])\\s+-\\s+(.+)#', $album, $match)) {
            $year = $match[1];
            $month = $match[2];
            $album = $match[3];
        }
    }
    if ($cfg['name_source'] == 'tags') {
        $ThisFileInfo = $getID3->analyze($file[0]);
        getid3_lib::CopyTagsToComments($ThisFileInfo);
        if (isset($ThisFileInfo['comments']['albumartist'][0])) {
            $artist = $ThisFileInfo['comments']['albumartist'][0];
        } elseif (isset($ThisFileInfo['comments']['band'][0])) {
            $artist = $ThisFileInfo['comments']['band'][0];
        }
        //elseif (isset($ThisFileInfo['comments']['albumartist'][0])) $artist = $ThisFileInfo['comments']['albumartist'][0];
        if ($artist == 'Unknown AlbumArtist') {
            //if (isset($ThisFileInfo['comments']['artist'][1])) $artist = $ThisFileInfo['comments']['artist'][1];
            if (isset($ThisFileInfo['comments']['artist'][0])) {
                $artist = $ThisFileInfo['comments']['artist'][0];
            }
            //elseif (isset($ThisFileInfo['id3v2']['comments']['artist'][0])) $artist = $ThisFileInfo['id3v2']['comments']['artist'][0];
            //elseif (isset($ThisFileInfo['ape']['comments']['artist'][0])) $artist = $ThisFileInfo['ape']['comments']['artist'][0];
        }
        $artist_alphabetic = $artist;
        if (isset($ThisFileInfo['comments']['year'][0])) {
            $year = $ThisFileInfo['comments']['year'][0];
        } elseif (isset($ThisFileInfo['comments']['date'][0])) {
            $year = $ThisFileInfo['comments']['date'][0];
        }
        if (preg_match('#[1][9][0-9]{2}|[2][0-9]{3}#', $year, $match)) {
            $year = $match[0];
        }
        if (isset($ThisFileInfo['comments']['album dynamic range'][0])) {
            $album_dr = $ThisFileInfo['comments']['album dynamic range'][0];
        } elseif (isset($ThisFileInfo['tags']['id3v2']['text']['ALBUM DYNAMIC RANGE'])) {
            $album_dr = $ThisFileInfo['tags']['id3v2']['text']['ALBUM DYNAMIC RANGE'];
        }
        if (isset($ThisFileInfo['comments']['genre'][0])) {
            $aGenre = $ThisFileInfo['comments']['genre'][0];
        }
        if (strpos(strtolower($dir), strtolower($cfg['misc_tracks_folder'])) === false && strpos(strtolower($dir), strtolower($cfg['misc_tracks_misc_artists_folder'])) === false) {
            //if (isset($ThisFileInfo['comments']['album'][1])) $album = $ThisFileInfo['comments']['album'][1];
            if (isset($ThisFileInfo['comments']['album'][0])) {
                $album = $ThisFileInfo['comments']['album'][0];
            } else {
                $album = 'Unknown Album Title';
            }
        } elseif (strpos(strtolower($dir), strtolower($cfg['misc_tracks_folder'])) !== false) {
            $year = NULL;
            $album = $cfg['misc_tracks_folder'] . $artist;
            /* if (strtolower(basename($dir)) == strtolower($cfg['misc_tracks_folder'])) 
            				$album = $cfg['misc_tracks_folder'] . $artist;				
            			else
            				$album = basename($dir); */
        } elseif (strpos(strtolower($dir), strtolower($cfg['misc_tracks_misc_artists_folder'])) !== false) {
            $artist = 'Various Artists';
            $artist_alphabetic = $artist;
            $aGenre = NULL;
            $year = NULL;
            if (strtolower(basename($dir)) == strtolower($cfg['misc_tracks_misc_artists_folder'])) {
                $album = $cfg['misc_tracks_misc_artists_folder'];
            } else {
                $album = basename($dir);
            }
        }
        $result = mysql_query('SELECT genre_id FROM genre WHERE genre="' . mysql_real_escape_string($aGenre) . '"');
        $row = mysql_fetch_assoc($result);
        $aGenre_id = $row["genre_id"];
        if (mysql_num_rows($result) == 0) {
            mysql_query('INSERT INTO genre (genre_id, genre, updated)
						VALUES ("' . mysql_real_escape_string($lastGenre_id) . '",
								"' . mysql_real_escape_string($aGenre) . '",
								1)');
            $aGenre_id = $lastGenre_id;
            ++$lastGenre_id;
        } else {
            $result = mysql_query("update genre set \n\t\t\t\tupdated = 1\n\t\t\t\tWHERE genre = '" . mysql_real_escape_string($aGenre) . "';");
        }
    }
    //
    // Update/Insert album information on the end of this function to be able to include image_id
    //
    ++$curDirsCounter;
    if ($cfg['cli_update'] == false && (microtime(true) - $cfg['timer']) * 1000 > $cfg['update_refresh_time'] && $curDirsCounter / $dirsCounter > $prevDirsCounter / $dirsCounter + 0.005) {
        $prevDirsCounter = $curDirsCounter;
        mysql_query('update update_progress set 
			structure_image = "<div class=\'out\'><div class=\'in\' style=\'width:' . html(floor($curDirsCounter / $dirsCounter * 200)) . 'px\'></div></div> ' . html(floor($curDirsCounter / $dirsCounter * 100)) . '%"');
        // @ob_flush();
        // flush();
        $cfg['timer'] = microtime(true);
    }
    if ($cfg['cli_update'] && $cfg['cli_silent_update'] == false) {
        echo $artist_alphabetic . ' - ' . $album . "\n";
    }
    // Track update
    $disc = 1;
    $number = NULL;
    for ($i = 0; $i < count($filename); $i++) {
        $relative_file = substr($file[$i], strlen($cfg['media_dir']));
        mysql_query('UPDATE track SET
			updated				= 1
			WHERE album_id		= "' . mysql_real_escape_string($album_id) . '"
			AND relative_file	= BINARY "' . mysql_real_escape_string($relative_file) . '"
			LIMIT 1');
        if ($cfg['force_filename_update'] || mysql_affected_rows($db) == 0) {
            $temp = decodeEscapeChar($filename[$i]);
            if ($cfg['name_source'] != 'tags') {
                //if (preg_match('#^(\d{' . $disc_digits . '})(\d{' . $track_digits . '})\s+-\s+(.+)#', $temp, $match)) {
                if (preg_match('#^(\\d{' . $disc_digits . '})(\\d{' . $track_digits . '})+\\.\\s+(.+)#', $temp, $match)) {
                    if ($disc_digits > 0) {
                        // Multiple disc
                        $disc = $match[1];
                        $number = $match[2];
                    } else {
                        // Single disc
                        $number = $match[2];
                    }
                    $temp = $match[3];
                    // Strip disc and track number
                }
                if (preg_match('#^(.+?)\\s+-\\s+(.+?)(?:\\s+Ft\\.\\s+(.+))?$#i', $temp, $match)) {
                    $track_artist = $match[1];
                    $title = $match[2];
                    $featuring = isset($match[3]) ? $match[3] : '';
                } elseif (preg_match('#^(.+?)(?:\\s+Ft\\.\\s+(.+))?$#i', $temp, $match)) {
                    $track_artist = $artist;
                    $title = $match[1];
                    $featuring = isset($match[2]) ? $match[2] : '';
                } else {
                    $track_artist = '*** UNSUPPORTED FILENAME FORMAT ***';
                    $title = '(' . $filename[$i] . ')';
                    $featuring = '';
                }
            }
            if ($cfg['name_source'] == 'tags') {
                $ThisFileInfo = $getID3->analyze($file[$i]);
                getid3_lib::CopyTagsToComments($ThisFileInfo);
                //$number = $ThisFileInfo['comments']['tracknumber'][0];
                if (isset($ThisFileInfo['comments']['tracknumber'][0])) {
                    $number = $ThisFileInfo['comments']['tracknumber'][0];
                } elseif (isset($ThisFileInfo['comments']['track_number'][0])) {
                    $number = $ThisFileInfo['comments']['track_number'][0];
                } elseif (isset($ThisFileInfo['comments']['track'][0])) {
                    $number = $ThisFileInfo['comments']['track'][0];
                }
                //if (isset($ThisFileInfo['comments']['artist'][1])) $track_artist = $ThisFileInfo['comments']['artist'][1];
                if (isset($ThisFileInfo['comments']['artist'][0])) {
                    $track_artist = $ThisFileInfo['comments']['artist'][0];
                } else {
                    $track_artist = 'Unknown Artist';
                }
                //if (isset($ThisFileInfo['comments']['title'][1])) $title = $ThisFileInfo['comments']['title'][1];
                if (isset($ThisFileInfo['comments']['title'][0])) {
                    $title = $ThisFileInfo['comments']['title'][0];
                } else {
                    $album = 'Unknown Title';
                }
                $featuring = '';
            }
            if (mysql_affected_rows($db) == 0) {
                mysql_query('INSERT INTO track (artist, featuring, title, relative_file, disc, number, album_id, updated)
					VALUES ("' . mysql_real_escape_string($track_artist) . '",
					"' . mysql_real_escape_string($featuring) . '",
					"' . mysql_real_escape_string($title) . '",
					"' . mysql_real_escape_string($relative_file) . '",
					' . (int) $disc . ',
					' . (is_null($number) ? 'NULL' : (int) $number) . ',
					"' . mysql_real_escape_string($album_id) . '",
					1)');
            } else {
                mysql_query('UPDATE track SET
					artist				= "' . mysql_real_escape_string($track_artist) . '",
					featuring			= "' . mysql_real_escape_string($featuring) . '",
					title				= "' . mysql_real_escape_string($title) . '",
					relative_file		= "' . mysql_real_escape_string($relative_file) . '",
					disc				= ' . (int) $disc . ',
					number				= ' . (is_null($number) ? 'NULL' : (int) $number) . ',
					album_id			= "' . mysql_real_escape_string($album_id) . '",
					updated				= 1
					WHERE album_id		= "' . mysql_real_escape_string($album_id) . '"
					AND relative_file	= BINARY "' . mysql_real_escape_string($relative_file) . '"
					LIMIT 1');
            }
        }
    }
    // Image update
    $image = NJB_HOME_DIR . 'image/no_image.png';
    $flag = 0;
    // No image
    $misc_tracks = false;
    if (is_file($dir . $cfg['image_front'] . '.jpg')) {
        $image = $dir . $cfg['image_front'] . '.jpg';
        $flag = 3;
        /* Stored image */
    } elseif (is_file($dir . $cfg['image_front'] . '.png')) {
        $image = $dir . $cfg['image_front'] . '.png';
        $flag = 3;
        /* Stored image */
    } elseif (strpos(strtolower($dir), strtolower($cfg['misc_tracks_folder'])) !== false || strpos(strtolower($dir), strtolower($cfg['misc_tracks_misc_artists_folder'])) !== false) {
        $image = NJB_HOME_DIR . 'image/misc_image.jpg';
        $flag = 3;
        /* Stored image */
        $misc_tracks = true;
    } elseif ($cfg['image_read_embedded']) {
        $getID3->analyze($file[0]);
        if (isset($ThisFileInfo['error']) == false && isset($ThisFileInfo['comments']['picture'][0]['image_mime']) && isset($ThisFileInfo['comments']['picture'][0]['data']) && ($ThisFileInfo['comments']['picture'][0]['image_mime'] == 'image/jpeg' || $ThisFileInfo['comments']['picture'][0]['image_mime'] == 'image/png')) {
            if ($ThisFileInfo['comments']['picture'][0]['image_mime'] == 'image/jpeg') {
                $image = NJB_HOME_DIR . 'tmp/' . $cfg['image_front'] . '.jpg';
            }
            if ($ThisFileInfo['comments']['picture'][0]['image_mime'] == 'image/png') {
                $image = NJB_HOME_DIR . 'tmp/' . $cfg['image_front'] . '.png';
            }
            if (file_put_contents($image, $ThisFileInfo['comments']['picture'][0]['data']) === false) {
                message(__FILE__, __LINE__, 'error', '[b]Failed to wtite image to:[/b][br]' . $image);
            }
            $flag = 0;
            // No image
        }
        unset($getID3);
    }
    $relative_dir = substr($dir, strlen($cfg['media_dir']));
    if (is_file($dir . $cfg['image_front'] . '.jpg')) {
        $image_front = $relative_dir . $cfg['image_front'] . '.jpg';
    } elseif (is_file($dir . $cfg['image_front'] . '.png')) {
        $image_front = $relative_dir . $cfg['image_front'] . '.png';
    } elseif ($misc_tracks) {
        $image_front = $image;
    } else {
        $image_front = '';
    }
    if (is_file($dir . $cfg['image_back'] . '.jpg')) {
        $image_back = $relative_dir . $cfg['image_back'] . '.jpg';
    } elseif (is_file($dir . $cfg['image_back'] . '.png')) {
        $image_back = $relative_dir . $cfg['image_back'] . '.png';
    } else {
        $image_back = '';
    }
    $filesize = filesize($image);
    $filemtime = filemtime($image);
    $query = mysql_query('SELECT filesize, filemtime, image_id, flag FROM bitmap WHERE album_id = "' . mysql_real_escape_string($album_id) . '"');
    $bitmap = mysql_fetch_assoc($query);
    if ($bitmap['filesize'] == $filesize && filemtimeCompare($bitmap['filemtime'], $filemtime)) {
        mysql_query('UPDATE bitmap SET
			image_front			= "' . mysql_real_escape_string($image_front) . '",
			image_back			= "' . mysql_real_escape_string($image_back) . '",
			updated				= 1
			WHERE album_id		= "' . mysql_real_escape_string($album_id) . '"
			LIMIT 1');
        $image_id = $bitmap['image_id'];
    } else {
        $imagesize = @getimagesize($image) or message(__FILE__, __LINE__, 'error', '[b]Failed to read image information from:[/b][br]' . $image);
        $image_id = $flag == 3 ? $album_id : 'no_image';
        $image_id .= '_' . base_convert(NJB_IMAGE_SIZE * 100 + NJB_IMAGE_QUALITY, 10, 36) . base_convert($filemtime, 10, 36) . base_convert($filesize, 10, 36);
        if ($bitmap['filemtime']) {
            mysql_query('UPDATE bitmap SET
				image				= "' . mysql_real_escape_string(resampleImage($image)) . '",
				filesize			= ' . (int) $filesize . ',
				filemtime			= ' . (int) $filemtime . ',
				flag				= ' . (int) $flag . ',
				image_front			= "' . mysql_real_escape_string($image_front) . '",
				image_back			= "' . mysql_real_escape_string($image_back) . '",
				image_front_width	= ' . ($flag == 3 ? $imagesize[0] : 0) . ',
				image_front_height	= ' . ($flag == 3 ? $imagesize[1] : 0) . ',
				image_id			= "' . mysql_real_escape_string($image_id) . '",
				updated				= 1
				WHERE album_id	= "' . mysql_real_escape_string($album_id) . '"
				LIMIT 1');
        } else {
            mysql_query('INSERT INTO bitmap (image, filesize, filemtime, flag, image_front, image_back, image_front_width, image_front_height, image_id, album_id, updated)
				VALUES ("' . mysql_real_escape_string(resampleImage($image)) . '",
				' . (int) $filesize . ',
				' . (int) $filemtime . ',
				' . (int) $flag . ',
				"' . mysql_real_escape_string($image_front) . '",
				"' . mysql_real_escape_string($image_back) . '",
				' . ($flag == 3 ? $imagesize[0] : 0) . ',
				' . ($flag == 3 ? $imagesize[1] : 0) . ',
				"' . mysql_real_escape_string($image_id) . '",
				"' . mysql_real_escape_string($album_id) . '",
				1)');
        }
    }
    mysql_query('UPDATE album SET
		artist_alphabetic	= "' . mysql_real_escape_string($artist_alphabetic) . '",
		artist				= "' . mysql_real_escape_string($artist) . '",
		album				= "' . mysql_real_escape_string($album) . '",
		year				= ' . (is_null($year) ? 'NULL' : (int) $year) . ',
		month				= ' . (is_null($month) ? 'NULL' : (int) $month) . ',
		discs				= ' . (int) $discs . ',
		image_id			= "' . mysql_real_escape_string($image_id) . '",
		genre_id				= "' . mysql_real_escape_string($aGenre_id) . '",
		updated				= 1,
		album_dr			= ' . (is_null($album_dr) ? 'NULL' : (int) $album_dr) . '
		WHERE album_id		= "' . mysql_real_escape_string($album_id) . '"
		LIMIT 1');
    if (mysql_affected_rows($db) == 0) {
        mysql_query('INSERT INTO album (artist_alphabetic, artist, album, year, month, genre_id, album_add_time, discs, image_id, album_id, updated, album_dr)
			VALUES (
			"' . mysql_real_escape_string($artist_alphabetic) . '",
			"' . mysql_real_escape_string($artist) . '",
			"' . mysql_real_escape_string($album) . '",
			' . (is_null($year) ? 'NULL' : (int) $year) . ',
			' . (is_null($month) ? 'NULL' : (int) $month) . ',
			' . (int) $aGenre_id . ',
			' . (int) $album_add_time . ',
			' . (int) $discs . ',
			"' . mysql_real_escape_string($image_id) . '",
			"' . mysql_real_escape_string($album_id) . '",
			1,
			' . (is_null($album_dr) ? 'NULL' : (int) $album_dr) . ')');
    }
    // Close getID3
    unset($getID3);
}
Ejemplo n.º 2
0
function copyFilename($filename)
{
    global $cfg;
    // Decode filename
    $filename = decodeEscapeChar($filename);
    // Example: %3F to ?
    // Encode for compatibility
    foreach ($cfg['album_copy_char_limit'] as $key => $value) {
        $filename = str_replace($value, $cfg['escape_char'][$value], $filename);
    }
    // Example: ? to %3F
    return $filename;
}