InfoChecker.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\App\CodeChecker;
  24. use OC\Hooks\BasicEmitter;
  25. use OCP\App\AppPathNotFoundException;
  26. use OCP\App\IAppManager;
  27. class InfoChecker extends BasicEmitter {
  28. /** @var string[] */
  29. private $shippedApps;
  30. /** @var string[] */
  31. private $alwaysEnabled;
  32. /**
  33. * @param string $appId
  34. * @return array
  35. * @throws \RuntimeException
  36. */
  37. public function analyse($appId): array {
  38. $appPath = \OC_App::getAppPath($appId);
  39. if ($appPath === false) {
  40. throw new \RuntimeException("No app with given id <$appId> known.");
  41. }
  42. $xml = new \DOMDocument();
  43. $xml->load($appPath . '/appinfo/info.xml');
  44. $schema = \OC::$SERVERROOT . '/resources/app-info.xsd';
  45. try {
  46. if ($this->isShipped($appId)) {
  47. // Shipped apps are allowed to have the public and default_enabled tags
  48. $schema = \OC::$SERVERROOT . '/resources/app-info-shipped.xsd';
  49. }
  50. } catch (\Exception $e) {
  51. // Assume it is not shipped
  52. }
  53. $errors = [];
  54. if (!$xml->schemaValidate($schema)) {
  55. foreach (libxml_get_errors() as $error) {
  56. $errors[] = [
  57. 'type' => 'parseError',
  58. 'field' => $error->message,
  59. ];
  60. $this->emit('InfoChecker', 'parseError', [$error->message]);
  61. }
  62. }
  63. return $errors;
  64. }
  65. /**
  66. * This is a copy of \OC\App\AppManager::isShipped(), keep both in sync.
  67. * This method is copied, so the code checker works even when Nextcloud is
  68. * not installed yet. The AppManager requires a database connection, which
  69. * fails in that case.
  70. *
  71. * @param string $appId
  72. * @return bool
  73. * @throws \Exception
  74. */
  75. protected function isShipped(string $appId): bool {
  76. $this->loadShippedJson();
  77. return \in_array($appId, $this->shippedApps, true);
  78. }
  79. /**
  80. * This is a copy of \OC\App\AppManager::loadShippedJson(), keep both in sync
  81. * This method is copied, so the code checker works even when Nextcloud is
  82. * not installed yet. The AppManager requires a database connection, which
  83. * fails in that case.
  84. *
  85. * @throws \Exception
  86. */
  87. protected function loadShippedJson() {
  88. if ($this->shippedApps === null) {
  89. $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
  90. if (!file_exists($shippedJson)) {
  91. throw new \Exception("File not found: $shippedJson");
  92. }
  93. $content = json_decode(file_get_contents($shippedJson), true);
  94. $this->shippedApps = $content['shippedApps'];
  95. $this->alwaysEnabled = $content['alwaysEnabled'];
  96. }
  97. }
  98. }