extension.cache.sqlite3.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. // extension.cache.mysqli.php - part of getID3() //
  9. // Please see readme.txt for more information //
  10. // //
  11. /////////////////////////////////////////////////////////////////
  12. // //
  13. // extension.cache.sqlite3.php - part of getID3() //
  14. // Please see readme.txt for more information //
  15. // //
  16. /////////////////////////////////////////////////////////////////
  17. /// //
  18. // MySQL extension written by Allan Hansen <ahØartemis*dk> //
  19. // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
  20. // MySQL extension was reworked for SQLite3 by //
  21. // Karl G. Holz <newaeonØmac*com> //
  22. // ///
  23. /////////////////////////////////////////////////////////////////
  24. /**
  25. * This is a caching extension for getID3(). It works the exact same
  26. * way as the getID3 class, but return cached information much faster
  27. *
  28. * Normal getID3 usage (example):
  29. *
  30. * require_once 'getid3/getid3.php';
  31. * $getID3 = new getID3;
  32. * $getID3->encoding = 'UTF-8';
  33. * $info1 = $getID3->analyze('file1.flac');
  34. * $info2 = $getID3->analyze('file2.wv');
  35. *
  36. * getID3_cached usage:
  37. *
  38. * require_once 'getid3/getid3.php';
  39. * require_once 'getid3/extension.cache.sqlite3.php';
  40. * // all parameters are optional, defaults are:
  41. * $getID3 = new getID3_cached_sqlite3($table='getid3_cache', $hide=FALSE);
  42. * $getID3->encoding = 'UTF-8';
  43. * $info1 = $getID3->analyze('file1.flac');
  44. * $info2 = $getID3->analyze('file2.wv');
  45. *
  46. *
  47. * Supported Cache Types (this extension)
  48. *
  49. * SQL Databases:
  50. *
  51. * cache_type cache_options
  52. * -------------------------------------------------------------------
  53. * mysql host, database, username, password
  54. *
  55. * sqlite3 table='getid3_cache', hide=false (PHP5)
  56. *
  57. *
  58. * *** database file will be stored in the same directory as this script,
  59. * *** webserver must have write access to that directory!
  60. * *** set $hide to TRUE to prefix db file with .ht to pervent access from web client
  61. * *** this is a default setting in the Apache configuration:
  62. *
  63. * The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.
  64. *
  65. * <Files ~ "^\.ht">
  66. * Order allow,deny
  67. * Deny from all
  68. * Satisfy all
  69. * </Files>
  70. *
  71. ********************************************************************************
  72. *
  73. * -------------------------------------------------------------------
  74. * DBM-Style Databases: (use extension.cache.dbm)
  75. *
  76. * cache_type cache_options
  77. * -------------------------------------------------------------------
  78. * gdbm dbm_filename, lock_filename
  79. * ndbm dbm_filename, lock_filename
  80. * db2 dbm_filename, lock_filename
  81. * db3 dbm_filename, lock_filename
  82. * db4 dbm_filename, lock_filename (PHP5 required)
  83. *
  84. * PHP must have write access to both dbm_filename and lock_filename.
  85. *
  86. * Recommended Cache Types
  87. *
  88. * Infrequent updates, many reads any DBM
  89. * Frequent updates mysql
  90. ********************************************************************************
  91. *
  92. * IMHO this is still a bit slow, I'm using this with MP4/MOV/ M4v files
  93. * there is a plan to add directory scanning and analyzing to make things work much faster
  94. *
  95. *
  96. */
  97. class getID3_cached_sqlite3 extends getID3
  98. {
  99. /**
  100. * hold the sqlite db
  101. *
  102. * @var SQLite3 Resource
  103. */
  104. private $db;
  105. /**
  106. * table to use for caching
  107. *
  108. * @var string $table
  109. */
  110. private $table;
  111. /**
  112. * @param string $table holds name of sqlite table
  113. * @param boolean $hide
  114. *
  115. * @throws getid3_exception
  116. * @throws Exception
  117. */
  118. public function __construct($table='getid3_cache', $hide=false) {
  119. // Check for SQLite3 support
  120. if (!function_exists('sqlite_open')) {
  121. throw new Exception('PHP not compiled with SQLite3 support.');
  122. }
  123. $this->table = $table; // Set table
  124. $file = dirname(__FILE__).'/'.basename(__FILE__, 'php').'sqlite';
  125. if ($hide) {
  126. $file = dirname(__FILE__).'/.ht.'.basename(__FILE__, 'php').'sqlite';
  127. }
  128. $this->db = new SQLite3($file);
  129. $db = $this->db;
  130. $this->create_table(); // Create cache table if not exists
  131. $version = '';
  132. $sql = $this->getQuery('version_check');
  133. $stmt = $db->prepare($sql);
  134. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  135. $result = $stmt->execute();
  136. list($version) = $result->fetchArray();
  137. if ($version != getID3::VERSION) { // Check version number and clear cache if changed
  138. $this->clear_cache();
  139. }
  140. parent::__construct();
  141. }
  142. /**
  143. * close the database connection
  144. */
  145. public function __destruct() {
  146. $db=$this->db;
  147. $db->close();
  148. }
  149. /**
  150. * clear the cache
  151. *
  152. * @return SQLite3Result
  153. */
  154. private function clear_cache() {
  155. $db = $this->db;
  156. $sql = $this->getQuery('delete_cache');
  157. $db->exec($sql);
  158. $sql = $this->getQuery('set_version');
  159. $stmt = $db->prepare($sql);
  160. $stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
  161. $stmt->bindValue(':dirname', getID3::VERSION, SQLITE3_TEXT);
  162. $stmt->bindValue(':val', getID3::VERSION, SQLITE3_TEXT);
  163. return $stmt->execute();
  164. }
  165. /**
  166. * analyze file and cache them, if cached pull from the db
  167. *
  168. * @param string $filename
  169. * @param integer $filesize
  170. * @param string $original_filename
  171. * @param resource $fp
  172. *
  173. * @return mixed|false
  174. */
  175. public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
  176. if (!file_exists($filename)) {
  177. return false;
  178. }
  179. // items to track for caching
  180. $filetime = filemtime($filename);
  181. $filesize_real = filesize($filename);
  182. // this will be saved for a quick directory lookup of analized files
  183. // ... why do 50 seperate sql quries when you can do 1 for the same result
  184. $dirname = dirname($filename);
  185. // Lookup file
  186. $db = $this->db;
  187. $sql = $this->getQuery('get_id3_data');
  188. $stmt = $db->prepare($sql);
  189. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  190. $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
  191. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  192. $res = $stmt->execute();
  193. list($result) = $res->fetchArray();
  194. if (count($result) > 0 ) {
  195. return unserialize(base64_decode($result));
  196. }
  197. // if it hasn't been analyzed before, then do it now
  198. $analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
  199. // Save result
  200. $sql = $this->getQuery('cache_file');
  201. $stmt = $db->prepare($sql);
  202. $stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
  203. $stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
  204. $stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
  205. $stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
  206. $stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
  207. $stmt->bindValue(':val', base64_encode(serialize($analysis)), SQLITE3_TEXT);
  208. $res = $stmt->execute();
  209. return $analysis;
  210. }
  211. /**
  212. * create data base table
  213. * this is almost the same as MySQL, with the exception of the dirname being added
  214. *
  215. * @return bool
  216. */
  217. private function create_table() {
  218. $db = $this->db;
  219. $sql = $this->getQuery('make_table');
  220. return $db->exec($sql);
  221. }
  222. /**
  223. * get cached directory
  224. *
  225. * This function is not in the MySQL extention, it's ment to speed up requesting multiple files
  226. * which is ideal for podcasting, playlists, etc.
  227. *
  228. * @param string $dir directory to search the cache database for
  229. *
  230. * @return array return an array of matching id3 data
  231. */
  232. public function get_cached_dir($dir) {
  233. $db = $this->db;
  234. $rows = array();
  235. $sql = $this->getQuery('get_cached_dir');
  236. $stmt = $db->prepare($sql);
  237. $stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
  238. $res = $stmt->execute();
  239. while ($row=$res->fetchArray()) {
  240. $rows[] = unserialize(base64_decode($row));
  241. }
  242. return $rows;
  243. }
  244. /**
  245. * returns NULL if query is not found
  246. *
  247. * @param string $name
  248. *
  249. * @return null|string
  250. */
  251. public function getQuery($name)
  252. {
  253. switch ($name) {
  254. case 'version_check':
  255. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
  256. case 'delete_cache':
  257. return "DELETE FROM $this->table";
  258. case 'set_version':
  259. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
  260. case 'get_id3_data':
  261. return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
  262. case 'cache_file':
  263. return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
  264. case 'make_table':
  265. return "CREATE TABLE IF NOT EXISTS $this->table (filename VARCHAR(255) DEFAULT '', dirname VARCHAR(255) DEFAULT '', filesize INT(11) DEFAULT '0', filetime INT(11) DEFAULT '0', analyzetime INT(11) DEFAULT '0', val text, PRIMARY KEY (filename, filesize, filetime))";
  266. case 'get_cached_dir':
  267. return "SELECT val FROM $this->table WHERE dirname = :dirname";
  268. default:
  269. return null;
  270. }
  271. }
  272. /**
  273. * use the magical __get() for sql queries
  274. *
  275. * access as easy as $this->{case name}, returns NULL if query is not found
  276. *
  277. * @param string $name
  278. *
  279. * @return string
  280. * @deprecated use getQuery() instead
  281. */
  282. public function __get($name) {
  283. return $this->getQuery($name);
  284. }
  285. }