functions.mailbox.inc.php 288 KB

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