PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/library/Simplepie/Cache/Mysql.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 322 lines | 254 code | 19 blank | 49 comment | 34 complexity | 71d8c7a4fb62f02f352813e832003ca7 MD5 | raw file
  1. <?php
  2. /**
  3. * Simplepie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2011, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the Simplepie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package Simplepie
  36. * @version 1.2.1
  37. * @copyright 2004-2011 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ Simplepie
  42. * @link http://simplepie.org/support/ Please submit all bug reports and feature requests to the Simplepie forums
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @todo phpDoc comments
  45. */
  46. /**
  47. * To change this template, choose Tools | Templates
  48. * and open the template in the editor.
  49. */
  50. class Simplepie_Cache_Mysql extends Simplepie_Cache_Db
  51. {
  52. var $mysql;
  53. var $options;
  54. var $id;
  55. function Simplepie_Cache_Mysql($mysql_location, $name, $extension)
  56. {
  57. $host = $mysql_location->get_host();
  58. if (Simplepie_Misc::stripos($host, 'unix(') === 0 && substr($host, -1) === ')')
  59. {
  60. $server = ':' . substr($host, 5, -1);
  61. }
  62. else
  63. {
  64. $server = $host;
  65. if ($mysql_location->get_port() !== null)
  66. {
  67. $server .= ':' . $mysql_location->get_port();
  68. }
  69. }
  70. if (strpos($mysql_location->get_userinfo(), ':') !== false)
  71. {
  72. list($username, $password) = explode(':', $mysql_location->get_userinfo(), 2);
  73. }
  74. else
  75. {
  76. $username = $mysql_location->get_userinfo();
  77. $password = null;
  78. }
  79. if ($this->mysql = mysql_connect($server, $username, $password))
  80. {
  81. $this->id = $name . $extension;
  82. $this->options = Simplepie_Misc::parse_str($mysql_location->get_query());
  83. if (!isset($this->options['prefix'][0]))
  84. {
  85. $this->options['prefix'][0] = '';
  86. }
  87. if (mysql_select_db(ltrim($mysql_location->get_path(), '/'))
  88. && mysql_query('SET NAMES utf8')
  89. && ($query = mysql_unbuffered_query('SHOW TABLES')))
  90. {
  91. $db = array();
  92. while ($row = mysql_fetch_row($query))
  93. {
  94. $db[] = $row[0];
  95. }
  96. if (!in_array($this->options['prefix'][0] . 'cache_data', $db))
  97. {
  98. if (!mysql_query('CREATE TABLE `' . $this->options['prefix'][0] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))'))
  99. {
  100. $this->mysql = null;
  101. }
  102. }
  103. if (!in_array($this->options['prefix'][0] . 'items', $db))
  104. {
  105. if (!mysql_query('CREATE TABLE `' . $this->options['prefix'][0] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` TEXT CHARACTER SET utf8 NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))'))
  106. {
  107. $this->mysql = null;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. $this->mysql = null;
  114. }
  115. }
  116. }
  117. function save($data)
  118. {
  119. if ($this->mysql)
  120. {
  121. $feed_id = "'" . mysql_real_escape_string($this->id) . "'";
  122. if (is_a($data, 'Simplepie'))
  123. {
  124. if (SIMPLEPIE_PHP5)
  125. {
  126. // This keyword needs to defy coding standards for PHP4 compatibility
  127. $data = clone($data);
  128. }
  129. $prepared = $this->prepare_simplepie_object_for_cache($data);
  130. if ($query = mysql_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = ' . $feed_id, $this->mysql))
  131. {
  132. if (mysql_num_rows($query))
  133. {
  134. $items = count($prepared[1]);
  135. if ($items)
  136. {
  137. $sql = 'UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `items` = ' . $items . ', `data` = \'' . mysql_real_escape_string($prepared[0]) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id;
  138. }
  139. else
  140. {
  141. $sql = 'UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `data` = \'' . mysql_real_escape_string($prepared[0]) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id;
  142. }
  143. if (!mysql_query($sql, $this->mysql))
  144. {
  145. return false;
  146. }
  147. }
  148. elseif (!mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(' . $feed_id . ', ' . count($prepared[1]) . ', \'' . mysql_real_escape_string($prepared[0]) . '\', ' . time() . ')', $this->mysql))
  149. {
  150. return false;
  151. }
  152. $ids = array_keys($prepared[1]);
  153. if (!empty($ids))
  154. {
  155. foreach ($ids as $id)
  156. {
  157. $database_ids[] = mysql_real_escape_string($id);
  158. }
  159. if ($query = mysql_unbuffered_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'items` WHERE `id` = \'' . implode('\' OR `id` = \'', $database_ids) . '\' AND `feed_id` = ' . $feed_id, $this->mysql))
  160. {
  161. $existing_ids = array();
  162. while ($row = mysql_fetch_row($query))
  163. {
  164. $existing_ids[] = $row[0];
  165. }
  166. $new_ids = array_diff($ids, $existing_ids);
  167. foreach ($new_ids as $new_id)
  168. {
  169. if (!($date = $prepared[1][$new_id]->get_date('U')))
  170. {
  171. $date = time();
  172. }
  173. if (!mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(' . $feed_id . ', \'' . mysql_real_escape_string($new_id) . '\', \'' . mysql_real_escape_string(serialize($prepared[1][$new_id]->data)) . '\', ' . $date . ')', $this->mysql))
  174. {
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. }
  181. else
  182. {
  183. return true;
  184. }
  185. }
  186. }
  187. elseif ($query = mysql_query('SELECT `id` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = ' . $feed_id, $this->mysql))
  188. {
  189. if (mysql_num_rows($query))
  190. {
  191. if (mysql_query('UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `items` = 0, `data` = \'' . mysql_real_escape_string(serialize($data)) . '\', `mtime` = ' . time() . ' WHERE `id` = ' . $feed_id, $this->mysql))
  192. {
  193. return true;
  194. }
  195. }
  196. elseif (mysql_query('INSERT INTO `' . $this->options['prefix'][0] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(\'' . mysql_real_escape_string($this->id) . '\', 0, \'' . mysql_real_escape_string(serialize($data)) . '\', ' . time() . ')', $this->mysql))
  197. {
  198. return true;
  199. }
  200. }
  201. }
  202. return false;
  203. }
  204. function load()
  205. {
  206. if ($this->mysql && ($query = mysql_query('SELECT `items`, `data` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($row = mysql_fetch_row($query)))
  207. {
  208. $data = unserialize($row[1]);
  209. if (isset($this->options['items'][0]))
  210. {
  211. $items = (int) $this->options['items'][0];
  212. }
  213. else
  214. {
  215. $items = (int) $row[0];
  216. }
  217. if ($items !== 0)
  218. {
  219. if (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0]))
  220. {
  221. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['feed'][0];
  222. }
  223. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0]))
  224. {
  225. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_ATOM_03]['feed'][0];
  226. }
  227. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0]))
  228. {
  229. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RDF]['RDF'][0];
  230. }
  231. elseif (isset($data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0]))
  232. {
  233. $feed =& $data['child'][SIMPLEPIE_NAMESPACE_RSS_20]['rss'][0];
  234. }
  235. else
  236. {
  237. $feed = null;
  238. }
  239. if ($feed !== null)
  240. {
  241. $sql = 'SELECT `data` FROM `' . $this->options['prefix'][0] . 'items` WHERE `feed_id` = \'' . mysql_real_escape_string($this->id) . '\' ORDER BY `posted` DESC';
  242. if ($items > 0)
  243. {
  244. $sql .= ' LIMIT ' . $items;
  245. }
  246. if ($query = mysql_unbuffered_query($sql, $this->mysql))
  247. {
  248. while ($row = mysql_fetch_row($query))
  249. {
  250. $feed['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['entry'][] = unserialize($row[0]);
  251. }
  252. }
  253. else
  254. {
  255. return false;
  256. }
  257. }
  258. }
  259. return $data;
  260. }
  261. return false;
  262. }
  263. function mtime()
  264. {
  265. if ($this->mysql && ($query = mysql_query('SELECT `mtime` FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($row = mysql_fetch_row($query)))
  266. {
  267. return $row[0];
  268. }
  269. else
  270. {
  271. return false;
  272. }
  273. }
  274. function touch()
  275. {
  276. if ($this->mysql && ($query = mysql_query('UPDATE `' . $this->options['prefix'][0] . 'cache_data` SET `mtime` = ' . time() . ' WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && mysql_affected_rows($this->mysql))
  277. {
  278. return true;
  279. }
  280. else
  281. {
  282. return false;
  283. }
  284. }
  285. function unlink()
  286. {
  287. if ($this->mysql && ($query = mysql_query('DELETE FROM `' . $this->options['prefix'][0] . 'cache_data` WHERE `id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)) && ($query2 = mysql_query('DELETE FROM `' . $this->options['prefix'][0] . 'items` WHERE `feed_id` = \'' . mysql_real_escape_string($this->id) . "'", $this->mysql)))
  288. {
  289. return true;
  290. }
  291. else
  292. {
  293. return false;
  294. }
  295. }
  296. }
  297. ?>