New Songbook
[songbook.git] / web / songbook.php
1 <?php
2 $baseDir = getcwd();
3 $songs   = glob (dirname($baseDir) . "/Songs/*.pro");
4 $debug   = $_REQUEST["debug"];
5
6 function debug ($msg) {
7   global $debug;
8
9   if (isset ($debug)) {
10     echo "<font color=red>DEBUG:</font> $msg<br>";
11   } // if
12 } // debug
13
14 function getSongs () {
15   global $songs;
16
17   $path = "/opt/songbook/Songs";
18
19   // Why didn't the previous one execute correctly?
20   $songs = glob("$path/*.pro");
21 } // getSongs
22
23 function songsDropdown () {
24   global $songs;
25
26   print "<form method=\"get\" action=\"webchord.cgi\" name=\"song\">";
27   print "Songs:&nbsp;&nbsp;";
28   print "<select name=\"chordpro\">";
29
30   sort ($songs);
31   foreach ($songs as $song) {
32     $title = basename ($song, ".pro");
33     $artist = getArtist ($song);
34
35     print "<option value=\"$title.pro\">$title</option>";
36
37     if ($artist != "") {
38       $title .= "&nbsp;($artist)";
39     } // if
40   } // foreach
41
42   print "<input type=\"submit\" value=\"Go\">";
43   print "</select>";
44   print "</form>";
45 } // songsDropdown
46
47 function artistsDropdown () {
48   global $songs;
49
50   $artists = getArtists ($songs);
51
52   print "<form method=\"get\" action=\"displayartist.php\" name=\"artist\">";
53   print "Artists:&nbsp;&nbsp;";
54   print "<select name=\"artist\">";
55
56   sort ($artists);
57   foreach ($artists as $artist) {
58     print "<option>$artist</option>";
59   } // foreach
60
61   print "<input type=\"submit\" value=\"Go\">";
62   print "</select>";
63   print "</form>";
64 } // artistsDropdown
65
66 function getArtist ($song) {
67   $lyrics = file_get_contents ($song);
68
69   if (preg_match ("/\{(st|subtitle):(.*)\}/", $lyrics, $matches)) {
70     return trim ($matches[2]);
71   } else {
72     return "";
73   } // if
74 } // getArtist
75
76 function getArtists ($songs) {
77   $artists = array();
78
79   foreach ($songs as $song) {
80     $artist = getArtist ($song);
81
82     if ($artist != '') {
83       $artists[$artist] = 1;
84     } // if
85   } // foreach
86
87   return array_keys ($artists);
88 } // getArtists
89
90 function formatTable ($songs) {
91   echo "<ol>";
92
93   foreach ($songs as $song) {
94     $artist = getArtist ($song);
95
96     $title = basename ($song, ".pro");
97
98     echo "<li><a href=\"webchord.cgi?chordpro=$song\">$title</a>";
99
100     if ($artist != "") {
101     echo "&nbsp;($artist)";
102     } // if
103   } // foreach
104
105   echo "</ol>";
106 } // formatTable