functions.mailbox.inc.php 289 KB

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