3299caae005f4be93f7bcd6ee27bc4a069b52f01
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / extension.cache.mysqli.php
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.mysqli.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
20 /**
21 * This is a caching extension for getID3(). It works the exact same
22 * way as the getID3 class, but return cached information very fast
23 *
24 * Example: (see also demo.cache.mysql.php in /demo/)
25 *
26 * Normal getID3 usage (example):
27 *
28 * require_once 'getid3/getid3.php';
29 * $getID3 = new getID3;
30 * $getID3->encoding = 'UTF-8';
31 * $info1 = $getID3->analyze('file1.flac');
32 * $info2 = $getID3->analyze('file2.wv');
33 *
34 * getID3_cached usage:
35 *
36 * require_once 'getid3/getid3.php';
37 * require_once 'getid3/getid3/extension.cache.mysqli.php';
38 * // 5th parameter (tablename) is optional, default is 'getid3_cache'
39 * $getID3 = new getID3_cached_mysqli('localhost', 'database', 'username', 'password', 'tablename');
40 * $getID3->encoding = 'UTF-8';
41 * $info1 = $getID3->analyze('file1.flac');
42 * $info2 = $getID3->analyze('file2.wv');
43 *
44 *
45 * Supported Cache Types (this extension)
46 *
47 * SQL Databases:
48 *
49 * cache_type cache_options
50 * -------------------------------------------------------------------
51 * mysqli host, database, username, password
52 *
53 *
54 * DBM-Style Databases: (use extension.cache.dbm)
55 *
56 * cache_type cache_options
57 * -------------------------------------------------------------------
58 * gdbm dbm_filename, lock_filename
59 * ndbm dbm_filename, lock_filename
60 * db2 dbm_filename, lock_filename
61 * db3 dbm_filename, lock_filename
62 * db4 dbm_filename, lock_filename (PHP5 required)
63 *
64 * PHP must have write access to both dbm_filename and lock_filename.
65 *
66 *
67 * Recommended Cache Types
68 *
69 * Infrequent updates, many reads any DBM
70 * Frequent updates mysqli
71 */
72
73 class getID3_cached_mysqli extends getID3
74 {
75 // private vars
76 private $mysqli;
77 private $cursor;
78
79
80 // public: constructor - see top of this file for cache type and cache_options
81 public function __construct($host, $database, $username, $password, $table='getid3_cache') {
82
83 // Check for mysqli support
84 if (!function_exists('mysqli_connect')) {
85 throw new Exception('PHP not compiled with mysqli support.');
86 }
87
88 // Connect to database
89 $this->mysqli = new mysqli($host, $username, $password);
90 if (!$this->mysqli) {
91 throw new Exception('mysqli_connect() failed - check permissions and spelling.');
92 }
93
94 // Select database
95 if (!$this->mysqli->select_db($database)) {
96 throw new Exception('Cannot use database '.$database);
97 }
98
99 // Set table
100 $this->table = $table;
101
102 // Create cache table if not exists
103 $this->create_table();
104
105 // Check version number and clear cache if changed
106 $version = '';
107 $SQLquery = 'SELECT `value`';
108 $SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
109 $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli->real_escape_string(getID3::VERSION).'\')';
110 $SQLquery .= ' AND (`filesize` = -1)';
111 $SQLquery .= ' AND (`filetime` = -1)';
112 $SQLquery .= ' AND (`analyzetime` = -1)';
113 if ($this->cursor = $this->mysqli->query($SQLquery)) {
114 list($version) = $this->cursor->fetch_array();
115 }
116 if ($version != getID3::VERSION) {
117 $this->clear_cache();
118 }
119
120 parent::__construct();
121 }
122
123
124 // public: clear cache
125 public function clear_cache() {
126 $this->mysqli->query('DELETE FROM `'.$this->mysqli->real_escape_string($this->table).'`');
127 $this->mysqli->query('INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')');
128 }
129
130
131 // public: analyze file
132 public function analyze($filename, $filesize=null, $original_filename='') {
133
134 if (file_exists($filename)) {
135
136 // Short-hands
137 $filetime = filemtime($filename);
138 $filesize = filesize($filename);
139
140 // Lookup file
141 $SQLquery = 'SELECT `value`';
142 $SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
143 $SQLquery .= ' WHERE (`filename` = \''.$this->mysqli->real_escape_string($filename).'\')';
144 $SQLquery .= ' AND (`filesize` = \''.$this->mysqli->real_escape_string($filesize).'\')';
145 $SQLquery .= ' AND (`filetime` = \''.$this->mysqli->real_escape_string($filetime).'\')';
146 $this->cursor = $this->mysqli->query($SQLquery);
147 if ($this->cursor->num_rows > 0) {
148 // Hit
149 list($result) = $this->cursor->fetch_array();
150 return unserialize(base64_decode($result));
151 }
152 }
153
154 // Miss
155 $analysis = parent::analyze($filename, $filesize, $original_filename);
156
157 // Save result
158 if (file_exists($filename)) {
159 $SQLquery = 'INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
160 $SQLquery .= '\''.$this->mysqli->real_escape_string($filename).'\'';
161 $SQLquery .= ', \''.$this->mysqli->real_escape_string($filesize).'\'';
162 $SQLquery .= ', \''.$this->mysqli->real_escape_string($filetime).'\'';
163 $SQLquery .= ', \''.$this->mysqli->real_escape_string(time() ).'\'';
164 $SQLquery .= ', \''.$this->mysqli->real_escape_string(base64_encode(serialize($analysis))).'\')';
165 $this->cursor = $this->mysqli->query($SQLquery);
166 }
167 return $analysis;
168 }
169
170
171 // private: (re)create mysqli table
172 private function create_table($drop=false) {
173 $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.$this->mysqli->real_escape_string($this->table).'` (';
174 $SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
175 $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
176 $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
177 $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
178 $SQLquery .= ', `value` LONGTEXT NOT NULL';
179 $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM CHARACTER SET=latin1 COLLATE=latin1_general_ci';
180 $this->cursor = $this->mysqli->query($SQLquery);
181 echo $this->mysqli->error;
182 }
183 }