functions.mailbox.inc.php 218 KB

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