functions.mailbox.inc.php 288 KB

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