PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/main/public/access_edit.php

https://gitlab.com/alexprowars/bitrix
PHP | 520 lines | 377 code | 116 blank | 27 comment | 100 complexity | 8b2763dc8244f83e54e02b6b994fea90 MD5 | raw file
  1. <?
  2. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
  3. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_js.php");
  4. IncludeModuleLangFile(__FILE__);
  5. $popupWindow = new CJSPopup('', array("SUFFIX"=>($_GET['subdialog'] == 'Y'? 'subdialog':'')));
  6. if (IsModuleInstalled("fileman"))
  7. {
  8. if (!$USER->CanDoOperation('fileman_edit_existent_folders') && !$USER->CanDoOperation('fileman_admin_folders'))
  9. $popupWindow->ShowError(GetMessage("FOLDER_EDIT_ACCESS_DENIED"));
  10. }
  11. $io = CBXVirtualIo::GetInstance();
  12. //Folder path
  13. $path = "/";
  14. if (isset($_REQUEST["path"]) && $_REQUEST["path"] <> '')
  15. $path = $io->CombinePath("/", $_REQUEST["path"]);
  16. //Site ID
  17. $site = SITE_ID;
  18. if (isset($_REQUEST["site"]) && $_REQUEST["site"] <> '')
  19. {
  20. $obSite = CSite::GetByID($_REQUEST["site"]);
  21. if ($arSite = $obSite->Fetch())
  22. $site = $_REQUEST["site"];
  23. }
  24. //Document Root
  25. $documentRoot = CSite::GetSiteDocRoot($site);
  26. //Check path permissions
  27. if (!$io->FileExists($documentRoot.$path) && !$io->DirectoryExists($documentRoot.$path))
  28. $popupWindow->ShowError(GetMessage("ACCESS_EDIT_FILE_NOT_FOUND")." (".htmlspecialcharsbx($path).")");
  29. elseif (!$USER->CanDoFileOperation('fm_edit_existent_folder', array($site, $path)))
  30. $popupWindow->ShowError(GetMessage("FOLDER_EDIT_ACCESS_DENIED"));
  31. elseif (!$USER->CanDoFileOperation('fm_edit_permission', array($site, $path)))
  32. $popupWindow->ShowError(GetMessage("EDIT_ACCESS_TO_DENIED")." \"".htmlspecialcharsbx($path)."\"");
  33. //Lang
  34. if (!isset($_REQUEST["lang"]) || $_REQUEST["lang"] == '')
  35. $lang = LANGUAGE_ID;
  36. //BackUrl
  37. $back_url = (isset($_REQUEST["back_url"]) ? $_REQUEST["back_url"] : "");
  38. //Is a folder?
  39. $isFolder = $io->DirectoryExists($documentRoot.$path);
  40. //Get only used user group from .access.php file
  41. $arUserGroupsID = array("*");
  42. $assignFileName = "";
  43. $assignFolderName = "";
  44. $currentPath = $path;
  45. while(true)
  46. {
  47. //Cut / from the end
  48. $currentPath = rtrim($currentPath, "/");
  49. if ($currentPath == '')
  50. {
  51. $accessFile = "/.access.php";
  52. $name = "/";
  53. }
  54. else
  55. {
  56. //Find file or folder name
  57. $position = mb_strrpos($currentPath, "/");
  58. if ($position === false)
  59. break;
  60. $name = mb_substr($currentPath, $position + 1);
  61. $name = TrimUnsafe($name); //security fix: under Windows "my." == "my"
  62. //Find parent folder
  63. $currentPath = mb_substr($currentPath, 0, $position + 1);
  64. $accessFile = $currentPath.".access.php";
  65. }
  66. $PERM = Array();
  67. if ($io->FileExists($documentRoot.$accessFile))
  68. include($io->GetPhysicalName($documentRoot.$accessFile));
  69. if ($assignFileName == "")
  70. {
  71. $assignFileName = $name;
  72. $assignFolderName = ($name == "/" ? "/" : $currentPath);
  73. }
  74. if (isset($PERM[$name]) && is_array($PERM[$name]))
  75. $arUserGroupsID = array_merge($arUserGroupsID, array_keys($PERM[$name]));
  76. if ($currentPath == '')
  77. break;
  78. }
  79. foreach($arUserGroupsID as $key=>$val)
  80. if(preg_match('/^[0-9]+$/', $val))
  81. $arUserGroupsID[$key] = "G".$val;
  82. $arUserGroupsID = array_unique($arUserGroupsID);
  83. //Get all tasks
  84. $arPermTypes = array();
  85. $obTask = CTask::GetList(array("LETTER" => "ASC"), array("MODULE_ID" => "main", "BINDING" => "file"));
  86. while($arTask = $obTask->Fetch())
  87. $arPermTypes[$arTask["ID"]] = CTask::GetLangTitle($arTask["NAME"], $arTask["MODULE_ID"]);
  88. //Current file/folder permissions
  89. $currentPermission = array();
  90. if($io->FileExists($documentRoot.$assignFolderName.".access.php"))
  91. {
  92. $PERM = array();
  93. include($io->GetPhysicalName($documentRoot.$assignFolderName.".access.php"));
  94. foreach($PERM as $file => $arPerm)
  95. foreach($arPerm as $code => $permission)
  96. $currentPermission[$file][(preg_match('/^[0-9]+$/', $code)? "G".$code : $code)] = $permission;
  97. }
  98. $strWarning = "";
  99. //Save permissions
  100. if ($_SERVER["REQUEST_METHOD"] == "POST" && !check_bitrix_sessid())
  101. {
  102. CUtil::JSPostUnescape();
  103. $strWarning = GetMessage("MAIN_SESSION_EXPIRED");
  104. }
  105. elseif ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_REQUEST["save"]))
  106. {
  107. CUtil::JSPostUnescape();
  108. $arSavePermission = array();
  109. if($_POST["REMOVE_PERMISSIONS"] == "Y")
  110. {
  111. if($path != "/")
  112. {
  113. $APPLICATION->RemoveFileAccessPermission(array($site, $path));
  114. if ($e = $APPLICATION->GetException())
  115. $strWarning = $e->msg;
  116. }
  117. }
  118. else
  119. {
  120. if (isset($_POST["PERMISSION"]) && is_array($_POST["PERMISSION"]))
  121. {
  122. if (isset($currentPermission[$assignFileName]) && is_array($currentPermission[$assignFileName]))
  123. $arSavePermission = $currentPermission[$assignFileName];
  124. $isAdmin = $USER->IsAdmin();
  125. foreach ($_POST["PERMISSION"] as $groupID => $taskID)
  126. {
  127. if($groupID !== "*")
  128. {
  129. $groupID = trim($groupID);
  130. if($groupID == '')
  131. continue;
  132. }
  133. elseif (!$isAdmin)
  134. continue;
  135. // if not set task - delete permission
  136. $taskID = intval($taskID);
  137. if ($taskID <= 0)
  138. {
  139. unset($arSavePermission[$groupID]);
  140. continue;
  141. }
  142. $obTask = CTask::GetById($taskID);
  143. if ( ($arTask = $obTask->Fetch()) && $arTask["LETTER"] && $arTask["SYS"] == "Y")
  144. $permLetter = $arTask["LETTER"];
  145. else
  146. $permLetter = "T_".$taskID;
  147. $arSavePermission[$groupID] = $permLetter;
  148. }
  149. }
  150. $APPLICATION->SetFileAccessPermission(array($site, $path), $arSavePermission);
  151. if ($e = $APPLICATION->GetException())
  152. $strWarning = $e->msg;
  153. }
  154. //Close window
  155. if ($strWarning == "")
  156. {
  157. $popupWindow->Close($bReload=($_GET['subdialog'] <> 'Y'), $back_url);
  158. die();
  159. }
  160. }
  161. echo CJSCore::Init(array('access'), true);
  162. //HTML output
  163. if ($isFolder)
  164. $popupWindow->ShowTitlebar(GetMessage("EDIT_ACCESS_TO_FOLDER"));
  165. else
  166. $popupWindow->ShowTitlebar(GetMessage("EDIT_ACCESS_TO_FILE"));
  167. $popupWindow->StartDescription($isFolder ? "bx-access-folder" : "bx-access-page");
  168. if ($strWarning != "")
  169. $popupWindow->ShowValidationError($strWarning);
  170. ?>
  171. <p><b><?=($isFolder ? GetMessage("EDIT_ACCESS_TO_FOLDER") : GetMessage("EDIT_ACCESS_TO_FILE"))?></b> <?=htmlspecialcharsbx($path);?></p>
  172. <?
  173. $popupWindow->EndDescription();
  174. $popupWindow->StartContent();
  175. ?>
  176. <table class="bx-width100" id="bx_permission_table">
  177. <tr>
  178. <td width="45%"><b><?=GetMessage("EDIT_ACCESS_USER_GROUP")?></b></td>
  179. <td><b><?=GetMessage("EDIT_ACCESS_PERMISSION")?></b> </td>
  180. </tr>
  181. <tr class="empty">
  182. <td colspan="2"></td>
  183. </tr>
  184. <?
  185. //names for access codes
  186. $access = new CAccess();
  187. $arNames = $access->GetNames($arUserGroupsID, true);
  188. //sort codes by sorted names
  189. $positions = array_flip(array_keys($arNames));
  190. usort($arUserGroupsID,
  191. function($a, $b) use ($positions)
  192. {
  193. if(!isset($positions[$a]) && !isset($positions[$b])) return 0;
  194. if(!isset($positions[$a])) return 1;
  195. if(!isset($positions[$b])) return -1;
  196. return ($positions[$a] > $positions[$b]? 1 : -1);
  197. }
  198. );
  199. //Javascript variables
  200. $jsTaskArray = "window.BXTaskArray = {'0':'".CUtil::JSEscape(GetMessage("EDIT_ACCESS_SET_INHERIT"))."'";
  201. foreach ($arPermTypes as $taskID => $taskTitle)
  202. $jsTaskArray .= ",'".$taskID."':'".CUtil::JSEscape($taskTitle)."'";
  203. $jsTaskArray .= "};";
  204. $jsInheritPerm = "";
  205. $jsInheritPermID = "var jsInheritPermIDs = [";
  206. $bWasCurrentPerm = false;
  207. foreach($arUserGroupsID as $access_code):
  208. //Restore post value if error occured
  209. $errorOccured = ($strWarning != "" && isset($_POST["PERMISSION"]) && is_array($_POST["PERMISSION"]) && array_key_exists($access_code, $_POST["PERMISSION"]));
  210. //Inherit Task
  211. list ($inheritTaskID) = $APPLICATION->GetFileAccessPermission(Array($site, $assignFolderName), Array($access_code), true);
  212. if (!array_key_exists($inheritTaskID, $arPermTypes))
  213. {
  214. if ($access_code == "*")
  215. $inheritTaskID = CTask::GetIdByLetter("D", "main", "file");
  216. else
  217. continue;
  218. }
  219. //Current permission
  220. $currentPerm = false;
  221. if ($errorOccured)
  222. {
  223. //Restore post value if error occured
  224. $currentPerm = intval($_POST["PERMISSION"][$access_code]);
  225. }
  226. elseif (isset($currentPermission[$assignFileName]) && isset($currentPermission[$assignFileName][$access_code]))
  227. {
  228. $permLetter = $currentPermission[$assignFileName][$access_code];
  229. if (mb_substr($permLetter, 0, 2) == "T_")
  230. {
  231. $currentPerm = intval(mb_substr($permLetter, 2));
  232. if (!array_key_exists($currentPerm, $arPermTypes))
  233. $currentPerm = false;
  234. }
  235. else
  236. $currentPerm = CTask::GetIdByLetter($permLetter, "main", "file");
  237. }
  238. if ($currentPerm === false && $access_code == "*" && $path == "/")
  239. $currentPerm = $inheritTaskID;
  240. if ($access_code == "*")
  241. $jsInheritPerm = $inheritTaskID;
  242. $permissionID = $access_code."_".intval($currentPerm)."_".intval($inheritTaskID);?>
  243. <tr>
  244. <td><?=(
  245. $access_code == "*" ?
  246. GetMessage("EDIT_ACCESS_ALL_GROUPS")
  247. : ($arNames[$access_code]["provider"] <> '' ? '<b>'.htmlspecialcharsbx($arNames[$access_code]["provider"]).': </b> ' : '')
  248. . htmlspecialcharsbx($arNames[$access_code]["name"])
  249. )?></td>
  250. <td>
  251. <?if ($currentPerm === false && $path != "/"): //Inherit permission
  252. $jsInheritPermID .= ",'".$permissionID."'";
  253. ?>
  254. <div id="bx_permission_view_<?=$permissionID?>" onclick="BXEditPermission('<?=$permissionID?>')" class="edit-field" style="width:90%;">
  255. <?=GetMessage("EDIT_ACCESS_SET_INHERITED")." &quot;".htmlspecialcharsEx($arPermTypes[$inheritTaskID])."&quot;"?>
  256. </div>
  257. <div id="bx_permission_edit_<?=$permissionID?>" style="display:none;"></div>
  258. <?
  259. else: //Current permission
  260. $bWasCurrentPerm = true;
  261. ?>
  262. <select name="PERMISSION[<?=$access_code?>]" style="width:90%;" id="bx_task_list_<?=$permissionID?>">
  263. <?if ($path == "/"):?>
  264. <option value="0"><?=GetMessage("EDIT_ACCESS_NOT_SET")?></option>
  265. <?else:?>
  266. <option value="0"><?=GetMessage("EDIT_ACCESS_SET_INHERIT")." &quot;".htmlspecialcharsEx($arPermTypes[$inheritTaskID])."&quot;"?></option>
  267. <?endif?>
  268. <?foreach ($arPermTypes as $taskID => $taskTitle):?>
  269. <option value="<?=$taskID?>"<?if ($currentPerm == $taskID):?> selected="selected"<?endif?>><?=htmlspecialcharsEx($taskTitle);?></option>
  270. <?endforeach?>
  271. </select>
  272. <?endif?>
  273. </td>
  274. </tr>
  275. <?
  276. endforeach;
  277. $jsInheritPermID .= "];";
  278. ?>
  279. </table>
  280. <p><a href="javascript:void(0)" onclick="BX.Access.ShowForm({callback:BXAddNewPermission})"><?=GetMessage("EDIT_ACCESS_ADD_PERMISSION")?></a></p>
  281. <?if($bWasCurrentPerm && $path != "/"):?>
  282. <p><b><a href="javascript:void(0)" onclick="BXClearPermission()"><?=($isFolder? GetMessage("EDIT_ACCESS_REMOVE_PERM"):GetMessage("EDIT_ACCESS_REMOVE_PERM_FILE"))?></a></b></p>
  283. <input type="hidden" name="REMOVE_PERMISSIONS" id="REMOVE_PERMISSIONS" value="">
  284. <?endif?>
  285. <input type="hidden" name="save" value="Y" />
  286. <?
  287. $popupWindow->EndContent();
  288. $popupWindow->ShowStandardButtons();
  289. $arSel = array();
  290. foreach($arUserGroupsID as $code)
  291. $arSel[$code] = true;
  292. ?>
  293. <script>
  294. BX.Access.Init();
  295. BX.Access.SetSelected(<?=CUtil::PhpToJSObject($arSel)?>);
  296. <?=$jsTaskArray?>
  297. window.BXAddNewPermission = function(arRights)
  298. {
  299. var table = document.getElementById("bx_permission_table");
  300. for(var provider in arRights)
  301. {
  302. for(var id in arRights[provider])
  303. {
  304. //Create new row
  305. var tableRow = table.insertRow(table.rows.length);
  306. var groupTD = tableRow.insertCell(0);
  307. var currentTD = tableRow.insertCell(1);
  308. var pr = BX.Access.GetProviderName(provider);
  309. groupTD.innerHTML = (pr? '<b>'+BX.util.htmlspecialchars(pr)+':</b> ':'')+BX.util.htmlspecialchars(arRights[provider][id].name);
  310. //Insert Task Select
  311. var permissionID = Math.round(Math.random() * 100000);
  312. var taskSelect = BXCreateTaskList(permissionID, 0, 0, id);
  313. taskSelect.onblur = "";
  314. currentTD.appendChild(taskSelect);
  315. }
  316. }
  317. return false;
  318. };
  319. window.BXCreateTaskList = function(permissionID, currentPermission, inheritPermission, userGroupID)
  320. {
  321. var select = document.createElement("SELECT");
  322. select.name = "PERMISSION["+userGroupID+"]";
  323. select.style.width = "90%";
  324. select.onblur = function(){BXBlurEditPermission(select, permissionID)};
  325. select.id = "bx_task_list_" + permissionID;
  326. //For IE 5.0
  327. var selectDocument = select.ownerDocument;
  328. if (!selectDocument)
  329. selectDocument = select.document;
  330. var selectedIndex = 0;
  331. <?if ($path == "/"):?>
  332. window.BXTaskArray["0"] = "<?=CUtil::JSEscape(GetMessage("EDIT_ACCESS_NOT_SET"))?>";
  333. <?else:?>
  334. window.BXTaskArray["0"] = "<?=CUtil::JSEscape(GetMessage("EDIT_ACCESS_SET_INHERIT"))?>" + " \"" + window.BXTaskArray[(inheritPermission == 0 ? <?=intval($jsInheritPerm)?> : inheritPermission)] + "\"";
  335. <?endif?>
  336. for(var taskID in BXTaskArray)
  337. {
  338. var option = selectDocument.createElement("OPTION");
  339. option.text = window.BXTaskArray[taskID];
  340. option.value = taskID;
  341. select.options.add(option);
  342. if (taskID == currentPermission)
  343. selectedIndex = select.options.length - 1;
  344. }
  345. select.selectedIndex = selectedIndex;
  346. return select;
  347. };
  348. window.BXBlurEditPermission = function(select, permissionID)
  349. {
  350. var viewPermission = document.getElementById("bx_permission_view_" + permissionID);
  351. var setPermission = select.options[select.selectedIndex].value;
  352. var arPermID = permissionID.split("_");
  353. var userGroupID = arPermID[0];
  354. var currentPermission = arPermID[1];
  355. if (setPermission == currentPermission)
  356. {
  357. var editPermission = document.getElementById("bx_permission_edit_" + permissionID);
  358. viewPermission.style.display = "block";
  359. editPermission.style.display = "none";
  360. while (editPermission.firstChild)
  361. editPermission.removeChild(editPermission.firstChild);
  362. }
  363. };
  364. window.BXEditPermission = function(permissionID)
  365. {
  366. if (document.getElementById("bx_task_list_" + permissionID))
  367. return;
  368. var arPermID = permissionID.split("_"); //Format permissionID: UserGroup_CurrentPermission_InheritPermission
  369. var userGroupID = arPermID[0];
  370. var currentPermission = arPermID[1];
  371. var inheritPermission = arPermID[2];
  372. if (userGroupID == "0")
  373. userGroupID = "*";
  374. var editPermission = document.getElementById("bx_permission_edit_" + permissionID);
  375. var viewPermission = document.getElementById("bx_permission_view_" + permissionID);
  376. editPermission.style.display = "block";
  377. viewPermission.style.display = "none";
  378. var taskSelect = BXCreateTaskList(permissionID, currentPermission, inheritPermission, userGroupID);
  379. editPermission.appendChild(taskSelect);
  380. taskSelect.focus();
  381. };
  382. window.BXCreateAccessHint = function()
  383. {
  384. var table = document.getElementById("bx_permission_table");
  385. var tableRow = table.rows[0];
  386. var groupTD = tableRow.cells[0];
  387. var currentTD = tableRow.cells[1];
  388. var oBXHint = new BXHint("<?=CUtil::JSEscape(GetMessage("EDIT_ACCESS_PERMISSION_INFO"))?>");
  389. currentTD.appendChild(oBXHint.oIcon);
  390. <?=$jsInheritPermID?>
  391. for (var index = 0; index < jsInheritPermIDs.length; index++)
  392. oBXHint = new BXHint("<?=CUtil::JSEscape(GetMessage("EDIT_ACCESS_SET_PERMISSION"))?>", document.getElementById("bx_permission_view_"+ jsInheritPermIDs[index]), {"width":200});
  393. };
  394. window.BXClearPermission = function()
  395. {
  396. if(confirm('<?=CUtil::JSEscape(GetMessage("EDIT_ACCESS_REMOVE_PERM_CONF"))?>'))
  397. {
  398. BX("REMOVE_PERMISSIONS").value = "Y";
  399. BX.WindowManager.Get().PostParameters();
  400. }
  401. };
  402. window.BXCreateAccessHint();
  403. </script>
  404. <?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin_js.php");?>