functions.mailbox.inc.php 298 KB

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