ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
extension.cache.mysql.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
8 // //
9 // extension.cache.mysql.php - part of getID3() //
10 // Please see readme.txt for more information //
11 // ///
13 // //
14 // This extension written by Allan Hansen <ahØartemis*dk> //
15 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
16 // ///
18 
19 
75 {
76 
77  // private vars
78  private $cursor;
79  private $connection;
80 
81 
82  // public: constructor - see top of this file for cache type and cache_options
83  public function __construct($host, $database, $username, $password, $table='getid3_cache') {
84 
85  // Check for mysql support
86  if (!function_exists('mysql_pconnect')) {
87  throw new Exception('PHP not compiled with mysql support.');
88  }
89 
90  // Connect to database
91  $this->connection = mysql_pconnect($host, $username, $password);
92  if (!$this->connection) {
93  throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
94  }
95 
96  // Select database
97  if (!mysql_select_db($database, $this->connection)) {
98  throw new Exception('Cannot use database '.$database);
99  }
100 
101  // Set table
102  $this->table = $table;
103 
104  // Create cache table if not exists
105  $this->create_table();
106 
107  // Check version number and clear cache if changed
108  $version = '';
109  $SQLquery = 'SELECT `value`';
110  $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
111  $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string(getID3::VERSION).'\')';
112  $SQLquery .= ' AND (`filesize` = -1)';
113  $SQLquery .= ' AND (`filetime` = -1)';
114  $SQLquery .= ' AND (`analyzetime` = -1)';
115  if ($this->cursor = mysql_query($SQLquery, $this->connection)) {
116  list($version) = mysql_fetch_array($this->cursor);
117  }
118  if ($version != getID3::VERSION) {
119  $this->clear_cache();
120  }
121 
122  parent::__construct();
123  }
124 
125 
126 
127  // public: clear cache
128  public function clear_cache() {
129 
130  $this->cursor = mysql_query('DELETE FROM `'.mysql_real_escape_string($this->table).'`', $this->connection);
131  $this->cursor = mysql_query('INSERT INTO `'.mysql_real_escape_string($this->table).'` VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')', $this->connection);
132  }
133 
134 
135 
136  // public: analyze file
137  public function analyze($filename, $filesize=null, $original_filename='') {
138 
139  if (file_exists($filename)) {
140 
141  // Short-hands
142  $filetime = filemtime($filename);
143  $filesize = filesize($filename);
144 
145  // Lookup file
146  $SQLquery = 'SELECT `value`';
147  $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
148  $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string($filename).'\')';
149  $SQLquery .= ' AND (`filesize` = \''.mysql_real_escape_string($filesize).'\')';
150  $SQLquery .= ' AND (`filetime` = \''.mysql_real_escape_string($filetime).'\')';
151  $this->cursor = mysql_query($SQLquery, $this->connection);
152  if (mysql_num_rows($this->cursor) > 0) {
153  // Hit
154  list($result) = mysql_fetch_array($this->cursor);
155  return unserialize(base64_decode($result));
156  }
157  }
158 
159  // Miss
160  $analysis = parent::analyze($filename, $filesize, $original_filename);
161 
162  // Save result
163  if (file_exists($filename)) {
164  $SQLquery = 'INSERT INTO `'.mysql_real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
165  $SQLquery .= '\''.mysql_real_escape_string($filename).'\'';
166  $SQLquery .= ', \''.mysql_real_escape_string($filesize).'\'';
167  $SQLquery .= ', \''.mysql_real_escape_string($filetime).'\'';
168  $SQLquery .= ', \''.mysql_real_escape_string(time() ).'\'';
169  $SQLquery .= ', \''.mysql_real_escape_string(base64_encode(serialize($analysis))).'\')';
170  $this->cursor = mysql_query($SQLquery, $this->connection);
171  }
172  return $analysis;
173  }
174 
175 
176 
177  // private: (re)create sql table
178  private function create_table($drop=false) {
179 
180  $SQLquery = 'CREATE TABLE IF NOT EXISTS `'.mysql_real_escape_string($this->table).'` (';
181  $SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
182  $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
183  $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
184  $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
185  $SQLquery .= ', `value` LONGTEXT NOT NULL';
186  $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM CHARACTER SET=latin1 COLLATE=latin1_general_ci';
187  $this->cursor = mysql_query($SQLquery, $this->connection);
188  echo mysql_error($this->connection);
189  }
190 }
getID3() by James Heinrich info@getid3.org //
$filename
Definition: getid3.php:105
const VERSION
Definition: getid3.php:115
$password
Definition: pwgen.php:17
__construct($host, $database, $username, $password, $table='getid3_cache')
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(empty($password)) $table
Definition: pwgen.php:24