functions.mailbox.inc.php 272 KB

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