2
0

functions.mailbox.inc.php 289 KB

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