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