functions.mailbox.inc.php 272 KB

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