functions.mailbox.inc.php 216 KB

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