functions.mailbox.inc.php 249 KB

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