functions.mailbox.inc.php 271 KB

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