PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/fof/utils/filescheck/filescheck.php

https://gitlab.com/ricardosanchez/prueba
PHP | 272 lines | 155 code | 42 blank | 75 comment | 33 complexity | 8bd14043102fc9e96a3c6f130dac356a MD5 | raw file
  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage utils
  5. * @copyright Copyright (C) 2010 - 2014 Akeeba Ltd. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. defined('FOF_INCLUDED') or die;
  9. /**
  10. * A utility class to check that your extension's files are not missing and have not been tampered with.
  11. *
  12. * You need a file called fileslist.php in your component's administrator root directory with the following contents:
  13. *
  14. * $phpFileChecker = array(
  15. * 'version' => 'revCEE2DAB',
  16. * 'date' => '2014-10-16',
  17. * 'directories' => array(
  18. * 'administrator/components/com_foobar',
  19. * ....
  20. * ),
  21. * 'files' => array(
  22. * 'administrator/components/com_foobar/access.xml' => array('705', '09aa0351a316bf011ecc8c1145134761', 'b95f00c7b49a07a60570dc674f2497c45c4e7152'),
  23. * ....
  24. * )
  25. * );
  26. *
  27. * All directory and file paths are relative to the site's root
  28. *
  29. * The directories array is a list of
  30. */
  31. class FOFUtilsFilescheck
  32. {
  33. /** @var string The name of the component */
  34. protected $option = '';
  35. /** @var string Current component version */
  36. protected $version = null;
  37. /** @var string Current component release date */
  38. protected $date = null;
  39. /** @var array List of files to check as filepath => (filesize, md5, sha1) */
  40. protected $fileList = array();
  41. /** @var array List of directories to check that exist */
  42. protected $dirList = array();
  43. /** @var bool Is the reported component version different than the version of the #__extensions table? */
  44. protected $wrongComponentVersion = false;
  45. /** @var bool Is the fileslist.php reporting a version different than the reported component version? */
  46. protected $wrongFilesVersion = false;
  47. /**
  48. * Create and initialise the object
  49. *
  50. * @param string $option Component name, e.g. com_foobar
  51. * @param string $version The current component version, as reported by the component
  52. * @param string $date The current component release date, as reported by the component
  53. */
  54. public function __construct($option, $version, $date)
  55. {
  56. // Initialise from parameters
  57. $this->option = $option;
  58. $this->version = $version;
  59. $this->date = $date;
  60. // Retrieve the date and version from the #__extensions table
  61. $db = JFactory::getDbo();
  62. $query = $db->getQuery(true)->select('*')->from($db->qn('#__extensions'))
  63. ->where($db->qn('element') . ' = ' . $db->q($this->option))
  64. ->where($db->qn('type') . ' = ' . $db->q('component'));
  65. $extension = $db->setQuery($query)->loadObject();
  66. // Check the version and date against those from #__extensions. I hate heavily nested IFs as much as the next
  67. // guy, but what can you do...
  68. if (!is_null($extension))
  69. {
  70. $manifestCache = $extension->manifest_cache;
  71. if (!empty($manifestCache))
  72. {
  73. $manifestCache = json_decode($manifestCache, true);
  74. if (is_array($manifestCache) && isset($manifestCache['creationDate']) && isset($manifestCache['version']))
  75. {
  76. // Make sure the fileslist.php version and date match the component's version
  77. if ($this->version != $manifestCache['version'])
  78. {
  79. $this->wrongComponentVersion = true;
  80. }
  81. if ($this->date != $manifestCache['creationDate'])
  82. {
  83. $this->wrongComponentVersion = true;
  84. }
  85. }
  86. }
  87. }
  88. // Try to load the fileslist.php file from the component's back-end root
  89. $filePath = JPATH_ADMINISTRATOR . '/components/' . $this->option . '/fileslist.php';
  90. if (!file_exists($filePath))
  91. {
  92. return;
  93. }
  94. include $filePath;
  95. // Make sure the fileslist.php version and date match the component's version
  96. if ($this->version != $phpFileChecker['version'])
  97. {
  98. $this->wrongFilesVersion = true;
  99. }
  100. if ($this->date != $phpFileChecker['date'])
  101. {
  102. $this->wrongFilesVersion = true;
  103. }
  104. // Initialise the files and directories lists
  105. $this->fileList = $phpFileChecker['files'];
  106. $this->dirList = $phpFileChecker['directories'];
  107. }
  108. /**
  109. * Is the reported component version different than the version of the #__extensions table?
  110. *
  111. * @return boolean
  112. */
  113. public function isWrongComponentVersion()
  114. {
  115. return $this->wrongComponentVersion;
  116. }
  117. /**
  118. * Is the fileslist.php reporting a version different than the reported component version?
  119. *
  120. * @return boolean
  121. */
  122. public function isWrongFilesVersion()
  123. {
  124. return $this->wrongFilesVersion;
  125. }
  126. /**
  127. * Performs a fast check of file and folders. If even one of the files/folders doesn't exist, or even one file has
  128. * the wrong file size it will return false.
  129. *
  130. * @return bool False when there are mismatched files and directories
  131. */
  132. public function fastCheck()
  133. {
  134. // Check that all directories exist
  135. foreach ($this->dirList as $directory)
  136. {
  137. $directory = JPATH_ROOT . '/' . $directory;
  138. if (!@is_dir($directory))
  139. {
  140. return false;
  141. }
  142. }
  143. // Check that all files exist and have the right size
  144. foreach ($this->fileList as $filePath => $fileData)
  145. {
  146. $filePath = JPATH_ROOT . '/' . $filePath;
  147. if (!@file_exists($filePath))
  148. {
  149. return false;
  150. }
  151. $fileSize = @filesize($filePath);
  152. if ($fileSize != $fileData[0])
  153. {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. /**
  160. * Performs a slow, thorough check of all files and folders (including MD5/SHA1 sum checks)
  161. *
  162. * @param int $idx The index from where to start
  163. *
  164. * @return array Progress report
  165. */
  166. public function slowCheck($idx = 0)
  167. {
  168. $ret = array(
  169. 'done' => false,
  170. 'files' => array(),
  171. 'folders' => array(),
  172. 'idx' => $idx
  173. );
  174. $totalFiles = count($this->fileList);
  175. $totalFolders = count($this->dirList);
  176. $fileKeys = array_keys($this->fileList);
  177. $timer = new FOFUtilsTimer(3.0, 75.0);
  178. while ($timer->getTimeLeft() && (($idx < $totalFiles) || ($idx < $totalFolders)))
  179. {
  180. if ($idx < $totalFolders)
  181. {
  182. $directory = JPATH_ROOT . '/' . $this->dirList[$idx];
  183. if (!@is_dir($directory))
  184. {
  185. $ret['folders'][] = $directory;
  186. }
  187. }
  188. if ($idx < $totalFiles)
  189. {
  190. $fileKey = $fileKeys[$idx];
  191. $filePath = JPATH_ROOT . '/' . $fileKey;
  192. $fileData = $this->fileList[$fileKey];
  193. if (!@file_exists($filePath))
  194. {
  195. $ret['files'][] = $fileKey . ' (missing)';
  196. }
  197. elseif (@filesize($filePath) != $fileData[0])
  198. {
  199. $ret['files'][] = $fileKey . ' (size ' . @filesize($filePath) . ' ≠ ' . $fileData[0] . ')';
  200. }
  201. else
  202. {
  203. if (function_exists('sha1_file'))
  204. {
  205. $fileSha1 = @sha1_file($filePath);
  206. if ($fileSha1 != $fileData[2])
  207. {
  208. $ret['files'][] = $fileKey . ' (SHA1 ' . $fileSha1 . ' ≠ ' . $fileData[2] . ')';
  209. }
  210. }
  211. elseif (function_exists('md5_file'))
  212. {
  213. $fileMd5 = @md5_file($filePath);
  214. if ($fileMd5 != $fileData[1])
  215. {
  216. $ret['files'][] = $fileKey . ' (MD5 ' . $fileMd5 . ' ≠ ' . $fileData[1] . ')';
  217. }
  218. }
  219. }
  220. }
  221. $idx++;
  222. }
  223. if (($idx >= $totalFiles) && ($idx >= $totalFolders))
  224. {
  225. $ret['done'] = true;
  226. }
  227. $ret['idx'] = $idx;
  228. return $ret;
  229. }
  230. }