functions.mailbox.inc.php 263 KB

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