functions.mailbox.inc.php 258 KB

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