functions.mailbox.inc.php 215 KB

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