functions.mailbox.inc.php 280 KB

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