functions.mailbox.inc.php 261 KB

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