functions.mailbox.inc.php 279 KB

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