functions.mailbox.inc.php 270 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769
  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. $exec_fields = array(
  3208. 'cmd' => 'doveadm',
  3209. 'task' => 'get_acl',
  3210. 'id' => $old_username
  3211. );
  3212. $imap_acls = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  3213. // delete imap acls
  3214. foreach ($imap_acls as $imap_acl) {
  3215. $exec_fields = array(
  3216. 'cmd' => 'doveadm',
  3217. 'task' => 'delete_acl',
  3218. 'user' => $imap_acl['user'],
  3219. 'mailbox' => $imap_acl['mailbox'],
  3220. 'id' => $imap_acl['id']
  3221. );
  3222. docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3223. }
  3224. // rename username in sql
  3225. try {
  3226. $pdo->beginTransaction();
  3227. $pdo->exec('SET FOREIGN_KEY_CHECKS = 0');
  3228. $pdo->prepare('UPDATE mailbox SET username = :new_username, local_part = :new_local_part WHERE username = :old_username')
  3229. ->execute([
  3230. ':new_username' => $new_username,
  3231. ':new_local_part' => $new_local_part,
  3232. ':old_username' => $old_username
  3233. ]);
  3234. // Update the username in all related tables
  3235. $tables = [
  3236. 'tags_mailbox' => 'username',
  3237. 'sieve_filters' => 'username',
  3238. 'app_passwd' => 'mailbox',
  3239. 'user_acl' => 'username',
  3240. 'da_acl' => 'username',
  3241. 'quota2' => 'username',
  3242. 'quota2replica' => 'username',
  3243. 'pushover' => 'username'
  3244. ];
  3245. foreach ($tables as $table => $column) {
  3246. $pdo->prepare("UPDATE $table SET $column = :new_username WHERE $column = :old_username")
  3247. ->execute([
  3248. ':new_username' => $new_username,
  3249. ':old_username' => $old_username
  3250. ]);
  3251. }
  3252. $pdo->prepare("UPDATE _sogo_static_view SET c_uid = :new_username, c_name = :new_username2, mail = :new_username3 WHERE c_uid = :old_username")
  3253. ->execute([
  3254. ':new_username' => $new_username,
  3255. ':new_username2' => $new_username,
  3256. ':new_username3' => $new_username,
  3257. ':old_username' => $old_username
  3258. ]);
  3259. $pdo->prepare("UPDATE alias SET address = :new_username, goto = :new_username2 WHERE address = :old_username")
  3260. ->execute([
  3261. ':new_username' => $new_username,
  3262. ':new_username2' => $new_username,
  3263. ':old_username' => $old_username
  3264. ]);
  3265. // Re-enable foreign key checks
  3266. $pdo->exec('SET FOREIGN_KEY_CHECKS = 1');
  3267. $pdo->commit();
  3268. } catch (PDOException $e) {
  3269. // Rollback the transaction if something goes wrong
  3270. $pdo->rollBack();
  3271. $_SESSION['return'][] = array(
  3272. 'type' => 'danger',
  3273. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3274. 'msg' => $e->getMessage()
  3275. );
  3276. }
  3277. // move maildir
  3278. $exec_fields = array(
  3279. 'cmd' => 'maildir',
  3280. 'task' => 'move',
  3281. 'old_maildir' => $domain . '/' . $old_local_part,
  3282. 'new_maildir' => $domain . '/' . $new_local_part
  3283. );
  3284. docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3285. // rename username in sogo
  3286. $exec_fields = array(
  3287. 'cmd' => 'sogo',
  3288. 'task' => 'rename_user',
  3289. 'old_username' => $old_username,
  3290. 'new_username' => $new_username
  3291. );
  3292. docker('post', 'sogo-mailcow', 'exec', $exec_fields);
  3293. // set imap acls
  3294. foreach ($imap_acls as $imap_acl) {
  3295. $exec_fields = array(
  3296. 'cmd' => 'doveadm',
  3297. 'task' => 'set_acl',
  3298. 'user' => $imap_acl['user'],
  3299. 'mailbox' => $imap_acl['mailbox'],
  3300. 'id' => $new_username,
  3301. 'rights' => $imap_acl['rights']
  3302. );
  3303. docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3304. }
  3305. // create alias
  3306. if ($create_alias == 1) {
  3307. mailbox("add", "alias", array(
  3308. "address" => $old_username,
  3309. "goto" => $new_username,
  3310. "active" => 1,
  3311. "sogo_visible" => 1,
  3312. "private_comment" => sprintf($lang['success']['mailbox_renamed'], $old_username, $new_username)
  3313. ));
  3314. }
  3315. $_SESSION['return'][] = array(
  3316. 'type' => 'success',
  3317. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3318. 'msg' => array('mailbox_renamed', $old_username, $new_username)
  3319. );
  3320. break;
  3321. case 'mailbox_templates':
  3322. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3323. $_SESSION['return'][] = array(
  3324. 'type' => 'danger',
  3325. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3326. 'msg' => 'access_denied'
  3327. );
  3328. return false;
  3329. }
  3330. if (!is_array($_data['ids'])) {
  3331. $ids = array();
  3332. $ids[] = $_data['ids'];
  3333. }
  3334. else {
  3335. $ids = $_data['ids'];
  3336. }
  3337. foreach ($ids as $id) {
  3338. $is_now = mailbox("get", "mailbox_templates", $id);
  3339. if (empty($is_now) ||
  3340. $is_now["type"] != "mailbox"){
  3341. $_SESSION['return'][] = array(
  3342. 'type' => 'danger',
  3343. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3344. 'msg' => 'template_id_invalid'
  3345. );
  3346. continue;
  3347. }
  3348. // check name
  3349. if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
  3350. // keep template name of Default template
  3351. $_data["template"] = $is_now["template"];
  3352. }
  3353. else {
  3354. $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
  3355. }
  3356. // check attributes
  3357. $attr = array();
  3358. $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
  3359. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : $is_now['tags'];
  3360. $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
  3361. $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
  3362. $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : $is_now['rl_frame'];
  3363. $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : $is_now['rl_value'];
  3364. $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : $is_now['force_pw_update'];
  3365. $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : $is_now['sogo_access'];
  3366. $attr["active"] = isset($_data['active']) ? intval($_data['active']) : $is_now['active'];
  3367. $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
  3368. $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
  3369. if (isset($_data['protocol_access'])) {
  3370. $_data['protocol_access'] = (array)$_data['protocol_access'];
  3371. $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  3372. $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  3373. $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  3374. $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
  3375. }
  3376. else {
  3377. foreach ($is_now as $key => $value){
  3378. $attr[$key] = $is_now[$key];
  3379. }
  3380. }
  3381. if (isset($_data['acl'])) {
  3382. $_data['acl'] = (array)$_data['acl'];
  3383. $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
  3384. $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
  3385. $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
  3386. $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
  3387. $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
  3388. $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
  3389. $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
  3390. $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
  3391. $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
  3392. $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
  3393. $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
  3394. $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
  3395. $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
  3396. $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
  3397. $attr['acl_pw_reset'] = (in_array('pw_reset', $_data['acl'])) ? 1 : 0;
  3398. } else {
  3399. foreach ($is_now as $key => $value){
  3400. $attr[$key] = $is_now[$key];
  3401. }
  3402. }
  3403. // update template
  3404. $stmt = $pdo->prepare("UPDATE `templates`
  3405. SET `template` = :template, `attributes` = :attributes
  3406. WHERE id = :id");
  3407. $stmt->execute(array(
  3408. ":id" => $id ,
  3409. ":template" => $_data["template"] ,
  3410. ":attributes" => json_encode($attr)
  3411. ));
  3412. }
  3413. $_SESSION['return'][] = array(
  3414. 'type' => 'success',
  3415. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3416. 'msg' => array('template_modified', $_data["template"])
  3417. );
  3418. return true;
  3419. break;
  3420. case 'mailbox_custom_attribute':
  3421. $_data['attribute'] = isset($_data['attribute']) ? $_data['attribute'] : array();
  3422. $_data['attribute'] = is_array($_data['attribute']) ? $_data['attribute'] : array($_data['attribute']);
  3423. $_data['attribute'] = array_map(function($value) { return str_replace(' ', '', $value); }, $_data['attribute']);
  3424. $_data['value'] = isset($_data['value']) ? $_data['value'] : array();
  3425. $_data['value'] = is_array($_data['value']) ? $_data['value'] : array($_data['value']);
  3426. $attributes = (object)array_combine($_data['attribute'], $_data['value']);
  3427. $mailboxes = is_array($_data['mailboxes']) ? $_data['mailboxes'] : array($_data['mailboxes']);
  3428. foreach ($mailboxes as $mailbox) {
  3429. if (!filter_var($mailbox, FILTER_VALIDATE_EMAIL)) {
  3430. $_SESSION['return'][] = array(
  3431. 'type' => 'danger',
  3432. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3433. 'msg' => array('username_invalid', $mailbox)
  3434. );
  3435. continue;
  3436. }
  3437. $is_now = mailbox('get', 'mailbox_details', $mailbox);
  3438. if(!empty($is_now)){
  3439. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $is_now['domain'])) {
  3440. $_SESSION['return'][] = array(
  3441. 'type' => 'danger',
  3442. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3443. 'msg' => 'access_denied'
  3444. );
  3445. continue;
  3446. }
  3447. }
  3448. else {
  3449. $_SESSION['return'][] = array(
  3450. 'type' => 'danger',
  3451. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3452. 'msg' => 'access_denied'
  3453. );
  3454. continue;
  3455. }
  3456. $stmt = $pdo->prepare("UPDATE `mailbox`
  3457. SET `custom_attributes` = :custom_attributes
  3458. WHERE username = :username");
  3459. $stmt->execute(array(
  3460. ":username" => $mailbox,
  3461. ":custom_attributes" => json_encode($attributes)
  3462. ));
  3463. $_SESSION['return'][] = array(
  3464. 'type' => 'success',
  3465. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3466. 'msg' => array('mailbox_modified', $mailbox)
  3467. );
  3468. }
  3469. return true;
  3470. break;
  3471. case 'resource':
  3472. if (!is_array($_data['name'])) {
  3473. $names = array();
  3474. $names[] = $_data['name'];
  3475. }
  3476. else {
  3477. $names = $_data['name'];
  3478. }
  3479. foreach ($names as $name) {
  3480. $is_now = mailbox('get', 'resource_details', $name);
  3481. if (!empty($is_now)) {
  3482. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  3483. $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
  3484. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  3485. $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
  3486. }
  3487. else {
  3488. $_SESSION['return'][] = array(
  3489. 'type' => 'danger',
  3490. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3491. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3492. );
  3493. continue;
  3494. }
  3495. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  3496. $_SESSION['return'][] = array(
  3497. 'type' => 'danger',
  3498. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3499. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3500. );
  3501. continue;
  3502. }
  3503. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  3504. $multiple_bookings = -1;
  3505. }
  3506. if (empty($description)) {
  3507. $_SESSION['return'][] = array(
  3508. 'type' => 'danger',
  3509. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3510. 'msg' => array('description_invalid', htmlspecialchars($name))
  3511. );
  3512. continue;
  3513. }
  3514. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  3515. $_SESSION['return'][] = array(
  3516. 'type' => 'danger',
  3517. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3518. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3519. );
  3520. continue;
  3521. }
  3522. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  3523. $_SESSION['return'][] = array(
  3524. 'type' => 'danger',
  3525. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3526. 'msg' => 'access_denied'
  3527. );
  3528. continue;
  3529. }
  3530. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  3531. `active` = :active,
  3532. `name`= :description,
  3533. `kind`= :kind,
  3534. `multiple_bookings`= :multiple_bookings
  3535. WHERE `username` = :name");
  3536. $stmt->execute(array(
  3537. ':active' => $active,
  3538. ':description' => $description,
  3539. ':multiple_bookings' => $multiple_bookings,
  3540. ':kind' => $kind,
  3541. ':name' => $name
  3542. ));
  3543. $_SESSION['return'][] = array(
  3544. 'type' => 'success',
  3545. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3546. 'msg' => array('resource_modified', htmlspecialchars($name))
  3547. );
  3548. }
  3549. break;
  3550. case 'domain_wide_footer':
  3551. if (!is_array($_data['domains'])) {
  3552. $domains = array();
  3553. $domains[] = $_data['domains'];
  3554. }
  3555. else {
  3556. $domains = $_data['domains'];
  3557. }
  3558. $footers = array();
  3559. $footers['html'] = isset($_data['html']) ? $_data['html'] : '';
  3560. $footers['plain'] = isset($_data['plain']) ? $_data['plain'] : '';
  3561. $footers['skip_replies'] = isset($_data['skip_replies']) ? (int)$_data['skip_replies'] : 0;
  3562. $footers['mbox_exclude'] = array();
  3563. $footers['alias_domain_exclude'] = array();
  3564. if (isset($_data["exclude"])){
  3565. if (!is_array($_data["exclude"])) {
  3566. $_data["exclude"] = array($_data["exclude"]);
  3567. }
  3568. foreach ($_data["exclude"] as $exclude) {
  3569. if (filter_var($exclude, FILTER_VALIDATE_EMAIL)) {
  3570. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `address` = :address
  3571. UNION
  3572. SELECT `username` FROM `mailbox` WHERE `username` = :username");
  3573. $stmt->execute(array(
  3574. ':address' => $exclude,
  3575. ':username' => $exclude,
  3576. ));
  3577. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3578. if(!$row){
  3579. $_SESSION['return'][] = array(
  3580. 'type' => 'danger',
  3581. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3582. 'msg' => array('username_invalid', $exclude)
  3583. );
  3584. continue;
  3585. }
  3586. array_push($footers['mbox_exclude'], $exclude);
  3587. }
  3588. elseif (is_valid_domain_name($exclude)) {
  3589. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  3590. $stmt->execute(array(
  3591. ':alias_domain' => $exclude,
  3592. ));
  3593. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3594. if(!$row){
  3595. $_SESSION['return'][] = array(
  3596. 'type' => 'danger',
  3597. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3598. 'msg' => array('username_invalid', $exclude)
  3599. );
  3600. continue;
  3601. }
  3602. array_push($footers['alias_domain_exclude'], $exclude);
  3603. }
  3604. else {
  3605. $_SESSION['return'][] = array(
  3606. 'type' => 'danger',
  3607. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3608. 'msg' => array('username_invalid', $exclude)
  3609. );
  3610. }
  3611. }
  3612. }
  3613. foreach ($domains as $domain) {
  3614. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  3615. if (!is_valid_domain_name($domain)) {
  3616. $_SESSION['return'][] = array(
  3617. 'type' => 'danger',
  3618. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3619. 'msg' => 'domain_invalid'
  3620. );
  3621. return false;
  3622. }
  3623. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  3624. $_SESSION['return'][] = array(
  3625. 'type' => 'danger',
  3626. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3627. 'msg' => 'access_denied'
  3628. );
  3629. return false;
  3630. }
  3631. try {
  3632. $stmt = $pdo->prepare("DELETE FROM `domain_wide_footer` WHERE `domain`= :domain");
  3633. $stmt->execute(array(':domain' => $domain));
  3634. $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)");
  3635. $stmt->execute(array(
  3636. ':domain' => $domain,
  3637. ':html' => $footers['html'],
  3638. ':plain' => $footers['plain'],
  3639. ':mbox_exclude' => json_encode($footers['mbox_exclude']),
  3640. ':alias_domain_exclude' => json_encode($footers['alias_domain_exclude']),
  3641. ':skip_replies' => $footers['skip_replies'],
  3642. ));
  3643. }
  3644. catch (PDOException $e) {
  3645. $_SESSION['return'][] = array(
  3646. 'type' => 'danger',
  3647. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3648. 'msg' => $e->getMessage()
  3649. );
  3650. return false;
  3651. }
  3652. $_SESSION['return'][] = array(
  3653. 'type' => 'success',
  3654. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3655. 'msg' => array('domain_footer_modified', htmlspecialchars($domain))
  3656. );
  3657. }
  3658. break;
  3659. }
  3660. break;
  3661. case 'get':
  3662. switch ($_type) {
  3663. case 'sender_acl_handles':
  3664. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  3665. return false;
  3666. }
  3667. $data['sender_acl_domains']['ro'] = array();
  3668. $data['sender_acl_domains']['rw'] = array();
  3669. $data['sender_acl_domains']['selectable'] = array();
  3670. $data['sender_acl_addresses']['ro'] = array();
  3671. $data['sender_acl_addresses']['rw'] = array();
  3672. $data['sender_acl_addresses']['selectable'] = array();
  3673. $data['fixed_sender_aliases'] = array();
  3674. $data['external_sender_aliases'] = array();
  3675. // Fixed addresses
  3676. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
  3677. $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
  3678. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3679. while ($row = array_shift($rows)) {
  3680. $data['fixed_sender_aliases'][] = $row['address'];
  3681. }
  3682. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
  3683. WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
  3684. AND `mailbox`.`username` = :username");
  3685. $stmt->execute(array(':username' => $_data));
  3686. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3687. while ($row = array_shift($rows)) {
  3688. if (!empty($row['alias_domain_alias'])) {
  3689. $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
  3690. }
  3691. }
  3692. // External addresses
  3693. $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
  3694. $stmt->execute(array(':logged_in_as' => $_data));
  3695. $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3696. while ($row = array_shift($exernal_rows)) {
  3697. if (!empty($row['send_as_external'])) {
  3698. $data['external_sender_aliases'][] = $row['send_as_external'];
  3699. }
  3700. }
  3701. // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
  3702. // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
  3703. $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` = '*')");
  3704. $stmt->execute(array(':logged_in_as' => $_data));
  3705. $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3706. while ($domain_row = array_shift($domain_rows)) {
  3707. if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3708. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3709. continue;
  3710. }
  3711. if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3712. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3713. continue;
  3714. }
  3715. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  3716. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3717. }
  3718. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3719. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3720. }
  3721. }
  3722. $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` != '*')");
  3723. $stmt->execute(array(':logged_in_as' => $_data));
  3724. $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3725. while ($address_row = array_shift($address_rows)) {
  3726. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3727. $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
  3728. continue;
  3729. }
  3730. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3731. $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
  3732. continue;
  3733. }
  3734. }
  3735. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  3736. WHERE `domain` NOT IN (
  3737. SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
  3738. WHERE `logged_in_as` = :logged_in_as1
  3739. AND `external` = '0'
  3740. AND `send_as` LIKE '@%')
  3741. UNION
  3742. SELECT '*' FROM `domain`
  3743. WHERE '*' NOT IN (
  3744. SELECT `send_as` FROM `sender_acl`
  3745. WHERE `logged_in_as` = :logged_in_as2
  3746. AND `external` = '0'
  3747. )");
  3748. $stmt->execute(array(
  3749. ':logged_in_as1' => $_data,
  3750. ':logged_in_as2' => $_data
  3751. ));
  3752. $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3753. while ($row_domain = array_shift($rows_domain)) {
  3754. if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
  3755. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3756. continue;
  3757. }
  3758. if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3759. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3760. continue;
  3761. }
  3762. }
  3763. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  3764. WHERE `goto` != :goto
  3765. AND `address` NOT IN (
  3766. SELECT `send_as` FROM `sender_acl`
  3767. WHERE `logged_in_as` = :logged_in_as
  3768. AND `external` = '0'
  3769. AND `send_as` NOT LIKE '@%')");
  3770. $stmt->execute(array(
  3771. ':logged_in_as' => $_data,
  3772. ':goto' => $_data
  3773. ));
  3774. $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3775. while ($row = array_shift($rows_mbox)) {
  3776. // Aliases are not selectable
  3777. if (in_array($row['address'], $data['fixed_sender_aliases'])) {
  3778. continue;
  3779. }
  3780. if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
  3781. $data['sender_acl_addresses']['selectable'][] = $row['address'];
  3782. }
  3783. }
  3784. return $data;
  3785. break;
  3786. case 'mailboxes':
  3787. $mailboxes = array();
  3788. if (isset($_extra) && is_array($_extra) && isset($_data)) {
  3789. // get by domain and tags
  3790. $tags = is_array($_extra) ? $_extra : array();
  3791. $sql = "";
  3792. foreach ($tags as $key => $tag) {
  3793. $sql = $sql."SELECT DISTINCT `username` FROM `tags_mailbox` WHERE `username` LIKE ? AND `tag_name` LIKE ?"; // distinct, avoid duplicates
  3794. if ($key === array_key_last($tags)) break;
  3795. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  3796. }
  3797. // prepend domain to array
  3798. $params = array();
  3799. foreach ($tags as $key => $val){
  3800. array_push($params, '%'.$_data.'%');
  3801. array_push($params, '%'.$val.'%');
  3802. }
  3803. $stmt = $pdo->prepare($sql);
  3804. $stmt->execute($params);
  3805. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3806. while($row = array_shift($rows)) {
  3807. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], explode('@', $row['username'])[1]))
  3808. $mailboxes[] = $row['username'];
  3809. }
  3810. }
  3811. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3812. // get by domain
  3813. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
  3814. $stmt->execute(array(
  3815. ':domain' => $_data,
  3816. ));
  3817. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3818. while($row = array_shift($rows)) {
  3819. $mailboxes[] = $row['username'];
  3820. }
  3821. }
  3822. else {
  3823. $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)");
  3824. $stmt->execute(array(
  3825. ':username' => $_SESSION['mailcow_cc_username'],
  3826. ':role' => $_SESSION['mailcow_cc_role'],
  3827. ));
  3828. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3829. while($row = array_shift($rows)) {
  3830. $mailboxes[] = $row['username'];
  3831. }
  3832. }
  3833. return $mailboxes;
  3834. break;
  3835. case 'tls_policy':
  3836. $attrs = array();
  3837. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3838. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3839. return false;
  3840. }
  3841. }
  3842. else {
  3843. $_data = $_SESSION['mailcow_cc_username'];
  3844. }
  3845. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3846. $stmt->execute(array(':username' => $_data));
  3847. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3848. $attrs = json_decode($attrs['attributes'], true);
  3849. return array(
  3850. 'tls_enforce_in' => $attrs['tls_enforce_in'],
  3851. 'tls_enforce_out' => $attrs['tls_enforce_out']
  3852. );
  3853. break;
  3854. case 'quarantine_notification':
  3855. $attrs = array();
  3856. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3857. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3858. return false;
  3859. }
  3860. }
  3861. else {
  3862. $_data = $_SESSION['mailcow_cc_username'];
  3863. }
  3864. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3865. $stmt->execute(array(':username' => $_data));
  3866. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3867. $attrs = json_decode($attrs['attributes'], true);
  3868. return $attrs['quarantine_notification'];
  3869. break;
  3870. case 'quarantine_category':
  3871. $attrs = array();
  3872. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3873. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3874. return false;
  3875. }
  3876. }
  3877. else {
  3878. $_data = $_SESSION['mailcow_cc_username'];
  3879. }
  3880. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3881. $stmt->execute(array(':username' => $_data));
  3882. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3883. $attrs = json_decode($attrs['attributes'], true);
  3884. return $attrs['quarantine_category'];
  3885. break;
  3886. case 'filters':
  3887. $filters = array();
  3888. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3889. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3890. return false;
  3891. }
  3892. }
  3893. else {
  3894. $_data = $_SESSION['mailcow_cc_username'];
  3895. }
  3896. $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
  3897. $stmt->execute(array(':username' => $_data));
  3898. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3899. while($row = array_shift($rows)) {
  3900. $filters[] = $row['id'];
  3901. }
  3902. return $filters;
  3903. break;
  3904. case 'global_filter_details':
  3905. $global_filters = array();
  3906. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3907. return false;
  3908. }
  3909. $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
  3910. $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
  3911. return $global_filters;
  3912. break;
  3913. case 'filter_details':
  3914. $filter_details = array();
  3915. if (!is_numeric($_data)) {
  3916. return false;
  3917. }
  3918. $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
  3919. id,
  3920. username,
  3921. filter_type,
  3922. script_data,
  3923. script_desc
  3924. FROM `sieve_filters`
  3925. WHERE `id` = :id");
  3926. $stmt->execute(array(':id' => $_data));
  3927. $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
  3928. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
  3929. return false;
  3930. }
  3931. return $filter_details;
  3932. break;
  3933. case 'active_user_sieve':
  3934. $filter_details = array();
  3935. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3936. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3937. return false;
  3938. }
  3939. }
  3940. else {
  3941. $_data = $_SESSION['mailcow_cc_username'];
  3942. }
  3943. $exec_fields = array(
  3944. 'cmd' => 'sieve',
  3945. 'task' => 'list',
  3946. 'username' => $_data
  3947. );
  3948. $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3949. $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
  3950. foreach ($filters as $filter) {
  3951. if (preg_match('/.+ ACTIVE/i', $filter)) {
  3952. $exec_fields = array(
  3953. 'cmd' => 'sieve',
  3954. 'task' => 'print',
  3955. 'script_name' => substr($filter, 0, -7),
  3956. 'username' => $_data
  3957. );
  3958. $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3959. // Remove first line
  3960. return preg_replace('/^.+\n/', '', $script);
  3961. }
  3962. }
  3963. return false;
  3964. break;
  3965. case 'syncjob_details':
  3966. $syncjobdetails = array();
  3967. if (!is_numeric($_data)) {
  3968. return false;
  3969. }
  3970. if (isset($_extra) && in_array('no_log', $_extra)) {
  3971. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
  3972. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3973. while($field = array_shift($fields)) {
  3974. $shown_fields[] = $field['Field'];
  3975. }
  3976. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3977. `active`
  3978. FROM `imapsync` WHERE id = :id");
  3979. }
  3980. elseif (isset($_extra) && in_array('with_password', $_extra)) {
  3981. $stmt = $pdo->prepare("SELECT *,
  3982. `active`
  3983. FROM `imapsync` WHERE id = :id");
  3984. }
  3985. else {
  3986. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
  3987. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3988. while($field = array_shift($fields)) {
  3989. $shown_fields[] = $field['Field'];
  3990. }
  3991. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3992. `active`
  3993. FROM `imapsync` WHERE id = :id");
  3994. }
  3995. $stmt->execute(array(':id' => $_data));
  3996. $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
  3997. if (!empty($syncjobdetails['returned_text'])) {
  3998. $syncjobdetails['log'] = $syncjobdetails['returned_text'];
  3999. }
  4000. else {
  4001. $syncjobdetails['log'] = '';
  4002. }
  4003. unset($syncjobdetails['returned_text']);
  4004. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
  4005. return false;
  4006. }
  4007. return $syncjobdetails;
  4008. break;
  4009. case 'syncjobs':
  4010. $syncjobdata = array();
  4011. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4012. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4013. return false;
  4014. }
  4015. }
  4016. else {
  4017. $_data = $_SESSION['mailcow_cc_username'];
  4018. }
  4019. $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
  4020. $stmt->execute(array(':username' => $_data));
  4021. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4022. while($row = array_shift($rows)) {
  4023. $syncjobdata[] = $row['id'];
  4024. }
  4025. return $syncjobdata;
  4026. break;
  4027. case 'spam_score':
  4028. $curl = curl_init();
  4029. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  4030. curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
  4031. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  4032. $default_actions = curl_exec($curl);
  4033. if (!curl_errno($curl)) {
  4034. $data_array = json_decode($default_actions, true);
  4035. curl_close($curl);
  4036. foreach ($data_array as $data) {
  4037. if ($data['action'] == 'reject') {
  4038. $reject = $data['value'];
  4039. continue;
  4040. }
  4041. elseif ($data['action'] == 'add header') {
  4042. $add_header = $data['value'];
  4043. continue;
  4044. }
  4045. }
  4046. if (empty($add_header) || empty($reject)) {
  4047. // Assume default, set warning
  4048. $default = "5, 15";
  4049. $_SESSION['return'][] = array(
  4050. 'type' => 'warning',
  4051. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4052. 'msg' => 'Could not determine servers default spam score, assuming default'
  4053. );
  4054. }
  4055. else {
  4056. $default = $add_header . ', ' . $reject;
  4057. }
  4058. }
  4059. else {
  4060. // Assume default, set warning
  4061. $default = "5, 15";
  4062. $_SESSION['return'][] = array(
  4063. 'type' => 'warning',
  4064. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4065. 'msg' => 'Could not determine servers default spam score, assuming default'
  4066. );
  4067. }
  4068. curl_close($curl);
  4069. $policydata = array();
  4070. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4071. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4072. return false;
  4073. }
  4074. }
  4075. else {
  4076. $_data = $_SESSION['mailcow_cc_username'];
  4077. }
  4078. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
  4079. (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  4080. $stmt->execute(array(':username' => $_data));
  4081. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  4082. if (empty($num_results)) {
  4083. return $default;
  4084. }
  4085. else {
  4086. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
  4087. $stmt->execute(array(':username' => $_data));
  4088. $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4089. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
  4090. $stmt->execute(array(':username' => $_data));
  4091. $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4092. return $lowspamlevel['value'].', '.$highspamlevel['value'];
  4093. }
  4094. break;
  4095. case 'time_limited_aliases':
  4096. $tladata = array();
  4097. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4098. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4099. return false;
  4100. }
  4101. }
  4102. else {
  4103. $_data = $_SESSION['mailcow_cc_username'];
  4104. }
  4105. $stmt = $pdo->prepare("SELECT `address`,
  4106. `goto`,
  4107. `validity`,
  4108. `created`,
  4109. `modified`
  4110. FROM `spamalias`
  4111. WHERE `goto` = :username
  4112. AND `validity` >= :unixnow");
  4113. $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
  4114. $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4115. return $tladata;
  4116. break;
  4117. case 'delimiter_action':
  4118. $policydata = array();
  4119. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4120. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4121. return false;
  4122. }
  4123. }
  4124. else {
  4125. $_data = $_SESSION['mailcow_cc_username'];
  4126. }
  4127. try {
  4128. if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
  4129. return "subject";
  4130. }
  4131. elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
  4132. return "subfolder";
  4133. }
  4134. else {
  4135. return "none";
  4136. }
  4137. }
  4138. catch (RedisException $e) {
  4139. $_SESSION['return'][] = array(
  4140. 'type' => 'danger',
  4141. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4142. 'msg' => array('redis_error', $e)
  4143. );
  4144. return false;
  4145. }
  4146. break;
  4147. case 'resources':
  4148. $resources = array();
  4149. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4150. return false;
  4151. }
  4152. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4153. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
  4154. $stmt->execute(array(
  4155. ':domain' => $_data,
  4156. ));
  4157. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4158. while($row = array_shift($rows)) {
  4159. $resources[] = $row['username'];
  4160. }
  4161. }
  4162. else {
  4163. $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");
  4164. $stmt->execute(array(
  4165. ':username' => $_SESSION['mailcow_cc_username'],
  4166. ':role' => $_SESSION['mailcow_cc_role'],
  4167. ));
  4168. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4169. while($row = array_shift($rows)) {
  4170. $resources[] = $row['username'];
  4171. }
  4172. }
  4173. return $resources;
  4174. break;
  4175. case 'alias_domains':
  4176. $aliasdomains = array();
  4177. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4178. return false;
  4179. }
  4180. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4181. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
  4182. $stmt->execute(array(
  4183. ':domain' => $_data,
  4184. ));
  4185. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4186. while($row = array_shift($rows)) {
  4187. $aliasdomains[] = $row['alias_domain'];
  4188. }
  4189. }
  4190. else {
  4191. $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");
  4192. $stmt->execute(array(
  4193. ':username' => $_SESSION['mailcow_cc_username'],
  4194. ':role' => $_SESSION['mailcow_cc_role'],
  4195. ));
  4196. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4197. while($row = array_shift($rows)) {
  4198. $aliasdomains[] = $row['alias_domain'];
  4199. }
  4200. }
  4201. return $aliasdomains;
  4202. break;
  4203. case 'aliases':
  4204. $aliases = array();
  4205. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4206. return false;
  4207. }
  4208. $stmt = $pdo->prepare("SELECT `id`, `address` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
  4209. $stmt->execute(array(
  4210. ':domain' => $_data,
  4211. ));
  4212. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4213. while($row = array_shift($rows)) {
  4214. if ($_extra == "address"){
  4215. $aliases[] = $row['address'];
  4216. } else {
  4217. $aliases[] = $row['id'];
  4218. }
  4219. }
  4220. return $aliases;
  4221. break;
  4222. case 'alias_details':
  4223. $aliasdata = array();
  4224. $stmt = $pdo->prepare("SELECT
  4225. `id`,
  4226. `domain`,
  4227. `goto`,
  4228. `address`,
  4229. `public_comment`,
  4230. `private_comment`,
  4231. `active`,
  4232. `sogo_visible`,
  4233. `created`,
  4234. `modified`
  4235. FROM `alias`
  4236. WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
  4237. $stmt->execute(array(
  4238. ':id' => $_data,
  4239. ':address' => $_data,
  4240. ));
  4241. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4242. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4243. $stmt->execute(array(
  4244. ':domain' => $row['domain'],
  4245. ));
  4246. $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
  4247. if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
  4248. $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
  4249. }
  4250. else {
  4251. $aliasdata['in_primary_domain'] = "";
  4252. }
  4253. $aliasdata['id'] = $row['id'];
  4254. $aliasdata['domain'] = $row['domain'];
  4255. $aliasdata['public_comment'] = $row['public_comment'];
  4256. $aliasdata['private_comment'] = $row['private_comment'];
  4257. $aliasdata['domain'] = $row['domain'];
  4258. $aliasdata['goto'] = $row['goto'];
  4259. $aliasdata['address'] = $row['address'];
  4260. (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
  4261. $aliasdata['active'] = $row['active'];
  4262. $aliasdata['active_int'] = $row['active'];
  4263. $aliasdata['sogo_visible'] = $row['sogo_visible'];
  4264. $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
  4265. $aliasdata['created'] = $row['created'];
  4266. $aliasdata['modified'] = $row['modified'];
  4267. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
  4268. return false;
  4269. }
  4270. return $aliasdata;
  4271. break;
  4272. case 'alias_domain_details':
  4273. $aliasdomaindata = array();
  4274. $rl = ratelimit('get', 'domain', $_data);
  4275. $stmt = $pdo->prepare("SELECT
  4276. `alias_domain`,
  4277. `target_domain`,
  4278. `active`,
  4279. `created`,
  4280. `modified`
  4281. FROM `alias_domain`
  4282. WHERE `alias_domain` = :aliasdomain");
  4283. $stmt->execute(array(
  4284. ':aliasdomain' => $_data,
  4285. ));
  4286. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4287. $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
  4288. $stmt->execute(array(
  4289. ':target_domain' => $row['target_domain']
  4290. ));
  4291. $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
  4292. $aliasdomaindata['alias_domain'] = $row['alias_domain'];
  4293. $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
  4294. $aliasdomaindata['target_domain'] = $row['target_domain'];
  4295. $aliasdomaindata['active'] = $row['active'];
  4296. $aliasdomaindata['active_int'] = $row['active'];
  4297. $aliasdomaindata['rl'] = $rl;
  4298. $aliasdomaindata['created'] = $row['created'];
  4299. $aliasdomaindata['modified'] = $row['modified'];
  4300. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
  4301. return false;
  4302. }
  4303. return $aliasdomaindata;
  4304. break;
  4305. case 'shared_aliases':
  4306. $shared_aliases = array();
  4307. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4308. WHERE `goto` REGEXP ','
  4309. AND `address` NOT LIKE '@%'
  4310. AND `goto` != `address`");
  4311. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4312. while($row = array_shift($rows)) {
  4313. $domain = explode("@", $row['address'])[1];
  4314. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4315. $shared_aliases[] = $row['address'];
  4316. }
  4317. }
  4318. return $shared_aliases;
  4319. break;
  4320. case 'direct_aliases':
  4321. $direct_aliases = array();
  4322. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4323. WHERE `goto` NOT LIKE '%,%'
  4324. AND `address` NOT LIKE '@%'
  4325. AND `goto` != `address`");
  4326. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4327. while($row = array_shift($rows)) {
  4328. $domain = explode("@", $row['address'])[1];
  4329. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4330. $direct_aliases[] = $row['address'];
  4331. }
  4332. }
  4333. return $direct_aliases;
  4334. break;
  4335. case 'domains':
  4336. $domains = array();
  4337. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4338. return false;
  4339. }
  4340. if (isset($_extra) && is_array($_extra)){
  4341. // get by tags
  4342. $tags = is_array($_extra) ? $_extra : array();
  4343. // add % as prefix and suffix to every element for relative searching
  4344. $tags = array_map(function($x){ return '%'.$x.'%'; }, $tags);
  4345. $sql = "";
  4346. foreach ($tags as $key => $tag) {
  4347. $sql = $sql."SELECT DISTINCT `domain` FROM `tags_domain` WHERE `tag_name` LIKE ?"; // distinct, avoid duplicates
  4348. if ($key === array_key_last($tags)) break;
  4349. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  4350. }
  4351. $stmt = $pdo->prepare($sql);
  4352. $stmt->execute($tags);
  4353. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4354. while($row = array_shift($rows)) {
  4355. if ($_SESSION['mailcow_cc_role'] == "admin")
  4356. $domains[] = $row['domain'];
  4357. elseif (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['domain']))
  4358. $domains[] = $row['domain'];
  4359. }
  4360. } else {
  4361. // get all
  4362. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  4363. WHERE (`domain` IN (
  4364. SELECT `domain` from `domain_admins`
  4365. WHERE (`active`='1' AND `username` = :username))
  4366. )
  4367. OR 'admin'= :role");
  4368. $stmt->execute(array(
  4369. ':username' => $_SESSION['mailcow_cc_username'],
  4370. ':role' => $_SESSION['mailcow_cc_role'],
  4371. ));
  4372. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4373. while($row = array_shift($rows)) {
  4374. $domains[] = $row['domain'];
  4375. }
  4376. }
  4377. return $domains;
  4378. break;
  4379. case 'domain_details':
  4380. $domaindata = array();
  4381. $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4382. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4383. return false;
  4384. }
  4385. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4386. $stmt->execute(array(
  4387. ':domain' => $_data
  4388. ));
  4389. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4390. if (!empty($row)) {
  4391. $_data = $row['target_domain'];
  4392. }
  4393. $stmt = $pdo->prepare("SELECT
  4394. `domain`,
  4395. `description`,
  4396. `aliases`,
  4397. `mailboxes`,
  4398. `defquota`,
  4399. `maxquota`,
  4400. `created`,
  4401. `modified`,
  4402. `quota`,
  4403. `relayhost`,
  4404. `relay_all_recipients`,
  4405. `relay_unknown_only`,
  4406. `backupmx`,
  4407. `gal`,
  4408. `active`
  4409. FROM `domain` WHERE `domain`= :domain");
  4410. $stmt->execute(array(
  4411. ':domain' => $_data
  4412. ));
  4413. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4414. if (empty($row)) {
  4415. return false;
  4416. }
  4417. $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
  4418. COALESCE(SUM(`quota`), 0) AS `in_use`
  4419. FROM `mailbox`
  4420. WHERE (`kind` = '' OR `kind` = NULL)
  4421. AND `domain` = :domain");
  4422. $stmt->execute(array(':domain' => $row['domain']));
  4423. $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4424. $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
  4425. WHERE `username` IN (
  4426. SELECT `username` FROM `mailbox`
  4427. WHERE `domain` = :domain
  4428. );");
  4429. $stmt->execute(array(':domain' => $row['domain']));
  4430. $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
  4431. $rl = ratelimit('get', 'domain', $_data);
  4432. $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
  4433. if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
  4434. $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
  4435. }
  4436. $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
  4437. if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
  4438. $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
  4439. }
  4440. $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
  4441. if (!empty($SumQuotaInUse['bytes_total'])) {
  4442. $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
  4443. }
  4444. else {
  4445. $domaindata['bytes_total'] = 0;
  4446. }
  4447. if (!empty($SumQuotaInUse['msgs_total'])) {
  4448. $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
  4449. }
  4450. else {
  4451. $domaindata['msgs_total'] = 0;
  4452. }
  4453. $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
  4454. $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
  4455. $domaindata['domain_name'] = $row['domain'];
  4456. $domaindata['domain_h_name'] = idn_to_utf8($row['domain']);
  4457. $domaindata['description'] = $row['description'];
  4458. $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
  4459. $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
  4460. $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
  4461. $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
  4462. $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
  4463. $domaindata['relayhost'] = $row['relayhost'];
  4464. $domaindata['backupmx'] = $row['backupmx'];
  4465. $domaindata['backupmx_int'] = $row['backupmx'];
  4466. $domaindata['gal'] = $row['gal'];
  4467. $domaindata['gal_int'] = $row['gal'];
  4468. $domaindata['rl'] = $rl;
  4469. $domaindata['active'] = $row['active'];
  4470. $domaindata['active_int'] = $row['active'];
  4471. $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
  4472. $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
  4473. $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
  4474. $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
  4475. $domaindata['created'] = $row['created'];
  4476. $domaindata['modified'] = $row['modified'];
  4477. $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
  4478. WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
  4479. AND `address` NOT IN (
  4480. SELECT `username` FROM `mailbox`
  4481. )");
  4482. $stmt->execute(array(
  4483. ':domain' => $_data,
  4484. ':domain2' => $_data
  4485. ));
  4486. $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4487. (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
  4488. $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
  4489. if ($_SESSION['mailcow_cc_role'] == "admin")
  4490. {
  4491. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
  4492. $stmt->execute(array(
  4493. ':domain' => $_data
  4494. ));
  4495. $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
  4496. (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
  4497. }
  4498. $stmt = $pdo->prepare("SELECT `tag_name`
  4499. FROM `tags_domain` WHERE `domain`= :domain");
  4500. $stmt->execute(array(
  4501. ':domain' => $_data
  4502. ));
  4503. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4504. while ($tag = array_shift($tags)) {
  4505. $domaindata['tags'][] = $tag['tag_name'];
  4506. }
  4507. return $domaindata;
  4508. break;
  4509. case 'domain_templates':
  4510. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4511. return false;
  4512. }
  4513. $_data = (isset($_data)) ? intval($_data) : null;
  4514. if (isset($_data)){
  4515. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4516. WHERE `id` = :id AND type = :type");
  4517. $stmt->execute(array(
  4518. ":id" => $_data,
  4519. ":type" => "domain"
  4520. ));
  4521. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4522. if (empty($row)){
  4523. return false;
  4524. }
  4525. $row["attributes"] = json_decode($row["attributes"], true);
  4526. return $row;
  4527. }
  4528. else {
  4529. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'domain'");
  4530. $stmt->execute();
  4531. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4532. if (empty($rows)){
  4533. return false;
  4534. }
  4535. foreach($rows as $key => $row){
  4536. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4537. }
  4538. return $rows;
  4539. }
  4540. break;
  4541. case 'mailbox_details':
  4542. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4543. return false;
  4544. }
  4545. $mailboxdata = array();
  4546. if (preg_match('/y|yes/i', getenv('MASTER'))) {
  4547. $stmt = $pdo->prepare("SELECT
  4548. `domain`.`backupmx`,
  4549. `mailbox`.`username`,
  4550. `mailbox`.`name`,
  4551. `mailbox`.`active`,
  4552. `mailbox`.`domain`,
  4553. `mailbox`.`local_part`,
  4554. `mailbox`.`quota`,
  4555. `mailbox`.`created`,
  4556. `mailbox`.`modified`,
  4557. `quota2`.`bytes`,
  4558. `attributes`,
  4559. `custom_attributes`,
  4560. `quota2`.`messages`
  4561. FROM `mailbox`, `quota2`, `domain`
  4562. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4563. AND `mailbox`.`username` = `quota2`.`username`
  4564. AND `domain`.`domain` = `mailbox`.`domain`
  4565. AND `mailbox`.`username` = :mailbox");
  4566. }
  4567. else {
  4568. $stmt = $pdo->prepare("SELECT
  4569. `domain`.`backupmx`,
  4570. `mailbox`.`username`,
  4571. `mailbox`.`name`,
  4572. `mailbox`.`active`,
  4573. `mailbox`.`domain`,
  4574. `mailbox`.`local_part`,
  4575. `mailbox`.`quota`,
  4576. `mailbox`.`created`,
  4577. `mailbox`.`modified`,
  4578. `quota2replica`.`bytes`,
  4579. `attributes`,
  4580. `custom_attributes`,
  4581. `quota2replica`.`messages`
  4582. FROM `mailbox`, `quota2replica`, `domain`
  4583. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4584. AND `mailbox`.`username` = `quota2replica`.`username`
  4585. AND `domain`.`domain` = `mailbox`.`domain`
  4586. AND `mailbox`.`username` = :mailbox");
  4587. }
  4588. $stmt->execute(array(
  4589. ':mailbox' => $_data,
  4590. ));
  4591. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4592. $mailboxdata['username'] = $row['username'];
  4593. $mailboxdata['active'] = $row['active'];
  4594. $mailboxdata['active_int'] = $row['active'];
  4595. $mailboxdata['domain'] = $row['domain'];
  4596. $mailboxdata['name'] = $row['name'];
  4597. $mailboxdata['local_part'] = $row['local_part'];
  4598. $mailboxdata['quota'] = $row['quota'];
  4599. $mailboxdata['messages'] = $row['messages'];
  4600. $mailboxdata['attributes'] = json_decode($row['attributes'], true);
  4601. $mailboxdata['custom_attributes'] = json_decode($row['custom_attributes'], true);
  4602. $mailboxdata['quota_used'] = intval($row['bytes']);
  4603. $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
  4604. $mailboxdata['created'] = $row['created'];
  4605. $mailboxdata['modified'] = $row['modified'];
  4606. if ($mailboxdata['percent_in_use'] === '- ') {
  4607. $mailboxdata['percent_class'] = "info";
  4608. }
  4609. elseif ($mailboxdata['percent_in_use'] >= 90) {
  4610. $mailboxdata['percent_class'] = "danger";
  4611. }
  4612. elseif ($mailboxdata['percent_in_use'] >= 75) {
  4613. $mailboxdata['percent_class'] = "warning";
  4614. }
  4615. else {
  4616. $mailboxdata['percent_class'] = "success";
  4617. }
  4618. // Determine last logins
  4619. $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
  4620. WHERE `username` = :mailbox
  4621. GROUP BY `service` DESC");
  4622. $stmt->execute(array(':mailbox' => $_data));
  4623. $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4624. foreach ($SaslLogsData as $SaslLogs) {
  4625. if ($SaslLogs['service'] == 'imap') {
  4626. $last_imap_login = strtotime($SaslLogs['datetime']);
  4627. }
  4628. else if ($SaslLogs['service'] == 'smtp') {
  4629. $last_smtp_login = strtotime($SaslLogs['datetime']);
  4630. }
  4631. else if ($SaslLogs['service'] == 'pop3') {
  4632. $last_pop3_login = strtotime($SaslLogs['datetime']);
  4633. }
  4634. }
  4635. if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4636. $last_imap_login = 0;
  4637. }
  4638. if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4639. $last_smtp_login = 0;
  4640. }
  4641. if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4642. $last_pop3_login = 0;
  4643. }
  4644. $mailboxdata['last_imap_login'] = $last_imap_login;
  4645. $mailboxdata['last_smtp_login'] = $last_smtp_login;
  4646. $mailboxdata['last_pop3_login'] = $last_pop3_login;
  4647. if (!isset($_extra) || $_extra != 'reduced') {
  4648. $rl = ratelimit('get', 'mailbox', $_data);
  4649. $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
  4650. $stmt->execute(array(':domain' => $row['domain']));
  4651. $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
  4652. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
  4653. $stmt->execute(array(':username' => $_data));
  4654. $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
  4655. $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
  4656. $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
  4657. $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4658. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
  4659. $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
  4660. $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4661. $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
  4662. $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
  4663. $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
  4664. if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
  4665. $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
  4666. }
  4667. if (!empty($rl)) {
  4668. $mailboxdata['rl'] = $rl;
  4669. $mailboxdata['rl_scope'] = 'mailbox';
  4670. }
  4671. else {
  4672. $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
  4673. $mailboxdata['rl_scope'] = 'domain';
  4674. }
  4675. $mailboxdata['is_relayed'] = $row['backupmx'];
  4676. }
  4677. $stmt = $pdo->prepare("SELECT `tag_name`
  4678. FROM `tags_mailbox` WHERE `username`= :username");
  4679. $stmt->execute(array(
  4680. ':username' => $_data
  4681. ));
  4682. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4683. while ($tag = array_shift($tags)) {
  4684. $mailboxdata['tags'][] = $tag['tag_name'];
  4685. }
  4686. return $mailboxdata;
  4687. break;
  4688. case 'mailbox_templates':
  4689. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4690. return false;
  4691. }
  4692. $_data = (isset($_data)) ? intval($_data) : null;
  4693. if (isset($_data)){
  4694. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4695. WHERE `id` = :id AND type = :type");
  4696. $stmt->execute(array(
  4697. ":id" => $_data,
  4698. ":type" => "mailbox"
  4699. ));
  4700. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4701. if (empty($row)){
  4702. return false;
  4703. }
  4704. $row["attributes"] = json_decode($row["attributes"], true);
  4705. return $row;
  4706. }
  4707. else {
  4708. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'mailbox'");
  4709. $stmt->execute();
  4710. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4711. if (empty($rows)){
  4712. return false;
  4713. }
  4714. foreach($rows as $key => $row){
  4715. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4716. }
  4717. return $rows;
  4718. }
  4719. break;
  4720. case 'resource_details':
  4721. $resourcedata = array();
  4722. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4723. return false;
  4724. }
  4725. $stmt = $pdo->prepare("SELECT
  4726. `username`,
  4727. `name`,
  4728. `kind`,
  4729. `multiple_bookings`,
  4730. `local_part`,
  4731. `active`,
  4732. `domain`
  4733. FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
  4734. $stmt->execute(array(
  4735. ':resource' => $_data,
  4736. ));
  4737. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4738. $resourcedata['name'] = $row['username'];
  4739. $resourcedata['kind'] = $row['kind'];
  4740. $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
  4741. $resourcedata['description'] = $row['name'];
  4742. $resourcedata['active'] = $row['active'];
  4743. $resourcedata['active_int'] = $row['active'];
  4744. $resourcedata['domain'] = $row['domain'];
  4745. $resourcedata['local_part'] = $row['local_part'];
  4746. if (!isset($resourcedata['domain']) ||
  4747. (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
  4748. return false;
  4749. }
  4750. return $resourcedata;
  4751. break;
  4752. case 'domain_wide_footer':
  4753. $domain = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4754. if (!is_valid_domain_name($domain)) {
  4755. $_SESSION['return'][] = array(
  4756. 'type' => 'danger',
  4757. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4758. 'msg' => 'domain_invalid'
  4759. );
  4760. return false;
  4761. }
  4762. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4763. $_SESSION['return'][] = array(
  4764. 'type' => 'danger',
  4765. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4766. 'msg' => 'access_denied'
  4767. );
  4768. return false;
  4769. }
  4770. try {
  4771. $stmt = $pdo->prepare("SELECT `html`, `plain`, `mbox_exclude`, `alias_domain_exclude`, `skip_replies` FROM `domain_wide_footer`
  4772. WHERE `domain` = :domain");
  4773. $stmt->execute(array(
  4774. ':domain' => $domain
  4775. ));
  4776. $footer = $stmt->fetch(PDO::FETCH_ASSOC);
  4777. }
  4778. catch (PDOException $e) {
  4779. $_SESSION['return'][] = array(
  4780. 'type' => 'danger',
  4781. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4782. 'msg' => $e->getMessage()
  4783. );
  4784. return false;
  4785. }
  4786. return $footer;
  4787. break;
  4788. }
  4789. break;
  4790. case 'delete':
  4791. switch ($_type) {
  4792. case 'syncjob':
  4793. if (!is_array($_data['id'])) {
  4794. $ids = array();
  4795. $ids[] = $_data['id'];
  4796. }
  4797. else {
  4798. $ids = $_data['id'];
  4799. }
  4800. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  4801. $_SESSION['return'][] = array(
  4802. 'type' => 'danger',
  4803. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4804. 'msg' => 'access_denied'
  4805. );
  4806. return false;
  4807. }
  4808. foreach ($ids as $id) {
  4809. if (!is_numeric($id)) {
  4810. return false;
  4811. }
  4812. $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
  4813. $stmt->execute(array(':id' => $id));
  4814. $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
  4815. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
  4816. $_SESSION['return'][] = array(
  4817. 'type' => 'danger',
  4818. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4819. 'msg' => 'access_denied'
  4820. );
  4821. continue;
  4822. }
  4823. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
  4824. $stmt->execute(array(':id' => $id));
  4825. $_SESSION['return'][] = array(
  4826. 'type' => 'success',
  4827. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4828. 'msg' => array('deleted_syncjob', $id)
  4829. );
  4830. }
  4831. break;
  4832. case 'filter':
  4833. if (!is_array($_data['id'])) {
  4834. $ids = array();
  4835. $ids[] = $_data['id'];
  4836. }
  4837. else {
  4838. $ids = $_data['id'];
  4839. }
  4840. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  4841. $_SESSION['return'][] = array(
  4842. 'type' => 'danger',
  4843. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4844. 'msg' => 'access_denied'
  4845. );
  4846. return false;
  4847. }
  4848. foreach ($ids as $id) {
  4849. if (!is_numeric($id)) {
  4850. continue;
  4851. }
  4852. $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
  4853. $stmt->execute(array(':id' => $id));
  4854. $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
  4855. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
  4856. $_SESSION['return'][] = array(
  4857. 'type' => 'danger',
  4858. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4859. 'msg' => 'access_denied'
  4860. );
  4861. continue;
  4862. }
  4863. $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
  4864. $stmt->execute(array(':id' => $id));
  4865. $_SESSION['return'][] = array(
  4866. 'type' => 'success',
  4867. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4868. 'msg' => array('delete_filter', $id)
  4869. );
  4870. }
  4871. break;
  4872. case 'time_limited_alias':
  4873. if (!is_array($_data['address'])) {
  4874. $addresses = array();
  4875. $addresses[] = $_data['address'];
  4876. }
  4877. else {
  4878. $addresses = $_data['address'];
  4879. }
  4880. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  4881. $_SESSION['return'][] = array(
  4882. 'type' => 'danger',
  4883. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4884. 'msg' => 'access_denied'
  4885. );
  4886. return false;
  4887. }
  4888. foreach ($addresses as $address) {
  4889. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  4890. $stmt->execute(array(':address' => $address));
  4891. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  4892. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  4893. $_SESSION['return'][] = array(
  4894. 'type' => 'danger',
  4895. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4896. 'msg' => 'access_denied'
  4897. );
  4898. continue;
  4899. }
  4900. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
  4901. $stmt->execute(array(
  4902. ':username' => $goto,
  4903. ':item' => $address
  4904. ));
  4905. $_SESSION['return'][] = array(
  4906. 'type' => 'success',
  4907. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4908. 'msg' => array('mailbox_modified', htmlspecialchars($goto))
  4909. );
  4910. }
  4911. break;
  4912. case 'eas_cache':
  4913. if (!is_array($_data['username'])) {
  4914. $usernames = array();
  4915. $usernames[] = $_data['username'];
  4916. }
  4917. else {
  4918. $usernames = $_data['username'];
  4919. }
  4920. if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
  4921. $_SESSION['return'][] = array(
  4922. 'type' => 'danger',
  4923. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4924. 'msg' => 'access_denied'
  4925. );
  4926. return false;
  4927. }
  4928. foreach ($usernames as $username) {
  4929. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4930. $_SESSION['return'][] = array(
  4931. 'type' => 'danger',
  4932. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4933. 'msg' => 'access_denied'
  4934. );
  4935. continue;
  4936. }
  4937. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4938. $stmt->execute(array(
  4939. ':username' => $username
  4940. ));
  4941. $_SESSION['return'][] = array(
  4942. 'type' => 'success',
  4943. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4944. 'msg' => array('eas_reset', htmlspecialchars($username))
  4945. );
  4946. }
  4947. break;
  4948. case 'sogo_profile':
  4949. if (!is_array($_data['username'])) {
  4950. $usernames = array();
  4951. $usernames[] = $_data['username'];
  4952. }
  4953. else {
  4954. $usernames = $_data['username'];
  4955. }
  4956. if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
  4957. $_SESSION['return'][] = array(
  4958. 'type' => 'danger',
  4959. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4960. 'msg' => 'access_denied'
  4961. );
  4962. return false;
  4963. }
  4964. foreach ($usernames as $username) {
  4965. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4966. $_SESSION['return'][] = array(
  4967. 'type' => 'danger',
  4968. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4969. 'msg' => 'access_denied'
  4970. );
  4971. continue;
  4972. }
  4973. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  4974. $stmt->execute(array(
  4975. ':username' => $username
  4976. ));
  4977. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4978. $stmt->execute(array(
  4979. ':username' => $username
  4980. ));
  4981. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
  4982. $stmt->execute(array(
  4983. ':username' => $username
  4984. ));
  4985. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4986. $stmt->execute(array(
  4987. ':username' => $username
  4988. ));
  4989. $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)");
  4990. $stmt->execute(array(
  4991. ':username' => $username
  4992. ));
  4993. $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)");
  4994. $stmt->execute(array(
  4995. ':username' => $username
  4996. ));
  4997. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  4998. $stmt->execute(array(
  4999. ':username' => $username
  5000. ));
  5001. $_SESSION['return'][] = array(
  5002. 'type' => 'success',
  5003. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5004. 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
  5005. );
  5006. }
  5007. break;
  5008. case 'domain':
  5009. if (!is_array($_data['domain'])) {
  5010. $domains = array();
  5011. $domains[] = $_data['domain'];
  5012. }
  5013. else {
  5014. $domains = $_data['domain'];
  5015. }
  5016. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5017. $_SESSION['return'][] = array(
  5018. 'type' => 'danger',
  5019. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5020. 'msg' => 'access_denied'
  5021. );
  5022. return false;
  5023. }
  5024. foreach ($domains as $domain) {
  5025. if (!is_valid_domain_name($domain)) {
  5026. $_SESSION['return'][] = array(
  5027. 'type' => 'danger',
  5028. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5029. 'msg' => 'domain_invalid'
  5030. );
  5031. continue;
  5032. }
  5033. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  5034. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  5035. WHERE `domain` = :domain");
  5036. $stmt->execute(array(':domain' => $domain));
  5037. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  5038. if ($num_results != 0 || !empty($num_results)) {
  5039. $_SESSION['return'][] = array(
  5040. 'type' => 'danger',
  5041. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5042. 'msg' => array('domain_not_empty', $domain)
  5043. );
  5044. continue;
  5045. }
  5046. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
  5047. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  5048. if ($maildir_gc['type'] != 'success') {
  5049. $_SESSION['return'][] = array(
  5050. 'type' => 'warning',
  5051. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5052. 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
  5053. );
  5054. }
  5055. $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
  5056. $stmt->execute(array(
  5057. ':domain' => $domain,
  5058. ));
  5059. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
  5060. $stmt->execute(array(
  5061. ':domain' => $domain,
  5062. ));
  5063. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
  5064. $stmt->execute(array(
  5065. ':domain' => $domain,
  5066. ));
  5067. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
  5068. $stmt->execute(array(
  5069. ':domain' => $domain,
  5070. ));
  5071. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
  5072. $stmt->execute(array(
  5073. ':domain' => $domain,
  5074. ));
  5075. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
  5076. $stmt->execute(array(
  5077. ':domain' => '%@'.$domain,
  5078. ));
  5079. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
  5080. $stmt->execute(array(
  5081. ':domain' => '%@'.$domain,
  5082. ));
  5083. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
  5084. $stmt->execute(array(
  5085. ':domain' => '%@'.$domain,
  5086. ));
  5087. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
  5088. $stmt->execute(array(
  5089. ':domain' => '%@'.$domain,
  5090. ));
  5091. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5092. $stmt->execute(array(
  5093. ':domain' => '%@'.$domain,
  5094. ));
  5095. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
  5096. $stmt->execute(array(
  5097. ':domain' => $domain,
  5098. ));
  5099. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
  5100. $stmt->execute(array(
  5101. ':domain' => $domain,
  5102. ));
  5103. $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5104. $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5105. try {
  5106. $redis->hDel('DOMAIN_MAP', $domain);
  5107. $redis->hDel('RL_VALUE', $domain);
  5108. }
  5109. catch (RedisException $e) {
  5110. $_SESSION['return'][] = array(
  5111. 'type' => 'danger',
  5112. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5113. 'msg' => array('redis_error', $e)
  5114. );
  5115. continue;
  5116. }
  5117. $_SESSION['return'][] = array(
  5118. 'type' => 'success',
  5119. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5120. 'msg' => array('domain_removed', htmlspecialchars($domain))
  5121. );
  5122. }
  5123. break;
  5124. case 'domain_templates':
  5125. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5126. $_SESSION['return'][] = array(
  5127. 'type' => 'danger',
  5128. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5129. 'msg' => 'access_denied'
  5130. );
  5131. return false;
  5132. }
  5133. if (!is_array($_data['ids'])) {
  5134. $ids = array();
  5135. $ids[] = $_data['ids'];
  5136. }
  5137. else {
  5138. $ids = $_data['ids'];
  5139. }
  5140. foreach ($ids as $id) {
  5141. // delete template
  5142. $stmt = $pdo->prepare("DELETE FROM `templates`
  5143. WHERE id = :id AND type = :type AND NOT template = :template");
  5144. $stmt->execute(array(
  5145. ":id" => $id,
  5146. ":type" => "domain",
  5147. ":template" => "Default"
  5148. ));
  5149. $_SESSION['return'][] = array(
  5150. 'type' => 'success',
  5151. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5152. 'msg' => array('template_removed', htmlspecialchars($id))
  5153. );
  5154. return true;
  5155. }
  5156. break;
  5157. case 'alias':
  5158. if (!is_array($_data['id'])) {
  5159. $ids = array();
  5160. $ids[] = $_data['id'];
  5161. }
  5162. else {
  5163. $ids = $_data['id'];
  5164. }
  5165. foreach ($ids as $id) {
  5166. $alias_data = mailbox('get', 'alias_details', $id);
  5167. if (empty($alias_data)) {
  5168. $_SESSION['return'][] = array(
  5169. 'type' => 'danger',
  5170. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5171. 'msg' => 'access_denied'
  5172. );
  5173. continue;
  5174. }
  5175. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
  5176. $stmt->execute(array(
  5177. ':id' => $alias_data['id']
  5178. ));
  5179. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
  5180. $stmt->execute(array(
  5181. ':alias_address' => $alias_data['address']
  5182. ));
  5183. $_SESSION['return'][] = array(
  5184. 'type' => 'success',
  5185. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5186. 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
  5187. );
  5188. }
  5189. break;
  5190. case 'alias_domain':
  5191. if (!is_array($_data['alias_domain'])) {
  5192. $alias_domains = array();
  5193. $alias_domains[] = $_data['alias_domain'];
  5194. }
  5195. else {
  5196. $alias_domains = $_data['alias_domain'];
  5197. }
  5198. foreach ($alias_domains as $alias_domain) {
  5199. if (!is_valid_domain_name($alias_domain)) {
  5200. $_SESSION['return'][] = array(
  5201. 'type' => 'danger',
  5202. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5203. 'msg' => 'domain_invalid'
  5204. );
  5205. continue;
  5206. }
  5207. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
  5208. WHERE `alias_domain`= :alias_domain");
  5209. $stmt->execute(array(':alias_domain' => $alias_domain));
  5210. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  5211. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
  5212. $_SESSION['return'][] = array(
  5213. 'type' => 'danger',
  5214. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5215. 'msg' => 'access_denied'
  5216. );
  5217. continue;
  5218. }
  5219. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  5220. $stmt->execute(array(
  5221. ':alias_domain' => $alias_domain,
  5222. ));
  5223. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
  5224. $stmt->execute(array(
  5225. ':alias_domain' => $alias_domain,
  5226. ));
  5227. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5228. $stmt->execute(array(
  5229. ':domain' => '%@'.$alias_domain,
  5230. ));
  5231. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
  5232. $stmt->execute(array(
  5233. ':alias_domain' => $alias_domain,
  5234. ));
  5235. try {
  5236. $redis->hDel('DOMAIN_MAP', $alias_domain);
  5237. $redis->hDel('RL_VALUE', $domain);
  5238. }
  5239. catch (RedisException $e) {
  5240. $_SESSION['return'][] = array(
  5241. 'type' => 'danger',
  5242. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5243. 'msg' => array('redis_error', $e)
  5244. );
  5245. continue;
  5246. }
  5247. $_SESSION['return'][] = array(
  5248. 'type' => 'success',
  5249. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5250. 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
  5251. );
  5252. }
  5253. break;
  5254. case 'mailbox':
  5255. if (!is_array($_data['username'])) {
  5256. $usernames = array();
  5257. $usernames[] = $_data['username'];
  5258. }
  5259. else {
  5260. $usernames = $_data['username'];
  5261. }
  5262. foreach ($usernames as $username) {
  5263. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5264. $_SESSION['return'][] = array(
  5265. 'type' => 'danger',
  5266. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5267. 'msg' => 'access_denied'
  5268. );
  5269. continue;
  5270. }
  5271. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  5272. $_SESSION['return'][] = array(
  5273. 'type' => 'danger',
  5274. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5275. 'msg' => 'access_denied'
  5276. );
  5277. continue;
  5278. }
  5279. $mailbox_details = mailbox('get', 'mailbox_details', $username);
  5280. if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
  5281. $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
  5282. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
  5283. if (getenv("CLUSTERMODE") == "replication") {
  5284. // broadcast to each dovecot container
  5285. docker('broadcast', 'dovecot-mailcow', 'exec', $exec_fields);
  5286. } else {
  5287. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  5288. if ($maildir_gc['type'] != 'success') {
  5289. $_SESSION['return'][] = array(
  5290. 'type' => 'warning',
  5291. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5292. 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
  5293. );
  5294. }
  5295. }
  5296. }
  5297. else {
  5298. $_SESSION['return'][] = array(
  5299. 'type' => 'warning',
  5300. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5301. 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
  5302. );
  5303. }
  5304. if (strtolower(getenv('SKIP_SOLR')) == 'n' && strtolower(getenv('FLATCURVE_EXPERIMENTAL')) != 'y') {
  5305. $curl = curl_init();
  5306. curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
  5307. curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
  5308. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  5309. curl_setopt($curl, CURLOPT_POST, 1);
  5310. curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
  5311. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  5312. $response = curl_exec($curl);
  5313. if ($response === false) {
  5314. $err = curl_error($curl);
  5315. $_SESSION['return'][] = array(
  5316. 'type' => 'warning',
  5317. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5318. 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
  5319. );
  5320. }
  5321. curl_close($curl);
  5322. }
  5323. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
  5324. $stmt->execute(array(
  5325. ':username' => $username
  5326. ));
  5327. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
  5328. $stmt->execute(array(
  5329. ':username' => $username
  5330. ));
  5331. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
  5332. $stmt->execute(array(
  5333. ':username' => $username
  5334. ));
  5335. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
  5336. $stmt->execute(array(
  5337. ':username' => $username
  5338. ));
  5339. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
  5340. $stmt->execute(array(
  5341. ':username' => $username
  5342. ));
  5343. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5344. $stmt->execute(array(
  5345. ':username' => $username
  5346. ));
  5347. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as OR `send_as` = :send_as");
  5348. $stmt->execute(array(
  5349. ':logged_in_as' => $username,
  5350. ':send_as' => $username
  5351. ));
  5352. // fk, better safe than sorry
  5353. $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
  5354. $stmt->execute(array(
  5355. ':username' => $username
  5356. ));
  5357. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
  5358. $stmt->execute(array(
  5359. ':username' => $username
  5360. ));
  5361. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
  5362. $stmt->execute(array(
  5363. ':username' => $username
  5364. ));
  5365. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
  5366. $stmt->execute(array(
  5367. ':username' => $username
  5368. ));
  5369. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5370. $stmt->execute(array(
  5371. ':username' => $username
  5372. ));
  5373. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5374. $stmt->execute(array(
  5375. ':username' => $username
  5376. ));
  5377. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
  5378. $stmt->execute(array(
  5379. ':username' => $username
  5380. ));
  5381. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5382. $stmt->execute(array(
  5383. ':username' => $username
  5384. ));
  5385. $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)");
  5386. $stmt->execute(array(
  5387. ':username' => $username
  5388. ));
  5389. $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)");
  5390. $stmt->execute(array(
  5391. ':username' => $username
  5392. ));
  5393. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5394. $stmt->execute(array(
  5395. ':username' => $username
  5396. ));
  5397. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
  5398. $stmt->execute(array(
  5399. ':username' => $username
  5400. ));
  5401. $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
  5402. $stmt->execute(array(
  5403. ':username' => $username
  5404. ));
  5405. $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
  5406. $stmt->execute(array(
  5407. ':username' => $username
  5408. ));
  5409. $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
  5410. $stmt->execute(array(
  5411. ':username' => $username
  5412. ));
  5413. $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
  5414. $stmt->execute(array(
  5415. ':username' => $username,
  5416. ));
  5417. $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
  5418. $stmt->execute(array(
  5419. ':username' => $username,
  5420. ));
  5421. $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
  5422. WHERE `goto` REGEXP :username");
  5423. $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
  5424. $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  5425. foreach ($GotoData as $gotos) {
  5426. $goto_exploded = explode(',', $gotos['goto']);
  5427. if (($key = array_search($username, $goto_exploded)) !== false) {
  5428. unset($goto_exploded[$key]);
  5429. }
  5430. $gotos_rebuild = implode(',', (array)$goto_exploded);
  5431. $stmt = $pdo->prepare("UPDATE `alias` SET
  5432. `goto` = :goto
  5433. WHERE `address` = :address");
  5434. $stmt->execute(array(
  5435. ':goto' => $gotos_rebuild,
  5436. ':address' => $gotos['address']
  5437. ));
  5438. }
  5439. try {
  5440. $redis->hDel('RL_VALUE', $username);
  5441. }
  5442. catch (RedisException $e) {
  5443. $_SESSION['return'][] = array(
  5444. 'type' => 'danger',
  5445. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5446. 'msg' => array('redis_error', $e)
  5447. );
  5448. continue;
  5449. }
  5450. update_sogo_static_view($username);
  5451. $_SESSION['return'][] = array(
  5452. 'type' => 'success',
  5453. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5454. 'msg' => array('mailbox_removed', htmlspecialchars($username))
  5455. );
  5456. }
  5457. return true;
  5458. break;
  5459. case 'mailbox_templates':
  5460. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5461. $_SESSION['return'][] = array(
  5462. 'type' => 'danger',
  5463. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5464. 'msg' => 'access_denied'
  5465. );
  5466. return false;
  5467. }
  5468. if (!is_array($_data['ids'])) {
  5469. $ids = array();
  5470. $ids[] = $_data['ids'];
  5471. }
  5472. else {
  5473. $ids = $_data['ids'];
  5474. }
  5475. foreach ($ids as $id) {
  5476. // delete template
  5477. $stmt = $pdo->prepare("DELETE FROM `templates`
  5478. WHERE id = :id AND type = :type AND NOT template = :template");
  5479. $stmt->execute(array(
  5480. ":id" => $id,
  5481. ":type" => "mailbox",
  5482. ":template" => "Default"
  5483. ));
  5484. }
  5485. $_SESSION['return'][] = array(
  5486. 'type' => 'success',
  5487. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5488. 'msg' => 'template_removed'
  5489. );
  5490. return true;
  5491. break;
  5492. case 'resource':
  5493. if (!is_array($_data['name'])) {
  5494. $names = array();
  5495. $names[] = $_data['name'];
  5496. }
  5497. else {
  5498. $names = $_data['name'];
  5499. }
  5500. foreach ($names as $name) {
  5501. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  5502. $_SESSION['return'][] = array(
  5503. 'type' => 'danger',
  5504. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5505. 'msg' => 'access_denied'
  5506. );
  5507. continue;
  5508. }
  5509. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  5510. $_SESSION['return'][] = array(
  5511. 'type' => 'danger',
  5512. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5513. 'msg' => 'access_denied'
  5514. );
  5515. continue;
  5516. }
  5517. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5518. $stmt->execute(array(
  5519. ':username' => $name
  5520. ));
  5521. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5522. $stmt->execute(array(
  5523. ':username' => $name
  5524. ));
  5525. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5526. $stmt->execute(array(
  5527. ':username' => $name
  5528. ));
  5529. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
  5530. $stmt->execute(array(
  5531. ':username' => $name
  5532. ));
  5533. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5534. $stmt->execute(array(
  5535. ':username' => $name
  5536. ));
  5537. $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)");
  5538. $stmt->execute(array(
  5539. ':username' => $name
  5540. ));
  5541. $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)");
  5542. $stmt->execute(array(
  5543. ':username' => $name
  5544. ));
  5545. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5546. $stmt->execute(array(
  5547. ':username' => $name
  5548. ));
  5549. $_SESSION['return'][] = array(
  5550. 'type' => 'success',
  5551. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5552. 'msg' => array('resource_removed', htmlspecialchars($name))
  5553. );
  5554. }
  5555. break;
  5556. case 'tags_domain':
  5557. if (!is_array($_data['domain'])) {
  5558. $domains = array();
  5559. $domains[] = $_data['domain'];
  5560. }
  5561. else {
  5562. $domains = $_data['domain'];
  5563. }
  5564. $tags = $_data['tags'];
  5565. if (!is_array($tags)) $tags = array();
  5566. $wasModified = false;
  5567. foreach ($domains as $domain) {
  5568. if (!is_valid_domain_name($domain)) {
  5569. $_SESSION['return'][] = array(
  5570. 'type' => 'danger',
  5571. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5572. 'msg' => 'domain_invalid'
  5573. );
  5574. continue;
  5575. }
  5576. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5577. $_SESSION['return'][] = array(
  5578. 'type' => 'danger',
  5579. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5580. 'msg' => 'access_denied'
  5581. );
  5582. return false;
  5583. }
  5584. foreach($tags as $tag){
  5585. // delete tag
  5586. $wasModified = true;
  5587. $stmt = $pdo->prepare("DELETE FROM `tags_domain` WHERE `domain` = :domain AND `tag_name` = :tag_name");
  5588. $stmt->execute(array(
  5589. ':domain' => $domain,
  5590. ':tag_name' => $tag,
  5591. ));
  5592. }
  5593. }
  5594. if (!$wasModified) return false;
  5595. $_SESSION['return'][] = array(
  5596. 'type' => 'success',
  5597. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5598. 'msg' => array('domain_modified', $domain)
  5599. );
  5600. break;
  5601. case 'tags_mailbox':
  5602. if (!is_array($_data['username'])) {
  5603. $usernames = array();
  5604. $usernames[] = $_data['username'];
  5605. }
  5606. else {
  5607. $usernames = $_data['username'];
  5608. }
  5609. $tags = $_data['tags'];
  5610. if (!is_array($tags)) $tags = array();
  5611. $wasModified = false;
  5612. foreach ($usernames as $username) {
  5613. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5614. $_SESSION['return'][] = array(
  5615. 'type' => 'danger',
  5616. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5617. 'msg' => 'email invalid'
  5618. );
  5619. continue;
  5620. }
  5621. $is_now = mailbox('get', 'mailbox_details', $username);
  5622. $domain = $is_now['domain'];
  5623. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5624. $_SESSION['return'][] = array(
  5625. 'type' => 'danger',
  5626. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5627. 'msg' => 'access_denied'
  5628. );
  5629. continue;
  5630. }
  5631. // delete tags
  5632. foreach($tags as $tag){
  5633. $wasModified = true;
  5634. $stmt = $pdo->prepare("DELETE FROM `tags_mailbox` WHERE `username` = :username AND `tag_name` = :tag_name");
  5635. $stmt->execute(array(
  5636. ':username' => $username,
  5637. ':tag_name' => $tag,
  5638. ));
  5639. }
  5640. }
  5641. if (!$wasModified) return false;
  5642. $_SESSION['return'][] = array(
  5643. 'type' => 'success',
  5644. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5645. 'msg' => array('mailbox_modified', $username)
  5646. );
  5647. break;
  5648. }
  5649. break;
  5650. }
  5651. if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'resource')) && getenv('SKIP_SOGO') != "y") {
  5652. update_sogo_static_view();
  5653. }
  5654. }