functions.mailbox.inc.php 269 KB

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