functions.mailbox.inc.php 247 KB

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