BxDolStudioTools.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. .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. function GenCacheEnginesTable()
  97. {
  98. $sRet = '<table width="100%" cellspacing="1" cellpadding="0" class="install_table">';
  99. $sRet .= '
  100. <tr class="head troubled">
  101. <td></td>
  102. <td class="center_aligned">' . _t('_sys_adm_installed') . '</td>
  103. <td class="center_aligned">' . _t('_sys_adm_cache_support') . '</td>
  104. </tr>';
  105. $aEngines = array ('File', 'Memcache', 'APC', 'XCache');
  106. foreach ($aEngines as $sEngine) {
  107. $oCacheObject = @bx_instance ('BxDolCache' . $sEngine);
  108. $sRet .= '
  109. <tr class="head troubled">
  110. <td class="left_aligned">' . $sEngine . '</td>
  111. <td class="center_aligned">' . (@$oCacheObject->isInstalled() ? '<font color="green">' . _t('_Yes') . '</font>' : '<font color="red">' . _t('_No') . '</font>') . '</td>
  112. <td class="center_aligned">' . (@$oCacheObject->isAvailable() ? '<font color="green">' . _t('_Yes') . '</font>' : '<font color="red">' . _t('_No') . '</font>') . '</td>
  113. </tr>';
  114. }
  115. $sRet .= '</table>';
  116. return $sRet;
  117. }
  118. function GenTabbedPage($isShowModules = false)
  119. {
  120. $sTitleC = _t('_adm_admtools_title');
  121. $sAuditC = _t('');
  122. $sPermissionsC = _t('');
  123. $sCacheEnginesC = _t('');
  124. $sAuditTab = $this->GenAuditPage();
  125. $sPermissionsTab = $this->GenPermTable($isShowModules);
  126. $sCacheEnginesTab = $this->GenCacheEnginesTable();
  127. $sBoxContent = <<<EOF
  128. <script type="text/javascript">
  129. <!--
  130. function switchAdmPage(oLink)
  131. {
  132. var sType = $(oLink).attr('id').replace('main_menu', '');
  133. var sName = '#page' + sType;
  134. $(oLink).parent('.notActive').hide().siblings('.notActive:hidden').show().siblings('.active').hide().siblings('#' + $(oLink).attr('id') + '-act').show();
  135. $(sName).siblings('div:visible').bx_anim('hide', 'fade', 'slow', function(){
  136. $(sName).bx_anim('show', 'fade', 'slow');
  137. });
  138. return false;
  139. }
  140. -->
  141. </script>
  142. <div class="boxContent" id="adm_pages">
  143. <div id="page0" class="visible">{$sAuditTab}</div>
  144. <div id="page1" class="hidden">{$sPermissionsTab}</div>
  145. <div id="page2" class="hidden">
  146. <iframe frameborder="0" width="100%" height="800" scrolling="auto" src="host_tools.php?get_phpinfo=true"></iframe>
  147. </div>
  148. <div id="page3" class="hidden">{$sCacheEnginesTab}</div>
  149. </div>
  150. EOF;
  151. $aTopItems = array(
  152. 'main_menu0' => array('href' => 'javascript:void(0)', 'onclick' => 'javascript:switchAdmPage(this)', 'title' => _t('_adm_admtools_Audit'), 'active' => 1),
  153. 'main_menu1' => array('href' => 'javascript:void(0)', 'onclick' => 'javascript:switchAdmPage(this)', 'title' => _t('_adm_admtools_Permissions'), 'active' => 0),
  154. 'main_menu2' => array('href' => 'javascript:void(0)', 'onclick' => 'javascript:switchAdmPage(this)', 'title' => _t('_adm_admtools_phpinfo'), 'active' => 0),
  155. );
  156. return DesignBoxAdmin($sTitleC, $sBoxContent, $aTopItems, '', 11);
  157. }
  158. function GenAuditPage()
  159. {
  160. $oAudit = new BxDolStudioToolsAudit();
  161. return $oAudit->generate();
  162. }
  163. protected function _getHtmlPermissionRow($s, $r)
  164. {
  165. $sAwaitedPerm = BX_DOL_PERM_EXE == $r['type'] ? _t('_adm_admtools_Executable') : _t('_adm_admtools_Writable');
  166. $sResultPerm = $sAwaitedPerm;
  167. $sPerm = $this->getPermissions($s);
  168. $sClass = 'bx-permissions-ok';
  169. if (BX_DOL_PERM_FAIL == $r['res']) {
  170. $sClass = 'bx-permissions-wrong';
  171. if (false === $sPerm)
  172. $sResultPerm = _t('_adm_admtools_Not_Exists');
  173. else
  174. $sResultPerm = BX_DOL_PERM_EXE == $r['type'] ? _t('_adm_admtools_Non_Executable') : _t('_adm_admtools_Non_Writable');
  175. }
  176. return <<<EOF
  177. <tr class="bx-def-color-bg-hl-even">
  178. <td class="bx-def-padding-thd">{$s}</td>
  179. <td class="bx-def-padding-thd {$sClass}">{$sResultPerm}</td>
  180. <td class="bx-def-padding-thd">{$sAwaitedPerm}</td>
  181. </tr>
  182. EOF;
  183. }
  184. protected function _getHtmlPermissionTable($sRows)
  185. {
  186. $sDirsC = _t('_adm_admtools_Path');
  187. $sCurrentLevelC = _t('_adm_admtools_Current_level');
  188. $sDesiredLevelC = _t('_adm_admtools_Desired_level');
  189. return <<<EOF
  190. <table width="100%" class="bx-permissions-table">
  191. <thead class="bx-def-border-bottom bx-def-border-top">
  192. <tr>
  193. <td class="bx-def-padding-thd bx-def-font-h3">{$sDirsC}</td>
  194. <td class="bx-def-padding-thd bx-def-font-h3">{$sCurrentLevelC}</td>
  195. <td class="bx-def-padding-thd bx-def-font-h3">{$sDesiredLevelC}</td>
  196. </tr>
  197. </thead>
  198. <tbody>
  199. {$sRows}
  200. </tbody>
  201. </table>
  202. EOF;
  203. }
  204. protected function _getFileType($s)
  205. {
  206. $sType = BX_DOL_PERM_FILE;
  207. if (is_dir($this->sRootPath . $s))
  208. $sType = BX_DOL_PERM_DIR;
  209. elseif (substr($s, -4) === '.exe')
  210. $sType = BX_DOL_PERM_EXE;
  211. return $sType;
  212. }
  213. protected function _checkPermissionsModules(&$aMessages)
  214. {
  215. $bRet = true;
  216. $oDbModules = new BxDolModuleDb();
  217. $aModules = $oDbModules->getModules();
  218. foreach ($aModules as $a) {
  219. if (empty($a['path']) || !include(BX_DIRECTORY_PATH_MODULES . $a['path'] . 'install/config.php'))
  220. continue;
  221. if (empty($aConfig['install_permissions']) || !is_array($aConfig['install_permissions']['writable']))
  222. continue;
  223. foreach ($aConfig['install_permissions']['writable'] as $sPath) {
  224. $s = basename(BX_DIRECTORY_PATH_MODULES) . '/' . $a['path'] . $sPath;
  225. $sType = $this->_getFileType($s);
  226. $isOk = BX_DOL_PERM_EXE ? $this->isExecutable($s) : $this->isWritable($s);
  227. $aMessages[$s] = array ('res' => $isOk ? BX_DOL_PERM_OK : BX_DOL_PERM_FAIL, 'type' => $sType);
  228. if (!$isOk && $bRet)
  229. $bRet = false;
  230. }
  231. }
  232. return $bRet;
  233. }
  234. }
  235. /** @} */