PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/app/CliTools/Utility/Typo3Utility.php

https://gitlab.com/vectorci/clitools
PHP | 158 lines | 68 code | 27 blank | 63 comment | 13 complexity | 87580277da00b262012cbd1f61ff32b5 MD5 | raw file
  1. <?php
  2. namespace CliTools\Utility;
  3. /*
  4. * CliTools Command
  5. * Copyright (C) 2016 WebDevOps.io
  6. * Copyright (C) 2015 Markus Blaschke <markus@familie-blaschke.net>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. use CliTools\Iterator\Filter\Typo3RecursiveDirectoryFilter;
  22. use Symfony\Component\Console\Input\InputInterface;
  23. class Typo3Utility
  24. {
  25. const PASSWORD_TYPE_MD5_SALTED = 'md5_salted';
  26. const PASSWORD_TYPE_MD5 = 'md5';
  27. /**
  28. * Generate TYPO3 password
  29. *
  30. * @param string $password Password
  31. * @param string $type Type of password (see constants)
  32. *
  33. * @return null|string
  34. */
  35. public static function generatePassword($password, $type = null)
  36. {
  37. $ret = null;
  38. if ($type === null) {
  39. $type = self::PASSWORD_TYPE_MD5_SALTED;
  40. }
  41. switch ($type) {
  42. // ##############
  43. // Salted MD5
  44. // ##############
  45. case self::PASSWORD_TYPE_MD5_SALTED:
  46. // Salted md5
  47. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  48. $salt = '$1$' . substr(str_shuffle($chars), 0, 6) . '$';
  49. $ret = crypt($password, $salt);
  50. break;
  51. // ##############
  52. // MD5
  53. // ##############
  54. case self::PASSWORD_TYPE_MD5:
  55. $ret = md5($password);
  56. break;
  57. }
  58. return $ret;
  59. }
  60. /**
  61. * Guess best TYPO3 base path
  62. *
  63. * @param string $basePath System base path
  64. * @param InputInterface $input Input instance
  65. * @param null|string $inputArgName Input option name for base path
  66. *
  67. * @return null|string
  68. * @throws \RuntimeException
  69. */
  70. public static function guessBestTypo3BasePath($basePath, $input = null, $inputArgName = null)
  71. {
  72. $ret = null;
  73. $userPath = null;
  74. if ($input !== null && $input instanceof InputInterface && $inputArgName !== null) {
  75. if ($input->hasArgument($inputArgName)) {
  76. $userPath = $input->getArgument($inputArgName);
  77. }
  78. }
  79. if (empty($userPath)) {
  80. // No user path specified, only use base path
  81. $ret = $basePath;
  82. } else {
  83. // check if path is an absolute path
  84. if (strpos($userPath, '/') === 0) {
  85. $ret = $userPath;
  86. } else {
  87. // relative path? try to guess the best match
  88. $guessPath = $basePath . '/' . $userPath;
  89. if (is_dir($guessPath)) {
  90. $ret = $guessPath;
  91. }
  92. }
  93. }
  94. if ($ret === null) {
  95. throw new \RuntimeException('Could not guess TYPO3 base path');
  96. }
  97. return $ret;
  98. }
  99. /**
  100. * Get TYPO3 instance paths
  101. *
  102. * @param string $basePath Base path
  103. * @param int $maxDepth Max depth
  104. *
  105. * @return array
  106. */
  107. public static function getTypo3InstancePathList($basePath, $maxDepth = 3)
  108. {
  109. $ret = array();
  110. // ####################
  111. // Iterators
  112. // ####################
  113. $dirIterator = new \RecursiveDirectoryIterator($basePath);
  114. $dirIterator = new Typo3RecursiveDirectoryFilter($dirIterator);
  115. $dirIterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST);
  116. $dirIterator->setMaxDepth($maxDepth);
  117. // ####################
  118. // Find and loop through TYPO3 instances
  119. // ####################
  120. foreach ($dirIterator as $dirEntry) {
  121. /** @var \DirectoryIterator $dirEntry */
  122. $dirPath = $dirEntry->getRealPath();
  123. // Check if current dir is possible typo3 instance
  124. if (is_dir($dirPath . '/typo3conf/')) {
  125. // seems to be a valid typo3 path
  126. $ret[] = $dirPath;
  127. }
  128. }
  129. return $ret;
  130. }
  131. }