functions.mailbox.inc.php 261 KB

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