PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/php-getid3-2.0.0b5/demos/demo.browse.dhtml.php

#
PHP | 441 lines | 292 code | 95 blank | 54 comment | 39 complexity | e238ccd45466c9c88420b06676f28520 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2 of the GPL license, |
  8. // | that is bundled with this package in the file license.txt and is |
  9. // | available through the world-wide-web at the following url: |
  10. // | http://www.gnu.org/copyleft/gpl.html |
  11. // +----------------------------------------------------------------------+
  12. // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: James Heinrich <info拽etid3*org> |
  15. // | Allan Hansen <ah住rtemis*dk> |
  16. // +----------------------------------------------------------------------+
  17. // | demo.browse.dhtml.php |
  18. // | getID3() demo file - browse directory and display information using |
  19. // | DHTML techniques compatible with MSIE5 and Mozilla. |
  20. // | dependencies: getid3, extras/abstration.php, getid3.css |
  21. // +----------------------------------------------------------------------+
  22. //
  23. // $Id: demo.browse.dhtml.php,v 1.4 2006/12/03 19:28:17 ah Exp $
  24. // Set this directory to the root of your audio files - do not set to "/" !
  25. $root_path = '/data/getid3/';
  26. // Set to true for 1280+ width
  27. $wide_screen = true;
  28. // Rewrite and check root_path
  29. $root_path = realpath($root_path);
  30. if (!$root_path || $root_path == '/') {
  31. die('$root_path set to non-existing path or / (latter not allowed)');
  32. }
  33. // Define based on screen width
  34. define('GETID3_COMPRESS_LENGTH', $wide_screen ? 28 : 16);
  35. define('GETID3_FILENAME_LENGTH', $wide_screen ? 28 : 20);
  36. // Misc settings
  37. set_time_limit(20*3600);
  38. error_reporting (E_STRICT | E_ALL);
  39. ignore_user_abort(false);
  40. // Include dependencies
  41. require_once('../getid3/getid3.php');
  42. require_once('../extras/abstraction.php');
  43. // Initialize getID3 engine
  44. $getid3 = new getID3;
  45. $getid3->encoding = 'UTF-8';
  46. // Get time in microseconds
  47. function getmicrotime() {
  48. list($usec, $sec) = explode(' ', microtime());
  49. return ((float) $usec + (float) $sec);
  50. }
  51. // HTML header
  52. function CommonHeader($heading) {
  53. header('Content-Type: text/html; charset=UTF-8');
  54. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  55. echo '<html>';
  56. echo '<head>';
  57. echo '<title>getID3() - /demos/demo.browse.php (sample script)</title>';
  58. echo "<meta http-equiv='Content-Style-Type' content='text/css'>";
  59. echo "<link href='getid3.css' type='text/css' rel='stylesheet'>";
  60. echo '</head>';
  61. echo '<body>';
  62. echo "<h1>getID3() - $heading</h1>";
  63. }
  64. // HTML footer
  65. function CommonFooter() {
  66. foreach (array ('iconv', 'zlib', 'exif', 'mysql', 'dba') as $ext) {
  67. $support[] = (extension_loaded($ext) ? '+' : '-') . $ext;
  68. }
  69. echo xml_gen::br();
  70. echo xml_gen::p('getID3() ' . getid3::VERSION . '.<br>PHP ' . phpversion() . ' (' . implode(xml_gen::space(2), $support) . ').');
  71. echo xml_gen::p(xml_gen::a('http://getid3.sourceforge.net/', 'http://getid3.sourceforge.net'));
  72. echo '</body></html>';
  73. }
  74. // Compressed output - js alert to show more
  75. function compress($string) {
  76. static $i;
  77. $i++;
  78. $string2 = str_replace('<br>', ', ', $string);
  79. if (strlen($string2) <= GETID3_COMPRESS_LENGTH) {
  80. return $string2;
  81. }
  82. $string3 = str_replace("<br>", "\\n", addslashes(str_replace('"', "''", $string)));
  83. return xml_gen::a("javascript:alert('$string3')", substr($string2, 0, GETID3_COMPRESS_LENGTH-2) . '...');
  84. }
  85. //// Show file info or embedded cover
  86. if (@$_GET['filename']) {
  87. $_GET['filename'] = realpath($_GET['filename']);
  88. if (!strstr('*'.$_GET['filename'], '*'.$root_path)) {
  89. die('ACCESS DENIED to '. $_GET["filename"]);
  90. }
  91. function dump(&$var) {
  92. if (!is_array($var)) {
  93. if (is_int($var)) {
  94. return number_format($var);
  95. }
  96. if (is_bool($var)) {
  97. return $var ? 'true' : 'false';
  98. }
  99. return $var;
  100. }
  101. else {
  102. $t = new Table(3, "class=dump");
  103. foreach ($var as $key => $value) {
  104. $t->data($key);
  105. // Show cover
  106. if ($key == 'data' && isset($var['image_mime']) && isset($var['dataoffset'])) {
  107. $t->data('embedded image');
  108. $t->data("<img src='demo.browse.php?filename=".urlencode($_GET['filename'])."&show_img=".md5($value)."'>");
  109. break;
  110. }
  111. $type = gettype($value);
  112. $t->data($type);
  113. if ($type == 'string') {
  114. echo '('.strlen($value).')';
  115. }
  116. elseif ($type == 'array') {
  117. echo '('.sizeof($value).')';
  118. }
  119. $t->data(null, 'class=dump_'.$type);
  120. echo dump($value);
  121. }
  122. $t->done();
  123. }
  124. }
  125. function dump_img(&$var, $md5) {
  126. if (is_array($var)) {
  127. foreach ($var as $key => $value) {
  128. if ($key == 'data' && isset($var['image_mime']) && isset($var['dataoffset'])) {
  129. if (md5($value) == $md5) {
  130. header("Content-type: " . $var['image_mime']);
  131. echo $value;
  132. break;
  133. }
  134. }
  135. dump_img($value, $md5);
  136. }
  137. }
  138. }
  139. $getid3->option_tags_images = true;
  140. // Show embedded cover
  141. if (@$_GET["show_img"]) {
  142. try {
  143. $getid3->Analyze($_GET['filename']);
  144. dump_img($getid3->info, $_GET["show_img"]);
  145. die();
  146. }
  147. catch (Exception $e) {
  148. echo xml_gen::p('ERROR: ' . $e->message);
  149. }
  150. }
  151. // Show file info
  152. CommonHeader($_GET['filename']);
  153. $pd = pathinfo($_GET['filename']);
  154. $pd = $pd['dirname'];
  155. echo xml_gen::p('Browse: ' . xml_gen::a($_SERVER['SCRIPT_NAME'].'?directory='.urlencode($pd), $pd));
  156. try {
  157. $getid3->Analyze($_GET['filename']);
  158. dump($getid3->info);
  159. }
  160. catch (Exception $e) {
  161. echo xml_gen::p('ERROR: ' . $e->message);
  162. }
  163. CommonFooter();
  164. die();
  165. }
  166. //// Browse directory
  167. // Fast scan of directories
  168. $getid3->option_accurate_results = false;
  169. // Create dir variable
  170. $dir = @$_GET["directory"] ? realpath($_GET["directory"]) : $root_path;
  171. // Check that path is wihtin root
  172. if (!strstr('*'.$dir, '*'.$root_path)) {
  173. die('ACCESS DENIED to '. $dir);
  174. }
  175. // Begin page
  176. CommonHeader("$dir");
  177. // Read files and directories
  178. if (is_dir($dir)) {
  179. if ($dh = opendir($dir)) {
  180. while (($file = readdir($dh)) !== false) {
  181. $full_name = "$dir/$file";
  182. if (is_file($full_name)) {
  183. $files[$full_name] = $file;
  184. }
  185. elseif ($file[0] != '.') {
  186. $dirs[$full_name] = $file;
  187. }
  188. }
  189. closedir($dh);
  190. }
  191. }
  192. // Create table
  193. $g = new xml_gen;
  194. $t = new Table(10, "class='table'", ';odd_files;even_files', 'left;left;right;right;left;left;left;left;left;right');
  195. // Output parent dir
  196. if ($dir != $root_path) {
  197. $t->data(xml_gen::a($_SERVER['SCRIPT_NAME'].'?directory='.urlencode(realpath($dir.'/..')), '[parent directory]'), 'colspan=10');
  198. $t->end_row();
  199. }
  200. // Output directories
  201. if (@$dirs) {
  202. asort($dirs);
  203. foreach ($dirs as $full_name => $short_name) {
  204. $t->data(xml_gen::a($_SERVER['SCRIPT_NAME'].'?directory='.urlencode($full_name), $short_name), 'colspan=10');
  205. $t->end_row();
  206. }
  207. }
  208. // Output file rows
  209. if (@$files) {
  210. asort($files);
  211. $counter = $i = 0;
  212. foreach ($files as $full_name => $short_name) {
  213. // Table header
  214. if (!$counter--) {
  215. // empty row
  216. $t->data('&nbsp;');
  217. $t->end_row();
  218. $t->data('Filename', 'class=header');
  219. $t->data('Format', 'class=header');
  220. $t->data('Length', 'class=header');
  221. $t->data('Bitrate', 'class=header');
  222. $t->data('Audio', 'class=header');
  223. $t->data('Artist', 'class=header');
  224. $t->data('Title', 'class=header');
  225. $t->data('Tags', 'class=header');
  226. $t->data('Warnings', 'class=header');
  227. $t->data('Scan&nbsp;Time', 'class=header');
  228. //$t->data('Edit&nbsp;Tags', 'class=header');
  229. $counter = 19;
  230. }
  231. $link_name = $short_name;
  232. if (strlen($short_name) > GETID3_FILENAME_LENGTH) {
  233. if (preg_match('/^(.*)\.([a-zA-Z0-9]{1,5})$/', $short_name, $r)) {
  234. $link_name = substr($r[1], 0, GETID3_FILENAME_LENGTH-2-strlen($r[2])) . '...' . $r[2];
  235. }
  236. else {
  237. $link_name = substr($short_name, 0, GETID3_FILENAME_LENGTH-2);
  238. }
  239. }
  240. $i++;
  241. $t->row_attr("id='row$i'");
  242. $t->data(xml_gen::a($_SERVER['SCRIPT_NAME'].'?filename='.urlencode($full_name), $link_name, 'title="'.$short_name.'"'));
  243. $t->data('Scanning in progress... Please wait.', "colspan=8 id=wait$i");
  244. $t->data(null, "id=scan$i");
  245. //$t->data();
  246. }
  247. }
  248. // Finish table
  249. $t->done();
  250. // Finish page
  251. CommonFooter();
  252. // DHTML/PHP update scripts
  253. echo "
  254. <script>
  255. function upd_entry(row_num, format, playtime, bitrate, audio, artist, title, tags, warnings, ms) {
  256. row = document.getElementById('row' + row_num);
  257. row.deleteCell(1);
  258. cell = row.insertCell(1); cell.innerHTML = format;
  259. cell = row.insertCell(2); cell.innerHTML = playtime;
  260. cell = row.insertCell(3); cell.innerHTML = bitrate;
  261. cell = row.insertCell(4); cell.innerHTML = audio;
  262. cell = row.insertCell(5); cell.innerHTML = artist;
  263. cell = row.insertCell(6); cell.innerHTML = title;
  264. cell = row.insertCell(7); cell.innerHTML = tags;
  265. cell = row.insertCell(8); cell.innerHTML = warnings;
  266. document.getElementById('scan' + row_num).innerHTML = ms;
  267. }
  268. function upd_error(row_num, msg, ms) {
  269. document.getElementById('wait' + row_num).innerHTML = '<span class=error>ERROR: ' + msg + '</span>';
  270. document.getElementById('scan' + row_num).innerHTML = ms;
  271. }
  272. </script>
  273. ";
  274. function upd_entry($row_num, $format, $playtime, $bitrate, $audio, $artist, $title, $tags, $warnings, $ms) {
  275. $time = number_format($ms*1000) . " ms";
  276. $format = addslashes($format); // str_replace("\"", "\\\"", $format);
  277. $artist = addslashes($artist); // str_replace("\"", "\\\"", $artist);
  278. $title = addslashes($title); // str_replace("\"", "\\\"", $title);
  279. $warnings = addslashes($warnings); // str_replace("\"", "\\\"", $warnings);
  280. echo "\n<script>upd_entry($row_num, \"$format\", '$playtime', '$bitrate', '$audio', \"$artist\", \"$title\", '$tags', \"$warnings\", '$time');</script>";
  281. flush();
  282. }
  283. function upd_error($row_num, $msg, $ms) {
  284. $msg = str_replace("\"", "\\\"", $msg);
  285. $time = number_format($ms*1000) . " ms";
  286. echo "\n<script>upd_error($row_num, \"$msg\", '$time');</script>";
  287. flush();
  288. }
  289. // Analyze files
  290. if (@$files) {
  291. $i = 0;
  292. foreach ($files as $full_name => $short_name) {
  293. $i++;
  294. try {
  295. $time = getmicrotime();
  296. $getid3->Analyze($full_name);
  297. $time = getmicrotime() - $time;
  298. $format = @$getid3->info['fileformat'];
  299. if (@$getid3->info['audio']['dataformat'] && $getid3->info['audio']['dataformat'] != $getid3->info['fileformat']) {
  300. $format .= '/' . @$getid3->info['audio']['dataformat'];
  301. }
  302. if (@$getid3->info['video']['dataformat'] && $getid3->info['video']['dataformat'] != $getid3->info['fileformat'] && $getid3->info['video']['dataformat'] != @$getid3->info['audio']['dataformat']) {
  303. $format .= '/' . @$getid3->info['video']['dataformat'];
  304. }
  305. $playtime = @$getid3->info['playtime_string'];
  306. $bitrate = (@$getid3->info['bitrate'] ? number_format($getid3->info['bitrate']/1000) . 'k' : '');
  307. $audio = (@$getid3->info['audio']['sample_rate'] ? number_format($getid3->info['audio']['sample_rate']) . '/' . (@$getid3->info['audio']['bits_per_sample'] ? $getid3->info['audio']['bits_per_sample'] . '/' : '') . @$getid3->info['audio']['channels'] : '');
  308. $artist = $title = '';
  309. if (@$getid3->info['tags']) {
  310. foreach ($getid3->info['tags'] as $tag => $tag_info) {
  311. if (@$getid3->info['tags'][$tag]['artist'] || @$getid3->info['tags'][$tag]['title']) {
  312. $artist = @implode('<br>', @$getid3->info['tags'][$tag]['artist']);
  313. $title = @implode('<br>', @$getid3->info['tags'][$tag]['title']);
  314. break;
  315. }
  316. }
  317. }
  318. $artist = compress($artist);
  319. $title = compress($title);
  320. $tags = @implode(",&nbsp;", @array_keys(@$getid3->info['tags']));
  321. $warnings = compress(@implode("<br>", @$getid3->info['warning']));
  322. upd_entry($i, $format, $playtime, $bitrate, $audio, $artist, $title, $tags, $warnings, $time);
  323. }
  324. catch (Exception $e) {
  325. $time = getmicrotime() - $time;
  326. upd_error($i, $e->message, $time);
  327. }
  328. }
  329. }
  330. ?>