functions.mailbox.inc.php 252 KB

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