functions.mailbox.inc.php 289 KB

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