PageRenderTime 57ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/playlist/lib/lib.php

https://github.com/dariusgm/PHPFee
PHP | 336 lines | 186 code | 112 blank | 38 comment | 13 complexity | a88346d3998ef8e1d03290498467b012 MD5 | raw file
  1. <?php
  2. #Playlist Skript by Darius Murawski !
  3. define ("quali",128000);#128kb/s Mp3
  4. define ("verzoegerung", 2);#2h
  5. function playlist()
  6. {
  7. $startzeit=microtime();
  8. #How IT Works: Idee: Darius Murawski
  9. #| Selectiere alle Datein aus dem Genre (mysql)
  10. #|-> Sortiere sie nach zuletzt gespielt, von 0 nach x(mysql)
  11. # |-> Nochmal Durchmischen (php)
  12. #| Selectiere alle Jingels (mysql)
  13. #|-> F&uuml;ge sie so oft ins Array ein, wie sie gespielt werden sollen (ab hier php)
  14. #-> Rechne aus, wie viele Jingels pro 2h gespielt werden m&uuml;ssen
  15. #-> Rechne das in Kilobyte um
  16. #-> Erzeuge die eigentliche Playlist
  17. # |-> Fuelle dafuer alle Jingels in einen Temporaeren Block
  18. # |-> Packe nun so viele Songs dazu, bis der Block voll ist.
  19. $playlist_selected=filter($_POST["playlist"]);
  20. if(!is_numeric($playlist_selected))
  21. { echo 'Fehler';exit(); }
  22. $db=mysql_connect("localhost","portal","psacln");
  23. $connect=mysql_select_db("portal",$db);
  24. # MUSIK
  25. $sql1_1="SELECT id,pfad FROM playlist_genre WHERE werbung=0";
  26. $result1_1=mysql_query($sql1_1);
  27. while($zeile1_1=mysql_fetch_array($result1_1))# Hole Alle Musikordner
  28. {
  29. $sql1_2="SELECT * FROM playlist_file WHERE genre=".$zeile1_1["id"]." ORDER BY 'played_last' ASC";#Sortiert nach AM wenigsten gespielt
  30. $result1_2=mysql_query($sql1_2);
  31. while($zeile1_2=mysql_fetch_array($result1_2))
  32. {
  33. $playlist_files[]=array($zeile1_2["genre"],$zeile1_1["pfad"].'/'.$zeile1_2["dateiname"],
  34. $zeile1_2["groesse"],$zeile1_2["played"],$zeile1_2["played_last"],$zeile1_2["avg_listeners"]);
  35. }
  36. }
  37. # Musik nochmal durchmischen
  38. shuffle($playlist_files);
  39. # JINGLES
  40. # Wir gehen zu Testzwecken von einem Anzeil der Songs von 2x pro 2h aus. Und das Playlist 1 Selektiert wurde.
  41. $sql2_1="SELECT id,pfad FROM playlist_genre WHERE werbung=1";# Anteil: Anzahl Pro 2h in denen Gespielt werden soll
  42. # echo $sql2_1;
  43. $result2_1=mysql_query($sql2_1);
  44. while($zeile2_1=mysql_fetch_array($result2_1))# Hole Alle Jingleordner
  45. {
  46. $sql2_2="SELECT dateiname FROM playlist_file WHERE genre=".$zeile2_1["id"]."";
  47. # echo $sql2_2;
  48. $result2_2=mysql_query($sql2_2);
  49. while($zeile2_2=mysql_fetch_array($result2_2))
  50. {
  51. $sql2_3="SELECT anteil FROM playlist_playlists WHERE playlist=".$playlist_selected." AND genre=".$zeile2_1["id"]."";
  52. # echo $sql2_3;
  53. $result2_3=mysql_query($sql2_3);
  54. $zeile2_3=mysql_fetch_array($result2_3);
  55. # Setze Den Jingel so oft rein wie er in 2h gespielt werden soll.
  56. $i=0;
  57. while($i<$zeile2_3["anteil"])
  58. {
  59. $playlist_jingles[]=array($zeile2_1["pfad"].'/'.$zeile2_2["dateiname"]);
  60. $i++;
  61. }
  62. }
  63. }
  64. shuffle($playlist_jingles);
  65. # CORE BEGIN
  66. # 100% Spielzeit = 2h = Qualitaet [Kilobit pro Sekunde] * 60 [Sekunden] * 60 [Minuten] * 2[h] /8 [Bit] ( Da 8 Bit 1 Byte, die dateigroesse ist in Byte)
  67. $musik_pro_block=((((quali*1.1)/8)*60*60*verzoegerung));# Byte pro 2h Block
  68. # Zeiger Der Arrays an den Anfang setzen:
  69. reset($playlist_jingles);
  70. reset($playlist_files);
  71. $playlist_fertig=''; #Buffer fuer das schreiben ins Dateisystem
  72. $playlist_block_kb=0; #Speichert die Aktuelle Groesse des Blockes
  73. $playlist_block_temp=array(); #Temp. Array fuer einen Block
  74. $umbruch="\n";
  75. echo '<br><br>';
  76. for($i=0; $i<count($playlist_files); $i++)
  77. {
  78. if ($playlist_block_kb<$musik_pro_block)
  79. {
  80. $playlist_block_temp[]=$playlist_files[$i][1];#Song hinzufuegen
  81. $playlist_block_kb=$playlist_block_kb+$playlist_files[$i][2];
  82. }
  83. else
  84. {
  85. shuffle($playlist_block_temp);#Block mischen
  86. #Temoraren Block Wegschreiben
  87. for($temp=0; $temp<count($playlist_block_temp); $temp++)
  88. {
  89. $playlist_fertig.=$playlist_block_temp[$temp].$umbruch;
  90. }
  91. #Neuen Block initalisieren
  92. $playlist_block_kb=0;
  93. $playlist_block_temp=array();
  94. for($j=0;$j<count($playlist_jingles);$j++)
  95. {
  96. #Alle Jingles einbauen
  97. $playlist_block_temp[]=$playlist_jingles[$j][0];
  98. }
  99. }
  100. }
  101. #Nun das eigentliche Schreiben der Datein ( ENDLICH !...)
  102. $datenstream = fopen("playlist".filter($_POST["stream"]).".fee", "w");
  103. fwrite($datenstream, $playlist_fertig);
  104. fclose($datenstream);
  105. echo 'Playlist erzeugt.<br />';
  106. echo 'Dauer:' ;
  107. echo (microtime()-$startzeit);
  108. echo ' ms';
  109. }
  110. function show_start()
  111. {
  112. if (isset($_POST["playlist"]))
  113. {
  114. playlist();
  115. playlist_linux_start();
  116. }
  117. else
  118. {
  119. $db=mysql_connect("localhost","portal","psacln");
  120. $connect=mysql_select_db("portal",$db);
  121. $sql1="SELECT * FROM playlist_name";
  122. $result1=mysql_query($sql1);
  123. echo '<table border="1"><th width="100">NUMMER</th><th width="200">NAME</th></tr>';
  124. while($zeile1=mysql_fetch_array($result1))
  125. {
  126. $drop.='<option value="'.$zeile1["id"].'">'.$zeile1["name"].'</option>';
  127. echo '<tr><td>'.$zeile1["id"].'</td><td>'.$zeile1["name"].'</td></tr>';
  128. }
  129. echo '</table>';
  130. echo '<form method="post" action="index.php?x=start">
  131. <input type="hidden" name="userinput" value="'.$_POST["userinput"].'" />
  132. <input type="hidden" name="passinput" value="'.$_POST["passinput"].'" />
  133. <select name="playlist">'.$drop.'</select>
  134. <select name="stream">
  135. <option value="1">Stream 1</option>
  136. <option value="2">Stream 2</option>
  137. <option value="3">Stream 3</option>
  138. </select>
  139. <button type="submit">S T A R T E N</button></form>';
  140. }
  141. }
  142. function show_stop()
  143. {
  144. if ($_POST["do"]=="stop")
  145. {
  146. playlist_linux_stop();
  147. }
  148. else
  149. {
  150. echo '<form method="post" action="index.php?x=stop">
  151. <select name="stream" size="1">
  152. <option value="1">Stream 1</option>
  153. <option value="2">Stream 2</option>
  154. <option value="3">Stream 3</option>
  155. </select>
  156. <input type="hidden" name="userinput" value="'.$_POST["userinput"].'" />
  157. <input type="hidden" name="passinput" value="'.$_POST["passinput"].'" />
  158. <input type="hidden" name="do" value="stop" />
  159. <button type="submit">S T O P P E N</button></form>';
  160. }
  161. }
  162. function playlist_linux_stop()
  163. {
  164. $db=mysql_connect("localhost","portal","psacln");
  165. $connect=mysql_select_db("portal",$db);
  166. $sql1="SELECT playlist_pid FROM grusbox_config WHERE stream=".filter($_POST["stream"]);
  167. $result1=mysql_query($sql1);
  168. $zeile1=mysql_fetch_array($result1);
  169. if ($zeile1["playlist_pid"]!=0)
  170. {
  171. exec("kill ".$zeile1["playlist_pid"]."");
  172. $sql2="UPDATE grusbox_config SET playlist_pid=0 WHERE stream=".filter($_POST["stream"]);
  173. $result2=mysql_query($sql2);
  174. echo 'Die Playlist von Stream '.filter($_POST["stream"]).' ist nun deaktiviert!';
  175. }
  176. else
  177. { echo 'Auf diesem Stream wurde keine Aktive Playlist gefunden ( ist sie aus ? )';}
  178. }
  179. function playlist_linux_start()
  180. {
  181. #Starte eine neue Instanz
  182. if (!is_numeric($_POST["stream"]))
  183. {exit();}
  184. #$cmd="./sc_trans_linux ./sc_trans".$_POST["stream"].".conf > /dev/null > $1 ";
  185. $cmd="screen -A -m -d -s pl".escapeshellcmd($_POST["stream"])." ./sc_trans_linux sc_trans".escapeshellcmd($_POST["stream"]).".conf";
  186. exec($cmd);
  187. //echo $cmd;
  188. #Warte 5 Sekunden damit das Programm gestartet werden kann
  189. sleep(5);
  190. #Hole die Prozess ID
  191. $prozessliste=shell_exec("ps -C sc_trans_linux");
  192. #$prozessliste=' PID TTY TIME CMD
  193. #24325 ? 03:01:29 sc_serv
  194. #24362 ? 00:01:26 sc_serv';
  195. #Suche Nach PID
  196. preg_match_all("/[0-9]{4,6}/", $prozessliste, $prozessid);
  197. var_dump($prozessid);
  198. $db=mysql_connect("localhost","portal","psacln");
  199. $connect=mysql_select_db("portal",$db);
  200. $sql1="SELECT playlist_pid FROM grusbox_config";
  201. $result1=mysql_query($sql1);
  202. while($zeile1=mysql_fetch_array($result1))
  203. {
  204. for($temp=0; $temp<count($prozessid); $temp++)
  205. {
  206. if ($zeile1["playlist_pid"]==$prozessid[0][$temp])
  207. {
  208. #Falls die PID bereits vorhanden ist, loesche sie aus dem Array
  209. unset($prozessid[0][$temp]);
  210. #Schleife beenden
  211. break;
  212. }
  213. }
  214. }
  215. if(count($prozessid)==0)
  216. { echo 'Fehler. Abbruch.'; exit(); }
  217. if(count($prozessid)==1)
  218. { $sql2="UPDATE grusbox_config SET playlist_pid=".$prozessid[0][0]." WHERE stream=".filter($_POST["stream"]);
  219. $result2=mysql_query($sql2);
  220. echo 'Playlist gestartet. PID: ' .$prozessid[0][0]. ' ';
  221. }
  222. }
  223. ?>