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.

106 lines
4.0 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\App\CodeChecker;
  25. class DatabaseSchemaChecker {
  26. /**
  27. * @param string $appId
  28. * @return array
  29. */
  30. public function analyse($appId) {
  31. $appPath = \OC_App::getAppPath($appId);
  32. if ($appPath === false) {
  33. throw new \RuntimeException("No app with given id <$appId> known.");
  34. }
  35. if (!file_exists($appPath . '/appinfo/database.xml')) {
  36. return ['errors' => [], 'warnings' => []];
  37. }
  38. libxml_use_internal_errors(true);
  39. $loadEntities = libxml_disable_entity_loader(false);
  40. $xml = simplexml_load_file($appPath . '/appinfo/database.xml');
  41. libxml_disable_entity_loader($loadEntities);
  42. $errors = $warnings = [];
  43. foreach ($xml->table as $table) {
  44. // Table names
  45. if (strpos((string)$table->name, '*dbprefix*') !== 0) {
  46. $errors[] = 'Database schema error: name of table ' . (string)$table->name . ' does not start with *dbprefix*';
  47. }
  48. $tableName = substr((string)$table->name, strlen('*dbprefix*'));
  49. if (strpos($tableName, '*dbprefix*') !== false) {
  50. $warnings[] = 'Database schema warning: *dbprefix* should only appear once in name of table ' . (string)$table->name;
  51. }
  52. if (strlen($tableName) > 27) {
  53. $errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
  54. }
  55. $hasAutoIncrement = false;
  56. // Column names
  57. foreach ($table->declaration->field as $column) {
  58. if (strpos((string)$column->name, '*dbprefix*') !== false) {
  59. $warnings[] = 'Database schema warning: *dbprefix* should not appear in name of column ' . (string)$column->name . ' on table ' . (string)$table->name;
  60. }
  61. if (strlen((string)$column->name) > 30) {
  62. $errors[] = 'Database schema error: Name of column ' . (string)$column->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 30 characters allowed';
  63. }
  64. if ($column->autoincrement) {
  65. if ($hasAutoIncrement) {
  66. $errors[] = 'Database schema error: Table ' . (string)$table->name . ' has multiple autoincrement columns';
  67. }
  68. if (strlen($tableName) > 21) {
  69. $errors[] = 'Database schema error: Name of table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters (21 characters for tables with autoincrement) + *dbprefix* allowed';
  70. }
  71. $hasAutoIncrement = true;
  72. }
  73. }
  74. // Index names
  75. foreach ($table->declaration->index as $index) {
  76. $hasPrefix = strpos((string)$index->name, '*dbprefix*');
  77. if ($hasPrefix !== false && $hasPrefix !== 0) {
  78. $warnings[] = 'Database schema warning: *dbprefix* should only appear at the beginning in name of index ' . (string)$index->name . ' on table ' . (string)$table->name;
  79. }
  80. $indexName = $hasPrefix === 0 ? substr((string)$index->name, strlen('*dbprefix*')) : (string)$index->name;
  81. if (strlen($indexName) > 27) {
  82. $errors[] = 'Database schema error: Name of index ' . (string)$index->name . ' on table ' . (string)$table->name . ' is too long (' . strlen($tableName) . '), max. 27 characters + *dbprefix* allowed';
  83. }
  84. }
  85. }
  86. return ['errors' => $errors, 'warnings' => $warnings];
  87. }
  88. }