functions.mailbox.inc.php 263 KB

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