functions.mailbox.inc.php 253 KB

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