users.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. $ldapMgrCfg = parse_ini_file(dirname(__FILE__) . '/ldapmgr.ini');
  3. $apiUrl = $ldapMgrCfg['API_URL'];
  4. $apiKey = $ldapMgrCfg['API_KEY'];
  5. class remoteLdapBase {
  6. protected $apiUrl = '';
  7. protected $apiKey = '';
  8. protected $urlInterface = '';
  9. const USER_CREATE = '/create_user';
  10. const USER_GROUP = '/add_member';
  11. const USER_GROUP_DEL = '/remove_member';
  12. const USER_DEL = '/remove_user';
  13. const USER_PASSWD = '/change_passwd';
  14. public function __construct($apiUrl, $apiKey) {
  15. $this->apiUrl = $apiUrl;
  16. $this->apiKey = $apiKey;
  17. if ((!empty($this->apiKey)) AND ( !empty($this->apiUrl))) {
  18. $this->urlInterface = $this->apiUrl . '?module=remoteapi&key=' . $this->apiKey . '&action=ldapmgr&param=';
  19. $this->getQueueData();
  20. } else {
  21. die('Error reading config file' . "\n");
  22. }
  23. }
  24. protected function log($event, $data = '') {
  25. $filename = dirname(__FILE__) . '/ldapmgr.log';
  26. if (file_exists($filename)) {
  27. if (is_writable($filename)) {
  28. $curtime = date("Y-m-d H:i:s");
  29. file_put_contents($filename, $curtime . ' ' . $event . "\n", FILE_APPEND);
  30. if (!empty($data)) {
  31. file_put_contents($filename, $data . "\n", FILE_APPEND);
  32. }
  33. }
  34. }
  35. }
  36. protected function getQueueData() {
  37. $result = array();
  38. $rawData = file_get_contents($this->urlInterface . 'queue');
  39. if (!empty($rawData)) {
  40. $tmpArr = json_decode($rawData, true);
  41. if (!empty($tmpArr)) {
  42. foreach ($tmpArr as $io => $each) {
  43. switch ($each['task']) {
  44. case 'userdelete':
  45. $delResult = shell_exec(dirname(__FILE__) . self::USER_DEL . ' ' . $each['param']);
  46. $this->log('USERDELETE: ' . $each['param'], $delResult);
  47. break;
  48. case 'usercreate':
  49. $createResult = shell_exec(dirname(__FILE__) . self::USER_CREATE . ' ' . $each['param'] . ' fakepassword');
  50. $this->log('USERCREATE: ' . $each['param'], $createResult);
  51. break;
  52. case 'userpassword':
  53. $passParam = json_decode($each['param'], true);
  54. $passResult = shell_exec(dirname(__FILE__) . self::USER_PASSWD . ' ' . $passParam['login'] . ' ' . $passParam['password']);
  55. $this->log('USERPASS: ' . $passParam['login'], $passResult);
  56. break;
  57. case 'usergroups':
  58. $groupParam = json_decode($each['param'], true);
  59. $userLogin = $groupParam['login'];
  60. $userGroups = $groupParam['groups'];
  61. if (!empty($userGroups)) {
  62. foreach ($userGroups as $ia => $eachGroup) {
  63. $groupResult = shell_exec(dirname(__FILE__) . self::USER_GROUP . ' ' . $userLogin . ' ' . $eachGroup);
  64. $this->log('USERGROUP: ' . $userLogin . '->' . $eachGroup, $groupResult);
  65. }
  66. }
  67. break;
  68. case 'usergroupsremove':
  69. $groupParam = json_decode($each['param'], true);
  70. $userLogin = $groupParam['login'];
  71. $userGroups = $groupParam['groups'];
  72. if (!empty($userGroups)) {
  73. foreach ($userGroups as $ia => $eachGroup) {
  74. $groupDelResult = shell_exec(dirname(__FILE__) . self::USER_GROUP_DEL . ' ' . $userLogin . ' ' . $eachGroup);
  75. $this->log('USERGROUPDEL: ' . $userLogin . '->' . $eachGroup, $groupDelResult);
  76. }
  77. }
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. return ($result);
  84. }
  85. }
  86. $remoteBase = new remoteLdapBase($apiUrl, $apiKey);
  87. ?>