Migrate to AwlQuery and preg*, and remove need for globals.
[adorno.git] / www / edit_track.php
blobc7111be50c867c43215bb9e482c22e57eb52620b
1 <?php
2 $refresh = false;
3 $page_title = "Edit Track Information";
4 include("always.php");
6 if ( isset( $submit ) && $submit == "update" ) {
7 // update the track
8 // $actions = "<p>Updating tracks";
9 for ( $i=0 ; isset( $hash[$i] ); $i++ ) {
10 $q = "SELECT * FROM tracks WHERE hash_key='$hash[$i]';";
11 $res = awm_pgexec( $dbconn, $q );
12 // $actions .= "<p>Query: $q";
13 if ( $res && pg_NumRows( $res) == 1 ) {
14 $track = pg_Fetch_Object( $res, 0 );
15 // $actions .= "<p>Editing: $track->title";
16 if ( ! isset( $artist[$i] ) ) $artist[$i] = $artist[0];
17 if ( ! isset( $album[$i] ) ) $album[$i] = $album[0];
18 if ( preg_match( '{\.ogg$}i', $track->path_name ) ) {
19 // Update the details on an ogg file
20 $command = "/usr/bin/vorbiscomment -w " . escapeshellarg($track->path_name);
21 $command .= " -t " . escapeshellarg( "ARTIST=$artist[$i]" );
22 $command .= " -t " . escapeshellarg( "ALBUM=$album[$i]" );
23 $command .= " -t " . escapeshellarg( "TITLE=$title[$i]" );
24 $command .= " -t " . escapeshellarg( "TRACKNUMBER=$tracknum[$i]" );
26 else {
27 // Update the details on an mp3 file
28 $command = "/usr/bin/id3v2 -a " . escapeshellarg($artist[$i]) . " -A " . escapeshellarg($album[$i]);
29 $command .= " -t " . escapeshellarg($title[$i]) . " -T " . escapeshellarg($tracknum[$i]) . " ". escapeshellarg($track->path_name);
31 // $actions .= "<p>Command: $command";
32 $result = `$command`;
33 if ( preg_match( '{error.*opening}i', $result ) ) {
34 $actions .= "<p class=error>Error opening file - are your files / directories mode 666 / 777?";
36 $command = "/usr/bin/md5sum " . escapeshellarg($track->path_name);
37 $md5 = `$command`;
38 // $actions .= "<p>Command: $command";
39 list( $newhashkey, $track) = explode( " ", trim($md5), 2);
40 $q = "UPDATE played SET hash_key = '$newhashkey' WHERE hash_key = '" . pg_escape_string($hash[$i]) . "'; ";
41 $q .= "UPDATE tracks SET hash_key = '$newhashkey', ";
42 $q .= "artist = '" . pg_escape_string($artist[$i]) . "', ";
43 $q .= "album = '" . pg_escape_string($album[$i]) . "', ";
44 $q .= "title = '" . pg_escape_string($title[$i]) . "', ";
45 $q .= "tracknum = '" . pg_escape_string($tracknum[$i]) . "' ";
46 $q .= "WHERE hash_key = '" . pg_escape_string($hash[$i]) . "'; ";
47 $res = awm_pgexec( $dbconn, $q );
48 // $actions .= "<p>Query: $q";
49 $actions .= "<p>Updated $title[$i]";
51 else if ( $res ) {
52 $actions .= "<p class=error>Looks like the hash $hash[$i] is insufficiently unique!";
54 else {
55 $actions .= "<p class=error>No tracks found for hash $hash[$i] !";
60 include("header.php");
62 echo "$actions";
64 $letter_get = ( "$altr" == "" ? "" : "&altr=$altr" ) . ( "$lltr" == "" ? "" : "&lltr=$lltr" );
66 function show_edit( $i, $trk ) {
67 echo "<input type=hidden name=hash[$i] value=$trk->hash_key>\n";
68 if ( $i == 0 ) {
69 echo "<tr><td colspan=\"4\"><hr style=\"height: 5px;\"></td></tr>\n";
70 echo "<tr><th align=right>Artist:</th>";
71 echo "<td colspan=\"3\"><input type=text size=60 name=artist[$i] value=\"" . htmlentities($trk->artist) . "\"></td></tr>\n";
72 echo "<tr><th align=right>Album:</th>";
73 echo "<td colspan=\"3\"><input type=text size=60 name=album[$i] value=\"" . htmlentities($trk->album) . "\"></td></tr>\n";
74 echo "<tr><td colspan=\"4\"><hr style=\"height: 5px;\"></td></tr>\n";
76 $class = sprintf( 'class="row%d"', $i%2 );
77 $prompt_fmt = sprintf( '<th %s align="right">%%s:</th><td %s><input type="text" size="%%d" name="%%s" value="%%s">%%s</td>', $class, $class );
78 $row = sprintf( "<tr %s>%s%s</tr>", $class, $prompt_fmt, $prompt_fmt );
79 printf( $row, "Number", 3, "tracknum[$i]", $trk->tracknum, "",
80 "Title", 40, "title[$i]", htmlentities($trk->title), " ".$trk->path_name );
81 echo "<tr><td colspan=\"4\"><hr></td></tr>\n";
84 if ( isset($l) && isset($a) ) {
85 $q = "SELECT * FROM tracks WHERE album = '" . addslashes($l) . "' AND artist = '" . addslashes($a) . "' ";
86 if ( isset($t) ) {
87 $q .= "AND title='" . addslashes($t) . "' ";
89 $q .= "ORDER BY tracknum; ";
90 $res = awm_pgexec( $dbconn, $q, "db");
91 if ( $res && pg_NumRows($res) > 0 ) {
92 echo "<form method=post>\n";
93 echo "<table width=100%>\n";
94 for ( $i=0; $i < pg_NumRows($res); $i++ ) {
95 $track = pg_Fetch_Object( $res, $i );
96 show_edit( $i, $track );
98 echo "<tr><td>&nbsp;</td><td align=left><input type=submit name=submit value=update></td></tr>\n";
99 echo "</table>\n";
100 echo "</form>\n";
103 else {
104 echo "<h1>Error: No Album Selected</h1>
105 <p>You will need to select an album or a track for editing";
108 show_queue();
111 </body>
112 </html>