BxDolStudioTools.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_DOL_PERM_FILE', 'file');
  10. define('BX_DOL_PERM_DIR', 'dir');
  11. define('BX_DOL_PERM_EXE', 'exe');
  12. define('BX_DOL_PERM_FAIL', false);
  13. define('BX_DOL_PERM_OK', true);
  14. class BxDolStudioTools extends BxDolIO
  15. {
  16. protected $bInstallScript;
  17. protected $sRootPath;
  18. public $aInstallPermissions;
  19. public $aPostInstallPermissions;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->aInstallPermissions = array(
  24. 'inc',
  25. 'cache',
  26. 'cache_public',
  27. 'logs',
  28. 'tmp',
  29. 'storage',
  30. defined('BX_SYSTEM_FFMPEG') ? bx_ltrim_str(BX_SYSTEM_FFMPEG, BX_DIRECTORY_PATH_ROOT) : 'plugins/ffmpeg/ffmpeg.exe',
  31. );
  32. // remove 'inc' folder if script is already installed
  33. if (defined('BX_DOL'))
  34. array_shift($this->aInstallPermissions);
  35. $this->aPostInstallPermissions = array(
  36. );
  37. if (defined('BX_DOL_INSTALL') && BX_DOL_INSTALL) {
  38. $this->bInstallScript = true;
  39. $this->sRootPath = BX_INSTALL_URL_ROOT;
  40. } else {
  41. $this->bInstallScript = false;
  42. $this->sRootPath = BX_DOL_URL_ROOT;
  43. }
  44. }
  45. public function generateStyles()
  46. {
  47. $sRet = <<<EOF
  48. <style type="text/css">
  49. .bx-adm-hidden {
  50. display:none;
  51. }
  52. .bx-permissions-table {
  53. border-collapse:collapse;
  54. }
  55. .bx-permissions-table thead td {
  56. font-weight:bold;
  57. }
  58. .bx-permissions-table td:not(:first-child) {
  59. text-align:center;
  60. }
  61. .bx-permissions-wrong {
  62. color:red;
  63. font-weight:bold;
  64. }
  65. .bx-permissions-ok {
  66. color:green;
  67. font-weight:bold;
  68. }
  69. </style>
  70. EOF;
  71. return $sRet;
  72. }
  73. public function checkPermissions($isShowModules = false, $bEcho = true, &$aOutputMessages = null)
  74. {
  75. $bRet = true;
  76. $aMessages = array ();
  77. foreach ($this->aInstallPermissions as $s) {
  78. $sType = $this->_getFileType($s);
  79. $isOk = BX_DOL_PERM_EXE == $sType ? $this->isExecutable($s) : $this->isWritable($s);
  80. $aMessages[$s] = array ('res' => $isOk ? BX_DOL_PERM_OK : BX_DOL_PERM_FAIL, 'type' => $sType);
  81. if (!$isOk && $bRet)
  82. $bRet = false;
  83. }
  84. if ($isShowModules && !$this->_checkPermissionsModules($aMessages) && $bRet)
  85. $bRet = false;
  86. if (null !== $aOutputMessages)
  87. $aOutputMessages = $aMessages;
  88. if ($bEcho) {
  89. $sHtml = '';
  90. foreach ($aMessages as $s => $r)
  91. $sHtml .= $this->_getHtmlPermissionRow($s, $r);
  92. echo $this->_getHtmlPermissionTable($sHtml);
  93. }
  94. return $bRet;
  95. }
  96. /**
  97. * The permission checks as data: each path with whether it passes and its current and desired state as titles.
  98. */
  99. public function getPermissionsReport($isShowModules = false)
  100. {
  101. $aMessages = array();
  102. $this->checkPermissions($isShowModules, false, $aMessages);
  103. $aRows = array();
  104. foreach ($aMessages as $s => $r)
  105. $aRows[] = $this->_getPermissionRow($s, $r);
  106. return $aRows;
  107. }
  108. /**
  109. * One permission check as data: the path, whether it passes, and its current and desired state as titles.
  110. */
  111. protected function _getPermissionRow($s, $r)
  112. {
  113. $sDesired = BX_DOL_PERM_EXE == $r['type'] ? _t('_adm_admtools_Executable') : _t('_adm_admtools_Writable');
  114. $sCurrent = $sDesired;
  115. if (BX_DOL_PERM_FAIL == $r['res']) {
  116. if (false === $this->getPermissions($s))
  117. $sCurrent = _t('_adm_admtools_Not_Exists');
  118. else
  119. $sCurrent = BX_DOL_PERM_EXE == $r['type'] ? _t('_adm_admtools_Non_Executable') : _t('_adm_admtools_Non_Writable');
  120. }
  121. return array('path' => $s, 'ok' => BX_DOL_PERM_OK == $r['res'], 'current' => $sCurrent, 'desired' => $sDesired);
  122. }
  123. protected function _getHtmlPermissionRow($s, $r)
  124. {
  125. $aRow = $this->_getPermissionRow($s, $r);
  126. $sClass = $aRow['ok'] ? 'bx-permissions-ok' : 'bx-permissions-wrong';
  127. return <<<EOF
  128. <tr class="bx-def-color-bg-hl-even">
  129. <td class="bx-def-padding-thd">{$aRow['path']}</td>
  130. <td class="bx-def-padding-thd"><span class="{$sClass}">{$aRow['current']}</span></td>
  131. <td class="bx-def-padding-thd">{$aRow['desired']}</td>
  132. </tr>
  133. EOF;
  134. }
  135. protected function _getHtmlPermissionTable($sRows)
  136. {
  137. $sDirsC = _t('_adm_admtools_Path');
  138. $sCurrentLevelC = _t('_adm_admtools_Current_level');
  139. $sDesiredLevelC = _t('_adm_admtools_Desired_level');
  140. return <<<EOF
  141. <table width="100%" class="bx-permissions-table">
  142. <thead class="bx-def-border-bottom bx-def-border-top">
  143. <tr>
  144. <td class="bx-def-padding-thd bx-def-font-h3">{$sDirsC}</td>
  145. <td class="bx-def-padding-thd bx-def-font-h3">{$sCurrentLevelC}</td>
  146. <td class="bx-def-padding-thd bx-def-font-h3">{$sDesiredLevelC}</td>
  147. </tr>
  148. </thead>
  149. <tbody>
  150. {$sRows}
  151. </tbody>
  152. </table>
  153. EOF;
  154. }
  155. protected function _getFileType($s)
  156. {
  157. $sType = BX_DOL_PERM_FILE;
  158. if (is_dir($this->sRootPath . $s))
  159. $sType = BX_DOL_PERM_DIR;
  160. elseif (substr($s, -4) === '.exe')
  161. $sType = BX_DOL_PERM_EXE;
  162. return $sType;
  163. }
  164. protected function _checkPermissionsModules(&$aMessages)
  165. {
  166. $bRet = true;
  167. $oDbModules = new BxDolModuleDb();
  168. $aModules = $oDbModules->getModules();
  169. foreach ($aModules as $a) {
  170. if (empty($a['path']) || !include(BX_DIRECTORY_PATH_MODULES . $a['path'] . 'install/config.php'))
  171. continue;
  172. if (empty($aConfig['install_permissions']) || !is_array($aConfig['install_permissions']['writable']))
  173. continue;
  174. foreach ($aConfig['install_permissions']['writable'] as $sPath) {
  175. $s = basename(BX_DIRECTORY_PATH_MODULES) . '/' . $a['path'] . $sPath;
  176. $sType = $this->_getFileType($s);
  177. $isOk = BX_DOL_PERM_EXE ? $this->isExecutable($s) : $this->isWritable($s);
  178. $aMessages[$s] = array ('res' => $isOk ? BX_DOL_PERM_OK : BX_DOL_PERM_FAIL, 'type' => $sType);
  179. if (!$isOk && $bRet)
  180. $bRet = false;
  181. }
  182. }
  183. return $bRet;
  184. }
  185. }
  186. /** @} */