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

/radio/index.php

https://github.com/Sazaju/Zero-Fansub-website
PHP | 408 lines | 326 code | 44 blank | 38 comment | 57 complexity | 0f4170e823e5781c894f54a6fa8a7949 MD5 | raw file
  1. <?php
  2. //
  3. // Made by db0
  4. // Contact db0company@gmail.com
  5. // Website http://db0.fr/
  6. //
  7. define('TEST_MODE_ACTIVATED', !isset($_GET['noTest']) && in_array($_SERVER["SERVER_NAME"], array(
  8. '127.0.0.1',
  9. 'localhost',
  10. 'to-do-list.me',
  11. 'www.sazaju-hitokage.fr'
  12. ), true));
  13. /**********************************\
  14. ERROR MANAGING
  15. \**********************************/
  16. function error_handler($code, $message, $file, $line)
  17. {
  18. if (0 == error_reporting())
  19. {
  20. return;
  21. }
  22. throw new ErrorException($message, 0, $code, $file, $line);
  23. }
  24. function exception_handler($exception) {
  25. if (!TEST_MODE_ACTIVATED) {
  26. // TODO
  27. $administrators = "sazaju@gmail.com";
  28. $subject = "ERROR";
  29. $message = "aze";//$exception->getMessage();
  30. $header = "From: noreply@zerofansub.net\r\n";
  31. $sent = false;//mail($administrators, $subject, $message, $header);
  32. echo "Une erreur est survenue. Rafraîchir la page peut régler le problème. Si ce n'est pas le cas, ".(
  33. $sent ? "les administrateurs en ont été notifiés"
  34. : "contactez les administrateurs : ".$administrators
  35. ).".";
  36. }
  37. else {
  38. echo "Une erreur est survenue : ".$exception;
  39. if (defined('TESTING_FEATURE')) {
  40. echo "<br/><br/>".TESTING_FEATURE;
  41. }
  42. phpinfo();
  43. }
  44. }
  45. set_error_handler("error_handler");
  46. set_exception_handler('exception_handler');
  47. if (TEST_MODE_ACTIVATED) {
  48. error_reporting(E_ALL);
  49. ini_set('display_errors', '1');
  50. ini_set('display_startup_errors', TRUE);
  51. } else {
  52. // official server, don't display errors
  53. }
  54. /*****************************\
  55. CONSTANTS
  56. \*****************************/
  57. define('PAGE_TITLE', 'Radio . Zéro');
  58. define('PLAY', 'play');
  59. define('ROOT_SONG_DIR', 'mp3');
  60. define('CLEAR_SESSION', 'clear_session');
  61. /*****************************\
  62. CLASSES
  63. \*****************************/
  64. class Song {
  65. private $path = null;
  66. private $title = null;
  67. public function __construct($path, $title = NULL) {
  68. $this->path = $path;
  69. if ($title !== NULL) {
  70. $this->title = $title;
  71. } else {
  72. $title = preg_replace("#.*/([^/]+)\\.[^.]+#", "\\1", $path);
  73. $title = preg_replace("#_+#", " ", $title);
  74. $this->title = $title;
  75. }
  76. }
  77. public function getPath() {
  78. return $this->path;
  79. }
  80. public function getTitle() {
  81. return $this->title;
  82. }
  83. }
  84. class SongDir {
  85. private $path = null;
  86. private $songs = null;
  87. private $subdirs = null;
  88. public function __construct($dirPath) {
  89. if (!is_dir($dirPath)) {
  90. throw new Exception("$dirPath is not a directory");
  91. } else {
  92. $this->path = $dirPath;
  93. }
  94. if (substr($dirPath, -1) == '/') {
  95. // already the good format
  96. } else {
  97. $dirPath = $dirPath.'/';
  98. }
  99. $this->songs = array();
  100. $this->subdirs = array();
  101. $handle = opendir($dirPath);
  102. while (($filename = readdir($handle)) !== FALSE) {
  103. $filePath = $dirPath.$filename;
  104. if (in_array($filename, array(".", ".."))) {
  105. // system directories, ignore them
  106. } else if (is_dir($filePath)) {
  107. $this->subdirs[] = new SongDir($filePath);
  108. } else if (substr($filename, -4) == '.mp3') {
  109. $this->songs[] = new Song($filePath);
  110. } else {
  111. // not a song file
  112. }
  113. }
  114. usort($this->songs, function ($a, $b) {
  115. return strnatcasecmp($a->getTitle(), $b->getTitle());
  116. });
  117. usort($this->subdirs, function ($a, $b) {
  118. return strnatcasecmp($a->getPath(), $b->getPath());
  119. });
  120. }
  121. public function getSongs() {
  122. return $this->songs;
  123. }
  124. public function getAllSongs() {
  125. $songs = array();
  126. foreach($this->subdirs as $subdir) {
  127. $songs = array_merge($songs, $subdir->getAllSongs());
  128. }
  129. $songs = array_merge($songs, $this->songs);
  130. return $songs;
  131. }
  132. public function getSubdirs() {
  133. return $this->subdirs;
  134. }
  135. public function getPath() {
  136. return $this->path;
  137. }
  138. }
  139. /*****************************\
  140. FUNCTIONS
  141. \*****************************/
  142. function showFileExtension($filepath) {
  143. preg_match('/[^?]*/', $filepath, $matches);
  144. $string = $matches[0];
  145. $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
  146. if(count($pattern) > 1) {
  147. $filenamepart = $pattern[count($pattern)-1][0];
  148. preg_match('/[^?]*/', $filenamepart, $matches);
  149. return ($matches[0]);
  150. }
  151. return ('');
  152. }
  153. function generateId(SongDir $dir) {
  154. return preg_replace("#[^a-zA-Z0-9]#", "_", $dir->getPath());
  155. }
  156. function displayDir(SongDir $dir, $displayedDir, $level = 1) {
  157. echo '<div id="'.generateId($dir).'" ';
  158. if ($level != 1 && ($displayedDir === null || strncmp($dir->getPath(), $displayedDir->getPath(), strlen($dir->getPath())) != 0)) {
  159. echo 'style="display:none;"';
  160. }
  161. echo '>';
  162. $subdirs = $dir->getSubdirs();
  163. foreach ($subdirs as $subdir) {
  164. for ($i = 0 ; $i < $level ; ++$i) {
  165. echo '-- ';
  166. }
  167. echo "\n";
  168. echo '<a href="#" onClick="show(\''.generateId($subdir).'\');return(false)" id="plus">'."\n";
  169. echo ' <img src="img/folder.png" alt="folder" /> ';
  170. echo basename($subdir->getPath());
  171. echo '</a> '."\n";
  172. echo '<a href="?play='.urlencode($subdir->getPath()).'/'.'">'."\n";
  173. echo ' <img src="img/play.png" alt="play" />';
  174. echo '</a>';
  175. echo '<br />'."\n";
  176. displayDir($subdir, $displayedDir, $level+1);
  177. }
  178. $songs = $dir->getSongs();
  179. foreach ($songs as $song) {
  180. for ($i = 0 ; $i < $level ; ++$i) {
  181. echo '-- ';
  182. }
  183. echo "\n";
  184. echo '<img src="img/music.png" alt="folder" /> '."\n";
  185. echo ' '.$song->getTitle();
  186. echo ' <a href="?play='.urlencode($song->getPath()).'">'."\n";
  187. echo ' <img src="img/play.png" alt="play" />'."\n";
  188. echo '</a>'."\n";
  189. echo ' <a href="'.$song->getPath().'" target="_blank">'."\n";
  190. echo ' <img src="img/download.png" alt="download" />'."\n";
  191. echo '</a>'.'<br />'."\n";
  192. }
  193. echo '</div>';
  194. }
  195. function printAlert($message) {
  196. ?>
  197. <script type="text/javascript" language="Javascript">
  198. <?php
  199. echo "alert(\"$message\");";
  200. ?>
  201. </script>
  202. <?php
  203. }
  204. /*****************************\
  205. INIT
  206. \*****************************/
  207. session_start();
  208. if (TEST_MODE_ACTIVATED) {
  209. if (isset($_GET[CLEAR_SESSION])) {
  210. $_SESSION = array();
  211. } else {
  212. // no clear requested
  213. }
  214. } else {
  215. // official server, don't execute testing commands
  216. }
  217. if (isset($_GET[PLAY])) {
  218. $play = $_GET[PLAY]; // don't use urldecode, $_GET items are already decoded
  219. if (strncmp($play, ROOT_SONG_DIR, strlen(ROOT_SONG_DIR)) == 0 && file_exists($play)) {
  220. $_SESSION[PLAY] = $play;
  221. } else {
  222. throw new Exception("Requête invalide : ".$play);
  223. }
  224. } else {
  225. // nothing requested to play
  226. }
  227. /*****************************\
  228. INTERFACE
  229. \*****************************/
  230. ?>
  231. <!DOCTYPE html>
  232. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
  233. <head>
  234. <title><?php
  235. echo PAGE_TITLE;
  236. ?></title>
  237. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  238. <link rel="stylesheet" href="style.css" type="text/css" media="screen" title="Normal" />
  239. <link rel="shortcut icon" href="fav.ico" />
  240. <script type="text/javascript" language="Javascript">
  241. function show(nom_champ) {
  242. if(document.getElementById) {
  243. tabler = document.getElementById(nom_champ);
  244. if(tabler.style.display=="none") {
  245. tabler.style.display="";
  246. } else {
  247. tabler.style.display="none";
  248. }
  249. }
  250. }
  251. </script>
  252. </head>
  253. <body>
  254. <?php
  255. /*****************************\
  256. TESTING
  257. \*****************************/
  258. if (TEST_MODE_ACTIVATED) {
  259. ?>
  260. <div id='test'>
  261. Test:
  262. <a href="?<?php echo CLEAR_SESSION; ?>">Clear</a>
  263. </div>
  264. <?php
  265. } else {
  266. // official server, don't display testing commands
  267. }
  268. ?>
  269. <!--
  270. *******************************
  271. PLAYER
  272. *******************************
  273. -->
  274. <?php
  275. if (isset($_SESSION[PLAY])) {
  276. $play = $_SESSION[PLAY];
  277. if (is_file($play)) {
  278. $songs = array(new Song($play));
  279. } else if (is_dir($play)) {
  280. $dir = new SongDir($play);
  281. $songs = $dir->getAllSongs();
  282. } else if (!file_exists($play)) {
  283. printAlert("Impossible de trouver ".$play);
  284. $songs = array();
  285. } else {
  286. throw new Exception("Impossible de lire $play");
  287. }
  288. if (empty($songs)) {
  289. // no songs to play
  290. } else {
  291. //sort($songs);
  292. $song = $songs[0];
  293. //reset($songs);
  294. ?>
  295. <audio controls id="player" autoplay>
  296. <source src="<?php echo $song->getPath() ?>" type="audio/mpeg">
  297. Your browser does not support the audio element.
  298. </audio>
  299. <a href="."><img src="img/stop.gif" alt="stop player" /></a><br/>
  300. <span id="title"><?php echo $song->getTitle() ?></span><br/>
  301. <br/>
  302. <script>
  303. var current = 0;
  304. var paths = [
  305. <?php
  306. foreach ($songs as $song) {
  307. echo json_encode($song->getPath()).",\n";
  308. }
  309. ?>
  310. ];
  311. var titles = [
  312. <?php
  313. foreach ($songs as $song) {
  314. echo json_encode($song->getTitle()).",\n";
  315. }
  316. ?>
  317. ];
  318. var player = document.getElementById('player');
  319. var currentTitle = document.getElementById('title');
  320. player.addEventListener('ended', function() {
  321. current++;
  322. if (current >= paths.length) {
  323. current = 0;
  324. }
  325. currentTitle.textContent = titles[current];
  326. player.src = paths[current];
  327. player.pause();
  328. player.load();
  329. player.play();
  330. }
  331. );
  332. </script>
  333. <?php
  334. }
  335. } else {
  336. // nothing to play, don't display the player
  337. }
  338. ?>
  339. <!--
  340. *******************************
  341. SONGS
  342. *******************************
  343. -->
  344. <?php
  345. $rootDir = new SongDir(ROOT_SONG_DIR);
  346. ?>
  347. <a href="#" onClick="show('<?php echo generateId($rootDir); ?>');return(false)" id="plus">All</a>
  348. <a href="?play=<?php echo urlencode(ROOT_SONG_DIR); ?>"><img src="img/play.png" alt="play" /></a>
  349. <?php
  350. $displayedDir = null;
  351. if (!isset($_SESSION[PLAY])) {
  352. $displayedDir = null;
  353. } else if (is_file($_SESSION[PLAY])) {
  354. $song = new Song($_SESSION[PLAY]);
  355. $displayedDir = new Songdir(dirname($song->getPath()));
  356. } else {
  357. $displayedDir = new SongDir($_SESSION[PLAY]);
  358. }
  359. displayDir($rootDir, $displayedDir);
  360. ?>
  361. </body>
  362. </html>