functions.mailbox.inc.php 293 KB

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