PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/source/innomatic/core/classes/innomatic/datatransfer/cache/CachedItem.php

https://bitbucket.org/innoteam/innomatic
PHP | 280 lines | 182 code | 22 blank | 76 comment | 35 complexity | 6220a0d006372c60c2e95eb49b176e77 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Innomatic
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.
  9. *
  10. * @copyright 1999-2012 Innoteam S.r.l.
  11. * @license http://www.innomatic.org/license/ BSD License
  12. * @link http://www.innomatic.org
  13. * @since Class available since Release 5.0
  14. */
  15. require_once('innomatic/dataaccess/DataAccess.php');
  16. /*!
  17. @class CachedItem
  18. @abstract Handles Innomatic items cache.
  19. @discussion Handles Innomatic items cache.
  20. */
  21. class CachedItem
  22. {
  23. /*! @var mrRootDb DataAccess class - Innomatic database handler. */
  24. protected $mrRootDb;
  25. /*! @var mApplication string - Application id name. */
  26. public $mApplication;
  27. /*! @var mItemId string - Item id. */
  28. public $mItemId;
  29. /*! @var mItemFile string - Cached item file name. */
  30. public $mItemFile;
  31. /*! @var mValidator string - Optional item validator. */
  32. public $mValidator;
  33. /*! @var mResult integer - Last action result; may be one of the CachedItem::ITEM_x defines. */
  34. public $mResult = 0;
  35. public $mDomainId = 0;
  36. public $mUserId = 0;
  37. private $cachePath;
  38. const ITEM_FOUND = -1;
  39. const ITEM_NOT_FOUND = -2;
  40. const ITEM_STORED = -5;
  41. const ITEM_NOT_STORED = -6;
  42. const ITEM_NOT_EQUAL = -3;
  43. const ITEM_EQUAL = -4;
  44. /*!
  45. @function CachedItem
  46. @abstract Class constructor.
  47. @discussion Class constructor.
  48. @param rrootDb DataAccess class - Innomatic database handler.
  49. @param application string - Application id name.
  50. @param itemId string - Item id.
  51. */
  52. public function CachedItem(DataAccess $rrootDb, $application, $itemId, $domainId = 0, $userId = 0)
  53. {
  54. $this->cachePath = InnomaticContainer::instance('innomaticcontainer')->getHome().'core/temp/cache/';
  55. $domainId = (int) $domainId;
  56. $userId = (int) $userId;
  57. $this->mrRootDb = $rrootDb;
  58. if (!$rrootDb->isConnected()) {
  59. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  60. } else {
  61. if (strlen($itemId) and strlen($application)) {
  62. $this->mItemId = $itemId;
  63. $this->mApplication = $application;
  64. $this->mDomainId = $domainId;
  65. $this->mUserId = $userId;
  66. $item_query = $this->mrRootDb->execute('SELECT itemfile,validator,domainid,userid FROM cache_items WHERE application='.$this->mrRootDb->formatText($this->mApplication).' AND itemid='.$this->mrRootDb->formatText($this->mItemId). ($domainId ? ' AND domainid='.$domainId : ''). ($userId ? ' AND userid='.$userId : ''));
  67. if ($item_query->getNumberRows()) {
  68. $this->mValidator = $item_query->getFields('validator');
  69. $this->mItemFile = $this->cachePath.$item_query->getFields('itemfile');
  70. $this->mDomainId = $item_query->getFields('domainid');
  71. $this->mUserId = $item_query->getFields('userid');
  72. $this->mResult = CachedItem::ITEM_FOUND;
  73. } else
  74. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  75. }
  76. }
  77. }
  78. /*!
  79. @function Store
  80. @abstract Stores the item in the cache.
  81. @discussion Stores the item in the cache.
  82. @param $content string - Item content.
  83. @param $validator string - Optional validator.
  84. @result TRUE if the item has been stored.
  85. */
  86. public function store($content, $validator = '')
  87. {
  88. $result = false;
  89. $this->mResult = CachedItem::ITEM_NOT_STORED;
  90. if (!$this->mrRootDb->isConnected()) {
  91. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  92. return false;
  93. }
  94. $goon = false;
  95. require_once('innomatic/process/Semaphore.php');
  96. $sem = new Semaphore('cache', $this->mItemFile);
  97. $sem->WaitGreen();
  98. $sem->setRed();
  99. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  100. if ($fh = @fopen($this->mItemFile, 'w')) {
  101. if (@fwrite($fh, $content)) {
  102. $name = $this->mItemFile;
  103. $goon = true;
  104. }
  105. fclose($fh);
  106. }
  107. } else {
  108. $name = $this->cachePath.date('Ymd').'_cacheditem_'.rand();
  109. if (!file_exists($this->cachePath)) {
  110. require_once('innomatic/io/filesystem/DirectoryUtils.php');
  111. DirectoryUtils::mktree($this->cachePath, 0755);
  112. }
  113. if ($fh = @fopen($name, 'w')) {
  114. if (@fwrite($fh, $content)) {
  115. $goon = true;
  116. }
  117. @fclose($fh);
  118. }
  119. }
  120. if ($goon) {
  121. $item_query = $this->mrRootDb->execute('SELECT itemid FROM cache_items WHERE itemid='.$this->mrRootDb->formatText($this->mItemId).' AND application='.$this->mrRootDb->formatText($this->mApplication). ($this->mDomainId ? ' AND domainid='.$this->mDomainId : ''). ($this->mUserId ? ' AND userid='.$this->mUserId : ''));
  122. if ($item_query->getNumberRows()) {
  123. if ($this->mrRootDb->execute('UPDATE cache_items SET validator='.$this->mrRootDb->formatText($validator).',itemfile='.$this->mrRootDb->formatText(basename($name)).',domainid='.$this->mDomainId.',userid='.$this->mUserId.' WHERE itemid='.$this->mrRootDb->formatText($this->mItemId).' AND application='.$this->mrRootDb->formatText($this->mApplication))) {
  124. $result = true;
  125. }
  126. } else {
  127. if ($this->mrRootDb->execute('INSERT INTO cache_items VALUES ('.$this->mrRootDb->formatText($this->mApplication).','.$this->mrRootDb->formatText($this->mItemId).','.$this->mrRootDb->formatText(basename($name)).','.$this->mrRootDb->formatText($validator).','.$this->mDomainId.','.$this->mUserId.')')) {
  128. $result = true;
  129. }
  130. }
  131. if ($result) {
  132. $this->mItemFile = $name;
  133. $this->mValidator = $validator;
  134. $this->mResult = CachedItem::ITEM_STORED;
  135. }
  136. }
  137. // The semaphore gets unlocked anyway, even if the operation has failed.
  138. $sem->setGreen();
  139. return $result;
  140. }
  141. /*!
  142. @function Retrieve
  143. @abstract Retrieves the item from the cache.
  144. @discussion Retrieves the item from the cache.
  145. @param md5 string - Optional md5 hash to be checked with the cached item one.
  146. @result The item content.
  147. */
  148. public function retrieve($md5 = '')
  149. {
  150. $result = false;
  151. require_once('innomatic/process/Semaphore.php');
  152. $sem = new Semaphore('cache', $this->mItemFile);
  153. $sem->WaitGreen();
  154. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  155. $sem->setRed();
  156. $goon = true;
  157. if (strlen($md5)) {
  158. if ($this->getItemMd5() == $md5)
  159. $goon = true;
  160. else
  161. $goon = false;
  162. }
  163. if ($goon) {
  164. if (file_exists($this->mItemFile)) {
  165. $result = file_get_contents($this->mItemFile);
  166. }
  167. } else
  168. $this->mResult = CachedItem::ITEM_NOT_EQUAL;
  169. $sem->setGreen();
  170. } else
  171. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  172. return $result;
  173. }
  174. /*!
  175. @function CheckValidator
  176. @abstract Checks if the optional validator is equal to a given one.
  177. @discussion Checks if the optional validator is equal to a given one.
  178. @param validator string - Validator to be checked.
  179. @result TRUE if the validators are equals.
  180. */
  181. public function checkValidator($validator)
  182. {
  183. $result = false;
  184. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  185. if ($validator == $this->mValidator)
  186. $result = true;
  187. }
  188. return $result;
  189. }
  190. /*!
  191. @function Destroy
  192. @abstract Destroys the item from the cache.
  193. @discussion Destroys the item from the cache.
  194. @result TRUE if the item has been destroyed.
  195. */
  196. public function destroy()
  197. {
  198. $result = false;
  199. require_once('innomatic/process/Semaphore.php');
  200. $sem = new Semaphore('cache', $this->mItemFile);
  201. $sem->WaitGreen();
  202. $sem->setRed();
  203. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  204. $result = @unlink($this->mItemFile);
  205. } else
  206. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  207. if ($result)
  208. $result = $this->mrRootDb->execute('DELETE FROM cache_items WHERE application='.$this->mrRootDb->formatText($this->mApplication).' AND itemid='.$this->mrRootDb->formatText($this->mItemId));
  209. $sem->setGreen();
  210. return $result;
  211. }
  212. /*!
  213. @function CompareMd5
  214. @abstract Checks if the md5 of the cached item is equal to the md5 of a given item.
  215. @discussion Checks if the md5 of the cached item is equal to the md5 of a given item.
  216. @param itemContent string - Content of the item to be checked.
  217. @result TRUE if the md5 of the items are equals.
  218. */
  219. public function compareMd5($itemContent)
  220. {
  221. $result = false;
  222. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  223. if (md5($itemContent) == $this->getItemMd5()) {
  224. $this->mResult = CachedItem::ITEM_EQUAL;
  225. $result = true;
  226. } else
  227. $this->mResult = CachedItem::ITEM_NOT_EQUAL;
  228. } else
  229. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  230. return $result;
  231. }
  232. /*!
  233. @function getItemMd5
  234. @abstract Gets the md5 hash of the file, in order to compare it with the original item.
  235. @discussion Gets the md5 hash of the file, in order to compare it with the original item.
  236. @result The md5 hash.
  237. */
  238. public function getItemMd5()
  239. {
  240. $result = false;
  241. if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
  242. if (function_exists('md5_file'))
  243. $result = md5_file($this->mItemFile);
  244. else {
  245. $result = md5(file_get_contents($this->mItemFile));
  246. }
  247. $this->mResult = CachedItem::ITEM_FOUND;
  248. } else
  249. $this->mResult = CachedItem::ITEM_NOT_FOUND;
  250. return $result;
  251. }
  252. public function getCachePath()
  253. {
  254. return $this->cachePath;
  255. }
  256. }