functions.mailbox.inc.php 279 KB

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