PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/getid3/extension.cache.mysql.php

https://bitbucket.org/chamilo/chamilo/
PHP | 171 lines | 58 code | 32 blank | 81 comment | 8 complexity | 2bfa99184c7169ee517df5f626c070c9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. /////////////////////////////////////////////////////////////////
  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. // ///
  15. /////////////////////////////////////////////////////////////////
  16. /**
  17. * This is a caching extension for getID3(). It works the exact same
  18. * way as the getID3 class, but return cached information very fast
  19. *
  20. * Example: (see also demo.cache.mysql.php in /demo/)
  21. *
  22. * Normal getID3 usage (example):
  23. *
  24. * require_once 'getid3/getid3.php';
  25. * $getID3 = new getID3;
  26. * $getID3->encoding = 'UTF-8';
  27. * $info1 = $getID3->analyze('file1.flac');
  28. * $info2 = $getID3->analyze('file2.wv');
  29. *
  30. * getID3_cached usage:
  31. *
  32. * require_once 'getid3/getid3.php';
  33. * require_once 'getid3/getid3/extension.cache.mysql.php';
  34. * $getID3 = new getID3_cached_mysql('localhost', 'database',
  35. * 'username', 'password');
  36. * $getID3->encoding = 'UTF-8';
  37. * $info1 = $getID3->analyze('file1.flac');
  38. * $info2 = $getID3->analyze('file2.wv');
  39. *
  40. *
  41. * Supported Cache Types (this extension)
  42. *
  43. * SQL Databases:
  44. *
  45. * cache_type cache_options
  46. * -------------------------------------------------------------------
  47. * mysql host, database, username, password
  48. *
  49. *
  50. * DBM-Style Databases: (use extension.cache.dbm)
  51. *
  52. * cache_type cache_options
  53. * -------------------------------------------------------------------
  54. * gdbm dbm_filename, lock_filename
  55. * ndbm dbm_filename, lock_filename
  56. * db2 dbm_filename, lock_filename
  57. * db3 dbm_filename, lock_filename
  58. * db4 dbm_filename, lock_filename (PHP5 required)
  59. *
  60. * PHP must have write access to both dbm_filename and lock_filename.
  61. *
  62. *
  63. * Recommended Cache Types
  64. *
  65. * Infrequent updates, many reads any DBM
  66. * Frequent updates mysql
  67. */
  68. class getID3_cached_mysql extends getID3
  69. {
  70. // private vars
  71. var $cursor;
  72. var $connection;
  73. // public: constructor - see top of this file for cache type and cache_options
  74. function __construct($host, $database, $username, $password) {
  75. // Check for mysql support
  76. if (!function_exists('mysql_pconnect')) {
  77. die('PHP not compiled with mysql support.');
  78. }
  79. // Connect to database
  80. $this->connection = mysql_pconnect($host, $username, $password);
  81. if (!$this->connection) {
  82. die('mysql_pconnect() failed - check permissions and spelling.');
  83. }
  84. // Select database
  85. if (!mysql_select_db($database, $this->connection)) {
  86. die('Cannot use database '.$database);
  87. }
  88. // Create cache table if not exists
  89. $this->create_table();
  90. // Check version number and clear cache if changed
  91. $this->cursor = mysql_query("SELECT `value` FROM `getid3_cache` WHERE (`filename` = '".GETID3_VERSION."') AND (`filesize` = '-1') AND (`filetime` = '-1') AND (`analyzetime` = '-1')", $this->connection);
  92. list($version) = @mysql_fetch_array($this->cursor);
  93. if ($version != GETID3_VERSION) {
  94. $this->clear_cache();
  95. }
  96. parent::getID3();
  97. }
  98. // public: clear cache
  99. function clear_cache() {
  100. $this->cursor = mysql_query("DELETE FROM `getid3_cache`", $this->connection);
  101. $this->cursor = mysql_query("INSERT INTO `getid3_cache` VALUES ('".GETID3_VERSION."', -1, -1, -1, '".GETID3_VERSION."')", $this->connection);
  102. }
  103. // public: analyze file
  104. function analyze($filename) {
  105. if (file_exists($filename)) {
  106. // Short-hands
  107. $filetime = filemtime($filename);
  108. $filesize = filesize($filename);
  109. $filenam2 = mysql_escape_string($filename);
  110. // Loopup file
  111. $this->cursor = mysql_query("SELECT `value` FROM `getid3_cache` WHERE (`filename`='".$filenam2."') AND (`filesize`='".$filesize."') AND (`filetime`='".$filetime."')", $this->connection);
  112. list($result) = @mysql_fetch_array($this->cursor);
  113. // Hit
  114. if ($result) {
  115. return unserialize($result);
  116. }
  117. }
  118. // Miss
  119. $result = parent::analyze($filename);
  120. // Save result
  121. if (file_exists($filename)) {
  122. $res2 = mysql_escape_string(serialize($result));
  123. $this->cursor = mysql_query("INSERT INTO `getid3_cache` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES ('".$filenam2."', '".$filesize."', '".$filetime."', '".time()."', '".$res2."')", $this->connection);
  124. }
  125. return $result;
  126. }
  127. // private: (re)create sql table
  128. function create_table($drop = false) {
  129. $this->cursor = mysql_query("CREATE TABLE IF NOT EXISTS `getid3_cache` (
  130. `filename` VARCHAR(255) NOT NULL DEFAULT '',
  131. `filesize` INT(11) NOT NULL DEFAULT '0',
  132. `filetime` INT(11) NOT NULL DEFAULT '0',
  133. `analyzetime` INT(11) NOT NULL DEFAULT '0',
  134. `value` TEXT NOT NULL,
  135. PRIMARY KEY (`filename`,`filesize`,`filetime`)) TYPE=MyISAM", $this->connection);
  136. echo mysql_error($this->connection);
  137. }
  138. }
  139. ?>