functions.mailbox.inc.php 269 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737
  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. // rename username in sql
  3207. try {
  3208. $pdo->beginTransaction();
  3209. $pdo->exec('SET FOREIGN_KEY_CHECKS = 0');
  3210. $pdo->prepare('UPDATE mailbox SET username = :new_username, local_part = :new_local_part WHERE username = :old_username')
  3211. ->execute([
  3212. ':new_username' => $new_username,
  3213. ':new_local_part' => $new_local_part,
  3214. ':old_username' => $old_username
  3215. ]);
  3216. // Update the username in all related tables
  3217. $tables = [
  3218. 'tags_mailbox' => 'username',
  3219. 'sieve_filters' => 'username',
  3220. 'app_passwd' => 'mailbox',
  3221. 'user_acl' => 'username',
  3222. 'da_acl' => 'username',
  3223. 'quota2' => 'username',
  3224. 'quota2replica' => 'username',
  3225. 'pushover' => 'username'
  3226. ];
  3227. foreach ($tables as $table => $column) {
  3228. $pdo->prepare("UPDATE $table SET $column = :new_username WHERE $column = :old_username")
  3229. ->execute([
  3230. ':new_username' => $new_username,
  3231. ':old_username' => $old_username
  3232. ]);
  3233. }
  3234. $pdo->prepare("UPDATE _sogo_static_view SET c_uid = :new_username, c_name = :new_username2, mail = :new_username3 WHERE c_uid = :old_username")
  3235. ->execute([
  3236. ':new_username' => $new_username,
  3237. ':new_username2' => $new_username,
  3238. ':new_username3' => $new_username,
  3239. ':old_username' => $old_username
  3240. ]);
  3241. $pdo->prepare("UPDATE alias SET address = :new_username, goto = :new_username2 WHERE address = :old_username")
  3242. ->execute([
  3243. ':new_username' => $new_username,
  3244. ':new_username2' => $new_username,
  3245. ':old_username' => $old_username
  3246. ]);
  3247. // Re-enable foreign key checks
  3248. $pdo->exec('SET FOREIGN_KEY_CHECKS = 1');
  3249. $pdo->commit();
  3250. } catch (PDOException $e) {
  3251. // Rollback the transaction if something goes wrong
  3252. $pdo->rollBack();
  3253. $_SESSION['return'][] = array(
  3254. 'type' => 'danger',
  3255. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3256. 'msg' => $e->getMessage()
  3257. );
  3258. }
  3259. // move maildir
  3260. $exec_fields = array(
  3261. 'cmd' => 'maildir',
  3262. 'task' => 'move',
  3263. 'old_maildir' => $domain . '/' . $old_local_part,
  3264. 'new_maildir' => $domain . '/' . $new_local_part
  3265. );
  3266. docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3267. // rename username in sogo
  3268. $exec_fields = array(
  3269. 'cmd' => 'sogo',
  3270. 'task' => 'rename_user',
  3271. 'old_username' => $old_username,
  3272. 'new_username' => $new_username
  3273. );
  3274. docker('post', 'sogo-mailcow', 'exec', $exec_fields);
  3275. // create alias
  3276. if ($create_alias == 1) {
  3277. mailbox("add", "alias", array(
  3278. "address" => $old_username,
  3279. "goto" => $new_username,
  3280. "active" => 1,
  3281. "sogo_visible" => 1,
  3282. "private_comment" => sprintf($lang['success']['mailbox_renamed'], $old_username, $new_username)
  3283. ));
  3284. }
  3285. $_SESSION['return'][] = array(
  3286. 'type' => 'success',
  3287. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3288. 'msg' => array('mailbox_renamed', $old_username, $new_username)
  3289. );
  3290. break;
  3291. case 'mailbox_templates':
  3292. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3293. $_SESSION['return'][] = array(
  3294. 'type' => 'danger',
  3295. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3296. 'msg' => 'access_denied'
  3297. );
  3298. return false;
  3299. }
  3300. if (!is_array($_data['ids'])) {
  3301. $ids = array();
  3302. $ids[] = $_data['ids'];
  3303. }
  3304. else {
  3305. $ids = $_data['ids'];
  3306. }
  3307. foreach ($ids as $id) {
  3308. $is_now = mailbox("get", "mailbox_templates", $id);
  3309. if (empty($is_now) ||
  3310. $is_now["type"] != "mailbox"){
  3311. $_SESSION['return'][] = array(
  3312. 'type' => 'danger',
  3313. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3314. 'msg' => 'template_id_invalid'
  3315. );
  3316. continue;
  3317. }
  3318. // check name
  3319. if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
  3320. // keep template name of Default template
  3321. $_data["template"] = $is_now["template"];
  3322. }
  3323. else {
  3324. $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
  3325. }
  3326. // check attributes
  3327. $attr = array();
  3328. $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
  3329. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : $is_now['tags'];
  3330. $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
  3331. $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
  3332. $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : $is_now['rl_frame'];
  3333. $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : $is_now['rl_value'];
  3334. $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : $is_now['force_pw_update'];
  3335. $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : $is_now['sogo_access'];
  3336. $attr["active"] = isset($_data['active']) ? intval($_data['active']) : $is_now['active'];
  3337. $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
  3338. $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
  3339. if (isset($_data['protocol_access'])) {
  3340. $_data['protocol_access'] = (array)$_data['protocol_access'];
  3341. $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  3342. $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  3343. $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  3344. $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
  3345. }
  3346. else {
  3347. foreach ($is_now as $key => $value){
  3348. $attr[$key] = $is_now[$key];
  3349. }
  3350. }
  3351. if (isset($_data['acl'])) {
  3352. $_data['acl'] = (array)$_data['acl'];
  3353. $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
  3354. $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
  3355. $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
  3356. $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
  3357. $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
  3358. $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
  3359. $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
  3360. $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
  3361. $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
  3362. $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
  3363. $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
  3364. $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
  3365. $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
  3366. $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
  3367. $attr['acl_pw_reset'] = (in_array('pw_reset', $_data['acl'])) ? 1 : 0;
  3368. } else {
  3369. foreach ($is_now as $key => $value){
  3370. $attr[$key] = $is_now[$key];
  3371. }
  3372. }
  3373. // update template
  3374. $stmt = $pdo->prepare("UPDATE `templates`
  3375. SET `template` = :template, `attributes` = :attributes
  3376. WHERE id = :id");
  3377. $stmt->execute(array(
  3378. ":id" => $id ,
  3379. ":template" => $_data["template"] ,
  3380. ":attributes" => json_encode($attr)
  3381. ));
  3382. }
  3383. $_SESSION['return'][] = array(
  3384. 'type' => 'success',
  3385. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3386. 'msg' => array('template_modified', $_data["template"])
  3387. );
  3388. return true;
  3389. break;
  3390. case 'mailbox_custom_attribute':
  3391. $_data['attribute'] = isset($_data['attribute']) ? $_data['attribute'] : array();
  3392. $_data['attribute'] = is_array($_data['attribute']) ? $_data['attribute'] : array($_data['attribute']);
  3393. $_data['attribute'] = array_map(function($value) { return str_replace(' ', '', $value); }, $_data['attribute']);
  3394. $_data['value'] = isset($_data['value']) ? $_data['value'] : array();
  3395. $_data['value'] = is_array($_data['value']) ? $_data['value'] : array($_data['value']);
  3396. $attributes = (object)array_combine($_data['attribute'], $_data['value']);
  3397. $mailboxes = is_array($_data['mailboxes']) ? $_data['mailboxes'] : array($_data['mailboxes']);
  3398. foreach ($mailboxes as $mailbox) {
  3399. if (!filter_var($mailbox, FILTER_VALIDATE_EMAIL)) {
  3400. $_SESSION['return'][] = array(
  3401. 'type' => 'danger',
  3402. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3403. 'msg' => array('username_invalid', $mailbox)
  3404. );
  3405. continue;
  3406. }
  3407. $is_now = mailbox('get', 'mailbox_details', $mailbox);
  3408. if(!empty($is_now)){
  3409. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $is_now['domain'])) {
  3410. $_SESSION['return'][] = array(
  3411. 'type' => 'danger',
  3412. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3413. 'msg' => 'access_denied'
  3414. );
  3415. continue;
  3416. }
  3417. }
  3418. else {
  3419. $_SESSION['return'][] = array(
  3420. 'type' => 'danger',
  3421. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3422. 'msg' => 'access_denied'
  3423. );
  3424. continue;
  3425. }
  3426. $stmt = $pdo->prepare("UPDATE `mailbox`
  3427. SET `custom_attributes` = :custom_attributes
  3428. WHERE username = :username");
  3429. $stmt->execute(array(
  3430. ":username" => $mailbox,
  3431. ":custom_attributes" => json_encode($attributes)
  3432. ));
  3433. $_SESSION['return'][] = array(
  3434. 'type' => 'success',
  3435. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3436. 'msg' => array('mailbox_modified', $mailbox)
  3437. );
  3438. }
  3439. return true;
  3440. break;
  3441. case 'resource':
  3442. if (!is_array($_data['name'])) {
  3443. $names = array();
  3444. $names[] = $_data['name'];
  3445. }
  3446. else {
  3447. $names = $_data['name'];
  3448. }
  3449. foreach ($names as $name) {
  3450. $is_now = mailbox('get', 'resource_details', $name);
  3451. if (!empty($is_now)) {
  3452. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  3453. $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
  3454. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  3455. $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
  3456. }
  3457. else {
  3458. $_SESSION['return'][] = array(
  3459. 'type' => 'danger',
  3460. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3461. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3462. );
  3463. continue;
  3464. }
  3465. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  3466. $_SESSION['return'][] = array(
  3467. 'type' => 'danger',
  3468. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3469. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3470. );
  3471. continue;
  3472. }
  3473. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  3474. $multiple_bookings = -1;
  3475. }
  3476. if (empty($description)) {
  3477. $_SESSION['return'][] = array(
  3478. 'type' => 'danger',
  3479. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3480. 'msg' => array('description_invalid', htmlspecialchars($name))
  3481. );
  3482. continue;
  3483. }
  3484. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  3485. $_SESSION['return'][] = array(
  3486. 'type' => 'danger',
  3487. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3488. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3489. );
  3490. continue;
  3491. }
  3492. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  3493. $_SESSION['return'][] = array(
  3494. 'type' => 'danger',
  3495. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3496. 'msg' => 'access_denied'
  3497. );
  3498. continue;
  3499. }
  3500. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  3501. `active` = :active,
  3502. `name`= :description,
  3503. `kind`= :kind,
  3504. `multiple_bookings`= :multiple_bookings
  3505. WHERE `username` = :name");
  3506. $stmt->execute(array(
  3507. ':active' => $active,
  3508. ':description' => $description,
  3509. ':multiple_bookings' => $multiple_bookings,
  3510. ':kind' => $kind,
  3511. ':name' => $name
  3512. ));
  3513. $_SESSION['return'][] = array(
  3514. 'type' => 'success',
  3515. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3516. 'msg' => array('resource_modified', htmlspecialchars($name))
  3517. );
  3518. }
  3519. break;
  3520. case 'domain_wide_footer':
  3521. if (!is_array($_data['domains'])) {
  3522. $domains = array();
  3523. $domains[] = $_data['domains'];
  3524. }
  3525. else {
  3526. $domains = $_data['domains'];
  3527. }
  3528. $footers = array();
  3529. $footers['html'] = isset($_data['html']) ? $_data['html'] : '';
  3530. $footers['plain'] = isset($_data['plain']) ? $_data['plain'] : '';
  3531. $footers['skip_replies'] = isset($_data['skip_replies']) ? (int)$_data['skip_replies'] : 0;
  3532. $footers['mbox_exclude'] = array();
  3533. $footers['alias_domain_exclude'] = array();
  3534. if (isset($_data["exclude"])){
  3535. if (!is_array($_data["exclude"])) {
  3536. $_data["exclude"] = array($_data["exclude"]);
  3537. }
  3538. foreach ($_data["exclude"] as $exclude) {
  3539. if (filter_var($exclude, FILTER_VALIDATE_EMAIL)) {
  3540. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `address` = :address
  3541. UNION
  3542. SELECT `username` FROM `mailbox` WHERE `username` = :username");
  3543. $stmt->execute(array(
  3544. ':address' => $exclude,
  3545. ':username' => $exclude,
  3546. ));
  3547. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3548. if(!$row){
  3549. $_SESSION['return'][] = array(
  3550. 'type' => 'danger',
  3551. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3552. 'msg' => array('username_invalid', $exclude)
  3553. );
  3554. continue;
  3555. }
  3556. array_push($footers['mbox_exclude'], $exclude);
  3557. }
  3558. elseif (is_valid_domain_name($exclude)) {
  3559. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  3560. $stmt->execute(array(
  3561. ':alias_domain' => $exclude,
  3562. ));
  3563. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  3564. if(!$row){
  3565. $_SESSION['return'][] = array(
  3566. 'type' => 'danger',
  3567. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3568. 'msg' => array('username_invalid', $exclude)
  3569. );
  3570. continue;
  3571. }
  3572. array_push($footers['alias_domain_exclude'], $exclude);
  3573. }
  3574. else {
  3575. $_SESSION['return'][] = array(
  3576. 'type' => 'danger',
  3577. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3578. 'msg' => array('username_invalid', $exclude)
  3579. );
  3580. }
  3581. }
  3582. }
  3583. foreach ($domains as $domain) {
  3584. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  3585. if (!is_valid_domain_name($domain)) {
  3586. $_SESSION['return'][] = array(
  3587. 'type' => 'danger',
  3588. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3589. 'msg' => 'domain_invalid'
  3590. );
  3591. return false;
  3592. }
  3593. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  3594. $_SESSION['return'][] = array(
  3595. 'type' => 'danger',
  3596. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3597. 'msg' => 'access_denied'
  3598. );
  3599. return false;
  3600. }
  3601. try {
  3602. $stmt = $pdo->prepare("DELETE FROM `domain_wide_footer` WHERE `domain`= :domain");
  3603. $stmt->execute(array(':domain' => $domain));
  3604. $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)");
  3605. $stmt->execute(array(
  3606. ':domain' => $domain,
  3607. ':html' => $footers['html'],
  3608. ':plain' => $footers['plain'],
  3609. ':mbox_exclude' => json_encode($footers['mbox_exclude']),
  3610. ':alias_domain_exclude' => json_encode($footers['alias_domain_exclude']),
  3611. ':skip_replies' => $footers['skip_replies'],
  3612. ));
  3613. }
  3614. catch (PDOException $e) {
  3615. $_SESSION['return'][] = array(
  3616. 'type' => 'danger',
  3617. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3618. 'msg' => $e->getMessage()
  3619. );
  3620. return false;
  3621. }
  3622. $_SESSION['return'][] = array(
  3623. 'type' => 'success',
  3624. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3625. 'msg' => array('domain_footer_modified', htmlspecialchars($domain))
  3626. );
  3627. }
  3628. break;
  3629. }
  3630. break;
  3631. case 'get':
  3632. switch ($_type) {
  3633. case 'sender_acl_handles':
  3634. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  3635. return false;
  3636. }
  3637. $data['sender_acl_domains']['ro'] = array();
  3638. $data['sender_acl_domains']['rw'] = array();
  3639. $data['sender_acl_domains']['selectable'] = array();
  3640. $data['sender_acl_addresses']['ro'] = array();
  3641. $data['sender_acl_addresses']['rw'] = array();
  3642. $data['sender_acl_addresses']['selectable'] = array();
  3643. $data['fixed_sender_aliases'] = array();
  3644. $data['external_sender_aliases'] = array();
  3645. // Fixed addresses
  3646. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
  3647. $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
  3648. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3649. while ($row = array_shift($rows)) {
  3650. $data['fixed_sender_aliases'][] = $row['address'];
  3651. }
  3652. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
  3653. WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
  3654. AND `mailbox`.`username` = :username");
  3655. $stmt->execute(array(':username' => $_data));
  3656. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3657. while ($row = array_shift($rows)) {
  3658. if (!empty($row['alias_domain_alias'])) {
  3659. $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
  3660. }
  3661. }
  3662. // External addresses
  3663. $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
  3664. $stmt->execute(array(':logged_in_as' => $_data));
  3665. $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3666. while ($row = array_shift($exernal_rows)) {
  3667. if (!empty($row['send_as_external'])) {
  3668. $data['external_sender_aliases'][] = $row['send_as_external'];
  3669. }
  3670. }
  3671. // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
  3672. // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
  3673. $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` = '*')");
  3674. $stmt->execute(array(':logged_in_as' => $_data));
  3675. $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3676. while ($domain_row = array_shift($domain_rows)) {
  3677. if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3678. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3679. continue;
  3680. }
  3681. if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3682. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3683. continue;
  3684. }
  3685. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  3686. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3687. }
  3688. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3689. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3690. }
  3691. }
  3692. $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` != '*')");
  3693. $stmt->execute(array(':logged_in_as' => $_data));
  3694. $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3695. while ($address_row = array_shift($address_rows)) {
  3696. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3697. $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
  3698. continue;
  3699. }
  3700. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3701. $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
  3702. continue;
  3703. }
  3704. }
  3705. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  3706. WHERE `domain` NOT IN (
  3707. SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
  3708. WHERE `logged_in_as` = :logged_in_as1
  3709. AND `external` = '0'
  3710. AND `send_as` LIKE '@%')
  3711. UNION
  3712. SELECT '*' FROM `domain`
  3713. WHERE '*' NOT IN (
  3714. SELECT `send_as` FROM `sender_acl`
  3715. WHERE `logged_in_as` = :logged_in_as2
  3716. AND `external` = '0'
  3717. )");
  3718. $stmt->execute(array(
  3719. ':logged_in_as1' => $_data,
  3720. ':logged_in_as2' => $_data
  3721. ));
  3722. $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3723. while ($row_domain = array_shift($rows_domain)) {
  3724. if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
  3725. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3726. continue;
  3727. }
  3728. if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3729. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3730. continue;
  3731. }
  3732. }
  3733. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  3734. WHERE `goto` != :goto
  3735. AND `address` NOT IN (
  3736. SELECT `send_as` FROM `sender_acl`
  3737. WHERE `logged_in_as` = :logged_in_as
  3738. AND `external` = '0'
  3739. AND `send_as` NOT LIKE '@%')");
  3740. $stmt->execute(array(
  3741. ':logged_in_as' => $_data,
  3742. ':goto' => $_data
  3743. ));
  3744. $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3745. while ($row = array_shift($rows_mbox)) {
  3746. // Aliases are not selectable
  3747. if (in_array($row['address'], $data['fixed_sender_aliases'])) {
  3748. continue;
  3749. }
  3750. if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
  3751. $data['sender_acl_addresses']['selectable'][] = $row['address'];
  3752. }
  3753. }
  3754. return $data;
  3755. break;
  3756. case 'mailboxes':
  3757. $mailboxes = array();
  3758. if (isset($_extra) && is_array($_extra) && isset($_data)) {
  3759. // get by domain and tags
  3760. $tags = is_array($_extra) ? $_extra : array();
  3761. $sql = "";
  3762. foreach ($tags as $key => $tag) {
  3763. $sql = $sql."SELECT DISTINCT `username` FROM `tags_mailbox` WHERE `username` LIKE ? AND `tag_name` LIKE ?"; // distinct, avoid duplicates
  3764. if ($key === array_key_last($tags)) break;
  3765. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  3766. }
  3767. // prepend domain to array
  3768. $params = array();
  3769. foreach ($tags as $key => $val){
  3770. array_push($params, '%'.$_data.'%');
  3771. array_push($params, '%'.$val.'%');
  3772. }
  3773. $stmt = $pdo->prepare($sql);
  3774. $stmt->execute($params);
  3775. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3776. while($row = array_shift($rows)) {
  3777. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], explode('@', $row['username'])[1]))
  3778. $mailboxes[] = $row['username'];
  3779. }
  3780. }
  3781. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3782. // get by domain
  3783. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
  3784. $stmt->execute(array(
  3785. ':domain' => $_data,
  3786. ));
  3787. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3788. while($row = array_shift($rows)) {
  3789. $mailboxes[] = $row['username'];
  3790. }
  3791. }
  3792. else {
  3793. $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)");
  3794. $stmt->execute(array(
  3795. ':username' => $_SESSION['mailcow_cc_username'],
  3796. ':role' => $_SESSION['mailcow_cc_role'],
  3797. ));
  3798. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3799. while($row = array_shift($rows)) {
  3800. $mailboxes[] = $row['username'];
  3801. }
  3802. }
  3803. return $mailboxes;
  3804. break;
  3805. case 'tls_policy':
  3806. $attrs = array();
  3807. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3808. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3809. return false;
  3810. }
  3811. }
  3812. else {
  3813. $_data = $_SESSION['mailcow_cc_username'];
  3814. }
  3815. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3816. $stmt->execute(array(':username' => $_data));
  3817. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3818. $attrs = json_decode($attrs['attributes'], true);
  3819. return array(
  3820. 'tls_enforce_in' => $attrs['tls_enforce_in'],
  3821. 'tls_enforce_out' => $attrs['tls_enforce_out']
  3822. );
  3823. break;
  3824. case 'quarantine_notification':
  3825. $attrs = array();
  3826. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3827. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3828. return false;
  3829. }
  3830. }
  3831. else {
  3832. $_data = $_SESSION['mailcow_cc_username'];
  3833. }
  3834. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3835. $stmt->execute(array(':username' => $_data));
  3836. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3837. $attrs = json_decode($attrs['attributes'], true);
  3838. return $attrs['quarantine_notification'];
  3839. break;
  3840. case 'quarantine_category':
  3841. $attrs = array();
  3842. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3843. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3844. return false;
  3845. }
  3846. }
  3847. else {
  3848. $_data = $_SESSION['mailcow_cc_username'];
  3849. }
  3850. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3851. $stmt->execute(array(':username' => $_data));
  3852. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3853. $attrs = json_decode($attrs['attributes'], true);
  3854. return $attrs['quarantine_category'];
  3855. break;
  3856. case 'filters':
  3857. $filters = array();
  3858. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3859. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3860. return false;
  3861. }
  3862. }
  3863. else {
  3864. $_data = $_SESSION['mailcow_cc_username'];
  3865. }
  3866. $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
  3867. $stmt->execute(array(':username' => $_data));
  3868. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3869. while($row = array_shift($rows)) {
  3870. $filters[] = $row['id'];
  3871. }
  3872. return $filters;
  3873. break;
  3874. case 'global_filter_details':
  3875. $global_filters = array();
  3876. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3877. return false;
  3878. }
  3879. $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
  3880. $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
  3881. return $global_filters;
  3882. break;
  3883. case 'filter_details':
  3884. $filter_details = array();
  3885. if (!is_numeric($_data)) {
  3886. return false;
  3887. }
  3888. $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
  3889. id,
  3890. username,
  3891. filter_type,
  3892. script_data,
  3893. script_desc
  3894. FROM `sieve_filters`
  3895. WHERE `id` = :id");
  3896. $stmt->execute(array(':id' => $_data));
  3897. $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
  3898. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
  3899. return false;
  3900. }
  3901. return $filter_details;
  3902. break;
  3903. case 'active_user_sieve':
  3904. $filter_details = array();
  3905. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3906. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3907. return false;
  3908. }
  3909. }
  3910. else {
  3911. $_data = $_SESSION['mailcow_cc_username'];
  3912. }
  3913. $exec_fields = array(
  3914. 'cmd' => 'sieve',
  3915. 'task' => 'list',
  3916. 'username' => $_data
  3917. );
  3918. $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3919. $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
  3920. foreach ($filters as $filter) {
  3921. if (preg_match('/.+ ACTIVE/i', $filter)) {
  3922. $exec_fields = array(
  3923. 'cmd' => 'sieve',
  3924. 'task' => 'print',
  3925. 'script_name' => substr($filter, 0, -7),
  3926. 'username' => $_data
  3927. );
  3928. $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3929. // Remove first line
  3930. return preg_replace('/^.+\n/', '', $script);
  3931. }
  3932. }
  3933. return false;
  3934. break;
  3935. case 'syncjob_details':
  3936. $syncjobdetails = array();
  3937. if (!is_numeric($_data)) {
  3938. return false;
  3939. }
  3940. if (isset($_extra) && in_array('no_log', $_extra)) {
  3941. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
  3942. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3943. while($field = array_shift($fields)) {
  3944. $shown_fields[] = $field['Field'];
  3945. }
  3946. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3947. `active`
  3948. FROM `imapsync` WHERE id = :id");
  3949. }
  3950. elseif (isset($_extra) && in_array('with_password', $_extra)) {
  3951. $stmt = $pdo->prepare("SELECT *,
  3952. `active`
  3953. FROM `imapsync` WHERE id = :id");
  3954. }
  3955. else {
  3956. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
  3957. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3958. while($field = array_shift($fields)) {
  3959. $shown_fields[] = $field['Field'];
  3960. }
  3961. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3962. `active`
  3963. FROM `imapsync` WHERE id = :id");
  3964. }
  3965. $stmt->execute(array(':id' => $_data));
  3966. $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
  3967. if (!empty($syncjobdetails['returned_text'])) {
  3968. $syncjobdetails['log'] = $syncjobdetails['returned_text'];
  3969. }
  3970. else {
  3971. $syncjobdetails['log'] = '';
  3972. }
  3973. unset($syncjobdetails['returned_text']);
  3974. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
  3975. return false;
  3976. }
  3977. return $syncjobdetails;
  3978. break;
  3979. case 'syncjobs':
  3980. $syncjobdata = array();
  3981. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3982. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3983. return false;
  3984. }
  3985. }
  3986. else {
  3987. $_data = $_SESSION['mailcow_cc_username'];
  3988. }
  3989. $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
  3990. $stmt->execute(array(':username' => $_data));
  3991. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3992. while($row = array_shift($rows)) {
  3993. $syncjobdata[] = $row['id'];
  3994. }
  3995. return $syncjobdata;
  3996. break;
  3997. case 'spam_score':
  3998. $curl = curl_init();
  3999. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  4000. curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
  4001. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  4002. $default_actions = curl_exec($curl);
  4003. if (!curl_errno($curl)) {
  4004. $data_array = json_decode($default_actions, true);
  4005. curl_close($curl);
  4006. foreach ($data_array as $data) {
  4007. if ($data['action'] == 'reject') {
  4008. $reject = $data['value'];
  4009. continue;
  4010. }
  4011. elseif ($data['action'] == 'add header') {
  4012. $add_header = $data['value'];
  4013. continue;
  4014. }
  4015. }
  4016. if (empty($add_header) || empty($reject)) {
  4017. // Assume default, set warning
  4018. $default = "5, 15";
  4019. $_SESSION['return'][] = array(
  4020. 'type' => 'warning',
  4021. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4022. 'msg' => 'Could not determine servers default spam score, assuming default'
  4023. );
  4024. }
  4025. else {
  4026. $default = $add_header . ', ' . $reject;
  4027. }
  4028. }
  4029. else {
  4030. // Assume default, set warning
  4031. $default = "5, 15";
  4032. $_SESSION['return'][] = array(
  4033. 'type' => 'warning',
  4034. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4035. 'msg' => 'Could not determine servers default spam score, assuming default'
  4036. );
  4037. }
  4038. curl_close($curl);
  4039. $policydata = array();
  4040. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4041. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4042. return false;
  4043. }
  4044. }
  4045. else {
  4046. $_data = $_SESSION['mailcow_cc_username'];
  4047. }
  4048. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
  4049. (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  4050. $stmt->execute(array(':username' => $_data));
  4051. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  4052. if (empty($num_results)) {
  4053. return $default;
  4054. }
  4055. else {
  4056. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
  4057. $stmt->execute(array(':username' => $_data));
  4058. $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4059. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
  4060. $stmt->execute(array(':username' => $_data));
  4061. $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4062. return $lowspamlevel['value'].', '.$highspamlevel['value'];
  4063. }
  4064. break;
  4065. case 'time_limited_aliases':
  4066. $tladata = array();
  4067. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4068. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4069. return false;
  4070. }
  4071. }
  4072. else {
  4073. $_data = $_SESSION['mailcow_cc_username'];
  4074. }
  4075. $stmt = $pdo->prepare("SELECT `address`,
  4076. `goto`,
  4077. `validity`,
  4078. `created`,
  4079. `modified`
  4080. FROM `spamalias`
  4081. WHERE `goto` = :username
  4082. AND `validity` >= :unixnow");
  4083. $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
  4084. $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4085. return $tladata;
  4086. break;
  4087. case 'delimiter_action':
  4088. $policydata = array();
  4089. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4090. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4091. return false;
  4092. }
  4093. }
  4094. else {
  4095. $_data = $_SESSION['mailcow_cc_username'];
  4096. }
  4097. try {
  4098. if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
  4099. return "subject";
  4100. }
  4101. elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
  4102. return "subfolder";
  4103. }
  4104. else {
  4105. return "none";
  4106. }
  4107. }
  4108. catch (RedisException $e) {
  4109. $_SESSION['return'][] = array(
  4110. 'type' => 'danger',
  4111. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4112. 'msg' => array('redis_error', $e)
  4113. );
  4114. return false;
  4115. }
  4116. break;
  4117. case 'resources':
  4118. $resources = array();
  4119. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4120. return false;
  4121. }
  4122. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4123. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
  4124. $stmt->execute(array(
  4125. ':domain' => $_data,
  4126. ));
  4127. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4128. while($row = array_shift($rows)) {
  4129. $resources[] = $row['username'];
  4130. }
  4131. }
  4132. else {
  4133. $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");
  4134. $stmt->execute(array(
  4135. ':username' => $_SESSION['mailcow_cc_username'],
  4136. ':role' => $_SESSION['mailcow_cc_role'],
  4137. ));
  4138. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4139. while($row = array_shift($rows)) {
  4140. $resources[] = $row['username'];
  4141. }
  4142. }
  4143. return $resources;
  4144. break;
  4145. case 'alias_domains':
  4146. $aliasdomains = array();
  4147. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4148. return false;
  4149. }
  4150. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4151. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
  4152. $stmt->execute(array(
  4153. ':domain' => $_data,
  4154. ));
  4155. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4156. while($row = array_shift($rows)) {
  4157. $aliasdomains[] = $row['alias_domain'];
  4158. }
  4159. }
  4160. else {
  4161. $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");
  4162. $stmt->execute(array(
  4163. ':username' => $_SESSION['mailcow_cc_username'],
  4164. ':role' => $_SESSION['mailcow_cc_role'],
  4165. ));
  4166. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4167. while($row = array_shift($rows)) {
  4168. $aliasdomains[] = $row['alias_domain'];
  4169. }
  4170. }
  4171. return $aliasdomains;
  4172. break;
  4173. case 'aliases':
  4174. $aliases = array();
  4175. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4176. return false;
  4177. }
  4178. $stmt = $pdo->prepare("SELECT `id`, `address` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
  4179. $stmt->execute(array(
  4180. ':domain' => $_data,
  4181. ));
  4182. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4183. while($row = array_shift($rows)) {
  4184. if ($_extra == "address"){
  4185. $aliases[] = $row['address'];
  4186. } else {
  4187. $aliases[] = $row['id'];
  4188. }
  4189. }
  4190. return $aliases;
  4191. break;
  4192. case 'alias_details':
  4193. $aliasdata = array();
  4194. $stmt = $pdo->prepare("SELECT
  4195. `id`,
  4196. `domain`,
  4197. `goto`,
  4198. `address`,
  4199. `public_comment`,
  4200. `private_comment`,
  4201. `active`,
  4202. `sogo_visible`,
  4203. `created`,
  4204. `modified`
  4205. FROM `alias`
  4206. WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
  4207. $stmt->execute(array(
  4208. ':id' => $_data,
  4209. ':address' => $_data,
  4210. ));
  4211. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4212. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4213. $stmt->execute(array(
  4214. ':domain' => $row['domain'],
  4215. ));
  4216. $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
  4217. if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
  4218. $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
  4219. }
  4220. else {
  4221. $aliasdata['in_primary_domain'] = "";
  4222. }
  4223. $aliasdata['id'] = $row['id'];
  4224. $aliasdata['domain'] = $row['domain'];
  4225. $aliasdata['public_comment'] = $row['public_comment'];
  4226. $aliasdata['private_comment'] = $row['private_comment'];
  4227. $aliasdata['domain'] = $row['domain'];
  4228. $aliasdata['goto'] = $row['goto'];
  4229. $aliasdata['address'] = $row['address'];
  4230. (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
  4231. $aliasdata['active'] = $row['active'];
  4232. $aliasdata['active_int'] = $row['active'];
  4233. $aliasdata['sogo_visible'] = $row['sogo_visible'];
  4234. $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
  4235. $aliasdata['created'] = $row['created'];
  4236. $aliasdata['modified'] = $row['modified'];
  4237. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
  4238. return false;
  4239. }
  4240. return $aliasdata;
  4241. break;
  4242. case 'alias_domain_details':
  4243. $aliasdomaindata = array();
  4244. $rl = ratelimit('get', 'domain', $_data);
  4245. $stmt = $pdo->prepare("SELECT
  4246. `alias_domain`,
  4247. `target_domain`,
  4248. `active`,
  4249. `created`,
  4250. `modified`
  4251. FROM `alias_domain`
  4252. WHERE `alias_domain` = :aliasdomain");
  4253. $stmt->execute(array(
  4254. ':aliasdomain' => $_data,
  4255. ));
  4256. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4257. $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
  4258. $stmt->execute(array(
  4259. ':target_domain' => $row['target_domain']
  4260. ));
  4261. $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
  4262. $aliasdomaindata['alias_domain'] = $row['alias_domain'];
  4263. $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
  4264. $aliasdomaindata['target_domain'] = $row['target_domain'];
  4265. $aliasdomaindata['active'] = $row['active'];
  4266. $aliasdomaindata['active_int'] = $row['active'];
  4267. $aliasdomaindata['rl'] = $rl;
  4268. $aliasdomaindata['created'] = $row['created'];
  4269. $aliasdomaindata['modified'] = $row['modified'];
  4270. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
  4271. return false;
  4272. }
  4273. return $aliasdomaindata;
  4274. break;
  4275. case 'shared_aliases':
  4276. $shared_aliases = array();
  4277. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4278. WHERE `goto` REGEXP ','
  4279. AND `address` NOT LIKE '@%'
  4280. AND `goto` != `address`");
  4281. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4282. while($row = array_shift($rows)) {
  4283. $domain = explode("@", $row['address'])[1];
  4284. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4285. $shared_aliases[] = $row['address'];
  4286. }
  4287. }
  4288. return $shared_aliases;
  4289. break;
  4290. case 'direct_aliases':
  4291. $direct_aliases = array();
  4292. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4293. WHERE `goto` NOT LIKE '%,%'
  4294. AND `address` NOT LIKE '@%'
  4295. AND `goto` != `address`");
  4296. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4297. while($row = array_shift($rows)) {
  4298. $domain = explode("@", $row['address'])[1];
  4299. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4300. $direct_aliases[] = $row['address'];
  4301. }
  4302. }
  4303. return $direct_aliases;
  4304. break;
  4305. case 'domains':
  4306. $domains = array();
  4307. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4308. return false;
  4309. }
  4310. if (isset($_extra) && is_array($_extra)){
  4311. // get by tags
  4312. $tags = is_array($_extra) ? $_extra : array();
  4313. // add % as prefix and suffix to every element for relative searching
  4314. $tags = array_map(function($x){ return '%'.$x.'%'; }, $tags);
  4315. $sql = "";
  4316. foreach ($tags as $key => $tag) {
  4317. $sql = $sql."SELECT DISTINCT `domain` FROM `tags_domain` WHERE `tag_name` LIKE ?"; // distinct, avoid duplicates
  4318. if ($key === array_key_last($tags)) break;
  4319. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  4320. }
  4321. $stmt = $pdo->prepare($sql);
  4322. $stmt->execute($tags);
  4323. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4324. while($row = array_shift($rows)) {
  4325. if ($_SESSION['mailcow_cc_role'] == "admin")
  4326. $domains[] = $row['domain'];
  4327. elseif (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['domain']))
  4328. $domains[] = $row['domain'];
  4329. }
  4330. } else {
  4331. // get all
  4332. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  4333. WHERE (`domain` IN (
  4334. SELECT `domain` from `domain_admins`
  4335. WHERE (`active`='1' AND `username` = :username))
  4336. )
  4337. OR 'admin'= :role");
  4338. $stmt->execute(array(
  4339. ':username' => $_SESSION['mailcow_cc_username'],
  4340. ':role' => $_SESSION['mailcow_cc_role'],
  4341. ));
  4342. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4343. while($row = array_shift($rows)) {
  4344. $domains[] = $row['domain'];
  4345. }
  4346. }
  4347. return $domains;
  4348. break;
  4349. case 'domain_details':
  4350. $domaindata = array();
  4351. $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4352. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4353. return false;
  4354. }
  4355. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4356. $stmt->execute(array(
  4357. ':domain' => $_data
  4358. ));
  4359. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4360. if (!empty($row)) {
  4361. $_data = $row['target_domain'];
  4362. }
  4363. $stmt = $pdo->prepare("SELECT
  4364. `domain`,
  4365. `description`,
  4366. `aliases`,
  4367. `mailboxes`,
  4368. `defquota`,
  4369. `maxquota`,
  4370. `created`,
  4371. `modified`,
  4372. `quota`,
  4373. `relayhost`,
  4374. `relay_all_recipients`,
  4375. `relay_unknown_only`,
  4376. `backupmx`,
  4377. `gal`,
  4378. `active`
  4379. FROM `domain` WHERE `domain`= :domain");
  4380. $stmt->execute(array(
  4381. ':domain' => $_data
  4382. ));
  4383. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4384. if (empty($row)) {
  4385. return false;
  4386. }
  4387. $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
  4388. COALESCE(SUM(`quota`), 0) AS `in_use`
  4389. FROM `mailbox`
  4390. WHERE (`kind` = '' OR `kind` = NULL)
  4391. AND `domain` = :domain");
  4392. $stmt->execute(array(':domain' => $row['domain']));
  4393. $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4394. $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
  4395. WHERE `username` IN (
  4396. SELECT `username` FROM `mailbox`
  4397. WHERE `domain` = :domain
  4398. );");
  4399. $stmt->execute(array(':domain' => $row['domain']));
  4400. $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
  4401. $rl = ratelimit('get', 'domain', $_data);
  4402. $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
  4403. if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
  4404. $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
  4405. }
  4406. $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
  4407. if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
  4408. $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
  4409. }
  4410. $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
  4411. if (!empty($SumQuotaInUse['bytes_total'])) {
  4412. $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
  4413. }
  4414. else {
  4415. $domaindata['bytes_total'] = 0;
  4416. }
  4417. if (!empty($SumQuotaInUse['msgs_total'])) {
  4418. $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
  4419. }
  4420. else {
  4421. $domaindata['msgs_total'] = 0;
  4422. }
  4423. $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
  4424. $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
  4425. $domaindata['domain_name'] = $row['domain'];
  4426. $domaindata['domain_h_name'] = idn_to_utf8($row['domain']);
  4427. $domaindata['description'] = $row['description'];
  4428. $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
  4429. $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
  4430. $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
  4431. $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
  4432. $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
  4433. $domaindata['relayhost'] = $row['relayhost'];
  4434. $domaindata['backupmx'] = $row['backupmx'];
  4435. $domaindata['backupmx_int'] = $row['backupmx'];
  4436. $domaindata['gal'] = $row['gal'];
  4437. $domaindata['gal_int'] = $row['gal'];
  4438. $domaindata['rl'] = $rl;
  4439. $domaindata['active'] = $row['active'];
  4440. $domaindata['active_int'] = $row['active'];
  4441. $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
  4442. $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
  4443. $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
  4444. $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
  4445. $domaindata['created'] = $row['created'];
  4446. $domaindata['modified'] = $row['modified'];
  4447. $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
  4448. WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
  4449. AND `address` NOT IN (
  4450. SELECT `username` FROM `mailbox`
  4451. )");
  4452. $stmt->execute(array(
  4453. ':domain' => $_data,
  4454. ':domain2' => $_data
  4455. ));
  4456. $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4457. (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
  4458. $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
  4459. if ($_SESSION['mailcow_cc_role'] == "admin")
  4460. {
  4461. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
  4462. $stmt->execute(array(
  4463. ':domain' => $_data
  4464. ));
  4465. $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
  4466. (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
  4467. }
  4468. $stmt = $pdo->prepare("SELECT `tag_name`
  4469. FROM `tags_domain` WHERE `domain`= :domain");
  4470. $stmt->execute(array(
  4471. ':domain' => $_data
  4472. ));
  4473. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4474. while ($tag = array_shift($tags)) {
  4475. $domaindata['tags'][] = $tag['tag_name'];
  4476. }
  4477. return $domaindata;
  4478. break;
  4479. case 'domain_templates':
  4480. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4481. return false;
  4482. }
  4483. $_data = (isset($_data)) ? intval($_data) : null;
  4484. if (isset($_data)){
  4485. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4486. WHERE `id` = :id AND type = :type");
  4487. $stmt->execute(array(
  4488. ":id" => $_data,
  4489. ":type" => "domain"
  4490. ));
  4491. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4492. if (empty($row)){
  4493. return false;
  4494. }
  4495. $row["attributes"] = json_decode($row["attributes"], true);
  4496. return $row;
  4497. }
  4498. else {
  4499. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'domain'");
  4500. $stmt->execute();
  4501. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4502. if (empty($rows)){
  4503. return false;
  4504. }
  4505. foreach($rows as $key => $row){
  4506. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4507. }
  4508. return $rows;
  4509. }
  4510. break;
  4511. case 'mailbox_details':
  4512. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4513. return false;
  4514. }
  4515. $mailboxdata = array();
  4516. if (preg_match('/y|yes/i', getenv('MASTER'))) {
  4517. $stmt = $pdo->prepare("SELECT
  4518. `domain`.`backupmx`,
  4519. `mailbox`.`username`,
  4520. `mailbox`.`name`,
  4521. `mailbox`.`active`,
  4522. `mailbox`.`domain`,
  4523. `mailbox`.`local_part`,
  4524. `mailbox`.`quota`,
  4525. `mailbox`.`created`,
  4526. `mailbox`.`modified`,
  4527. `quota2`.`bytes`,
  4528. `attributes`,
  4529. `custom_attributes`,
  4530. `quota2`.`messages`
  4531. FROM `mailbox`, `quota2`, `domain`
  4532. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4533. AND `mailbox`.`username` = `quota2`.`username`
  4534. AND `domain`.`domain` = `mailbox`.`domain`
  4535. AND `mailbox`.`username` = :mailbox");
  4536. }
  4537. else {
  4538. $stmt = $pdo->prepare("SELECT
  4539. `domain`.`backupmx`,
  4540. `mailbox`.`username`,
  4541. `mailbox`.`name`,
  4542. `mailbox`.`active`,
  4543. `mailbox`.`domain`,
  4544. `mailbox`.`local_part`,
  4545. `mailbox`.`quota`,
  4546. `mailbox`.`created`,
  4547. `mailbox`.`modified`,
  4548. `quota2replica`.`bytes`,
  4549. `attributes`,
  4550. `custom_attributes`,
  4551. `quota2replica`.`messages`
  4552. FROM `mailbox`, `quota2replica`, `domain`
  4553. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4554. AND `mailbox`.`username` = `quota2replica`.`username`
  4555. AND `domain`.`domain` = `mailbox`.`domain`
  4556. AND `mailbox`.`username` = :mailbox");
  4557. }
  4558. $stmt->execute(array(
  4559. ':mailbox' => $_data,
  4560. ));
  4561. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4562. $mailboxdata['username'] = $row['username'];
  4563. $mailboxdata['active'] = $row['active'];
  4564. $mailboxdata['active_int'] = $row['active'];
  4565. $mailboxdata['domain'] = $row['domain'];
  4566. $mailboxdata['name'] = $row['name'];
  4567. $mailboxdata['local_part'] = $row['local_part'];
  4568. $mailboxdata['quota'] = $row['quota'];
  4569. $mailboxdata['messages'] = $row['messages'];
  4570. $mailboxdata['attributes'] = json_decode($row['attributes'], true);
  4571. $mailboxdata['custom_attributes'] = json_decode($row['custom_attributes'], true);
  4572. $mailboxdata['quota_used'] = intval($row['bytes']);
  4573. $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
  4574. $mailboxdata['created'] = $row['created'];
  4575. $mailboxdata['modified'] = $row['modified'];
  4576. if ($mailboxdata['percent_in_use'] === '- ') {
  4577. $mailboxdata['percent_class'] = "info";
  4578. }
  4579. elseif ($mailboxdata['percent_in_use'] >= 90) {
  4580. $mailboxdata['percent_class'] = "danger";
  4581. }
  4582. elseif ($mailboxdata['percent_in_use'] >= 75) {
  4583. $mailboxdata['percent_class'] = "warning";
  4584. }
  4585. else {
  4586. $mailboxdata['percent_class'] = "success";
  4587. }
  4588. // Determine last logins
  4589. $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
  4590. WHERE `username` = :mailbox
  4591. GROUP BY `service` DESC");
  4592. $stmt->execute(array(':mailbox' => $_data));
  4593. $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4594. foreach ($SaslLogsData as $SaslLogs) {
  4595. if ($SaslLogs['service'] == 'imap') {
  4596. $last_imap_login = strtotime($SaslLogs['datetime']);
  4597. }
  4598. else if ($SaslLogs['service'] == 'smtp') {
  4599. $last_smtp_login = strtotime($SaslLogs['datetime']);
  4600. }
  4601. else if ($SaslLogs['service'] == 'pop3') {
  4602. $last_pop3_login = strtotime($SaslLogs['datetime']);
  4603. }
  4604. }
  4605. if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4606. $last_imap_login = 0;
  4607. }
  4608. if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4609. $last_smtp_login = 0;
  4610. }
  4611. if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4612. $last_pop3_login = 0;
  4613. }
  4614. $mailboxdata['last_imap_login'] = $last_imap_login;
  4615. $mailboxdata['last_smtp_login'] = $last_smtp_login;
  4616. $mailboxdata['last_pop3_login'] = $last_pop3_login;
  4617. if (!isset($_extra) || $_extra != 'reduced') {
  4618. $rl = ratelimit('get', 'mailbox', $_data);
  4619. $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
  4620. $stmt->execute(array(':domain' => $row['domain']));
  4621. $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
  4622. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
  4623. $stmt->execute(array(':username' => $_data));
  4624. $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
  4625. $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
  4626. $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
  4627. $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4628. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
  4629. $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
  4630. $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4631. $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
  4632. $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
  4633. $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
  4634. if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
  4635. $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
  4636. }
  4637. if (!empty($rl)) {
  4638. $mailboxdata['rl'] = $rl;
  4639. $mailboxdata['rl_scope'] = 'mailbox';
  4640. }
  4641. else {
  4642. $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
  4643. $mailboxdata['rl_scope'] = 'domain';
  4644. }
  4645. $mailboxdata['is_relayed'] = $row['backupmx'];
  4646. }
  4647. $stmt = $pdo->prepare("SELECT `tag_name`
  4648. FROM `tags_mailbox` WHERE `username`= :username");
  4649. $stmt->execute(array(
  4650. ':username' => $_data
  4651. ));
  4652. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4653. while ($tag = array_shift($tags)) {
  4654. $mailboxdata['tags'][] = $tag['tag_name'];
  4655. }
  4656. return $mailboxdata;
  4657. break;
  4658. case 'mailbox_templates':
  4659. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4660. return false;
  4661. }
  4662. $_data = (isset($_data)) ? intval($_data) : null;
  4663. if (isset($_data)){
  4664. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4665. WHERE `id` = :id AND type = :type");
  4666. $stmt->execute(array(
  4667. ":id" => $_data,
  4668. ":type" => "mailbox"
  4669. ));
  4670. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4671. if (empty($row)){
  4672. return false;
  4673. }
  4674. $row["attributes"] = json_decode($row["attributes"], true);
  4675. return $row;
  4676. }
  4677. else {
  4678. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'mailbox'");
  4679. $stmt->execute();
  4680. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4681. if (empty($rows)){
  4682. return false;
  4683. }
  4684. foreach($rows as $key => $row){
  4685. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4686. }
  4687. return $rows;
  4688. }
  4689. break;
  4690. case 'resource_details':
  4691. $resourcedata = array();
  4692. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4693. return false;
  4694. }
  4695. $stmt = $pdo->prepare("SELECT
  4696. `username`,
  4697. `name`,
  4698. `kind`,
  4699. `multiple_bookings`,
  4700. `local_part`,
  4701. `active`,
  4702. `domain`
  4703. FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
  4704. $stmt->execute(array(
  4705. ':resource' => $_data,
  4706. ));
  4707. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4708. $resourcedata['name'] = $row['username'];
  4709. $resourcedata['kind'] = $row['kind'];
  4710. $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
  4711. $resourcedata['description'] = $row['name'];
  4712. $resourcedata['active'] = $row['active'];
  4713. $resourcedata['active_int'] = $row['active'];
  4714. $resourcedata['domain'] = $row['domain'];
  4715. $resourcedata['local_part'] = $row['local_part'];
  4716. if (!isset($resourcedata['domain']) ||
  4717. (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
  4718. return false;
  4719. }
  4720. return $resourcedata;
  4721. break;
  4722. case 'domain_wide_footer':
  4723. $domain = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4724. if (!is_valid_domain_name($domain)) {
  4725. $_SESSION['return'][] = array(
  4726. 'type' => 'danger',
  4727. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4728. 'msg' => 'domain_invalid'
  4729. );
  4730. return false;
  4731. }
  4732. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4733. $_SESSION['return'][] = array(
  4734. 'type' => 'danger',
  4735. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4736. 'msg' => 'access_denied'
  4737. );
  4738. return false;
  4739. }
  4740. try {
  4741. $stmt = $pdo->prepare("SELECT `html`, `plain`, `mbox_exclude`, `alias_domain_exclude`, `skip_replies` FROM `domain_wide_footer`
  4742. WHERE `domain` = :domain");
  4743. $stmt->execute(array(
  4744. ':domain' => $domain
  4745. ));
  4746. $footer = $stmt->fetch(PDO::FETCH_ASSOC);
  4747. }
  4748. catch (PDOException $e) {
  4749. $_SESSION['return'][] = array(
  4750. 'type' => 'danger',
  4751. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4752. 'msg' => $e->getMessage()
  4753. );
  4754. return false;
  4755. }
  4756. return $footer;
  4757. break;
  4758. }
  4759. break;
  4760. case 'delete':
  4761. switch ($_type) {
  4762. case 'syncjob':
  4763. if (!is_array($_data['id'])) {
  4764. $ids = array();
  4765. $ids[] = $_data['id'];
  4766. }
  4767. else {
  4768. $ids = $_data['id'];
  4769. }
  4770. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  4771. $_SESSION['return'][] = array(
  4772. 'type' => 'danger',
  4773. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4774. 'msg' => 'access_denied'
  4775. );
  4776. return false;
  4777. }
  4778. foreach ($ids as $id) {
  4779. if (!is_numeric($id)) {
  4780. return false;
  4781. }
  4782. $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
  4783. $stmt->execute(array(':id' => $id));
  4784. $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
  4785. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
  4786. $_SESSION['return'][] = array(
  4787. 'type' => 'danger',
  4788. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4789. 'msg' => 'access_denied'
  4790. );
  4791. continue;
  4792. }
  4793. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
  4794. $stmt->execute(array(':id' => $id));
  4795. $_SESSION['return'][] = array(
  4796. 'type' => 'success',
  4797. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4798. 'msg' => array('deleted_syncjob', $id)
  4799. );
  4800. }
  4801. break;
  4802. case 'filter':
  4803. if (!is_array($_data['id'])) {
  4804. $ids = array();
  4805. $ids[] = $_data['id'];
  4806. }
  4807. else {
  4808. $ids = $_data['id'];
  4809. }
  4810. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  4811. $_SESSION['return'][] = array(
  4812. 'type' => 'danger',
  4813. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4814. 'msg' => 'access_denied'
  4815. );
  4816. return false;
  4817. }
  4818. foreach ($ids as $id) {
  4819. if (!is_numeric($id)) {
  4820. continue;
  4821. }
  4822. $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
  4823. $stmt->execute(array(':id' => $id));
  4824. $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
  4825. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
  4826. $_SESSION['return'][] = array(
  4827. 'type' => 'danger',
  4828. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4829. 'msg' => 'access_denied'
  4830. );
  4831. continue;
  4832. }
  4833. $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
  4834. $stmt->execute(array(':id' => $id));
  4835. $_SESSION['return'][] = array(
  4836. 'type' => 'success',
  4837. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4838. 'msg' => array('delete_filter', $id)
  4839. );
  4840. }
  4841. break;
  4842. case 'time_limited_alias':
  4843. if (!is_array($_data['address'])) {
  4844. $addresses = array();
  4845. $addresses[] = $_data['address'];
  4846. }
  4847. else {
  4848. $addresses = $_data['address'];
  4849. }
  4850. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  4851. $_SESSION['return'][] = array(
  4852. 'type' => 'danger',
  4853. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4854. 'msg' => 'access_denied'
  4855. );
  4856. return false;
  4857. }
  4858. foreach ($addresses as $address) {
  4859. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  4860. $stmt->execute(array(':address' => $address));
  4861. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  4862. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  4863. $_SESSION['return'][] = array(
  4864. 'type' => 'danger',
  4865. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4866. 'msg' => 'access_denied'
  4867. );
  4868. continue;
  4869. }
  4870. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
  4871. $stmt->execute(array(
  4872. ':username' => $goto,
  4873. ':item' => $address
  4874. ));
  4875. $_SESSION['return'][] = array(
  4876. 'type' => 'success',
  4877. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4878. 'msg' => array('mailbox_modified', htmlspecialchars($goto))
  4879. );
  4880. }
  4881. break;
  4882. case 'eas_cache':
  4883. if (!is_array($_data['username'])) {
  4884. $usernames = array();
  4885. $usernames[] = $_data['username'];
  4886. }
  4887. else {
  4888. $usernames = $_data['username'];
  4889. }
  4890. if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
  4891. $_SESSION['return'][] = array(
  4892. 'type' => 'danger',
  4893. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4894. 'msg' => 'access_denied'
  4895. );
  4896. return false;
  4897. }
  4898. foreach ($usernames as $username) {
  4899. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4900. $_SESSION['return'][] = array(
  4901. 'type' => 'danger',
  4902. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4903. 'msg' => 'access_denied'
  4904. );
  4905. continue;
  4906. }
  4907. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4908. $stmt->execute(array(
  4909. ':username' => $username
  4910. ));
  4911. $_SESSION['return'][] = array(
  4912. 'type' => 'success',
  4913. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4914. 'msg' => array('eas_reset', htmlspecialchars($username))
  4915. );
  4916. }
  4917. break;
  4918. case 'sogo_profile':
  4919. if (!is_array($_data['username'])) {
  4920. $usernames = array();
  4921. $usernames[] = $_data['username'];
  4922. }
  4923. else {
  4924. $usernames = $_data['username'];
  4925. }
  4926. if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
  4927. $_SESSION['return'][] = array(
  4928. 'type' => 'danger',
  4929. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4930. 'msg' => 'access_denied'
  4931. );
  4932. return false;
  4933. }
  4934. foreach ($usernames as $username) {
  4935. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4936. $_SESSION['return'][] = array(
  4937. 'type' => 'danger',
  4938. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4939. 'msg' => 'access_denied'
  4940. );
  4941. continue;
  4942. }
  4943. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  4944. $stmt->execute(array(
  4945. ':username' => $username
  4946. ));
  4947. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4948. $stmt->execute(array(
  4949. ':username' => $username
  4950. ));
  4951. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
  4952. $stmt->execute(array(
  4953. ':username' => $username
  4954. ));
  4955. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4956. $stmt->execute(array(
  4957. ':username' => $username
  4958. ));
  4959. $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)");
  4960. $stmt->execute(array(
  4961. ':username' => $username
  4962. ));
  4963. $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)");
  4964. $stmt->execute(array(
  4965. ':username' => $username
  4966. ));
  4967. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  4968. $stmt->execute(array(
  4969. ':username' => $username
  4970. ));
  4971. $_SESSION['return'][] = array(
  4972. 'type' => 'success',
  4973. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4974. 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
  4975. );
  4976. }
  4977. break;
  4978. case 'domain':
  4979. if (!is_array($_data['domain'])) {
  4980. $domains = array();
  4981. $domains[] = $_data['domain'];
  4982. }
  4983. else {
  4984. $domains = $_data['domain'];
  4985. }
  4986. if ($_SESSION['mailcow_cc_role'] != "admin") {
  4987. $_SESSION['return'][] = array(
  4988. 'type' => 'danger',
  4989. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4990. 'msg' => 'access_denied'
  4991. );
  4992. return false;
  4993. }
  4994. foreach ($domains as $domain) {
  4995. if (!is_valid_domain_name($domain)) {
  4996. $_SESSION['return'][] = array(
  4997. 'type' => 'danger',
  4998. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4999. 'msg' => 'domain_invalid'
  5000. );
  5001. continue;
  5002. }
  5003. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  5004. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  5005. WHERE `domain` = :domain");
  5006. $stmt->execute(array(':domain' => $domain));
  5007. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  5008. if ($num_results != 0 || !empty($num_results)) {
  5009. $_SESSION['return'][] = array(
  5010. 'type' => 'danger',
  5011. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5012. 'msg' => array('domain_not_empty', $domain)
  5013. );
  5014. continue;
  5015. }
  5016. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
  5017. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  5018. if ($maildir_gc['type'] != 'success') {
  5019. $_SESSION['return'][] = array(
  5020. 'type' => 'warning',
  5021. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5022. 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
  5023. );
  5024. }
  5025. $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
  5026. $stmt->execute(array(
  5027. ':domain' => $domain,
  5028. ));
  5029. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
  5030. $stmt->execute(array(
  5031. ':domain' => $domain,
  5032. ));
  5033. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
  5034. $stmt->execute(array(
  5035. ':domain' => $domain,
  5036. ));
  5037. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
  5038. $stmt->execute(array(
  5039. ':domain' => $domain,
  5040. ));
  5041. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
  5042. $stmt->execute(array(
  5043. ':domain' => $domain,
  5044. ));
  5045. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
  5046. $stmt->execute(array(
  5047. ':domain' => '%@'.$domain,
  5048. ));
  5049. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
  5050. $stmt->execute(array(
  5051. ':domain' => '%@'.$domain,
  5052. ));
  5053. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
  5054. $stmt->execute(array(
  5055. ':domain' => '%@'.$domain,
  5056. ));
  5057. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
  5058. $stmt->execute(array(
  5059. ':domain' => '%@'.$domain,
  5060. ));
  5061. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5062. $stmt->execute(array(
  5063. ':domain' => '%@'.$domain,
  5064. ));
  5065. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
  5066. $stmt->execute(array(
  5067. ':domain' => $domain,
  5068. ));
  5069. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
  5070. $stmt->execute(array(
  5071. ':domain' => $domain,
  5072. ));
  5073. $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5074. $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5075. try {
  5076. $redis->hDel('DOMAIN_MAP', $domain);
  5077. $redis->hDel('RL_VALUE', $domain);
  5078. }
  5079. catch (RedisException $e) {
  5080. $_SESSION['return'][] = array(
  5081. 'type' => 'danger',
  5082. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5083. 'msg' => array('redis_error', $e)
  5084. );
  5085. continue;
  5086. }
  5087. $_SESSION['return'][] = array(
  5088. 'type' => 'success',
  5089. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5090. 'msg' => array('domain_removed', htmlspecialchars($domain))
  5091. );
  5092. }
  5093. break;
  5094. case 'domain_templates':
  5095. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5096. $_SESSION['return'][] = array(
  5097. 'type' => 'danger',
  5098. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5099. 'msg' => 'access_denied'
  5100. );
  5101. return false;
  5102. }
  5103. if (!is_array($_data['ids'])) {
  5104. $ids = array();
  5105. $ids[] = $_data['ids'];
  5106. }
  5107. else {
  5108. $ids = $_data['ids'];
  5109. }
  5110. foreach ($ids as $id) {
  5111. // delete template
  5112. $stmt = $pdo->prepare("DELETE FROM `templates`
  5113. WHERE id = :id AND type = :type AND NOT template = :template");
  5114. $stmt->execute(array(
  5115. ":id" => $id,
  5116. ":type" => "domain",
  5117. ":template" => "Default"
  5118. ));
  5119. $_SESSION['return'][] = array(
  5120. 'type' => 'success',
  5121. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5122. 'msg' => array('template_removed', htmlspecialchars($id))
  5123. );
  5124. return true;
  5125. }
  5126. break;
  5127. case 'alias':
  5128. if (!is_array($_data['id'])) {
  5129. $ids = array();
  5130. $ids[] = $_data['id'];
  5131. }
  5132. else {
  5133. $ids = $_data['id'];
  5134. }
  5135. foreach ($ids as $id) {
  5136. $alias_data = mailbox('get', 'alias_details', $id);
  5137. if (empty($alias_data)) {
  5138. $_SESSION['return'][] = array(
  5139. 'type' => 'danger',
  5140. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5141. 'msg' => 'access_denied'
  5142. );
  5143. continue;
  5144. }
  5145. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
  5146. $stmt->execute(array(
  5147. ':id' => $alias_data['id']
  5148. ));
  5149. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
  5150. $stmt->execute(array(
  5151. ':alias_address' => $alias_data['address']
  5152. ));
  5153. $_SESSION['return'][] = array(
  5154. 'type' => 'success',
  5155. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5156. 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
  5157. );
  5158. }
  5159. break;
  5160. case 'alias_domain':
  5161. if (!is_array($_data['alias_domain'])) {
  5162. $alias_domains = array();
  5163. $alias_domains[] = $_data['alias_domain'];
  5164. }
  5165. else {
  5166. $alias_domains = $_data['alias_domain'];
  5167. }
  5168. foreach ($alias_domains as $alias_domain) {
  5169. if (!is_valid_domain_name($alias_domain)) {
  5170. $_SESSION['return'][] = array(
  5171. 'type' => 'danger',
  5172. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5173. 'msg' => 'domain_invalid'
  5174. );
  5175. continue;
  5176. }
  5177. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
  5178. WHERE `alias_domain`= :alias_domain");
  5179. $stmt->execute(array(':alias_domain' => $alias_domain));
  5180. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  5181. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
  5182. $_SESSION['return'][] = array(
  5183. 'type' => 'danger',
  5184. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5185. 'msg' => 'access_denied'
  5186. );
  5187. continue;
  5188. }
  5189. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  5190. $stmt->execute(array(
  5191. ':alias_domain' => $alias_domain,
  5192. ));
  5193. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
  5194. $stmt->execute(array(
  5195. ':alias_domain' => $alias_domain,
  5196. ));
  5197. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5198. $stmt->execute(array(
  5199. ':domain' => '%@'.$alias_domain,
  5200. ));
  5201. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
  5202. $stmt->execute(array(
  5203. ':alias_domain' => $alias_domain,
  5204. ));
  5205. try {
  5206. $redis->hDel('DOMAIN_MAP', $alias_domain);
  5207. $redis->hDel('RL_VALUE', $domain);
  5208. }
  5209. catch (RedisException $e) {
  5210. $_SESSION['return'][] = array(
  5211. 'type' => 'danger',
  5212. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5213. 'msg' => array('redis_error', $e)
  5214. );
  5215. continue;
  5216. }
  5217. $_SESSION['return'][] = array(
  5218. 'type' => 'success',
  5219. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5220. 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
  5221. );
  5222. }
  5223. break;
  5224. case 'mailbox':
  5225. if (!is_array($_data['username'])) {
  5226. $usernames = array();
  5227. $usernames[] = $_data['username'];
  5228. }
  5229. else {
  5230. $usernames = $_data['username'];
  5231. }
  5232. foreach ($usernames as $username) {
  5233. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5234. $_SESSION['return'][] = array(
  5235. 'type' => 'danger',
  5236. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5237. 'msg' => 'access_denied'
  5238. );
  5239. continue;
  5240. }
  5241. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  5242. $_SESSION['return'][] = array(
  5243. 'type' => 'danger',
  5244. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5245. 'msg' => 'access_denied'
  5246. );
  5247. continue;
  5248. }
  5249. $mailbox_details = mailbox('get', 'mailbox_details', $username);
  5250. if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
  5251. $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
  5252. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
  5253. if (getenv("CLUSTERMODE") == "replication") {
  5254. // broadcast to each dovecot container
  5255. docker('broadcast', 'dovecot-mailcow', 'exec', $exec_fields);
  5256. } else {
  5257. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  5258. if ($maildir_gc['type'] != 'success') {
  5259. $_SESSION['return'][] = array(
  5260. 'type' => 'warning',
  5261. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5262. 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
  5263. );
  5264. }
  5265. }
  5266. }
  5267. else {
  5268. $_SESSION['return'][] = array(
  5269. 'type' => 'warning',
  5270. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5271. 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
  5272. );
  5273. }
  5274. if (strtolower(getenv('SKIP_SOLR')) == 'n' && strtolower(getenv('FLATCURVE_EXPERIMENTAL')) != 'y') {
  5275. $curl = curl_init();
  5276. curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
  5277. curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
  5278. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  5279. curl_setopt($curl, CURLOPT_POST, 1);
  5280. curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
  5281. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  5282. $response = curl_exec($curl);
  5283. if ($response === false) {
  5284. $err = curl_error($curl);
  5285. $_SESSION['return'][] = array(
  5286. 'type' => 'warning',
  5287. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5288. 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
  5289. );
  5290. }
  5291. curl_close($curl);
  5292. }
  5293. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
  5294. $stmt->execute(array(
  5295. ':username' => $username
  5296. ));
  5297. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
  5298. $stmt->execute(array(
  5299. ':username' => $username
  5300. ));
  5301. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
  5302. $stmt->execute(array(
  5303. ':username' => $username
  5304. ));
  5305. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
  5306. $stmt->execute(array(
  5307. ':username' => $username
  5308. ));
  5309. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
  5310. $stmt->execute(array(
  5311. ':username' => $username
  5312. ));
  5313. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5314. $stmt->execute(array(
  5315. ':username' => $username
  5316. ));
  5317. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as OR `send_as` = :send_as");
  5318. $stmt->execute(array(
  5319. ':logged_in_as' => $username,
  5320. ':send_as' => $username
  5321. ));
  5322. // fk, better safe than sorry
  5323. $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
  5324. $stmt->execute(array(
  5325. ':username' => $username
  5326. ));
  5327. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
  5328. $stmt->execute(array(
  5329. ':username' => $username
  5330. ));
  5331. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
  5332. $stmt->execute(array(
  5333. ':username' => $username
  5334. ));
  5335. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
  5336. $stmt->execute(array(
  5337. ':username' => $username
  5338. ));
  5339. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5340. $stmt->execute(array(
  5341. ':username' => $username
  5342. ));
  5343. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5344. $stmt->execute(array(
  5345. ':username' => $username
  5346. ));
  5347. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
  5348. $stmt->execute(array(
  5349. ':username' => $username
  5350. ));
  5351. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5352. $stmt->execute(array(
  5353. ':username' => $username
  5354. ));
  5355. $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)");
  5356. $stmt->execute(array(
  5357. ':username' => $username
  5358. ));
  5359. $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)");
  5360. $stmt->execute(array(
  5361. ':username' => $username
  5362. ));
  5363. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5364. $stmt->execute(array(
  5365. ':username' => $username
  5366. ));
  5367. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
  5368. $stmt->execute(array(
  5369. ':username' => $username
  5370. ));
  5371. $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
  5372. $stmt->execute(array(
  5373. ':username' => $username
  5374. ));
  5375. $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
  5376. $stmt->execute(array(
  5377. ':username' => $username
  5378. ));
  5379. $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
  5380. $stmt->execute(array(
  5381. ':username' => $username
  5382. ));
  5383. $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
  5384. $stmt->execute(array(
  5385. ':username' => $username,
  5386. ));
  5387. $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
  5388. $stmt->execute(array(
  5389. ':username' => $username,
  5390. ));
  5391. $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
  5392. WHERE `goto` REGEXP :username");
  5393. $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
  5394. $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  5395. foreach ($GotoData as $gotos) {
  5396. $goto_exploded = explode(',', $gotos['goto']);
  5397. if (($key = array_search($username, $goto_exploded)) !== false) {
  5398. unset($goto_exploded[$key]);
  5399. }
  5400. $gotos_rebuild = implode(',', (array)$goto_exploded);
  5401. $stmt = $pdo->prepare("UPDATE `alias` SET
  5402. `goto` = :goto
  5403. WHERE `address` = :address");
  5404. $stmt->execute(array(
  5405. ':goto' => $gotos_rebuild,
  5406. ':address' => $gotos['address']
  5407. ));
  5408. }
  5409. try {
  5410. $redis->hDel('RL_VALUE', $username);
  5411. }
  5412. catch (RedisException $e) {
  5413. $_SESSION['return'][] = array(
  5414. 'type' => 'danger',
  5415. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5416. 'msg' => array('redis_error', $e)
  5417. );
  5418. continue;
  5419. }
  5420. update_sogo_static_view($username);
  5421. $_SESSION['return'][] = array(
  5422. 'type' => 'success',
  5423. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5424. 'msg' => array('mailbox_removed', htmlspecialchars($username))
  5425. );
  5426. }
  5427. return true;
  5428. break;
  5429. case 'mailbox_templates':
  5430. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5431. $_SESSION['return'][] = array(
  5432. 'type' => 'danger',
  5433. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5434. 'msg' => 'access_denied'
  5435. );
  5436. return false;
  5437. }
  5438. if (!is_array($_data['ids'])) {
  5439. $ids = array();
  5440. $ids[] = $_data['ids'];
  5441. }
  5442. else {
  5443. $ids = $_data['ids'];
  5444. }
  5445. foreach ($ids as $id) {
  5446. // delete template
  5447. $stmt = $pdo->prepare("DELETE FROM `templates`
  5448. WHERE id = :id AND type = :type AND NOT template = :template");
  5449. $stmt->execute(array(
  5450. ":id" => $id,
  5451. ":type" => "mailbox",
  5452. ":template" => "Default"
  5453. ));
  5454. }
  5455. $_SESSION['return'][] = array(
  5456. 'type' => 'success',
  5457. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5458. 'msg' => 'template_removed'
  5459. );
  5460. return true;
  5461. break;
  5462. case 'resource':
  5463. if (!is_array($_data['name'])) {
  5464. $names = array();
  5465. $names[] = $_data['name'];
  5466. }
  5467. else {
  5468. $names = $_data['name'];
  5469. }
  5470. foreach ($names as $name) {
  5471. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  5472. $_SESSION['return'][] = array(
  5473. 'type' => 'danger',
  5474. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5475. 'msg' => 'access_denied'
  5476. );
  5477. continue;
  5478. }
  5479. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  5480. $_SESSION['return'][] = array(
  5481. 'type' => 'danger',
  5482. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5483. 'msg' => 'access_denied'
  5484. );
  5485. continue;
  5486. }
  5487. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5488. $stmt->execute(array(
  5489. ':username' => $name
  5490. ));
  5491. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5492. $stmt->execute(array(
  5493. ':username' => $name
  5494. ));
  5495. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5496. $stmt->execute(array(
  5497. ':username' => $name
  5498. ));
  5499. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
  5500. $stmt->execute(array(
  5501. ':username' => $name
  5502. ));
  5503. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5504. $stmt->execute(array(
  5505. ':username' => $name
  5506. ));
  5507. $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)");
  5508. $stmt->execute(array(
  5509. ':username' => $name
  5510. ));
  5511. $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)");
  5512. $stmt->execute(array(
  5513. ':username' => $name
  5514. ));
  5515. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5516. $stmt->execute(array(
  5517. ':username' => $name
  5518. ));
  5519. $_SESSION['return'][] = array(
  5520. 'type' => 'success',
  5521. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5522. 'msg' => array('resource_removed', htmlspecialchars($name))
  5523. );
  5524. }
  5525. break;
  5526. case 'tags_domain':
  5527. if (!is_array($_data['domain'])) {
  5528. $domains = array();
  5529. $domains[] = $_data['domain'];
  5530. }
  5531. else {
  5532. $domains = $_data['domain'];
  5533. }
  5534. $tags = $_data['tags'];
  5535. if (!is_array($tags)) $tags = array();
  5536. $wasModified = false;
  5537. foreach ($domains as $domain) {
  5538. if (!is_valid_domain_name($domain)) {
  5539. $_SESSION['return'][] = array(
  5540. 'type' => 'danger',
  5541. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5542. 'msg' => 'domain_invalid'
  5543. );
  5544. continue;
  5545. }
  5546. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5547. $_SESSION['return'][] = array(
  5548. 'type' => 'danger',
  5549. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5550. 'msg' => 'access_denied'
  5551. );
  5552. return false;
  5553. }
  5554. foreach($tags as $tag){
  5555. // delete tag
  5556. $wasModified = true;
  5557. $stmt = $pdo->prepare("DELETE FROM `tags_domain` WHERE `domain` = :domain AND `tag_name` = :tag_name");
  5558. $stmt->execute(array(
  5559. ':domain' => $domain,
  5560. ':tag_name' => $tag,
  5561. ));
  5562. }
  5563. }
  5564. if (!$wasModified) return false;
  5565. $_SESSION['return'][] = array(
  5566. 'type' => 'success',
  5567. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5568. 'msg' => array('domain_modified', $domain)
  5569. );
  5570. break;
  5571. case 'tags_mailbox':
  5572. if (!is_array($_data['username'])) {
  5573. $usernames = array();
  5574. $usernames[] = $_data['username'];
  5575. }
  5576. else {
  5577. $usernames = $_data['username'];
  5578. }
  5579. $tags = $_data['tags'];
  5580. if (!is_array($tags)) $tags = array();
  5581. $wasModified = false;
  5582. foreach ($usernames as $username) {
  5583. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5584. $_SESSION['return'][] = array(
  5585. 'type' => 'danger',
  5586. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5587. 'msg' => 'email invalid'
  5588. );
  5589. continue;
  5590. }
  5591. $is_now = mailbox('get', 'mailbox_details', $username);
  5592. $domain = $is_now['domain'];
  5593. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5594. $_SESSION['return'][] = array(
  5595. 'type' => 'danger',
  5596. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5597. 'msg' => 'access_denied'
  5598. );
  5599. continue;
  5600. }
  5601. // delete tags
  5602. foreach($tags as $tag){
  5603. $wasModified = true;
  5604. $stmt = $pdo->prepare("DELETE FROM `tags_mailbox` WHERE `username` = :username AND `tag_name` = :tag_name");
  5605. $stmt->execute(array(
  5606. ':username' => $username,
  5607. ':tag_name' => $tag,
  5608. ));
  5609. }
  5610. }
  5611. if (!$wasModified) return false;
  5612. $_SESSION['return'][] = array(
  5613. 'type' => 'success',
  5614. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5615. 'msg' => array('mailbox_modified', $username)
  5616. );
  5617. break;
  5618. }
  5619. break;
  5620. }
  5621. if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'resource')) && getenv('SKIP_SOGO') != "y") {
  5622. update_sogo_static_view();
  5623. }
  5624. }