You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

3 years ago
  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. class InfoChecker extends BasicEmitter {
  26. /** @var string[] */
  27. private $shippedApps;
  28. /** @var string[] */
  29. private $alwaysEnabled;
  30. /**
  31. * @param string $appId
  32. * @return array
  33. * @throws \RuntimeException
  34. */
  35. public function analyse($appId): array {
  36. $appPath = \OC_App::getAppPath($appId);
  37. if ($appPath === false) {
  38. throw new \RuntimeException("No app with given id <$appId> known.");
  39. }
  40. $xml = new \DOMDocument();
  41. $xml->load($appPath . '/appinfo/info.xml');
  42. $schema = \OC::$SERVERROOT . '/resources/app-info.xsd';
  43. try {
  44. if ($this->isShipped($appId)) {
  45. // Shipped apps are allowed to have the public and default_enabled tags
  46. $schema = \OC::$SERVERROOT . '/resources/app-info-shipped.xsd';
  47. }
  48. } catch (\Exception $e) {
  49. // Assume it is not shipped
  50. }
  51. $errors = [];
  52. if (!$xml->schemaValidate($schema)) {
  53. foreach (libxml_get_errors() as $error) {
  54. $errors[] = [
  55. 'type' => 'parseError',
  56. 'field' => $error->message,
  57. ];
  58. $this->emit('InfoChecker', 'parseError', [$error->message]);
  59. }
  60. }
  61. return $errors;
  62. }
  63. /**
  64. * This is a copy of \OC\App\AppManager::isShipped(), keep both in sync.
  65. * This method is copied, so the code checker works even when Nextcloud is
  66. * not installed yet. The AppManager requires a database connection, which
  67. * fails in that case.
  68. *
  69. * @param string $appId
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. protected function isShipped(string $appId): bool {
  74. $this->loadShippedJson();
  75. return \in_array($appId, $this->shippedApps, true);
  76. }
  77. /**
  78. * This is a copy of \OC\App\AppManager::loadShippedJson(), keep both in sync
  79. * This method is copied, so the code checker works even when Nextcloud is
  80. * not installed yet. The AppManager requires a database connection, which
  81. * fails in that case.
  82. *
  83. * @throws \Exception
  84. */
  85. protected function loadShippedJson() {
  86. if ($this->shippedApps === null) {
  87. $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
  88. if (!file_exists($shippedJson)) {
  89. throw new \Exception("File not found: $shippedJson");
  90. }
  91. $content = json_decode(file_get_contents($shippedJson), true);
  92. $this->shippedApps = $content['shippedApps'];
  93. $this->alwaysEnabled = $content['alwaysEnabled'];
  94. }
  95. }
  96. }