functions.mailbox.inc.php 247 KB

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