functions.mailbox.inc.php 204 KB

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