functions.mailbox.inc.php 279 KB

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