functions.mailbox.inc.php 269 KB

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