functions.mailbox.inc.php 272 KB

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