functions.mailbox.inc.php 268 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689
  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'))){
  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'])['attributes'];
  1024. }
  1025. if (empty($template_attr)) {
  1026. $template_attr = mailbox('get', 'mailbox_templates')[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. update_sogo_static_view($username);
  1316. $_SESSION['return'][] = array(
  1317. 'type' => 'success',
  1318. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1319. 'msg' => array('mailbox_added', htmlspecialchars($username))
  1320. );
  1321. break;
  1322. case 'mailbox_from_template':
  1323. $stmt = $pdo->prepare("SELECT * FROM `templates`
  1324. WHERE `template` = :template AND type = 'mailbox'");
  1325. $stmt->execute(array(
  1326. ":template" => $_data['template']
  1327. ));
  1328. $mbox_template_data = $stmt->fetch(PDO::FETCH_ASSOC);
  1329. if (empty($mbox_template_data)){
  1330. $_SESSION['return'][] = array(
  1331. 'type' => 'danger',
  1332. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1333. 'msg' => 'template_missing'
  1334. );
  1335. return false;
  1336. }
  1337. $attribute_hash = sha1(json_encode($mbox_template_data["attributes"]));
  1338. $mbox_template_data = json_decode($mbox_template_data["attributes"], true);
  1339. $mbox_template_data['domain'] = $_data['domain'];
  1340. $mbox_template_data['local_part'] = $_data['local_part'];
  1341. $mbox_template_data['authsource'] = $_data['authsource'];
  1342. $mbox_template_data['attribute_hash'] = $attribute_hash;
  1343. $mbox_template_data['quota'] = intval($mbox_template_data['quota'] / 1048576);
  1344. $mailbox_attributes = array('acl' => array());
  1345. foreach ($mbox_template_data as $key => $value){
  1346. switch (true) {
  1347. case (strpos($key, 'acl_') === 0 && $value != 0):
  1348. array_push($mailbox_attributes['acl'], str_replace('acl_' , '', $key));
  1349. break;
  1350. default:
  1351. $mailbox_attributes[$key] = $value;
  1352. break;
  1353. }
  1354. }
  1355. return mailbox('add', 'mailbox', $mailbox_attributes, array('iam_create_login' => true));
  1356. break;
  1357. case 'resource':
  1358. $domain = idn_to_ascii(strtolower(trim($_data['domain'])), 0, INTL_IDNA_VARIANT_UTS46);
  1359. $description = $_data['description'];
  1360. $local_part = preg_replace('/[^\da-z]/i', '', preg_quote($description, '/'));
  1361. $name = $local_part . '@' . $domain;
  1362. $kind = $_data['kind'];
  1363. $multiple_bookings = intval($_data['multiple_bookings']);
  1364. $active = intval($_data['active']);
  1365. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  1366. $_SESSION['return'][] = array(
  1367. 'type' => 'danger',
  1368. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1369. 'msg' => 'resource_invalid'
  1370. );
  1371. return false;
  1372. }
  1373. if (empty($description)) {
  1374. $_SESSION['return'][] = array(
  1375. 'type' => 'danger',
  1376. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1377. 'msg' => 'description_invalid'
  1378. );
  1379. return false;
  1380. }
  1381. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  1382. $multiple_bookings = -1;
  1383. }
  1384. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  1385. $_SESSION['return'][] = array(
  1386. 'type' => 'danger',
  1387. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1388. 'msg' => 'resource_invalid'
  1389. );
  1390. return false;
  1391. }
  1392. if (!is_valid_domain_name($domain)) {
  1393. $_SESSION['return'][] = array(
  1394. 'type' => 'danger',
  1395. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1396. 'msg' => 'domain_invalid'
  1397. );
  1398. return false;
  1399. }
  1400. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  1401. $_SESSION['return'][] = array(
  1402. 'type' => 'danger',
  1403. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1404. 'msg' => 'access_denied'
  1405. );
  1406. return false;
  1407. }
  1408. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :name");
  1409. $stmt->execute(array(':name' => $name));
  1410. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1411. if ($num_results != 0) {
  1412. $_SESSION['return'][] = array(
  1413. 'type' => 'danger',
  1414. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1415. 'msg' => array('object_exists', htmlspecialchars($name))
  1416. );
  1417. return false;
  1418. }
  1419. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE address= :name");
  1420. $stmt->execute(array(':name' => $name));
  1421. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1422. if ($num_results != 0) {
  1423. $_SESSION['return'][] = array(
  1424. 'type' => 'danger',
  1425. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1426. 'msg' => array('is_alias', htmlspecialchars($name))
  1427. );
  1428. return false;
  1429. }
  1430. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias` WHERE `address`= :name");
  1431. $stmt->execute(array(':name' => $name));
  1432. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1433. if ($num_results != 0) {
  1434. $_SESSION['return'][] = array(
  1435. 'type' => 'danger',
  1436. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1437. 'msg' => array('is_spam_alias', htmlspecialchars($name))
  1438. );
  1439. return false;
  1440. }
  1441. $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain`= :domain");
  1442. $stmt->execute(array(':domain' => $domain));
  1443. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  1444. if ($num_results == 0) {
  1445. $_SESSION['return'][] = array(
  1446. 'type' => 'danger',
  1447. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1448. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  1449. );
  1450. return false;
  1451. }
  1452. $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
  1453. VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
  1454. $stmt->execute(array(
  1455. ':name' => $name,
  1456. ':description' => $description,
  1457. ':local_part' => $local_part,
  1458. ':domain' => $domain,
  1459. ':active' => $active,
  1460. ':kind' => $kind,
  1461. ':multiple_bookings' => $multiple_bookings
  1462. ));
  1463. $_SESSION['return'][] = array(
  1464. 'type' => 'success',
  1465. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1466. 'msg' => array('resource_added', htmlspecialchars($name))
  1467. );
  1468. break;
  1469. case 'domain_templates':
  1470. if ($_SESSION['mailcow_cc_role'] != "admin") {
  1471. $_SESSION['return'][] = array(
  1472. 'type' => 'danger',
  1473. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1474. 'msg' => 'access_denied'
  1475. );
  1476. return false;
  1477. }
  1478. if (empty($_data["template"])){
  1479. $_SESSION['return'][] = array(
  1480. 'type' => 'danger',
  1481. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1482. 'msg' => 'template_name_invalid'
  1483. );
  1484. return false;
  1485. }
  1486. // check if template name exists, return false
  1487. $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
  1488. $stmt->execute(array(
  1489. ":type" => "domain",
  1490. ":template" => $_data["template"]
  1491. ));
  1492. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  1493. if (!empty($row)){
  1494. $_SESSION['return'][] = array(
  1495. 'type' => 'danger',
  1496. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1497. 'msg' => array('template_exists', $_data["template"])
  1498. );
  1499. return false;
  1500. }
  1501. // check attributes
  1502. $attr = array();
  1503. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
  1504. $attr['max_num_aliases_for_domain'] = (!empty($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 400;
  1505. $attr['max_num_mboxes_for_domain'] = (!empty($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 10;
  1506. $attr['def_quota_for_mbox'] = (!empty($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 3072 * 1048576;
  1507. $attr['max_quota_for_mbox'] = (!empty($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 10240 * 1048576;
  1508. $attr['max_quota_for_domain'] = (!empty($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 10240 * 1048576;
  1509. $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
  1510. $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
  1511. $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
  1512. $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
  1513. $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
  1514. $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
  1515. $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
  1516. $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
  1517. $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
  1518. // save template
  1519. $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
  1520. VALUES (:type, :template, :attributes)");
  1521. $stmt->execute(array(
  1522. ":type" => "domain",
  1523. ":template" => $_data["template"],
  1524. ":attributes" => json_encode($attr)
  1525. ));
  1526. // success
  1527. $_SESSION['return'][] = array(
  1528. 'type' => 'success',
  1529. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1530. 'msg' => array('template_added', $_data["template"])
  1531. );
  1532. return true;
  1533. break;
  1534. case 'mailbox_templates':
  1535. if ($_SESSION['mailcow_cc_role'] != "admin") {
  1536. $_SESSION['return'][] = array(
  1537. 'type' => 'danger',
  1538. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1539. 'msg' => 'access_denied'
  1540. );
  1541. return false;
  1542. }
  1543. if (empty($_data["template"])){
  1544. $_SESSION['return'][] = array(
  1545. 'type' => 'danger',
  1546. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1547. 'msg' => 'template_name_invalid'
  1548. );
  1549. return false;
  1550. }
  1551. // check if template name exists, return false
  1552. $stmt = $pdo->prepare("SELECT id FROM `templates` WHERE `type` = :type AND `template` = :template");
  1553. $stmt->execute(array(
  1554. ":type" => "mailbox",
  1555. ":template" => $_data["template"]
  1556. ));
  1557. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  1558. if (!empty($row)){
  1559. $_SESSION['return'][] = array(
  1560. 'type' => 'danger',
  1561. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  1562. 'msg' => array('template_exists', $_data["template"])
  1563. );
  1564. return false;
  1565. }
  1566. // check attributes
  1567. $attr = array();
  1568. $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
  1569. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
  1570. $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_notification']);
  1571. $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : strval($MAILBOX_DEFAULT_ATTRIBUTES['quarantine_category']);
  1572. $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
  1573. $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
  1574. $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['force_pw_update']);
  1575. $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['sogo_access']);
  1576. $attr["active"] = isset($_data['active']) ? intval($_data['active']) : 1;
  1577. $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_in']);
  1578. $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : intval($MAILBOX_DEFAULT_ATTRIBUTES['tls_enforce_out']);
  1579. if (isset($_data['protocol_access'])) {
  1580. $_data['protocol_access'] = (array)$_data['protocol_access'];
  1581. $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  1582. $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  1583. $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  1584. $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
  1585. }
  1586. else {
  1587. $attr['imap_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['imap_access']);
  1588. $attr['pop3_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['pop3_access']);
  1589. $attr['smtp_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['smtp_access']);
  1590. $attr['sieve_access'] = intval($MAILBOX_DEFAULT_ATTRIBUTES['sieve_access']);
  1591. }
  1592. if (isset($_data['acl'])) {
  1593. $_data['acl'] = (array)$_data['acl'];
  1594. $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
  1595. $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
  1596. $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
  1597. $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
  1598. $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
  1599. $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
  1600. $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
  1601. $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
  1602. $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
  1603. $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
  1604. $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
  1605. $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
  1606. $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
  1607. $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
  1608. } else {
  1609. $_data['acl'] = (array)$_data['acl'];
  1610. $attr['acl_spam_alias'] = 0;
  1611. $attr['acl_tls_policy'] = 0;
  1612. $attr['acl_spam_score'] = 0;
  1613. $attr['acl_spam_policy'] = 0;
  1614. $attr['acl_delimiter_action'] = 0;
  1615. $attr['acl_syncjobs'] = 0;
  1616. $attr['acl_eas_reset'] = 0;
  1617. $attr['acl_sogo_profile_reset'] = 0;
  1618. $attr['acl_pushover'] = 0;
  1619. $attr['acl_quarantine'] = 0;
  1620. $attr['acl_quarantine_attachments'] = 0;
  1621. $attr['acl_quarantine_notification'] = 0;
  1622. $attr['acl_quarantine_category'] = 0;
  1623. $attr['acl_app_passwds'] = 0;
  1624. }
  1625. // save template
  1626. $stmt = $pdo->prepare("INSERT INTO `templates` (`type`, `template`, `attributes`)
  1627. VALUES (:type, :template, :attributes)");
  1628. $stmt->execute(array(
  1629. ":type" => "mailbox",
  1630. ":template" => $_data["template"],
  1631. ":attributes" => json_encode($attr)
  1632. ));
  1633. // success
  1634. $_SESSION['return'][] = array(
  1635. 'type' => 'success',
  1636. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1637. 'msg' => array('template_added', $_data["template"])
  1638. );
  1639. return true;
  1640. break;
  1641. }
  1642. break;
  1643. case 'edit':
  1644. switch ($_type) {
  1645. case 'alias_domain':
  1646. $alias_domains = (array)$_data['alias_domain'];
  1647. foreach ($alias_domains as $alias_domain) {
  1648. $alias_domain = idn_to_ascii(strtolower(trim($alias_domain)), 0, INTL_IDNA_VARIANT_UTS46);
  1649. $is_now = mailbox('get', 'alias_domain_details', $alias_domain);
  1650. if (!empty($is_now)) {
  1651. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  1652. $target_domain = (!empty($_data['target_domain'])) ? idn_to_ascii(strtolower(trim($_data['target_domain'])), 0, INTL_IDNA_VARIANT_UTS46) : $is_now['target_domain'];
  1653. }
  1654. else {
  1655. $_SESSION['return'][] = array(
  1656. 'type' => 'danger',
  1657. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1658. 'msg' => array('alias_domain_invalid', htmlspecialchars($alias_domain))
  1659. );
  1660. continue;
  1661. }
  1662. if (!is_valid_domain_name($target_domain)) {
  1663. $_SESSION['return'][] = array(
  1664. 'type' => 'danger',
  1665. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1666. 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
  1667. );
  1668. continue;
  1669. }
  1670. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $target_domain)) {
  1671. $_SESSION['return'][] = array(
  1672. 'type' => 'danger',
  1673. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1674. 'msg' => 'access_denied'
  1675. );
  1676. continue;
  1677. }
  1678. if (empty(mailbox('get', 'domain_details', $target_domain)) || !empty(mailbox('get', 'alias_domain_details', $target_domain))) {
  1679. $_SESSION['return'][] = array(
  1680. 'type' => 'danger',
  1681. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1682. 'msg' => array('target_domain_invalid', htmlspecialchars($target_domain))
  1683. );
  1684. continue;
  1685. }
  1686. $stmt = $pdo->prepare("UPDATE `alias_domain` SET
  1687. `target_domain` = :target_domain,
  1688. `active` = :active
  1689. WHERE `alias_domain` = :alias_domain");
  1690. $stmt->execute(array(
  1691. ':alias_domain' => $alias_domain,
  1692. ':target_domain' => $target_domain,
  1693. ':active' => $active
  1694. ));
  1695. $_SESSION['return'][] = array(
  1696. 'type' => 'success',
  1697. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1698. 'msg' => array('aliasd_modified', htmlspecialchars($alias_domain))
  1699. );
  1700. }
  1701. break;
  1702. case 'tls_policy':
  1703. if (!is_array($_data['username'])) {
  1704. $usernames = array();
  1705. $usernames[] = $_data['username'];
  1706. }
  1707. else {
  1708. $usernames = $_data['username'];
  1709. }
  1710. if (!isset($_SESSION['acl']['tls_policy']) || $_SESSION['acl']['tls_policy'] != "1" ) {
  1711. $_SESSION['return'][] = array(
  1712. 'type' => 'danger',
  1713. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1714. 'msg' => 'access_denied'
  1715. );
  1716. return false;
  1717. }
  1718. foreach ($usernames as $username) {
  1719. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1720. $_SESSION['return'][] = array(
  1721. 'type' => 'danger',
  1722. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1723. 'msg' => 'access_denied'
  1724. );
  1725. continue;
  1726. }
  1727. $is_now = mailbox('get', 'tls_policy', $username);
  1728. if (!empty($is_now)) {
  1729. $tls_enforce_in = (isset($_data['tls_enforce_in'])) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
  1730. $tls_enforce_out = (isset($_data['tls_enforce_out'])) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
  1731. }
  1732. else {
  1733. $_SESSION['return'][] = array(
  1734. 'type' => 'danger',
  1735. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1736. 'msg' => 'access_denied'
  1737. );
  1738. continue;
  1739. }
  1740. $stmt = $pdo->prepare("UPDATE `mailbox`
  1741. SET `attributes` = JSON_SET(`attributes`, '$.tls_enforce_out', :tls_out),
  1742. `attributes` = JSON_SET(`attributes`, '$.tls_enforce_in', :tls_in)
  1743. WHERE `username` = :username");
  1744. $stmt->execute(array(
  1745. ':tls_out' => intval($tls_enforce_out),
  1746. ':tls_in' => intval($tls_enforce_in),
  1747. ':username' => $username
  1748. ));
  1749. $_SESSION['return'][] = array(
  1750. 'type' => 'success',
  1751. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1752. 'msg' => array('mailbox_modified', $username)
  1753. );
  1754. }
  1755. break;
  1756. case 'quarantine_notification':
  1757. if (!is_array($_data['username'])) {
  1758. $usernames = array();
  1759. $usernames[] = $_data['username'];
  1760. }
  1761. else {
  1762. $usernames = $_data['username'];
  1763. }
  1764. if (!isset($_SESSION['acl']['quarantine_notification']) || $_SESSION['acl']['quarantine_notification'] != "1" ) {
  1765. $_SESSION['return'][] = array(
  1766. 'type' => 'danger',
  1767. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1768. 'msg' => 'access_denied'
  1769. );
  1770. return false;
  1771. }
  1772. foreach ($usernames as $username) {
  1773. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1774. $_SESSION['return'][] = array(
  1775. 'type' => 'danger',
  1776. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1777. 'msg' => 'access_denied'
  1778. );
  1779. continue;
  1780. }
  1781. $is_now = mailbox('get', 'quarantine_notification', $username);
  1782. if (!empty($is_now)) {
  1783. $quarantine_notification = (isset($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
  1784. }
  1785. else {
  1786. $_SESSION['return'][] = array(
  1787. 'type' => 'danger',
  1788. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1789. 'msg' => 'access_denied'
  1790. );
  1791. continue;
  1792. }
  1793. if (!in_array($quarantine_notification, array('never', 'hourly', 'daily', 'weekly'))) {
  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. $stmt = $pdo->prepare("UPDATE `mailbox`
  1802. SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
  1803. WHERE `username` = :username");
  1804. $stmt->execute(array(
  1805. ':quarantine_notification' => $quarantine_notification,
  1806. ':username' => $username
  1807. ));
  1808. $_SESSION['return'][] = array(
  1809. 'type' => 'success',
  1810. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1811. 'msg' => array('mailbox_modified', $username)
  1812. );
  1813. }
  1814. break;
  1815. case 'quarantine_category':
  1816. if (!is_array($_data['username'])) {
  1817. $usernames = array();
  1818. $usernames[] = $_data['username'];
  1819. }
  1820. else {
  1821. $usernames = $_data['username'];
  1822. }
  1823. if (!isset($_SESSION['acl']['quarantine_category']) || $_SESSION['acl']['quarantine_category'] != "1" ) {
  1824. $_SESSION['return'][] = array(
  1825. 'type' => 'danger',
  1826. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1827. 'msg' => 'access_denied'
  1828. );
  1829. return false;
  1830. }
  1831. foreach ($usernames as $username) {
  1832. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  1833. $_SESSION['return'][] = array(
  1834. 'type' => 'danger',
  1835. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1836. 'msg' => 'access_denied'
  1837. );
  1838. continue;
  1839. }
  1840. $is_now = mailbox('get', 'quarantine_category', $username);
  1841. if (!empty($is_now)) {
  1842. $quarantine_category = (isset($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
  1843. }
  1844. else {
  1845. $_SESSION['return'][] = array(
  1846. 'type' => 'danger',
  1847. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1848. 'msg' => 'access_denied'
  1849. );
  1850. continue;
  1851. }
  1852. if (!in_array($quarantine_category, array('add_header', 'reject', 'all'))) {
  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. $stmt = $pdo->prepare("UPDATE `mailbox`
  1861. SET `attributes` = JSON_SET(`attributes`, '$.quarantine_category', :quarantine_category)
  1862. WHERE `username` = :username");
  1863. $stmt->execute(array(
  1864. ':quarantine_category' => $quarantine_category,
  1865. ':username' => $username
  1866. ));
  1867. $_SESSION['return'][] = array(
  1868. 'type' => 'success',
  1869. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1870. 'msg' => array('mailbox_modified', $username)
  1871. );
  1872. }
  1873. break;
  1874. case 'spam_score':
  1875. if (!is_array($_data['username'])) {
  1876. $usernames = array();
  1877. $usernames[] = $_data['username'];
  1878. }
  1879. else {
  1880. $usernames = $_data['username'];
  1881. }
  1882. if (!isset($_SESSION['acl']['spam_score']) || $_SESSION['acl']['spam_score'] != "1" ) {
  1883. $_SESSION['return'][] = array(
  1884. 'type' => 'danger',
  1885. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1886. 'msg' => 'access_denied'
  1887. );
  1888. return false;
  1889. }
  1890. foreach ($usernames as $username) {
  1891. if ($_data['spam_score'] == "default") {
  1892. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
  1893. AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  1894. $stmt->execute(array(
  1895. ':username' => $username
  1896. ));
  1897. $_SESSION['return'][] = array(
  1898. 'type' => 'success',
  1899. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1900. 'msg' => array('mailbox_modified', $username)
  1901. );
  1902. continue;
  1903. }
  1904. $lowspamlevel = explode(',', $_data['spam_score'])[0];
  1905. $highspamlevel = explode(',', $_data['spam_score'])[1];
  1906. if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
  1907. $_SESSION['return'][] = array(
  1908. 'type' => 'danger',
  1909. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1910. 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
  1911. );
  1912. continue;
  1913. }
  1914. if ($lowspamlevel == $highspamlevel) {
  1915. $highspamlevel = $highspamlevel + 0.1;
  1916. }
  1917. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
  1918. AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  1919. $stmt->execute(array(
  1920. ':username' => $username
  1921. ));
  1922. $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
  1923. VALUES (:username, 'highspamlevel', :highspamlevel)");
  1924. $stmt->execute(array(
  1925. ':username' => $username,
  1926. ':highspamlevel' => $highspamlevel
  1927. ));
  1928. $stmt = $pdo->prepare("INSERT INTO `filterconf` (`object`, `option`, `value`)
  1929. VALUES (:username, 'lowspamlevel', :lowspamlevel)");
  1930. $stmt->execute(array(
  1931. ':username' => $username,
  1932. ':lowspamlevel' => $lowspamlevel
  1933. ));
  1934. $_SESSION['return'][] = array(
  1935. 'type' => 'success',
  1936. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1937. 'msg' => array('mailbox_modified', $username)
  1938. );
  1939. }
  1940. break;
  1941. case 'time_limited_alias':
  1942. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  1943. $_SESSION['return'][] = array(
  1944. 'type' => 'danger',
  1945. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1946. 'msg' => 'access_denied'
  1947. );
  1948. return false;
  1949. }
  1950. if (!is_array($_data['address'])) {
  1951. $addresses = array();
  1952. $addresses[] = $_data['address'];
  1953. }
  1954. else {
  1955. $addresses = $_data['address'];
  1956. }
  1957. foreach ($addresses as $address) {
  1958. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  1959. $stmt->execute(array(':address' => $address));
  1960. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  1961. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  1962. $_SESSION['return'][] = array(
  1963. 'type' => 'danger',
  1964. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1965. 'msg' => 'access_denied'
  1966. );
  1967. continue;
  1968. }
  1969. if (empty($_data['validity'])) {
  1970. continue;
  1971. }
  1972. $validity = round((int)time() + ($_data['validity'] * 3600));
  1973. $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE
  1974. `address` = :address");
  1975. $stmt->execute(array(
  1976. ':address' => $address,
  1977. ':validity' => $validity
  1978. ));
  1979. $_SESSION['return'][] = array(
  1980. 'type' => 'success',
  1981. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1982. 'msg' => array('mailbox_modified', htmlspecialchars(implode(', ', (array)$usernames)))
  1983. );
  1984. }
  1985. break;
  1986. case 'delimiter_action':
  1987. if (!is_array($_data['username'])) {
  1988. $usernames = array();
  1989. $usernames[] = $_data['username'];
  1990. }
  1991. else {
  1992. $usernames = $_data['username'];
  1993. }
  1994. if (!isset($_SESSION['acl']['delimiter_action']) || $_SESSION['acl']['delimiter_action'] != "1" ) {
  1995. $_SESSION['return'][] = array(
  1996. 'type' => 'danger',
  1997. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  1998. 'msg' => 'access_denied'
  1999. );
  2000. return false;
  2001. }
  2002. foreach ($usernames as $username) {
  2003. if (!filter_var($username, FILTER_VALIDATE_EMAIL) || !hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  2004. $_SESSION['return'][] = array(
  2005. 'type' => 'danger',
  2006. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2007. 'msg' => 'access_denied'
  2008. );
  2009. continue;
  2010. }
  2011. if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subject") {
  2012. try {
  2013. $redis->hSet('RCPT_WANTS_SUBJECT_TAG', $username, 1);
  2014. $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
  2015. }
  2016. catch (RedisException $e) {
  2017. $_SESSION['return'][] = array(
  2018. 'type' => 'danger',
  2019. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2020. 'msg' => array('redis_error', $e)
  2021. );
  2022. continue;
  2023. }
  2024. }
  2025. else if (isset($_data['tagged_mail_handler']) && $_data['tagged_mail_handler'] == "subfolder") {
  2026. try {
  2027. $redis->hSet('RCPT_WANTS_SUBFOLDER_TAG', $username, 1);
  2028. $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
  2029. }
  2030. catch (RedisException $e) {
  2031. $_SESSION['return'][] = array(
  2032. 'type' => 'danger',
  2033. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2034. 'msg' => array('redis_error', $e)
  2035. );
  2036. continue;
  2037. }
  2038. }
  2039. else {
  2040. try {
  2041. $redis->hDel('RCPT_WANTS_SUBJECT_TAG', $username);
  2042. $redis->hDel('RCPT_WANTS_SUBFOLDER_TAG', $username);
  2043. }
  2044. catch (RedisException $e) {
  2045. $_SESSION['return'][] = array(
  2046. 'type' => 'danger',
  2047. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2048. 'msg' => array('redis_error', $e)
  2049. );
  2050. continue;
  2051. }
  2052. }
  2053. $_SESSION['return'][] = array(
  2054. 'type' => 'success',
  2055. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2056. 'msg' => array('mailbox_modified', $username)
  2057. );
  2058. }
  2059. break;
  2060. case 'syncjob':
  2061. if (!is_array($_data['id'])) {
  2062. $ids = array();
  2063. $ids[] = $_data['id'];
  2064. }
  2065. else {
  2066. $ids = $_data['id'];
  2067. }
  2068. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  2069. $_SESSION['return'][] = array(
  2070. 'type' => 'danger',
  2071. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2072. 'msg' => 'access_denied'
  2073. );
  2074. return false;
  2075. }
  2076. foreach ($ids as $id) {
  2077. $is_now = mailbox('get', 'syncjob_details', $id, array('with_password'));
  2078. if (!empty($is_now)) {
  2079. $username = $is_now['user2'];
  2080. $user1 = (!empty($_data['user1'])) ? $_data['user1'] : $is_now['user1'];
  2081. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2082. $last_run = (isset($_data['last_run'])) ? NULL : $is_now['last_run'];
  2083. $success = (isset($_data['success'])) ? NULL : $is_now['success'];
  2084. $delete2duplicates = (isset($_data['delete2duplicates'])) ? intval($_data['delete2duplicates']) : $is_now['delete2duplicates'];
  2085. $subscribeall = (isset($_data['subscribeall'])) ? intval($_data['subscribeall']) : $is_now['subscribeall'];
  2086. $dry = (isset($_data['dry'])) ? intval($_data['dry']) : $is_now['dry'];
  2087. $delete1 = (isset($_data['delete1'])) ? intval($_data['delete1']) : $is_now['delete1'];
  2088. $delete2 = (isset($_data['delete2'])) ? intval($_data['delete2']) : $is_now['delete2'];
  2089. $automap = (isset($_data['automap'])) ? intval($_data['automap']) : $is_now['automap'];
  2090. $skipcrossduplicates = (isset($_data['skipcrossduplicates'])) ? intval($_data['skipcrossduplicates']) : $is_now['skipcrossduplicates'];
  2091. $port1 = (!empty($_data['port1'])) ? $_data['port1'] : $is_now['port1'];
  2092. $password1 = (!empty($_data['password1'])) ? $_data['password1'] : $is_now['password1'];
  2093. $host1 = (!empty($_data['host1'])) ? $_data['host1'] : $is_now['host1'];
  2094. $subfolder2 = (isset($_data['subfolder2'])) ? $_data['subfolder2'] : $is_now['subfolder2'];
  2095. $enc1 = (!empty($_data['enc1'])) ? $_data['enc1'] : $is_now['enc1'];
  2096. $mins_interval = (!empty($_data['mins_interval'])) ? $_data['mins_interval'] : $is_now['mins_interval'];
  2097. $exclude = (isset($_data['exclude'])) ? $_data['exclude'] : $is_now['exclude'];
  2098. $custom_params = (isset($_data['custom_params'])) ? $_data['custom_params'] : $is_now['custom_params'];
  2099. $maxage = (isset($_data['maxage']) && $_data['maxage'] != "") ? intval($_data['maxage']) : $is_now['maxage'];
  2100. $maxbytespersecond = (isset($_data['maxbytespersecond']) && $_data['maxbytespersecond'] != "") ? intval($_data['maxbytespersecond']) : $is_now['maxbytespersecond'];
  2101. $timeout1 = (isset($_data['timeout1']) && $_data['timeout1'] != "") ? intval($_data['timeout1']) : $is_now['timeout1'];
  2102. $timeout2 = (isset($_data['timeout2']) && $_data['timeout2'] != "") ? intval($_data['timeout2']) : $is_now['timeout2'];
  2103. }
  2104. else {
  2105. $_SESSION['return'][] = array(
  2106. 'type' => 'danger',
  2107. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2108. 'msg' => 'access_denied'
  2109. );
  2110. continue;
  2111. }
  2112. // validate custom params
  2113. foreach (explode('-', $custom_params) as $param){
  2114. if(empty($param)) continue;
  2115. // extract option
  2116. if (str_contains($param, '=')) $param = explode('=', $param)[0];
  2117. else $param = rtrim($param, ' ');
  2118. // remove first char if first char is -
  2119. if ($param[0] == '-') $param = ltrim($param, $param[0]);
  2120. if (str_contains($param, ' ')) {
  2121. // bad char
  2122. $_SESSION['return'][] = array(
  2123. 'type' => 'danger',
  2124. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2125. 'msg' => 'bad character SPACE'
  2126. );
  2127. return false;
  2128. }
  2129. // check if param is whitelisted
  2130. if (!in_array(strtolower($param), $GLOBALS["IMAPSYNC_OPTIONS"]["whitelist"])){
  2131. // bad option
  2132. $_SESSION['return'][] = array(
  2133. 'type' => 'danger',
  2134. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2135. 'msg' => 'bad option '. $param
  2136. );
  2137. return false;
  2138. }
  2139. }
  2140. if (empty($subfolder2)) {
  2141. $subfolder2 = "";
  2142. }
  2143. if (!isset($maxage) || !filter_var($maxage, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  2144. $maxage = "0";
  2145. }
  2146. if (!isset($timeout1) || !filter_var($timeout1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  2147. $timeout1 = "600";
  2148. }
  2149. if (!isset($timeout2) || !filter_var($timeout2, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 32000)))) {
  2150. $timeout2 = "600";
  2151. }
  2152. if (!isset($maxbytespersecond) || !filter_var($maxbytespersecond, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 125000000)))) {
  2153. $maxbytespersecond = "0";
  2154. }
  2155. if (!filter_var($port1, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 65535)))) {
  2156. $_SESSION['return'][] = array(
  2157. 'type' => 'danger',
  2158. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2159. 'msg' => 'access_denied'
  2160. );
  2161. continue;
  2162. }
  2163. if (!filter_var($mins_interval, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 43800)))) {
  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 (!is_valid_domain_name($host1)) {
  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 ($enc1 != "TLS" && $enc1 != "SSL" && $enc1 != "PLAIN") {
  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 (@preg_match("/" . $exclude . "/", null) === false) {
  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. $stmt = $pdo->prepare("UPDATE `imapsync` SET `delete1` = :delete1,
  2196. `delete2` = :delete2,
  2197. `automap` = :automap,
  2198. `skipcrossduplicates` = :skipcrossduplicates,
  2199. `maxage` = :maxage,
  2200. `maxbytespersecond` = :maxbytespersecond,
  2201. `subfolder2` = :subfolder2,
  2202. `exclude` = :exclude,
  2203. `host1` = :host1,
  2204. `last_run` = :last_run,
  2205. `success` = :success,
  2206. `user1` = :user1,
  2207. `password1` = :password1,
  2208. `mins_interval` = :mins_interval,
  2209. `port1` = :port1,
  2210. `enc1` = :enc1,
  2211. `delete2duplicates` = :delete2duplicates,
  2212. `custom_params` = :custom_params,
  2213. `timeout1` = :timeout1,
  2214. `timeout2` = :timeout2,
  2215. `subscribeall` = :subscribeall,
  2216. `dry` = :dry,
  2217. `active` = :active
  2218. WHERE `id` = :id");
  2219. $stmt->execute(array(
  2220. ':delete1' => $delete1,
  2221. ':delete2' => $delete2,
  2222. ':automap' => $automap,
  2223. ':skipcrossduplicates' => $skipcrossduplicates,
  2224. ':id' => $id,
  2225. ':exclude' => $exclude,
  2226. ':maxage' => $maxage,
  2227. ':maxbytespersecond' => $maxbytespersecond,
  2228. ':subfolder2' => $subfolder2,
  2229. ':host1' => $host1,
  2230. ':user1' => $user1,
  2231. ':password1' => $password1,
  2232. ':last_run' => $last_run,
  2233. ':success' => $success,
  2234. ':mins_interval' => $mins_interval,
  2235. ':port1' => $port1,
  2236. ':enc1' => $enc1,
  2237. ':delete2duplicates' => $delete2duplicates,
  2238. ':custom_params' => $custom_params,
  2239. ':timeout1' => $timeout1,
  2240. ':timeout2' => $timeout2,
  2241. ':subscribeall' => $subscribeall,
  2242. ':dry' => $dry,
  2243. ':active' => $active,
  2244. ));
  2245. $_SESSION['return'][] = array(
  2246. 'type' => 'success',
  2247. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2248. 'msg' => array('mailbox_modified', $username)
  2249. );
  2250. }
  2251. break;
  2252. case 'filter':
  2253. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  2254. $_SESSION['return'][] = array(
  2255. 'type' => 'danger',
  2256. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2257. 'msg' => 'access_denied'
  2258. );
  2259. return false;
  2260. }
  2261. $sieve = new Sieve\SieveParser();
  2262. if (!is_array($_data['id'])) {
  2263. $ids = array();
  2264. $ids[] = $_data['id'];
  2265. }
  2266. else {
  2267. $ids = $_data['id'];
  2268. }
  2269. foreach ($ids as $id) {
  2270. $is_now = mailbox('get', 'filter_details', $id);
  2271. if (!empty($is_now)) {
  2272. $username = $is_now['username'];
  2273. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2274. $script_desc = (!empty($_data['script_desc'])) ? $_data['script_desc'] : $is_now['script_desc'];
  2275. $script_data = (!empty($_data['script_data'])) ? $_data['script_data'] : $is_now['script_data'];
  2276. $filter_type = (!empty($_data['filter_type'])) ? $_data['filter_type'] : $is_now['filter_type'];
  2277. }
  2278. else {
  2279. $_SESSION['return'][] = array(
  2280. 'type' => 'danger',
  2281. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2282. 'msg' => 'access_denied'
  2283. );
  2284. continue;
  2285. }
  2286. try {
  2287. $sieve->parse($script_data);
  2288. }
  2289. catch (Exception $e) {
  2290. $_SESSION['return'][] = array(
  2291. 'type' => 'danger',
  2292. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2293. 'msg' => array('sieve_error', $e->getMessage())
  2294. );
  2295. continue;
  2296. }
  2297. if ($filter_type != 'postfilter' && $filter_type != 'prefilter') {
  2298. $_SESSION['return'][] = array(
  2299. 'type' => 'danger',
  2300. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2301. 'msg' => 'filter_type'
  2302. );
  2303. continue;
  2304. }
  2305. if ($active == '1') {
  2306. $script_name = 'active';
  2307. $stmt = $pdo->prepare("UPDATE `sieve_filters`
  2308. SET `script_name` = 'inactive'
  2309. WHERE `username` = :username
  2310. AND `filter_type` = :filter_type");
  2311. $stmt->execute(array(
  2312. ':username' => $username,
  2313. ':filter_type' => $filter_type
  2314. ));
  2315. }
  2316. else {
  2317. $script_name = 'inactive';
  2318. }
  2319. $stmt = $pdo->prepare("UPDATE `sieve_filters` SET `script_desc` = :script_desc, `script_data` = :script_data, `script_name` = :script_name, `filter_type` = :filter_type
  2320. WHERE `id` = :id");
  2321. $stmt->execute(array(
  2322. ':script_desc' => $script_desc,
  2323. ':script_data' => $script_data,
  2324. ':script_name' => $script_name,
  2325. ':filter_type' => $filter_type,
  2326. ':id' => $id
  2327. ));
  2328. $_SESSION['return'][] = array(
  2329. 'type' => 'success',
  2330. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2331. 'msg' => array('mailbox_modified', $username)
  2332. );
  2333. }
  2334. break;
  2335. case 'alias':
  2336. if (!is_array($_data['id'])) {
  2337. $ids = array();
  2338. $ids[] = $_data['id'];
  2339. }
  2340. else {
  2341. $ids = $_data['id'];
  2342. }
  2343. foreach ($ids as $id) {
  2344. $is_now = mailbox('get', 'alias_details', $id);
  2345. if (!empty($is_now)) {
  2346. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2347. $sogo_visible = (isset($_data['sogo_visible'])) ? intval($_data['sogo_visible']) : $is_now['sogo_visible'];
  2348. $goto_null = (isset($_data['goto_null'])) ? intval($_data['goto_null']) : 0;
  2349. $goto_spam = (isset($_data['goto_spam'])) ? intval($_data['goto_spam']) : 0;
  2350. $goto_ham = (isset($_data['goto_ham'])) ? intval($_data['goto_ham']) : 0;
  2351. $public_comment = (isset($_data['public_comment'])) ? $_data['public_comment'] : $is_now['public_comment'];
  2352. $private_comment = (isset($_data['private_comment'])) ? $_data['private_comment'] : $is_now['private_comment'];
  2353. $goto = (!empty($_data['goto'])) ? $_data['goto'] : $is_now['goto'];
  2354. $address = (!empty($_data['address'])) ? $_data['address'] : $is_now['address'];
  2355. }
  2356. else {
  2357. $_SESSION['return'][] = array(
  2358. 'type' => 'danger',
  2359. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2360. 'msg' => array('alias_invalid', $address)
  2361. );
  2362. continue;
  2363. }
  2364. if ($_data['expand_alias'] === true || $_data['expand_alias'] == 1) {
  2365. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  2366. WHERE `address` = :address
  2367. AND `domain` NOT IN (
  2368. SELECT `alias_domain` FROM `alias_domain`
  2369. )");
  2370. $stmt->execute(array(
  2371. ':address' => $address,
  2372. ));
  2373. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  2374. if ($num_results == 0) {
  2375. $_SESSION['return'][] = array(
  2376. 'type' => 'warning',
  2377. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2378. 'msg' => array('is_not_primary_alias', htmlspecialchars($address))
  2379. );
  2380. continue;
  2381. }
  2382. $stmt = $pdo->prepare("SELECT `goto`, GROUP_CONCAT(CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`)) AS `missing_alias`
  2383. FROM `alias` JOIN `alias_domain` ON `alias_domain`.`target_domain` = `alias`.`domain`
  2384. WHERE CONCAT(SUBSTRING(`alias`.`address`, 1, LOCATE('@', `alias`.`address`) - 1), '@', `alias_domain`.`alias_domain`) NOT IN (
  2385. SELECT `address` FROM `alias` WHERE `address` != `goto`
  2386. )
  2387. AND `alias`.`address` NOT IN (
  2388. SELECT `address` FROM `alias` WHERE `address` = `goto`
  2389. )
  2390. AND `address` = :address ;");
  2391. $stmt->execute(array(
  2392. ':address' => $address
  2393. ));
  2394. $missing_aliases = $stmt->fetch(PDO::FETCH_ASSOC);
  2395. if (!empty($missing_aliases['missing_alias'])) {
  2396. mailbox('add', 'alias', array(
  2397. 'address' => $missing_aliases['missing_alias'],
  2398. 'goto' => $missing_aliases['goto'],
  2399. 'sogo_visible' => 1,
  2400. 'active' => 1
  2401. ));
  2402. }
  2403. $_SESSION['return'][] = array(
  2404. 'type' => 'success',
  2405. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2406. 'msg' => array('alias_modified', htmlspecialchars($address))
  2407. );
  2408. continue;
  2409. }
  2410. $domain = idn_to_ascii(substr(strstr($address, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  2411. if ($is_now['address'] != $address) {
  2412. $local_part = strstr($address, '@', true);
  2413. $address = $local_part.'@'.$domain;
  2414. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2415. $_SESSION['return'][] = array(
  2416. 'type' => 'danger',
  2417. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2418. 'msg' => 'access_denied'
  2419. );
  2420. continue;
  2421. }
  2422. if ((!filter_var($address, FILTER_VALIDATE_EMAIL) === true) && !empty($local_part)) {
  2423. $_SESSION['return'][] = array(
  2424. 'type' => 'danger',
  2425. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2426. 'msg' => array('alias_invalid', $address)
  2427. );
  2428. continue;
  2429. }
  2430. if (strtolower($is_now['address']) != strtolower($address)) {
  2431. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  2432. WHERE `address`= :address OR `address` IN (
  2433. SELECT `username` FROM `mailbox`, `alias_domain`
  2434. WHERE (
  2435. `alias_domain`.`alias_domain` = :address_d
  2436. AND `mailbox`.`username` = CONCAT(:address_l, '@', alias_domain.target_domain)))");
  2437. $stmt->execute(array(
  2438. ':address' => $address,
  2439. ':address_l' => $local_part,
  2440. ':address_d' => $domain
  2441. ));
  2442. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  2443. if ($num_results != 0) {
  2444. $_SESSION['return'][] = array(
  2445. 'type' => 'danger',
  2446. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2447. 'msg' => array('is_alias_or_mailbox', htmlspecialchars($address))
  2448. );
  2449. continue;
  2450. }
  2451. }
  2452. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  2453. WHERE `domain`= :domain1 OR `domain` = (SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain2)");
  2454. $stmt->execute(array(':domain1' => $domain, ':domain2' => $domain));
  2455. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  2456. if ($num_results == 0) {
  2457. $_SESSION['return'][] = array(
  2458. 'type' => 'danger',
  2459. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2460. 'msg' => array('domain_not_found', htmlspecialchars($domain))
  2461. );
  2462. continue;
  2463. }
  2464. $stmt = $pdo->prepare("SELECT `address` FROM `spamalias`
  2465. WHERE `address`= :address");
  2466. $stmt->execute(array(':address' => $address));
  2467. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  2468. if ($num_results != 0) {
  2469. $_SESSION['return'][] = array(
  2470. 'type' => 'danger',
  2471. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2472. 'msg' => array('is_spam_alias', htmlspecialchars($address))
  2473. );
  2474. continue;
  2475. }
  2476. }
  2477. if ($goto_null == "1") {
  2478. $goto = "null@localhost";
  2479. }
  2480. elseif ($goto_spam == "1") {
  2481. $goto = "spam@localhost";
  2482. }
  2483. elseif ($goto_ham == "1") {
  2484. $goto = "ham@localhost";
  2485. }
  2486. else {
  2487. $gotos = array_map('trim', preg_split( "/( |,|;|\n)/", $goto));
  2488. foreach ($gotos as $i => &$goto) {
  2489. if (empty($goto)) {
  2490. continue;
  2491. }
  2492. if (!filter_var($goto, FILTER_VALIDATE_EMAIL)) {
  2493. $_SESSION['return'][] = array(
  2494. 'type' => 'danger',
  2495. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2496. 'msg' => array('goto_invalid', $goto)
  2497. );
  2498. unset($gotos[$i]);
  2499. continue;
  2500. }
  2501. if ($goto == $address) {
  2502. $_SESSION['return'][] = array(
  2503. 'type' => 'danger',
  2504. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2505. 'msg' => 'alias_goto_identical'
  2506. );
  2507. unset($gotos[$i]);
  2508. continue;
  2509. }
  2510. // Delete from sender_acl to prevent duplicates
  2511. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE
  2512. `logged_in_as` = :goto AND
  2513. `send_as` = :address");
  2514. $stmt->execute(array(
  2515. ':goto' => $goto,
  2516. ':address' => $address
  2517. ));
  2518. }
  2519. $gotos = array_unique($gotos);
  2520. $gotos = array_filter($gotos);
  2521. $goto = implode(",", (array)$gotos);
  2522. }
  2523. if (!empty($goto)) {
  2524. $stmt = $pdo->prepare("UPDATE `alias` SET
  2525. `address` = :address,
  2526. `public_comment` = :public_comment,
  2527. `private_comment` = :private_comment,
  2528. `domain` = :domain,
  2529. `goto` = :goto,
  2530. `sogo_visible`= :sogo_visible,
  2531. `active`= :active
  2532. WHERE `id` = :id");
  2533. $stmt->execute(array(
  2534. ':address' => $address,
  2535. ':public_comment' => $public_comment,
  2536. ':private_comment' => $private_comment,
  2537. ':domain' => $domain,
  2538. ':goto' => $goto,
  2539. ':sogo_visible' => $sogo_visible,
  2540. ':active' => $active,
  2541. ':id' => $is_now['id']
  2542. ));
  2543. }
  2544. $_SESSION['return'][] = array(
  2545. 'type' => 'success',
  2546. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2547. 'msg' => array('alias_modified', htmlspecialchars($address))
  2548. );
  2549. }
  2550. break;
  2551. case 'domain':
  2552. if (!is_array($_data['domain'])) {
  2553. $domains = array();
  2554. $domains[] = $_data['domain'];
  2555. }
  2556. else {
  2557. $domains = $_data['domain'];
  2558. }
  2559. foreach ($domains as $domain) {
  2560. $domain = idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
  2561. if (!is_valid_domain_name($domain)) {
  2562. $_SESSION['return'][] = array(
  2563. 'type' => 'danger',
  2564. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2565. 'msg' => 'domain_invalid'
  2566. );
  2567. continue;
  2568. }
  2569. if ($_SESSION['mailcow_cc_role'] == "domainadmin" &&
  2570. hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2571. $is_now = mailbox('get', 'domain_details', $domain);
  2572. if (!empty($is_now)) {
  2573. $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
  2574. $description = (!empty($_data['description']) && isset($_SESSION['acl']['domain_desc']) && $_SESSION['acl']['domain_desc'] == "1") ? $_data['description'] : $is_now['description'];
  2575. (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['domain_relayhost']) && $_SESSION['acl']['domain_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['relayhost']);
  2576. $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
  2577. }
  2578. else {
  2579. $_SESSION['return'][] = array(
  2580. 'type' => 'danger',
  2581. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2582. 'msg' => 'domain_invalid'
  2583. );
  2584. continue;
  2585. }
  2586. $stmt = $pdo->prepare("UPDATE `domain` SET
  2587. `description` = :description,
  2588. `gal` = :gal
  2589. WHERE `domain` = :domain");
  2590. $stmt->execute(array(
  2591. ':description' => $description,
  2592. ':gal' => $gal,
  2593. ':domain' => $domain
  2594. ));
  2595. // save tags
  2596. foreach($tags as $index => $tag){
  2597. if (empty($tag)) continue;
  2598. if ($index > $GLOBALS['TAGGING_LIMIT']) {
  2599. $_SESSION['return'][] = array(
  2600. 'type' => 'warning',
  2601. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2602. 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
  2603. );
  2604. break;
  2605. }
  2606. $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
  2607. $stmt->execute(array(
  2608. ':domain' => $domain,
  2609. ':tag_name' => $tag,
  2610. ));
  2611. }
  2612. $_SESSION['return'][] = array(
  2613. 'type' => 'success',
  2614. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2615. 'msg' => array('domain_modified', htmlspecialchars($domain))
  2616. );
  2617. }
  2618. elseif ($_SESSION['mailcow_cc_role'] == "admin") {
  2619. $is_now = mailbox('get', 'domain_details', $domain);
  2620. if (!empty($is_now)) {
  2621. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2622. $backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx'];
  2623. $gal = (isset($_data['gal'])) ? intval($_data['gal']) : $is_now['gal'];
  2624. $relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients'];
  2625. $relay_unknown_only = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : $is_now['relay_unknown_only'];
  2626. $relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
  2627. $aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
  2628. $mailboxes = (isset($_data['mailboxes']) && $_data['mailboxes'] != '') ? intval($_data['mailboxes']) : $is_now['max_num_mboxes_for_domain'];
  2629. $defquota = (isset($_data['defquota']) && $_data['defquota'] != '') ? intval($_data['defquota']) : ($is_now['def_quota_for_mbox'] / 1048576);
  2630. $maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
  2631. $quota = (!empty($_data['quota'])) ? $_data['quota'] : ($is_now['max_quota_for_domain'] / 1048576);
  2632. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  2633. $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
  2634. if ($relay_all_recipients == '1') {
  2635. $backupmx = '1';
  2636. }
  2637. if ($relay_unknown_only == '1') {
  2638. $backupmx = '1';
  2639. $relay_all_recipients = '1';
  2640. }
  2641. }
  2642. else {
  2643. $_SESSION['return'][] = array(
  2644. 'type' => 'danger',
  2645. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2646. 'msg' => 'domain_invalid'
  2647. );
  2648. continue;
  2649. }
  2650. // todo: should be using api here
  2651. $stmt = $pdo->prepare("SELECT
  2652. COUNT(*) AS count,
  2653. MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
  2654. COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
  2655. FROM `mailbox`
  2656. WHERE (`kind` = '' OR `kind` = NULL)
  2657. AND domain = :domain");
  2658. $stmt->execute(array(':domain' => $domain));
  2659. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  2660. // todo: should be using api here
  2661. $stmt = $pdo->prepare("SELECT COUNT(*) AS `count` FROM `alias`
  2662. WHERE domain = :domain
  2663. AND address NOT IN (
  2664. SELECT `username` FROM `mailbox`
  2665. )");
  2666. $stmt->execute(array(':domain' => $domain));
  2667. $AliasData = $stmt->fetch(PDO::FETCH_ASSOC);
  2668. if ($defquota > $maxquota) {
  2669. $_SESSION['return'][] = array(
  2670. 'type' => 'danger',
  2671. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2672. 'msg' => 'mailbox_defquota_exceeds_mailbox_maxquota'
  2673. );
  2674. continue;
  2675. }
  2676. if ($defquota == "0" || empty($defquota)) {
  2677. $_SESSION['return'][] = array(
  2678. 'type' => 'danger',
  2679. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2680. 'msg' => 'defquota_empty'
  2681. );
  2682. continue;
  2683. }
  2684. if ($maxquota > $quota) {
  2685. $_SESSION['return'][] = array(
  2686. 'type' => 'danger',
  2687. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2688. 'msg' => 'mailbox_quota_exceeds_domain_quota'
  2689. );
  2690. continue;
  2691. }
  2692. if ($maxquota == "0" || empty($maxquota)) {
  2693. $_SESSION['return'][] = array(
  2694. 'type' => 'danger',
  2695. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2696. 'msg' => 'maxquota_empty'
  2697. );
  2698. continue;
  2699. }
  2700. if ($MailboxData['biggest_mailbox'] > $maxquota) {
  2701. $_SESSION['return'][] = array(
  2702. 'type' => 'danger',
  2703. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2704. 'msg' => array('max_quota_in_use', $MailboxData['biggest_mailbox'])
  2705. );
  2706. continue;
  2707. }
  2708. if ($MailboxData['quota_all'] > $quota) {
  2709. $_SESSION['return'][] = array(
  2710. 'type' => 'danger',
  2711. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2712. 'msg' => array('domain_quota_m_in_use', $MailboxData['quota_all'])
  2713. );
  2714. continue;
  2715. }
  2716. if ($MailboxData['count'] > $mailboxes) {
  2717. $_SESSION['return'][] = array(
  2718. 'type' => 'danger',
  2719. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2720. 'msg' => array('mailboxes_in_use', $MailboxData['count'])
  2721. );
  2722. continue;
  2723. }
  2724. if ($AliasData['count'] > $aliases) {
  2725. $_SESSION['return'][] = array(
  2726. 'type' => 'danger',
  2727. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2728. 'msg' => array('aliases_in_use', $AliasData['count'])
  2729. );
  2730. continue;
  2731. }
  2732. $stmt = $pdo->prepare("UPDATE `domain` SET
  2733. `relay_all_recipients` = :relay_all_recipients,
  2734. `relay_unknown_only` = :relay_unknown_only,
  2735. `backupmx` = :backupmx,
  2736. `gal` = :gal,
  2737. `active` = :active,
  2738. `quota` = :quota,
  2739. `defquota` = :defquota,
  2740. `maxquota` = :maxquota,
  2741. `relayhost` = :relayhost,
  2742. `mailboxes` = :mailboxes,
  2743. `aliases` = :aliases,
  2744. `description` = :description
  2745. WHERE `domain` = :domain");
  2746. $stmt->execute(array(
  2747. ':relay_all_recipients' => $relay_all_recipients,
  2748. ':relay_unknown_only' => $relay_unknown_only,
  2749. ':backupmx' => $backupmx,
  2750. ':gal' => $gal,
  2751. ':active' => $active,
  2752. ':quota' => $quota,
  2753. ':defquota' => $defquota,
  2754. ':maxquota' => $maxquota,
  2755. ':relayhost' => $relayhost,
  2756. ':mailboxes' => $mailboxes,
  2757. ':aliases' => $aliases,
  2758. ':description' => $description,
  2759. ':domain' => $domain
  2760. ));
  2761. // save tags
  2762. foreach($tags as $index => $tag){
  2763. if (empty($tag)) continue;
  2764. if ($index > $GLOBALS['TAGGING_LIMIT']) {
  2765. $_SESSION['return'][] = array(
  2766. 'type' => 'warning',
  2767. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2768. 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
  2769. );
  2770. break;
  2771. }
  2772. $stmt = $pdo->prepare("INSERT INTO `tags_domain` (`domain`, `tag_name`) VALUES (:domain, :tag_name)");
  2773. $stmt->execute(array(
  2774. ':domain' => $domain,
  2775. ':tag_name' => $tag,
  2776. ));
  2777. }
  2778. $_SESSION['return'][] = array(
  2779. 'type' => 'success',
  2780. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2781. 'msg' => array('domain_modified', htmlspecialchars($domain))
  2782. );
  2783. }
  2784. }
  2785. break;
  2786. case 'domain_templates':
  2787. if ($_SESSION['mailcow_cc_role'] != "admin") {
  2788. $_SESSION['return'][] = array(
  2789. 'type' => 'danger',
  2790. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  2791. 'msg' => 'access_denied'
  2792. );
  2793. return false;
  2794. }
  2795. if (!is_array($_data['ids'])) {
  2796. $ids = array();
  2797. $ids[] = $_data['ids'];
  2798. }
  2799. else {
  2800. $ids = $_data['ids'];
  2801. }
  2802. foreach ($ids as $id) {
  2803. $is_now = mailbox("get", "domain_templates", $id);
  2804. if (empty($is_now) ||
  2805. $is_now["type"] != "domain"){
  2806. $_SESSION['return'][] = array(
  2807. 'type' => 'danger',
  2808. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  2809. 'msg' => 'template_id_invalid'
  2810. );
  2811. continue;
  2812. }
  2813. // check name
  2814. if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
  2815. // keep template name of Default template
  2816. $_data["template"] = $is_now["template"];
  2817. }
  2818. else {
  2819. $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
  2820. }
  2821. // check attributes
  2822. $attr = array();
  2823. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : array();
  2824. $attr['max_num_aliases_for_domain'] = (isset($_data['max_num_aliases_for_domain'])) ? intval($_data['max_num_aliases_for_domain']) : 0;
  2825. $attr['max_num_mboxes_for_domain'] = (isset($_data['max_num_mboxes_for_domain'])) ? intval($_data['max_num_mboxes_for_domain']) : 0;
  2826. $attr['def_quota_for_mbox'] = (isset($_data['def_quota_for_mbox'])) ? intval($_data['def_quota_for_mbox']) * 1048576 : 0;
  2827. $attr['max_quota_for_mbox'] = (isset($_data['max_quota_for_mbox'])) ? intval($_data['max_quota_for_mbox']) * 1048576 : 0;
  2828. $attr['max_quota_for_domain'] = (isset($_data['max_quota_for_domain'])) ? intval($_data['max_quota_for_domain']) * 1048576 : 0;
  2829. $attr['rl_frame'] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : "s";
  2830. $attr['rl_value'] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : "";
  2831. $attr['active'] = isset($_data['active']) ? intval($_data['active']) : 1;
  2832. $attr['gal'] = (isset($_data['gal'])) ? intval($_data['gal']) : 1;
  2833. $attr['backupmx'] = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : 0;
  2834. $attr['relay_all_recipients'] = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : 0;
  2835. $attr['relay_unknown_only'] = (isset($_data['relay_unknown_only'])) ? intval($_data['relay_unknown_only']) : 0;
  2836. $attr['dkim_selector'] = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : "dkim";
  2837. $attr['key_size'] = isset($_data['key_size']) ? intval($_data['key_size']) : 2048;
  2838. // update template
  2839. $stmt = $pdo->prepare("UPDATE `templates`
  2840. SET `template` = :template, `attributes` = :attributes
  2841. WHERE id = :id");
  2842. $stmt->execute(array(
  2843. ":id" => $id ,
  2844. ":template" => $_data["template"] ,
  2845. ":attributes" => json_encode($attr)
  2846. ));
  2847. }
  2848. $_SESSION['return'][] = array(
  2849. 'type' => 'success',
  2850. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2851. 'msg' => array('template_modified', $_data["template"])
  2852. );
  2853. return true;
  2854. break;
  2855. case 'mailbox':
  2856. if (!is_array($_data['username'])) {
  2857. $usernames = array();
  2858. $usernames[] = $_data['username'];
  2859. }
  2860. else {
  2861. $usernames = $_data['username'];
  2862. }
  2863. foreach ($usernames as $username) {
  2864. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  2865. $_SESSION['return'][] = array(
  2866. 'type' => 'danger',
  2867. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2868. 'msg' => array('username_invalid', $username)
  2869. );
  2870. continue;
  2871. }
  2872. $is_now = mailbox('get', 'mailbox_details', $username);
  2873. if (isset($_data['protocol_access'])) {
  2874. $_data['protocol_access'] = (array)$_data['protocol_access'];
  2875. $_data['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  2876. $_data['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  2877. $_data['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  2878. $_data['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
  2879. }
  2880. if (!empty($is_now)) {
  2881. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  2882. (int)$force_pw_update = (isset($_data['force_pw_update'])) ? intval($_data['force_pw_update']) : intval($is_now['attributes']['force_pw_update']);
  2883. (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']);
  2884. (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']);
  2885. (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']);
  2886. (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']);
  2887. (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']);
  2888. (int)$relayhost = (isset($_data['relayhost']) && isset($_SESSION['acl']['mailbox_relayhost']) && $_SESSION['acl']['mailbox_relayhost'] == "1") ? intval($_data['relayhost']) : intval($is_now['attributes']['relayhost']);
  2889. (int)$quota_m = (isset_has_content($_data['quota'])) ? intval($_data['quota']) : ($is_now['quota'] / 1048576);
  2890. $name = (!empty($_data['name'])) ? ltrim(rtrim($_data['name'], '>'), '<') : $is_now['name'];
  2891. $domain = $is_now['domain'];
  2892. $quota_b = $quota_m * 1048576;
  2893. $password = (!empty($_data['password'])) ? $_data['password'] : null;
  2894. $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
  2895. $tags = (is_array($_data['tags']) ? $_data['tags'] : array());
  2896. $attribute_hash = (!empty($_data['attribute_hash'])) ? $_data['attribute_hash'] : '';
  2897. $authsource = $is_now['authsource'];
  2898. if (in_array($_data['authsource'], array('mailcow', 'keycloak', 'generic-oidc'))){
  2899. $authsource = $_data['authsource'];
  2900. }
  2901. }
  2902. else {
  2903. $_SESSION['return'][] = array(
  2904. 'type' => 'danger',
  2905. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2906. 'msg' => 'access_denied'
  2907. );
  2908. continue;
  2909. }
  2910. // if already 0 == ok
  2911. if ((!isset($_SESSION['acl']['unlimited_quota']) || $_SESSION['acl']['unlimited_quota'] != "1") && ($quota_m == 0 && $is_now['quota'] != 0)) {
  2912. $_SESSION['return'][] = array(
  2913. 'type' => 'danger',
  2914. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2915. 'msg' => 'unlimited_quota_acl'
  2916. );
  2917. return false;
  2918. }
  2919. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  2920. $_SESSION['return'][] = array(
  2921. 'type' => 'danger',
  2922. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2923. 'msg' => 'access_denied'
  2924. );
  2925. continue;
  2926. }
  2927. $DomainData = mailbox('get', 'domain_details', $domain);
  2928. if ($quota_m > ($is_now['max_new_quota'] / 1048576)) {
  2929. $_SESSION['return'][] = array(
  2930. 'type' => 'danger',
  2931. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2932. 'msg' => array('mailbox_quota_left_exceeded', ($is_now['max_new_quota'] / 1048576))
  2933. );
  2934. continue;
  2935. }
  2936. if ($quota_m > $DomainData['max_quota_for_mbox']) {
  2937. $_SESSION['return'][] = array(
  2938. 'type' => 'danger',
  2939. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2940. 'msg' => array('mailbox_quota_exceeded', $DomainData['max_quota_for_mbox'])
  2941. );
  2942. continue;
  2943. }
  2944. $extra_acls = array();
  2945. if (isset($_data['extended_sender_acl'])) {
  2946. if (!isset($_SESSION['acl']['extend_sender_acl']) || $_SESSION['acl']['extend_sender_acl'] != "1" ) {
  2947. $_SESSION['return'][] = array(
  2948. 'type' => 'danger',
  2949. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2950. 'msg' => 'extended_sender_acl_denied'
  2951. );
  2952. }
  2953. else {
  2954. $extra_acls = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['extended_sender_acl']));
  2955. foreach ($extra_acls as $i => &$extra_acl) {
  2956. if (empty($extra_acl)) {
  2957. continue;
  2958. }
  2959. if (substr($extra_acl, 0, 1) === "@") {
  2960. $extra_acl = ltrim($extra_acl, '@');
  2961. }
  2962. if (!filter_var($extra_acl, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name($extra_acl)) {
  2963. $_SESSION['return'][] = array(
  2964. 'type' => 'danger',
  2965. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2966. 'msg' => array('extra_acl_invalid', htmlspecialchars($extra_acl))
  2967. );
  2968. unset($extra_acls[$i]);
  2969. continue;
  2970. }
  2971. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  2972. if (filter_var($extra_acl, FILTER_VALIDATE_EMAIL)) {
  2973. $extra_acl_domain = idn_to_ascii(substr(strstr($extra_acl, '@'), 1), 0, INTL_IDNA_VARIANT_UTS46);
  2974. if (in_array($extra_acl_domain, $domains)) {
  2975. $_SESSION['return'][] = array(
  2976. 'type' => 'danger',
  2977. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2978. 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
  2979. );
  2980. unset($extra_acls[$i]);
  2981. continue;
  2982. }
  2983. }
  2984. else {
  2985. if (in_array($extra_acl, $domains)) {
  2986. $_SESSION['return'][] = array(
  2987. 'type' => 'danger',
  2988. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  2989. 'msg' => array('extra_acl_invalid_domain', $extra_acl_domain)
  2990. );
  2991. unset($extra_acls[$i]);
  2992. continue;
  2993. }
  2994. $extra_acl = '@' . $extra_acl;
  2995. }
  2996. }
  2997. $extra_acls = array_filter($extra_acls);
  2998. $extra_acls = array_values($extra_acls);
  2999. $extra_acls = array_unique($extra_acls);
  3000. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 1 AND `logged_in_as` = :username");
  3001. $stmt->execute(array(
  3002. ':username' => $username
  3003. ));
  3004. foreach ($extra_acls as $sender_acl_external) {
  3005. $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`, `external`)
  3006. VALUES (:sender_acl, :username, 1)");
  3007. $stmt->execute(array(
  3008. ':sender_acl' => $sender_acl_external,
  3009. ':username' => $username
  3010. ));
  3011. }
  3012. }
  3013. }
  3014. if (isset($_data['sender_acl'])) {
  3015. // Get sender_acl items set by admin
  3016. $sender_acl_admin = array_merge(
  3017. mailbox('get', 'sender_acl_handles', $username)['sender_acl_domains']['ro'],
  3018. mailbox('get', 'sender_acl_handles', $username)['sender_acl_addresses']['ro']
  3019. );
  3020. // Get sender_acl items from POST array
  3021. // Set sender_acl_domain_admin to empty array if sender_acl contains "default" to trigger a reset
  3022. // Delete records from sender_acl if sender_acl contains "*" and set to array("*")
  3023. $_data['sender_acl'] = (array)$_data['sender_acl'];
  3024. if (in_array("*", $_data['sender_acl'])) {
  3025. $sender_acl_domain_admin = array('*');
  3026. }
  3027. elseif (array("default") === $_data['sender_acl']) {
  3028. $sender_acl_domain_admin = array();
  3029. }
  3030. else {
  3031. if (array_search('default', $_data['sender_acl']) !== false){
  3032. unset($_data['sender_acl'][array_search('default', $_data['sender_acl'])]);
  3033. }
  3034. $sender_acl_domain_admin = $_data['sender_acl'];
  3035. }
  3036. if (!empty($sender_acl_domain_admin) || !empty($sender_acl_admin)) {
  3037. // Check items in POST array and skip invalid
  3038. foreach ($sender_acl_domain_admin as $key => $val) {
  3039. // Check for invalid domain or email format or not *
  3040. if (!filter_var($val, FILTER_VALIDATE_EMAIL) && !is_valid_domain_name(ltrim($val, '@')) && $val != '*') {
  3041. $_SESSION['return'][] = array(
  3042. 'type' => 'danger',
  3043. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3044. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  3045. );
  3046. unset($sender_acl_domain_admin[$key]);
  3047. continue;
  3048. }
  3049. // Check if user has domain access (if object is domain)
  3050. $domain = ltrim($sender_acl_domain_admin[$key], '@');
  3051. if (is_valid_domain_name($domain)) {
  3052. // Check for- and skip non-mailcow domains
  3053. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  3054. if (!empty($domains)) {
  3055. if (!in_array($domain, $domains)) {
  3056. $_SESSION['return'][] = array(
  3057. 'type' => 'danger',
  3058. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3059. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  3060. );
  3061. unset($sender_acl_domain_admin[$key]);
  3062. continue;
  3063. }
  3064. }
  3065. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  3066. $_SESSION['return'][] = array(
  3067. 'type' => 'danger',
  3068. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3069. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  3070. );
  3071. unset($sender_acl_domain_admin[$key]);
  3072. continue;
  3073. }
  3074. }
  3075. // Wildcard can only be used if role == admin
  3076. if ($val == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  3077. $_SESSION['return'][] = array(
  3078. 'type' => 'danger',
  3079. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3080. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  3081. );
  3082. unset($sender_acl_domain_admin[$key]);
  3083. continue;
  3084. }
  3085. // Check if user has alias access (if object is email)
  3086. if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
  3087. if (!hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $val)) {
  3088. $_SESSION['return'][] = array(
  3089. 'type' => 'danger',
  3090. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3091. 'msg' => array('sender_acl_invalid', $sender_acl_domain_admin[$key])
  3092. );
  3093. unset($sender_acl_domain_admin[$key]);
  3094. continue;
  3095. }
  3096. }
  3097. }
  3098. // Merge both arrays
  3099. $sender_acl_merged = array_merge($sender_acl_domain_admin, $sender_acl_admin);
  3100. // If merged array still contains "*", set it as only value
  3101. !in_array('*', $sender_acl_merged) ?: $sender_acl_merged = array('*');
  3102. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
  3103. $stmt->execute(array(
  3104. ':username' => $username
  3105. ));
  3106. $fixed_sender_aliases = mailbox('get', 'sender_acl_handles', $username)['fixed_sender_aliases'];
  3107. foreach ($sender_acl_merged as $sender_acl) {
  3108. $domain = ltrim($sender_acl, '@');
  3109. if (is_valid_domain_name($domain)) {
  3110. $sender_acl = '@' . $domain;
  3111. }
  3112. // Don't add if allowed by alias
  3113. if (in_array($sender_acl, $fixed_sender_aliases)) {
  3114. continue;
  3115. }
  3116. $stmt = $pdo->prepare("INSERT INTO `sender_acl` (`send_as`, `logged_in_as`)
  3117. VALUES (:sender_acl, :username)");
  3118. $stmt->execute(array(
  3119. ':sender_acl' => $sender_acl,
  3120. ':username' => $username
  3121. ));
  3122. }
  3123. }
  3124. else {
  3125. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `external` = 0 AND `logged_in_as` = :username");
  3126. $stmt->execute(array(
  3127. ':username' => $username
  3128. ));
  3129. }
  3130. }
  3131. if (!empty($password)) {
  3132. if (password_check($password, $password2) !== true) {
  3133. continue;
  3134. }
  3135. $password_hashed = hash_password($password);
  3136. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  3137. `password` = :password_hashed,
  3138. `attributes` = JSON_SET(`attributes`, '$.passwd_update', NOW())
  3139. WHERE `username` = :username AND authsource = 'mailcow'");
  3140. $stmt->execute(array(
  3141. ':password_hashed' => $password_hashed,
  3142. ':username' => $username
  3143. ));
  3144. }
  3145. // 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)
  3146. $stmt = $pdo->prepare("UPDATE `alias` SET
  3147. `active` = :active
  3148. WHERE `address` = :address");
  3149. $stmt->execute(array(
  3150. ':address' => $username,
  3151. ':active' => $active
  3152. ));
  3153. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  3154. `active` = :active,
  3155. `name`= :name,
  3156. `quota` = :quota_b,
  3157. `authsource` = :authsource,
  3158. `attributes` = JSON_SET(`attributes`, '$.force_pw_update', :force_pw_update),
  3159. `attributes` = JSON_SET(`attributes`, '$.sogo_access', :sogo_access),
  3160. `attributes` = JSON_SET(`attributes`, '$.imap_access', :imap_access),
  3161. `attributes` = JSON_SET(`attributes`, '$.sieve_access', :sieve_access),
  3162. `attributes` = JSON_SET(`attributes`, '$.pop3_access', :pop3_access),
  3163. `attributes` = JSON_SET(`attributes`, '$.relayhost', :relayhost),
  3164. `attributes` = JSON_SET(`attributes`, '$.smtp_access', :smtp_access),
  3165. `attributes` = JSON_SET(`attributes`, '$.attribute_hash', :attribute_hash)
  3166. WHERE `username` = :username");
  3167. $stmt->execute(array(
  3168. ':active' => $active,
  3169. ':name' => $name,
  3170. ':quota_b' => $quota_b,
  3171. ':attribute_hash' => $attribute_hash,
  3172. ':force_pw_update' => $force_pw_update,
  3173. ':sogo_access' => $sogo_access,
  3174. ':imap_access' => $imap_access,
  3175. ':pop3_access' => $pop3_access,
  3176. ':sieve_access' => $sieve_access,
  3177. ':smtp_access' => $smtp_access,
  3178. ':relayhost' => $relayhost,
  3179. ':username' => $username,
  3180. ':authsource' => $authsource
  3181. ));
  3182. // save tags
  3183. foreach($tags as $index => $tag){
  3184. if (empty($tag)) continue;
  3185. if ($index > $GLOBALS['TAGGING_LIMIT']) {
  3186. $_SESSION['return'][] = array(
  3187. 'type' => 'warning',
  3188. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3189. 'msg' => array('tag_limit_exceeded', 'limit '.$GLOBALS['TAGGING_LIMIT'])
  3190. );
  3191. break;
  3192. }
  3193. try {
  3194. $stmt = $pdo->prepare("INSERT INTO `tags_mailbox` (`username`, `tag_name`) VALUES (:username, :tag_name)");
  3195. $stmt->execute(array(
  3196. ':username' => $username,
  3197. ':tag_name' => $tag,
  3198. ));
  3199. } catch (Exception $e) {
  3200. }
  3201. }
  3202. $_SESSION['return'][] = array(
  3203. 'type' => 'success',
  3204. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3205. 'msg' => array('mailbox_modified', $username)
  3206. );
  3207. update_sogo_static_view($username);
  3208. }
  3209. return true;
  3210. break;
  3211. case 'mailbox_from_template':
  3212. $stmt = $pdo->prepare("SELECT * FROM `templates`
  3213. WHERE `template` = :template AND type = 'mailbox'");
  3214. $stmt->execute(array(
  3215. ":template" => $_data['template']
  3216. ));
  3217. $mbox_template_data = $stmt->fetch(PDO::FETCH_ASSOC);
  3218. if (empty($mbox_template_data)){
  3219. $_SESSION['return'][] = array(
  3220. 'type' => 'danger',
  3221. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3222. 'msg' => 'template_missing'
  3223. );
  3224. return false;
  3225. }
  3226. $attribute_hash = sha1(json_encode($mbox_template_data["attributes"]));
  3227. $is_now = mailbox('get', 'mailbox_details', $_data['username']);
  3228. if ($is_now['attributes']['attribute_hash'] == $attribute_hash)
  3229. return true;
  3230. $mbox_template_data = json_decode($mbox_template_data["attributes"], true);
  3231. $mbox_template_data['attribute_hash'] = $attribute_hash;
  3232. $quarantine_attributes = array('username' => $_data['username']);
  3233. $tls_attributes = array('username' => $_data['username']);
  3234. $ratelimit_attributes = array('object' => $_data['username']);
  3235. $acl_attributes = array('username' => $_data['username'], 'user_acl' => array());
  3236. $mailbox_attributes = array('username' => $_data['username']);
  3237. foreach ($mbox_template_data as $key => $value){
  3238. switch (true) {
  3239. case (strpos($key, 'quarantine_') === 0):
  3240. $quarantine_attributes[$key] = $value;
  3241. break;
  3242. case (strpos($key, 'tls_') === 0):
  3243. if ($value == null)
  3244. $value = 0;
  3245. $tls_attributes[$key] = $value;
  3246. break;
  3247. case (strpos($key, 'rl_') === 0):
  3248. $ratelimit_attributes[$key] = $value;
  3249. break;
  3250. case (strpos($key, 'acl_') === 0 && $value != 0):
  3251. array_push($acl_attributes['user_acl'], str_replace('acl_' , '', $key));
  3252. break;
  3253. default:
  3254. $mailbox_attributes[$key] = $value;
  3255. break;
  3256. }
  3257. }
  3258. $mailbox_attributes['quota'] = intval($mailbox_attributes['quota'] / 1048576);
  3259. $result = mailbox('edit', 'mailbox', $mailbox_attributes);
  3260. if ($result === false) return $result;
  3261. $result = mailbox('edit', 'tls_policy', $tls_attributes);
  3262. if ($result === false) return $result;
  3263. $result = mailbox('edit', 'quarantine_notification', $quarantine_attributes);
  3264. if ($result === false) return $result;
  3265. $result = mailbox('edit', 'quarantine_category', $quarantine_attributes);
  3266. if ($result === false) return $result;
  3267. $result = ratelimit('edit', 'mailbox', $ratelimit_attributes);
  3268. if ($result === false) return $result;
  3269. $result = acl('edit', 'user', $acl_attributes);
  3270. if ($result === false) return $result;
  3271. return true;
  3272. break;
  3273. case 'mailbox_templates':
  3274. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3275. $_SESSION['return'][] = array(
  3276. 'type' => 'danger',
  3277. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3278. 'msg' => 'access_denied'
  3279. );
  3280. return false;
  3281. }
  3282. if (!is_array($_data['ids'])) {
  3283. $ids = array();
  3284. $ids[] = $_data['ids'];
  3285. }
  3286. else {
  3287. $ids = $_data['ids'];
  3288. }
  3289. foreach ($ids as $id) {
  3290. $is_now = mailbox("get", "mailbox_templates", $id);
  3291. if (empty($is_now) ||
  3292. $is_now["type"] != "mailbox"){
  3293. $_SESSION['return'][] = array(
  3294. 'type' => 'danger',
  3295. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_extra),
  3296. 'msg' => 'template_id_invalid'
  3297. );
  3298. continue;
  3299. }
  3300. // check name
  3301. if ($is_now["template"] == "Default" && $is_now["template"] != $_data["template"]){
  3302. // keep template name of Default template
  3303. $_data["template"] = $is_now["template"];
  3304. }
  3305. else {
  3306. $_data["template"] = (isset($_data["template"])) ? $_data["template"] : $is_now["template"];
  3307. }
  3308. // check attributes
  3309. $attr = array();
  3310. $attr["quota"] = isset($_data['quota']) ? intval($_data['quota']) * 1048576 : 0;
  3311. $attr['tags'] = (isset($_data['tags'])) ? $_data['tags'] : $is_now['tags'];
  3312. $attr["quarantine_notification"] = (!empty($_data['quarantine_notification'])) ? $_data['quarantine_notification'] : $is_now['quarantine_notification'];
  3313. $attr["quarantine_category"] = (!empty($_data['quarantine_category'])) ? $_data['quarantine_category'] : $is_now['quarantine_category'];
  3314. $attr["rl_frame"] = (!empty($_data['rl_frame'])) ? $_data['rl_frame'] : $is_now['rl_frame'];
  3315. $attr["rl_value"] = (!empty($_data['rl_value'])) ? $_data['rl_value'] : $is_now['rl_value'];
  3316. $attr["force_pw_update"] = isset($_data['force_pw_update']) ? intval($_data['force_pw_update']) : $is_now['force_pw_update'];
  3317. $attr["sogo_access"] = isset($_data['sogo_access']) ? intval($_data['sogo_access']) : $is_now['sogo_access'];
  3318. $attr["active"] = isset($_data['active']) ? intval($_data['active']) : $is_now['active'];
  3319. $attr["tls_enforce_in"] = isset($_data['tls_enforce_in']) ? intval($_data['tls_enforce_in']) : $is_now['tls_enforce_in'];
  3320. $attr["tls_enforce_out"] = isset($_data['tls_enforce_out']) ? intval($_data['tls_enforce_out']) : $is_now['tls_enforce_out'];
  3321. if (isset($_data['protocol_access'])) {
  3322. $_data['protocol_access'] = (array)$_data['protocol_access'];
  3323. $attr['imap_access'] = (in_array('imap', $_data['protocol_access'])) ? 1 : 0;
  3324. $attr['pop3_access'] = (in_array('pop3', $_data['protocol_access'])) ? 1 : 0;
  3325. $attr['smtp_access'] = (in_array('smtp', $_data['protocol_access'])) ? 1 : 0;
  3326. $attr['sieve_access'] = (in_array('sieve', $_data['protocol_access'])) ? 1 : 0;
  3327. }
  3328. else {
  3329. foreach ($is_now as $key => $value){
  3330. $attr[$key] = $is_now[$key];
  3331. }
  3332. }
  3333. if (isset($_data['acl'])) {
  3334. $_data['acl'] = (array)$_data['acl'];
  3335. $attr['acl_spam_alias'] = (in_array('spam_alias', $_data['acl'])) ? 1 : 0;
  3336. $attr['acl_tls_policy'] = (in_array('tls_policy', $_data['acl'])) ? 1 : 0;
  3337. $attr['acl_spam_score'] = (in_array('spam_score', $_data['acl'])) ? 1 : 0;
  3338. $attr['acl_spam_policy'] = (in_array('spam_policy', $_data['acl'])) ? 1 : 0;
  3339. $attr['acl_delimiter_action'] = (in_array('delimiter_action', $_data['acl'])) ? 1 : 0;
  3340. $attr['acl_syncjobs'] = (in_array('syncjobs', $_data['acl'])) ? 1 : 0;
  3341. $attr['acl_eas_reset'] = (in_array('eas_reset', $_data['acl'])) ? 1 : 0;
  3342. $attr['acl_sogo_profile_reset'] = (in_array('sogo_profile_reset', $_data['acl'])) ? 1 : 0;
  3343. $attr['acl_pushover'] = (in_array('pushover', $_data['acl'])) ? 1 : 0;
  3344. $attr['acl_quarantine'] = (in_array('quarantine', $_data['acl'])) ? 1 : 0;
  3345. $attr['acl_quarantine_attachments'] = (in_array('quarantine_attachments', $_data['acl'])) ? 1 : 0;
  3346. $attr['acl_quarantine_notification'] = (in_array('quarantine_notification', $_data['acl'])) ? 1 : 0;
  3347. $attr['acl_quarantine_category'] = (in_array('quarantine_category', $_data['acl'])) ? 1 : 0;
  3348. $attr['acl_app_passwds'] = (in_array('app_passwds', $_data['acl'])) ? 1 : 0;
  3349. } else {
  3350. foreach ($is_now as $key => $value){
  3351. $attr[$key] = $is_now[$key];
  3352. }
  3353. }
  3354. // update template
  3355. $stmt = $pdo->prepare("UPDATE `templates`
  3356. SET `template` = :template, `attributes` = :attributes
  3357. WHERE id = :id");
  3358. $stmt->execute(array(
  3359. ":id" => $id ,
  3360. ":template" => $_data["template"] ,
  3361. ":attributes" => json_encode($attr)
  3362. ));
  3363. }
  3364. $_SESSION['return'][] = array(
  3365. 'type' => 'success',
  3366. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3367. 'msg' => array('template_modified', $_data["template"])
  3368. );
  3369. return true;
  3370. break;
  3371. case 'mailbox_custom_attribute':
  3372. $_data['attribute'] = isset($_data['attribute']) ? $_data['attribute'] : array();
  3373. $_data['attribute'] = is_array($_data['attribute']) ? $_data['attribute'] : array($_data['attribute']);
  3374. $_data['attribute'] = array_map(function($value) { return str_replace(' ', '', $value); }, $_data['attribute']);
  3375. $_data['value'] = isset($_data['value']) ? $_data['value'] : array();
  3376. $_data['value'] = is_array($_data['value']) ? $_data['value'] : array($_data['value']);
  3377. $attributes = (object)array_combine($_data['attribute'], $_data['value']);
  3378. $mailboxes = is_array($_data['mailboxes']) ? $_data['mailboxes'] : array($_data['mailboxes']);
  3379. foreach ($mailboxes as $mailbox) {
  3380. if (!filter_var($mailbox, FILTER_VALIDATE_EMAIL)) {
  3381. $_SESSION['return'][] = array(
  3382. 'type' => 'danger',
  3383. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3384. 'msg' => array('username_invalid', $mailbox)
  3385. );
  3386. continue;
  3387. }
  3388. $is_now = mailbox('get', 'mailbox_details', $mailbox);
  3389. if(!empty($is_now)){
  3390. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $is_now['domain'])) {
  3391. $_SESSION['return'][] = array(
  3392. 'type' => 'danger',
  3393. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3394. 'msg' => 'access_denied'
  3395. );
  3396. continue;
  3397. }
  3398. }
  3399. else {
  3400. $_SESSION['return'][] = array(
  3401. 'type' => 'danger',
  3402. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3403. 'msg' => 'access_denied'
  3404. );
  3405. continue;
  3406. }
  3407. $stmt = $pdo->prepare("UPDATE `mailbox`
  3408. SET `custom_attributes` = :custom_attributes
  3409. WHERE username = :username");
  3410. $stmt->execute(array(
  3411. ":username" => $mailbox,
  3412. ":custom_attributes" => json_encode($attributes)
  3413. ));
  3414. $_SESSION['return'][] = array(
  3415. 'type' => 'success',
  3416. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3417. 'msg' => array('mailbox_modified', $mailbox)
  3418. );
  3419. }
  3420. return true;
  3421. break;
  3422. case 'resource':
  3423. if (!is_array($_data['name'])) {
  3424. $names = array();
  3425. $names[] = $_data['name'];
  3426. }
  3427. else {
  3428. $names = $_data['name'];
  3429. }
  3430. foreach ($names as $name) {
  3431. $is_now = mailbox('get', 'resource_details', $name);
  3432. if (!empty($is_now)) {
  3433. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  3434. $multiple_bookings = (isset($_data['multiple_bookings'])) ? intval($_data['multiple_bookings']) : $is_now['multiple_bookings'];
  3435. $description = (!empty($_data['description'])) ? $_data['description'] : $is_now['description'];
  3436. $kind = (!empty($_data['kind'])) ? $_data['kind'] : $is_now['kind'];
  3437. }
  3438. else {
  3439. $_SESSION['return'][] = array(
  3440. 'type' => 'danger',
  3441. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3442. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3443. );
  3444. continue;
  3445. }
  3446. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  3447. $_SESSION['return'][] = array(
  3448. 'type' => 'danger',
  3449. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3450. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3451. );
  3452. continue;
  3453. }
  3454. if (!isset($multiple_bookings) || $multiple_bookings < -1) {
  3455. $multiple_bookings = -1;
  3456. }
  3457. if (empty($description)) {
  3458. $_SESSION['return'][] = array(
  3459. 'type' => 'danger',
  3460. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3461. 'msg' => array('description_invalid', htmlspecialchars($name))
  3462. );
  3463. continue;
  3464. }
  3465. if ($kind != 'location' && $kind != 'group' && $kind != 'thing') {
  3466. $_SESSION['return'][] = array(
  3467. 'type' => 'danger',
  3468. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3469. 'msg' => array('resource_invalid', htmlspecialchars($name))
  3470. );
  3471. continue;
  3472. }
  3473. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  3474. $_SESSION['return'][] = array(
  3475. 'type' => 'danger',
  3476. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3477. 'msg' => 'access_denied'
  3478. );
  3479. continue;
  3480. }
  3481. $stmt = $pdo->prepare("UPDATE `mailbox` SET
  3482. `active` = :active,
  3483. `name`= :description,
  3484. `kind`= :kind,
  3485. `multiple_bookings`= :multiple_bookings
  3486. WHERE `username` = :name");
  3487. $stmt->execute(array(
  3488. ':active' => $active,
  3489. ':description' => $description,
  3490. ':multiple_bookings' => $multiple_bookings,
  3491. ':kind' => $kind,
  3492. ':name' => $name
  3493. ));
  3494. $_SESSION['return'][] = array(
  3495. 'type' => 'success',
  3496. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3497. 'msg' => array('resource_modified', htmlspecialchars($name))
  3498. );
  3499. }
  3500. break;
  3501. case 'domain_wide_footer':
  3502. if (!is_array($_data['domains'])) {
  3503. $domains = array();
  3504. $domains[] = $_data['domains'];
  3505. }
  3506. else {
  3507. $domains = $_data['domains'];
  3508. }
  3509. $footers = array();
  3510. $footers['html'] = isset($_data['html']) ? $_data['html'] : '';
  3511. $footers['plain'] = isset($_data['plain']) ? $_data['plain'] : '';
  3512. $footers['skip_replies'] = isset($_data['skip_replies']) ? (int)$_data['skip_replies'] : 0;
  3513. $footers['mbox_exclude'] = array();
  3514. if (isset($_data["mbox_exclude"])){
  3515. if (!is_array($_data["mbox_exclude"])) {
  3516. $_data["mbox_exclude"] = array($_data["mbox_exclude"]);
  3517. }
  3518. foreach ($_data["mbox_exclude"] as $mailbox) {
  3519. if (!filter_var($mailbox, FILTER_VALIDATE_EMAIL)) {
  3520. $_SESSION['return'][] = array(
  3521. 'type' => 'danger',
  3522. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3523. 'msg' => array('username_invalid', $mailbox)
  3524. );
  3525. continue;
  3526. }
  3527. $is_now = mailbox('get', 'mailbox_details', $mailbox);
  3528. if(empty($is_now)){
  3529. $_SESSION['return'][] = array(
  3530. 'type' => 'danger',
  3531. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3532. 'msg' => array('username_invalid', $mailbox)
  3533. );
  3534. continue;
  3535. }
  3536. array_push($footers['mbox_exclude'], $mailbox);
  3537. }
  3538. }
  3539. foreach ($domains as $domain) {
  3540. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  3541. if (!is_valid_domain_name($domain)) {
  3542. $_SESSION['return'][] = array(
  3543. 'type' => 'danger',
  3544. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3545. 'msg' => 'domain_invalid'
  3546. );
  3547. return false;
  3548. }
  3549. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  3550. $_SESSION['return'][] = array(
  3551. 'type' => 'danger',
  3552. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3553. 'msg' => 'access_denied'
  3554. );
  3555. return false;
  3556. }
  3557. try {
  3558. $stmt = $pdo->prepare("DELETE FROM `domain_wide_footer` WHERE `domain`= :domain");
  3559. $stmt->execute(array(':domain' => $domain));
  3560. $stmt = $pdo->prepare("INSERT INTO `domain_wide_footer` (`domain`, `html`, `plain`, `mbox_exclude`, `skip_replies`) VALUES (:domain, :html, :plain, :mbox_exclude, :skip_replies)");
  3561. $stmt->execute(array(
  3562. ':domain' => $domain,
  3563. ':html' => $footers['html'],
  3564. ':plain' => $footers['plain'],
  3565. ':mbox_exclude' => json_encode($footers['mbox_exclude']),
  3566. ':skip_replies' => $footers['skip_replies'],
  3567. ));
  3568. }
  3569. catch (PDOException $e) {
  3570. $_SESSION['return'][] = array(
  3571. 'type' => 'danger',
  3572. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3573. 'msg' => $e->getMessage()
  3574. );
  3575. return false;
  3576. }
  3577. $_SESSION['return'][] = array(
  3578. 'type' => 'success',
  3579. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3580. 'msg' => array('domain_footer_modified', htmlspecialchars($domain))
  3581. );
  3582. }
  3583. break;
  3584. }
  3585. break;
  3586. case 'get':
  3587. switch ($_type) {
  3588. case 'sender_acl_handles':
  3589. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  3590. return false;
  3591. }
  3592. $data['sender_acl_domains']['ro'] = array();
  3593. $data['sender_acl_domains']['rw'] = array();
  3594. $data['sender_acl_domains']['selectable'] = array();
  3595. $data['sender_acl_addresses']['ro'] = array();
  3596. $data['sender_acl_addresses']['rw'] = array();
  3597. $data['sender_acl_addresses']['selectable'] = array();
  3598. $data['fixed_sender_aliases'] = array();
  3599. $data['external_sender_aliases'] = array();
  3600. // Fixed addresses
  3601. $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` REGEXP :goto AND `address` NOT LIKE '@%'");
  3602. $stmt->execute(array(':goto' => '(^|,)'.$_data.'($|,)'));
  3603. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3604. while ($row = array_shift($rows)) {
  3605. $data['fixed_sender_aliases'][] = $row['address'];
  3606. }
  3607. $stmt = $pdo->prepare("SELECT CONCAT(`local_part`, '@', `alias_domain`.`alias_domain`) AS `alias_domain_alias` FROM `mailbox`, `alias_domain`
  3608. WHERE `alias_domain`.`target_domain` = `mailbox`.`domain`
  3609. AND `mailbox`.`username` = :username");
  3610. $stmt->execute(array(':username' => $_data));
  3611. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3612. while ($row = array_shift($rows)) {
  3613. if (!empty($row['alias_domain_alias'])) {
  3614. $data['fixed_sender_aliases'][] = $row['alias_domain_alias'];
  3615. }
  3616. }
  3617. // External addresses
  3618. $stmt = $pdo->prepare("SELECT `send_as` as `send_as_external` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '1'");
  3619. $stmt->execute(array(':logged_in_as' => $_data));
  3620. $exernal_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3621. while ($row = array_shift($exernal_rows)) {
  3622. if (!empty($row['send_as_external'])) {
  3623. $data['external_sender_aliases'][] = $row['send_as_external'];
  3624. }
  3625. }
  3626. // Return array $data['sender_acl_domains/addresses']['ro'] with read-only objects
  3627. // Return array $data['sender_acl_domains/addresses']['rw'] with read-write objects (can be deleted)
  3628. $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` = '*')");
  3629. $stmt->execute(array(':logged_in_as' => $_data));
  3630. $domain_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3631. while ($domain_row = array_shift($domain_rows)) {
  3632. if (is_valid_domain_name($domain_row['send_as']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3633. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3634. continue;
  3635. }
  3636. if (is_valid_domain_name($domain_row['send_as']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain_row['send_as'])) {
  3637. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3638. continue;
  3639. }
  3640. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] != 'admin') {
  3641. $data['sender_acl_domains']['ro'][] = $domain_row['send_as'];
  3642. }
  3643. if ($domain_row['send_as'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3644. $data['sender_acl_domains']['rw'][] = $domain_row['send_as'];
  3645. }
  3646. }
  3647. $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` != '*')");
  3648. $stmt->execute(array(':logged_in_as' => $_data));
  3649. $address_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3650. while ($address_row = array_shift($address_rows)) {
  3651. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && !hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3652. $data['sender_acl_addresses']['ro'][] = $address_row['send_as'];
  3653. continue;
  3654. }
  3655. if (filter_var($address_row['send_as'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $address_row['send_as'])) {
  3656. $data['sender_acl_addresses']['rw'][] = $address_row['send_as'];
  3657. continue;
  3658. }
  3659. }
  3660. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  3661. WHERE `domain` NOT IN (
  3662. SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
  3663. WHERE `logged_in_as` = :logged_in_as1
  3664. AND `external` = '0'
  3665. AND `send_as` LIKE '@%')
  3666. UNION
  3667. SELECT '*' FROM `domain`
  3668. WHERE '*' NOT IN (
  3669. SELECT `send_as` FROM `sender_acl`
  3670. WHERE `logged_in_as` = :logged_in_as2
  3671. AND `external` = '0'
  3672. )");
  3673. $stmt->execute(array(
  3674. ':logged_in_as1' => $_data,
  3675. ':logged_in_as2' => $_data
  3676. ));
  3677. $rows_domain = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3678. while ($row_domain = array_shift($rows_domain)) {
  3679. if (is_valid_domain_name($row_domain['domain']) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row_domain['domain'])) {
  3680. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3681. continue;
  3682. }
  3683. if ($row_domain['domain'] == '*' && $_SESSION['mailcow_cc_role'] == 'admin') {
  3684. $data['sender_acl_domains']['selectable'][] = $row_domain['domain'];
  3685. continue;
  3686. }
  3687. }
  3688. $stmt = $pdo->prepare("SELECT `address` FROM `alias`
  3689. WHERE `goto` != :goto
  3690. AND `address` NOT IN (
  3691. SELECT `send_as` FROM `sender_acl`
  3692. WHERE `logged_in_as` = :logged_in_as
  3693. AND `external` = '0'
  3694. AND `send_as` NOT LIKE '@%')");
  3695. $stmt->execute(array(
  3696. ':logged_in_as' => $_data,
  3697. ':goto' => $_data
  3698. ));
  3699. $rows_mbox = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3700. while ($row = array_shift($rows_mbox)) {
  3701. // Aliases are not selectable
  3702. if (in_array($row['address'], $data['fixed_sender_aliases'])) {
  3703. continue;
  3704. }
  3705. if (filter_var($row['address'], FILTER_VALIDATE_EMAIL) && hasAliasObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['address'])) {
  3706. $data['sender_acl_addresses']['selectable'][] = $row['address'];
  3707. }
  3708. }
  3709. return $data;
  3710. break;
  3711. case 'mailboxes':
  3712. $mailboxes = array();
  3713. if (isset($_extra) && is_array($_extra) && isset($_data)) {
  3714. // get by domain and tags
  3715. $tags = is_array($_extra) ? $_extra : array();
  3716. $sql = "";
  3717. foreach ($tags as $key => $tag) {
  3718. $sql = $sql."SELECT DISTINCT `username` FROM `tags_mailbox` WHERE `username` LIKE ? AND `tag_name` LIKE ?"; // distinct, avoid duplicates
  3719. if ($key === array_key_last($tags)) break;
  3720. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  3721. }
  3722. // prepend domain to array
  3723. $params = array();
  3724. foreach ($tags as $key => $val){
  3725. array_push($params, '%'.$_data.'%');
  3726. array_push($params, '%'.$val.'%');
  3727. }
  3728. $stmt = $pdo->prepare($sql);
  3729. $stmt->execute($params);
  3730. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3731. while($row = array_shift($rows)) {
  3732. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], explode('@', $row['username'])[1]))
  3733. $mailboxes[] = $row['username'];
  3734. }
  3735. }
  3736. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3737. // get by domain
  3738. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain");
  3739. $stmt->execute(array(
  3740. ':domain' => $_data,
  3741. ));
  3742. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3743. while($row = array_shift($rows)) {
  3744. $mailboxes[] = $row['username'];
  3745. }
  3746. }
  3747. else {
  3748. $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)");
  3749. $stmt->execute(array(
  3750. ':username' => $_SESSION['mailcow_cc_username'],
  3751. ':role' => $_SESSION['mailcow_cc_role'],
  3752. ));
  3753. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3754. while($row = array_shift($rows)) {
  3755. $mailboxes[] = $row['username'];
  3756. }
  3757. }
  3758. return $mailboxes;
  3759. break;
  3760. case 'tls_policy':
  3761. $attrs = array();
  3762. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3763. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3764. return false;
  3765. }
  3766. }
  3767. else {
  3768. $_data = $_SESSION['mailcow_cc_username'];
  3769. }
  3770. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3771. $stmt->execute(array(':username' => $_data));
  3772. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3773. $attrs = json_decode($attrs['attributes'], true);
  3774. return array(
  3775. 'tls_enforce_in' => $attrs['tls_enforce_in'],
  3776. 'tls_enforce_out' => $attrs['tls_enforce_out']
  3777. );
  3778. break;
  3779. case 'quarantine_notification':
  3780. $attrs = array();
  3781. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3782. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3783. return false;
  3784. }
  3785. }
  3786. else {
  3787. $_data = $_SESSION['mailcow_cc_username'];
  3788. }
  3789. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3790. $stmt->execute(array(':username' => $_data));
  3791. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3792. $attrs = json_decode($attrs['attributes'], true);
  3793. return $attrs['quarantine_notification'];
  3794. break;
  3795. case 'quarantine_category':
  3796. $attrs = array();
  3797. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3798. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3799. return false;
  3800. }
  3801. }
  3802. else {
  3803. $_data = $_SESSION['mailcow_cc_username'];
  3804. }
  3805. $stmt = $pdo->prepare("SELECT `attributes` FROM `mailbox` WHERE `username` = :username");
  3806. $stmt->execute(array(':username' => $_data));
  3807. $attrs = $stmt->fetch(PDO::FETCH_ASSOC);
  3808. $attrs = json_decode($attrs['attributes'], true);
  3809. return $attrs['quarantine_category'];
  3810. break;
  3811. case 'filters':
  3812. $filters = array();
  3813. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3814. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3815. return false;
  3816. }
  3817. }
  3818. else {
  3819. $_data = $_SESSION['mailcow_cc_username'];
  3820. }
  3821. $stmt = $pdo->prepare("SELECT `id` FROM `sieve_filters` WHERE `username` = :username");
  3822. $stmt->execute(array(':username' => $_data));
  3823. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3824. while($row = array_shift($rows)) {
  3825. $filters[] = $row['id'];
  3826. }
  3827. return $filters;
  3828. break;
  3829. case 'global_filter_details':
  3830. $global_filters = array();
  3831. if ($_SESSION['mailcow_cc_role'] != "admin") {
  3832. return false;
  3833. }
  3834. $global_filters['prefilter'] = file_get_contents('/global_sieve/before');
  3835. $global_filters['postfilter'] = file_get_contents('/global_sieve/after');
  3836. return $global_filters;
  3837. break;
  3838. case 'filter_details':
  3839. $filter_details = array();
  3840. if (!is_numeric($_data)) {
  3841. return false;
  3842. }
  3843. $stmt = $pdo->prepare("SELECT CASE `script_name` WHEN 'active' THEN 1 ELSE 0 END AS `active`,
  3844. id,
  3845. username,
  3846. filter_type,
  3847. script_data,
  3848. script_desc
  3849. FROM `sieve_filters`
  3850. WHERE `id` = :id");
  3851. $stmt->execute(array(':id' => $_data));
  3852. $filter_details = $stmt->fetch(PDO::FETCH_ASSOC);
  3853. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $filter_details['username'])) {
  3854. return false;
  3855. }
  3856. return $filter_details;
  3857. break;
  3858. case 'active_user_sieve':
  3859. $filter_details = array();
  3860. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3861. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3862. return false;
  3863. }
  3864. }
  3865. else {
  3866. $_data = $_SESSION['mailcow_cc_username'];
  3867. }
  3868. $exec_fields = array(
  3869. 'cmd' => 'sieve',
  3870. 'task' => 'list',
  3871. 'username' => $_data
  3872. );
  3873. $filters = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3874. $filters = array_filter(preg_split("/(\r\n|\n|\r)/",$filters));
  3875. foreach ($filters as $filter) {
  3876. if (preg_match('/.+ ACTIVE/i', $filter)) {
  3877. $exec_fields = array(
  3878. 'cmd' => 'sieve',
  3879. 'task' => 'print',
  3880. 'script_name' => substr($filter, 0, -7),
  3881. 'username' => $_data
  3882. );
  3883. $script = docker('post', 'dovecot-mailcow', 'exec', $exec_fields);
  3884. // Remove first line
  3885. return preg_replace('/^.+\n/', '', $script);
  3886. }
  3887. }
  3888. return false;
  3889. break;
  3890. case 'syncjob_details':
  3891. $syncjobdetails = array();
  3892. if (!is_numeric($_data)) {
  3893. return false;
  3894. }
  3895. if (isset($_extra) && in_array('no_log', $_extra)) {
  3896. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("returned_text", "password1")');
  3897. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3898. while($field = array_shift($fields)) {
  3899. $shown_fields[] = $field['Field'];
  3900. }
  3901. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3902. `active`
  3903. FROM `imapsync` WHERE id = :id");
  3904. }
  3905. elseif (isset($_extra) && in_array('with_password', $_extra)) {
  3906. $stmt = $pdo->prepare("SELECT *,
  3907. `active`
  3908. FROM `imapsync` WHERE id = :id");
  3909. }
  3910. else {
  3911. $field_query = $pdo->query('SHOW FIELDS FROM `imapsync` WHERE FIELD NOT IN ("password1")');
  3912. $fields = $field_query->fetchAll(PDO::FETCH_ASSOC);
  3913. while($field = array_shift($fields)) {
  3914. $shown_fields[] = $field['Field'];
  3915. }
  3916. $stmt = $pdo->prepare("SELECT " . implode(',', (array)$shown_fields) . ",
  3917. `active`
  3918. FROM `imapsync` WHERE id = :id");
  3919. }
  3920. $stmt->execute(array(':id' => $_data));
  3921. $syncjobdetails = $stmt->fetch(PDO::FETCH_ASSOC);
  3922. if (!empty($syncjobdetails['returned_text'])) {
  3923. $syncjobdetails['log'] = $syncjobdetails['returned_text'];
  3924. }
  3925. else {
  3926. $syncjobdetails['log'] = '';
  3927. }
  3928. unset($syncjobdetails['returned_text']);
  3929. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $syncjobdetails['user2'])) {
  3930. return false;
  3931. }
  3932. return $syncjobdetails;
  3933. break;
  3934. case 'syncjobs':
  3935. $syncjobdata = array();
  3936. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3937. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3938. return false;
  3939. }
  3940. }
  3941. else {
  3942. $_data = $_SESSION['mailcow_cc_username'];
  3943. }
  3944. $stmt = $pdo->prepare("SELECT `id` FROM `imapsync` WHERE `user2` = :username");
  3945. $stmt->execute(array(':username' => $_data));
  3946. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  3947. while($row = array_shift($rows)) {
  3948. $syncjobdata[] = $row['id'];
  3949. }
  3950. return $syncjobdata;
  3951. break;
  3952. case 'spam_score':
  3953. $curl = curl_init();
  3954. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
  3955. curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
  3956. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  3957. $default_actions = curl_exec($curl);
  3958. if (!curl_errno($curl)) {
  3959. $data_array = json_decode($default_actions, true);
  3960. curl_close($curl);
  3961. foreach ($data_array as $data) {
  3962. if ($data['action'] == 'reject') {
  3963. $reject = $data['value'];
  3964. continue;
  3965. }
  3966. elseif ($data['action'] == 'add header') {
  3967. $add_header = $data['value'];
  3968. continue;
  3969. }
  3970. }
  3971. if (empty($add_header) || empty($reject)) {
  3972. // Assume default, set warning
  3973. $default = "5, 15";
  3974. $_SESSION['return'][] = array(
  3975. 'type' => 'warning',
  3976. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3977. 'msg' => 'Could not determine servers default spam score, assuming default'
  3978. );
  3979. }
  3980. else {
  3981. $default = $add_header . ', ' . $reject;
  3982. }
  3983. }
  3984. else {
  3985. // Assume default, set warning
  3986. $default = "5, 15";
  3987. $_SESSION['return'][] = array(
  3988. 'type' => 'warning',
  3989. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  3990. 'msg' => 'Could not determine servers default spam score, assuming default'
  3991. );
  3992. }
  3993. curl_close($curl);
  3994. $policydata = array();
  3995. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  3996. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  3997. return false;
  3998. }
  3999. }
  4000. else {
  4001. $_data = $_SESSION['mailcow_cc_username'];
  4002. }
  4003. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `object` = :username AND
  4004. (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
  4005. $stmt->execute(array(':username' => $_data));
  4006. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  4007. if (empty($num_results)) {
  4008. return $default;
  4009. }
  4010. else {
  4011. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'highspamlevel' AND `object` = :username");
  4012. $stmt->execute(array(':username' => $_data));
  4013. $highspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4014. $stmt = $pdo->prepare("SELECT `value` FROM `filterconf` WHERE `option` = 'lowspamlevel' AND `object` = :username");
  4015. $stmt->execute(array(':username' => $_data));
  4016. $lowspamlevel = $stmt->fetch(PDO::FETCH_ASSOC);
  4017. return $lowspamlevel['value'].', '.$highspamlevel['value'];
  4018. }
  4019. break;
  4020. case 'time_limited_aliases':
  4021. $tladata = array();
  4022. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4023. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4024. return false;
  4025. }
  4026. }
  4027. else {
  4028. $_data = $_SESSION['mailcow_cc_username'];
  4029. }
  4030. $stmt = $pdo->prepare("SELECT `address`,
  4031. `goto`,
  4032. `validity`,
  4033. `created`,
  4034. `modified`
  4035. FROM `spamalias`
  4036. WHERE `goto` = :username
  4037. AND `validity` >= :unixnow");
  4038. $stmt->execute(array(':username' => $_data, ':unixnow' => time()));
  4039. $tladata = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4040. return $tladata;
  4041. break;
  4042. case 'delimiter_action':
  4043. $policydata = array();
  4044. if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
  4045. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4046. return false;
  4047. }
  4048. }
  4049. else {
  4050. $_data = $_SESSION['mailcow_cc_username'];
  4051. }
  4052. try {
  4053. if ($redis->hGet('RCPT_WANTS_SUBJECT_TAG', $_data)) {
  4054. return "subject";
  4055. }
  4056. elseif ($redis->hGet('RCPT_WANTS_SUBFOLDER_TAG', $_data)) {
  4057. return "subfolder";
  4058. }
  4059. else {
  4060. return "none";
  4061. }
  4062. }
  4063. catch (RedisException $e) {
  4064. $_SESSION['return'][] = array(
  4065. 'type' => 'danger',
  4066. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4067. 'msg' => array('redis_error', $e)
  4068. );
  4069. return false;
  4070. }
  4071. break;
  4072. case 'resources':
  4073. $resources = array();
  4074. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4075. return false;
  4076. }
  4077. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4078. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `domain` = :domain");
  4079. $stmt->execute(array(
  4080. ':domain' => $_data,
  4081. ));
  4082. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4083. while($row = array_shift($rows)) {
  4084. $resources[] = $row['username'];
  4085. }
  4086. }
  4087. else {
  4088. $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");
  4089. $stmt->execute(array(
  4090. ':username' => $_SESSION['mailcow_cc_username'],
  4091. ':role' => $_SESSION['mailcow_cc_role'],
  4092. ));
  4093. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4094. while($row = array_shift($rows)) {
  4095. $resources[] = $row['username'];
  4096. }
  4097. }
  4098. return $resources;
  4099. break;
  4100. case 'alias_domains':
  4101. $aliasdomains = array();
  4102. if (isset($_data) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4103. return false;
  4104. }
  4105. elseif (isset($_data) && hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4106. $stmt = $pdo->prepare("SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain");
  4107. $stmt->execute(array(
  4108. ':domain' => $_data,
  4109. ));
  4110. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4111. while($row = array_shift($rows)) {
  4112. $aliasdomains[] = $row['alias_domain'];
  4113. }
  4114. }
  4115. else {
  4116. $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");
  4117. $stmt->execute(array(
  4118. ':username' => $_SESSION['mailcow_cc_username'],
  4119. ':role' => $_SESSION['mailcow_cc_role'],
  4120. ));
  4121. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4122. while($row = array_shift($rows)) {
  4123. $aliasdomains[] = $row['alias_domain'];
  4124. }
  4125. }
  4126. return $aliasdomains;
  4127. break;
  4128. case 'aliases':
  4129. $aliases = array();
  4130. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4131. return false;
  4132. }
  4133. $stmt = $pdo->prepare("SELECT `id`, `address` FROM `alias` WHERE `address` != `goto` AND `domain` = :domain");
  4134. $stmt->execute(array(
  4135. ':domain' => $_data,
  4136. ));
  4137. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4138. while($row = array_shift($rows)) {
  4139. if ($_extra == "address"){
  4140. $aliases[] = $row['address'];
  4141. } else {
  4142. $aliases[] = $row['id'];
  4143. }
  4144. }
  4145. return $aliases;
  4146. break;
  4147. case 'alias_details':
  4148. $aliasdata = array();
  4149. $stmt = $pdo->prepare("SELECT
  4150. `id`,
  4151. `domain`,
  4152. `goto`,
  4153. `address`,
  4154. `public_comment`,
  4155. `private_comment`,
  4156. `active`,
  4157. `sogo_visible`,
  4158. `created`,
  4159. `modified`
  4160. FROM `alias`
  4161. WHERE (`id` = :id OR `address` = :address) AND `address` != `goto`");
  4162. $stmt->execute(array(
  4163. ':id' => $_data,
  4164. ':address' => $_data,
  4165. ));
  4166. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4167. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4168. $stmt->execute(array(
  4169. ':domain' => $row['domain'],
  4170. ));
  4171. $row_alias_domain = $stmt->fetch(PDO::FETCH_ASSOC);
  4172. if (isset($row_alias_domain['target_domain']) && !empty($row_alias_domain['target_domain'])) {
  4173. $aliasdata['in_primary_domain'] = $row_alias_domain['target_domain'];
  4174. }
  4175. else {
  4176. $aliasdata['in_primary_domain'] = "";
  4177. }
  4178. $aliasdata['id'] = $row['id'];
  4179. $aliasdata['domain'] = $row['domain'];
  4180. $aliasdata['public_comment'] = $row['public_comment'];
  4181. $aliasdata['private_comment'] = $row['private_comment'];
  4182. $aliasdata['domain'] = $row['domain'];
  4183. $aliasdata['goto'] = $row['goto'];
  4184. $aliasdata['address'] = $row['address'];
  4185. (!filter_var($aliasdata['address'], FILTER_VALIDATE_EMAIL)) ? $aliasdata['is_catch_all'] = 1 : $aliasdata['is_catch_all'] = 0;
  4186. $aliasdata['active'] = $row['active'];
  4187. $aliasdata['active_int'] = $row['active'];
  4188. $aliasdata['sogo_visible'] = $row['sogo_visible'];
  4189. $aliasdata['sogo_visible_int'] = $row['sogo_visible'];
  4190. $aliasdata['created'] = $row['created'];
  4191. $aliasdata['modified'] = $row['modified'];
  4192. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdata['domain'])) {
  4193. return false;
  4194. }
  4195. return $aliasdata;
  4196. break;
  4197. case 'alias_domain_details':
  4198. $aliasdomaindata = array();
  4199. $rl = ratelimit('get', 'domain', $_data);
  4200. $stmt = $pdo->prepare("SELECT
  4201. `alias_domain`,
  4202. `target_domain`,
  4203. `active`,
  4204. `created`,
  4205. `modified`
  4206. FROM `alias_domain`
  4207. WHERE `alias_domain` = :aliasdomain");
  4208. $stmt->execute(array(
  4209. ':aliasdomain' => $_data,
  4210. ));
  4211. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4212. $stmt = $pdo->prepare("SELECT `backupmx` FROM `domain` WHERE `domain` = :target_domain");
  4213. $stmt->execute(array(
  4214. ':target_domain' => $row['target_domain']
  4215. ));
  4216. $row_parent = $stmt->fetch(PDO::FETCH_ASSOC);
  4217. $aliasdomaindata['alias_domain'] = $row['alias_domain'];
  4218. $aliasdomaindata['parent_is_backupmx'] = $row_parent['backupmx'];
  4219. $aliasdomaindata['target_domain'] = $row['target_domain'];
  4220. $aliasdomaindata['active'] = $row['active'];
  4221. $aliasdomaindata['active_int'] = $row['active'];
  4222. $aliasdomaindata['rl'] = $rl;
  4223. $aliasdomaindata['created'] = $row['created'];
  4224. $aliasdomaindata['modified'] = $row['modified'];
  4225. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $aliasdomaindata['target_domain'])) {
  4226. return false;
  4227. }
  4228. return $aliasdomaindata;
  4229. break;
  4230. case 'shared_aliases':
  4231. $shared_aliases = array();
  4232. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4233. WHERE `goto` REGEXP ','
  4234. AND `address` NOT LIKE '@%'
  4235. AND `goto` != `address`");
  4236. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4237. while($row = array_shift($rows)) {
  4238. $domain = explode("@", $row['address'])[1];
  4239. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4240. $shared_aliases[] = $row['address'];
  4241. }
  4242. }
  4243. return $shared_aliases;
  4244. break;
  4245. case 'direct_aliases':
  4246. $direct_aliases = array();
  4247. $stmt = $pdo->query("SELECT `address` FROM `alias`
  4248. WHERE `goto` NOT LIKE '%,%'
  4249. AND `address` NOT LIKE '@%'
  4250. AND `goto` != `address`");
  4251. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4252. while($row = array_shift($rows)) {
  4253. $domain = explode("@", $row['address'])[1];
  4254. if (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  4255. $direct_aliases[] = $row['address'];
  4256. }
  4257. }
  4258. return $direct_aliases;
  4259. break;
  4260. case 'domains':
  4261. $domains = array();
  4262. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4263. return false;
  4264. }
  4265. if (isset($_extra) && is_array($_extra)){
  4266. // get by tags
  4267. $tags = is_array($_extra) ? $_extra : array();
  4268. // add % as prefix and suffix to every element for relative searching
  4269. $tags = array_map(function($x){ return '%'.$x.'%'; }, $tags);
  4270. $sql = "";
  4271. foreach ($tags as $key => $tag) {
  4272. $sql = $sql."SELECT DISTINCT `domain` FROM `tags_domain` WHERE `tag_name` LIKE ?"; // distinct, avoid duplicates
  4273. if ($key === array_key_last($tags)) break;
  4274. $sql = $sql.' UNION DISTINCT '; // combine querys with union - distinct, avoid duplicates
  4275. }
  4276. $stmt = $pdo->prepare($sql);
  4277. $stmt->execute($tags);
  4278. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4279. while($row = array_shift($rows)) {
  4280. if ($_SESSION['mailcow_cc_role'] == "admin")
  4281. $domains[] = $row['domain'];
  4282. elseif (hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $row['domain']))
  4283. $domains[] = $row['domain'];
  4284. }
  4285. } else {
  4286. // get all
  4287. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  4288. WHERE (`domain` IN (
  4289. SELECT `domain` from `domain_admins`
  4290. WHERE (`active`='1' AND `username` = :username))
  4291. )
  4292. OR 'admin'= :role");
  4293. $stmt->execute(array(
  4294. ':username' => $_SESSION['mailcow_cc_username'],
  4295. ':role' => $_SESSION['mailcow_cc_role'],
  4296. ));
  4297. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4298. while($row = array_shift($rows)) {
  4299. $domains[] = $row['domain'];
  4300. }
  4301. }
  4302. return $domains;
  4303. break;
  4304. case 'domain_details':
  4305. $domaindata = array();
  4306. $_data = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4307. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4308. return false;
  4309. }
  4310. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` WHERE `alias_domain` = :domain");
  4311. $stmt->execute(array(
  4312. ':domain' => $_data
  4313. ));
  4314. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4315. if (!empty($row)) {
  4316. $_data = $row['target_domain'];
  4317. }
  4318. $stmt = $pdo->prepare("SELECT
  4319. `domain`,
  4320. `description`,
  4321. `aliases`,
  4322. `mailboxes`,
  4323. `defquota`,
  4324. `maxquota`,
  4325. `created`,
  4326. `modified`,
  4327. `quota`,
  4328. `relayhost`,
  4329. `relay_all_recipients`,
  4330. `relay_unknown_only`,
  4331. `backupmx`,
  4332. `gal`,
  4333. `active`
  4334. FROM `domain` WHERE `domain`= :domain");
  4335. $stmt->execute(array(
  4336. ':domain' => $_data
  4337. ));
  4338. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4339. if (empty($row)) {
  4340. return false;
  4341. }
  4342. $stmt = $pdo->prepare("SELECT COUNT(`username`) AS `count`,
  4343. COALESCE(SUM(`quota`), 0) AS `in_use`
  4344. FROM `mailbox`
  4345. WHERE (`kind` = '' OR `kind` = NULL)
  4346. AND `domain` = :domain");
  4347. $stmt->execute(array(':domain' => $row['domain']));
  4348. $MailboxDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4349. $stmt = $pdo->prepare("SELECT SUM(bytes) AS `bytes_total`, SUM(messages) AS `msgs_total` FROM `quota2`
  4350. WHERE `username` IN (
  4351. SELECT `username` FROM `mailbox`
  4352. WHERE `domain` = :domain
  4353. );");
  4354. $stmt->execute(array(':domain' => $row['domain']));
  4355. $SumQuotaInUse = $stmt->fetch(PDO::FETCH_ASSOC);
  4356. $rl = ratelimit('get', 'domain', $_data);
  4357. $domaindata['max_new_mailbox_quota'] = ($row['quota'] * 1048576) - $MailboxDataDomain['in_use'];
  4358. if ($domaindata['max_new_mailbox_quota'] > ($row['maxquota'] * 1048576)) {
  4359. $domaindata['max_new_mailbox_quota'] = ($row['maxquota'] * 1048576);
  4360. }
  4361. $domaindata['def_new_mailbox_quota'] = $domaindata['max_new_mailbox_quota'];
  4362. if ($domaindata['def_new_mailbox_quota'] > ($row['defquota'] * 1048576)) {
  4363. $domaindata['def_new_mailbox_quota'] = ($row['defquota'] * 1048576);
  4364. }
  4365. $domaindata['quota_used_in_domain'] = $MailboxDataDomain['in_use'];
  4366. if (!empty($SumQuotaInUse['bytes_total'])) {
  4367. $domaindata['bytes_total'] = $SumQuotaInUse['bytes_total'];
  4368. }
  4369. else {
  4370. $domaindata['bytes_total'] = 0;
  4371. }
  4372. if (!empty($SumQuotaInUse['msgs_total'])) {
  4373. $domaindata['msgs_total'] = $SumQuotaInUse['msgs_total'];
  4374. }
  4375. else {
  4376. $domaindata['msgs_total'] = 0;
  4377. }
  4378. $domaindata['mboxes_in_domain'] = $MailboxDataDomain['count'];
  4379. $domaindata['mboxes_left'] = $row['mailboxes'] - $MailboxDataDomain['count'];
  4380. $domaindata['domain_name'] = $row['domain'];
  4381. $domaindata['description'] = $row['description'];
  4382. $domaindata['max_num_aliases_for_domain'] = $row['aliases'];
  4383. $domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
  4384. $domaindata['def_quota_for_mbox'] = $row['defquota'] * 1048576;
  4385. $domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
  4386. $domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
  4387. $domaindata['relayhost'] = $row['relayhost'];
  4388. $domaindata['backupmx'] = $row['backupmx'];
  4389. $domaindata['backupmx_int'] = $row['backupmx'];
  4390. $domaindata['gal'] = $row['gal'];
  4391. $domaindata['gal_int'] = $row['gal'];
  4392. $domaindata['rl'] = $rl;
  4393. $domaindata['active'] = $row['active'];
  4394. $domaindata['active_int'] = $row['active'];
  4395. $domaindata['relay_all_recipients'] = $row['relay_all_recipients'];
  4396. $domaindata['relay_all_recipients_int'] = $row['relay_all_recipients'];
  4397. $domaindata['relay_unknown_only'] = $row['relay_unknown_only'];
  4398. $domaindata['relay_unknown_only_int'] = $row['relay_unknown_only'];
  4399. $domaindata['created'] = $row['created'];
  4400. $domaindata['modified'] = $row['modified'];
  4401. $stmt = $pdo->prepare("SELECT COUNT(`address`) AS `alias_count` FROM `alias`
  4402. WHERE (`domain`= :domain OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = :domain2))
  4403. AND `address` NOT IN (
  4404. SELECT `username` FROM `mailbox`
  4405. )");
  4406. $stmt->execute(array(
  4407. ':domain' => $_data,
  4408. ':domain2' => $_data
  4409. ));
  4410. $AliasDataDomain = $stmt->fetch(PDO::FETCH_ASSOC);
  4411. (isset($AliasDataDomain['alias_count'])) ? $domaindata['aliases_in_domain'] = $AliasDataDomain['alias_count'] : $domaindata['aliases_in_domain'] = "0";
  4412. $domaindata['aliases_left'] = $row['aliases'] - $AliasDataDomain['alias_count'];
  4413. if ($_SESSION['mailcow_cc_role'] == "admin")
  4414. {
  4415. $stmt = $pdo->prepare("SELECT GROUP_CONCAT(`username` SEPARATOR ', ') AS domain_admins FROM `domain_admins` WHERE `domain` = :domain");
  4416. $stmt->execute(array(
  4417. ':domain' => $_data
  4418. ));
  4419. $domain_admins = $stmt->fetch(PDO::FETCH_ASSOC);
  4420. (isset($domain_admins['domain_admins'])) ? $domaindata['domain_admins'] = $domain_admins['domain_admins'] : $domaindata['domain_admins'] = "-";
  4421. }
  4422. $stmt = $pdo->prepare("SELECT `tag_name`
  4423. FROM `tags_domain` WHERE `domain`= :domain");
  4424. $stmt->execute(array(
  4425. ':domain' => $_data
  4426. ));
  4427. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4428. while ($tag = array_shift($tags)) {
  4429. $domaindata['tags'][] = $tag['tag_name'];
  4430. }
  4431. return $domaindata;
  4432. break;
  4433. case 'domain_templates':
  4434. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4435. return false;
  4436. }
  4437. $_data = (isset($_data)) ? intval($_data) : null;
  4438. if (isset($_data)){
  4439. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4440. WHERE `id` = :id AND type = :type");
  4441. $stmt->execute(array(
  4442. ":id" => $_data,
  4443. ":type" => "domain"
  4444. ));
  4445. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4446. if (empty($row)){
  4447. return false;
  4448. }
  4449. $row["attributes"] = json_decode($row["attributes"], true);
  4450. return $row;
  4451. }
  4452. else {
  4453. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'domain'");
  4454. $stmt->execute();
  4455. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4456. if (empty($rows)){
  4457. return false;
  4458. }
  4459. foreach($rows as $key => $row){
  4460. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4461. }
  4462. return $rows;
  4463. }
  4464. break;
  4465. case 'mailbox_details':
  4466. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4467. return false;
  4468. }
  4469. $mailboxdata = array();
  4470. if (preg_match('/y|yes/i', getenv('MASTER'))) {
  4471. $stmt = $pdo->prepare("SELECT
  4472. `domain`.`backupmx`,
  4473. `mailbox`.`username`,
  4474. `mailbox`.`name`,
  4475. `mailbox`.`active`,
  4476. `mailbox`.`domain`,
  4477. `mailbox`.`local_part`,
  4478. `mailbox`.`quota`,
  4479. `mailbox`.`created`,
  4480. `mailbox`.`modified`,
  4481. `mailbox`.`authsource`,
  4482. `quota2`.`bytes`,
  4483. `attributes`,
  4484. `custom_attributes`,
  4485. `quota2`.`messages`
  4486. FROM `mailbox`, `quota2`, `domain`
  4487. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4488. AND `mailbox`.`username` = `quota2`.`username`
  4489. AND `domain`.`domain` = `mailbox`.`domain`
  4490. AND `mailbox`.`username` = :mailbox");
  4491. }
  4492. else {
  4493. $stmt = $pdo->prepare("SELECT
  4494. `domain`.`backupmx`,
  4495. `mailbox`.`username`,
  4496. `mailbox`.`name`,
  4497. `mailbox`.`active`,
  4498. `mailbox`.`domain`,
  4499. `mailbox`.`local_part`,
  4500. `mailbox`.`quota`,
  4501. `mailbox`.`created`,
  4502. `mailbox`.`modified`,
  4503. `mailbox`.`authsource`,
  4504. `quota2replica`.`bytes`,
  4505. `attributes`,
  4506. `custom_attributes`,
  4507. `quota2replica`.`messages`
  4508. FROM `mailbox`, `quota2replica`, `domain`
  4509. WHERE (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)
  4510. AND `mailbox`.`username` = `quota2replica`.`username`
  4511. AND `domain`.`domain` = `mailbox`.`domain`
  4512. AND `mailbox`.`username` = :mailbox");
  4513. }
  4514. $stmt->execute(array(
  4515. ':mailbox' => $_data,
  4516. ));
  4517. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4518. $mailboxdata['username'] = $row['username'];
  4519. $mailboxdata['active'] = $row['active'];
  4520. $mailboxdata['active_int'] = $row['active'];
  4521. $mailboxdata['domain'] = $row['domain'];
  4522. $mailboxdata['name'] = $row['name'];
  4523. $mailboxdata['local_part'] = $row['local_part'];
  4524. $mailboxdata['quota'] = $row['quota'];
  4525. $mailboxdata['messages'] = $row['messages'];
  4526. $mailboxdata['attributes'] = json_decode($row['attributes'], true);
  4527. $mailboxdata['custom_attributes'] = json_decode($row['custom_attributes'], true);
  4528. $mailboxdata['quota_used'] = intval($row['bytes']);
  4529. $mailboxdata['percent_in_use'] = ($row['quota'] == 0) ? '- ' : round((intval($row['bytes']) / intval($row['quota'])) * 100);
  4530. $mailboxdata['created'] = $row['created'];
  4531. $mailboxdata['modified'] = $row['modified'];
  4532. $mailboxdata['authsource'] = ($row['authsource']) ? $row['authsource'] : 'mailcow';
  4533. if ($mailboxdata['percent_in_use'] === '- ') {
  4534. $mailboxdata['percent_class'] = "info";
  4535. }
  4536. elseif ($mailboxdata['percent_in_use'] >= 90) {
  4537. $mailboxdata['percent_class'] = "danger";
  4538. }
  4539. elseif ($mailboxdata['percent_in_use'] >= 75) {
  4540. $mailboxdata['percent_class'] = "warning";
  4541. }
  4542. else {
  4543. $mailboxdata['percent_class'] = "success";
  4544. }
  4545. // Determine last logins
  4546. $stmt = $pdo->prepare("SELECT MAX(`datetime`) AS `datetime`, `service` FROM `sasl_log`
  4547. WHERE `username` = :mailbox
  4548. GROUP BY `service` DESC");
  4549. $stmt->execute(array(':mailbox' => $_data));
  4550. $SaslLogsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4551. foreach ($SaslLogsData as $SaslLogs) {
  4552. if ($SaslLogs['service'] == 'imap') {
  4553. $last_imap_login = strtotime($SaslLogs['datetime']);
  4554. }
  4555. else if ($SaslLogs['service'] == 'smtp') {
  4556. $last_smtp_login = strtotime($SaslLogs['datetime']);
  4557. }
  4558. else if ($SaslLogs['service'] == 'pop3') {
  4559. $last_pop3_login = strtotime($SaslLogs['datetime']);
  4560. }
  4561. }
  4562. if (!isset($last_imap_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4563. $last_imap_login = 0;
  4564. }
  4565. if (!isset($last_smtp_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4566. $last_smtp_login = 0;
  4567. }
  4568. if (!isset($last_pop3_login) || $GLOBALS['SHOW_LAST_LOGIN'] === false) {
  4569. $last_pop3_login = 0;
  4570. }
  4571. $mailboxdata['last_imap_login'] = $last_imap_login;
  4572. $mailboxdata['last_smtp_login'] = $last_smtp_login;
  4573. $mailboxdata['last_pop3_login'] = $last_pop3_login;
  4574. if (!isset($_extra) || $_extra != 'reduced') {
  4575. $rl = ratelimit('get', 'mailbox', $_data);
  4576. $stmt = $pdo->prepare("SELECT `maxquota`, `quota` FROM `domain` WHERE `domain` = :domain");
  4577. $stmt->execute(array(':domain' => $row['domain']));
  4578. $DomainQuota = $stmt->fetch(PDO::FETCH_ASSOC);
  4579. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`active`), 0) AS `pushover_active` FROM `pushover` WHERE `username` = :username AND `active` = 1");
  4580. $stmt->execute(array(':username' => $_data));
  4581. $PushoverActive = $stmt->fetch(PDO::FETCH_ASSOC);
  4582. $stmt = $pdo->prepare("SELECT COALESCE(SUM(`quota`), 0) as `in_use` FROM `mailbox` WHERE (`kind` = '' OR `kind` = NULL) AND `domain` = :domain AND `username` != :username");
  4583. $stmt->execute(array(':domain' => $row['domain'], ':username' => $_data));
  4584. $MailboxUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4585. $stmt = $pdo->prepare("SELECT IFNULL(COUNT(`address`), 0) AS `sa_count` FROM `spamalias` WHERE `goto` = :address AND `validity` >= :unixnow");
  4586. $stmt->execute(array(':address' => $_data, ':unixnow' => time()));
  4587. $SpamaliasUsage = $stmt->fetch(PDO::FETCH_ASSOC);
  4588. $mailboxdata['max_new_quota'] = ($DomainQuota['quota'] * 1048576) - $MailboxUsage['in_use'];
  4589. $mailboxdata['spam_aliases'] = $SpamaliasUsage['sa_count'];
  4590. $mailboxdata['pushover_active'] = ($PushoverActive['pushover_active'] == 1) ? 1 : 0;
  4591. if ($mailboxdata['max_new_quota'] > ($DomainQuota['maxquota'] * 1048576)) {
  4592. $mailboxdata['max_new_quota'] = ($DomainQuota['maxquota'] * 1048576);
  4593. }
  4594. if (!empty($rl)) {
  4595. $mailboxdata['rl'] = $rl;
  4596. $mailboxdata['rl_scope'] = 'mailbox';
  4597. }
  4598. else {
  4599. $mailboxdata['rl'] = ratelimit('get', 'domain', $row['domain']);
  4600. $mailboxdata['rl_scope'] = 'domain';
  4601. }
  4602. $mailboxdata['is_relayed'] = $row['backupmx'];
  4603. }
  4604. $stmt = $pdo->prepare("SELECT `tag_name`
  4605. FROM `tags_mailbox` WHERE `username`= :username");
  4606. $stmt->execute(array(
  4607. ':username' => $_data
  4608. ));
  4609. $tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4610. while ($tag = array_shift($tags)) {
  4611. $mailboxdata['tags'][] = $tag['tag_name'];
  4612. }
  4613. return $mailboxdata;
  4614. break;
  4615. case 'mailbox_templates':
  4616. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  4617. return false;
  4618. }
  4619. $_data = (isset($_data)) ? intval($_data) : null;
  4620. if (isset($_data)){
  4621. $stmt = $pdo->prepare("SELECT * FROM `templates`
  4622. WHERE `id` = :id AND type = :type");
  4623. $stmt->execute(array(
  4624. ":id" => $_data,
  4625. ":type" => "mailbox"
  4626. ));
  4627. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4628. if (empty($row)){
  4629. return false;
  4630. }
  4631. $row["attributes"] = json_decode($row["attributes"], true);
  4632. return $row;
  4633. }
  4634. else {
  4635. $stmt = $pdo->prepare("SELECT * FROM `templates` WHERE `type` = 'mailbox'");
  4636. $stmt->execute();
  4637. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  4638. if (empty($rows)){
  4639. return false;
  4640. }
  4641. foreach($rows as $key => $row){
  4642. $rows[$key]["attributes"] = json_decode($row["attributes"], true);
  4643. }
  4644. return $rows;
  4645. }
  4646. break;
  4647. case 'resource_details':
  4648. $resourcedata = array();
  4649. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4650. return false;
  4651. }
  4652. $stmt = $pdo->prepare("SELECT
  4653. `username`,
  4654. `name`,
  4655. `kind`,
  4656. `multiple_bookings`,
  4657. `local_part`,
  4658. `active`,
  4659. `domain`
  4660. FROM `mailbox` WHERE `kind` REGEXP 'location|thing|group' AND `username` = :resource");
  4661. $stmt->execute(array(
  4662. ':resource' => $_data,
  4663. ));
  4664. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  4665. $resourcedata['name'] = $row['username'];
  4666. $resourcedata['kind'] = $row['kind'];
  4667. $resourcedata['multiple_bookings'] = $row['multiple_bookings'];
  4668. $resourcedata['description'] = $row['name'];
  4669. $resourcedata['active'] = $row['active'];
  4670. $resourcedata['active_int'] = $row['active'];
  4671. $resourcedata['domain'] = $row['domain'];
  4672. $resourcedata['local_part'] = $row['local_part'];
  4673. if (!isset($resourcedata['domain']) ||
  4674. (isset($resourcedata['domain']) && !hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $resourcedata['domain']))) {
  4675. return false;
  4676. }
  4677. return $resourcedata;
  4678. break;
  4679. case 'domain_wide_footer':
  4680. $domain = idn_to_ascii(strtolower(trim($_data)), 0, INTL_IDNA_VARIANT_UTS46);
  4681. if (!is_valid_domain_name($domain)) {
  4682. $_SESSION['return'][] = array(
  4683. 'type' => 'danger',
  4684. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4685. 'msg' => 'domain_invalid'
  4686. );
  4687. return false;
  4688. }
  4689. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {
  4690. $_SESSION['return'][] = array(
  4691. 'type' => 'danger',
  4692. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4693. 'msg' => 'access_denied'
  4694. );
  4695. return false;
  4696. }
  4697. try {
  4698. $stmt = $pdo->prepare("SELECT `html`, `plain`, `mbox_exclude`, `skip_replies` FROM `domain_wide_footer`
  4699. WHERE `domain` = :domain");
  4700. $stmt->execute(array(
  4701. ':domain' => $domain
  4702. ));
  4703. $footer = $stmt->fetch(PDO::FETCH_ASSOC);
  4704. }
  4705. catch (PDOException $e) {
  4706. $_SESSION['return'][] = array(
  4707. 'type' => 'danger',
  4708. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4709. 'msg' => $e->getMessage()
  4710. );
  4711. return false;
  4712. }
  4713. return $footer;
  4714. break;
  4715. }
  4716. break;
  4717. case 'delete':
  4718. switch ($_type) {
  4719. case 'syncjob':
  4720. if (!is_array($_data['id'])) {
  4721. $ids = array();
  4722. $ids[] = $_data['id'];
  4723. }
  4724. else {
  4725. $ids = $_data['id'];
  4726. }
  4727. if (!isset($_SESSION['acl']['syncjobs']) || $_SESSION['acl']['syncjobs'] != "1" ) {
  4728. $_SESSION['return'][] = array(
  4729. 'type' => 'danger',
  4730. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4731. 'msg' => 'access_denied'
  4732. );
  4733. return false;
  4734. }
  4735. foreach ($ids as $id) {
  4736. if (!is_numeric($id)) {
  4737. return false;
  4738. }
  4739. $stmt = $pdo->prepare("SELECT `user2` FROM `imapsync` WHERE id = :id");
  4740. $stmt->execute(array(':id' => $id));
  4741. $user2 = $stmt->fetch(PDO::FETCH_ASSOC)['user2'];
  4742. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $user2)) {
  4743. $_SESSION['return'][] = array(
  4744. 'type' => 'danger',
  4745. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4746. 'msg' => 'access_denied'
  4747. );
  4748. continue;
  4749. }
  4750. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `id`= :id");
  4751. $stmt->execute(array(':id' => $id));
  4752. $_SESSION['return'][] = array(
  4753. 'type' => 'success',
  4754. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4755. 'msg' => array('deleted_syncjob', $id)
  4756. );
  4757. }
  4758. break;
  4759. case 'filter':
  4760. if (!is_array($_data['id'])) {
  4761. $ids = array();
  4762. $ids[] = $_data['id'];
  4763. }
  4764. else {
  4765. $ids = $_data['id'];
  4766. }
  4767. if (!isset($_SESSION['acl']['filters']) || $_SESSION['acl']['filters'] != "1" ) {
  4768. $_SESSION['return'][] = array(
  4769. 'type' => 'danger',
  4770. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4771. 'msg' => 'access_denied'
  4772. );
  4773. return false;
  4774. }
  4775. foreach ($ids as $id) {
  4776. if (!is_numeric($id)) {
  4777. continue;
  4778. }
  4779. $stmt = $pdo->prepare("SELECT `username` FROM `sieve_filters` WHERE id = :id");
  4780. $stmt->execute(array(':id' => $id));
  4781. $usr = $stmt->fetch(PDO::FETCH_ASSOC)['username'];
  4782. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $usr)) {
  4783. $_SESSION['return'][] = array(
  4784. 'type' => 'danger',
  4785. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4786. 'msg' => 'access_denied'
  4787. );
  4788. continue;
  4789. }
  4790. $stmt = $pdo->prepare("DELETE FROM `sieve_filters` WHERE `id`= :id");
  4791. $stmt->execute(array(':id' => $id));
  4792. $_SESSION['return'][] = array(
  4793. 'type' => 'success',
  4794. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4795. 'msg' => array('delete_filter', $id)
  4796. );
  4797. }
  4798. break;
  4799. case 'time_limited_alias':
  4800. if (!is_array($_data['address'])) {
  4801. $addresses = array();
  4802. $addresses[] = $_data['address'];
  4803. }
  4804. else {
  4805. $addresses = $_data['address'];
  4806. }
  4807. if (!isset($_SESSION['acl']['spam_alias']) || $_SESSION['acl']['spam_alias'] != "1" ) {
  4808. $_SESSION['return'][] = array(
  4809. 'type' => 'danger',
  4810. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4811. 'msg' => 'access_denied'
  4812. );
  4813. return false;
  4814. }
  4815. foreach ($addresses as $address) {
  4816. $stmt = $pdo->prepare("SELECT `goto` FROM `spamalias` WHERE `address` = :address");
  4817. $stmt->execute(array(':address' => $address));
  4818. $goto = $stmt->fetch(PDO::FETCH_ASSOC)['goto'];
  4819. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $goto)) {
  4820. $_SESSION['return'][] = array(
  4821. 'type' => 'danger',
  4822. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4823. 'msg' => 'access_denied'
  4824. );
  4825. continue;
  4826. }
  4827. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username AND `address` = :item");
  4828. $stmt->execute(array(
  4829. ':username' => $goto,
  4830. ':item' => $address
  4831. ));
  4832. $_SESSION['return'][] = array(
  4833. 'type' => 'success',
  4834. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4835. 'msg' => array('mailbox_modified', htmlspecialchars($goto))
  4836. );
  4837. }
  4838. break;
  4839. case 'eas_cache':
  4840. if (!is_array($_data['username'])) {
  4841. $usernames = array();
  4842. $usernames[] = $_data['username'];
  4843. }
  4844. else {
  4845. $usernames = $_data['username'];
  4846. }
  4847. if (!isset($_SESSION['acl']['eas_reset']) || $_SESSION['acl']['eas_reset'] != "1" ) {
  4848. $_SESSION['return'][] = array(
  4849. 'type' => 'danger',
  4850. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4851. 'msg' => 'access_denied'
  4852. );
  4853. return false;
  4854. }
  4855. foreach ($usernames as $username) {
  4856. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4857. $_SESSION['return'][] = array(
  4858. 'type' => 'danger',
  4859. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4860. 'msg' => 'access_denied'
  4861. );
  4862. continue;
  4863. }
  4864. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4865. $stmt->execute(array(
  4866. ':username' => $username
  4867. ));
  4868. $_SESSION['return'][] = array(
  4869. 'type' => 'success',
  4870. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4871. 'msg' => array('eas_reset', htmlspecialchars($username))
  4872. );
  4873. }
  4874. break;
  4875. case 'sogo_profile':
  4876. if (!is_array($_data['username'])) {
  4877. $usernames = array();
  4878. $usernames[] = $_data['username'];
  4879. }
  4880. else {
  4881. $usernames = $_data['username'];
  4882. }
  4883. if (!isset($_SESSION['acl']['sogo_profile_reset']) || $_SESSION['acl']['sogo_profile_reset'] != "1" ) {
  4884. $_SESSION['return'][] = array(
  4885. 'type' => 'danger',
  4886. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4887. 'msg' => 'access_denied'
  4888. );
  4889. return false;
  4890. }
  4891. foreach ($usernames as $username) {
  4892. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  4893. $_SESSION['return'][] = array(
  4894. 'type' => 'danger',
  4895. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4896. 'msg' => 'access_denied'
  4897. );
  4898. continue;
  4899. }
  4900. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  4901. $stmt->execute(array(
  4902. ':username' => $username
  4903. ));
  4904. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  4905. $stmt->execute(array(
  4906. ':username' => $username
  4907. ));
  4908. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $username . "/%' OR `c_uid` = :username");
  4909. $stmt->execute(array(
  4910. ':username' => $username
  4911. ));
  4912. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  4913. $stmt->execute(array(
  4914. ':username' => $username
  4915. ));
  4916. $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)");
  4917. $stmt->execute(array(
  4918. ':username' => $username
  4919. ));
  4920. $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)");
  4921. $stmt->execute(array(
  4922. ':username' => $username
  4923. ));
  4924. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  4925. $stmt->execute(array(
  4926. ':username' => $username
  4927. ));
  4928. $_SESSION['return'][] = array(
  4929. 'type' => 'success',
  4930. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4931. 'msg' => array('sogo_profile_reset', htmlspecialchars($username))
  4932. );
  4933. }
  4934. break;
  4935. case 'domain':
  4936. if (!is_array($_data['domain'])) {
  4937. $domains = array();
  4938. $domains[] = $_data['domain'];
  4939. }
  4940. else {
  4941. $domains = $_data['domain'];
  4942. }
  4943. if ($_SESSION['mailcow_cc_role'] != "admin") {
  4944. $_SESSION['return'][] = array(
  4945. 'type' => 'danger',
  4946. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4947. 'msg' => 'access_denied'
  4948. );
  4949. return false;
  4950. }
  4951. foreach ($domains as $domain) {
  4952. if (!is_valid_domain_name($domain)) {
  4953. $_SESSION['return'][] = array(
  4954. 'type' => 'danger',
  4955. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4956. 'msg' => 'domain_invalid'
  4957. );
  4958. continue;
  4959. }
  4960. $domain = idn_to_ascii(strtolower(trim($domain)), 0, INTL_IDNA_VARIANT_UTS46);
  4961. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  4962. WHERE `domain` = :domain");
  4963. $stmt->execute(array(':domain' => $domain));
  4964. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  4965. if ($num_results != 0 || !empty($num_results)) {
  4966. $_SESSION['return'][] = array(
  4967. 'type' => 'danger',
  4968. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4969. 'msg' => array('domain_not_empty', $domain)
  4970. );
  4971. continue;
  4972. }
  4973. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $domain);
  4974. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  4975. if ($maildir_gc['type'] != 'success') {
  4976. $_SESSION['return'][] = array(
  4977. 'type' => 'warning',
  4978. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  4979. 'msg' => 'Could not move mail storage to garbage collector: ' . $maildir_gc['msg']
  4980. );
  4981. }
  4982. $stmt = $pdo->prepare("DELETE FROM `domain` WHERE `domain` = :domain");
  4983. $stmt->execute(array(
  4984. ':domain' => $domain,
  4985. ));
  4986. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `domain` = :domain");
  4987. $stmt->execute(array(
  4988. ':domain' => $domain,
  4989. ));
  4990. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :domain");
  4991. $stmt->execute(array(
  4992. ':domain' => $domain,
  4993. ));
  4994. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `target_domain` = :domain");
  4995. $stmt->execute(array(
  4996. ':domain' => $domain,
  4997. ));
  4998. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `domain` = :domain");
  4999. $stmt->execute(array(
  5000. ':domain' => $domain,
  5001. ));
  5002. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` LIKE :domain");
  5003. $stmt->execute(array(
  5004. ':domain' => '%@'.$domain,
  5005. ));
  5006. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` LIKE :domain");
  5007. $stmt->execute(array(
  5008. ':domain' => '%@'.$domain,
  5009. ));
  5010. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` LIKE :domain");
  5011. $stmt->execute(array(
  5012. ':domain' => '%@'.$domain,
  5013. ));
  5014. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` LIKE :domain");
  5015. $stmt->execute(array(
  5016. ':domain' => '%@'.$domain,
  5017. ));
  5018. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5019. $stmt->execute(array(
  5020. ':domain' => '%@'.$domain,
  5021. ));
  5022. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :domain");
  5023. $stmt->execute(array(
  5024. ':domain' => $domain,
  5025. ));
  5026. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :domain");
  5027. $stmt->execute(array(
  5028. ':domain' => $domain,
  5029. ));
  5030. $stmt = $pdo->query("DELETE FROM `admin` WHERE `superadmin` = 0 AND `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5031. $stmt = $pdo->query("DELETE FROM `da_acl` WHERE `username` NOT IN (SELECT `username`FROM `domain_admins`);");
  5032. try {
  5033. $redis->hDel('DOMAIN_MAP', $domain);
  5034. $redis->hDel('RL_VALUE', $domain);
  5035. }
  5036. catch (RedisException $e) {
  5037. $_SESSION['return'][] = array(
  5038. 'type' => 'danger',
  5039. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5040. 'msg' => array('redis_error', $e)
  5041. );
  5042. continue;
  5043. }
  5044. $_SESSION['return'][] = array(
  5045. 'type' => 'success',
  5046. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5047. 'msg' => array('domain_removed', htmlspecialchars($domain))
  5048. );
  5049. }
  5050. break;
  5051. case 'domain_templates':
  5052. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5053. $_SESSION['return'][] = array(
  5054. 'type' => 'danger',
  5055. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5056. 'msg' => 'access_denied'
  5057. );
  5058. return false;
  5059. }
  5060. if (!is_array($_data['ids'])) {
  5061. $ids = array();
  5062. $ids[] = $_data['ids'];
  5063. }
  5064. else {
  5065. $ids = $_data['ids'];
  5066. }
  5067. foreach ($ids as $id) {
  5068. // delete template
  5069. $stmt = $pdo->prepare("DELETE FROM `templates`
  5070. WHERE id = :id AND type = :type AND NOT template = :template");
  5071. $stmt->execute(array(
  5072. ":id" => $id,
  5073. ":type" => "domain",
  5074. ":template" => "Default"
  5075. ));
  5076. $_SESSION['return'][] = array(
  5077. 'type' => 'success',
  5078. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5079. 'msg' => array('template_removed', htmlspecialchars($id))
  5080. );
  5081. return true;
  5082. }
  5083. break;
  5084. case 'alias':
  5085. if (!is_array($_data['id'])) {
  5086. $ids = array();
  5087. $ids[] = $_data['id'];
  5088. }
  5089. else {
  5090. $ids = $_data['id'];
  5091. }
  5092. foreach ($ids as $id) {
  5093. $alias_data = mailbox('get', 'alias_details', $id);
  5094. if (empty($alias_data)) {
  5095. $_SESSION['return'][] = array(
  5096. 'type' => 'danger',
  5097. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5098. 'msg' => 'access_denied'
  5099. );
  5100. continue;
  5101. }
  5102. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `id` = :id");
  5103. $stmt->execute(array(
  5104. ':id' => $alias_data['id']
  5105. ));
  5106. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `send_as` = :alias_address");
  5107. $stmt->execute(array(
  5108. ':alias_address' => $alias_data['address']
  5109. ));
  5110. $_SESSION['return'][] = array(
  5111. 'type' => 'success',
  5112. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5113. 'msg' => array('alias_removed', htmlspecialchars($alias_data['address']))
  5114. );
  5115. }
  5116. break;
  5117. case 'alias_domain':
  5118. if (!is_array($_data['alias_domain'])) {
  5119. $alias_domains = array();
  5120. $alias_domains[] = $_data['alias_domain'];
  5121. }
  5122. else {
  5123. $alias_domains = $_data['alias_domain'];
  5124. }
  5125. foreach ($alias_domains as $alias_domain) {
  5126. if (!is_valid_domain_name($alias_domain)) {
  5127. $_SESSION['return'][] = array(
  5128. 'type' => 'danger',
  5129. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5130. 'msg' => 'domain_invalid'
  5131. );
  5132. continue;
  5133. }
  5134. $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain`
  5135. WHERE `alias_domain`= :alias_domain");
  5136. $stmt->execute(array(':alias_domain' => $alias_domain));
  5137. $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
  5138. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $DomainData['target_domain'])) {
  5139. $_SESSION['return'][] = array(
  5140. 'type' => 'danger',
  5141. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5142. 'msg' => 'access_denied'
  5143. );
  5144. continue;
  5145. }
  5146. $stmt = $pdo->prepare("DELETE FROM `alias_domain` WHERE `alias_domain` = :alias_domain");
  5147. $stmt->execute(array(
  5148. ':alias_domain' => $alias_domain,
  5149. ));
  5150. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `domain` = :alias_domain");
  5151. $stmt->execute(array(
  5152. ':alias_domain' => $alias_domain,
  5153. ));
  5154. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `address` LIKE :domain");
  5155. $stmt->execute(array(
  5156. ':domain' => '%@'.$alias_domain,
  5157. ));
  5158. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :alias_domain");
  5159. $stmt->execute(array(
  5160. ':alias_domain' => $alias_domain,
  5161. ));
  5162. try {
  5163. $redis->hDel('DOMAIN_MAP', $alias_domain);
  5164. $redis->hDel('RL_VALUE', $domain);
  5165. }
  5166. catch (RedisException $e) {
  5167. $_SESSION['return'][] = array(
  5168. 'type' => 'danger',
  5169. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5170. 'msg' => array('redis_error', $e)
  5171. );
  5172. continue;
  5173. }
  5174. $_SESSION['return'][] = array(
  5175. 'type' => 'success',
  5176. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5177. 'msg' => array('alias_domain_removed', htmlspecialchars($alias_domain))
  5178. );
  5179. }
  5180. break;
  5181. case 'mailbox':
  5182. if (!is_array($_data['username'])) {
  5183. $usernames = array();
  5184. $usernames[] = $_data['username'];
  5185. }
  5186. else {
  5187. $usernames = $_data['username'];
  5188. }
  5189. foreach ($usernames as $username) {
  5190. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5191. $_SESSION['return'][] = array(
  5192. 'type' => 'danger',
  5193. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5194. 'msg' => 'access_denied'
  5195. );
  5196. continue;
  5197. }
  5198. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $username)) {
  5199. $_SESSION['return'][] = array(
  5200. 'type' => 'danger',
  5201. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5202. 'msg' => 'access_denied'
  5203. );
  5204. continue;
  5205. }
  5206. $mailbox_details = mailbox('get', 'mailbox_details', $username);
  5207. if (!empty($mailbox_details['domain']) && !empty($mailbox_details['local_part'])) {
  5208. $maildir = $mailbox_details['domain'] . '/' . $mailbox_details['local_part'];
  5209. $exec_fields = array('cmd' => 'maildir', 'task' => 'cleanup', 'maildir' => $maildir);
  5210. if (getenv("CLUSTERMODE") == "replication") {
  5211. // broadcast to each dovecot container
  5212. docker('broadcast', 'dovecot-mailcow', 'exec', $exec_fields);
  5213. } else {
  5214. $maildir_gc = json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true);
  5215. if ($maildir_gc['type'] != 'success') {
  5216. $_SESSION['return'][] = array(
  5217. 'type' => 'warning',
  5218. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5219. 'msg' => 'Could not move maildir to garbage collector: ' . $maildir_gc['msg']
  5220. );
  5221. }
  5222. }
  5223. }
  5224. else {
  5225. $_SESSION['return'][] = array(
  5226. 'type' => 'warning',
  5227. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5228. 'msg' => 'Could not move maildir to garbage collector: variables local_part and/or domain empty'
  5229. );
  5230. }
  5231. if (strtolower(getenv('SKIP_SOLR')) == 'n') {
  5232. $curl = curl_init();
  5233. curl_setopt($curl, CURLOPT_URL, 'http://solr:8983/solr/dovecot-fts/update?commit=true');
  5234. curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
  5235. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  5236. curl_setopt($curl, CURLOPT_POST, 1);
  5237. curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
  5238. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  5239. $response = curl_exec($curl);
  5240. if ($response === false) {
  5241. $err = curl_error($curl);
  5242. $_SESSION['return'][] = array(
  5243. 'type' => 'warning',
  5244. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5245. 'msg' => 'Could not remove Solr index: ' . print_r($err, true)
  5246. );
  5247. }
  5248. curl_close($curl);
  5249. }
  5250. $stmt = $pdo->prepare("DELETE FROM `alias` WHERE `goto` = :username");
  5251. $stmt->execute(array(
  5252. ':username' => $username
  5253. ));
  5254. $stmt = $pdo->prepare("DELETE FROM `pushover` WHERE `username` = :username");
  5255. $stmt->execute(array(
  5256. ':username' => $username
  5257. ));
  5258. $stmt = $pdo->prepare("DELETE FROM `quarantine` WHERE `rcpt` = :username");
  5259. $stmt->execute(array(
  5260. ':username' => $username
  5261. ));
  5262. $stmt = $pdo->prepare("DELETE FROM `quota2` WHERE `username` = :username");
  5263. $stmt->execute(array(
  5264. ':username' => $username
  5265. ));
  5266. $stmt = $pdo->prepare("DELETE FROM `quota2replica` WHERE `username` = :username");
  5267. $stmt->execute(array(
  5268. ':username' => $username
  5269. ));
  5270. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5271. $stmt->execute(array(
  5272. ':username' => $username
  5273. ));
  5274. $stmt = $pdo->prepare("DELETE FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as OR `send_as` = :send_as");
  5275. $stmt->execute(array(
  5276. ':logged_in_as' => $username,
  5277. ':send_as' => $username
  5278. ));
  5279. // fk, better safe than sorry
  5280. $stmt = $pdo->prepare("DELETE FROM `user_acl` WHERE `username` = :username");
  5281. $stmt->execute(array(
  5282. ':username' => $username
  5283. ));
  5284. $stmt = $pdo->prepare("DELETE FROM `spamalias` WHERE `goto` = :username");
  5285. $stmt->execute(array(
  5286. ':username' => $username
  5287. ));
  5288. $stmt = $pdo->prepare("DELETE FROM `imapsync` WHERE `user2` = :username");
  5289. $stmt->execute(array(
  5290. ':username' => $username
  5291. ));
  5292. $stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username");
  5293. $stmt->execute(array(
  5294. ':username' => $username
  5295. ));
  5296. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5297. $stmt->execute(array(
  5298. ':username' => $username
  5299. ));
  5300. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5301. $stmt->execute(array(
  5302. ':username' => $username
  5303. ));
  5304. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . str_replace('%', '\%', $username) . "/%' OR `c_uid` = :username");
  5305. $stmt->execute(array(
  5306. ':username' => $username
  5307. ));
  5308. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5309. $stmt->execute(array(
  5310. ':username' => $username
  5311. ));
  5312. $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)");
  5313. $stmt->execute(array(
  5314. ':username' => $username
  5315. ));
  5316. $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)");
  5317. $stmt->execute(array(
  5318. ':username' => $username
  5319. ));
  5320. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5321. $stmt->execute(array(
  5322. ':username' => $username
  5323. ));
  5324. $stmt = $pdo->prepare("DELETE FROM `bcc_maps` WHERE `local_dest` = :username");
  5325. $stmt->execute(array(
  5326. ':username' => $username
  5327. ));
  5328. $stmt = $pdo->prepare("DELETE FROM `oauth_access_tokens` WHERE `user_id` = :username");
  5329. $stmt->execute(array(
  5330. ':username' => $username
  5331. ));
  5332. $stmt = $pdo->prepare("DELETE FROM `oauth_refresh_tokens` WHERE `user_id` = :username");
  5333. $stmt->execute(array(
  5334. ':username' => $username
  5335. ));
  5336. $stmt = $pdo->prepare("DELETE FROM `oauth_authorization_codes` WHERE `user_id` = :username");
  5337. $stmt->execute(array(
  5338. ':username' => $username
  5339. ));
  5340. $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
  5341. $stmt->execute(array(
  5342. ':username' => $username,
  5343. ));
  5344. $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
  5345. $stmt->execute(array(
  5346. ':username' => $username,
  5347. ));
  5348. $stmt = $pdo->prepare("SELECT `address`, `goto` FROM `alias`
  5349. WHERE `goto` REGEXP :username");
  5350. $stmt->execute(array(':username' => '(^|,)'.$username.'($|,)'));
  5351. $GotoData = $stmt->fetchAll(PDO::FETCH_ASSOC);
  5352. foreach ($GotoData as $gotos) {
  5353. $goto_exploded = explode(',', $gotos['goto']);
  5354. if (($key = array_search($username, $goto_exploded)) !== false) {
  5355. unset($goto_exploded[$key]);
  5356. }
  5357. $gotos_rebuild = implode(',', (array)$goto_exploded);
  5358. $stmt = $pdo->prepare("UPDATE `alias` SET
  5359. `goto` = :goto
  5360. WHERE `address` = :address");
  5361. $stmt->execute(array(
  5362. ':goto' => $gotos_rebuild,
  5363. ':address' => $gotos['address']
  5364. ));
  5365. }
  5366. try {
  5367. $redis->hDel('RL_VALUE', $username);
  5368. }
  5369. catch (RedisException $e) {
  5370. $_SESSION['return'][] = array(
  5371. 'type' => 'danger',
  5372. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5373. 'msg' => array('redis_error', $e)
  5374. );
  5375. continue;
  5376. }
  5377. update_sogo_static_view($username);
  5378. $_SESSION['return'][] = array(
  5379. 'type' => 'success',
  5380. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5381. 'msg' => array('mailbox_removed', htmlspecialchars($username))
  5382. );
  5383. }
  5384. return true;
  5385. break;
  5386. case 'mailbox_templates':
  5387. if ($_SESSION['mailcow_cc_role'] != "admin") {
  5388. $_SESSION['return'][] = array(
  5389. 'type' => 'danger',
  5390. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5391. 'msg' => 'access_denied'
  5392. );
  5393. return false;
  5394. }
  5395. if (!is_array($_data['ids'])) {
  5396. $ids = array();
  5397. $ids[] = $_data['ids'];
  5398. }
  5399. else {
  5400. $ids = $_data['ids'];
  5401. }
  5402. foreach ($ids as $id) {
  5403. // delete template
  5404. $stmt = $pdo->prepare("DELETE FROM `templates`
  5405. WHERE id = :id AND type = :type AND NOT template = :template");
  5406. $stmt->execute(array(
  5407. ":id" => $id,
  5408. ":type" => "mailbox",
  5409. ":template" => "Default"
  5410. ));
  5411. }
  5412. $_SESSION['return'][] = array(
  5413. 'type' => 'success',
  5414. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5415. 'msg' => 'template_removed'
  5416. );
  5417. return true;
  5418. break;
  5419. case 'resource':
  5420. if (!is_array($_data['name'])) {
  5421. $names = array();
  5422. $names[] = $_data['name'];
  5423. }
  5424. else {
  5425. $names = $_data['name'];
  5426. }
  5427. foreach ($names as $name) {
  5428. if (!filter_var($name, FILTER_VALIDATE_EMAIL)) {
  5429. $_SESSION['return'][] = array(
  5430. 'type' => 'danger',
  5431. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5432. 'msg' => 'access_denied'
  5433. );
  5434. continue;
  5435. }
  5436. if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $name)) {
  5437. $_SESSION['return'][] = array(
  5438. 'type' => 'danger',
  5439. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5440. 'msg' => 'access_denied'
  5441. );
  5442. continue;
  5443. }
  5444. $stmt = $pdo->prepare("DELETE FROM `mailbox` WHERE `username` = :username");
  5445. $stmt->execute(array(
  5446. ':username' => $name
  5447. ));
  5448. $stmt = $pdo->prepare("DELETE FROM `sogo_user_profile` WHERE `c_uid` = :username");
  5449. $stmt->execute(array(
  5450. ':username' => $name
  5451. ));
  5452. $stmt = $pdo->prepare("DELETE FROM `sogo_cache_folder` WHERE `c_uid` = :username");
  5453. $stmt->execute(array(
  5454. ':username' => $name
  5455. ));
  5456. $stmt = $pdo->prepare("DELETE FROM `sogo_acl` WHERE `c_object` LIKE '%/" . $name . "/%' OR `c_uid` = :username");
  5457. $stmt->execute(array(
  5458. ':username' => $name
  5459. ));
  5460. $stmt = $pdo->prepare("DELETE FROM `sogo_store` WHERE `c_folder_id` IN (SELECT `c_folder_id` FROM `sogo_folder_info` WHERE `c_path2` = :username)");
  5461. $stmt->execute(array(
  5462. ':username' => $name
  5463. ));
  5464. $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)");
  5465. $stmt->execute(array(
  5466. ':username' => $name
  5467. ));
  5468. $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)");
  5469. $stmt->execute(array(
  5470. ':username' => $name
  5471. ));
  5472. $stmt = $pdo->prepare("DELETE FROM `sogo_folder_info` WHERE `c_path2` = :username");
  5473. $stmt->execute(array(
  5474. ':username' => $name
  5475. ));
  5476. $_SESSION['return'][] = array(
  5477. 'type' => 'success',
  5478. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5479. 'msg' => array('resource_removed', htmlspecialchars($name))
  5480. );
  5481. }
  5482. break;
  5483. case 'tags_domain':
  5484. if (!is_array($_data['domain'])) {
  5485. $domains = array();
  5486. $domains[] = $_data['domain'];
  5487. }
  5488. else {
  5489. $domains = $_data['domain'];
  5490. }
  5491. $tags = $_data['tags'];
  5492. if (!is_array($tags)) $tags = array();
  5493. $wasModified = false;
  5494. foreach ($domains as $domain) {
  5495. if (!is_valid_domain_name($domain)) {
  5496. $_SESSION['return'][] = array(
  5497. 'type' => 'danger',
  5498. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5499. 'msg' => 'domain_invalid'
  5500. );
  5501. continue;
  5502. }
  5503. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5504. $_SESSION['return'][] = array(
  5505. 'type' => 'danger',
  5506. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5507. 'msg' => 'access_denied'
  5508. );
  5509. return false;
  5510. }
  5511. foreach($tags as $tag){
  5512. // delete tag
  5513. $wasModified = true;
  5514. $stmt = $pdo->prepare("DELETE FROM `tags_domain` WHERE `domain` = :domain AND `tag_name` = :tag_name");
  5515. $stmt->execute(array(
  5516. ':domain' => $domain,
  5517. ':tag_name' => $tag,
  5518. ));
  5519. }
  5520. }
  5521. if (!$wasModified) return false;
  5522. $_SESSION['return'][] = array(
  5523. 'type' => 'success',
  5524. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5525. 'msg' => array('domain_modified', $domain)
  5526. );
  5527. break;
  5528. case 'tags_mailbox':
  5529. if (!is_array($_data['username'])) {
  5530. $usernames = array();
  5531. $usernames[] = $_data['username'];
  5532. }
  5533. else {
  5534. $usernames = $_data['username'];
  5535. }
  5536. $tags = $_data['tags'];
  5537. if (!is_array($tags)) $tags = array();
  5538. $wasModified = false;
  5539. foreach ($usernames as $username) {
  5540. if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
  5541. $_SESSION['return'][] = array(
  5542. 'type' => 'danger',
  5543. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5544. 'msg' => 'email invalid'
  5545. );
  5546. continue;
  5547. }
  5548. $is_now = mailbox('get', 'mailbox_details', $username);
  5549. $domain = $is_now['domain'];
  5550. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  5551. $_SESSION['return'][] = array(
  5552. 'type' => 'danger',
  5553. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5554. 'msg' => 'access_denied'
  5555. );
  5556. continue;
  5557. }
  5558. // delete tags
  5559. foreach($tags as $tag){
  5560. $wasModified = true;
  5561. $stmt = $pdo->prepare("DELETE FROM `tags_mailbox` WHERE `username` = :username AND `tag_name` = :tag_name");
  5562. $stmt->execute(array(
  5563. ':username' => $username,
  5564. ':tag_name' => $tag,
  5565. ));
  5566. }
  5567. }
  5568. if (!$wasModified) return false;
  5569. $_SESSION['return'][] = array(
  5570. 'type' => 'success',
  5571. 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
  5572. 'msg' => array('mailbox_modified', $username)
  5573. );
  5574. break;
  5575. }
  5576. break;
  5577. }
  5578. if ($_action != 'get' && in_array($_type, array('domain', 'alias', 'alias_domain', 'resource')) && getenv('SKIP_SOGO') != "y") {
  5579. update_sogo_static_view();
  5580. }
  5581. return true;
  5582. }