demo.dirscan.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // //
  8. // /demo/demo.dirscan.php - part of getID3() //
  9. // Directory Scanning and Caching CLI tool for batch media //
  10. // file processing with getID3() //
  11. // by Karl G. Holz <newaeonØmac*com> //
  12. // ///
  13. /////////////////////////////////////////////////////////////////
  14. die('For security reasons, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in demos/'.basename(__FILE__));
  15. /**
  16. * This is a directory scanning and caching cli tool for getID3().
  17. *
  18. * use like so for the default sqlite3 database, which is hidden:
  19. *
  20. * cd <path you want to start scanning from>
  21. * php <path to getid3 files>/demo.dirscan.php
  22. *
  23. * or
  24. *
  25. * php <path to getid3 files>/demo.dirscan.php <dir to scan> <file ext in csv list>
  26. *
  27. * Supported Cache Types (this extension)
  28. *
  29. * SQL Databases:
  30. *
  31. * cache_type
  32. * -------------------------------------------------------------------
  33. * mysql
  34. $cache='mysql';
  35. $database['host']='';
  36. $database['database']='';
  37. $database['username']='';
  38. $database['password']='';
  39. $database['table']='';
  40. * sqlite3
  41. $cache='sqlite3';
  42. $database['table']='getid3_cache';
  43. $database['hide']=true;
  44. */
  45. $dir = $_SERVER['PWD'];
  46. $media = array('mp4', 'm4v', 'mov', 'mp3', 'm4a', 'jpg', 'png', 'gif');
  47. $database = array();
  48. /**
  49. * configure the database bellow
  50. */
  51. // sqlite3
  52. $cache = 'sqlite3';
  53. $database['table'] = 'getid3_cache';
  54. $database['hide'] = true;
  55. /**
  56. * mysql
  57. $cache = 'mysql';
  58. $database['host'] = '';
  59. $database['database'] = '';
  60. $database['username'] = '';
  61. $database['password'] = '';
  62. $database['table'] = '';
  63. */
  64. /**
  65. * id3 tags class file
  66. */
  67. require_once(dirname(__FILE__).'/getid3.php');
  68. /**
  69. * dirscan scans all directories for files that match your selected filetypes into the cache database
  70. * this is useful for a lot of media files
  71. *
  72. *
  73. * @package dirscan
  74. * @author Karl Holz
  75. *
  76. */
  77. class dirscan {
  78. /**
  79. * type_brace() * Might not work on Solaris and other non GNU systems *
  80. *
  81. * Configures a filetype list for use with glob searches,
  82. * will match uppercase or lowercase extensions only, no mixing
  83. * @param string $dir directory to use
  84. * @param mixed $search cvs list of extentions or an array
  85. * @return string or null if checks fail
  86. */
  87. private function type_brace($dir, $search=array()) {
  88. $dir = str_replace(array('///', '//'), array('/', '/'), $dir);
  89. if (!is_dir($dir)) {
  90. return null;
  91. }
  92. if (!is_array($search)) {
  93. $e = explode(',', $search);
  94. } elseif (count($search) < 1) {
  95. return null;
  96. } else {
  97. $e = $search;
  98. }
  99. $ext = array();
  100. foreach ($e as $new) {
  101. $ext[] = strtolower(trim($new));
  102. $ext[] = strtoupper(trim($new));
  103. }
  104. $b = $dir.'/*.{'.implode(',', $ext).'}';
  105. return $b;
  106. }
  107. /**
  108. * this function will search 4 levels deep for directories
  109. * will return null on failure
  110. * @param string $root
  111. * @return array return an array of dirs under root
  112. * @todo figure out how to block tabo directories with ease
  113. */
  114. private function getDirs($root) {
  115. switch ($root) { // return null on tabo directories, add as needed -> case {dir to block }: this is not perfect yet
  116. case '/':
  117. case '/var':
  118. case '/etc':
  119. case '/home':
  120. case '/usr':
  121. case '/root':
  122. case '/private/etc':
  123. case '/private/var':
  124. case '/etc/apache2':
  125. case '/home':
  126. case '/tmp':
  127. case '/var/log':
  128. return null;
  129. break;
  130. default: // scan 4 directories deep
  131. if (!is_dir($root)) {
  132. return null;
  133. }
  134. $dirs = array_merge(glob($root.'/*', GLOB_ONLYDIR), glob($root.'/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*/*/*', GLOB_ONLYDIR));
  135. break;
  136. }
  137. if (count($dirs) < 1) {
  138. $dirs = array($root);
  139. }
  140. return $dirs;
  141. }
  142. /**
  143. * file_check() check the number of file that are found that match the brace search
  144. *
  145. * @param string $search
  146. * @return mixed
  147. */
  148. private function file_check($search) {
  149. $t = array();
  150. $s = glob($search, GLOB_BRACE);
  151. foreach ($s as $file) {
  152. $t[] = str_replace(array('///', '//'), array('/', '/'), $file);
  153. }
  154. if (count($t) > 0) {
  155. return $t;
  156. }
  157. return null;
  158. }
  159. function getTime() {
  160. return microtime(true);
  161. // old method for PHP < 5
  162. //$a = explode(' ', microtime());
  163. //return (double) $a[0] + $a[1];
  164. }
  165. /**
  166. *
  167. * @param string $dir
  168. * @param mixed $match search type name extentions, can be an array or csv list
  169. * @param string $cache caching extention, select one of sqlite3, mysql, dbm
  170. * @param array $opt database options,
  171. */
  172. function scan_files($dir, $match, $cache='sqlite3', $opt=array('table'=>'getid3_cache', 'hide'=>true)) {
  173. $Start = self::getTime();
  174. switch ($cache) { // load the caching module
  175. case 'sqlite3':
  176. if (!class_exists('getID3_cached_sqlite3')) {
  177. require_once(dirname(__FILE__)).'/extension.cache.sqlite3.php';
  178. }
  179. $id3 = new getID3_cached_sqlite3($opt['table'], $opt['hide']);
  180. break;
  181. case 'mysql':
  182. if (!class_exists('getID3_cached_mysql')) {
  183. require_once(dirname(__FILE__)).'/extension.cache.mysql.php';
  184. }
  185. $id3 = new getID3_cached_mysql($opt['host'], $opt['database'], $opt['username'], $opt['password'], $opt['table']);
  186. break;
  187. // I'll leave this for some one else
  188. //case 'dbm':
  189. // if (!class_exists('getID3_cached_dbm')) {
  190. // require_once(dirname(__FILE__)).'/extension.cache.dbm.php';
  191. // }
  192. // die(' This has not be implemented, sorry for the inconvenience');
  193. // break;
  194. default:
  195. die(' You have selected an Invalid cache type, only "sqlite3" and "mysql" are valid'."\n");
  196. break;
  197. }
  198. $count = array('dir'=>0, 'file'=>0);
  199. $dirs = self::getDirs($dir);
  200. if ($dirs !== null) {
  201. foreach ($dirs as $d) {
  202. echo ' Scanning: '.$d."\n";
  203. $search = self::type_brace($d, $match);
  204. if ($search !== null) {
  205. $files = self::file_check($search);
  206. if ($files !== null) {
  207. foreach ($files as $f) {
  208. echo ' * Analyzing '.$f.' '."\n";
  209. $id3->analyze($f);
  210. $count['file']++;
  211. }
  212. $count['dir']++;
  213. } else {
  214. echo 'Failed to get files '."\n";
  215. }
  216. } else {
  217. echo 'Failed to create match string '."\n";
  218. }
  219. }
  220. echo '**************************************'."\n";
  221. echo '* Finished Scanning your directories '."\n*\n";
  222. echo '* Directories '.$count['dir']."\n";
  223. echo '* Files '.$count['file']."\n";
  224. $End = self::getTime();
  225. $t = number_format(($End - $Start) / 60, 2);
  226. echo '* Time taken to scan '.$dir.' '.$t.' min '."\n";
  227. echo '**************************************'."\n";
  228. } else {
  229. echo ' failed to get directories '."\n";
  230. }
  231. }
  232. }
  233. if (PHP_SAPI === 'cli') {
  234. if (count($argv) == 2) {
  235. if (is_dir($argv[1])) {
  236. $dir = $argv[1];
  237. }
  238. if (count(explode(',', $argv[2])) > 0) {
  239. $media = $arg[2];
  240. }
  241. }
  242. echo ' * Starting to scan directory: '.$dir."\n";
  243. echo ' * Using default media types: '.implode(',', $media)."\n";
  244. dirscan::scan_files($dir, $media, $cache, $database);
  245. }