functions.app_passwd.inc.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. function app_passwd($_action, $_data = null) {
  3. global $pdo;
  4. global $lang;
  5. $_data_log = $_data;
  6. if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
  7. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
  8. $_SESSION['return'][] = array(
  9. 'type' => 'danger',
  10. 'log' => array(__FUNCTION__, $_action, $_data_log),
  11. 'msg' => 'access_denied'
  12. );
  13. return false;
  14. }
  15. else {
  16. $username = $_data['username'];
  17. }
  18. }
  19. else {
  20. $username = $_SESSION['mailcow_cc_username'];
  21. }
  22. switch ($_action) {
  23. case 'add':
  24. $app_name = trim($_data['app_name']);
  25. $password = $_data['app_passwd'];
  26. $password2 = $_data['app_passwd2'];
  27. $active = intval($_data['active']);
  28. $domain = mailbox('get', 'mailbox_details', $username)['domain'];
  29. if (empty($domain)) {
  30. $_SESSION['return'][] = array(
  31. 'type' => 'danger',
  32. 'log' => array(__FUNCTION__, $_action, $_data_log),
  33. 'msg' => 'access_denied'
  34. );
  35. return false;
  36. }
  37. if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
  38. $_SESSION['return'][] = array(
  39. 'type' => 'danger',
  40. 'log' => array(__FUNCTION__, $_action, $_data_log),
  41. 'msg' => 'password_complexity'
  42. );
  43. return false;
  44. }
  45. if ($password != $password2) {
  46. $_SESSION['return'][] = array(
  47. 'type' => 'danger',
  48. 'log' => array(__FUNCTION__, $_action, $_data_log),
  49. 'msg' => 'password_mismatch'
  50. );
  51. return false;
  52. }
  53. $password_hashed = hash_password($password);
  54. if (empty($app_name)) {
  55. $_SESSION['return'][] = array(
  56. 'type' => 'danger',
  57. 'log' => array(__FUNCTION__, $_action, $_data_log),
  58. 'msg' => 'app_name_empty'
  59. );
  60. return false;
  61. }
  62. $stmt = $pdo->prepare("INSERT INTO `app_passwd` (`name`, `mailbox`, `domain`, `password`, `active`)
  63. VALUES (:app_name, :mailbox, :domain, :password, :active)");
  64. $stmt->execute(array(
  65. ':app_name' => $app_name,
  66. ':mailbox' => $username,
  67. ':domain' => $domain,
  68. ':password' => $password_hashed,
  69. ':active' => $active
  70. ));
  71. $_SESSION['return'][] = array(
  72. 'type' => 'success',
  73. 'log' => array(__FUNCTION__, $_action, $_data_log),
  74. 'msg' => 'app_passwd_added'
  75. );
  76. break;
  77. case 'edit':
  78. $ids = (array)$_data['id'];
  79. foreach ($ids as $id) {
  80. $is_now = app_passwd('details', $id);
  81. if (!empty($is_now)) {
  82. $app_name = (!empty($_data['app_name'])) ? $_data['app_name'] : $is_now['name'];
  83. $password = (!empty($_data['password'])) ? $_data['password'] : null;
  84. $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
  85. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
  86. }
  87. else {
  88. $_SESSION['return'][] = array(
  89. 'type' => 'danger',
  90. 'log' => array(__FUNCTION__, $_action, $_data_log),
  91. 'msg' => array('app_passwd_id_invalid', $id)
  92. );
  93. continue;
  94. }
  95. $app_name = trim($app_name);
  96. if (!empty($password) && !empty($password2)) {
  97. if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
  98. $_SESSION['return'][] = array(
  99. 'type' => 'danger',
  100. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  101. 'msg' => 'password_complexity'
  102. );
  103. continue;
  104. }
  105. if ($password != $password2) {
  106. $_SESSION['return'][] = array(
  107. 'type' => 'danger',
  108. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  109. 'msg' => 'password_mismatch'
  110. );
  111. continue;
  112. }
  113. $password_hashed = hash_password($password);
  114. $stmt = $pdo->prepare("UPDATE `app_passwd` SET
  115. `password` = :password_hashed
  116. WHERE `mailbox` = :username AND `id` = :id");
  117. $stmt->execute(array(
  118. ':password_hashed' => $password_hashed,
  119. ':username' => $username,
  120. ':id' => $id
  121. ));
  122. }
  123. $stmt = $pdo->prepare("UPDATE `app_passwd` SET
  124. `name` = :app_name,
  125. `mailbox` = :username,
  126. `active` = :active
  127. WHERE `id` = :id");
  128. $stmt->execute(array(
  129. ':app_name' => $app_name,
  130. ':username' => $username,
  131. ':active' => $active,
  132. ':id' => $id
  133. ));
  134. $_SESSION['return'][] = array(
  135. 'type' => 'success',
  136. 'log' => array(__FUNCTION__, $_action, $_data_log),
  137. 'msg' => array('object_modified', htmlspecialchars($ids))
  138. );
  139. }
  140. break;
  141. case 'delete':
  142. $ids = (array)$_data['id'];
  143. foreach ($ids as $id) {
  144. $stmt = $pdo->prepare("SELECT `mailbox` FROM `app_passwd` WHERE `id` = :id");
  145. $stmt->execute(array(':id' => $id));
  146. $mailbox = $stmt->fetch(PDO::FETCH_ASSOC)['mailbox'];
  147. if (empty($mailbox)) {
  148. $_SESSION['return'][] = array(
  149. 'type' => 'danger',
  150. 'log' => array(__FUNCTION__, $_action, $_data_log),
  151. 'msg' => 'app_passwd_id_invalid'
  152. );
  153. return false;
  154. }
  155. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $mailbox)) {
  156. $_SESSION['return'][] = array(
  157. 'type' => 'danger',
  158. 'log' => array(__FUNCTION__, $_action, $_data_log),
  159. 'msg' => 'access_denied'
  160. );
  161. return false;
  162. }
  163. $stmt = $pdo->prepare("DELETE FROM `app_passwd` WHERE `id`= :id");
  164. $stmt->execute(array(':id' => $id));
  165. $_SESSION['return'][] = array(
  166. 'type' => 'success',
  167. 'log' => array(__FUNCTION__, $_action, $_data_log),
  168. 'msg' => array('app_passwd_removed', htmlspecialchars($id))
  169. );
  170. }
  171. break;
  172. case 'get':
  173. $app_passwds = array();
  174. $stmt = $pdo->prepare("SELECT `id`, `name` FROM `app_passwd` WHERE `mailbox` = :username");
  175. $stmt->execute(array(':username' => $username));
  176. $app_passwds = $stmt->fetchAll(PDO::FETCH_ASSOC);
  177. return $app_passwds;
  178. break;
  179. case 'details':
  180. $app_passwd_data = array();
  181. $stmt = $pdo->prepare("SELECT `id`,
  182. `name`,
  183. `mailbox`,
  184. `domain`,
  185. `created`,
  186. `modified`,
  187. `active` AS `active_int`,
  188. CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`
  189. FROM `app_passwd`
  190. WHERE `id` = :id");
  191. $stmt->execute(array(':id' => $_data['id']));
  192. $app_passwd_data = $stmt->fetch(PDO::FETCH_ASSOC);
  193. if (empty($app_passwd_data)) {
  194. return false;
  195. }
  196. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $app_passwd_data['mailbox'])) {
  197. $app_passwd_data = array();
  198. return false;
  199. }
  200. return $app_passwd_data;
  201. break;
  202. }
  203. }