functions.mailbox.inc.php 250 KB

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