functions.mailbox.inc.php 247 KB

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