functions.mailbox.inc.php 262 KB

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