functions.mailbox.inc.php 270 KB

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