functions.mailbox.inc.php 265 KB

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