2
0

functions.mailbox.inc.php 280 KB

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