functions.mailbox.inc.php 278 KB

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