functions.mailbox.inc.php 272 KB

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