functions.mailbox.inc.php 199 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260
  1. <?php
  2. function mailbox($_action, $_type, $_data = null, $_extra = null) {
  3. global $pdo;
  4. global $redis;
  5. global $lang;
  6. global $MAILBOX_DEFAULT_ATTRIBUTES;
  7. $_data_log = $_data;
  8. !isset($_data_log['password']) ?: $_data_log['password'] = '*';
  9. !isset($_data_log['password2']) ?: $_data_log['password2'] = '*';
  10. switch ($_action) {
  11. case 'add':
  12. switch ($_type) {
  13. case 'time_limited_alias':
  14. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  15. $_SESSION['return'][] = array(
  16. 'type' => 'danger',
  17. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  18. 'msg' => 'access_denied'
  19. );
  20. return false;
  21. }
  22. if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
  23. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
  24. $_SESSION['return'][] = array(
  25. 'type' => 'danger',
  26. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  27. 'msg' => 'access_denied'
  28. );
  29. return false;
  30. }
  31. else {
  32. $username = $_data['username'];
  33. }
  34. }
  35. else {
  36. $username = $_SESSION['mailcow_cc_username'];
  37. }
  38. if (!is_numeric($_data["validity"]) || $_data["validity"] > 672) {
  39. $_SESSION['return'][] = array(
  40. 'type' => 'danger',
  41. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  42. 'msg' => 'validity_missing'
  43. );
  44. return false;
  45. }
  46. $domain = mailbox('get', 'mailbox_details', $username)['domain'];
  47. if (!is_valid_domain_name($domain)) {
  48. $_SESSION['return'][] = array(
  49. 'type' => 'danger',
  50. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  51. 'msg' => 'domain_invalid'
  52. );
  53. return false;
  54. }
  55. $validity = strtotime("+".$_data["validity"]." hour");
  56. $letters = 'abcefghijklmnopqrstuvwxyz1234567890';
  57. $random_name = substr(str_shuffle($letters), 0, 24);
  58. $stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `goto`, `validity`) VALUES
  59. (:address, :goto, :validity)");
  60. $stmt->execute(array(
  61. ':address' => $random_name . '@' . $domain,
  62. ':goto' => $username,
  63. ':validity' => $validity
  64. ));
  65. $_SESSION['return'][] = array(
  66. 'type' => 'success',
  67. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  68. 'msg' => array('mailbox_modified', $username)
  69. );
  70. break;
  71. case 'global_filter':
  72. if ($_SESSION['mailcow_cc_role'] != "admin") {
  73. $_SESSION['return'][] = array(
  74. 'type' => 'danger',
  75. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  76. 'msg' => 'access_denied'
  77. );
  78. return false;
  79. }
  80. $sieve = new Sieve\SieveParser();
  81. $script_data = $_data['script_data'];
  82. $script_data = str_replace("\r\n", "\n", $script_data); // windows -> unix
  83. $script_data = str_replace("\r", "\n", $script_data); // remaining -> unix
  84. $filter_type = $_data['filter_type'];
  85. try {
  86. $sieve->parse($script_data);
  87. }
  88. catch (Exception $e) {
  89. $_SESSION['return'][] = array(
  90. 'type' => 'danger',
  91. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  92. 'msg' => array('sieve_error', $e->getMessage())
  93. );
  94. return false;
  95. }
  96. if ($filter_type == 'prefilter') {
  97. try {
  98. if (file_exists('/global_sieve/before')) {
  99. $filter_handle = fopen('/global_sieve/before', 'w');
  100. if (!$filter_handle) {
  101. throw new Exception($lang['danger']['file_open_error']);
  102. }
  103. fwrite($filter_handle, $script_data);
  104. fclose($filter_handle);
  105. }
  106. $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
  107. if ($restart_response['type'] == "success") {
  108. $_SESSION['return'][] = array(
  109. 'type' => 'success',
  110. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  111. 'msg' => 'dovecot_restart_success'
  112. );
  113. }
  114. else {
  115. $_SESSION['return'][] = array(
  116. 'type' => 'warning',
  117. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  118. 'msg' => 'dovecot_restart_failed'
  119. );
  120. }
  121. }
  122. catch (Exception $e) {
  123. $_SESSION['return'][] = array(
  124. 'type' => 'danger',
  125. 'log' => array(__FUNCTION__, $_action, $_data_log),
  126. 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
  127. );
  128. return false;
  129. }
  130. }
  131. elseif ($filter_type == 'postfilter') {
  132. try {
  133. if (file_exists('/global_sieve/after')) {
  134. $filter_handle = fopen('/global_sieve/after', 'w');
  135. if (!$filter_handle) {
  136. throw new Exception($lang['danger']['file_open_error']);
  137. }
  138. fwrite($filter_handle, $script_data);
  139. fclose($filter_handle);
  140. }
  141. $restart_response = json_decode(docker('post', 'dovecot-mailcow', 'restart'), true);
  142. if ($restart_response['type'] == "success") {
  143. $_SESSION['return'][] = array(
  144. 'type' => 'success',
  145. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  146. 'msg' => 'dovecot_restart_success'
  147. );
  148. }
  149. else {
  150. $_SESSION['return'][] = array(
  151. 'type' => 'warning',
  152. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  153. 'msg' => 'dovecot_restart_failed'
  154. );
  155. }
  156. }
  157. catch (Exception $e) {
  158. $_SESSION['return'][] = array(
  159. 'type' => 'danger',
  160. 'log' => array(__FUNCTION__, $_action, $_data_log),
  161. 'msg' => array('global_filter_write_error', htmlspecialchars($e->getMessage()))
  162. );
  163. return false;
  164. }
  165. }
  166. else {
  167. $_SESSION['return'][] = array(
  168. 'type' => 'danger',
  169. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  170. 'msg' => 'invalid_filter_type'
  171. );
  172. return false;
  173. }
  174. $_SESSION['return'][] = array(
  175. 'type' => 'success',
  176. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  177. 'msg' => 'global_filter_written'
  178. );
  179. return true;
  180. case 'filter':
  181. $sieve = new Sieve\SieveParser();
  182. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  183. $_SESSION['return'][] = array(
  184. 'type' => 'danger',
  185. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  186. 'msg' => 'access_denied'
  187. );
  188. return false;
  189. }
  190. if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
  191. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
  192. $_SESSION['return'][] = array(
  193. 'type' => 'danger',
  194. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  195. 'msg' => 'access_denied'
  196. );
  197. return false;
  198. }
  199. else {
  200. $username = $_data['username'];
  201. }
  202. }
  203. elseif ($_SESSION['mailcow_cc_role'] == "user") {
  204. $username = $_SESSION['mailcow_cc_username'];
  205. }
  206. else {
  207. $_SESSION['return'][] = array(
  208. 'type' => 'danger',
  209. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  210. 'msg' => 'no_user_defined'
  211. );
  212. return false;
  213. }
  214. $active = intval($_data['active']);
  215. $script_data = $_data['script_data'];
  216. $script_desc = $_data['script_desc'];
  217. $filter_type = $_data['filter_type'];
  218. if (empty($script_data)) {
  219. $_SESSION['return'][] = array(
  220. 'type' => 'danger',
  221. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  222. 'msg' => 'script_empty'
  223. );
  224. return false;
  225. }
  226. try {
  227. $sieve->parse($script_data);
  228. }
  229. catch (Exception $e) {
  230. $_SESSION['return'][] = array(
  231. 'type' => 'danger',
  232. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  233. 'msg' => array('sieve_error', $e->getMessage())
  234. );
  235. return false;
  236. }
  237. if (empty($script_data) || empty($script_desc)) {
  238. $_SESSION['return'][] = array(
  239. 'type' => 'danger',
  240. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  241. 'msg' => 'value_missing'
  242. );
  243. return false;
  244. }
  245. if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
  246. $_SESSION['return'][] = array(
  247. 'type' => 'danger',
  248. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  249. 'msg' => 'filter_type'
  250. );
  251. return false;
  252. }
  253. if (!empty($active)) {
  254. $script_name = 'active';
  255. $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_name` = 'inactive' WHERE `username` = :username AND `filter_type` = :filter_type");
  256. $stmt->execute(array(
  257. ':username' => $username,
  258. ':filter_type' => $filter_type
  259. ));
  260. }
  261. else {
  262. $script_name = 'inactive';
  263. }
  264. $stmt = $pdo->prepare("INSERT INTO `sieve_filters` (`username`, `script_data`, `script_desc`, `script_name`, `filter_type`)
  265. VALUES (:username, :script_data, :script_desc, :script_name, :filter_type)");
  266. $stmt->execute(array(
  267. ':username' => $username,
  268. ':script_data' => $script_data,
  269. ':script_desc' => $script_desc,
  270. ':script_name' => $script_name,
  271. ':filter_type' => $filter_type
  272. ));
  273. $_SESSION['return'][] = array(
  274. 'type' => 'success',
  275. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  276. 'msg' => array('mailbox_modified', $username)
  277. );
  278. break;
  279. case 'syncjob':
  280. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  281. $_SESSION['return'][] = array(
  282. 'type' => 'danger',
  283. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  284. 'msg' => 'access_denied'
  285. );
  286. return false;
  287. }
  288. if (isset($_data['username']) && filter_var($_data['username'], FILTER_VALIDATE_EMAIL)) {
  289. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data['username'])) {
  290. $_SESSION['return'][] = array(
  291. 'type' => 'danger',
  292. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  293. 'msg' => 'access_denied'
  294. );
  295. return false;
  296. }
  297. else {
  298. $username = $_data['username'];
  299. }
  300. }
  301. elseif ($_SESSION['mailcow_cc_role'] == "user") {
  302. $username = $_SESSION['mailcow_cc_username'];
  303. }
  304. else {
  305. $_SESSION['return'][] = array(
  306. 'type' => 'danger',
  307. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  308. 'msg' => 'no_user_defined'
  309. );
  310. return false;
  311. }
  312. $active = intval($_data['active']);
  313. $subscribeall = intval($_data['subscribeall']);
  314. $delete2duplicates = intval($_data['delete2duplicates']);
  315. $delete1 = intval($_data['delete1']);
  316. $delete2 = intval($_data['delete2']);
  317. $timeout1 = intval($_data['timeout1']);
  318. $timeout2 = intval($_data['timeout2']);
  319. $skipcrossduplicates = intval($_data['skipcrossduplicates']);
  320. $automap = intval($_data['automap']);
  321. $port1 = $_data['port1'];
  322. $host1 = strtolower($_data['host1']);
  323. $password1 = $_data['password1'];
  324. $exclude = $_data['exclude'];
  325. $maxage = $_data['maxage'];
  326. $maxbytespersecond = $_data['maxbytespersecond'];
  327. $subfolder2 = $_data['subfolder2'];
  328. $user1 = $_data['user1'];
  329. $mins_interval = $_data['mins_interval'];
  330. $enc1 = $_data['enc1'];
  331. $custom_params = (empty(trim($_data['custom_params']))) ? '' : trim($_data['custom_params']);
  332. // Workaround, fixme
  333. if (strpos($custom_params, 'pipemess')) {
  334. $custom_params = '';
  335. }
  336. if (empty($subfolder2)) {
  337. $subfolder2 = "";
  338. }
  339. if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  340. $maxage = "0";
  341. }
  342. if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  343. $timeout1 = "600";
  344. }
  345. if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  346. $timeout2 = "600";
  347. }
  348. if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
  349. $maxbytespersecond = "0";
  350. }
  351. if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
  352. $_SESSION['return'][] = array(
  353. 'type' => 'danger',
  354. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  355. 'msg' => 'access_denied'
  356. );
  357. return false;
  358. }
  359. if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
  360. $_SESSION['return'][] = array(
  361. 'type' => 'danger',
  362. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  363. 'msg' => 'access_denied'
  364. );
  365. return false;
  366. }
  367. // if (!is_valid_domain_name($host1)) {
  368. // $_SESSION['return'][] = array(
  369. // 'type' => 'danger',
  370. // 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  371. // 'msg' => 'access_denied'
  372. // );
  373. // return false;
  374. // }
  375. if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
  376. $_SESSION['return'][] = array(
  377. 'type' => 'danger',
  378. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  379. 'msg' => 'access_denied'
  380. );
  381. return false;
  382. }
  383. if (@preg_match("/" . $exclude . "/", null) === false) {
  384. $_SESSION['return'][] = array(
  385. 'type' => 'danger',
  386. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  387. 'msg' => 'access_denied'
  388. );
  389. return false;
  390. }
  391. $stmt = $pdo->prepare("SELECT '1' FROM `imapsync`
  392. WHERE `user2` = :user2 AND `user1` = :user1 AND `host1` = :host1");
  393. $stmt->execute(array(':user1' => $user1, ':user2' => $username, ':host1' => $host1));
  394. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  395. if ($num_results != 0) {
  396. $_SESSION['return'][] = array(
  397. 'type' => 'danger',
  398. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  399. 'msg' => array('object_exists', htmlspecialchars($host1 . ' / ' . $user1))
  400. );
  401. return false;
  402. }
  403. $stmt = $pdo->prepare("INSERT INTO `imapsync` (`user2`, `exclude`, `delete1`, `delete2`, `timeout1`, `timeout2`, `automap`, `skipcrossduplicates`, `maxbytespersecond`, `subscribeall`, `maxage`, `subfolder2`, `host1`, `authmech1`, `user1`, `password1`, `mins_interval`, `port1`, `enc1`, `delete2duplicates`, `custom_params`, `active`)
  404. VALUES (:user2, :exclude, :delete1, :delete2, :timeout1, :timeout2, :automap, :skipcrossduplicates, :maxbytespersecond, :subscribeall, :maxage, :subfolder2, :host1, :authmech1, :user1, :password1, :mins_interval, :port1, :enc1, :delete2duplicates, :custom_params, :active)");
  405. $stmt->execute(array(
  406. ':user2' => $username,
  407. ':custom_params' => $custom_params,
  408. ':exclude' => $exclude,
  409. ':maxage' => $maxage,
  410. ':delete1' => $delete1,
  411. ':delete2' => $delete2,
  412. ':timeout1' => $timeout1,
  413. ':timeout2' => $timeout2,
  414. ':automap' => $automap,
  415. ':skipcrossduplicates' => $skipcrossduplicates,
  416. ':maxbytespersecond' => $maxbytespersecond,
  417. ':subscribeall' => $subscribeall,
  418. ':subfolder2' => $subfolder2,
  419. ':host1' => $host1,
  420. ':authmech1' => 'PLAIN',
  421. ':user1' => $user1,
  422. ':password1' => $password1,
  423. ':mins_interval' => $mins_interval,
  424. ':port1' => $port1,
  425. ':enc1' => $enc1,
  426. ':delete2duplicates' => $delete2duplicates,
  427. ':active' => $active,
  428. ));
  429. $_SESSION['return'][] = array(
  430. 'type' => 'success',
  431. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  432. 'msg' => array('mailbox_modified', $username)
  433. );
  434. break;
  435. case 'domain':
  436. if ($_SESSION['mailcow_cc_role'] != "admin") {
  437. $_SESSION['return'][] = array(
  438. 'type' => 'danger',
  439. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  440. 'msg' => 'access_denied'
  441. );
  442. return false;
  443. }
  444. $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
  445. $description = $_data['description'];
  446. if (empty($description)) {
  447. $description = $domain;
  448. }
  449. $aliases = $_data['aliases'];
  450. $mailboxes = $_data['mailboxes'];
  451. $defquota = $_data['defquota'];
  452. $maxquota = $_data['maxquota'];
  453. $restart_sogo = $_data['restart_sogo'];
  454. $quota = $_data['quota'];
  455. if ($defquota > $maxquota) {
  456. $_SESSION['return'][] = array(
  457. 'type' => 'danger',
  458. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  459. 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
  460. );
  461. return false;
  462. }
  463. if ($maxquota > $quota) {
  464. $_SESSION['return'][] = array(
  465. 'type' => 'danger',
  466. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  467. 'msg' => 'mailbox_quota_exceeds_domain_quota'
  468. );
  469. return false;
  470. }
  471. if ($defquota == "0" || empty($defquota)) {
  472. $_SESSION['return'][] = array(
  473. 'type' => 'danger',
  474. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  475. 'msg' => 'defquota_empty'
  476. );
  477. return false;
  478. }
  479. if ($maxquota == "0" || empty($maxquota)) {
  480. $_SESSION['return'][] = array(
  481. 'type' => 'danger',
  482. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  483. 'msg' => 'maxquota_empty'
  484. );
  485. return false;
  486. }
  487. $active = intval($_data['active']);
  488. $relay_all_recipients = intval($_data['relay_all_recipients']);
  489. $relay_unknown_only = intval($_data['relay_unknown_only']);
  490. $backupmx = intval($_data['backupmx']);
  491. $gal = intval($_data['gal']);
  492. if ($relay_all_recipients == 1) {
  493. $backupmx = '1';
  494. }
  495. if ($relay_unknown_only == 1) {
  496. $backupmx = 1;
  497. $relay_all_recipients = 1;
  498. }
  499. if (!is_valid_domain_name($domain)) {
  500. $_SESSION['return'][] = array(
  501. 'type' => 'danger',
  502. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  503. 'msg' => 'domain_invalid'
  504. );
  505. return false;
  506. }
  507. foreach (array($quota, $maxquota, $mailboxes, $aliases) as $data) {
  508. if (!is_numeric($data)) {
  509. $_SESSION['return'][] = array(
  510. 'type' => 'danger',
  511. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  512. 'msg' => array('object_is_not_numeric', htmlspecialchars($data))
  513. );
  514. return false;
  515. }
  516. }
  517. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  518. WHERE `domain` = :domain");
  519. $stmt->execute(array(':domain' => $domain));
  520. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  521. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain`
  522. WHERE `alias_domain` = :domain");
  523. $stmt->execute(array(':domain' => $domain));
  524. $num_results = $num_results + count($stmt->fetchAll(PDO::FETCH_ASSOC));
  525. if ($num_results != 0) {
  526. $_SESSION['return'][] = array(
  527. 'type' => 'danger',
  528. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  529. 'msg' => array('domain_exists', htmlspecialchars($domain))
  530. );
  531. return false;
  532. }
  533. if ($domain == getenv('MAILCOW_HOSTNAME')) {
  534. $_SESSION['return'][] = array(
  535. 'type' => 'danger',
  536. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  537. 'msg' => 'domain_cannot_match_hostname'
  538. );
  539. return false;
  540. }
  541. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
  542. $stmt->execute(array(
  543. ':domain' => '%@' . $domain
  544. ));
  545. $stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `defquota`, `maxquota`, `quota`, `backupmx`, `gal`, `active`, `relay_unknown_only`, `relay_all_recipients`)
  546. VALUES (:domain, :description, :aliases, :mailboxes, :defquota, :maxquota, :quota, :backupmx, :gal, :active, :relay_unknown_only, :relay_all_recipients)");
  547. $stmt->execute(array(
  548. ':domain' => $domain,
  549. ':description' => $description,
  550. ':aliases' => $aliases,
  551. ':mailboxes' => $mailboxes,
  552. ':defquota' => $defquota,
  553. ':maxquota' => $maxquota,
  554. ':quota' => $quota,
  555. ':backupmx' => $backupmx,
  556. ':gal' => $gal,
  557. ':active' => $active,
  558. ':relay_unknown_only' => $relay_unknown_only,
  559. ':relay_all_recipients' => $relay_all_recipients
  560. ));
  561. try {
  562. $redis->hSet('DOMAIN_MAP', $domain, 1);
  563. }
  564. catch (RedisException $e) {
  565. $_SESSION['return'][] = array(
  566. 'type' => 'danger',
  567. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  568. 'msg' => array('redis_error', $e)
  569. );
  570. return false;
  571. }
  572. if (!empty(intval($_data['rl_value']))) {
  573. ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $domain));
  574. }
  575. if (!empty($restart_sogo)) {
  576. $restart_response = json_decode(docker('post', 'sogo-mailcow', 'restart'), true);
  577. if ($restart_response['type'] == "success") {
  578. $_SESSION['return'][] = array(
  579. 'type' => 'success',
  580. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  581. 'msg' => array('domain_added', htmlspecialchars($domain))
  582. );
  583. return true;
  584. }
  585. else {
  586. $_SESSION['return'][] = array(
  587. 'type' => 'warning',
  588. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  589. 'msg' => 'domain_added_sogo_failed'
  590. );
  591. return false;
  592. }
  593. }
  594. $_SESSION['return'][] = array(
  595. 'type' => 'success',
  596. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  597. 'msg' => array('domain_added', htmlspecialchars($domain))
  598. );
  599. return true;
  600. break;
  601. case 'alias':
  602. $addresses = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['address']));
  603. $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['goto']));
  604. $active = intval($_data['active']);
  605. $sogo_visible = intval($_data['sogo_visible']);
  606. $goto_null = intval($_data['goto_null']);
  607. $goto_spam = intval($_data['goto_spam']);
  608. $goto_ham = intval($_data['goto_ham']);
  609. $private_comment = $_data['private_comment'];
  610. $public_comment = $_data['public_comment'];
  611. if (strlen($private_comment) > 160 | strlen($public_comment) > 160){
  612. $_SESSION['return'][] = array(
  613. 'type' => 'danger',
  614. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  615. 'msg' => 'comment_too_long'
  616. );
  617. return false;
  618. }
  619. if (empty($addresses[0])) {
  620. $_SESSION['return'][] = array(
  621. 'type' => 'danger',
  622. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  623. 'msg' => 'alias_empty'
  624. );
  625. return false;
  626. }
  627. if (empty($gotos[0]) && ($goto_null + $goto_spam + $goto_ham == 0)) {
  628. $_SESSION['return'][] = array(
  629. 'type' => 'danger',
  630. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  631. 'msg' => 'goto_empty'
  632. );
  633. return false;
  634. }
  635. if ($goto_null == "1") {
  636. $goto = "null@localhost";
  637. }
  638. elseif ($goto_spam == "1") {
  639. $goto = "spam@localhost";
  640. }
  641. elseif ($goto_ham == "1") {
  642. $goto = "ham@localhost";
  643. }
  644. else {
  645. foreach ($gotos as $i => &$goto) {
  646. if (empty($goto)) {
  647. continue;
  648. }
  649. $goto_domain = idn_to_ascii(substr(strstr($goto, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  650. $goto_local_part = strstr($goto, '@', true);
  651. $goto = $goto_local_part.'@'.$goto_domain;
  652. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  653. WHERE `kind` REGEXP 'location|thing|group'
  654. AND `username`= :goto");
  655. $stmt->execute(array(':goto' => $goto));
  656. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  657. if ($num_results != 0) {
  658. $_SESSION['return'][] = array(
  659. 'type' => 'danger',
  660. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  661. 'msg' => array('goto_invalid', htmlspecialchars($goto))
  662. );
  663. unset($gotos[$i]);
  664. continue;
  665. }
  666. if (!filter_var($goto, FILTER_VALIDATE_EMAIL) === true) {
  667. $_SESSION['return'][] = array(
  668. 'type' => 'danger',
  669. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  670. 'msg' => array('goto_invalid', htmlspecialchars($goto))
  671. );
  672. unset($gotos[$i]);
  673. continue;
  674. }
  675. }
  676. $gotos = array_filter($gotos);
  677. if (empty($gotos)) { return false; }
  678. $goto = implode(",", $gotos);
  679. }
  680. foreach ($addresses as $address) {
  681. if (empty($address)) {
  682. continue;
  683. }
  684. if (in_array($address, $gotos)) {
  685. continue;
  686. }
  687. $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  688. $local_part = strstr($address, '@', true);
  689. $address = $local_part.'@'.$domain;
  690. $domaindata = mailbox('get', 'domain_details', $domain);
  691. if (is_array($domaindata) && $domaindata['aliases_left'] == "0") {
  692. $_SESSION['return'][] = array(
  693. 'type' => 'danger',
  694. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  695. 'msg' => 'max_alias_exceeded'
  696. );
  697. return false;
  698. }
  699. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  700. WHERE `address`= :address OR `address` IN (
  701. SELECT `username` FROM `mailbox`, `alias_domain`
  702. WHERE (
  703. `alias_domain`.`alias_domain` = :address_d
  704. AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
  705. $stmt->execute(array(
  706. ':address' => $address,
  707. ':address_l' => $local_part,
  708. ':address_d' => $domain
  709. ));
  710. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  711. if ($num_results != 0) {
  712. $_SESSION['return'][] = array(
  713. 'type' => 'danger',
  714. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  715. 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
  716. );
  717. continue;
  718. }
  719. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  720. WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
  721. $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
  722. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  723. if ($num_results == 0) {
  724. $_SESSION['return'][] = array(
  725. 'type' => 'danger',
  726. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  727. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  728. );
  729. continue;
  730. }
  731. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
  732. WHERE `address`= :address");
  733. $stmt->execute(array(':address' => $address));
  734. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  735. if ($num_results != 0) {
  736. $_SESSION['return'][] = array(
  737. 'type' => 'danger',
  738. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  739. 'msg' => array('is_spam_alias', htmlspecialchars($address))
  740. );
  741. continue;
  742. }
  743. if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
  744. $_SESSION['return'][] = array(
  745. 'type' => 'danger',
  746. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  747. 'msg' => array('alias_invalid', $address)
  748. );
  749. continue;
  750. }
  751. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  752. $_SESSION['return'][] = array(
  753. 'type' => 'danger',
  754. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  755. 'msg' => 'access_denied'
  756. );
  757. continue;
  758. }
  759. $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `public_comment`, `private_comment`, `goto`, `domain`, `sogo_visible`, `active`)
  760. VALUES (:address, :public_comment, :private_comment, :goto, :domain, :sogo_visible, :active)");
  761. if (!filter_var($address, FILTER_VALIDATE_EMAIL) === true) {
  762. $stmt->execute(array(
  763. ':address' => '@'.$domain,
  764. ':public_comment' => $public_comment,
  765. ':private_comment' => $private_comment,
  766. ':address' => '@'.$domain,
  767. ':goto' => $goto,
  768. ':domain' => $domain,
  769. ':sogo_visible' => $sogo_visible,
  770. ':active' => $active
  771. ));
  772. }
  773. else {
  774. $stmt->execute(array(
  775. ':address' => $address,
  776. ':public_comment' => $public_comment,
  777. ':private_comment' => $private_comment,
  778. ':goto' => $goto,
  779. ':domain' => $domain,
  780. ':sogo_visible' => $sogo_visible,
  781. ':active' => $active
  782. ));
  783. }
  784. $id = $pdo->lastInsertId();
  785. $_SESSION['return'][] = array(
  786. 'type' => 'success',
  787. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  788. 'msg' => array('alias_added', $address, $id)
  789. );
  790. }
  791. break;
  792. case 'alias_domain':
  793. $active = intval($_data['active']);
  794. $alias_domains = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['alias_domain']));
  795. $alias_domains = array_filter($alias_domains);
  796. $target_domain = idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46);
  797. if (!isset($_SESSION['acl']['alias_domains']) || $_SESSION['acl']['alias_domains'] != "1" ) {
  798. $_SESSION['return'][] = array(
  799. 'type' => 'danger',
  800. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  801. 'msg' => 'access_denied'
  802. );
  803. return false;
  804. }
  805. if (!is_valid_domain_name($target_domain)) {
  806. $_SESSION['return'][] = array(
  807. 'type' => 'danger',
  808. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  809. 'msg' => 'target_domain_invalid'
  810. );
  811. return false;
  812. }
  813. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
  814. $_SESSION['return'][] = array(
  815. 'type' => 'danger',
  816. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  817. 'msg' => 'access_denied'
  818. );
  819. return false;
  820. }
  821. foreach ($alias_domains as $i => $alias_domain) {
  822. $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
  823. if (!is_valid_domain_name($alias_domain)) {
  824. $_SESSION['return'][] = array(
  825. 'type' => 'danger',
  826. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  827. 'msg' => array('alias_domain_invalid', htmlspecialchars(alias_domain))
  828. );
  829. continue;
  830. }
  831. if ($alias_domain == $target_domain) {
  832. $_SESSION['return'][] = array(
  833. 'type' => 'danger',
  834. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  835. 'msg' => array('aliasd_targetd_identical', htmlspecialchars($target_domain))
  836. );
  837. continue;
  838. }
  839. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  840. WHERE `domain`= :target_domain");
  841. $stmt->execute(array(':target_domain' => $target_domain));
  842. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  843. if ($num_results == 0) {
  844. $_SESSION['return'][] = array(
  845. 'type' => 'danger',
  846. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  847. 'msg' => array('targetd_not_found', htmlspecialchars($target_domain))
  848. );
  849. continue;
  850. }
  851. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  852. WHERE `domain`= :target_domain AND `backupmx` = '1'");
  853. $stmt->execute(array(':target_domain' => $target_domain));
  854. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  855. if ($num_results == 1) {
  856. $_SESSION['return'][] = array(
  857. 'type' => 'danger',
  858. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  859. 'msg' => array('targetd_relay_domain', htmlspecialchars($target_domain))
  860. );
  861. continue;
  862. }
  863. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain`= :alias_domain
  864. UNION
  865. SELECT `domain` FROM `domain` WHERE `domain`= :alias_domain_in_domain");
  866. $stmt->execute(array(':alias_domain' => $alias_domain, ':alias_domain_in_domain' => $alias_domain));
  867. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  868. if ($num_results != 0) {
  869. $_SESSION['return'][] = array(
  870. 'type' => 'danger',
  871. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  872. 'msg' => array('alias_domain_invalid', $alias_domain)
  873. );
  874. continue;
  875. }
  876. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `send_as` LIKE :domain");
  877. $stmt->execute(array(
  878. ':domain' => '%@' . $domain
  879. ));
  880. $stmt = $pdo->prepare("INSERT INTO `alias_domain` (`alias_domain`, `target_domain`, `active`)
  881. VALUES (:alias_domain, :target_domain, :active)");
  882. $stmt->execute(array(
  883. ':alias_domain' => $alias_domain,
  884. ':target_domain' => $target_domain,
  885. ':active' => $active
  886. ));
  887. try {
  888. $redis->hSet('DOMAIN_MAP', $alias_domain, 1);
  889. }
  890. catch (RedisException $e) {
  891. $_SESSION['return'][] = array(
  892. 'type' => 'danger',
  893. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  894. 'msg' => array('redis_error', $e)
  895. );
  896. return false;
  897. }
  898. if (!empty(intval($_data['rl_value']))) {
  899. ratelimit('edit', 'domain', array('rl_value' => $_data['rl_value'], 'rl_frame' => $_data['rl_frame'], 'object' => $alias_domain));
  900. }
  901. $_SESSION['return'][] = array(
  902. 'type' => 'success',
  903. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  904. 'msg' => array('aliasd_added', htmlspecialchars($alias_domain))
  905. );
  906. }
  907. break;
  908. case 'mailbox':
  909. $local_part = strtolower(trim($_data['local_part']));
  910. $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
  911. $username = $local_part . '@' . $domain;
  912. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  913. $_SESSION['return'][] = array(
  914. 'type' => 'danger',
  915. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  916. 'msg' => 'mailbox_invalid'
  917. );
  918. return false;
  919. }
  920. if (empty($_data['local_part'])) {
  921. $_SESSION['return'][] = array(
  922. 'type' => 'danger',
  923. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  924. 'msg' => 'mailbox_invalid'
  925. );
  926. return false;
  927. }
  928. $password = $_data['password'];
  929. $password2 = $_data['password2'];
  930. $name = ltrim(rtrim($_data['name'], '>'), '<');
  931. $quota_m = intval($_data['quota']);
  932. if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && $quota_m === 0) {
  933. $_SESSION['return'][] = array(
  934. 'type' => 'danger',
  935. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  936. 'msg' => 'unlimited_quota_acl'
  937. );
  938. return false;
  939. }
  940. if (empty($name)) {
  941. $name = $local_part;
  942. }
  943. $active = intval($_data['active']);
  944. $force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
  945. $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
  946. $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
  947. $sogo_access = (isset($_data['sogo_access'])) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
  948. $imap_access = (isset($_data['imap_access'])) ? intval($_data['imap_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
  949. $pop3_access = (isset($_data['pop3_access'])) ? intval($_data['pop3_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
  950. $smtp_access = (isset($_data['smtp_access'])) ? intval($_data['smtp_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
  951. $quarantine_notification = (isset($_data['quarantine_notification'])) ? strval($_data['quarantine_notification']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
  952. $quarantine_category = (isset($_data['quarantine_category'])) ? strval($_data['quarantine_category']) : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
  953. $quota_b = ($quota_m * 1048576);
  954. $mailbox_attrs = json_encode(
  955. array(
  956. 'force_pw_update' => strval($force_pw_update),
  957. 'tls_enforce_in' => strval($tls_enforce_in),
  958. 'tls_enforce_out' => strval($tls_enforce_out),
  959. 'sogo_access' => strval($sogo_access),
  960. 'imap_access' => strval($imap_access),
  961. 'pop3_access' => strval($pop3_access),
  962. 'smtp_access' => strval($smtp_access),
  963. 'mailbox_format' => strval($MAILBOX_DEFAULT_ATTRIBUTES['mailbox_format']),
  964. 'quarantine_notification' => strval($quarantine_notification),
  965. 'quarantine_category' => strval($quarantine_category)
  966. )
  967. );
  968. if (!is_valid_domain_name($domain)) {
  969. $_SESSION['return'][] = array(
  970. 'type' => 'danger',
  971. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  972. 'msg' => 'domain_invalid'
  973. );
  974. return false;
  975. }
  976. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  977. $_SESSION['return'][] = array(
  978. 'type' => 'danger',
  979. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  980. 'msg' => 'access_denied'
  981. );
  982. return false;
  983. }
  984. $stmt = $pdo->prepare("SELECT `mailboxes`, `maxquota`, `quota` FROM `domain`
  985. WHERE `domain` = :domain");
  986. $stmt->execute(array(':domain' => $domain));
  987. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  988. $stmt = $pdo->prepare("SELECT
  989. COUNT(*) as count,
  990. COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota`
  991. FROM `mailbox`
  992. WHERE `kind` NOT REGEXP 'location|thing|group'
  993. AND `domain` = :domain");
  994. $stmt->execute(array(':domain' => $domain));
  995. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  996. $stmt = $pdo->prepare("SELECT `local_part` FROM `mailbox` WHERE `local_part` = :local_part and `domain`= :domain");
  997. $stmt->execute(array(':local_part' => $local_part, ':domain' => $domain));
  998. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  999. if ($num_results != 0) {
  1000. $_SESSION['return'][] = array(
  1001. 'type' => 'danger',
  1002. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1003. 'msg' => array('object_exists', htmlspecialchars($username))
  1004. );
  1005. return false;
  1006. }
  1007. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :username");
  1008. $stmt->execute(array(':username' => $username));
  1009. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1010. if ($num_results != 0) {
  1011. $_SESSION['return'][] = array(
  1012. 'type' => 'danger',
  1013. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1014. 'msg' => array('is_alias', htmlspecialchars($username))
  1015. );
  1016. return false;
  1017. }
  1018. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :username");
  1019. $stmt->execute(array(':username' => $username));
  1020. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1021. if ($num_results != 0) {
  1022. $_SESSION['return'][] = array(
  1023. 'type' => 'danger',
  1024. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1025. 'msg' => array('is_spam_alias', htmlspecialchars($username))
  1026. );
  1027. return false;
  1028. }
  1029. $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
  1030. $stmt->execute(array(':domain' => $domain));
  1031. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1032. if ($num_results == 0) {
  1033. $_SESSION['return'][] = array(
  1034. 'type' => 'danger',
  1035. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1036. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  1037. );
  1038. return false;
  1039. }
  1040. if (!empty($password) && !empty($password2)) {
  1041. if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
  1042. $_SESSION['return'][] = array(
  1043. 'type' => 'danger',
  1044. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1045. 'msg' => 'password_complexity'
  1046. );
  1047. return false;
  1048. }
  1049. if ($password != $password2) {
  1050. $_SESSION['return'][] = array(
  1051. 'type' => 'danger',
  1052. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1053. 'msg' => 'password_mismatch'
  1054. );
  1055. return false;
  1056. }
  1057. $password_hashed = hash_password($password);
  1058. }
  1059. else {
  1060. $_SESSION['return'][] = array(
  1061. 'type' => 'danger',
  1062. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1063. 'msg' => 'password_empty'
  1064. );
  1065. return false;
  1066. }
  1067. if ($MailboxData['count'] >= $DomainData['mailboxes']) {
  1068. $_SESSION['return'][] = array(
  1069. 'type' => 'danger',
  1070. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1071. 'msg' => array('max_mailbox_exceeded', $MailboxData['count'], $DomainData['mailboxes'])
  1072. );
  1073. return false;
  1074. }
  1075. if ($quota_m > $DomainData['maxquota']) {
  1076. $_SESSION['return'][] = array(
  1077. 'type' => 'danger',
  1078. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1079. 'msg' => array('mailbox_quota_exceeded', $DomainData['maxquota'])
  1080. );
  1081. return false;
  1082. }
  1083. if (($MailboxData['quota'] + $quota_m) > $DomainData['quota']) {
  1084. $quota_left_m = ($DomainData['quota'] - $MailboxData['quota']);
  1085. $_SESSION['return'][] = array(
  1086. 'type' => 'danger',
  1087. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1088. 'msg' => array('mailbox_quota_left_exceeded', $quota_left_m)
  1089. );
  1090. return false;
  1091. }
  1092. $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`)
  1093. VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)");
  1094. $stmt->execute(array(
  1095. ':username' => $username,
  1096. ':password_hashed' => $password_hashed,
  1097. ':name' => $name,
  1098. ':quota_b' => $quota_b,
  1099. ':local_part' => $local_part,
  1100. ':domain' => $domain,
  1101. ':mailbox_attrs' => $mailbox_attrs,
  1102. ':active' => $active
  1103. ));
  1104. $stmt = $pdo->prepare("INSERT INTO `quota2` (`username`, `bytes`, `messages`)
  1105. VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
  1106. $stmt->execute(array(':username' => $username));
  1107. $stmt = $pdo->prepare("INSERT INTO `quota2replica` (`username`, `bytes`, `messages`)
  1108. VALUES (:username, '0', '0') ON DUPLICATE KEY UPDATE `bytes` = '0', `messages` = '0';");
  1109. $stmt->execute(array(':username' => $username));
  1110. $stmt = $pdo->prepare("INSERT INTO `alias` (`address`, `goto`, `domain`, `active`)
  1111. VALUES (:username1, :username2, :domain, :active)");
  1112. $stmt->execute(array(
  1113. ':username1' => $username,
  1114. ':username2' => $username,
  1115. ':domain' => $domain,
  1116. ':active' => $active
  1117. ));
  1118. $stmt = $pdo->prepare("INSERT INTO `user_acl` (`username`) VALUES (:username)");
  1119. $stmt->execute(array(
  1120. ':username' => $username
  1121. ));
  1122. $_SESSION['return'][] = array(
  1123. 'type' => 'success',
  1124. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1125. 'msg' => array('mailbox_added', htmlspecialchars($username))
  1126. );
  1127. break;
  1128. case 'resource':
  1129. $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
  1130. $description = $_data['description'];
  1131. $local_part = preg_replace('/[^\da-z]/i', '', preg_quote($description, '/'));
  1132. $name = $local_part . '@' . $domain;
  1133. $kind = $_data['kind'];
  1134. $multiple_bookings = intval($_data['multiple_bookings']);
  1135. $active = intval($_data['active']);
  1136. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  1137. $_SESSION['return'][] = array(
  1138. 'type' => 'danger',
  1139. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1140. 'msg' => 'resource_invalid'
  1141. );
  1142. return false;
  1143. }
  1144. if (empty($description)) {
  1145. $_SESSION['return'][] = array(
  1146. 'type' => 'danger',
  1147. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1148. 'msg' => 'description_invalid'
  1149. );
  1150. return false;
  1151. }
  1152. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  1153. $multiple_bookings = -1;
  1154. }
  1155. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  1156. $_SESSION['return'][] = array(
  1157. 'type' => 'danger',
  1158. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1159. 'msg' => 'resource_invalid'
  1160. );
  1161. return false;
  1162. }
  1163. if (!is_valid_domain_name($domain)) {
  1164. $_SESSION['return'][] = array(
  1165. 'type' => 'danger',
  1166. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1167. 'msg' => 'domain_invalid'
  1168. );
  1169. return false;
  1170. }
  1171. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  1172. $_SESSION['return'][] = array(
  1173. 'type' => 'danger',
  1174. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1175. 'msg' => 'access_denied'
  1176. );
  1177. return false;
  1178. }
  1179. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :name");
  1180. $stmt->execute(array(':name' => $name));
  1181. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1182. if ($num_results != 0) {
  1183. $_SESSION['return'][] = array(
  1184. 'type' => 'danger',
  1185. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1186. 'msg' => array('object_exists', htmlspecialchars($name))
  1187. );
  1188. return false;
  1189. }
  1190. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :name");
  1191. $stmt->execute(array(':name' => $name));
  1192. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1193. if ($num_results != 0) {
  1194. $_SESSION['return'][] = array(
  1195. 'type' => 'danger',
  1196. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1197. 'msg' => array('is_alias', htmlspecialchars($name))
  1198. );
  1199. return false;
  1200. }
  1201. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :name");
  1202. $stmt->execute(array(':name' => $name));
  1203. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1204. if ($num_results != 0) {
  1205. $_SESSION['return'][] = array(
  1206. 'type' => 'danger',
  1207. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1208. 'msg' => array('is_spam_alias', htmlspecialchars($name))
  1209. );
  1210. return false;
  1211. }
  1212. $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
  1213. $stmt->execute(array(':domain' => $domain));
  1214. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1215. if ($num_results == 0) {
  1216. $_SESSION['return'][] = array(
  1217. 'type' => 'danger',
  1218. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1219. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  1220. );
  1221. return false;
  1222. }
  1223. $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
  1224. VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
  1225. $stmt->execute(array(
  1226. ':name' => $name,
  1227. ':description' => $description,
  1228. ':local_part' => $local_part,
  1229. ':domain' => $domain,
  1230. ':active' => $active,
  1231. ':kind' => $kind,
  1232. ':multiple_bookings' => $multiple_bookings
  1233. ));
  1234. $_SESSION['return'][] = array(
  1235. 'type' => 'success',
  1236. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1237. 'msg' => array('resource_added', htmlspecialchars($name))
  1238. );
  1239. break;
  1240. }
  1241. break;
  1242. case 'edit':
  1243. switch ($_type) {
  1244. case 'alias_domain':
  1245. $alias_domains = (array)$_data['alias_domain'];
  1246. foreach ($alias_domains as $alias_domain) {
  1247. $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
  1248. $is_now = mailbox('get', 'alias_domain_details', $alias_domain);
  1249. if (!empty($is_now)) {
  1250. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  1251. $target_domain = (!empty($_data['target_domain'])) ? idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46) : $is_now['target_domain'];
  1252. }
  1253. else {
  1254. $_SESSION['return'][] = array(
  1255. 'type' => 'danger',
  1256. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1257. 'msg' => array('alias_domain_invalid', htmlspecialchars($alias_domain))
  1258. );
  1259. continue;
  1260. }
  1261. if (!is_valid_domain_name($target_domain)) {
  1262. $_SESSION['return'][] = array(
  1263. 'type' => 'danger',
  1264. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1265. 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
  1266. );
  1267. continue;
  1268. }
  1269. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
  1270. $_SESSION['return'][] = array(
  1271. 'type' => 'danger',
  1272. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1273. 'msg' => 'access_denied'
  1274. );
  1275. continue;
  1276. }
  1277. if (empty(mailbox('get', 'domain_details', $target_domain)) || !empty(mailbox('get', 'alias_domain_details', $target_domain))) {
  1278. $_SESSION['return'][] = array(
  1279. 'type' => 'danger',
  1280. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1281. 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
  1282. );
  1283. continue;
  1284. }
  1285. $stmt = $pdo->prepare("UPDATE `alias_domain` SET
  1286. `target_domain` = :target_domain,
  1287. `active` = :active
  1288. WHERE `alias_domain` = :alias_domain");
  1289. $stmt->execute(array(
  1290. ':alias_domain' => $alias_domain,
  1291. ':target_domain' => $target_domain,
  1292. ':active' => $active
  1293. ));
  1294. $_SESSION['return'][] = array(
  1295. 'type' => 'success',
  1296. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1297. 'msg' => array('aliasd_modified', htmlspecialchars($alias_domain))
  1298. );
  1299. }
  1300. break;
  1301. case 'tls_policy':
  1302. if (!is_array($_data['username'])) {
  1303. $usernames = array();
  1304. $usernames[] = $_data['username'];
  1305. }
  1306. else {
  1307. $usernames = $_data['username'];
  1308. }
  1309. if (!isset($_SESSION['acl']['tls_policy']) || $_SESSION['acl']['tls_policy'] != "1" ) {
  1310. $_SESSION['return'][] = array(
  1311. 'type' => 'danger',
  1312. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1313. 'msg' => 'access_denied'
  1314. );
  1315. return false;
  1316. }
  1317. foreach ($usernames as $username) {
  1318. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1319. $_SESSION['return'][] = array(
  1320. 'type' => 'danger',
  1321. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1322. 'msg' => 'access_denied'
  1323. );
  1324. continue;
  1325. }
  1326. $is_now = mailbox('get', 'tls_policy', $username);
  1327. if (!empty($is_now)) {
  1328. $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
  1329. $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
  1330. }
  1331. else {
  1332. $_SESSION['return'][] = array(
  1333. 'type' => 'danger',
  1334. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1335. 'msg' => 'access_denied'
  1336. );
  1337. continue;
  1338. }
  1339. $stmt = $pdo->prepare("UPDATE `mailbox`
  1340. SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_out),
  1341. `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_in)
  1342. WHERE `username` = :username");
  1343. $stmt->execute(array(
  1344. ':tls_out' => intval($tls_enforce_out),
  1345. ':tls_in' => intval($tls_enforce_in),
  1346. ':username' => $username
  1347. ));
  1348. $_SESSION['return'][] = array(
  1349. 'type' => 'success',
  1350. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1351. 'msg' => array('mailbox_modified', $username)
  1352. );
  1353. }
  1354. break;
  1355. case 'quarantine_notification':
  1356. if (!is_array($_data['username'])) {
  1357. $usernames = array();
  1358. $usernames[] = $_data['username'];
  1359. }
  1360. else {
  1361. $usernames = $_data['username'];
  1362. }
  1363. if (!isset($_SESSION['acl']['quarantine_notification']) || $_SESSION['acl']['quarantine_notification'] != "1" ) {
  1364. $_SESSION['return'][] = array(
  1365. 'type' => 'danger',
  1366. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1367. 'msg' => 'access_denied'
  1368. );
  1369. return false;
  1370. }
  1371. foreach ($usernames as $username) {
  1372. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1373. $_SESSION['return'][] = array(
  1374. 'type' => 'danger',
  1375. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1376. 'msg' => 'access_denied'
  1377. );
  1378. continue;
  1379. }
  1380. $is_now = mailbox('get', 'quarantine_notification', $username);
  1381. if (!empty($is_now)) {
  1382. $quarantine_notification = (isset($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
  1383. }
  1384. else {
  1385. $_SESSION['return'][] = array(
  1386. 'type' => 'danger',
  1387. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1388. 'msg' => 'access_denied'
  1389. );
  1390. continue;
  1391. }
  1392. if (!in_array($quarantine_notification, array('never', 'hourly', 'daily', 'weekly'))) {
  1393. $_SESSION['return'][] = array(
  1394. 'type' => 'danger',
  1395. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1396. 'msg' => 'access_denied'
  1397. );
  1398. continue;
  1399. }
  1400. $stmt = $pdo->prepare("UPDATE `mailbox`
  1401. SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
  1402. WHERE `username` = :username");
  1403. $stmt->execute(array(
  1404. ':quarantine_notification' => $quarantine_notification,
  1405. ':username' => $username
  1406. ));
  1407. $_SESSION['return'][] = array(
  1408. 'type' => 'success',
  1409. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1410. 'msg' => array('mailbox_modified', $username)
  1411. );
  1412. }
  1413. break;
  1414. case 'quarantine_category':
  1415. if (!is_array($_data['username'])) {
  1416. $usernames = array();
  1417. $usernames[] = $_data['username'];
  1418. }
  1419. else {
  1420. $usernames = $_data['username'];
  1421. }
  1422. if (!isset($_SESSION['acl']['quarantine_category']) || $_SESSION['acl']['quarantine_category'] != "1" ) {
  1423. $_SESSION['return'][] = array(
  1424. 'type' => 'danger',
  1425. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1426. 'msg' => 'access_denied'
  1427. );
  1428. return false;
  1429. }
  1430. foreach ($usernames as $username) {
  1431. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1432. $_SESSION['return'][] = array(
  1433. 'type' => 'danger',
  1434. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1435. 'msg' => 'access_denied'
  1436. );
  1437. continue;
  1438. }
  1439. $is_now = mailbox('get', 'quarantine_category', $username);
  1440. if (!empty($is_now)) {
  1441. $quarantine_category = (isset($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
  1442. }
  1443. else {
  1444. $_SESSION['return'][] = array(
  1445. 'type' => 'danger',
  1446. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1447. 'msg' => 'access_denied'
  1448. );
  1449. continue;
  1450. }
  1451. if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
  1452. $_SESSION['return'][] = array(
  1453. 'type' => 'danger',
  1454. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1455. 'msg' => 'access_denied'
  1456. );
  1457. continue;
  1458. }
  1459. $stmt = $pdo->prepare("UPDATE `mailbox`
  1460. SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', :quarantine_category)
  1461. WHERE `username` = :username");
  1462. $stmt->execute(array(
  1463. ':quarantine_category' => $quarantine_category,
  1464. ':username' => $username
  1465. ));
  1466. $_SESSION['return'][] = array(
  1467. 'type' => 'success',
  1468. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1469. 'msg' => array('mailbox_modified', $username)
  1470. );
  1471. }
  1472. break;
  1473. case 'spam_score':
  1474. if (!is_array($_data['username'])) {
  1475. $usernames = array();
  1476. $usernames[] = $_data['username'];
  1477. }
  1478. else {
  1479. $usernames = $_data['username'];
  1480. }
  1481. if (!isset($_SESSION['acl']['spam_score']) || $_SESSION['acl']['spam_score'] != "1" ) {
  1482. $_SESSION['return'][] = array(
  1483. 'type' => 'danger',
  1484. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1485. 'msg' => 'access_denied'
  1486. );
  1487. return false;
  1488. }
  1489. foreach ($usernames as $username) {
  1490. if ($_data['spam_score'] == "default") {
  1491. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
  1492. AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  1493. $stmt->execute(array(
  1494. ':username' => $username
  1495. ));
  1496. $_SESSION['return'][] = array(
  1497. 'type' => 'success',
  1498. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1499. 'msg' => array('mailbox_modified', $username)
  1500. );
  1501. continue;
  1502. }
  1503. $lowspamlevel = explode(',', $_data['spam_score'])[0];
  1504. $highspamlevel = explode(',', $_data['spam_score'])[1];
  1505. if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
  1506. $_SESSION['return'][] = array(
  1507. 'type' => 'danger',
  1508. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1509. 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
  1510. );
  1511. continue;
  1512. }
  1513. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
  1514. AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  1515. $stmt->execute(array(
  1516. ':username' => $username
  1517. ));
  1518. $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
  1519. VALUES (:username, 'highspamlevel', :highspamlevel)");
  1520. $stmt->execute(array(
  1521. ':username' => $username,
  1522. ':highspamlevel' => $highspamlevel
  1523. ));
  1524. $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
  1525. VALUES (:username, 'lowspamlevel', :lowspamlevel)");
  1526. $stmt->execute(array(
  1527. ':username' => $username,
  1528. ':lowspamlevel' => $lowspamlevel
  1529. ));
  1530. $_SESSION['return'][] = array(
  1531. 'type' => 'success',
  1532. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1533. 'msg' => array('mailbox_modified', $username)
  1534. );
  1535. }
  1536. break;
  1537. case 'time_limited_alias':
  1538. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  1539. $_SESSION['return'][] = array(
  1540. 'type' => 'danger',
  1541. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1542. 'msg' => 'access_denied'
  1543. );
  1544. return false;
  1545. }
  1546. if (!is_array($_data['address'])) {
  1547. $addresses = array();
  1548. $addresses[] = $_data['address'];
  1549. }
  1550. else {
  1551. $addresses = $_data['address'];
  1552. }
  1553. foreach ($addresses as $address) {
  1554. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  1555. $stmt->execute(array(':address' => $address));
  1556. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  1557. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  1558. $_SESSION['return'][] = array(
  1559. 'type' => 'danger',
  1560. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1561. 'msg' => 'access_denied'
  1562. );
  1563. continue;
  1564. }
  1565. if (empty($_data['validity'])) {
  1566. continue;
  1567. }
  1568. $validity = round((int)time() + ($_data['validity'] * 3600));
  1569. $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE
  1570. `address` = :address");
  1571. $stmt->execute(array(
  1572. ':address' => $address,
  1573. ':validity' => $validity
  1574. ));
  1575. $_SESSION['return'][] = array(
  1576. 'type' => 'success',
  1577. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1578. 'msg' => array('mailbox_modified', htmlspecialchars(implode(', ', $usernames)))
  1579. );
  1580. }
  1581. break;
  1582. case 'delimiter_action':
  1583. if (!is_array($_data['username'])) {
  1584. $usernames = array();
  1585. $usernames[] = $_data['username'];
  1586. }
  1587. else {
  1588. $usernames = $_data['username'];
  1589. }
  1590. if (!isset($_SESSION['acl']['delimiter_action']) || $_SESSION['acl']['delimiter_action'] != "1" ) {
  1591. $_SESSION['return'][] = array(
  1592. 'type' => 'danger',
  1593. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1594. 'msg' => 'access_denied'
  1595. );
  1596. return false;
  1597. }
  1598. foreach ($usernames as $username) {
  1599. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1600. $_SESSION['return'][] = array(
  1601. 'type' => 'danger',
  1602. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1603. 'msg' => 'access_denied'
  1604. );
  1605. continue;
  1606. }
  1607. if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subject") {
  1608. try {
  1609. $redis->hSet('RCPT_WANTS_SUBJECT_TAG', $username, 1);
  1610. $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
  1611. }
  1612. catch (RedisException $e) {
  1613. $_SESSION['return'][] = array(
  1614. 'type' => 'danger',
  1615. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1616. 'msg' => array('redis_error', $e)
  1617. );
  1618. continue;
  1619. }
  1620. }
  1621. else if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subfolder") {
  1622. try {
  1623. $redis->hSet('RCPT_WANTS_SUBFOLDER_TAG', $username, 1);
  1624. $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
  1625. }
  1626. catch (RedisException $e) {
  1627. $_SESSION['return'][] = array(
  1628. 'type' => 'danger',
  1629. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1630. 'msg' => array('redis_error', $e)
  1631. );
  1632. continue;
  1633. }
  1634. }
  1635. else {
  1636. try {
  1637. $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
  1638. $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
  1639. }
  1640. catch (RedisException $e) {
  1641. $_SESSION['return'][] = array(
  1642. 'type' => 'danger',
  1643. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1644. 'msg' => array('redis_error', $e)
  1645. );
  1646. continue;
  1647. }
  1648. }
  1649. $_SESSION['return'][] = array(
  1650. 'type' => 'success',
  1651. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1652. 'msg' => array('mailbox_modified', $username)
  1653. );
  1654. }
  1655. break;
  1656. case 'syncjob':
  1657. if (!is_array($_data['id'])) {
  1658. $ids = array();
  1659. $ids[] = $_data['id'];
  1660. }
  1661. else {
  1662. $ids = $_data['id'];
  1663. }
  1664. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  1665. $_SESSION['return'][] = array(
  1666. 'type' => 'danger',
  1667. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1668. 'msg' => 'access_denied'
  1669. );
  1670. return false;
  1671. }
  1672. foreach ($ids as $id) {
  1673. $is_now = mailbox('get', 'syncjob_details', $id, array('with_password'));
  1674. if (!empty($is_now)) {
  1675. $username = $is_now['user2'];
  1676. $user1 = (!empty($_data['user1'])) ? $_data['user1'] : $is_now['user1'];
  1677. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  1678. $last_run = (isset($_data['last_run'])) ? NULL : $is_now['last_run'];
  1679. $delete2duplicates = (isset($_data['delete2duplicates'])) ? intval($_data['delete2duplicates']) : $is_now['delete2duplicates'];
  1680. $subscribeall = (isset($_data['subscribeall'])) ? intval($_data['subscribeall']) : $is_now['subscribeall'];
  1681. $delete1 = (isset($_data['delete1'])) ? intval($_data['delete1']) : $is_now['delete1'];
  1682. $delete2 = (isset($_data['delete2'])) ? intval($_data['delete2']) : $is_now['delete2'];
  1683. $automap = (isset($_data['automap'])) ? intval($_data['automap']) : $is_now['automap'];
  1684. $skipcrossduplicates = (isset($_data['skipcrossduplicates'])) ? intval($_data['skipcrossduplicates']) : $is_now['skipcrossduplicates'];
  1685. $port1 = (!empty($_data['port1'])) ? $_data['port1'] : $is_now['port1'];
  1686. $password1 = (!empty($_data['password1'])) ? $_data['password1'] : $is_now['password1'];
  1687. $host1 = (!empty($_data['host1'])) ? $_data['host1'] : $is_now['host1'];
  1688. $subfolder2 = (isset($_data['subfolder2'])) ? $_data['subfolder2'] : $is_now['subfolder2'];
  1689. $enc1 = (!empty($_data['enc1'])) ? $_data['enc1'] : $is_now['enc1'];
  1690. $mins_interval = (!empty($_data['mins_interval'])) ? $_data['mins_interval'] : $is_now['mins_interval'];
  1691. $exclude = (isset($_data['exclude'])) ? $_data['exclude'] : $is_now['exclude'];
  1692. $custom_params = (isset($_data['custom_params'])) ? $_data['custom_params'] : $is_now['custom_params'];
  1693. $maxage = (isset($_data['maxage']) && $_data['maxage'] != "") ? intval($_data['maxage']) : $is_now['maxage'];
  1694. $maxbytespersecond = (isset($_data['maxbytespersecond']) && $_data['maxbytespersecond'] != "") ? intval($_data['maxbytespersecond']) : $is_now['maxbytespersecond'];
  1695. $timeout1 = (isset($_data['timeout1']) && $_data['timeout1'] != "") ? intval($_data['timeout1']) : $is_now['timeout1'];
  1696. $timeout2 = (isset($_data['timeout2']) && $_data['timeout2'] != "") ? intval($_data['timeout2']) : $is_now['timeout2'];
  1697. }
  1698. else {
  1699. $_SESSION['return'][] = array(
  1700. 'type' => 'danger',
  1701. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1702. 'msg' => 'access_denied'
  1703. );
  1704. continue;
  1705. }
  1706. if (strpos($custom_params, 'pipemess')) {
  1707. $custom_params = '';
  1708. }
  1709. if (empty($subfolder2)) {
  1710. $subfolder2 = "";
  1711. }
  1712. if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  1713. $maxage = "0";
  1714. }
  1715. if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  1716. $timeout1 = "600";
  1717. }
  1718. if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  1719. $timeout2 = "600";
  1720. }
  1721. if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
  1722. $maxbytespersecond = "0";
  1723. }
  1724. if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
  1725. $_SESSION['return'][] = array(
  1726. 'type' => 'danger',
  1727. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1728. 'msg' => 'access_denied'
  1729. );
  1730. continue;
  1731. }
  1732. if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
  1733. $_SESSION['return'][] = array(
  1734. 'type' => 'danger',
  1735. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1736. 'msg' => 'access_denied'
  1737. );
  1738. continue;
  1739. }
  1740. if (!is_valid_domain_name($host1)) {
  1741. $_SESSION['return'][] = array(
  1742. 'type' => 'danger',
  1743. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1744. 'msg' => 'access_denied'
  1745. );
  1746. continue;
  1747. }
  1748. if ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
  1749. $_SESSION['return'][] = array(
  1750. 'type' => 'danger',
  1751. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1752. 'msg' => 'access_denied'
  1753. );
  1754. continue;
  1755. }
  1756. if (@preg_match("/" . $exclude . "/", null) === false) {
  1757. $_SESSION['return'][] = array(
  1758. 'type' => 'danger',
  1759. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1760. 'msg' => 'access_denied'
  1761. );
  1762. continue;
  1763. }
  1764. $stmt = $pdo->prepare("UPDATE `imapsync` SET `delete1` = :delete1,
  1765. `delete2` = :delete2,
  1766. `automap` = :automap,
  1767. `skipcrossduplicates` = :skipcrossduplicates,
  1768. `maxage` = :maxage,
  1769. `maxbytespersecond` = :maxbytespersecond,
  1770. `subfolder2` = :subfolder2,
  1771. `exclude` = :exclude,
  1772. `host1` = :host1,
  1773. `last_run` = :last_run,
  1774. `user1` = :user1,
  1775. `password1` = :password1,
  1776. `mins_interval` = :mins_interval,
  1777. `port1` = :port1,
  1778. `enc1` = :enc1,
  1779. `delete2duplicates` = :delete2duplicates,
  1780. `custom_params` = :custom_params,
  1781. `timeout1` = :timeout1,
  1782. `timeout2` = :timeout2,
  1783. `subscribeall` = :subscribeall,
  1784. `active` = :active
  1785. WHERE `id` = :id");
  1786. $stmt->execute(array(
  1787. ':delete1' => $delete1,
  1788. ':delete2' => $delete2,
  1789. ':automap' => $automap,
  1790. ':skipcrossduplicates' => $skipcrossduplicates,
  1791. ':id' => $id,
  1792. ':exclude' => $exclude,
  1793. ':maxage' => $maxage,
  1794. ':maxbytespersecond' => $maxbytespersecond,
  1795. ':subfolder2' => $subfolder2,
  1796. ':host1' => $host1,
  1797. ':user1' => $user1,
  1798. ':password1' => $password1,
  1799. ':last_run' => $last_run,
  1800. ':mins_interval' => $mins_interval,
  1801. ':port1' => $port1,
  1802. ':enc1' => $enc1,
  1803. ':delete2duplicates' => $delete2duplicates,
  1804. ':custom_params' => $custom_params,
  1805. ':timeout1' => $timeout1,
  1806. ':timeout2' => $timeout2,
  1807. ':subscribeall' => $subscribeall,
  1808. ':active' => $active,
  1809. ));
  1810. $_SESSION['return'][] = array(
  1811. 'type' => 'success',
  1812. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1813. 'msg' => array('mailbox_modified', $username)
  1814. );
  1815. }
  1816. break;
  1817. case 'filter':
  1818. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  1819. $_SESSION['return'][] = array(
  1820. 'type' => 'danger',
  1821. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1822. 'msg' => 'access_denied'
  1823. );
  1824. return false;
  1825. }
  1826. $sieve = new Sieve\SieveParser();
  1827. if (!is_array($_data['id'])) {
  1828. $ids = array();
  1829. $ids[] = $_data['id'];
  1830. }
  1831. else {
  1832. $ids = $_data['id'];
  1833. }
  1834. foreach ($ids as $id) {
  1835. $is_now = mailbox('get', 'filter_details', $id);
  1836. if (!empty($is_now)) {
  1837. $username = $is_now['username'];
  1838. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  1839. $script_desc = (!empty($_data['script_desc'])) ? $_data['script_desc'] : $is_now['script_desc'];
  1840. $script_data = (!empty($_data['script_data'])) ? $_data['script_data'] : $is_now['script_data'];
  1841. $filter_type = (!empty($_data['filter_type'])) ? $_data['filter_type'] : $is_now['filter_type'];
  1842. }
  1843. else {
  1844. $_SESSION['return'][] = array(
  1845. 'type' => 'danger',
  1846. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1847. 'msg' => 'access_denied'
  1848. );
  1849. continue;
  1850. }
  1851. try {
  1852. $sieve->parse($script_data);
  1853. }
  1854. catch (Exception $e) {
  1855. $_SESSION['return'][] = array(
  1856. 'type' => 'danger',
  1857. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1858. 'msg' => array('sieve_error', $e->getMessage())
  1859. );
  1860. continue;
  1861. }
  1862. if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
  1863. $_SESSION['return'][] = array(
  1864. 'type' => 'danger',
  1865. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1866. 'msg' => 'filter_type'
  1867. );
  1868. continue;
  1869. }
  1870. if ($active == '1') {
  1871. $script_name = 'active';
  1872. $stmt = $pdo->prepare("UPDATE `sieve_filters`
  1873. SET `script_name` = 'inactive'
  1874. WHERE `username` = :username
  1875. AND `filter_type` = :filter_type");
  1876. $stmt->execute(array(
  1877. ':username' => $username,
  1878. ':filter_type' => $filter_type
  1879. ));
  1880. }
  1881. else {
  1882. $script_name = 'inactive';
  1883. }
  1884. $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_desc` = :script_desc, `script_data` = :script_data, `script_name` = :script_name, `filter_type` = :filter_type
  1885. WHERE `id` = :id");
  1886. $stmt->execute(array(
  1887. ':script_desc' => $script_desc,
  1888. ':script_data' => $script_data,
  1889. ':script_name' => $script_name,
  1890. ':filter_type' => $filter_type,
  1891. ':id' => $id
  1892. ));
  1893. $_SESSION['return'][] = array(
  1894. 'type' => 'success',
  1895. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1896. 'msg' => array('mailbox_modified', $username)
  1897. );
  1898. }
  1899. break;
  1900. case 'alias':
  1901. if (!is_array($_data['id'])) {
  1902. $ids = array();
  1903. $ids[] = $_data['id'];
  1904. }
  1905. else {
  1906. $ids = $_data['id'];
  1907. }
  1908. foreach ($ids as $id) {
  1909. $is_now = mailbox('get', 'alias_details', $id);
  1910. if (!empty($is_now)) {
  1911. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  1912. $sogo_visible = (isset($_data['sogo_visible'])) ? intval($_data['sogo_visible']) : $is_now['sogo_visible'];
  1913. $goto_null = (isset($_data['goto_null'])) ? intval($_data['goto_null']) : 0;
  1914. $goto_spam = (isset($_data['goto_spam'])) ? intval($_data['goto_spam']) : 0;
  1915. $goto_ham = (isset($_data['goto_ham'])) ? intval($_data['goto_ham']) : 0;
  1916. $public_comment = (isset($_data['public_comment'])) ? $_data['public_comment'] : $is_now['public_comment'];
  1917. $private_comment = (isset($_data['private_comment'])) ? $_data['private_comment'] : $is_now['private_comment'];
  1918. $goto = (!empty($_data['goto'])) ? $_data['goto'] : $is_now['goto'];
  1919. $address = (!empty($_data['address'])) ? $_data['address'] : $is_now['address'];
  1920. }
  1921. else {
  1922. $_SESSION['return'][] = array(
  1923. 'type' => 'danger',
  1924. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1925. 'msg' => array('alias_invalid', $address)
  1926. );
  1927. continue;
  1928. }
  1929. $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  1930. if ($is_now['address'] != $address) {
  1931. $local_part = strstr($address, '@', true);
  1932. $address = $local_part.'@'.$domain;
  1933. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  1934. $_SESSION['return'][] = array(
  1935. 'type' => 'danger',
  1936. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1937. 'msg' => 'access_denied'
  1938. );
  1939. continue;
  1940. }
  1941. if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
  1942. $_SESSION['return'][] = array(
  1943. 'type' => 'danger',
  1944. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1945. 'msg' => array('alias_invalid', $address)
  1946. );
  1947. continue;
  1948. }
  1949. if (strtolower($is_now['address']) != strtolower($address)) {
  1950. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  1951. WHERE `address`= :address OR `address` IN (
  1952. SELECT `username` FROM `mailbox`, `alias_domain`
  1953. WHERE (
  1954. `alias_domain`.`alias_domain` = :address_d
  1955. AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
  1956. $stmt->execute(array(
  1957. ':address' => $address,
  1958. ':address_l' => $local_part,
  1959. ':address_d' => $domain
  1960. ));
  1961. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1962. if ($num_results != 0) {
  1963. $_SESSION['return'][] = array(
  1964. 'type' => 'danger',
  1965. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1966. 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
  1967. );
  1968. continue;
  1969. }
  1970. }
  1971. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  1972. WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
  1973. $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
  1974. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1975. if ($num_results == 0) {
  1976. $_SESSION['return'][] = array(
  1977. 'type' => 'danger',
  1978. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1979. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  1980. );
  1981. continue;
  1982. }
  1983. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
  1984. WHERE `address`= :address");
  1985. $stmt->execute(array(':address' => $address));
  1986. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1987. if ($num_results != 0) {
  1988. $_SESSION['return'][] = array(
  1989. 'type' => 'danger',
  1990. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1991. 'msg' => array('is_spam_alias', htmlspecialchars($address))
  1992. );
  1993. continue;
  1994. }
  1995. }
  1996. if ($goto_null == "1") {
  1997. $goto = "null@localhost";
  1998. }
  1999. elseif ($goto_spam == "1") {
  2000. $goto = "spam@localhost";
  2001. }
  2002. elseif ($goto_ham == "1") {
  2003. $goto = "ham@localhost";
  2004. }
  2005. else {
  2006. $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $goto));
  2007. foreach ($gotos as $i => &$goto) {
  2008. if (empty($goto)) {
  2009. continue;
  2010. }
  2011. if (!filter_var($goto, FILTER_VALIDATE_EMAIL)) {
  2012. $_SESSION['return'][] = array(
  2013. 'type' => 'danger',
  2014. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2015. 'msg' => array('goto_invalid', $goto)
  2016. );
  2017. unset($gotos[$i]);
  2018. continue;
  2019. }
  2020. if ($goto == $address) {
  2021. $_SESSION['return'][] = array(
  2022. 'type' => 'danger',
  2023. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2024. 'msg' => 'alias_goto_identical'
  2025. );
  2026. unset($gotos[$i]);
  2027. continue;
  2028. }
  2029. // Delete from sender_acl to prevent duplicates
  2030. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE
  2031. `logged_in_as` = :goto AND
  2032. `send_as` = :address");
  2033. $stmt->execute(array(
  2034. ':goto' => $goto,
  2035. ':address' => $address
  2036. ));
  2037. }
  2038. $gotos = array_filter($gotos);
  2039. $goto = implode(",", $gotos);
  2040. }
  2041. if (!empty($goto)) {
  2042. $stmt = $pdo->prepare("UPDATE `alias` SET
  2043. `address` = :address,
  2044. `public_comment` = :public_comment,
  2045. `private_comment` = :private_comment,
  2046. `domain` = :domain,
  2047. `goto` = :goto,
  2048. `sogo_visible`= :sogo_visible,
  2049. `active`= :active
  2050. WHERE `id` = :id");
  2051. $stmt->execute(array(
  2052. ':address' => $address,
  2053. ':public_comment' => $public_comment,
  2054. ':private_comment' => $private_comment,
  2055. ':domain' => $domain,
  2056. ':goto' => $goto,
  2057. ':sogo_visible' => $sogo_visible,
  2058. ':active' => $active,
  2059. ':id' => $is_now['id']
  2060. ));
  2061. }
  2062. $_SESSION['return'][] = array(
  2063. 'type' => 'success',
  2064. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2065. 'msg' => array('alias_modified', htmlspecialchars($address))
  2066. );
  2067. }
  2068. break;
  2069. case 'domain':
  2070. if (!is_array($_data['domain'])) {
  2071. $domains = array();
  2072. $domains[] = $_data['domain'];
  2073. }
  2074. else {
  2075. $domains = $_data['domain'];
  2076. }
  2077. foreach ($domains as $domain) {
  2078. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  2079. if (!is_valid_domain_name($domain)) {
  2080. $_SESSION['return'][] = array(
  2081. 'type' => 'danger',
  2082. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2083. 'msg' => 'domain_invalid'
  2084. );
  2085. continue;
  2086. }
  2087. if ($_SESSION['mailcow_cc_role'] == "domainadmin" &&
  2088. hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2089. $is_now = mailbox('get', 'domain_details', $domain);
  2090. if (!empty($is_now)) {
  2091. $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
  2092. $description = (!empty($_data['description']) && isset($_SESSION['acl']['domain_desc']) && $_SESSION['acl']['domain_desc'] == "1") ? $_data['description'] : $is_now['description'];
  2093. }
  2094. else {
  2095. $_SESSION['return'][] = array(
  2096. 'type' => 'danger',
  2097. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2098. 'msg' => 'domain_invalid'
  2099. );
  2100. continue;
  2101. }
  2102. $stmt = $pdo->prepare("UPDATE `domain` SET
  2103. `description` = :description,
  2104. `gal` = :gal
  2105. WHERE `domain` = :domain");
  2106. $stmt->execute(array(
  2107. ':description' => $description,
  2108. ':gal' => $gal,
  2109. ':domain' => $domain
  2110. ));
  2111. $_SESSION['return'][] = array(
  2112. 'type' => 'success',
  2113. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2114. 'msg' => array('domain_modified', htmlspecialchars($domain))
  2115. );
  2116. }
  2117. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  2118. $is_now = mailbox('get', 'domain_details', $domain);
  2119. if (!empty($is_now)) {
  2120. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2121. $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx'];
  2122. $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
  2123. $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients'];
  2124. $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $is_now['relay_unknown_only'];
  2125. $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
  2126. $aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
  2127. $mailboxes = (isset($_data['mailboxes']) && $_data['mailboxes'] != '') ? intval($_data['mailboxes']) : $is_now['max_num_mboxes_for_domain'];
  2128. $defquota = (isset($_data['defquota']) && $_data['defquota'] != '') ? intval($_data['defquota']) : ($is_now['def_quota_for_mbox'] / 1048576);
  2129. $maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
  2130. $quota = (!empty($_data['quota'])) ? $_data['quota'] : ($is_now['max_quota_for_domain'] / 1048576);
  2131. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  2132. if ($relay_all_recipients == '1') {
  2133. $backupmx = '1';
  2134. }
  2135. if ($relay_unknown_only == '1') {
  2136. $backupmx = '1';
  2137. $relay_all_recipients = '1';
  2138. }
  2139. }
  2140. else {
  2141. $_SESSION['return'][] = array(
  2142. 'type' => 'danger',
  2143. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2144. 'msg' => 'domain_invalid'
  2145. );
  2146. continue;
  2147. }
  2148. // todo: should be using api here
  2149. $stmt = $pdo->prepare("SELECT
  2150. COUNT(*) AS count,
  2151. MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
  2152. COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
  2153. FROM `mailbox`
  2154. WHERE `kind` NOT REGEXP 'location|thing|group'
  2155. AND domain = :domain");
  2156. $stmt->execute(array(':domain' => $domain));
  2157. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  2158. // todo: should be using api here
  2159. $stmt = $pdo->prepare("SELECT COUNT(*) AS `count` FROM `alias`
  2160. WHERE domain = :domain
  2161. AND address NOT IN (
  2162. SELECT `username` FROM `mailbox`
  2163. )");
  2164. $stmt->execute(array(':domain' => $domain));
  2165. $AliasData = $stmt->fetch(PDO::FETCH_ASSOC);
  2166. if ($defquota > $maxquota) {
  2167. $_SESSION['return'][] = array(
  2168. 'type' => 'danger',
  2169. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2170. 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
  2171. );
  2172. continue;
  2173. }
  2174. if ($defquota == "0" || empty($defquota)) {
  2175. $_SESSION['return'][] = array(
  2176. 'type' => 'danger',
  2177. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2178. 'msg' => 'defquota_empty'
  2179. );
  2180. continue;
  2181. }
  2182. if ($maxquota > $quota) {
  2183. $_SESSION['return'][] = array(
  2184. 'type' => 'danger',
  2185. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2186. 'msg' => 'mailbox_quota_exceeds_domain_quota'
  2187. );
  2188. continue;
  2189. }
  2190. if ($maxquota == "0" || empty($maxquota)) {
  2191. $_SESSION['return'][] = array(
  2192. 'type' => 'danger',
  2193. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2194. 'msg' => 'maxquota_empty'
  2195. );
  2196. continue;
  2197. }
  2198. if ($MailboxData['biggest_mailbox'] > $maxquota) {
  2199. $_SESSION['return'][] = array(
  2200. 'type' => 'danger',
  2201. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2202. 'msg' => array('max_quota_in_use', $MailboxData['biggest_mailbox'])
  2203. );
  2204. continue;
  2205. }
  2206. if ($MailboxData['quota_all'] > $quota) {
  2207. $_SESSION['return'][] = array(
  2208. 'type' => 'danger',
  2209. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2210. 'msg' => array('domain_quota_m_in_use', $MailboxData['quota_all'])
  2211. );
  2212. continue;
  2213. }
  2214. if ($MailboxData['count'] > $mailboxes) {
  2215. $_SESSION['return'][] = array(
  2216. 'type' => 'danger',
  2217. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2218. 'msg' => array('mailboxes_in_use', $MailboxData['count'])
  2219. );
  2220. continue;
  2221. }
  2222. if ($AliasData['count'] > $aliases) {
  2223. $_SESSION['return'][] = array(
  2224. 'type' => 'danger',
  2225. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2226. 'msg' => array('aliases_in_use', $AliasData['count'])
  2227. );
  2228. continue;
  2229. }
  2230. $stmt = $pdo->prepare("UPDATE `domain` SET
  2231. `relay_all_recipients` = :relay_all_recipients,
  2232. `relay_unknown_only` = :relay_unknown_only,
  2233. `backupmx` = :backupmx,
  2234. `gal` = :gal,
  2235. `active` = :active,
  2236. `quota` = :quota,
  2237. `defquota` = :defquota,
  2238. `maxquota` = :maxquota,
  2239. `relayhost` = :relayhost,
  2240. `mailboxes` = :mailboxes,
  2241. `aliases` = :aliases,
  2242. `description` = :description
  2243. WHERE `domain` = :domain");
  2244. $stmt->execute(array(
  2245. ':relay_all_recipients' => $relay_all_recipients,
  2246. ':relay_unknown_only' => $relay_unknown_only,
  2247. ':backupmx' => $backupmx,
  2248. ':gal' => $gal,
  2249. ':active' => $active,
  2250. ':quota' => $quota,
  2251. ':defquota' => $defquota,
  2252. ':maxquota' => $maxquota,
  2253. ':relayhost' => $relayhost,
  2254. ':mailboxes' => $mailboxes,
  2255. ':aliases' => $aliases,
  2256. ':description' => $description,
  2257. ':domain' => $domain
  2258. ));
  2259. $_SESSION['return'][] = array(
  2260. 'type' => 'success',
  2261. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2262. 'msg' => array('domain_modified', htmlspecialchars($domain))
  2263. );
  2264. }
  2265. }
  2266. break;
  2267. case 'mailbox':
  2268. if (!is_array($_data['username'])) {
  2269. $usernames = array();
  2270. $usernames[] = $_data['username'];
  2271. }
  2272. else {
  2273. $usernames = $_data['username'];
  2274. }
  2275. foreach ($usernames as $username) {
  2276. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  2277. $_SESSION['return'][] = array(
  2278. 'type' => 'danger',
  2279. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2280. 'msg' => array('username_invalid', $username)
  2281. );
  2282. continue;
  2283. }
  2284. $is_now = mailbox('get', 'mailbox_details', $username);
  2285. if (isset($_data['protocol_access'])) {
  2286. $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  2287. $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  2288. $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  2289. }
  2290. if (!empty($is_now)) {
  2291. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2292. (int)$force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($is_now['attributes']['force_pw_update']);
  2293. (int)$sogo_access = (isset($_data['sogo_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['sogo_access']) : intval($is_now['attributes']['sogo_access']);
  2294. (int)$imap_access = (isset($_data['imap_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['imap_access']) : intval($is_now['attributes']['imap_access']);
  2295. (int)$pop3_access = (isset($_data['pop3_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['pop3_access']) : intval($is_now['attributes']['pop3_access']);
  2296. (int)$smtp_access = (isset($_data['smtp_access']) && isset($_SESSION['acl']['protocol_access']) && $_SESSION['acl']['protocol_access'] == "1") ? intval($_data['smtp_access']) : intval($is_now['attributes']['smtp_access']);
  2297. (int)$quota_m = (isset_has_content($_data['quota'])) ? intval($_data['quota']) : ($is_now['quota'] / 1048576);
  2298. $name = (!empty($_data['name'])) ? ltrim(rtrim($_data['name'], '>'), '<') : $is_now['name'];
  2299. $domain = $is_now['domain'];
  2300. $quota_b = $quota_m * 1048576;
  2301. $password = (!empty($_data['password'])) ? $_data['password'] : null;
  2302. $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
  2303. }
  2304. else {
  2305. $_SESSION['return'][] = array(
  2306. 'type' => 'danger',
  2307. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2308. 'msg' => 'access_denied'
  2309. );
  2310. continue;
  2311. }
  2312. // if already 0 == ok
  2313. if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && ($quota_m == 0 && $is_now['quota'] != 0)) {
  2314. $_SESSION['return'][] = array(
  2315. 'type' => 'danger',
  2316. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2317. 'msg' => 'unlimited_quota_acl'
  2318. );
  2319. return false;
  2320. }
  2321. $stmt = $pdo->prepare("SELECT `quota`, `maxquota`
  2322. FROM `domain`
  2323. WHERE `domain` = :domain");
  2324. $stmt->execute(array(':domain' => $domain));
  2325. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  2326. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2327. $_SESSION['return'][] = array(
  2328. 'type' => 'danger',
  2329. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2330. 'msg' => 'access_denied'
  2331. );
  2332. continue;
  2333. }
  2334. if ($quota_m > $DomainData['maxquota']) {
  2335. $_SESSION['return'][] = array(
  2336. 'type' => 'danger',
  2337. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2338. 'msg' => array('mailbox_quota_exceeded', $DomainData['maxquota'])
  2339. );
  2340. continue;
  2341. }
  2342. if (((($is_now['quota_used'] / 1048576) - $quota_m) + $quota_m) > $DomainData['quota']) {
  2343. $_SESSION['return'][] = array(
  2344. 'type' => 'danger',
  2345. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2346. 'msg' => array('mailbox_quota_left_exceeded', ($is_now['max_new_quota'] / 1048576))
  2347. );
  2348. continue;
  2349. }
  2350. $extra_acls = array();
  2351. if (isset($_data['extended_sender_acl'])) {
  2352. if (!isset($_SESSION['acl']['extend_sender_acl']) || $_SESSION['acl']['extend_sender_acl'] != "1" ) {
  2353. $_SESSION['return'][] = array(
  2354. 'type' => 'danger',
  2355. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2356. 'msg' => 'access_denied'
  2357. );
  2358. return false;
  2359. }
  2360. $extra_acls = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['extended_sender_acl']));
  2361. foreach ($extra_acls as $i => &$extra_acl) {
  2362. if (empty($extra_acl)) {
  2363. continue;
  2364. }
  2365. if (substr($extra_acl, 0, 1) === "@") {
  2366. $extra_acl = ltrim($extra_acl, '@');
  2367. }
  2368. if (!filter_var($extra_acl, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name($extra_acl)) {
  2369. $_SESSION['return'][] = array(
  2370. 'type' => 'danger',
  2371. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2372. 'msg' => array('extra_acl_invalid', htmlspecialchars($extra_acl))
  2373. );
  2374. unset($extra_acls[$i]);
  2375. continue;
  2376. }
  2377. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  2378. if (filter_var($extra_acl, FILTER_VALIDATE_EMAIL)) {
  2379. $extra_acl_domain = idn_to_ascii(substr(strstr($extra_acl, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  2380. if (in_array($extra_acl_domain, $domains)) {
  2381. $_SESSION['return'][] = array(
  2382. 'type' => 'danger',
  2383. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2384. 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
  2385. );
  2386. unset($extra_acls[$i]);
  2387. continue;
  2388. }
  2389. }
  2390. else {
  2391. if (in_array($extra_acl, $domains)) {
  2392. $_SESSION['return'][] = array(
  2393. 'type' => 'danger',
  2394. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2395. 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
  2396. );
  2397. unset($extra_acls[$i]);
  2398. continue;
  2399. }
  2400. $extra_acl = '@' . $extra_acl;
  2401. }
  2402. }
  2403. $extra_acls = array_filter($extra_acls);
  2404. $extra_acls = array_values($extra_acls);
  2405. $extra_acls = array_unique($extra_acls);
  2406. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `logged_in_as` = :username");
  2407. $stmt->execute(array(
  2408. ':username' => $username
  2409. ));
  2410. foreach ($extra_acls as $sender_acl_external) {
  2411. $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`, `external`)
  2412. VALUES (:sender_acl, :username, 1)");
  2413. $stmt->execute(array(
  2414. ':sender_acl' => $sender_acl_external,
  2415. ':username' => $username
  2416. ));
  2417. }
  2418. }
  2419. if (isset($_data['sender_acl'])) {
  2420. // Get sender_acl items set by admin
  2421. $sender_acl_admin = array_merge(
  2422. mailbox('get', 'sender_acl_handles', $username)['sender_acl_domains']['ro'],
  2423. mailbox('get', 'sender_acl_handles', $username)['sender_acl_addresses']['ro']
  2424. );
  2425. // Get sender_acl items from POST array
  2426. // Set sender_acl_domain_admin to empty array if sender_acl contains "default" to trigger a reset
  2427. // Delete records from sender_acl if sender_acl contains "*" and set to array("*")
  2428. $_data['sender_acl'] = (array)$_data['sender_acl'];
  2429. if (in_array("*", $_data['sender_acl'])) {
  2430. $sender_acl_domain_admin = array('*');
  2431. }
  2432. elseif (array("default") === $_data['sender_acl']) {
  2433. $sender_acl_domain_admin = array();
  2434. }
  2435. else {
  2436. if (array_search('default', $_data['sender_acl']) !== false){
  2437. unset($_data['sender_acl'][array_search('default', $_data['sender_acl'])]);
  2438. }
  2439. $sender_acl_domain_admin = $_data['sender_acl'];
  2440. }
  2441. if (!empty($sender_acl_domain_admin) || !empty($sender_acl_admin)) {
  2442. // Check items in POST array and skip invalid
  2443. foreach ($sender_acl_domain_admin as $key => $val) {
  2444. // Check for invalid domain or email format or not *
  2445. if (!filter_var($val, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name(ltrim($val, '@')) && $val != '*') {
  2446. $_SESSION['return'][] = array(
  2447. 'type' => 'danger',
  2448. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2449. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  2450. );
  2451. unset($sender_acl_domain_admin[$key]);
  2452. continue;
  2453. }
  2454. // Check if user has domain access (if object is domain)
  2455. $domain = ltrim($sender_acl_domain_admin[$key], '@');
  2456. if (is_valid_domain_name($domain)) {
  2457. // Check for- and skip non-mailcow domains
  2458. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  2459. if (!empty($domains)) {
  2460. if (!in_array($domain, $domains)) {
  2461. $_SESSION['return'][] = array(
  2462. 'type' => 'danger',
  2463. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2464. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  2465. );
  2466. unset($sender_acl_domain_admin[$key]);
  2467. continue;
  2468. }
  2469. }
  2470. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2471. $_SESSION['return'][] = array(
  2472. 'type' => 'danger',
  2473. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2474. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  2475. );
  2476. unset($sender_acl_domain_admin[$key]);
  2477. continue;
  2478. }
  2479. }
  2480. // Wildcard can only be used if role == admin
  2481. if ($val == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  2482. $_SESSION['return'][] = array(
  2483. 'type' => 'danger',
  2484. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2485. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  2486. );
  2487. unset($sender_acl_domain_admin[$key]);
  2488. continue;
  2489. }
  2490. // Check if user has alias access (if object is email)
  2491. if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
  2492. if (!hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $val)) {
  2493. $_SESSION['return'][] = array(
  2494. 'type' => 'danger',
  2495. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2496. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  2497. );
  2498. unset($sender_acl_domain_admin[$key]);
  2499. continue;
  2500. }
  2501. }
  2502. }
  2503. // Merge both arrays
  2504. $sender_acl_merged = array_merge($sender_acl_domain_admin, $sender_acl_admin);
  2505. // If merged array still contains "*", set it as only value
  2506. !in_array('*', $sender_acl_merged) ?: $sender_acl_merged = array('*');
  2507. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
  2508. $stmt->execute(array(
  2509. ':username' => $username
  2510. ));
  2511. $fixed_sender_aliases = mailbox('get', 'sender_acl_handles', $username)['fixed_sender_aliases'];
  2512. foreach ($sender_acl_merged as $sender_acl) {
  2513. $domain = ltrim($sender_acl, '@');
  2514. if (is_valid_domain_name($domain)) {
  2515. $sender_acl = '@' . $domain;
  2516. }
  2517. // Don't add if allowed by alias
  2518. if (in_array($sender_acl, $fixed_sender_aliases)) {
  2519. continue;
  2520. }
  2521. $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`)
  2522. VALUES (:sender_acl, :username)");
  2523. $stmt->execute(array(
  2524. ':sender_acl' => $sender_acl,
  2525. ':username' => $username
  2526. ));
  2527. }
  2528. }
  2529. else {
  2530. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
  2531. $stmt->execute(array(
  2532. ':username' => $username
  2533. ));
  2534. }
  2535. }
  2536. if (!empty($password) && !empty($password2)) {
  2537. if (!preg_match('/' . $GLOBALS['PASSWD_REGEP'] . '/', $password)) {
  2538. $_SESSION['return'][] = array(
  2539. 'type' => 'danger',
  2540. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2541. 'msg' => 'password_complexity'
  2542. );
  2543. continue;
  2544. }
  2545. if ($password != $password2) {
  2546. $_SESSION['return'][] = array(
  2547. 'type' => 'danger',
  2548. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2549. 'msg' => 'password_mismatch'
  2550. );
  2551. continue;
  2552. }
  2553. $password_hashed = hash_password($password);
  2554. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  2555. `password` = :password_hashed
  2556. WHERE `username` = :username");
  2557. $stmt->execute(array(
  2558. ':password_hashed' => $password_hashed,
  2559. ':username' => $username
  2560. ));
  2561. }
  2562. // We could either set alias = 1 if alias = 2 or tune the Postfix alias table (that's what we did, TODO: to it the other way)
  2563. $stmt = $pdo->prepare("UPDATE `alias` SET
  2564. `active` = :active
  2565. WHERE `address` = :address");
  2566. $stmt->execute(array(
  2567. ':address' => $username,
  2568. ':active' => $active
  2569. ));
  2570. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  2571. `active` = :active,
  2572. `name`= :name,
  2573. `quota` = :quota_b,
  2574. `attributes` = JSON_SET(`attributes`, '$.force_pw_update', :force_pw_update),
  2575. `attributes` = JSON_SET(`attributes`, '$.sogo_access', :sogo_access),
  2576. `attributes` = JSON_SET(`attributes`, '$.imap_access', :imap_access),
  2577. `attributes` = JSON_SET(`attributes`, '$.pop3_access', :pop3_access),
  2578. `attributes` = JSON_SET(`attributes`, '$.smtp_access', :smtp_access)
  2579. WHERE `username` = :username");
  2580. $stmt->execute(array(
  2581. ':active' => $active,
  2582. ':name' => $name,
  2583. ':quota_b' => $quota_b,
  2584. ':force_pw_update' => $force_pw_update,
  2585. ':sogo_access' => $sogo_access,
  2586. ':imap_access' => $imap_access,
  2587. ':pop3_access' => $pop3_access,
  2588. ':smtp_access' => $smtp_access,
  2589. ':username' => $username
  2590. ));
  2591. $_SESSION['return'][] = array(
  2592. 'type' => 'success',
  2593. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2594. 'msg' => array('mailbox_modified', $username)
  2595. );
  2596. }
  2597. break;
  2598. case 'resource':
  2599. if (!is_array($_data['name'])) {
  2600. $names = array();
  2601. $names[] = $_data['name'];
  2602. }
  2603. else {
  2604. $names = $_data['name'];
  2605. }
  2606. foreach ($names as $name) {
  2607. $is_now = mailbox('get', 'resource_details', $name);
  2608. if (!empty($is_now)) {
  2609. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2610. $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
  2611. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  2612. $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
  2613. }
  2614. else {
  2615. $_SESSION['return'][] = array(
  2616. 'type' => 'danger',
  2617. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2618. 'msg' => array('resource_invalid', htmlspecialchars($name))
  2619. );
  2620. continue;
  2621. }
  2622. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  2623. $_SESSION['return'][] = array(
  2624. 'type' => 'danger',
  2625. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2626. 'msg' => array('resource_invalid', htmlspecialchars($name))
  2627. );
  2628. continue;
  2629. }
  2630. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  2631. $multiple_bookings = -1;
  2632. }
  2633. if (empty($description)) {
  2634. $_SESSION['return'][] = array(
  2635. 'type' => 'danger',
  2636. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2637. 'msg' => array('description_invalid', htmlspecialchars($name))
  2638. );
  2639. continue;
  2640. }
  2641. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  2642. $_SESSION['return'][] = array(
  2643. 'type' => 'danger',
  2644. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2645. 'msg' => array('resource_invalid', htmlspecialchars($name))
  2646. );
  2647. continue;
  2648. }
  2649. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  2650. $_SESSION['return'][] = array(
  2651. 'type' => 'danger',
  2652. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2653. 'msg' => 'access_denied'
  2654. );
  2655. continue;
  2656. }
  2657. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  2658. `active` = :active,
  2659. `name`= :description,
  2660. `kind`= :kind,
  2661. `multiple_bookings`= :multiple_bookings
  2662. WHERE `username` = :name");
  2663. $stmt->execute(array(
  2664. ':active' => $active,
  2665. ':description' => $description,
  2666. ':multiple_bookings' => $multiple_bookings,
  2667. ':kind' => $kind,
  2668. ':name' => $name
  2669. ));
  2670. $_SESSION['return'][] = array(
  2671. 'type' => 'success',
  2672. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2673. 'msg' => array('resource_modified', htmlspecialchars($name))
  2674. );
  2675. }
  2676. break;
  2677. }
  2678. break;
  2679. case 'get':
  2680. switch ($_type) {
  2681. case 'sender_acl_handles':
  2682. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  2683. return false;
  2684. }
  2685. $data['sender_acl_domains']['ro'] = array();
  2686. $data['sender_acl_domains']['rw'] = array();
  2687. $data['sender_acl_domains']['selectable'] = array();
  2688. $data['sender_acl_addresses']['ro'] = array();
  2689. $data['sender_acl_addresses']['rw'] = array();
  2690. $data['sender_acl_addresses']['selectable'] = array();
  2691. $data['fixed_sender_aliases'] = array();
  2692. $data['external_sender_aliases'] = array();
  2693. // Fixed addresses
  2694. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
  2695. $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
  2696. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2697. while ($row = array_shift($rows)) {
  2698. $data['fixed_sender_aliases'][] = $row['address'];
  2699. }
  2700. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
  2701. WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
  2702. AND `mailbox`.`username` = :username");
  2703. $stmt->execute(array(':username' => $_data));
  2704. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2705. while ($row = array_shift($rows)) {
  2706. if (!empty($row['alias_domain_alias'])) {
  2707. $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
  2708. }
  2709. }
  2710. // External addresses
  2711. $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
  2712. $stmt->execute(array(':logged_in_as' => $_data));
  2713. $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2714. while ($row = array_shift($exernal_rows)) {
  2715. if (!empty($row['send_as_external'])) {
  2716. $data['external_sender_aliases'][] = $row['send_as_external'];
  2717. }
  2718. }
  2719. // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
  2720. // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
  2721. $stmt = $pdo->prepare("SELECT REPLACE(`send_as`, '@', '') AS `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '0' AND (`send_as` LIKE '@%' OR `send_as` = '*')");
  2722. $stmt->execute(array(':logged_in_as' => $_data));
  2723. $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2724. while ($domain_row = array_shift($domain_rows)) {
  2725. if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  2726. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  2727. continue;
  2728. }
  2729. if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  2730. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  2731. continue;
  2732. }
  2733. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  2734. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  2735. }
  2736. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  2737. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  2738. }
  2739. }
  2740. $stmt = $pdo->prepare("SELECT `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '0' AND (`send_as` NOT LIKE '@%' AND `send_as` != '*')");
  2741. $stmt->execute(array(':logged_in_as' => $_data));
  2742. $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2743. while ($address_row = array_shift($address_rows)) {
  2744. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  2745. $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
  2746. continue;
  2747. }
  2748. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  2749. $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
  2750. continue;
  2751. }
  2752. }
  2753. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  2754. WHERE `domain` NOT IN (
  2755. SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
  2756. WHERE `logged_in_as` = :logged_in_as1
  2757. AND `external` = '0'
  2758. AND `send_as` LIKE '@%')
  2759. UNION
  2760. SELECT '*' FROM `domain`
  2761. WHERE '*' NOT IN (
  2762. SELECT `send_as` FROM `sender_acl`
  2763. WHERE `logged_in_as` = :logged_in_as2
  2764. AND `external` = '0'
  2765. )");
  2766. $stmt->execute(array(
  2767. ':logged_in_as1' => $_data,
  2768. ':logged_in_as2' => $_data
  2769. ));
  2770. $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2771. while ($row_domain = array_shift($rows_domain)) {
  2772. if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
  2773. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  2774. continue;
  2775. }
  2776. if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  2777. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  2778. continue;
  2779. }
  2780. }
  2781. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  2782. WHERE `goto` != :goto
  2783. AND `address` NOT IN (
  2784. SELECT `send_as` FROM `sender_acl`
  2785. WHERE `logged_in_as` = :logged_in_as
  2786. AND `external` = '0'
  2787. AND `send_as` NOT LIKE '@%')");
  2788. $stmt->execute(array(
  2789. ':logged_in_as' => $_data,
  2790. ':goto' => $_data
  2791. ));
  2792. $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2793. while ($row = array_shift($rows_mbox)) {
  2794. // Aliases are not selectable
  2795. if (in_array($row['address'], $data['fixed_sender_aliases'])) {
  2796. continue;
  2797. }
  2798. if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
  2799. $data['sender_acl_addresses']['selectable'][] = $row['address'];
  2800. }
  2801. }
  2802. return $data;
  2803. break;
  2804. case 'mailboxes':
  2805. $mailboxes = array();
  2806. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2807. return false;
  2808. }
  2809. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2810. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` NOT REGEXP 'location|thing|group' AND `domain` = :domain");
  2811. $stmt->execute(array(
  2812. ':domain' => $_data,
  2813. ));
  2814. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2815. while($row = array_shift($rows)) {
  2816. $mailboxes[] = $row['username'];
  2817. }
  2818. }
  2819. else {
  2820. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` NOT REGEXP 'location|thing|group' AND (`domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role)");
  2821. $stmt->execute(array(
  2822. ':username' => $_SESSION['mailcow_cc_username'],
  2823. ':role' => $_SESSION['mailcow_cc_role'],
  2824. ));
  2825. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2826. while($row = array_shift($rows)) {
  2827. $mailboxes[] = $row['username'];
  2828. }
  2829. }
  2830. return $mailboxes;
  2831. break;
  2832. case 'tls_policy':
  2833. $attrs = array();
  2834. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  2835. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2836. return false;
  2837. }
  2838. }
  2839. else {
  2840. $_data = $_SESSION['mailcow_cc_username'];
  2841. }
  2842. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  2843. $stmt->execute(array(':username' => $_data));
  2844. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  2845. $attrs = json_decode($attrs['attributes'], true);
  2846. return array(
  2847. 'tls_enforce_in' => $attrs['tls_enforce_in'],
  2848. 'tls_enforce_out' => $attrs['tls_enforce_out']
  2849. );
  2850. break;
  2851. case 'quarantine_notification':
  2852. $attrs = array();
  2853. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  2854. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2855. return false;
  2856. }
  2857. }
  2858. else {
  2859. $_data = $_SESSION['mailcow_cc_username'];
  2860. }
  2861. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  2862. $stmt->execute(array(':username' => $_data));
  2863. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  2864. $attrs = json_decode($attrs['attributes'], true);
  2865. return $attrs['quarantine_notification'];
  2866. break;
  2867. case 'quarantine_category':
  2868. $attrs = array();
  2869. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  2870. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2871. return false;
  2872. }
  2873. }
  2874. else {
  2875. $_data = $_SESSION['mailcow_cc_username'];
  2876. }
  2877. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  2878. $stmt->execute(array(':username' => $_data));
  2879. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  2880. $attrs = json_decode($attrs['attributes'], true);
  2881. return $attrs['quarantine_category'];
  2882. break;
  2883. case 'filters':
  2884. $filters = array();
  2885. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  2886. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2887. return false;
  2888. }
  2889. }
  2890. else {
  2891. $_data = $_SESSION['mailcow_cc_username'];
  2892. }
  2893. $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
  2894. $stmt->execute(array(':username' => $_data));
  2895. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  2896. while($row = array_shift($rows)) {
  2897. $filters[] = $row['id'];
  2898. }
  2899. return $filters;
  2900. break;
  2901. case 'global_filter_details':
  2902. $global_filters = array();
  2903. if ($_SESSION['mailcow_cc_role'] != "admin") {
  2904. return false;
  2905. }
  2906. $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
  2907. $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
  2908. return $global_filters;
  2909. break;
  2910. case 'filter_details':
  2911. $filter_details = array();
  2912. if (!is_numeric($_data)) {
  2913. return false;
  2914. }
  2915. $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
  2916. id,
  2917. username,
  2918. filter_type,
  2919. script_data,
  2920. script_desc
  2921. FROM `sieve_filters`
  2922. WHERE `id` = :id");
  2923. $stmt->execute(array(':id' => $_data));
  2924. $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
  2925. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
  2926. return false;
  2927. }
  2928. return $filter_details;
  2929. break;
  2930. case 'active_user_sieve':
  2931. $filter_details = array();
  2932. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  2933. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  2934. return false;
  2935. }
  2936. }
  2937. else {
  2938. $_data = $_SESSION['mailcow_cc_username'];
  2939. }
  2940. $exec_fields = array(
  2941. 'cmd' => 'sieve',
  2942. 'task' => 'list',
  2943. 'username' => $_data
  2944. );
  2945. $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  2946. $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
  2947. foreach ($filters as $filter) {
  2948. if (preg_match('/.+ ACTIVE/i', $filter)) {
  2949. $exec_fields = array(
  2950. 'cmd' => 'sieve',
  2951. 'task' => 'print',
  2952. 'script_name' => substr($filter, 0, -7),
  2953. 'username' => $_data
  2954. );
  2955. $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  2956. // Remove first line
  2957. return preg_replace('/^.+\n/', '', $script);
  2958. }
  2959. }
  2960. return false;
  2961. break;
  2962. case 'syncjob_details':
  2963. $syncjobdetails = array();
  2964. if (!is_numeric($_data)) {
  2965. return false;
  2966. }
  2967. if (isset($_extra) && in_array('no_log', $_extra)) {
  2968. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
  2969. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  2970. while($field = array_shift($fields)) {
  2971. $shown_fields[] = $field['Field'];
  2972. }
  2973. $stmt = $pdo->prepare("SELECT " . implode(',', $shown_fields) . ",
  2974. `active`
  2975. FROM `imapsync` WHERE id = :id");
  2976. }
  2977. elseif (isset($_extra) && in_array('with_password', $_extra)) {
  2978. $stmt = $pdo->prepare("SELECT *,
  2979. `active`
  2980. FROM `imapsync` WHERE id = :id");
  2981. }
  2982. else {
  2983. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
  2984. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  2985. while($field = array_shift($fields)) {
  2986. $shown_fields[] = $field['Field'];
  2987. }
  2988. $stmt = $pdo->prepare("SELECT " . implode(',', $shown_fields) . ",
  2989. `active`
  2990. FROM `imapsync` WHERE id = :id");
  2991. }
  2992. $stmt->execute(array(':id' => $_data));
  2993. $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
  2994. if (!empty($syncjobdetails['returned_text'])) {
  2995. $syncjobdetails['log'] = $syncjobdetails['returned_text'];
  2996. }
  2997. else {
  2998. $syncjobdetails['log'] = '';
  2999. }
  3000. unset($syncjobdetails['returned_text']);
  3001. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
  3002. return false;
  3003. }
  3004. return $syncjobdetails;
  3005. break;
  3006. case 'syncjobs':
  3007. $syncjobdata = array();
  3008. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3009. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3010. return false;
  3011. }
  3012. }
  3013. else {
  3014. $_data = $_SESSION['mailcow_cc_username'];
  3015. }
  3016. $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
  3017. $stmt->execute(array(':username' => $_data));
  3018. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3019. while($row = array_shift($rows)) {
  3020. $syncjobdata[] = $row['id'];
  3021. }
  3022. return $syncjobdata;
  3023. break;
  3024. case 'spam_score':
  3025. $curl = curl_init();
  3026. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  3027. curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
  3028. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  3029. $default_actions = curl_exec($curl);
  3030. if (!curl_errno($curl)) {
  3031. $data_array = json_decode($default_actions, true);
  3032. curl_close($curl);
  3033. foreach ($data_array as $data) {
  3034. if ($data['action'] == 'reject') {
  3035. $reject = $data['value'];
  3036. continue;
  3037. }
  3038. elseif ($data['action'] == 'add header') {
  3039. $add_header = $data['value'];
  3040. continue;
  3041. }
  3042. }
  3043. if (empty($add_header) || empty($reject)) {
  3044. // Assume default, set warning
  3045. $default = "5, 15";
  3046. $_SESSION['return'][] = array(
  3047. 'type' => 'warning',
  3048. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3049. 'msg' => 'Could not determine servers default spam score, assuming default'
  3050. );
  3051. }
  3052. else {
  3053. $default = $add_header . ', ' . $reject;
  3054. }
  3055. }
  3056. else {
  3057. // Assume default, set warning
  3058. $default = "5, 15";
  3059. $_SESSION['return'][] = array(
  3060. 'type' => 'warning',
  3061. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3062. 'msg' => 'Could not determine servers default spam score, assuming default'
  3063. );
  3064. }
  3065. curl_close($curl);
  3066. $policydata = array();
  3067. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3068. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3069. return false;
  3070. }
  3071. }
  3072. else {
  3073. $_data = $_SESSION['mailcow_cc_username'];
  3074. }
  3075. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
  3076. (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  3077. $stmt->execute(array(':username' => $_data));
  3078. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  3079. if (empty($num_results)) {
  3080. return $default;
  3081. }
  3082. else {
  3083. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
  3084. $stmt->execute(array(':username' => $_data));
  3085. $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  3086. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
  3087. $stmt->execute(array(':username' => $_data));
  3088. $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  3089. return $lowspamlevel['value'].', '.$highspamlevel['value'];
  3090. }
  3091. break;
  3092. case 'time_limited_aliases':
  3093. $tladata = array();
  3094. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3095. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3096. return false;
  3097. }
  3098. }
  3099. else {
  3100. $_data = $_SESSION['mailcow_cc_username'];
  3101. }
  3102. $stmt = $pdo->prepare("SELECT `address`,
  3103. `goto`,
  3104. `validity`
  3105. FROM `spamalias`
  3106. WHERE `goto` = :username
  3107. AND `validity` >= :unixnow");
  3108. $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
  3109. $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3110. return $tladata;
  3111. break;
  3112. case 'delimiter_action':
  3113. $policydata = array();
  3114. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3115. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3116. return false;
  3117. }
  3118. }
  3119. else {
  3120. $_data = $_SESSION['mailcow_cc_username'];
  3121. }
  3122. try {
  3123. if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
  3124. return "subject";
  3125. }
  3126. elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
  3127. return "subfolder";
  3128. }
  3129. else {
  3130. return "none";
  3131. }
  3132. }
  3133. catch (RedisException $e) {
  3134. $_SESSION['return'][] = array(
  3135. 'type' => 'danger',
  3136. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3137. 'msg' => array('redis_error', $e)
  3138. );
  3139. return false;
  3140. }
  3141. break;
  3142. case 'resources':
  3143. $resources = array();
  3144. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3145. return false;
  3146. }
  3147. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3148. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
  3149. $stmt->execute(array(
  3150. ':domain' => $_data,
  3151. ));
  3152. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3153. while($row = array_shift($rows)) {
  3154. $resources[] = $row['username'];
  3155. }
  3156. }
  3157. else {
  3158. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role");
  3159. $stmt->execute(array(
  3160. ':username' => $_SESSION['mailcow_cc_username'],
  3161. ':role' => $_SESSION['mailcow_cc_role'],
  3162. ));
  3163. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3164. while($row = array_shift($rows)) {
  3165. $resources[] = $row['username'];
  3166. }
  3167. }
  3168. return $resources;
  3169. break;
  3170. case 'alias_domains':
  3171. $aliasdomains = array();
  3172. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3173. return false;
  3174. }
  3175. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3176. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
  3177. $stmt->execute(array(
  3178. ':domain' => $_data,
  3179. ));
  3180. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3181. while($row = array_shift($rows)) {
  3182. $aliasdomains[] = $row['alias_domain'];
  3183. }
  3184. }
  3185. else {
  3186. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` IN (SELECT `domain` FROM `domain_admins` WHERE `active` = '1' AND `username` = :username) OR 'admin' = :role");
  3187. $stmt->execute(array(
  3188. ':username' => $_SESSION['mailcow_cc_username'],
  3189. ':role' => $_SESSION['mailcow_cc_role'],
  3190. ));
  3191. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3192. while($row = array_shift($rows)) {
  3193. $aliasdomains[] = $row['alias_domain'];
  3194. }
  3195. }
  3196. return $aliasdomains;
  3197. break;
  3198. case 'aliases':
  3199. $aliases = array();
  3200. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3201. return false;
  3202. }
  3203. $stmt = $pdo->prepare("SELECT `id` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
  3204. $stmt->execute(array(
  3205. ':domain' => $_data,
  3206. ));
  3207. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3208. while($row = array_shift($rows)) {
  3209. $aliases[] = $row['id'];
  3210. }
  3211. return $aliases;
  3212. break;
  3213. case 'alias_details':
  3214. $aliasdata = array();
  3215. $stmt = $pdo->prepare("SELECT
  3216. `id`,
  3217. `domain`,
  3218. `goto`,
  3219. `address`,
  3220. `public_comment`,
  3221. `private_comment`,
  3222. `active`,
  3223. `sogo_visible`,
  3224. `created`,
  3225. `modified`
  3226. FROM `alias`
  3227. WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
  3228. $stmt->execute(array(
  3229. ':id' => $_data,
  3230. ':address' => $_data,
  3231. ));
  3232. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3233. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  3234. $stmt->execute(array(
  3235. ':domain' => $row['domain'],
  3236. ));
  3237. $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
  3238. if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
  3239. $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
  3240. }
  3241. else {
  3242. $aliasdata['in_primary_domain'] = "";
  3243. }
  3244. $aliasdata['id'] = $row['id'];
  3245. $aliasdata['domain'] = $row['domain'];
  3246. $aliasdata['public_comment'] = $row['public_comment'];
  3247. $aliasdata['private_comment'] = $row['private_comment'];
  3248. $aliasdata['domain'] = $row['domain'];
  3249. $aliasdata['goto'] = $row['goto'];
  3250. $aliasdata['address'] = $row['address'];
  3251. (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
  3252. $aliasdata['active'] = $row['active'];
  3253. $aliasdata['active_int'] = $row['active'];
  3254. $aliasdata['sogo_visible'] = $row['sogo_visible'];
  3255. $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
  3256. $aliasdata['created'] = $row['created'];
  3257. $aliasdata['modified'] = $row['modified'];
  3258. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
  3259. return false;
  3260. }
  3261. return $aliasdata;
  3262. break;
  3263. case 'alias_domain_details':
  3264. $aliasdomaindata = array();
  3265. $rl = ratelimit('get', 'domain', $_data);
  3266. $stmt = $pdo->prepare("SELECT
  3267. `alias_domain`,
  3268. `target_domain`,
  3269. `active`,
  3270. `created`,
  3271. `modified`
  3272. FROM `alias_domain`
  3273. WHERE `alias_domain` = :aliasdomain");
  3274. $stmt->execute(array(
  3275. ':aliasdomain' => $_data,
  3276. ));
  3277. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3278. $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
  3279. $stmt->execute(array(
  3280. ':target_domain' => $row['target_domain']
  3281. ));
  3282. $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
  3283. $aliasdomaindata['alias_domain'] = $row['alias_domain'];
  3284. $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
  3285. $aliasdomaindata['target_domain'] = $row['target_domain'];
  3286. $aliasdomaindata['active'] = $row['active'];
  3287. $aliasdomaindata['active_int'] = $row['active'];
  3288. $aliasdomaindata['rl'] = $rl;
  3289. $aliasdomaindata['created'] = $row['created'];
  3290. $aliasdomaindata['modified'] = $row['modified'];
  3291. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
  3292. return false;
  3293. }
  3294. return $aliasdomaindata;
  3295. break;
  3296. case 'domains':
  3297. $domains = array();
  3298. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  3299. return false;
  3300. }
  3301. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  3302. WHERE (`domain` IN (
  3303. SELECT `domain` from `domain_admins`
  3304. WHERE (`active`='1' AND `username` = :username))
  3305. )
  3306. OR 'admin'= :role");
  3307. $stmt->execute(array(
  3308. ':username' => $_SESSION['mailcow_cc_username'],
  3309. ':role' => $_SESSION['mailcow_cc_role'],
  3310. ));
  3311. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3312. while($row = array_shift($rows)) {
  3313. $domains[] = $row['domain'];
  3314. }
  3315. return $domains;
  3316. break;
  3317. case 'domain_details':
  3318. $domaindata = array();
  3319. $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  3320. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3321. return false;
  3322. }
  3323. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  3324. $stmt->execute(array(
  3325. ':domain' => $_data
  3326. ));
  3327. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3328. if (!empty($row)) {
  3329. $_data = $row['target_domain'];
  3330. }
  3331. $stmt = $pdo->prepare("SELECT
  3332. `domain`,
  3333. `description`,
  3334. `aliases`,
  3335. `mailboxes`,
  3336. `defquota`,
  3337. `maxquota`,
  3338. `quota`,
  3339. `relayhost`,
  3340. `relay_all_recipients`,
  3341. `relay_unknown_only`,
  3342. `backupmx`,
  3343. `gal`,
  3344. `active`
  3345. FROM `domain` WHERE `domain`= :domain");
  3346. $stmt->execute(array(
  3347. ':domain' => $_data
  3348. ));
  3349. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3350. if (empty($row)) {
  3351. return false;
  3352. }
  3353. $stmt = $pdo->prepare("SELECT COUNT(*) AS `count`,
  3354. COALESCE(SUM(`quota`), 0) AS `in_use`
  3355. FROM `mailbox`
  3356. WHERE `kind` NOT REGEXP 'location|thing|group'
  3357. AND `domain` = :domain");
  3358. $stmt->execute(array(':domain' => $row['domain']));
  3359. $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  3360. $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
  3361. WHERE `username` IN (
  3362. SELECT `username` FROM `mailbox`
  3363. WHERE `domain` = :domain
  3364. );");
  3365. $stmt->execute(array(':domain' => $row['domain']));
  3366. $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
  3367. $rl = ratelimit('get', 'domain', $_data);
  3368. $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
  3369. if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
  3370. $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
  3371. }
  3372. $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
  3373. if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
  3374. $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
  3375. }
  3376. $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
  3377. if (!empty($SumQuotaInUse['bytes_total'])) {
  3378. $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
  3379. }
  3380. else {
  3381. $domaindata['bytes_total'] = 0;
  3382. }
  3383. if (!empty($SumQuotaInUse['msgs_total'])) {
  3384. $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
  3385. }
  3386. else {
  3387. $domaindata['msgs_total'] = 0;
  3388. }
  3389. $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
  3390. $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
  3391. $domaindata['domain_name'] = $row['domain'];
  3392. $domaindata['description'] = $row['description'];
  3393. $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
  3394. $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
  3395. $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
  3396. $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
  3397. $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
  3398. $domaindata['relayhost'] = $row['relayhost'];
  3399. $domaindata['backupmx'] = $row['backupmx'];
  3400. $domaindata['backupmx_int'] = $row['backupmx'];
  3401. $domaindata['gal'] = $row['gal'];
  3402. $domaindata['gal_int'] = $row['gal'];
  3403. $domaindata['rl'] = $rl;
  3404. $domaindata['active'] = $row['active'];
  3405. $domaindata['active_int'] = $row['active'];
  3406. $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
  3407. $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
  3408. $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
  3409. $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
  3410. $stmt = $pdo->prepare("SELECT COUNT(*) AS `alias_count` FROM `alias`
  3411. WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
  3412. AND `address` NOT IN (
  3413. SELECT `username` FROM `mailbox`
  3414. )");
  3415. $stmt->execute(array(
  3416. ':domain' => $_data,
  3417. ':domain2' => $_data
  3418. ));
  3419. $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  3420. (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
  3421. $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
  3422. if ($_SESSION['mailcow_cc_role'] == "admin")
  3423. {
  3424. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
  3425. $stmt->execute(array(
  3426. ':domain' => $_data
  3427. ));
  3428. $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
  3429. (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
  3430. }
  3431. return $domaindata;
  3432. break;
  3433. case 'mailbox_details':
  3434. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3435. return false;
  3436. }
  3437. $mailboxdata = array();
  3438. $rl = ratelimit('get', 'mailbox', $_data);
  3439. $last_imap_login = $redis->Get('last-login/imap/' . $_data);
  3440. $last_smtp_login = $redis->Get('last-login/smtp/' . $_data);
  3441. $last_pop3_login = $redis->Get('last-login/pop3/' . $_data);
  3442. if ($last_imap_login === false || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  3443. $last_imap_login = '0';
  3444. }
  3445. if ($last_smtp_login === false || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  3446. $last_smtp_login = '0';
  3447. }
  3448. if ($last_pop3_login === false || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  3449. $last_pop3_login = '0';
  3450. }
  3451. if (preg_match('/y|yes/i', getenv('MASTER'))) {
  3452. $stmt = $pdo->prepare("SELECT
  3453. `domain`.`backupmx`,
  3454. `mailbox`.`username`,
  3455. `mailbox`.`name`,
  3456. `mailbox`.`active`,
  3457. `mailbox`.`domain`,
  3458. `mailbox`.`local_part`,
  3459. `mailbox`.`quota`,
  3460. `quota2`.`bytes`,
  3461. `attributes`,
  3462. `quota2`.`messages`
  3463. FROM `mailbox`, `quota2`, `domain`
  3464. WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group' AND `mailbox`.`username` = `quota2`.`username` AND `domain`.`domain` = `mailbox`.`domain` AND `mailbox`.`username` = :mailbox");
  3465. }
  3466. else {
  3467. $stmt = $pdo->prepare("SELECT
  3468. `domain`.`backupmx`,
  3469. `mailbox`.`username`,
  3470. `mailbox`.`name`,
  3471. `mailbox`.`active`,
  3472. `mailbox`.`domain`,
  3473. `mailbox`.`local_part`,
  3474. `mailbox`.`quota`,
  3475. `quota2replica`.`bytes`,
  3476. `attributes`,
  3477. `quota2replica`.`messages`
  3478. FROM `mailbox`, `quota2replica`, `domain`
  3479. WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group' AND `mailbox`.`username` = `quota2replica`.`username` AND `domain`.`domain` = `mailbox`.`domain` AND `mailbox`.`username` = :mailbox");
  3480. }
  3481. $stmt->execute(array(
  3482. ':mailbox' => $_data,
  3483. ));
  3484. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3485. $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
  3486. $stmt->execute(array(':domain' => $row['domain']));
  3487. $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
  3488. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
  3489. $stmt->execute(array(':username' => $_data));
  3490. $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
  3491. $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE `kind` NOT REGEXP 'location|thing|group' AND `domain` = :domain AND `username` != :username");
  3492. $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
  3493. $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  3494. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
  3495. $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
  3496. $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  3497. $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
  3498. if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
  3499. $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
  3500. }
  3501. $mailboxdata['username'] = $row['username'];
  3502. if (!empty($rl)) {
  3503. $mailboxdata['rl'] = $rl;
  3504. $mailboxdata['rl_scope'] = 'mailbox';
  3505. }
  3506. else {
  3507. $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
  3508. $mailboxdata['rl_scope'] = 'domain';
  3509. }
  3510. $mailboxdata['is_relayed'] = $row['backupmx'];
  3511. $mailboxdata['name'] = $row['name'];
  3512. $mailboxdata['last_imap_login'] = $last_imap_login;
  3513. $mailboxdata['last_smtp_login'] = $last_smtp_login;
  3514. $mailboxdata['last_pop3_login'] = $last_pop3_login;
  3515. $mailboxdata['active'] = $row['active'];
  3516. $mailboxdata['active_int'] = $row['active'];
  3517. $mailboxdata['domain'] = $row['domain'];
  3518. $mailboxdata['local_part'] = $row['local_part'];
  3519. $mailboxdata['quota'] = $row['quota'];
  3520. $mailboxdata['attributes'] = json_decode($row['attributes'], true);
  3521. $mailboxdata['quota_used'] = intval($row['bytes']);
  3522. $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
  3523. $mailboxdata['messages'] = $row['messages'];
  3524. $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
  3525. $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
  3526. if ($mailboxdata['percent_in_use'] === '- ') {
  3527. $mailboxdata['percent_class'] = "info";
  3528. }
  3529. elseif ($mailboxdata['percent_in_use'] >= 90) {
  3530. $mailboxdata['percent_class'] = "danger";
  3531. }
  3532. elseif ($mailboxdata['percent_in_use'] >= 75) {
  3533. $mailboxdata['percent_class'] = "warning";
  3534. }
  3535. else {
  3536. $mailboxdata['percent_class'] = "success";
  3537. }
  3538. return $mailboxdata;
  3539. break;
  3540. case 'resource_details':
  3541. $resourcedata = array();
  3542. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3543. return false;
  3544. }
  3545. $stmt = $pdo->prepare("SELECT
  3546. `username`,
  3547. `name`,
  3548. `kind`,
  3549. `multiple_bookings`,
  3550. `local_part`,
  3551. `active`,
  3552. `domain`
  3553. FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
  3554. $stmt->execute(array(
  3555. ':resource' => $_data,
  3556. ));
  3557. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3558. $resourcedata['name'] = $row['username'];
  3559. $resourcedata['kind'] = $row['kind'];
  3560. $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
  3561. $resourcedata['description'] = $row['name'];
  3562. $resourcedata['active'] = $row['active'];
  3563. $resourcedata['active_int'] = $row['active'];
  3564. $resourcedata['domain'] = $row['domain'];
  3565. $resourcedata['local_part'] = $row['local_part'];
  3566. if (!isset($resourcedata['domain']) ||
  3567. (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
  3568. return false;
  3569. }
  3570. return $resourcedata;
  3571. break;
  3572. }
  3573. break;
  3574. case 'delete':
  3575. switch ($_type) {
  3576. case 'syncjob':
  3577. if (!is_array($_data['id'])) {
  3578. $ids = array();
  3579. $ids[] = $_data['id'];
  3580. }
  3581. else {
  3582. $ids = $_data['id'];
  3583. }
  3584. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  3585. $_SESSION['return'][] = array(
  3586. 'type' => 'danger',
  3587. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3588. 'msg' => 'access_denied'
  3589. );
  3590. return false;
  3591. }
  3592. foreach ($ids as $id) {
  3593. if (!is_numeric($id)) {
  3594. return false;
  3595. }
  3596. $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
  3597. $stmt->execute(array(':id' => $id));
  3598. $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
  3599. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
  3600. $_SESSION['return'][] = array(
  3601. 'type' => 'danger',
  3602. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3603. 'msg' => 'access_denied'
  3604. );
  3605. continue;
  3606. }
  3607. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
  3608. $stmt->execute(array(':id' => $id));
  3609. $_SESSION['return'][] = array(
  3610. 'type' => 'success',
  3611. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3612. 'msg' => array('deleted_syncjob', $id)
  3613. );
  3614. }
  3615. break;
  3616. case 'filter':
  3617. if (!is_array($_data['id'])) {
  3618. $ids = array();
  3619. $ids[] = $_data['id'];
  3620. }
  3621. else {
  3622. $ids = $_data['id'];
  3623. }
  3624. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  3625. $_SESSION['return'][] = array(
  3626. 'type' => 'danger',
  3627. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3628. 'msg' => 'access_denied'
  3629. );
  3630. return false;
  3631. }
  3632. foreach ($ids as $id) {
  3633. if (!is_numeric($id)) {
  3634. continue;
  3635. }
  3636. $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
  3637. $stmt->execute(array(':id' => $id));
  3638. $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
  3639. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
  3640. $_SESSION['return'][] = array(
  3641. 'type' => 'danger',
  3642. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3643. 'msg' => 'access_denied'
  3644. );
  3645. continue;
  3646. }
  3647. $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
  3648. $stmt->execute(array(':id' => $id));
  3649. $_SESSION['return'][] = array(
  3650. 'type' => 'success',
  3651. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3652. 'msg' => array('delete_filter', $id)
  3653. );
  3654. }
  3655. break;
  3656. case 'time_limited_alias':
  3657. if (!is_array($_data['address'])) {
  3658. $addresses = array();
  3659. $addresses[] = $_data['address'];
  3660. }
  3661. else {
  3662. $addresses = $_data['address'];
  3663. }
  3664. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  3665. $_SESSION['return'][] = array(
  3666. 'type' => 'danger',
  3667. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3668. 'msg' => 'access_denied'
  3669. );
  3670. return false;
  3671. }
  3672. foreach ($addresses as $address) {
  3673. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  3674. $stmt->execute(array(':address' => $address));
  3675. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  3676. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  3677. $_SESSION['return'][] = array(
  3678. 'type' => 'danger',
  3679. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3680. 'msg' => 'access_denied'
  3681. );
  3682. continue;
  3683. }
  3684. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
  3685. $stmt->execute(array(
  3686. ':username' => $goto,
  3687. ':item' => $address
  3688. ));
  3689. $_SESSION['return'][] = array(
  3690. 'type' => 'success',
  3691. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3692. 'msg' => array('mailbox_modified', htmlspecialchars($goto))
  3693. );
  3694. }
  3695. break;
  3696. case 'eas_cache':
  3697. if (!is_array($_data['username'])) {
  3698. $usernames = array();
  3699. $usernames[] = $_data['username'];
  3700. }
  3701. else {
  3702. $usernames = $_data['username'];
  3703. }
  3704. if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
  3705. $_SESSION['return'][] = array(
  3706. 'type' => 'danger',
  3707. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3708. 'msg' => 'access_denied'
  3709. );
  3710. return false;
  3711. }
  3712. foreach ($usernames as $username) {
  3713. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  3714. $_SESSION['return'][] = array(
  3715. 'type' => 'danger',
  3716. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3717. 'msg' => 'access_denied'
  3718. );
  3719. continue;
  3720. }
  3721. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  3722. $stmt->execute(array(
  3723. ':username' => $username
  3724. ));
  3725. $_SESSION['return'][] = array(
  3726. 'type' => 'success',
  3727. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3728. 'msg' => array('eas_reset', htmlspecialchars($username))
  3729. );
  3730. }
  3731. break;
  3732. case 'sogo_profile':
  3733. if (!is_array($_data['username'])) {
  3734. $usernames = array();
  3735. $usernames[] = $_data['username'];
  3736. }
  3737. else {
  3738. $usernames = $_data['username'];
  3739. }
  3740. if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
  3741. $_SESSION['return'][] = array(
  3742. 'type' => 'danger',
  3743. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3744. 'msg' => 'access_denied'
  3745. );
  3746. return false;
  3747. }
  3748. foreach ($usernames as $username) {
  3749. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  3750. $_SESSION['return'][] = array(
  3751. 'type' => 'danger',
  3752. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3753. 'msg' => 'access_denied'
  3754. );
  3755. continue;
  3756. }
  3757. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  3758. $stmt->execute(array(
  3759. ':username' => $username
  3760. ));
  3761. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  3762. $stmt->execute(array(
  3763. ':username' => $username
  3764. ));
  3765. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
  3766. $stmt->execute(array(
  3767. ':username' => $username
  3768. ));
  3769. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  3770. $stmt->execute(array(
  3771. ':username' => $username
  3772. ));
  3773. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  3774. $stmt->execute(array(
  3775. ':username' => $username
  3776. ));
  3777. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  3778. $stmt->execute(array(
  3779. ':username' => $username
  3780. ));
  3781. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  3782. $stmt->execute(array(
  3783. ':username' => $username
  3784. ));
  3785. $_SESSION['return'][] = array(
  3786. 'type' => 'success',
  3787. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3788. 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
  3789. );
  3790. }
  3791. break;
  3792. case 'domain':
  3793. if (!is_array($_data['domain'])) {
  3794. $domains = array();
  3795. $domains[] = $_data['domain'];
  3796. }
  3797. else {
  3798. $domains = $_data['domain'];
  3799. }
  3800. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3801. $_SESSION['return'][] = array(
  3802. 'type' => 'danger',
  3803. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3804. 'msg' => 'access_denied'
  3805. );
  3806. return false;
  3807. }
  3808. foreach ($domains as $domain) {
  3809. if (!is_valid_domain_name($domain)) {
  3810. $_SESSION['return'][] = array(
  3811. 'type' => 'danger',
  3812. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3813. 'msg' => 'domain_invalid'
  3814. );
  3815. continue;
  3816. }
  3817. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  3818. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  3819. WHERE `domain` = :domain");
  3820. $stmt->execute(array(':domain' => $domain));
  3821. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  3822. if ($num_results != 0 || !empty($num_results)) {
  3823. $_SESSION['return'][] = array(
  3824. 'type' => 'danger',
  3825. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3826. 'msg' => array('domain_not_empty', $domain)
  3827. );
  3828. continue;
  3829. }
  3830. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
  3831. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  3832. if ($maildir_gc['type'] != 'success') {
  3833. $_SESSION['return'][] = array(
  3834. 'type' => 'warning',
  3835. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3836. 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
  3837. );
  3838. }
  3839. $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
  3840. $stmt->execute(array(
  3841. ':domain' => $domain,
  3842. ));
  3843. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
  3844. $stmt->execute(array(
  3845. ':domain' => $domain,
  3846. ));
  3847. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
  3848. $stmt->execute(array(
  3849. ':domain' => $domain,
  3850. ));
  3851. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
  3852. $stmt->execute(array(
  3853. ':domain' => $domain,
  3854. ));
  3855. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
  3856. $stmt->execute(array(
  3857. ':domain' => $domain,
  3858. ));
  3859. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
  3860. $stmt->execute(array(
  3861. ':domain' => '%@'.$domain,
  3862. ));
  3863. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
  3864. $stmt->execute(array(
  3865. ':domain' => '%@'.$domain,
  3866. ));
  3867. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
  3868. $stmt->execute(array(
  3869. ':domain' => '%@'.$domain,
  3870. ));
  3871. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
  3872. $stmt->execute(array(
  3873. ':domain' => '%@'.$domain,
  3874. ));
  3875. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  3876. $stmt->execute(array(
  3877. ':domain' => '%@'.$domain,
  3878. ));
  3879. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
  3880. $stmt->execute(array(
  3881. ':domain' => $domain,
  3882. ));
  3883. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
  3884. $stmt->execute(array(
  3885. ':domain' => $domain,
  3886. ));
  3887. $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  3888. $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  3889. try {
  3890. $redis->hDel('DOMAIN_MAP', $domain);
  3891. $redis->hDel('RL_VALUE', $domain);
  3892. }
  3893. catch (RedisException $e) {
  3894. $_SESSION['return'][] = array(
  3895. 'type' => 'danger',
  3896. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3897. 'msg' => array('redis_error', $e)
  3898. );
  3899. continue;
  3900. }
  3901. $_SESSION['return'][] = array(
  3902. 'type' => 'success',
  3903. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3904. 'msg' => array('domain_removed', htmlspecialchars($domain))
  3905. );
  3906. }
  3907. break;
  3908. case 'alias':
  3909. if (!is_array($_data['id'])) {
  3910. $ids = array();
  3911. $ids[] = $_data['id'];
  3912. }
  3913. else {
  3914. $ids = $_data['id'];
  3915. }
  3916. foreach ($ids as $id) {
  3917. $alias_data = mailbox('get', 'alias_details', $id);
  3918. if (empty($alias_data)) {
  3919. $_SESSION['return'][] = array(
  3920. 'type' => 'danger',
  3921. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3922. 'msg' => 'access_denied'
  3923. );
  3924. continue;
  3925. }
  3926. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
  3927. $stmt->execute(array(
  3928. ':id' => $alias_data['id']
  3929. ));
  3930. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
  3931. $stmt->execute(array(
  3932. ':alias_address' => $alias_data['address']
  3933. ));
  3934. $_SESSION['return'][] = array(
  3935. 'type' => 'success',
  3936. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3937. 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
  3938. );
  3939. }
  3940. break;
  3941. case 'alias_domain':
  3942. if (!is_array($_data['alias_domain'])) {
  3943. $alias_domains = array();
  3944. $alias_domains[] = $_data['alias_domain'];
  3945. }
  3946. else {
  3947. $alias_domains = $_data['alias_domain'];
  3948. }
  3949. foreach ($alias_domains as $alias_domain) {
  3950. if (!is_valid_domain_name($alias_domain)) {
  3951. $_SESSION['return'][] = array(
  3952. 'type' => 'danger',
  3953. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3954. 'msg' => 'domain_invalid'
  3955. );
  3956. continue;
  3957. }
  3958. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
  3959. WHERE `alias_domain`= :alias_domain");
  3960. $stmt->execute(array(':alias_domain' => $alias_domain));
  3961. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  3962. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
  3963. $_SESSION['return'][] = array(
  3964. 'type' => 'danger',
  3965. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3966. 'msg' => 'access_denied'
  3967. );
  3968. continue;
  3969. }
  3970. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  3971. $stmt->execute(array(
  3972. ':alias_domain' => $alias_domain,
  3973. ));
  3974. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
  3975. $stmt->execute(array(
  3976. ':alias_domain' => $alias_domain,
  3977. ));
  3978. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
  3979. $stmt->execute(array(
  3980. ':alias_domain' => $alias_domain,
  3981. ));
  3982. try {
  3983. $redis->hDel('DOMAIN_MAP', $alias_domain);
  3984. $redis->hDel('RL_VALUE', $domain);
  3985. }
  3986. catch (RedisException $e) {
  3987. $_SESSION['return'][] = array(
  3988. 'type' => 'danger',
  3989. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3990. 'msg' => array('redis_error', $e)
  3991. );
  3992. continue;
  3993. }
  3994. $_SESSION['return'][] = array(
  3995. 'type' => 'success',
  3996. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3997. 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
  3998. );
  3999. }
  4000. break;
  4001. case 'mailbox':
  4002. if (!is_array($_data['username'])) {
  4003. $usernames = array();
  4004. $usernames[] = $_data['username'];
  4005. }
  4006. else {
  4007. $usernames = $_data['username'];
  4008. }
  4009. foreach ($usernames as $username) {
  4010. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  4011. $_SESSION['return'][] = array(
  4012. 'type' => 'danger',
  4013. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4014. 'msg' => 'access_denied'
  4015. );
  4016. continue;
  4017. }
  4018. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4019. $_SESSION['return'][] = array(
  4020. 'type' => 'danger',
  4021. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4022. 'msg' => 'access_denied'
  4023. );
  4024. continue;
  4025. }
  4026. $mailbox_details = mailbox('get', 'mailbox_details', $username);
  4027. if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
  4028. $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
  4029. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
  4030. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  4031. if ($maildir_gc['type'] != 'success') {
  4032. $_SESSION['return'][] = array(
  4033. 'type' => 'warning',
  4034. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4035. 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
  4036. );
  4037. }
  4038. }
  4039. else {
  4040. $_SESSION['return'][] = array(
  4041. 'type' => 'warning',
  4042. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4043. 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
  4044. );
  4045. }
  4046. if (strtolower(getenv('SKIP_SOLR')) == 'n') {
  4047. $curl = curl_init();
  4048. curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
  4049. curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
  4050. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  4051. curl_setopt($curl, CURLOPT_POST, 1);
  4052. curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
  4053. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  4054. $response = curl_exec($curl);
  4055. if ($response === false) {
  4056. $err = curl_error($curl);
  4057. $_SESSION['return'][] = array(
  4058. 'type' => 'warning',
  4059. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4060. 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
  4061. );
  4062. }
  4063. curl_close($curl);
  4064. }
  4065. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
  4066. $stmt->execute(array(
  4067. ':username' => $username
  4068. ));
  4069. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
  4070. $stmt->execute(array(
  4071. ':username' => $username
  4072. ));
  4073. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
  4074. $stmt->execute(array(
  4075. ':username' => $username
  4076. ));
  4077. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
  4078. $stmt->execute(array(
  4079. ':username' => $username
  4080. ));
  4081. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
  4082. $stmt->execute(array(
  4083. ':username' => $username
  4084. ));
  4085. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  4086. $stmt->execute(array(
  4087. ':username' => $username
  4088. ));
  4089. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :username");
  4090. $stmt->execute(array(
  4091. ':username' => $username
  4092. ));
  4093. // fk, better safe than sorry
  4094. $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
  4095. $stmt->execute(array(
  4096. ':username' => $username
  4097. ));
  4098. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
  4099. $stmt->execute(array(
  4100. ':username' => $username
  4101. ));
  4102. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
  4103. $stmt->execute(array(
  4104. ':username' => $username
  4105. ));
  4106. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
  4107. $stmt->execute(array(
  4108. ':username' => $username
  4109. ));
  4110. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  4111. $stmt->execute(array(
  4112. ':username' => $username
  4113. ));
  4114. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4115. $stmt->execute(array(
  4116. ':username' => $username
  4117. ));
  4118. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
  4119. $stmt->execute(array(
  4120. ':username' => $username
  4121. ));
  4122. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4123. $stmt->execute(array(
  4124. ':username' => $username
  4125. ));
  4126. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4127. $stmt->execute(array(
  4128. ':username' => $username
  4129. ));
  4130. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4131. $stmt->execute(array(
  4132. ':username' => $username
  4133. ));
  4134. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  4135. $stmt->execute(array(
  4136. ':username' => $username
  4137. ));
  4138. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
  4139. $stmt->execute(array(
  4140. ':username' => $username
  4141. ));
  4142. $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
  4143. $stmt->execute(array(
  4144. ':username' => $username
  4145. ));
  4146. $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
  4147. $stmt->execute(array(
  4148. ':username' => $username
  4149. ));
  4150. $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
  4151. $stmt->execute(array(
  4152. ':username' => $username
  4153. ));
  4154. $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
  4155. WHERE `goto` REGEXP :username");
  4156. $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
  4157. $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4158. foreach ($GotoData as $gotos) {
  4159. $goto_exploded = explode(',', $gotos['goto']);
  4160. if (($key = array_search($username, $goto_exploded)) !== false) {
  4161. unset($goto_exploded[$key]);
  4162. }
  4163. $gotos_rebuild = implode(',', $goto_exploded);
  4164. $stmt = $pdo->prepare("UPDATE `alias` SET
  4165. `goto` = :goto
  4166. WHERE `address` = :address");
  4167. $stmt->execute(array(
  4168. ':goto' => $gotos_rebuild,
  4169. ':address' => $gotos['address']
  4170. ));
  4171. }
  4172. try {
  4173. $redis->hDel('RL_VALUE', $username);
  4174. }
  4175. catch (RedisException $e) {
  4176. $_SESSION['return'][] = array(
  4177. 'type' => 'danger',
  4178. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4179. 'msg' => array('redis_error', $e)
  4180. );
  4181. continue;
  4182. }
  4183. $_SESSION['return'][] = array(
  4184. 'type' => 'success',
  4185. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4186. 'msg' => array('mailbox_removed', htmlspecialchars($username))
  4187. );
  4188. }
  4189. break;
  4190. case 'resource':
  4191. if (!is_array($_data['name'])) {
  4192. $names = array();
  4193. $names[] = $_data['name'];
  4194. }
  4195. else {
  4196. $names = $_data['name'];
  4197. }
  4198. foreach ($names as $name) {
  4199. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  4200. $_SESSION['return'][] = array(
  4201. 'type' => 'danger',
  4202. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4203. 'msg' => 'access_denied'
  4204. );
  4205. continue;
  4206. }
  4207. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  4208. $_SESSION['return'][] = array(
  4209. 'type' => 'danger',
  4210. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4211. 'msg' => 'access_denied'
  4212. );
  4213. continue;
  4214. }
  4215. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  4216. $stmt->execute(array(
  4217. ':username' => $name
  4218. ));
  4219. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  4220. $stmt->execute(array(
  4221. ':username' => $name
  4222. ));
  4223. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4224. $stmt->execute(array(
  4225. ':username' => $name
  4226. ));
  4227. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
  4228. $stmt->execute(array(
  4229. ':username' => $name
  4230. ));
  4231. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4232. $stmt->execute(array(
  4233. ':username' => $name
  4234. ));
  4235. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_contact` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4236. $stmt->execute(array(
  4237. ':username' => $name
  4238. ));
  4239. $stmt = $pdo->prepare("DELETE FROM `sogo_quick_appointment` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4240. $stmt->execute(array(
  4241. ':username' => $name
  4242. ));
  4243. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  4244. $stmt->execute(array(
  4245. ':username' => $name
  4246. ));
  4247. $_SESSION['return'][] = array(
  4248. 'type' => 'success',
  4249. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4250. 'msg' => array('resource_removed', htmlspecialchars($name))
  4251. );
  4252. }
  4253. break;
  4254. }
  4255. break;
  4256. }
  4257. if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'mailbox', 'resource'))) {
  4258. update_sogo_static_view();
  4259. }
  4260. }