functions.mailbox.inc.php 279 KB

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