functions.mailbox.inc.php 264 KB

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