functions.mailbox.inc.php 253 KB

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