functions.mailbox.inc.php 271 KB

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