extension.cache.mysql.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.mysql.php - part of getID3() //
  9. // Please see readme.txt for more information //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. // //
  13. // This extension written by Allan Hansen <ahØartemis*dk> //
  14. // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
  15. // ///
  16. /////////////////////////////////////////////////////////////////
  17. /**
  18. * This is a caching extension for getID3(). It works the exact same
  19. * way as the getID3 class, but return cached information very fast
  20. *
  21. * Example: (see also demo.cache.mysql.php in /demo/)
  22. *
  23. * Normal getID3 usage (example):
  24. *
  25. * require_once 'getid3/getid3.php';
  26. * $getID3 = new getID3;
  27. * $getID3->encoding = 'UTF-8';
  28. * $info1 = $getID3->analyze('file1.flac');
  29. * $info2 = $getID3->analyze('file2.wv');
  30. *
  31. * getID3_cached usage:
  32. *
  33. * require_once 'getid3/getid3.php';
  34. * require_once 'getid3/getid3/extension.cache.mysql.php';
  35. * // 5th parameter (tablename) is optional, default is 'getid3_cache'
  36. * $getID3 = new getID3_cached_mysql('localhost', 'database', 'username', 'password', 'tablename');
  37. * $getID3->encoding = 'UTF-8';
  38. * $info1 = $getID3->analyze('file1.flac');
  39. * $info2 = $getID3->analyze('file2.wv');
  40. *
  41. *
  42. * Supported Cache Types (this extension)
  43. *
  44. * SQL Databases:
  45. *
  46. * cache_type cache_options
  47. * -------------------------------------------------------------------
  48. * mysql host, database, username, password
  49. *
  50. *
  51. * DBM-Style Databases: (use extension.cache.dbm)
  52. *
  53. * cache_type cache_options
  54. * -------------------------------------------------------------------
  55. * gdbm dbm_filename, lock_filename
  56. * ndbm dbm_filename, lock_filename
  57. * db2 dbm_filename, lock_filename
  58. * db3 dbm_filename, lock_filename
  59. * db4 dbm_filename, lock_filename (PHP5 required)
  60. *
  61. * PHP must have write access to both dbm_filename and lock_filename.
  62. *
  63. *
  64. * Recommended Cache Types
  65. *
  66. * Infrequent updates, many reads any DBM
  67. * Frequent updates mysql
  68. */
  69. class getID3_cached_mysql extends getID3
  70. {
  71. /**
  72. * @var resource
  73. */
  74. private $cursor;
  75. /**
  76. * @var resource
  77. */
  78. private $connection;
  79. /**
  80. * @var string
  81. */
  82. private $table;
  83. /**
  84. * constructor - see top of this file for cache type and cache_options
  85. *
  86. * @param string $host
  87. * @param string $database
  88. * @param string $username
  89. * @param string $password
  90. * @param string $table
  91. *
  92. * @throws Exception
  93. * @throws getid3_exception
  94. */
  95. public function __construct($host, $database, $username, $password, $table='getid3_cache') {
  96. // Check for mysql support
  97. if (!function_exists('mysql_pconnect')) {
  98. throw new Exception('PHP not compiled with mysql support.');
  99. }
  100. // Connect to database
  101. $this->connection = mysql_pconnect($host, $username, $password);
  102. if (!$this->connection) {
  103. throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
  104. }
  105. // Select database
  106. if (!mysql_select_db($database, $this->connection)) {
  107. throw new Exception('Cannot use database '.$database);
  108. }
  109. // Set table
  110. $this->table = $table;
  111. // Create cache table if not exists
  112. $this->create_table();
  113. // Check version number and clear cache if changed
  114. $version = '';
  115. $SQLquery = 'SELECT `value`';
  116. $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
  117. $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string(getID3::VERSION).'\')';
  118. $SQLquery .= ' AND (`filesize` = -1)';
  119. $SQLquery .= ' AND (`filetime` = -1)';
  120. $SQLquery .= ' AND (`analyzetime` = -1)';
  121. if ($this->cursor = mysql_query($SQLquery, $this->connection)) {
  122. list($version) = mysql_fetch_array($this->cursor);
  123. }
  124. if ($version != getID3::VERSION) {
  125. $this->clear_cache();
  126. }
  127. parent::__construct();
  128. }
  129. /**
  130. * clear cache
  131. */
  132. public function clear_cache() {
  133. $this->cursor = mysql_query('DELETE FROM `'.mysql_real_escape_string($this->table).'`', $this->connection);
  134. $this->cursor = mysql_query('INSERT INTO `'.mysql_real_escape_string($this->table).'` VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')', $this->connection);
  135. }
  136. /**
  137. * analyze file
  138. *
  139. * @param string $filename
  140. * @param int $filesize
  141. * @param string $original_filename
  142. * @param resource $fp
  143. *
  144. * @return mixed
  145. */
  146. public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
  147. $filetime = 0;
  148. if (file_exists($filename)) {
  149. // Short-hands
  150. $filetime = filemtime($filename);
  151. $filesize = filesize($filename);
  152. // Lookup file
  153. $SQLquery = 'SELECT `value`';
  154. $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
  155. $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string($filename).'\')';
  156. $SQLquery .= ' AND (`filesize` = \''.mysql_real_escape_string($filesize).'\')';
  157. $SQLquery .= ' AND (`filetime` = \''.mysql_real_escape_string($filetime).'\')';
  158. $this->cursor = mysql_query($SQLquery, $this->connection);
  159. if (mysql_num_rows($this->cursor) > 0) {
  160. // Hit
  161. list($result) = mysql_fetch_array($this->cursor);
  162. return unserialize(base64_decode($result));
  163. }
  164. }
  165. // Miss
  166. $analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
  167. // Save result
  168. if (file_exists($filename)) {
  169. $SQLquery = 'INSERT INTO `'.mysql_real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
  170. $SQLquery .= '\''.mysql_real_escape_string($filename).'\'';
  171. $SQLquery .= ', \''.mysql_real_escape_string($filesize).'\'';
  172. $SQLquery .= ', \''.mysql_real_escape_string($filetime).'\'';
  173. $SQLquery .= ', \''.mysql_real_escape_string(time() ).'\'';
  174. $SQLquery .= ', \''.mysql_real_escape_string(base64_encode(serialize($analysis))).'\')';
  175. $this->cursor = mysql_query($SQLquery, $this->connection);
  176. }
  177. return $analysis;
  178. }
  179. /**
  180. * (re)create sql table
  181. *
  182. * @param bool $drop
  183. */
  184. private function create_table($drop=false) {
  185. $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.mysql_real_escape_string($this->table).'` (';
  186. $SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
  187. $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
  188. $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
  189. $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
  190. $SQLquery .= ', `value` LONGTEXT NOT NULL';
  191. $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`))';
  192. $this->cursor = mysql_query($SQLquery, $this->connection);
  193. echo mysql_error($this->connection);
  194. }
  195. }