PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/plugins/id3/getid3/extension.cache.mysql.php

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