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