functions.mailbox.inc.php 289 KB

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