BxMessengerModule.php 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Messenger Messenger
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxMessengerStorage.php');
  12. define('BX_IM_TYPE_PUBLIC', 1);
  13. define('BX_IM_TYPE_PRIVATE', 2);
  14. define('BX_IM_TYPE_SETS', 3);
  15. define('BX_IM_TYPE_GROUPS', 4);
  16. define('BX_IM_TYPE_EVENTS', 5);
  17. define('BX_IM_TYPE_BROADCAST', 6);
  18. define('BX_IM_EMPTY_URL', '');
  19. define('BX_IM_EMPTY', 0);
  20. // Attachment types
  21. define('BX_ATT_TYPE_FILES', 'files');
  22. define('BX_ATT_TYPE_FILES_UPLOADING', 'uploading');
  23. define('BX_ATT_TYPE_GIPHY', 'giphy');
  24. define('BX_ATT_TYPE_REPOST', 'repost');
  25. define('BX_ATT_TYPE_CUSTOM', 'custom');
  26. define('BX_ATT_TYPE_REPLY', 'reply');
  27. define('BX_ATT_TYPE_VC', 'vc'); // video conference
  28. define('BX_ATT_GROUPS_ATTACH', 'attachment');
  29. // Reactions actions
  30. define('BX_JOT_REACTION_ADD', 'add');
  31. define('BX_JOT_REACTION_REMOVE', 'remove');
  32. // Public jitsi actions
  33. define('BX_JOT_PUBLIC_JITSI_LEAVE', 'leave');
  34. define('BX_JOT_PUBLIC_JITSI_JOIN', 'join');
  35. define('BX_JOT_PUBLIC_JITSI_CLOSE', 'close');
  36. // Notifications types
  37. define('BX_MSG_NTFS_MESSAGE', 'message');
  38. define('BX_MSG_NTFS_MENTION', 'mention');
  39. define('BX_MSG_NTFS_BROADCAST', 'broadcast');
  40. // Membership actions
  41. define('BX_MSG_ACTION_EDIT_MESSAGES', 'edit_messages');
  42. define('BX_MSG_ACTION_DELETE_MESSAGES', 'delete_messages');
  43. define('BX_MSG_ACTION_CREATE_TALKS', 'create_talks');
  44. define('BX_MSG_ACTION_ADMINISTRATE_TALKS', 'administrate_talks');
  45. define('BX_MSG_ACTION_SEND_MESSAGE', 'send_messages');
  46. define('BX_MSG_ACTION_SEND_FIELS', 'send_files');
  47. define('BX_MSG_ACTION_CREATE_VC', 'create_vc');
  48. define('BX_MSG_ACTION_CREATE_IM_VC', 'video_conference');
  49. define('BX_MSG_ACTION_VIDEO_RECORDER', 'video_recorder');
  50. define('BX_MSG_ACTION_JOIN_IM_VC', 'join_personal_vc');
  51. define('BX_MSG_ACTION_JOIN_TALK_VC', 'join_vc');
  52. define('BX_MSG_ACTION_CREATE_GROUPS', 'create_groups');
  53. define('BX_MSG_ACTION_CREATE_BROADCASTS', 'create_broadcasts');
  54. // Lot settings
  55. define('BX_MSG_SETTING_MSG', 'msg'); // allow to send messages
  56. define('BX_MSG_SETTING_GIPHY', 'giphy'); // allow to send giphy
  57. define('BX_MSG_SETTING_FILES', 'files'); // allow to send files
  58. define('BX_MSG_SETTING_VIDEO_RECORD', 'video_rec'); // allow to record videos
  59. define('BX_MSG_SETTING_SMILES', 'smiles'); // allow to record videos
  60. // Talk's types
  61. define('BX_MSG_TALK_TYPE_INBOX', 'inbox');
  62. define('BX_MSG_TALK_TYPE_THREADS', 'threads');
  63. define('BX_MSG_TALK_TYPE_GROUPS', 'groups');
  64. define('BX_MSG_TALK_TYPE_DIRECT', 'direct');
  65. define('BX_MSG_TALK_TYPE_FILES', 'files');
  66. define('BX_MSG_TALK_TYPE_MR', 'mr');
  67. define('BX_MSG_TALK_TYPE_REPLIES', 'replies');
  68. define('BX_MSG_TALK_TYPE_SAVED', 'saved');
  69. define('BX_MSG_TALK_TYPE_FAVORITES', 'favorites');
  70. define('BX_MSG_TALK_TYPE_BROADCAST', 'broadcast');
  71. // Pages types
  72. define('BX_MSG_TALK_TYPE_PAGES', 'bx_messenger_pages');
  73. // Search criteria
  74. define('BX_SEARCH_CRITERIA_TITLES', 'titles');
  75. define('BX_SEARCH_CRITERIA_PARTS', 'participants');
  76. define('BX_SEARCH_CRITERIA_CONTENT', 'content');
  77. // Visibility
  78. define('BX_MSG_VISIBILITY_VISIBLE', 0);
  79. define('BX_MSG_VISIBILITY_HIDDEN', 1);
  80. define('BX_MSG_VISIBILITY_ALL', 2);
  81. // Talks classes
  82. define('BX_MSG_TALK_CLASS_MEMBERS', 'members');
  83. define('BX_MSG_TALK_CLASS_MARKET', 'market');
  84. define('BX_MSG_TALK_CLASS_CUSTOM', 'custom');
  85. define('BX_MSG_TALK_CLASS_BLOCK', 'block');
  86. // Classes
  87. define('BX_MSG_CLASS_ATTACHMENT', 'message-attachment');
  88. /**
  89. * Messenger module
  90. */
  91. class BxMessengerModule extends BxBaseModGeneralModule
  92. {
  93. const BROADCAST_BATCH_SIZE = 200;
  94. const BROADCAST_BATCHES_PER_RUN = 5;
  95. private $_iUserId = 0;
  96. private $_iJotId = 0;
  97. private $_iSelectedProfileId = 0;
  98. private $_iSelectedConvoId = 0;
  99. private $_isBlockMessenger = false;
  100. private $_sWelcomeMessage = '';
  101. function __construct(&$aModule)
  102. {
  103. parent::__construct($aModule);
  104. $this->_iUserId = bx_get_logged_profile_id();
  105. }
  106. public function setProfileId($iProfileId)
  107. {
  108. $iPrev = (int)$this->_iProfileId;
  109. $this->_iProfileId = (int)$iProfileId;
  110. $this->_iUserId = (int)$iProfileId;
  111. return $iPrev;
  112. }
  113. /**
  114. * Returns left side block for messenger page and loads config data
  115. */
  116. public function serviceGetBlockInbox()
  117. {
  118. $mixedContent = $this->_oTemplate->getLotsList($this->_iProfileId, $this->_iSelectedConvoId);
  119. if (bx_is_api())
  120. return $mixedContent;
  121. return $mixedContent . $this->_oTemplate->loadConfig(
  122. $this->_iProfileId,
  123. [
  124. 'is_block_version' => false,
  125. 'lot' => $this->_iSelectedConvoId,
  126. 'jot' => $this->_iJotId,
  127. 'selected_profile' => $this->_iSelectedProfileId,
  128. 'welcome' => $this->_sWelcomeMessage
  129. ]
  130. );
  131. }
  132. /**
  133. * Returns right side block for messenger page
  134. */
  135. public function serviceGetBlockLot()
  136. {
  137. if ($this->_iSelectedConvoId) {
  138. return $this->_oTemplate->getTalkBlock($this->_iProfileId, $this->_iSelectedConvoId, $this->_iJotId);
  139. } else if ($this->_iSelectedProfileId && $this->onCheckContact($this->_iProfileId, $this->_iSelectedProfileId))
  140. return $this->_oTemplate->getTalkBlockByUserName($this->_iProfileId, $this->_iSelectedProfileId);
  141. return $this->_oTemplate->getTalkBlock($this->_iProfileId, BX_IM_EMPTY);
  142. }
  143. /**
  144. * Returns block with messenger for any page
  145. * @param string $sModule module name
  146. * @return string block's content
  147. */
  148. public function serviceGetBlockMessenger($sModule, $bAsHtml = false)
  149. {
  150. $this->_isBlockMessenger = true;
  151. $CNF = &$this->_oConfig->CNF;
  152. $this->_oTemplate->loadCssJs('view');
  153. $sClass = str_replace('{type}', '', $sModule);
  154. $aLotInfo = [];
  155. if ($sClass)
  156. $aLotInfo = $this->_oDb->getLotByClass($sClass);
  157. $sUrl = $this->_oConfig->getPageIdent();
  158. $iType = $this->_oConfig->getTalkType($sModule);
  159. if (empty($aLotInfo) && $sUrl)
  160. $aLotInfo = $this->_oDb->findLotByParams(array(
  161. $CNF['FIELD_TYPE'] => $iType,
  162. $CNF['FIELD_CLASS'] => BX_MSG_TALK_CLASS_CUSTOM,
  163. $CNF['FIELD_URL'] => $sUrl
  164. ));
  165. $iLotId = !empty($aLotInfo) && isset($aLotInfo[$CNF['FIELD_ID']]) ? (int) $aLotInfo[$CNF['FIELD_ID']] : 0;
  166. if (!$iLotId) {
  167. if (!$sUrl)
  168. return false;
  169. $sTalkUrl = $this->getPreparedUrl($sUrl);
  170. $sTalkTitle = BxDolTemplate::getInstance()->getPageHeader();
  171. $iLotId = $this->_oDb->createLot(
  172. $this->_iProfileId,
  173. array(
  174. 'url' => $sTalkUrl,
  175. 'title' => $sTalkTitle,
  176. 'type' => $iType,
  177. 'page' => $this->_oConfig->getPageName($sUrl)
  178. )
  179. );
  180. $this->_oDb->registerGroup($sUrl, $iLotId);
  181. }
  182. $sConfig = $this->_oTemplate->loadConfig($this->_iProfileId, ['lot' => $iLotId, 'type' => $iType]);
  183. $aHeader = $this->_oTemplate->getTalkHeader($iLotId, $this->_iProfileId, true, true);
  184. $sContent = $this->_oTemplate->parseHtmlByName('talk-body.html', array(
  185. 'history' => $this->_oTemplate->getHistory($this->_iProfileId, $iLotId, BX_IM_EMPTY),
  186. 'text_area' => $this->_oTemplate->getTextArea($this->_iProfileId, $iLotId),
  187. 'groups_list' => $this->_oTemplate->getTalksList($iLotId),
  188. 'info' => $this->_oTemplate->getInfoSection($iLotId, $this->_iProfileId),
  189. ));
  190. $this->_oTemplate->addCss(array('threads.css'));
  191. if ($bAsHtml) {
  192. $sContent = $this->_oTemplate->getTalkBlock($this->_iProfileId, $iLotId, BX_IM_EMPTY, $this->_isBlockMessenger);
  193. return $sConfig . $sContent;
  194. }
  195. $oMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_TALK_MENU']);
  196. $oMenu->setContentId($iLotId);
  197. return array(
  198. 'title' => $aHeader['title'],
  199. 'content' => $sConfig . $sContent,
  200. 'menu' => $oMenu
  201. );
  202. }
  203. private function initDefaultParams()
  204. {
  205. $CNF = &$this->_oConfig->CNF;
  206. $this->_iSelectedProfileId = (int) bx_get('profile_id');
  207. if ($this->_iSelectedProfileId && !$this->onCheckContact($this->_iProfileId, $this->_iSelectedProfileId))
  208. $this->_iSelectedProfileId = 0;
  209. if ($this->_iSelectedProfileId) {
  210. $oProfile = BxDolProfile::getInstance($this->_iSelectedProfileId);
  211. $sModule = $oProfile->getModule();
  212. $bIsProfile = BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile');
  213. if ($sModule && !$bIsProfile && BxDolRequest::serviceExists($sModule, 'is_group_profile') && BxDolService::call($sModule, 'is_group_profile')) {
  214. $aOwnerInfo = BxDolService::call($sModule, 'get_info', array($oProfile->getContentId(), false));
  215. if (!empty($aOwnerInfo) && is_array($aOwnerInfo) && BxDolService::call($sModule, 'check_allowed_view_for_profile', array($aOwnerInfo)) === CHECK_ACTION_RESULT_ALLOWED) {
  216. $oModule = BxDolModule::getInstance($sModule);
  217. if ($oModule->_oConfig) {
  218. $oMCNF = $oModule->_oConfig->CNF;
  219. $sUrl = "i={$oMCNF['URI_VIEW_ENTRY']}&id=" . $oProfile->getContentId();
  220. if ($sUrl && $aTalk = $this->_oDb->getLotByUrl($sUrl))
  221. $this->_iSelectedConvoId = $aTalk[$CNF['FIELD_ID']];
  222. }
  223. }
  224. } else {
  225. $aExistedTalk = $this->_oDb->getLotsByParticipantsList([$this->_iSelectedProfileId, $this->_iProfileId], BX_IM_TYPE_PRIVATE);
  226. if (!empty($aExistedTalk))
  227. $this->_iSelectedConvoId = $aExistedTalk[$CNF['FIELD_ID']];
  228. }
  229. } else {
  230. if ($this->_iJotId)
  231. $this->_iSelectedConvoId = $this->_oDb->getLotByJotId($this->_iJotId);
  232. else {
  233. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, ['inbox' => true]);
  234. $this->_iSelectedConvoId = !empty($aLotsList) ? current($aLotsList)[$CNF['FIELD_ID']] : 0;
  235. }
  236. }
  237. }
  238. public function serviceGetMainMessengerPage()
  239. {
  240. if (bx_is_api())
  241. return bx_srv($this->getName(), 'get_block_main', [], 'Services');
  242. $this->initDefaultParams();
  243. $bSimpleMode = $this->_oConfig->CNF['USE-UNIQUE-MODE'];
  244. $aData = [
  245. 'menu' => $this->_oTemplate->getLeftMainMenu(),
  246. 'info' => $this->_oTemplate->getInfoSection(),
  247. 'list' => $this->serviceGetBlockInbox(),
  248. 'history' => $this->serviceGetBlockLot(),
  249. 'list_width' => $bSimpleMode ? 'xl:col-span-3' : 'xl:col-span-3',
  250. 'history_width' => $bSimpleMode ? 'xl:col-span-7' : 'xl:col-span-5',
  251. ];
  252. return $this->_oTemplate->parseHtmlByName('main.html', $aData);
  253. }
  254. public function actionGetThreadReplies()
  255. {
  256. $iJotId = (int) bx_get('jot_id');
  257. $aJotInfo = $this->_oDb->getJotById($iJotId);
  258. $aLotInfo = $this->_oDb->getLotByParentId($iJotId);
  259. $CNF = &$this->_oConfig->CNF;
  260. if (empty($aJotInfo) || !$this->isLogged() || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)))
  261. return echoJson(array('code' => 1));
  262. echoJson(array('code' => 0, 'html' => $this->_oTemplate->getThreadReply($iJotId)));
  263. }
  264. public function actionGetThreadsPanel()
  265. {
  266. $iJotId = bx_get('jot_id');
  267. if (!$this->isLogged() || !$iJotId)
  268. return echoJson(array('code' => 1));
  269. $CNF = $this->_oConfig->CNF;
  270. $aJotInfo = $this->_oDb->getJotById($iJotId);
  271. if (empty($aJotInfo))
  272. return echoJson(array('code' => 1));
  273. $aLotInfo = $this->_oDb->getLotByParentId($iJotId);
  274. if (!empty($aLotInfo) && $aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId))
  275. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_participant')));
  276. echoJson(array('code' => 0, 'html' => $this->_oTemplate->getThreadsPanel($iJotId, $this->_iProfileId)));
  277. }
  278. /**
  279. * Adds messenger block to all pages with comments and trigger pages during installation
  280. */
  281. public function serviceAddMessengerBlocks()
  282. {
  283. if (!isAdmin())
  284. return '';
  285. $aPages = $this->_oDb->getPagesWithComments();
  286. $aUrl = array();
  287. foreach ($aPages as $sModule => $sPage) {
  288. $sParams = parse_url($sPage, PHP_URL_QUERY);
  289. if (!empty($sParams))
  290. parse_str($sParams, $aUrl);
  291. if (isset($aUrl['i'])) {
  292. $sPage = BxDolPageQuery::getPageObjectNameByURI($aUrl['i']);
  293. if (!$this->_oDb->isBlockAdded($sPage))
  294. $this->_oDb->addMessengerBlock($sPage);
  295. }
  296. }
  297. }
  298. /**
  299. * Builds main messenger page
  300. */
  301. public function actionHome()
  302. {
  303. if (!$this->isLogged())
  304. bx_login_form();
  305. $oPage = BxDolPage::getObjectInstance('bx_messenger_main');
  306. if (!$oPage) {
  307. $this->_oTemplate->displayPageNotFound();
  308. exit;
  309. }
  310. $oPage->displayPage();
  311. }
  312. public function actionArchive($iJotId)
  313. {
  314. $iLotId = $this->_oDb->getLotByJotId($iJotId);
  315. $this->_iJotId = $iJotId;
  316. if (!$iLotId) {
  317. $this->_sWelcomeMessage = _t('_bx_messenger_not_found_to_read');
  318. $this->_iJotId = BX_IM_EMPTY;
  319. } else
  320. if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId)) {
  321. $this->_sWelcomeMessage = _t('_bx_messenger_not_allowed_to_read');
  322. $this->_iJotId = BX_IM_EMPTY;
  323. }
  324. $this->actionHome();
  325. }
  326. /**
  327. * Create List of participants received from request (POST, GET)
  328. * @param mixed $mixedParticipants participants list
  329. * @param bool $bExcludeLogged don't add logged profile to the participants list
  330. * @return array participants list
  331. */
  332. private function getParticipantsList($mixedParticipants, $bExcludeLogged = false)
  333. {
  334. if (empty($mixedParticipants))
  335. return [];
  336. $aParticipants = is_array($mixedParticipants) ? $mixedParticipants : array(intval($mixedParticipants));
  337. if (!$bExcludeLogged && !in_array($this->_iProfileId, $aParticipants))
  338. $aParticipants[] = $this->_iProfileId;
  339. return array_unique($aParticipants, SORT_NUMERIC);
  340. }
  341. public function serviceSendMessage($iRecipient = 0, $mixedData = '', $iSender = 0)
  342. {
  343. if (!$iSender)
  344. $iSender = $this->_iProfileId;
  345. if (empty($mixedData))
  346. return _t('_bx_messenger_send_message_no_data');
  347. $aData = array();
  348. if (is_array($mixedData))
  349. $aData = $mixedData;
  350. elseif (is_string($mixedData) && strlen($mixedData))
  351. $aData['message'] = $mixedData;
  352. if (empty($aData))
  353. return _t('_bx_messenger_send_message_no_data');
  354. $aData['participants'] = array();
  355. if ($iRecipient)
  356. $aData['participants'][] = $iRecipient;
  357. if ($iSender)
  358. $aData['participants'][] = $iSender;
  359. $mixedResult = $this->sendMessage($aData, $iRecipient, $iSender);
  360. if (is_array($mixedResult) && isset($mixedResult['jot_id']))
  361. return true;
  362. return is_string($mixedResult) ? $mixedResult : false;
  363. }
  364. /**
  365. * Main function to send Messages
  366. * @param array $aData params for a message, files, text, participants...
  367. * @param int $iRecipient recipient's profile id
  368. * @param int $iSender sender's profile id
  369. * @return array/string
  370. */
  371. public function sendMessage(&$aData, $iRecipient = 0, $iSender = 0)
  372. {
  373. $sMessage = isset($aData['message']) ? trim($aData['message']) : '';
  374. $iLotId = isset($aData['lot']) ? (int) $aData['lot'] : 0;
  375. $iGroupId = isset($aData['group_id']) ? (int) $aData['group_id'] : 0;
  376. $iType = (isset($aData['type']) && $aData['type'] && $this->_oDb->isLotType($aData['type'])) ? (int) $aData['type'] : BX_IM_TYPE_PRIVATE;
  377. $aFiles = $aData[BX_ATT_TYPE_FILES] ?? [];
  378. $aGiphy = $aData[BX_ATT_TYPE_GIPHY] ?? [];
  379. $iReply = isset($aData['reply']) ? (int) $aData['reply'] : '';
  380. $aParticipants = $aData['participants'] ?? [];
  381. $aAttachments = $aData['attachment'] ?? [];
  382. $sClass = $aData['class'] ?? '';
  383. $sTitle = isset($aData['title']) ? html2txt($aData['title']) : '';
  384. $iParent = isset($aData['parent']) ? (int) $aData['parent'] : 0;
  385. $sUrl = '';
  386. $CNF = &$this->_oConfig->CNF;
  387. // check if message contains toxic
  388. if ($sMessage && $CNF['CHECK-CONTENT-FOR-TOXIC'] && BxDolRequest::serviceExists('bx_antispam', 'is_toxic')) {
  389. if (bx_srv('bx_antispam', 'is_toxic', [$sMessage]) === true)
  390. return _t('_bx_messenger_toxic_message');
  391. }
  392. if ($iRecipient && !($oRecipient = BxDolProfile::getInstance($iRecipient)))
  393. return _t('_bx_messenger_profile_not_found');
  394. if (!$iSender)
  395. $iSender = $this->_iProfileId;
  396. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_SEND_MESSAGE, $iSender);
  397. if ($mixedResult !== true)
  398. return $mixedResult;
  399. if ($iLotId) {
  400. if (!($aLotInfo = $this->_oDb->getLotInfoById($iLotId)))
  401. return _t('_bx_messenger_not_found');
  402. $mixedOptions = $this->_oDb->getLotSettings($iLotId);
  403. $bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $iSender) === true;
  404. $bIsAuthor = $bCheckAction || $this->_oDb->isAuthor($iLotId, $iSender);
  405. if ($sMessage && $mixedOptions !== false && !in_array(BX_MSG_SETTING_MSG, $mixedOptions) && !$bIsAuthor)
  406. return _t('_bx_messenger_send_message_save_error');
  407. if (!empty($aFiles)) {
  408. if (count($aFiles) == 1 && isset(current($aFiles)['content_type']) && current($aFiles)['content_type'] == BX_MSG_SETTING_VIDEO_RECORD) {
  409. if ($mixedOptions !== false && !in_array(BX_MSG_SETTING_VIDEO_RECORD, $mixedOptions) && !$bIsAuthor)
  410. return _t('_bx_messenger_send_message_save_error');
  411. }
  412. if ($mixedOptions !== false && !in_array(BX_MSG_SETTING_FILES, $mixedOptions) && !$bIsAuthor)
  413. return _t('_bx_messenger_send_message_save_error');
  414. }
  415. if (!empty($aGiphy) && $mixedOptions !== false && !in_array(BX_MSG_SETTING_GIPHY, $mixedOptions) && !$bIsAuthor)
  416. return _t('_bx_messenger_send_message_save_error');
  417. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  418. $iType = isset($aLotInfo[$CNF['FIELD_TYPE']]) ? $aLotInfo[$CNF['FIELD_TYPE']] : BX_IM_TYPE_PUBLIC;
  419. if ($iType == BX_IM_TYPE_PRIVATE && !$iRecipient) {
  420. $aPartList = $this->_oDb->getParticipantsList($iLotId, true, $iSender);
  421. if (count($aPartList) == 1) {
  422. $iRecipient = current($aPartList);
  423. }
  424. }
  425. }
  426. if ($iRecipient && !$this->onCheckContact($iSender, $iRecipient))
  427. return _t('_bx_messenger_contact_privacy_not_allowed');
  428. if ($iType !== BX_IM_TYPE_PRIVATE || $sClass)
  429. $sUrl = isset($aData['url']) ? $aData['url'] : '';
  430. // prepare participants list
  431. $aParticipants = $this->getParticipantsList($aParticipants, $iSender !== $this->_iProfileId);
  432. if (!$iLotId && empty($aParticipants) && $iType === BX_IM_TYPE_PRIVATE)
  433. return _t('_bx_messenger_save_part_failed');
  434. $aBroadcastParticipants = null;
  435. if ($iType === BX_IM_TYPE_BROADCAST && isset($aData[BX_MSG_TALK_TYPE_BROADCAST])) {
  436. $aBroadcastParticipants = $this->getProfilesByCriteria($aData[BX_MSG_TALK_TYPE_BROADCAST], [
  437. 'include_profiles' => $aParticipants
  438. ]);
  439. if (empty($aBroadcastParticipants))
  440. return _t('_bx_messenger_save_part_failed');
  441. }
  442. $sMessage = $this->prepareMessageToDb($sMessage);
  443. if ($sMessage && $iType != BX_IM_TYPE_PRIVATE && $sUrl)
  444. $sUrl = $this->getPreparedUrl($sUrl);
  445. if ($sClass === BX_MSG_TALK_CLASS_MARKET)
  446. $sUrl = $this->_oConfig->getPageIdent($sUrl);
  447. $aResult = [];
  448. if (
  449. ($sMessage || !empty($aFiles) || !empty($aGiphy) || !empty($aAttachments)) && ($iId = $this->_oDb->saveMessage(
  450. array(
  451. 'message' => $sMessage,
  452. 'type' => $iType,
  453. 'member_id' => $this->_iProfileId,
  454. 'url' => $sUrl,
  455. 'title' => $sTitle,
  456. 'lot' => $iLotId,
  457. 'reply' => $iReply,
  458. 'parent' => $iParent,
  459. 'group_id' => $iGroupId,
  460. 'class' => $sClass,
  461. 'participants' => $aParticipants
  462. )
  463. ))
  464. ) {
  465. if (!$iLotId)
  466. $aResult['lot_id'] = $this->_oDb->getLotByJotId($iId);
  467. $aBroadcastContext = [];
  468. if ($iType === BX_IM_TYPE_BROADCAST && !$iLotId && (int) $aResult['lot_id']) {
  469. $iAuthorId = $this->_iProfileId;
  470. $iCreatedLot = (int) $aResult['lot_id'];
  471. if (isset($aData[BX_MSG_TALK_TYPE_BROADCAST]['author']) && (int) $aData[BX_MSG_TALK_TYPE_BROADCAST]['author'])
  472. $iAuthorId = $aData[BX_MSG_TALK_TYPE_BROADCAST]['author'];
  473. if ($aBroadcastParticipants === null)
  474. $aBroadcastParticipants = $this->getProfilesByCriteria($aData[BX_MSG_TALK_TYPE_BROADCAST], [
  475. 'include_profiles' => $aParticipants
  476. ]);
  477. if (!is_array($aBroadcastParticipants))
  478. $aBroadcastParticipants = [];
  479. $aPartList = array_unique(array_merge($aParticipants, (array) $aBroadcastParticipants), SORT_NUMERIC);
  480. $sModule = $this->_oConfig->getObject('alert');
  481. $sTemplateName = $this->_oConfig->getName();
  482. bx_alert($sModule, 'broadcast_attachment_before', $iId, $aResult['lot_id'], [
  483. 'id' => &$iId,
  484. 'talk_id' => $aResult['lot_id'],
  485. 'author' => &$iAuthorId,
  486. 'template' => &$sTemplateName,
  487. 'participants' => &$aPartList,
  488. 'data' => &$aData
  489. ]);
  490. $aAttachments[$sTemplateName] = (int) $iId;
  491. $aNotificationsType = [];
  492. if (isset($aData[BX_MSG_TALK_TYPE_BROADCAST]['notify_by'])) {
  493. if ($aNotifData = $aData[BX_MSG_TALK_TYPE_BROADCAST]['notify_by'])
  494. $aNotificationsType['silent_mode'] = $this->_oConfig->getSelectedNotificationMode($aNotifData);
  495. }
  496. $aBroadcastContext = [
  497. 'author_id' => (int) $iAuthorId,
  498. 'lot_id' => $iCreatedLot,
  499. 'jot_id' => (int) $iId,
  500. 'participants' => array_values(array_unique(array_filter(array_map('intval', $aPartList), function ($iPart) {
  501. return $iPart > 0;
  502. }), SORT_NUMERIC)),
  503. 'notifications' => $aNotificationsType
  504. ];
  505. }
  506. if (!empty($aFiles)) {
  507. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  508. $aUploadingFilesNames = $aCompleteFilesNames = array();
  509. if (bx_is_api()) {
  510. if (is_array($aFiles))
  511. foreach ($aFiles as &$iFileId) {
  512. if ($aFile = $oStorage->getFile($iFileId)) {
  513. $aCompleteFilesNames[] = $aFile[$CNF['FIELD_ST_NAME']];
  514. $this->_oDb->updateFiles($iFileId, array(
  515. $CNF['FIELD_ST_JOT'] => $iId,
  516. ));
  517. $oStorage->afterUploadCleanup($iFileId, $this->_iProfileId);
  518. }
  519. }
  520. } else
  521. foreach ($aFiles as &$aFile) {
  522. if (!(int) $aFile['complete']) {
  523. $aUploadingFilesNames[] = $aFile['realname'];
  524. continue;
  525. }
  526. if (!$this->_oConfig->isValidToUpload($aFile['name'])) {
  527. $this->_oDb->deleteJot($iId, $this->_iProfileId, true);
  528. return _t('_bx_messenger_bad_request');
  529. }
  530. $sFile = BX_DIRECTORY_PATH_TMP . basename($aFile['name']);
  531. $sExt = $oStorage->getFileExt($sFile);
  532. $sFilename = $oStorage->getFileTitle($aFile['realname']) . ".{$sExt}";
  533. $iFile = $oStorage->storeFileFromPath($sFile, $iType == BX_IM_TYPE_PRIVATE, $this->_iProfileId, (int) $iId);
  534. if ($iFile) {
  535. $oStorage->afterUploadCleanup($iFile, $this->_iProfileId);
  536. $this->_oDb->updateFiles($iFile, array(
  537. $CNF['FIELD_ST_JOT'] => $iId,
  538. $CNF['FIELD_ST_NAME'] => $sFilename
  539. ));
  540. $aCompleteFilesNames[] = $sFilename;
  541. @unlink($sFile);
  542. }
  543. }
  544. if (!empty($aUploadingFilesNames))
  545. $aAttachments[BX_ATT_TYPE_FILES_UPLOADING] = $aUploadingFilesNames;
  546. if (!empty($aCompleteFilesNames))
  547. $aAttachments[BX_ATT_TYPE_FILES] = $aCompleteFilesNames;
  548. }
  549. if (is_array($aGiphy) && !empty($aGiphy))
  550. $aAttachments[BX_ATT_TYPE_GIPHY] = current($aGiphy);
  551. if ($iReply)
  552. $this->_oDb->updateJot($iId, $CNF['FIELD_MESSAGE_REPLY'], $iReply);
  553. if (!empty($aAttachments))
  554. $this->_oDb->addAttachment($iId, $aAttachments);
  555. if (!empty($aBroadcastContext)) {
  556. if ($aBroadcastContext['author_id'] && $aBroadcastContext['author_id'] !== (int) $this->_iProfileId) {
  557. $this->_oDb->updateConvoFiled($aBroadcastContext['lot_id'], $CNF['FIELD_AUTHOR'], $aBroadcastContext['author_id']);
  558. $this->_oDb->updateJot($aBroadcastContext['jot_id'], $CNF['FIELD_MESSAGE_AUTHOR'], $aBroadcastContext['author_id']);
  559. }
  560. if (!empty($aBroadcastContext['participants'])) {
  561. $this->_oDb->createBroadcastUsers($aBroadcastContext['lot_id'], $aBroadcastContext['participants']);
  562. if (!$this->queueBroadcastDelivery($aBroadcastContext['lot_id'], $aBroadcastContext['jot_id'], $aBroadcastContext['author_id'], $aBroadcastContext['notifications']))
  563. $this->serviceProcessBroadcastQueue($aBroadcastContext['lot_id'], $aBroadcastContext['jot_id'], $aBroadcastContext['author_id'], $aBroadcastContext['notifications']);
  564. }
  565. }
  566. $aResult['jot_id'] = $iId;
  567. $aJot = $this->_oDb->getJotById($iId);
  568. if (!empty($aJot))
  569. $aResult['time'] = bx_time_utc($aJot[$CNF['FIELD_MESSAGE_ADDED']]);
  570. // Push this jot before onSendJot so a sync agent reply
  571. // cannot appear on the socket before the message it is answering.
  572. $iBroadcastLot = $iLotId ? (int)$iLotId : (int)($aResult['lot_id'] ?? 0);
  573. if (!$iBroadcastLot)
  574. $iBroadcastLot = (int)$this->_oDb->getLotByJotId($iId);
  575. $this->broadcastNewJot($iBroadcastLot, $iId);
  576. $this->onSendJot($iId);
  577. } else
  578. return _t('_bx_messenger_send_message_save_error');
  579. BxDolSession::getInstance()->exists();
  580. return $aResult;
  581. }
  582. public function actionSend()
  583. {
  584. $aData = &$_POST;
  585. if (!$this->isLogged())
  586. return echoJson(['code' => 1, 'message' => _t('_bx_messenger_not_logged'), 'reload' => 1]);
  587. if (isset($aData[BX_MSG_TALK_TYPE_BROADCAST]) && $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_BROADCASTS) === true)
  588. $aData['type'] = BX_IM_TYPE_BROADCAST;
  589. $aLastJotInfo = isset($aData['lot']) ? $this->_oDb->getLatestJot($aData['lot'], BX_IM_EMPTY, false) : [];
  590. $mixedResult = $this->sendMessage($aData);
  591. if (is_array($mixedResult)) {
  592. if (isset($mixedResult['lot_id']))
  593. $mixedResult['header'] = $this->_oTemplate->getTalkHeader($mixedResult['lot_id'], $this->_iProfileId);
  594. $CNF = &$this->_oConfig->CNF;
  595. if (!empty($aLastJotInfo) && isset($aData['tmp_id'])) {
  596. $iTimer = $aLastJotInfo[$CNF['FIELD_MESSAGE_ADDED']];
  597. $iCurrent = intval($aData['tmp_id'] / 1000);
  598. if (($iCurrent - $iTimer) > $CNF['DATE-SHIFT'])
  599. $mixedResult['separator'] = $this->_oTemplate->getDateSeparator($iCurrent);
  600. }
  601. } else
  602. return echoJson(array('code' => 1, 'message' => $mixedResult));
  603. $mixedResult['code'] = 0;
  604. if ($iTmpId = bx_get('tmp_id'))
  605. $mixedResult['tmp_id'] = $iTmpId;
  606. echoJson($mixedResult);
  607. }
  608. /**
  609. * Loads talk to the right side block when member choose conversation or when open messenger page
  610. * @return array with json result
  611. */
  612. public function actionLoadTalk()
  613. {
  614. $iLotId = (int) bx_get('lot_id');
  615. $iJotId = (int) bx_get('jot_id');
  616. $sAreaType = bx_get('area_type');
  617. $this->_isBlockMessenger = (bool) bx_get('is_block');
  618. if (!$iLotId)
  619. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_empty_history'))));
  620. if (!$this->isLogged())
  621. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1));
  622. $CNF = &$this->_oConfig->CNF;
  623. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  624. $bIsParticipant = $this->_oDb->isParticipant($iLotId, $this->_iProfileId);
  625. if (!($bIsParticipant || $this->_oDb->getGroupIdByLotId($iLotId)) && !($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PUBLIC || $aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_BROADCAST)) {
  626. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_participant')), 'reload' => 1));
  627. }
  628. ;
  629. if ((int) bx_get('mark_as_read'))
  630. $this->_oDb->readAllMessages($iLotId, $this->_iProfileId);
  631. $aUnreadJots = $this->_oDb->getNewJots($this->_iProfileId, $iLotId);
  632. $iUnreadLotsJots = !empty($aUnreadJots) ? (int) $aUnreadJots[$CNF['FIELD_NEW_UNREAD']] : 0;
  633. $iLastUnreadJot = !empty($aUnreadJots) ? (int) $aUnreadJots[$CNF['FIELD_NEW_JOT']] : 0;
  634. $aHeader = $this->_oTemplate->getTalkHeader($iLotId, $this->_iProfileId, $this->_isBlockMessenger, true);
  635. $sHistory = $this->_oTemplate->getHistoryArea(
  636. [
  637. 'profile_id' => $this->_iProfileId,
  638. 'lot' => $iLotId,
  639. 'jot' => ($iJotId ? $iJotId : $iLastUnreadJot),
  640. 'area' => $sAreaType
  641. ],
  642. $iUnreadLotsJots && $iUnreadLotsJots < ($CNF['MAX_JOTS_BY_DEFAULT'] / 2)
  643. );
  644. $sTextArea = !in_array($sAreaType, $CNF['VIEW-IN-TALKS']) ? $this->_oTemplate->getTextArea($this->_iProfileId, $iLotId) : '';
  645. $aVars = [
  646. 'code' => 0,
  647. 'title' => trim(html2txt($aHeader['title'])),
  648. 'header' => $this->_oTemplate->parseHtmlByName('talk-header.html', $aHeader),
  649. 'history' => $sHistory,
  650. 'text_area' => $sTextArea,
  651. 'last_unread_jot' => $iLastUnreadJot,
  652. 'unread_jots' => $iUnreadLotsJots,
  653. 'muted' => (int) $this->_oDb->isMuted($iLotId, $this->_iProfileId),
  654. 'params' => $this->_oDb->getLotType($iLotId)
  655. ];
  656. if ($this->_isBlockMessenger)
  657. $aVars['talks_list'] = $this->_oTemplate->getTalksList($iLotId);
  658. BxDolSession::getInstance()->exists();
  659. echoJson($aVars);
  660. }
  661. public function actionLoadThreadTalk()
  662. {
  663. $CNF = &$this->_oConfig->CNF;
  664. if (!$this->isLogged())
  665. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1));
  666. $iJotId = (int) bx_get('jot_id');
  667. if (!($aJotInfo = $this->_oDb->getJotById($iJotId)))
  668. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_nothing_found')), 'reload' => 1));
  669. $aLotInfo = $this->_oDb->getLotByParentId($iJotId);
  670. if (empty($aLotInfo)) {
  671. $aParentLot = $this->_oDb->getLotInfoById($aJotInfo[$CNF['FIELD_MESSAGE_FK']]);
  672. if (empty($aParentLot))
  673. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_nothing_found')), 'reload' => 1));
  674. if (!$this->_oDb->isParticipant($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)) {
  675. return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_participant')), 'reload' => 1));
  676. }
  677. $iLotId = $this->_oDb->createLot(
  678. $this->_iProfileId,
  679. [
  680. 'title' => $aJotInfo[$CNF['FIELD_MESSAGE']] ? strmaxtextlen($aJotInfo[$CNF['FIELD_MESSAGE']]) : $aParentLot[$CNF['FIELD_TITLE']],
  681. 'type' => BX_IM_TYPE_PRIVATE,
  682. 'thread' => $iJotId,
  683. 'participants' => explode(',', $aParentLot[$CNF['FIELD_PARTICIPANTS']])
  684. ]
  685. );
  686. $this->onCreateLot($iLotId);
  687. } else
  688. $iLotId = $aLotInfo[$CNF['FIELD_ID']];
  689. echoJson(['code' => 0, 'lot' => $iLotId]);
  690. }
  691. public function actionMarkJotsAsRead()
  692. {
  693. $iId = (int) bx_get('lot');
  694. if (!$this->isLogged() || !$this->_oDb->isParticipant($iId, $this->_iUserId)) {
  695. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1));
  696. }
  697. ;
  698. $this->_oDb->readAllMessages($iId, $this->_iUserId);
  699. echoJson(array('code' => 0));
  700. }
  701. public function actionViewedJot()
  702. {
  703. $iJotId = (int) bx_get('jot_id');
  704. $aJotInfo = $this->_oDb->getJotById($iJotId);
  705. $aLotInfo = $this->_oDb->getLotByJotId($iJotId, false);
  706. $CNF = &$this->_oConfig->CNF;
  707. if (empty($aJotInfo) || !$this->isLogged() || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)))
  708. return echoJson(array('code' => 1));
  709. $this->_oDb->readMessage($iJotId, $this->_iProfileId);
  710. $this->_oDb->markNotificationAsRead($this->_iProfileId, $aJotInfo[$CNF['FIELD_MESSAGE_FK']]);
  711. $aUnreadInfo = $this->_oDb->getNewJots($this->_iProfileId, $aJotInfo[$CNF['FIELD_MESSAGE_FK']]);
  712. $iUnreadJotsNumber = $iLastUnreadJotId = 0;
  713. if (!empty($aUnreadInfo)) {
  714. $iLastUnreadJotId = (int) $aUnreadInfo[$CNF['FIELD_NEW_JOT']];
  715. $iUnreadJotsNumber = (int) $aUnreadInfo[$CNF['FIELD_NEW_UNREAD']];
  716. }
  717. echoJson(array('code' => 0, 'unread_jots' => $iUnreadJotsNumber, 'last_unread_jot' => $iLastUnreadJotId));
  718. }
  719. /**
  720. * Loads messages for specified lot(conversation)
  721. * @return array with json
  722. */
  723. public function actionLoadJots()
  724. {
  725. $iId = (int) bx_get('id');
  726. $iJotId = (int) bx_get('jot_id');
  727. if (!$this->isLogged() || !$iId)
  728. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged'))));
  729. if (!$this->_oDb->isParticipant($iId, $this->_iProfileId))
  730. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_participant'))));
  731. $sContent = $this->_oTemplate->getHistoryArea(['profile_id' => $this->_iProfileId, 'lot' => $iId, 'jot' => $iJotId]);
  732. echoJson(array('code' => 0, 'history' => $sContent));
  733. }
  734. /**
  735. * Loads search users area
  736. * @return array|void
  737. */
  738. public function actionLoadList()
  739. {
  740. if (!$this->isLogged())
  741. return echoJson(['code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))]);
  742. $sAction = bx_get('action');
  743. $iGroupId = (int) bx_get('group_id');
  744. $sGroupType = bx_get('area_type');
  745. $iLotId = (int) bx_get('lot');
  746. if ($sAction === 'edit') {
  747. if (!$iLotId || !($bAllowed = $this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true))
  748. return echoJson(['code' => 1]);
  749. } else
  750. $iLotId = 0;
  751. $aProfilesList = [];
  752. $bIsGroupedChat = $sGroupType === BX_MSG_TALK_TYPE_GROUPS && $iGroupId;
  753. if ($bIsGroupedChat) {
  754. $aProfilesList = $this->getParticipantsListByGroupAndFilter($iGroupId);
  755. if (!empty($aProfilesList))
  756. $aProfilesList = array_map(function ($aValue) {
  757. return $aValue['value'];
  758. }, $aProfilesList);
  759. }
  760. if (($mixedContent = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_TALKS, $this->_iProfileId)) !== true) {
  761. return echoJson([
  762. 'code' => 1,
  763. 'msg' => $sContent = MsgBox($mixedContent)
  764. ]);
  765. }
  766. $sContent = $this->_oTemplate->getCreateListArea($iLotId, $aProfilesList, $bIsGroupedChat);
  767. echoJson([
  768. 'code' => 0,
  769. 'content' => $sContent,
  770. 'text_area' => $this->_oTemplate->getTextArea($this->_iProfileId, $iLotId)
  771. ]);
  772. }
  773. private function getParticipantsListByGroupAndFilter($iGroupId, $sTerm = '')
  774. {
  775. $CNF = &$this->_oConfig->CNF;
  776. $mixedUsers = [];
  777. if (!$iGroupId || !($aGroupInfo = $this->_oDb->getGroupById($iGroupId)) || $aGroupInfo[$CNF['FMG_MODULE']] === BX_MSG_TALK_TYPE_PAGES)
  778. return false;
  779. $oContextProfile = BxDolProfile::getInstance($aGroupInfo[$CNF['FMG_PROFILE_ID']]);
  780. if ($oContextProfile && ($mixedUsers = bx_srv($aGroupInfo[$CNF['FMG_MODULE']], 'fans', array($oContextProfile->getContentId(), true)))) {
  781. if ($sTerm)
  782. $mixedUsers = array_filter($mixedUsers, function ($iVal) use ($sTerm) {
  783. $oProfile = BxDolProfile::getInstance($iVal);
  784. return stripos($oProfile->getDisplayName(), $sTerm) !== false ? ['value' => $iVal] : false;
  785. });
  786. $mixedUsers = array_map(function ($iVal) {
  787. return ['value' => $iVal];
  788. }, $mixedUsers);
  789. }
  790. return $mixedUsers;
  791. }
  792. function actionGetUsersList()
  793. {
  794. $aResult = array('code' => 1);
  795. $sTerm = bx_get('term');
  796. $iLotId = (int) bx_get('lot');
  797. $sAreaType = bx_get('area_type');
  798. if (!$this->isLogged())
  799. return echoJson($aResult);
  800. $sExcept = bx_get('except');
  801. $aExcept = array();
  802. if ($sExcept)
  803. $aExcept = explode(',', $sExcept);
  804. if ($iLotId && !$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  805. return echoJson($aResult);
  806. $mixedUsers = false;
  807. if ($iLotId && $sAreaType === 'groups' && ($iGroupId = $this->_oDb->getGroupIdByLotId($iLotId))) {
  808. $mixedUsers = $this->getParticipantsListByGroupAndFilter($iGroupId, $sTerm);
  809. if (is_array($mixedUsers))
  810. $mixedUsers = array_filter($mixedUsers, function ($aValue) use ($aExcept) {
  811. return !in_array($aValue['value'], $aExcept);
  812. });
  813. }
  814. $aUsers = $mixedUsers !== false ? $mixedUsers : $this->searchProfiles($sTerm, $aExcept, $this->_oConfig->CNF['PARAM_SEARCH_DEFAULT_USERS']);
  815. if (empty($aUsers))
  816. return echoJson(array('content' => MsgBox(_t('_bx_messenger_empty_users_list'))));
  817. $aProfiles = array();
  818. foreach ($aUsers as &$aValue) {
  819. if (!$this->onCheckContact($this->_iProfileId, $aValue['value']))
  820. continue;
  821. $aProfiles[] = $aValue['value'];
  822. }
  823. if (!empty($aProfiles))
  824. $aResult['content'] = $this->_oTemplate->getProfilesListPreviewForCreateTalkArea($aProfiles);
  825. echoJson($aResult);
  826. }
  827. /**
  828. * Search for Lots by keywords in the right side block
  829. * @return string with json
  830. */
  831. public function actionSearch()
  832. {
  833. if (!$this->isLogged())
  834. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1));
  835. $sParam = bx_get('param');
  836. $iStarred = bx_get('starred');
  837. $sType = bx_get('type');
  838. $iGroupId = bx_get('group_id');
  839. $aResult = ['code' => 0];
  840. $aParams = ['term' => $sParam, 'star' => (bool) $iStarred];
  841. $aFoundConvos = $aMyLots = [];
  842. switch ($sType) {
  843. case BX_MSG_TALK_TYPE_THREADS:
  844. $aParams['threads'] = true;
  845. $aMyLots = $this->_oDb->getMyLots($this->_iProfileId, $aParams, $aFoundConvos);
  846. break;
  847. case 'public':
  848. $aParams['type'] = [BX_IM_TYPE_SETS, BX_IM_TYPE_PUBLIC];
  849. $aMyLots = $this->_oDb->getMyLots($this->_iProfileId, $aParams, $aFoundConvos);
  850. break;
  851. case BX_MSG_TALK_TYPE_MR:
  852. $aMyLots = $this->_oDb->getReactionsMentionsLots($this->_iProfileId, $aParams);
  853. break;
  854. case BX_MSG_TALK_TYPE_REPLIES:
  855. $aMyLots = $this->_oDb->getLotsWithReplies($this->_iProfileId, $aParams);
  856. break;
  857. case BX_MSG_TALK_TYPE_SAVED:
  858. $aMyLots = $this->_oDb->getSavedJotInLots($this->_iProfileId, $aParams);
  859. break;
  860. case BX_MSG_TALK_TYPE_GROUPS:
  861. $aParams['group'] = $iGroupId;
  862. $aMyLots = $this->_oDb->getMyLots($this->_iProfileId, $aParams, $aFoundConvos);
  863. break;
  864. case BX_MSG_TALK_TYPE_DIRECT:
  865. $aParams['type'] = [BX_IM_TYPE_PRIVATE];
  866. default:
  867. $aMyLots = $this->_oDb->getMyLots($this->_iProfileId, $aParams, $aFoundConvos);
  868. }
  869. if (empty($aMyLots))
  870. $sContent = $sParam ? MsgBox(_t('_bx_messenger_txt_msg_no_results')) : $this->_oTemplate->getFriendsList($sParam); {
  871. $sContent = $this->_oTemplate->getLotsPreview($this->_iProfileId, $aMyLots);
  872. if (!empty($aFoundConvos['jots_list'])) {
  873. foreach ($aFoundConvos['jots_list'] as $iJot => $iConvo) {
  874. $aResult['search_list'][$iConvo]['list'][] = $iJot;
  875. }
  876. }
  877. }
  878. $aResult['html'] = $sContent;
  879. echoJson($aResult);
  880. }
  881. /**
  882. * Update brief of the specified lot in the lots list
  883. * @return string with json
  884. */
  885. public function actionUpdateLotBrief()
  886. {
  887. if (!$this->isLogged())
  888. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1));
  889. $iLotId = (int) bx_get('lot_id');
  890. if (!$iLotId || !$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  891. return echoJson(array('code' => 1));
  892. if ($aLots = $this->_oDb->getLotInfoById($iLotId)) {
  893. return echoJson(array(
  894. 'code' => 0,
  895. 'html' => $this->_oTemplate->getLotsPreview($this->_iProfileId, array($aLots)),
  896. 'muted' => (int) $this->_oDb->isMuted($iLotId, $this->_iProfileId),
  897. 'params' => $this->_oDb->getLotType($iLotId)
  898. ));
  899. }
  900. echoJson(array('code' => 1));
  901. }
  902. /**
  903. * Prepare url for Lot title if it was created on separated page
  904. * @param string URL
  905. * @return string URL
  906. */
  907. private function getPreparedUrl($sUrl)
  908. {
  909. if (!$sUrl)
  910. return false;
  911. $aUrl = parse_url($sUrl);
  912. return strtolower($aUrl['path'] . (isset($aUrl['query']) ? '?' . $aUrl['query'] : ''));
  913. }
  914. private function isAvailable($iLotId)
  915. {
  916. if (!$iLotId)
  917. return false;
  918. $CNF = &$this->_oConfig->CNF;
  919. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  920. if (!empty($aLotInfo) && !$this->_oDb->isParticipant($iLotId, $this->_iProfileId) && $aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
  921. return false;
  922. return true;
  923. }
  924. /**
  925. * Loads messages for lot(conversation) (when member wants to view history or get new messages from participants)
  926. * @return string with json
  927. */
  928. public function actionUpdate()
  929. {
  930. $CNF = &$this->_oConfig->CNF;
  931. $iJot = (int) bx_get('jot');
  932. $iLotId = (int) bx_get('lot');
  933. $sLoad = bx_get('load');
  934. $sArea = bx_get('area_type');
  935. if (!$this->isLogged() && !($sLoad && $iJot && $iLotId))
  936. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_logged'), 'reload' => 1));
  937. if (!$this->isAvailable($iLotId))
  938. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_talk_is_not_allowed')));
  939. $sUrl = bx_get('url');
  940. if ($sUrl)
  941. $sUrl = $this->getPreparedUrl($sUrl);
  942. $isFocused = (bool) bx_get('focus');
  943. $iRequestedJot = (int) bx_get('req_jot');
  944. $iLastViewedJot = (int) bx_get('last_viewed_jot');
  945. $bUpdateHistory = true;
  946. $sContent = '';
  947. $iUnreadJotsNumber = 0;
  948. $iLastUnreadJotId = 0;
  949. $bAttach = true;
  950. $bRemoveSeparator = false;
  951. switch ($sLoad) {
  952. case 'new':
  953. if (!$iJot) {
  954. $iJot = $this->_oDb->getFirstUnreadJot($this->_iProfileId, $iLotId);
  955. if ($iJot)
  956. $iJot = $this->_oDb->getPrevJot($iLotId, $iJot);
  957. else {
  958. $aLatestJot = $this->_oDb->getLatestJot($iLotId);
  959. $iJot = !empty($aLatestJot) ? (int) $aLatestJot[$CNF['FIELD_MESSAGE_ID']] : 0;
  960. }
  961. }
  962. if ($iRequestedJot) {
  963. if ($this->_oDb->getJotsNumber($iLotId, $iJot, $iRequestedJot) >= $CNF['MAX_JOTS_LOAD_HISTORY'])
  964. $bUpdateHistory = false;
  965. else if ($isFocused)
  966. $this->_oDb->readMessage($iRequestedJot, $this->_iProfileId);
  967. }
  968. if ($iLastViewedJot && $bUpdateHistory && $isFocused) {
  969. $this->_oDb->readMessage($iLastViewedJot, $this->_iProfileId);
  970. }
  971. $bAttach = $iJot ? $this->_oDb->getJotsNumber($iLotId, $iJot) < $CNF['MAX_JOTS_LOAD_HISTORY'] : false;
  972. case 'all':
  973. case 'prev':
  974. $aOptions = array(
  975. 'lot_id' => $iLotId,
  976. 'url' => $sUrl,
  977. 'start' => $iJot,
  978. 'load' => $sLoad,
  979. 'limit' => $CNF['MAX_JOTS_LOAD_HISTORY'],
  980. 'views' => true,
  981. 'dynamic' => true,
  982. 'area' => $sArea
  983. );
  984. $iLastUnreadJotId = $iUnreadJotsNumber = 0;
  985. $aUnreadInfo = $this->_oDb->getNewJots($this->_iProfileId, $iLotId);
  986. if (!empty($aUnreadInfo)) {
  987. $iLastUnreadJotId = (int) $aUnreadInfo[$CNF['FIELD_NEW_JOT']];
  988. $iUnreadJotsNumber = (int) $aUnreadInfo[$CNF['FIELD_NEW_UNREAD']];
  989. }
  990. $sContent = '';
  991. if ($bUpdateHistory) {
  992. $aJots = $this->_oTemplate->getJotsOfLot($this->_iProfileId, $aOptions);
  993. $sContent = $aJots['content'];
  994. if (isset($aJots['first_jot']) && $iJot && ($aJotInfo = $this->_oDb->getJotById($iJot))) {
  995. $iFDate = strtotime(date("Y-m-d", $aJots['first_jot'][$CNF['FIELD_MESSAGE_ADDED']]));
  996. $iSDate = strtotime(date("Y-m-d", $aJotInfo[$CNF['FIELD_MESSAGE_ADDED']]));
  997. $bRemoveSeparator = +($iFDate == $iSDate);
  998. }
  999. }
  1000. break;
  1001. case 'edit':
  1002. $aJotInfo = $this->_oDb->getJotById($iJot);
  1003. $sContent = $aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE']];
  1004. break;
  1005. case 'vc':
  1006. case 'delete':
  1007. $sContent = $this->_oTemplate->getMessageIcons($iJot, $sLoad, $this->_oDb->isAuthor($iLotId, $this->_iProfileId) || isAdmin());
  1008. break;
  1009. case 'check_viewed':
  1010. if ($iLotId && $iJot)
  1011. $sContent = $this->_oTemplate->getViewedJotProfiles($iJot, $this->_iProfileId);
  1012. break;
  1013. case 'reaction':
  1014. if ($iJot) {
  1015. $aJotInfo = $this->_oDb->getJotById($iJot);
  1016. if (!empty($aJotInfo))
  1017. $sContent = $this->_oTemplate->getJotReactions($iJot);
  1018. }
  1019. break;
  1020. }
  1021. $aResult = array(
  1022. 'code' => 0,
  1023. 'html' => $sContent,
  1024. 'unread_jots' => $iUnreadJotsNumber,
  1025. 'last_unread_jot' => $iLastUnreadJotId,
  1026. 'allow_attach' => +$bAttach,
  1027. 'remove_separator' => +$bRemoveSeparator
  1028. );
  1029. // update session
  1030. BxDolSession::getInstance()->exists();
  1031. echoJson($aResult);
  1032. }
  1033. public function searchFriends($sTerm, $aProfilesList = array())
  1034. {
  1035. if (!$sTerm || !($aTerms = preg_split("/[\s,]+/", $sTerm)))
  1036. return array();
  1037. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  1038. $aProfiles = !empty($aProfilesList) ? $aProfilesList : $oConnection->getConnectedContent($this->_iProfileId, true);
  1039. $aResult = array();
  1040. if (empty($aProfiles))
  1041. return $aResult;
  1042. $aModules = BxDolService::call('system', 'get_profiles_modules', array(), 'TemplServiceProfiles');
  1043. if (empty($aModules))
  1044. return $aResult;
  1045. $aProfilesModules = array();
  1046. foreach ($aModules as &$aModule)
  1047. $aProfilesModules[$aModule['name']] = BxDolModule::getInstance($aModule['name']);
  1048. $aFieldsFilter = array();
  1049. foreach ($aProfiles as &$iID) {
  1050. $oProfileInfo = BxDolProfile::getInstance($iID);
  1051. if (!empty($oProfileInfo)) {
  1052. $aInfo = $oProfileInfo->getInfo();
  1053. if (isset($aInfo['type']) && isset($aProfilesModules[$aInfo['type']])) {
  1054. $aProfileInfo = $aProfilesModules[$aInfo['type']]->_oDb->getContentInfoByProfileId($iID);
  1055. if (!isset($aFieldsFilter[$aInfo['type']])) {
  1056. $sFields = getParam($aProfilesModules[$aInfo['type']]->_oConfig->CNF['PARAM_SEARCHABLE_FIELDS']);
  1057. if ($sFields)
  1058. $aFieldsFilter[$aInfo['type']] = explode(',', $sFields);
  1059. }
  1060. if ($aFieldsFilter[$aInfo['type']]) {
  1061. $sSearchString = '';
  1062. foreach ($aFieldsFilter[$aInfo['type']] as &$sKey)
  1063. $sSearchString .= " {$aProfileInfo[$sKey]}";
  1064. if ($sSearchString) {
  1065. $bResult = true;
  1066. foreach ($aTerms as &$sValue)
  1067. if (stripos($sSearchString, $sValue) === FALSE) {
  1068. $bResult = false;
  1069. break;
  1070. }
  1071. if ($bResult)
  1072. $aResult[$aInfo['type']][] = $aProfileInfo;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. }
  1078. return $aResult;
  1079. }
  1080. public function searchProfiles($sTerm, $aExcept = array(), $iLimit = 10)
  1081. {
  1082. $CNF = &$this->_oConfig->CNF;
  1083. $aModules = BxDolService::call('system', 'get_profiles_modules', array(), 'TemplServiceProfiles');
  1084. if (empty($aModules))
  1085. return array();
  1086. if ($CNF['USE-FRIENDS-ONLY-MODE'])
  1087. $aSearchResult = $this->searchFriends($sTerm);
  1088. else {
  1089. $aModules = array_map(function ($aModule) {
  1090. return $aModule['name'];
  1091. }, $aModules);
  1092. $o = new BxDolSearch($aModules);
  1093. $o->setDataProcessing(true);
  1094. $o->setCustomSearchCondition(array('keyword' => $sTerm));
  1095. $o->setCustomCurrentCondition(array(
  1096. 'paginate' => array(
  1097. 'perPage' => $iLimit / count($aModules) + 0.5
  1098. )
  1099. ));
  1100. $aSearchResult = $o->response();
  1101. if (empty($aSearchResult))
  1102. return array();
  1103. }
  1104. bx_alert($this->getName(), 'get_profiles_list', 0, 0, ['term' => $sTerm, 'override_list' => &$aSearchResult]);
  1105. $aUsers = [];
  1106. if (!bx_is_api()) {
  1107. foreach ($aSearchResult as $sModule => $aItems) {
  1108. $aCNF = BxDolModule::getInstance($sModule)->_oConfig->CNF;
  1109. $sTitleField = $aCNF['FIELD_TITLE'];
  1110. $sIdField = $aCNF['FIELD_ID'];
  1111. foreach ($aItems as &$aItem) {
  1112. $sLabel = isset($aItem[$sTitleField]) ? $aItem[$sTitleField] : false;
  1113. if ($sLabel)
  1114. $aUsers[] = array(
  1115. 'value' => BxDolProfile::getInstanceByContentAndType($aItem[$sIdField], $sModule)->id(),
  1116. 'label' => $sLabel,
  1117. );
  1118. }
  1119. }
  1120. // sort result
  1121. usort($aUsers, function ($r1, $r2) {
  1122. return strcmp($r1['label'], $r2['label']);
  1123. });
  1124. if (!empty($aExcept))
  1125. $aUsers = array_filter($aUsers, function ($aUsers) use ($aExcept) {
  1126. return !in_array($aUsers['value'], $aExcept);
  1127. });
  1128. } else
  1129. $aUsers = $aSearchResult;
  1130. return array_slice($aUsers, 0, $iLimit);
  1131. }
  1132. /**
  1133. * Occurs when member adds or edit participants list for new of specified lot
  1134. * @return string with json code
  1135. */
  1136. public function actionGetAutoComplete()
  1137. {
  1138. if (!$this->isLogged())
  1139. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_logged'), 'reload' => 1));
  1140. $aResult = $aExcept = array();
  1141. $sExcept = bx_get('except');
  1142. if ($sExcept)
  1143. $aExcept = explode(',', $sExcept);
  1144. $aUsers = $this->searchProfiles(bx_get('term'), $aExcept, $this->_oConfig->CNF['PARAM_SEARCH_DEFAULT_USERS']);
  1145. if (empty($aUsers))
  1146. return echoJson(array('items' => $aUsers));
  1147. $bDonShowDesc = $this->_oConfig->CNF['DONT-SHOW-DESC'];
  1148. foreach ($aUsers as &$aValue) {
  1149. if (!$this->onCheckContact($this->_iUserId, $aValue['value']))
  1150. continue;
  1151. if (!($oProfile = BxDolProfile::getInstance($aValue['value'])))
  1152. continue;
  1153. $aProfileInfo = $oProfile->getInfo();
  1154. $aProfileInfoDetails = BxDolService::call($aProfileInfo['type'], 'get_content_info_by_id', array($aProfileInfo['content_id']));
  1155. $oAccountInfo = BxDolAccount::getInstance($aProfileInfo['account_id']);
  1156. $sThumb = $oProfile->getThumb();
  1157. $bThumb = stripos($sThumb, 'no-picture') === FALSE;
  1158. $sDisplayName = $oProfile->getDisplayName();
  1159. if (!empty($aProfileInfoDetails) && !empty($oAccountInfo)) {
  1160. $aResult[$aProfileInfo['type']]['results'][] = array(
  1161. 'value' => $sDisplayName,
  1162. 'no_desc' => (int) $bDonShowDesc,
  1163. 'icon' => $bThumb ? $sThumb : '',
  1164. 'color' => implode(', ', BxDolTemplate::getColorCode($aValue['value'], 1.0)),
  1165. 'letter' => mb_substr($sDisplayName, 0, 1),
  1166. 'id' => $oProfile->id(),
  1167. 'profile_url' => $oProfile->getUrl(),
  1168. 'description' => !$bDonShowDesc ? _t(
  1169. '_bx_messenger_search_desc',
  1170. bx_process_output($oAccountInfo->getInfo()['logged'], BX_DATA_DATE_TS),
  1171. bx_process_output($aProfileInfoDetails['added'], BX_DATA_DATE_TS)
  1172. ) : ''
  1173. );
  1174. }
  1175. }
  1176. foreach ($aResult as $sKey => $aValues) {
  1177. $aResult[$sKey]['name'] = _t("_{$sKey}");
  1178. }
  1179. echoJson(array('results' => $aResult));
  1180. }
  1181. /**
  1182. * Returns processed videos by received videos ids
  1183. * @return array with json
  1184. */
  1185. public function actionGetProcessedMedia()
  1186. {
  1187. $CNF = &$this->_oConfig->CNF;
  1188. $aMedia = bx_get('media');
  1189. $aResult = array();
  1190. if (empty($aMedia))
  1191. return echoJson($aResult);
  1192. $oTranscoderAudio = BxDolTranscoderAudio::getObjectInstance($CNF['OBJECT_MP3_TRANSCODER']);
  1193. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  1194. if (empty($oStorage))
  1195. return echoJson($aResult);
  1196. foreach ($aMedia as &$iMedia) {
  1197. $aFile = $oStorage->getFile($iMedia);
  1198. if ($oTranscoderAudio->isMimeTypeSupported($aFile['mime_type']) && $oTranscoderAudio->isFileReady($iMedia))
  1199. $aResult[$iMedia] = $this->_oTemplate->audioPlayer($oTranscoderAudio->getFileUrl($aFile[$CNF['FIELD_ST_ID']]), true);
  1200. if (!strncmp('video/', $aFile['mime_type'], 6))
  1201. $aResult[$iMedia] = $this->_oTemplate->getVideoFilesToPlay($aFile);
  1202. }
  1203. echoJson($aResult);
  1204. }
  1205. /**
  1206. * Search for lot by participants list and occurs when member edit participants list
  1207. * @return string with json
  1208. */
  1209. public function actionFindLot()
  1210. {
  1211. if (!$this->isLogged())
  1212. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_logged'), 'reload' => 1));
  1213. $CNF = &$this->_oConfig->CNF;
  1214. $aParticipants = $this->getParticipantsList(bx_get('participants'));
  1215. $aResult = array('lot' => 0);
  1216. if (
  1217. !empty($aParticipants) && ($aChat = $this->_oDb->findLotByParams(array(
  1218. $CNF['FIELD_PARTICIPANTS'] => $aParticipants,
  1219. $CNF['FIELD_TYPE'] => BX_IM_TYPE_PRIVATE,
  1220. $CNF['FIELD_CLASS'] => BX_MSG_TALK_CLASS_CUSTOM,
  1221. )))
  1222. )
  1223. $aResult['lot'] = $aChat[$this->_oConfig->CNF['FIELD_ID']];
  1224. echoJson($aResult);
  1225. }
  1226. /**
  1227. * Updats participants list (occurs when create new lost with specified participants or update already existed list)
  1228. * @return string with json
  1229. */
  1230. public function saveParticipantsList($aParticipants, $iLotId = 0, $bIsBlock = 0)
  1231. {
  1232. $iLotIdOr = $iLotId;
  1233. $bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true;
  1234. if ($iLotId && !($this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $bCheckAction))
  1235. return ['code' => 1, 'message' => _t('_bx_messenger_lot_action_not_allowed')];
  1236. $CNF = &$this->_oConfig->CNF;
  1237. if (!$iLotId) {
  1238. $aLot = $this->_oDb->findLotByParams(array(
  1239. $CNF['FIELD_PARTICIPANTS'] => $aParticipants,
  1240. $CNF['FIELD_TYPE'] => BX_IM_TYPE_PRIVATE,
  1241. $CNF['FIELD_CLASS'] => BX_MSG_TALK_CLASS_CUSTOM,
  1242. ));
  1243. if (!empty($aLot))
  1244. $iLotId = $aLot[$this->_oConfig->CNF['FIELD_ID']];
  1245. }
  1246. $oOriginalParts = [];
  1247. $aResult = ['code' => 0, 'message' => _t('_bx_messenger_save_part_success')];
  1248. if (!$iLotId) {
  1249. $iLotId = $this->_oDb->createNewLot($this->_iProfileId, ['title' => '', 'type' => BX_IM_TYPE_PRIVATE, 'url' => BX_IM_EMPTY_URL], $aParticipants);
  1250. $aResult['lot'] = $iLotId;
  1251. $this->onCreateLot($iLotId);
  1252. } else {
  1253. $oOriginalParts = $this->_oDb->getParticipantsList($iLotId);
  1254. if ($iLotIdOr == 0 && $iLotId) {
  1255. $aResult['lot'] = $iLotId;
  1256. } else {
  1257. if (!$this->_oDb->saveParticipantsList($iLotId, $aParticipants))
  1258. $aResult = ['code' => 2, 'message' => _t('_bx_messenger_lot_parts_error')];
  1259. }
  1260. }
  1261. $aRemoveParticipants = array_diff($oOriginalParts, $aParticipants);
  1262. $aNewParticipants = array_diff($aParticipants, $oOriginalParts);
  1263. foreach ($aNewParticipants as &$iPartId)
  1264. $this->onAddNewParticipant($iLotId, $iPartId);
  1265. foreach ($aRemoveParticipants as &$iPartId) {
  1266. $this->_oDb->deleteNewJot($iPartId, $iLotId);
  1267. $this->onRemoveParticipant($iLotId, $iPartId);
  1268. }
  1269. return $aResult;
  1270. }
  1271. public function actionSaveLotsParts()
  1272. {
  1273. if (!$this->isLogged())
  1274. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_logged'), 'reload' => 1));
  1275. $iLotId = bx_get('lot');
  1276. $bIsBlockVersion = +bx_get('is_block');
  1277. $aParticipants = $this->getParticipantsList(bx_get('participants'));
  1278. $aResult = array('message' => _t('_bx_messenger_save_part_failed'), 'code' => 1);
  1279. if (empty(bx_get('participants')))
  1280. return echoJson($aResult);
  1281. $aResult = $this->saveParticipantsList($aParticipants, $iLotId, $bIsBlockVersion);
  1282. if ($iLotId) {
  1283. $aHeader = $this->_oTemplate->getTalkHeader($iLotId, $this->_iProfileId, $bIsBlockVersion, true);
  1284. $aResult['header'] = $aHeader['title'];
  1285. $aResult['buttons'] = $aHeader['buttons'];
  1286. }
  1287. echoJson($aResult);
  1288. }
  1289. /**
  1290. * Removes specified lot
  1291. * @return string with json
  1292. */
  1293. public function actionDelete()
  1294. {
  1295. $iLotId = bx_get('lot');
  1296. $aResult = array('message' => _t('_bx_messenger_can_not_delete'), 'code' => 1);
  1297. $bAllowed = $this->_oDb->isAuthor($iLotId, $this->_iProfileId) || ($this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true);
  1298. if (!$iLotId || !$bAllowed)
  1299. return echoJson($aResult);
  1300. $CNF = &$this->_oConfig->CNF;
  1301. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  1302. if ($this->_oDb->deleteLot($iLotId)) {
  1303. $aResult = ['code' => 0];
  1304. $this->onDeleteLot($aLotInfo);
  1305. }
  1306. echoJson($aResult);
  1307. }
  1308. /**
  1309. * Clear specified lot
  1310. * @return string with json
  1311. */
  1312. public function actionClearHistory()
  1313. {
  1314. $iLotId = bx_get('lot');
  1315. $aResult = array('code' => 1, 'message' => _t('_Empty'));
  1316. $bAllowed = $this->_oDb->isAuthor($iLotId, $this->_iProfileId) || ($this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true);
  1317. if (!$iLotId || !$bAllowed) {
  1318. return echoJson($aResult);
  1319. }
  1320. if ($this->_oDb->clearLot($iLotId))
  1321. $aResult = array('code' => 0);
  1322. echoJson($aResult);
  1323. }
  1324. /**
  1325. * Removes specified jot
  1326. * @return string with json
  1327. */
  1328. public function actionDeleteJot()
  1329. {
  1330. $iJotId = bx_get('jot');
  1331. $bCompletely = (int) bx_get('completely');
  1332. $aReturn = $this->serviceDeleteJot($iJotId, $bCompletely);
  1333. echoJson($aReturn);
  1334. }
  1335. public function serviceDeleteJot($iJotId, $bCompletely = false)
  1336. {
  1337. $aResult = array('code' => 1);
  1338. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1339. if (empty($aJotInfo))
  1340. return $aResult;
  1341. $CNF = &$this->_oConfig->CNF;
  1342. $bIsAllowedToDelete = $this->_oDb->isAllowedToDeleteJot($iJotId, $this->_iProfileId, $aJotInfo[$CNF['FIELD_MESSAGE_AUTHOR']]);
  1343. if (!$bIsAllowedToDelete)
  1344. return $aResult;
  1345. $bIsLotAuthor = $this->_oDb->isAuthor($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId);
  1346. $bDelete = $bCompletely || $CNF['REMOVE_MESSAGE_IMMEDIATELY'];
  1347. if ($this->_oDb->deleteJot($iJotId, $this->_iProfileId, $bDelete)) {
  1348. if ($bDelete)
  1349. $this->onDeleteJot($aJotInfo);
  1350. $aResult = array('code' => 0, 'html' => !$bCompletely ? $this->_oTemplate->getMessageIcons($iJotId, 'delete', $bIsLotAuthor || isAdmin()) : '');
  1351. }
  1352. return $aResult;
  1353. }
  1354. /**
  1355. * React on defined jot
  1356. * @return string with json
  1357. */
  1358. public function actionJotReaction()
  1359. {
  1360. $iJotId = bx_get('jot');
  1361. $aEmoji = bx_get('emoji');
  1362. $sAction = bx_get('action');
  1363. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1364. $aResult = array('code' => 1);
  1365. $CNF = &$this->_oConfig->CNF;
  1366. if (empty($aJotInfo))
  1367. return echoJson($aResult);
  1368. $bIsParticipant = $this->_oDb->isParticipant($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId);
  1369. $aLotInfo = $this->_oDb->getLotInfoById($aJotInfo[$CNF['FIELD_MESSAGE_FK']]);
  1370. if (!$bIsParticipant) {
  1371. if ($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
  1372. return echoJson(array('code' => 1, 'msg' => _t('_bx_messenger_not_participant')));
  1373. if (!empty($aLotInfo))
  1374. $this->_oDb->addMemberToParticipantsList($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId);
  1375. }
  1376. if ($sAction == BX_JOT_REACTION_ADD && $this->_oDb->addJotReaction($iJotId, $this->_iProfileId, $aEmoji)) {
  1377. $this->onReactJot($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $iJotId, $aLotInfo[$CNF['FIELD_AUTHOR']], $sAction);
  1378. $aResult = array('code' => 0, 'html' => $this->_oTemplate->getJotReactions($iJotId));
  1379. }
  1380. echoJson($aResult);
  1381. }
  1382. public function actionUpdateReaction()
  1383. {
  1384. $iJotId = bx_get('jot');
  1385. $sEmoji = bx_get('emoji_id');
  1386. $sAction = bx_get('action');
  1387. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1388. $aResult = array('code' => 1);
  1389. $CNF = &$this->_oConfig->CNF;
  1390. if (empty($aJotInfo))
  1391. return echoJson($aResult);
  1392. $bIsParticipant = $this->_oDb->isParticipant($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $this->_iProfileId);
  1393. $aLotInfo = $this->_oDb->getLotInfoById($aJotInfo[$CNF['FIELD_MESSAGE_FK']]);
  1394. if (!$bIsParticipant && $sAction == BX_JOT_REACTION_ADD && $aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
  1395. return echoJson($aResult);
  1396. if ($this->_oDb->updateReaction($iJotId, $this->_iProfileId, $sEmoji, $sAction)) {
  1397. $this->onReactJot(
  1398. $aJotInfo[$CNF['FIELD_MESSAGE_FK']],
  1399. $iJotId,
  1400. $aLotInfo[$CNF['FIELD_AUTHOR']],
  1401. $sAction === BX_JOT_REACTION_ADD ? BX_JOT_REACTION_ADD : BX_JOT_REACTION_REMOVE
  1402. );
  1403. $aResult = array('code' => 0);
  1404. }
  1405. echoJson($aResult);
  1406. }
  1407. /**
  1408. * Get body of the jot
  1409. * @return string with json
  1410. */
  1411. public function actionViewJot()
  1412. {
  1413. $iJotId = bx_get('jot');
  1414. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1415. if (empty($aJotInfo) || !(isAdmin() || $this->_oDb->isAuthor($aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)))
  1416. return echoJson(array('code' => 1));
  1417. $aResult = array('code' => 0, 'html' => $this->_oTemplate->getJotsBody($iJotId));
  1418. echoJson($aResult);
  1419. }
  1420. /**
  1421. * Get body of the jot
  1422. * @return string with json
  1423. */
  1424. public function actionGetJotPreview()
  1425. {
  1426. $iJotId = bx_get('jot');
  1427. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1428. if (empty($aJotInfo) || !(isAdmin() || $this->_oDb->isAuthor($aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)))
  1429. return echoJson(array('code' => 1));
  1430. $aResult = array('code' => 0, 'html' => $this->_oTemplate->getReplyPreview($iJotId));
  1431. echoJson($aResult);
  1432. }
  1433. /**
  1434. * Jot edit panel
  1435. * @return string with json
  1436. */
  1437. public function actionEditJotForm()
  1438. {
  1439. $iJotId = bx_get('jot');
  1440. $aResult = array('code' => 1);
  1441. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1442. $mixedResult = $this->_oDb->isAllowedToEditJot($iJotId, $this->_iProfileId);
  1443. if (empty($aJotInfo) || $mixedResult !== true)
  1444. return echoJson($aResult);
  1445. echoJson(['code' => 0, 'html' => $this->_oTemplate->getEditJotArea($iJotId)]);
  1446. }
  1447. public function actionEditJot()
  1448. {
  1449. $iJotId = bx_get('jot');
  1450. $aResult = ['code' => 1];
  1451. $sMessageData = bx_get('message');
  1452. if (!($aJotInfo = $this->_oDb->getJotById($iJotId)))
  1453. return echoJson($aResult);
  1454. $sMessage = $this->prepareMessageToDb($sMessageData);
  1455. $mixedResult = $this->_oDb->isAllowedToEditJot($iJotId, $this->_iProfileId);
  1456. if ($mixedResult !== true)
  1457. return echoJson($aResult);
  1458. if ($this->_oDb->editJot($iJotId, $this->_iProfileId, $sMessage)) {
  1459. $aResult = ['code' => 0, 'html' => $this->_oTemplate->getMessageIcons($iJotId, 'edit')];
  1460. $this->onUpdateJot($aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE_FK']], $iJotId, $aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE_AUTHOR']]);
  1461. }
  1462. echoJson($aResult);
  1463. }
  1464. public function actionSaveJotItem()
  1465. {
  1466. if (!$this->isLogged())
  1467. return echoJson(array('code' => 1));
  1468. $iJotId = bx_get('jot');
  1469. $aJotInfo = $this->_oDb->getJotById($iJotId);
  1470. $aResult = array('code' => 1, 'msg' => _t('_bx_messenger_nothing_found'));
  1471. if (empty($aJotInfo))
  1472. return echoJson($aResult);
  1473. $CNF = &$this->_oConfig->CNF;
  1474. if (!$this->isAvailable($aJotInfo[$CNF['FIELD_MESSAGE_FK']]))
  1475. return echoJson(array('code' => 1, 'msg' => _t('_bx_messenger_not_participant')));
  1476. $bSaved = $this->_oDb->isJotSaved($iJotId, $this->_iProfileId);
  1477. $bCode = false;
  1478. if ($bSaved)
  1479. $bCode = $this->_oDb->deleteSavedJotItems($iJotId, $this->_iProfileId);
  1480. else
  1481. $bCode = $this->_oDb->saveJotItem($iJotId, $this->_iProfileId);
  1482. echoJson(array('code' => (int) !$bCode, 'status' => (int) !$bSaved));
  1483. }
  1484. /**
  1485. * Removes specified file by id
  1486. * @return string with json
  1487. */
  1488. public function actionDeleteFile()
  1489. {
  1490. $iFileId = bx_get('id');
  1491. $aResult = array('code' => 1, 'message' => _t('_bx_messenger_post_file_not_found'));
  1492. if (!$iFileId)
  1493. return echoJson($aResult);
  1494. $CNF = &$this->_oConfig->CNF;
  1495. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  1496. $aFile = $oStorage->getFile($iFileId);
  1497. if (empty($aFile))
  1498. return echoJson($aResult);
  1499. $bIsAllowedToDelete = $this->_oDb->isAllowedToDeleteJot($aFile[$CNF['FIELD_ST_JOT']], $this->_iProfileId);
  1500. if (!$bIsAllowedToDelete)
  1501. return echoJson($aResult);
  1502. $aJotInfo = $this->_oDb->getJotById($aFile[$CNF['FIELD_ST_JOT']]);
  1503. $this->_oDb->removeFileFromJot($aJotInfo[$CNF['FIELD_MESSAGE_ID']], $iFileId);
  1504. if ($oStorage->deleteFile($iFileId, $this->_iProfileId)) {
  1505. $aResult = array('code' => 0);
  1506. $aJotFiles = $this->_oDb->getJotFiles($aFile[$CNF['FIELD_ST_JOT']]);
  1507. if (count($aJotFiles) == 0 && !$aJotInfo[$CNF['FIELD_MESSAGE']] && $this->_oDb->deleteJot($aJotInfo[$CNF['FIELD_MESSAGE_ID']], $this->_iProfileId)) {
  1508. $aResult['empty_jot'] = 1;
  1509. $this->onDeleteJot($aJotInfo);
  1510. } else
  1511. $this->onUpdateJot($aJotInfo[$CNF['FIELD_MESSAGE_FK']], $aJotInfo[$CNF['FIELD_MESSAGE_ID']], $aJotInfo[$CNF['FIELD_MESSAGE_AUTHOR']]);
  1512. }
  1513. echoJson($aResult);
  1514. }
  1515. /**
  1516. * Leave talk with defined id
  1517. * @return string with json
  1518. */
  1519. public function actionLeave()
  1520. {
  1521. $iLotId = bx_get('lot');
  1522. if (!$iLotId || !$this->_oDb->isParticipant($iLotId, $this->_iProfileId)) {
  1523. return echoJson(['message' => _t('_bx_messenger_not_participant'), 'code' => 1]);
  1524. }
  1525. if ($this->_oDb->isAuthor($iLotId, $this->_iProfileId))
  1526. return echoJson(['message' => _t('_bx_messenger_cant_leave'), 'code' => 1]);
  1527. if ($this->_oDb->leaveLot($iLotId, $this->_iProfileId))
  1528. return echoJson(['message' => _t('_bx_messenger_successfully_left'), 'code' => 0]);
  1529. }
  1530. /**
  1531. * Block notifications from specified lot(conversation)
  1532. * @return string with json
  1533. */
  1534. public function actionMute()
  1535. {
  1536. $iLotId = bx_get('lot');
  1537. if ($iLotId && $this->isAvailable($iLotId)) {
  1538. $bMuted = $this->_oDb->muteLot($iLotId, $this->_iProfileId);
  1539. return echoJson(array('code' => $bMuted, 'title' => $bMuted ? _t('_bx_messenger_lots_menu_mute_info_on') : _t('_bx_messenger_lots_menu_mute_info_off')));
  1540. }
  1541. return echoJson(array('code' => 1));
  1542. }
  1543. /**
  1544. * Mark lot as favorite
  1545. * @return string with json
  1546. */
  1547. public function actionStar()
  1548. {
  1549. $iLotId = bx_get('lot');
  1550. if ($iLotId) {
  1551. $bStar = $this->_oDb->starLot($iLotId, $this->_iProfileId);
  1552. return echoJson(array('code' => $bStar, 'title' => !$bStar ? _t('_bx_messenger_lots_menu_star_on') : _t('_bx_messenger_lots_menu_star_off')));
  1553. }
  1554. return echoJson(array('code' => 1));
  1555. }
  1556. /**
  1557. * Returns number of lots with at least one unread message
  1558. * @return int
  1559. */
  1560. public function serviceGetUpdatedLotsNum($iProfileId = 0)
  1561. {
  1562. if (!$this->isLogged())
  1563. return array();
  1564. $aLots = $this->_oDb->getLotsWithUnreadMessages($iProfileId ? $iProfileId : $this->_iProfileId);
  1565. return sizeof($aLots);
  1566. }
  1567. /**
  1568. * Common function to send notifications to the Talk's participants
  1569. * @param $iLotId integer Talk's id
  1570. * @param $iJotId integer message id
  1571. * @return bool|string
  1572. */
  1573. private function sendMessageNotification($iLotId, $iJotId)
  1574. {
  1575. $aReceived = [];
  1576. if (!$iLotId || !$iJotId)
  1577. return false;
  1578. $CNF = &$this->_oConfig->CNF;
  1579. $aJot = $this->_oDb->getJotById($iJotId);
  1580. $sMessage = $aJot[$CNF['FIELD_MESSAGE']];
  1581. if ($sMessage) {
  1582. $sMessage = preg_replace('/<br\s?\/?>/i', "\r\n", $sMessage);
  1583. $sMessage = html2txt($sMessage);
  1584. $sMessage = get_mb_substr($sMessage, 0, (int) $CNF['PARAM_PUSH_NOTIFICATIONS_DEFAULT_SYMBOLS_NUM']);
  1585. // find mentions in the message and send notifications
  1586. if ($CNF['USE_MENTIONS'] && preg_match_all('/<a[^>]*class="bx-mention[^"]*"[^>]*data-id="(\d+)".*?<\/a>/i', $aJot[$CNF['FIELD_MESSAGE']], $aMatches)) {
  1587. list(, $aMentionedProfiles) = $aMatches;
  1588. if ($aMentionedProfiles) {
  1589. $this->sendNotification($iLotId, $iJotId, $sMessage, $aReceived, $aMentionedProfiles, BX_MSG_NTFS_MENTION);
  1590. $aReceived = array_diff($aReceived, $aMentionedProfiles);
  1591. }
  1592. }
  1593. }
  1594. return $this->sendNotification($iLotId, $iJotId, $sMessage, $aReceived);
  1595. }
  1596. /**
  1597. * Sends push notification to the Talk's participants through the Notifications module or
  1598. * using Messenger's push notifications settings.
  1599. *
  1600. * @param $iLotId
  1601. * @param $iJotId
  1602. * @param $sMessage
  1603. * @param array $aReceived
  1604. * @param array $aRecipients
  1605. * @param string $sType
  1606. * @return bool|string
  1607. */
  1608. private function sendNotification($iLotId, $iJotId, $sMessage, $aReceived = [], $aRecipients = [], $sType = BX_MSG_NTFS_MESSAGE)
  1609. {
  1610. // check if the Notifications module is installed and send notifications through it
  1611. if ($this->_oDb->isModuleByName('bx_notifications'))
  1612. return $this->sendNotifications($iLotId, $iJotId, $aReceived, $aRecipients, $sType);
  1613. }
  1614. /**
  1615. * Proper the list of the participants whom to send notifications
  1616. * @param $iLotId
  1617. * @param $iJotId
  1618. * @param array $aOnlineUsers
  1619. * @param array $aRecipients
  1620. * @param string $sType
  1621. * @return bool
  1622. */
  1623. public function sendNotifications($iLotId, $iJotId, $aOnlineUsers = [], $aRecipients = [], $sType = BX_MSG_NTFS_MESSAGE)
  1624. {
  1625. $CNF = &$this->_oConfig->CNF;
  1626. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  1627. $sModule = $this->_oConfig->getObject('alert');
  1628. if (empty($aLotInfo))
  1629. return false;
  1630. $aPartList = $this->_oDb->getParticipantsList($iLotId, true, $this->_iProfileId);
  1631. if ((int) $aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_BROADCAST) {
  1632. $iJotCount = $this->_oDb->getJotsNumber($iLotId, 0);
  1633. if ($iJotCount >= 2)
  1634. $aPartList = $this->_oDb->getBroadcastParticipants($iLotId);
  1635. }
  1636. if (empty($aPartList))
  1637. return false;
  1638. if (!empty($aRecipients)) {
  1639. $aDiff = array_diff($aRecipients, $aPartList);
  1640. if (!empty($aDiff) && (int) $aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE) // in case if the mentioned profiles are not in the participants list
  1641. $aPartList = array_diff($aRecipients, $aDiff);
  1642. else
  1643. $aPartList = $aRecipients;
  1644. }
  1645. $bIsMention = ($sType == BX_MSG_NTFS_MENTION);
  1646. foreach ($aPartList as &$iPart) {
  1647. if (array_search($iPart, $aOnlineUsers) !== FALSE || $this->_oDb->isMuted($iLotId, $iPart))
  1648. continue;
  1649. $iCount = $this->_oDb->getSentNtfsNumber($iPart, $iLotId);
  1650. if ($iCount >= (int) $CNF['MAX_NTFS_NUMBER'] && !$bIsMention)
  1651. continue;
  1652. bx_alert($sModule, !$bIsMention ? 'got_jot_ntfs' : 'got_mention_ntfs', $iLotId, $this->_iProfileId, array(
  1653. 'object_author_id' => $iPart,
  1654. 'recipient_id' => $iPart,
  1655. 'subobject_id' => $iJotId,
  1656. 'source' => $sModule . '_' . $iJotId,
  1657. 'source_mac' => $sModule . '_' . $this->_iProfileId . '_' . $iJotId,
  1658. ));
  1659. }
  1660. return true;
  1661. }
  1662. /**
  1663. * Creates template with member's avatar, name and etc... It is used when member posts a message to add message to member history immediately
  1664. * @return string json
  1665. */
  1666. public function actionLoadMembersTemplate()
  1667. {
  1668. $sTemplate = '';
  1669. if ($this->isLogged())
  1670. $sTemplate = $this->_oTemplate->getMembersJotTemplate($this->_iProfileId);
  1671. echoJson(
  1672. array(
  1673. 'data' => $sTemplate
  1674. )
  1675. );
  1676. }
  1677. /**
  1678. * Delete all content by profile ID
  1679. * @param object oAlert
  1680. * @return boolean
  1681. */
  1682. public function serviceDeleteHistoryByAuthor($oAlert)
  1683. {
  1684. return $oAlert->iObject && !empty($oAlert->aExtras['delete_with_content']) ?
  1685. $this->_oDb->deleteProfileInfo($oAlert->iObject) : false;
  1686. }
  1687. /**
  1688. * Process alerts actions
  1689. * @param object oAlert
  1690. * @return boolean
  1691. */
  1692. public function serviceResponse($oAlert)
  1693. {
  1694. $CNF = &$this->_oConfig->CNF;
  1695. $iGroupProfileId = 0;
  1696. $iExecutor = 0;
  1697. $sModule = '';
  1698. if ($oAlert->sAction == 'fan_added') {
  1699. $iGroupProfileId = $oAlert->iObject;
  1700. $iExecutor = $oAlert->aExtras['performer_id'];
  1701. $sModule = $oAlert->sUnit;
  1702. } else if ($oAlert->sAction == 'connection_removed') {
  1703. $iGroupProfileId = BxDolProfile::getInstance($oAlert->aExtras['initiator'])->getContentId();
  1704. $iExecutor = $oAlert->iSender;
  1705. $sModule = str_replace('_fans', '', $oAlert->sUnit);
  1706. }
  1707. if (BxDolRequest::serviceExists($sModule, 'is_group_profile') && $iGroupProfileId) {
  1708. $aGroupInfo = BxDolService::call($sModule, 'get_info', array($iGroupProfileId, false));
  1709. if (!empty($aGroupInfo) && is_array($aGroupInfo)) {
  1710. $oModule = BxDolModule::getInstance($sModule);
  1711. if ($oModule->_oConfig) {
  1712. $oMCNF = $oModule->_oConfig->CNF;
  1713. $sUrl = "i={$oMCNF['URI_VIEW_ENTRY']}&id=" . $aGroupInfo[$oMCNF['FIELD_ID']];
  1714. if ($sUrl && $aTalk = $this->_oDb->getLotByUrl($sUrl)) {
  1715. if ($oAlert->sAction == 'fan_added' && BxDolService::call($sModule, 'check_allowed_view_for_profile', array($aGroupInfo)) === CHECK_ACTION_RESULT_ALLOWED) {
  1716. if (!$this->_oDb->isParticipant($aTalk[$oMCNF['FIELD_ID']], $iExecutor))
  1717. $this->_oDb->addMemberToParticipantsList($aTalk[$oMCNF['FIELD_ID']], $iExecutor);
  1718. } else if ($oAlert->sAction == 'connection_removed' && $this->_oDb->isParticipant($aTalk[$oMCNF['FIELD_ID']], $iExecutor)) {
  1719. $this->_oDb->removeParticipant($aTalk[$oMCNF['FIELD_ID']], $iExecutor);
  1720. }
  1721. }
  1722. }
  1723. }
  1724. }
  1725. }
  1726. /**
  1727. * Parse jot's link (repost)
  1728. * @param object oAlert
  1729. * @return string json
  1730. */
  1731. public function actionParseLink()
  1732. {
  1733. $sUrl = bx_get('link');
  1734. $iJotId = (int) bx_get('jot_id');
  1735. $bDontAttach = (int) bx_get('dont_attach');
  1736. $aUrl = $this->_oConfig->isJotLink($sUrl);
  1737. if (!empty($aUrl)) {
  1738. $aJotInfo = $this->_oDb->getJotById($aUrl['id']);
  1739. if (!$this->_oDb->isParticipant($aJotInfo[$this->_oConfig->CNF['FIELD_MESSAGE_FK']], $this->_iUserId))
  1740. return echoJson(array('code' => 0));
  1741. $sHTML = $this->_oTemplate->getJotAsAttachment($aUrl['id']);
  1742. if ($sHTML && !$bDontAttach)
  1743. $this->_oDb->addAttachment($iJotId, $aUrl['id'], BX_ATT_TYPE_REPOST);
  1744. return echoJson(array('code' => 0, 'html' => $sHTML));
  1745. }
  1746. echoJson(array('code' => 1));
  1747. }
  1748. /**
  1749. * Parses link from the message
  1750. * @param object oAlert
  1751. * @return json
  1752. */
  1753. public function actionGetAttachment()
  1754. {
  1755. $iJotId = (int) bx_get('jot_id');
  1756. if ($iJotId) {
  1757. $aJot = $this->_oDb->getJotById($iJotId);
  1758. if (!empty($aJot) && $this->_oDb->isParticipant($aJot[$this->_oConfig->CNF['FIELD_MESSAGE_FK']], $this->_iProfileId)) {
  1759. if (($aAttachment = $this->_oTemplate->getAttachment($aJot, true)) && isset($aAttachment[BX_ATT_GROUPS_ATTACH]))
  1760. return echoJson(['code' => 0, 'html' => $aAttachment[BX_ATT_GROUPS_ATTACH]]);
  1761. }
  1762. }
  1763. echoJson(['code' => 1]);
  1764. }
  1765. /**
  1766. * Returns HTML popup form for video recording
  1767. */
  1768. public function actionGetRecordVideoForm()
  1769. {
  1770. header('Content-type: text/html; charset=utf-8');
  1771. echo $this->_oTemplate->getVideoRecordingForm($this->_iUserId);
  1772. exit;
  1773. }
  1774. /**
  1775. * Allows to delete/upload files in messages
  1776. * @return string
  1777. */
  1778. public function actionUploadTempFile()
  1779. {
  1780. if (!$this->isLogged()) {
  1781. echo _t('_bx_messenger_not_logged');
  1782. exit;
  1783. }
  1784. $CNF = &$this->_oConfig->CNF;
  1785. switch ($_SERVER['REQUEST_METHOD']) {
  1786. case 'DELETE':
  1787. if ($sName = @file_get_contents("php://input"))
  1788. parse_str($sName, $aName);
  1789. if (!empty($aName) && @unlink(BX_DIRECTORY_PATH_TMP . basename($aName['name'])))
  1790. echo 'OK';
  1791. break;
  1792. case 'POST':
  1793. $oStorage = new BxMessengerStorage($CNF['OBJECT_STORAGE']);
  1794. if (!$oStorage) {
  1795. echo 0;
  1796. exit;
  1797. }
  1798. $aFiles = array_filter($_FILES, function ($aFile, $sName) use ($CNF) {
  1799. return stripos($sName, $CNF['FILES_UPLOADER']) !== FALSE;
  1800. }, ARRAY_FILTER_USE_BOTH);
  1801. $aUploader = current($aFiles);
  1802. if (!empty($aUploader) && $oStorage->isValidFileExt($aUploader['name'])) {
  1803. $sTempFile = $aUploader['tmp_name'];
  1804. $sTargetFile = BX_DIRECTORY_PATH_TMP . basename($aUploader['name']);
  1805. move_uploaded_file($sTempFile, $sTargetFile);
  1806. echo 'OK';
  1807. }
  1808. }
  1809. return '';
  1810. }
  1811. /**
  1812. * Uploads recorded video file
  1813. */
  1814. public function actionUploadVideoFile()
  1815. {
  1816. $oStorage = new BxMessengerStorage($this->_oConfig->CNF['OBJECT_STORAGE']);
  1817. if (!$oStorage || !isset($_POST['name']) || empty($_FILES)) {
  1818. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_send_message_no_video')));
  1819. }
  1820. if ($_FILES['file'] && $oStorage->isValidFileExt($_FILES['file']['name'])) {
  1821. $sTempFile = $_FILES['file']['tmp_name'];
  1822. $sTargetFile = BX_DIRECTORY_PATH_TMP . basename($_POST['name']);
  1823. if (move_uploaded_file($sTempFile, $sTargetFile))
  1824. return echoJson(array('code' => 0));
  1825. }
  1826. echoJson(array('code' => 1, 'message' => _t('_bx_messenger_send_message_no_video')));
  1827. }
  1828. /**
  1829. * Checks is the file has valid extension to upload.
  1830. */
  1831. public function actionIsValidFile()
  1832. {
  1833. $oStorage = new BxMessengerStorage($this->_oConfig->CNF['OBJECT_STORAGE']);
  1834. if (!$oStorage)
  1835. return echoJson(array('code' => 1));
  1836. $sFileName = bx_get('name');
  1837. if ($sFileName && (int) $oStorage->isValidFileExt($sFileName)) {
  1838. $sIconFile = $oStorage->getFontIconNameByFileName($sFileName);
  1839. return echoJson(array('code' => 0, 'thumbnail' => $sIconFile ? $sIconFile : '', 'is_image' => (int) $oStorage->isImageExt($sFileName)));
  1840. }
  1841. echoJson(array('code' => 1));
  1842. }
  1843. /**
  1844. * Allows to download files from the talk
  1845. * @param $iFileId
  1846. * @return string
  1847. */
  1848. public function actionDownloadFile($iFileId)
  1849. {
  1850. if (!$iFileId || !$this->_iProfileId)
  1851. return '';
  1852. $CNF = &$this->_oConfig->CNF;
  1853. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  1854. $aFile = $oStorage->getFile((int) $iFileId);
  1855. if (empty($aFile)) {
  1856. echo _t('_bx_messenger_post_file_not_found');
  1857. exit;
  1858. }
  1859. if (isset($aFile[$CNF['FIELD_ST_JOT']])) {
  1860. $iLotId = $this->_oDb->getLotByJotId($aFile[$CNF['FIELD_ST_JOT']], true);
  1861. if (!$iLotId || !$this->_oDb->isParticipant($iLotId, $this->_iProfileId)) {
  1862. echo _t('_bx_messenger_not_participant');
  1863. exit;
  1864. }
  1865. }
  1866. $sToken = $oStorage->genToken($iFileId);
  1867. return $oStorage->download($aFile[$CNF['FIELD_ST_REMOTE']], $sToken);
  1868. }
  1869. /**
  1870. * Allow to zoom image from talk's history
  1871. * @param int $iStorageId file id
  1872. * @param int $iWidth width of the window
  1873. * @param int $iHeight height of the window
  1874. * @return string html content
  1875. */
  1876. function actionGetBigImage($iStorageId, $iWidth, $iHeight)
  1877. {
  1878. if (!$iStorageId)
  1879. return '';
  1880. $iWidth = (int) $iWidth * 0.9;
  1881. $iHeight = (int) $iHeight * 0.9;
  1882. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  1883. $aFile = $oStorage->getFile((int) $iStorageId);
  1884. $sFileUrl = $oStorage->getFileUrlById($iStorageId);
  1885. if (!empty($aFile)) {
  1886. $aInfo = @getimagesize($sFileUrl);
  1887. if (empty($aInfo)) {
  1888. echo MsgBox(_t('_bx_messenger_post_file_not_found'));
  1889. exit;
  1890. }
  1891. if ($aInfo[0] <= $iWidth && $aInfo[1] <= $iHeight) {
  1892. $iWidth = (int) $aInfo[0];
  1893. $iHeight = (int) $aInfo[1];
  1894. } else {
  1895. $fXRatio = $aInfo[0] / $iWidth;
  1896. $fYRatio = $aInfo[1] / $iHeight;
  1897. if ($fXRatio > $fYRatio)
  1898. $iHeight = 'auto';
  1899. else
  1900. $iWidth = 'auto';
  1901. }
  1902. }
  1903. echo $this->_oTemplate->parseHtmlByName('big_image.html', array(
  1904. 'height' => $iHeight,
  1905. 'width' => $iWidth,
  1906. 'url' => $oStorage->getFileUrlById((int) $iStorageId)
  1907. ));
  1908. exit;
  1909. }
  1910. /**
  1911. * Allows to register Messenger's actions in Notifications module through the Alerts
  1912. * @return array
  1913. */
  1914. public function serviceGetNotificationsData()
  1915. {
  1916. $sModule = $this->_aModule['name'];
  1917. return [
  1918. 'handlers' => [
  1919. ['group' => $sModule, 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'got_jot_ntfs', 'module_name' => $sModule, 'module_method' => 'get_message_content', 'module_class' => 'Module'],
  1920. ['group' => $sModule, 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'delete_jot_ntfs'],
  1921. ['group' => "{$sModule}_mention", 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'got_mention_ntfs', 'module_name' => $sModule, 'module_method' => 'get_message_content', 'module_class' => 'Module'],
  1922. ['group' => "{$sModule}_mention", 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'delete_mention_ntfs'],
  1923. ['group' => "{$sModule}_broadcast", 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'got_broadcast_ntfs', 'module_name' => $sModule, 'module_method' => 'get_broadcast_content', 'module_class' => 'Module'],
  1924. ['group' => "{$sModule}_broadcast", 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'delete_broadcast_ntfs']
  1925. ],
  1926. 'settings' => [
  1927. ['group' => $sModule, 'unit' => $sModule, 'action' => 'got_jot_ntfs', 'types' => ['personal']],
  1928. ['group' => "{$sModule}_mention", 'unit' => $sModule, 'action' => 'got_mention_ntfs', 'types' => ['personal']],
  1929. ['group' => "{$sModule}_broadcast", 'unit' => $sModule, 'action' => 'got_broadcast_ntfs', 'types' => ['personal']]
  1930. ],
  1931. 'alerts' => [
  1932. ['unit' => $sModule, 'action' => 'got_jot_ntfs'],
  1933. ['unit' => $sModule, 'action' => 'delete_jot_ntfs'],
  1934. ['unit' => $sModule, 'action' => 'got_mention_ntfs'],
  1935. ['unit' => $sModule, 'action' => 'delete_mention_ntfs'],
  1936. ['unit' => $sModule, 'action' => 'got_broadcast_ntfs'],
  1937. ['unit' => $sModule, 'action' => 'delete_broadcast_ntfs']
  1938. ]
  1939. ];
  1940. }
  1941. public function serviceGetBroadcastContent($aEvent)
  1942. {
  1943. $CNF = &$this->_oConfig->CNF;
  1944. $aJotInfo = $this->_oDb->getJotById($aEvent['subobject_id']);
  1945. $aLotInfo = $this->_oDb->getLotByJotId($aEvent['subobject_id'], false);
  1946. if (empty($aJotInfo) || empty($aLotInfo))
  1947. return array();
  1948. $sEntryUrl = $this->_oConfig->getRepostUrl($aJotInfo[$CNF['FIELD_MESSAGE_ID']]);
  1949. $iType = $aLotInfo[$CNF['FIELD_TYPE']];
  1950. $sTitle = _t('_bx_messenger_broadcast_message_title');
  1951. // replace br to spaces and truncate the line
  1952. $sMessage = _t('_bx_messenger_broadcast_message_body');
  1953. $sTruncatedMessage = '';
  1954. if ($aJotInfo[$CNF['FIELD_MESSAGE']]) {
  1955. $sTruncatedMessage = strmaxtextlen(preg_replace('/<br\W*?\/>|\n/', " ", $aJotInfo[$CNF['FIELD_MESSAGE']]), $CNF['PARAM_MAX_JOT_NTFS_MESSAGE_LENGTH']);
  1956. $sMessage = _t('_bx_messenger_txt_sample_comment_single', $sTruncatedMessage);
  1957. }
  1958. $aResult = [
  1959. 'entry_sample' => _t('_bx_messenger_message'),
  1960. 'entry_url' => $sEntryUrl,
  1961. 'entry_caption' => $sTitle,
  1962. 'entry_author' => $aEvent['object_owner_id'],
  1963. 'subentry_sample' => $sMessage,
  1964. 'lang_key' => '_bx_messenger_txt_subobject_added_broadcast'
  1965. ];
  1966. $sSubject = _t("_bx_messenger_notification_subject_broadcast", BxDolProfile::getInstanceMagic($aEvent['owner_id'])->getDisplayName());
  1967. $sAlterBody = $sTruncatedMessage ?: _t('_bx_messenger_txt_sample_email_push', html2txt($sMessage));
  1968. $aResult['lang_key'] = [
  1969. 'site' => $aResult['lang_key'],
  1970. 'email' => $sAlterBody,
  1971. 'push' => $sAlterBody
  1972. ];
  1973. $aResult['settings'] = [
  1974. 'email' => [
  1975. 'subject' => $sSubject
  1976. ],
  1977. 'push' => [
  1978. 'subject' => $sSubject
  1979. ]
  1980. ];
  1981. bx_alert(
  1982. $this->_oConfig->getObject('alert'),
  1983. 'before_broadcast_notification',
  1984. $aEvent['subobject_id'],
  1985. $aEvent['object_owner_id'],
  1986. ['data' => &$aResult, 'talk' => $aLotInfo, 'message' => $aJotInfo]
  1987. );
  1988. return $aResult;
  1989. }
  1990. /**
  1991. * Jot info for Notifications module
  1992. * @param array Input alert params
  1993. * @return array with info for notifications
  1994. */
  1995. public function serviceGetMessageContent($aEvent)
  1996. {
  1997. $CNF = &$this->_oConfig->CNF;
  1998. $aJotInfo = $this->_oDb->getJotById($aEvent['subobject_id']);
  1999. $aLotInfo = $this->_oDb->getLotByJotId($aEvent['subobject_id'], false);
  2000. if (empty($aJotInfo) || empty($aLotInfo))
  2001. return array();
  2002. $sType = BX_MSG_NTFS_MESSAGE;
  2003. if ($aEvent['action'] === 'got_mention_ntfs')
  2004. $sType = BX_MSG_NTFS_MENTION;
  2005. $sEntryUrl = $this->_oConfig->getRepostUrl($aJotInfo[$CNF['FIELD_MESSAGE_ID']]);
  2006. $sEntryUrlApi = $this->_oConfig->getRepostUrlApi($aLotInfo[$CNF['FIELD_HASH']]);
  2007. $iType = $aLotInfo[$CNF['FIELD_TYPE']];
  2008. $sTitle = isset($aLotInfo[$CNF['FIELD_TITLE']]) && $aLotInfo[$CNF['FIELD_TITLE']] ?
  2009. $aLotInfo[$CNF['FIELD_TITLE']] : _t('_bx_messenger_lots_private_lot');
  2010. if ($this->_oDb->isLinkedTitle($iType)) {
  2011. $sTitle = $this->_oDb->isLinkedTitle($iType) ? _t('_bx_messenger_linked_title', $sTitle) : _t($sTitle);
  2012. $sEntryUrl = $this->_oConfig->getPageLink($aLotInfo[$CNF['FIELD_URL']]);
  2013. }
  2014. $bAttachmentFiles = $this->_oConfig->isAttachmentType($aJotInfo, BX_ATT_TYPE_FILES);
  2015. $bAttachmentGiphy = $this->_oConfig->isAttachmentType($aJotInfo, BX_ATT_TYPE_GIPHY);
  2016. $bAttachmentReply = $this->_oConfig->isAttachmentType($aJotInfo, BX_ATT_TYPE_REPLY);
  2017. $bAttachmentRepost = $this->_oConfig->isAttachmentType($aJotInfo, BX_ATT_TYPE_REPOST);
  2018. $sTruncatedMessage = $sMessage = '';
  2019. // replace br to spaces and truncate the line
  2020. if ($aJotInfo[$CNF['FIELD_MESSAGE']] && !$bAttachmentRepost) {
  2021. $sTruncatedMessage = strmaxtextlen(preg_replace('/<br\W*?\/>|\n/', " ", $aJotInfo[$CNF['FIELD_MESSAGE']]), $CNF['PARAM_MAX_JOT_NTFS_MESSAGE_LENGTH']);
  2022. $sMessage = _t('_bx_messenger_txt_sample_comment_single', $sTruncatedMessage);
  2023. }
  2024. if ($bAttachmentFiles)
  2025. $sMessage = _t('_bx_messenger_txt_sample_comment_file_single', $this->_oDb->getJotFiles($aJotInfo[$CNF['FIELD_MESSAGE_ID']], true));
  2026. elseif ($bAttachmentGiphy)
  2027. $sMessage = _t('_bx_messenger_txt_sample_comment_giphy_single');
  2028. elseif ($bAttachmentReply)
  2029. $sMessage = _t('_bx_messenger_txt_sample_comment_reply_single');
  2030. elseif ($bAttachmentRepost)
  2031. $sMessage = _t('_bx_messenger_txt_sample_comment_repost_single');
  2032. $aResult = array(
  2033. 'entry_sample' => _t('_bx_messenger_message'),
  2034. 'entry_url' => $sEntryUrl,
  2035. 'entry_url_api' => $sEntryUrlApi,
  2036. 'entry_caption' => $sTitle,
  2037. 'entry_author' => $aEvent['object_owner_id'],
  2038. 'subentry_sample' => $sMessage,
  2039. 'lang_key' => $bAttachmentFiles || $bAttachmentGiphy
  2040. ? '_bx_messenger_txt_subobject_added_single' : "_bx_messenger_txt_subobject_added_{$sType}"
  2041. );
  2042. $sSubject = _t("_bx_messenger_notification_subject_{$sType}", BxDolProfile::getInstanceMagic($aEvent['owner_id'])->getDisplayName());
  2043. $sAlterBody = $sTruncatedMessage ? $sTruncatedMessage : _t('_bx_messenger_txt_sample_email_push', html2txt($sMessage));
  2044. $aResult['lang_key'] = array(
  2045. 'site' => $aResult['lang_key'],
  2046. 'email' => $sAlterBody,
  2047. 'push' => $sAlterBody
  2048. );
  2049. $aResult['settings'] = array(
  2050. 'email' => array(
  2051. 'subject' => $sSubject
  2052. ),
  2053. 'push' => array(
  2054. 'subject' => $sSubject
  2055. )
  2056. );
  2057. bx_alert(
  2058. $this->_oConfig->getObject('alert'),
  2059. 'before_message_notification',
  2060. $aEvent['subobject_id'],
  2061. $aEvent['object_owner_id'],
  2062. ['data' => &$aResult, 'talk' => $aLotInfo, 'message' => $aJotInfo]
  2063. );
  2064. return $aResult;
  2065. }
  2066. /**
  2067. * Check if the user can read Lot messages
  2068. * @param array $aDataEntry contains Lot details
  2069. * @param boolean $isPerformAction used for compatibility with parent method
  2070. * @param bool $iProfileId profile id of the member show has to get notification
  2071. * @return int
  2072. */
  2073. public function serviceCheckAllowedViewForProfile($aDataEntry, $isPerformAction = false, $iProfileId = false)
  2074. {
  2075. if (!$iProfileId)
  2076. $iProfileId = $this->_iProfileId;
  2077. $mixedResult = null;
  2078. bx_alert(
  2079. 'system',
  2080. 'check_allowed_view',
  2081. 0,
  2082. 0,
  2083. array(
  2084. 'module' => $this->getName(),
  2085. 'content_info' => $aDataEntry,
  2086. 'profile_id' => $iProfileId,
  2087. 'override_result' => &$mixedResult
  2088. )
  2089. );
  2090. if ($mixedResult !== null)
  2091. return $mixedResult;
  2092. $CNF = &$this->_oConfig->CNF;
  2093. if (empty($aDataEntry))
  2094. return CHECK_ACTION_RESULT_ALLOWED;
  2095. return $this->_oDb->isParticipant($aDataEntry[$CNF['FIELD_ID']], $iProfileId, true) === TRUE
  2096. || $aDataEntry[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PUBLIC
  2097. || $aDataEntry[$CNF['FIELD_TYPE']] == BX_IM_TYPE_SETS ? CHECK_ACTION_RESULT_ALLOWED : CHECK_ACTION_RESULT_NOT_ALLOWED;
  2098. }
  2099. /**
  2100. * Execute the list of actions which are required when message is sent
  2101. * @param $iJotId
  2102. * @return false
  2103. */
  2104. public function onSendJot($iJotId)
  2105. {
  2106. $aJotInfo = $this->_oDb->getJotById($iJotId);
  2107. /**
  2108. * @hooks
  2109. * @hookdef hook-bx_messenger-send_jot 'bx_messenger', 'send_jot' - hook after a jot (message) was sent
  2110. * - $unit_name - equals `bx_messenger`
  2111. * - $action - equals `send_jot`
  2112. * - $object_id - jot (message) id
  2113. * - $sender_id - jot (message) author profile id
  2114. * - $extra_params - array of additional params with the following array keys:
  2115. * - `jot_info` - [array] jot (message) info array as key&value pairs
  2116. * @hook @ref hook-bx_messenger-send_jot
  2117. */
  2118. bx_alert($this->_oConfig->getObject('alert'), 'send_jot', $iJotId, $this->_iProfileId, [
  2119. 'jot_info' => $aJotInfo
  2120. ]);
  2121. $CNF = &$this->_oConfig->CNF;
  2122. $aConvoInfo = $this->_oDb->getLotByJotId($iJotId, false);
  2123. $iLotId = $aConvoInfo[$CNF['FIELD_ID']];
  2124. if (!$iLotId)
  2125. return false;
  2126. $aPartList = $this->_oDb->getParticipantsList($iLotId, true, $this->_iProfileId);
  2127. if ((int) $aConvoInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_BROADCAST) {
  2128. $iJotCount = $this->_oDb->getJotsNumber($iLotId, 0);
  2129. if ($iJotCount >= 2)
  2130. $aPartList = $this->_oDb->getBroadcastParticipants($iLotId);
  2131. else
  2132. return false;
  2133. }
  2134. if (empty($aPartList))
  2135. return false;
  2136. foreach ($aPartList as &$iPart)
  2137. /**
  2138. * @hooks
  2139. * @hookdef hook-bx_messenger-got_jot 'bx_messenger', 'got_jot' - hook after a lot (conversation) got a new jot (message)
  2140. * - $unit_name - equals `bx_messenger`
  2141. * - $action - equals `got_jot`
  2142. * - $object_id - lot (conversation) id
  2143. * - $sender_id - jot (message) author profile id
  2144. * - $extra_params - array of additional params with the following array keys:
  2145. * - `recipient_id` - [int] recipient profile id
  2146. * - `subobject_id` - [int] jot (message) id
  2147. * - `subobject_info` - [array] jot (message) info array as key&value pairs
  2148. * @hook @ref hook-bx_messenger-got_jot
  2149. */
  2150. bx_alert($this->_oConfig->getObject('alert'), 'got_jot', $iLotId, $this->_iProfileId, [
  2151. 'recipient_id' => $iPart,
  2152. 'subobject_id' => $iJotId,
  2153. 'subobject_info' => $aJotInfo
  2154. ]);
  2155. if (!$this->_oDb->getIntervalJotsCount($iLotId, $iJotId))
  2156. $this->sendMessageNotification($iLotId, $iJotId);
  2157. }
  2158. public function onDeleteJot($aJotInfo)
  2159. {
  2160. if (empty($aJotInfo))
  2161. return false;
  2162. $CNF = &$this->_oConfig->CNF;
  2163. $iLotId = $aJotInfo[$CNF['FIELD_MESSAGE_FK']];
  2164. $iJotId = $aJotInfo[$CNF['FIELD_MESSAGE_ID']];
  2165. $iProfileId = $aJotInfo[$CNF['FIELD_MESSAGE_AUTHOR']];
  2166. $sModule = $this->_oConfig->getObject('alert');
  2167. bx_alert($sModule, 'delete_jot', $iJotId, $this->_iProfileId, ['author_id' => $iProfileId, 'lot_id' => $iLotId]);
  2168. bx_alert($sModule, 'delete_jot_ntfs', $iLotId, $iProfileId, ['subobject_id' => $iJotId]);
  2169. bx_alert($sModule, 'delete_mention_ntfs', $iLotId, $iProfileId, ['subobject_id' => $iJotId]);
  2170. $this->_oDb->removeNotifications($iJotId);
  2171. }
  2172. public function onReactJot($iLotId, $iJotId, $iAuthor, $sAction = 'add')
  2173. {
  2174. bx_alert($this->_oConfig->getObject('alert'), "reaction_" . $sAction, $iJotId, $this->_iProfileId, array('author_id' => $iAuthor, 'lot_id' => $iLotId));
  2175. }
  2176. public function onUpdateJot($iLotId, $iJotId, $iProfileId = 0)
  2177. {
  2178. bx_alert($this->_oConfig->getObject('alert'), 'update_jot', $iJotId, $this->_iUserId, array('author_id' => $iProfileId, 'lot_id' => $iLotId));
  2179. }
  2180. public function onCreateLot($iLotId)
  2181. {
  2182. bx_alert($this->_oConfig->getObject('alert'), 'create_lot', $iLotId, $this->_iProfileId, $this->_oDb->getLotInfoById($iLotId));
  2183. }
  2184. public function onDeleteLot($aData)
  2185. {
  2186. if (empty($aData))
  2187. return false;
  2188. $CNF = &$this->_oConfig->CNF;
  2189. $iAuthorId = $aData[$CNF['FIELD_AUTHOR']];
  2190. $iLotId = $aData[$CNF['FIELD_ID']];
  2191. bx_alert($this->_oConfig->getObject('alert'), 'delete_lot', $iLotId, $this->_iProfileId, ['author_id' => $iAuthorId]);
  2192. }
  2193. public function onAddNewParticipant($iLotId, $iParticipant, $iProfileId = 0)
  2194. {
  2195. bx_alert($this->_oConfig->getObject('alert'), 'add_part', $iParticipant, $this->_iUserId, array('lot_id' => $iLotId, 'author_id' => $iProfileId));
  2196. }
  2197. public function onRemoveParticipant($iLotId, $iParticipant, $iProfileId = 0)
  2198. {
  2199. bx_alert($this->_oConfig->getObject('alert'), 'remove_part', $iParticipant, $this->_iUserId, array('lot_id' => $iLotId, 'author_id' => $iProfileId));
  2200. }
  2201. /**
  2202. * Checks if logged member can contact with the person
  2203. * @param $iSender
  2204. * @param $iRecipient
  2205. * @return bool|mixed|true
  2206. */
  2207. public function onCheckContact($iSender, $iRecipient)
  2208. {
  2209. $CNF = &$this->_oConfig->CNF;
  2210. $oSenderProfile = BxDolProfile::getInstance($iSender);
  2211. $oRecipientProfile = BxDolProfile::getInstance($iRecipient);
  2212. if ($CNF['CONTACT-JOIN-ORGANIZATION'] && $oSenderProfile && $oRecipientProfile) {
  2213. $aSenderProfileInfo = $oSenderProfile->getInfo();
  2214. $aRecipientProfileInfo = $oRecipientProfile->getInfo();
  2215. if ($aSenderProfileInfo['type'] === 'bx_organizations' || $aRecipientProfileInfo['type'] === 'bx_organizations') {
  2216. if (BxDolConnection::getObjectInstance("bx_organizations_fans")->isConnected($iSender, $iRecipient, true) || $aSenderProfileInfo['type'] === 'bx_organizations')
  2217. return true;
  2218. return BxDolConnection::getObjectInstance("sys_profiles_subscriptions")->isConnected($iSender, $iRecipient);
  2219. }
  2220. }
  2221. if ($CNF['DISABLE-PROFILE-PRIVACY'])
  2222. return true;
  2223. if (method_exists('BxDolProfile', 'checkAllowedProfileContact')) {
  2224. $mixedResult = BxDolProfile::getInstance($iSender)->checkAllowedProfileContact($iRecipient);
  2225. if ($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  2226. return false;
  2227. }
  2228. $bCanContact = true;
  2229. bx_alert($this->_oConfig->getObject('alert'), 'check_contact', 0, false, array('can_contact' => &$bCanContact, 'sender' => $iSender, 'recipient' => $iRecipient, 'where' => $this->_oConfig->getName()));
  2230. return $bCanContact;
  2231. }
  2232. /**
  2233. * Check if the selected profile or any groups module's item is available to send messages for logged member
  2234. * @param $mixedObject integer Profile id
  2235. * @return bool|int|mixed|true
  2236. */
  2237. public function serviceIsContactAllowed($mixedObject)
  2238. {
  2239. if (!$this->_iProfileId)
  2240. return false;
  2241. $oProfile = BxDolProfile::getInstance($mixedObject);
  2242. $sModule = $oProfile->getModule();
  2243. if (BxDolRequest::serviceExists($sModule, 'act_as_profile') && BxDolService::call($sModule, 'act_as_profile'))
  2244. return $this->onCheckContact($this->_iProfileId, (int) $mixedObject);
  2245. if (BxDolRequest::serviceExists($sModule, 'is_group_profile') && BxDolService::call($sModule, 'is_group_profile')) {
  2246. $aOwnerInfo = BxDolService::call($sModule, 'get_info', array($oProfile->getContentId(), false));
  2247. if (empty($aOwnerInfo) || !is_array($aOwnerInfo))
  2248. return CHECK_ACTION_RESULT_ALLOWED;
  2249. return BxDolService::call($sModule, 'check_allowed_view_for_profile', array($aOwnerInfo)) === CHECK_ACTION_RESULT_ALLOWED;
  2250. }
  2251. return true;
  2252. }
  2253. /**
  2254. * Checks if video conference is available for member
  2255. * @param $mixedObject integer profile id
  2256. * @return bool
  2257. */
  2258. public function serviceIsVideoConferenceAllowed($mixedObject)
  2259. {
  2260. if (!$this->_iProfileId || !$this->onCheckContact($this->_iProfileId, (int) $mixedObject))
  2261. return false;
  2262. $CNF = &$this->_oConfig->CNF;
  2263. if (isset($_SERVER['REQUEST_URI'])) {
  2264. $sRoom = $this->_oConfig->getRoomId($_SERVER['REQUEST_URI']);
  2265. $aRoom = $this->_oDb->getPublicVideoRoom($sRoom);
  2266. $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_IM_VC, $this->_iProfileId);
  2267. if ((empty($aRoom) || !(int) $aRoom[$CNF['FPJVC_STATUS']]) && $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_IM_VC, $this->_iProfileId) !== true)
  2268. return false;
  2269. if (!empty($aRoom) && (int) $aRoom[$CNF['FPJVC_STATUS']] && (int) $mixedObject !== $this->_iProfileId && $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_IM_VC, $this->_iProfileId) !== true)
  2270. return false;
  2271. } else
  2272. return false;
  2273. $this->_oTemplate->addCss(array('video-conference.css'));
  2274. $this->_oTemplate->addJs('utils.js');
  2275. return true;
  2276. }
  2277. /**
  2278. * Sends immediate push notifications when Profile conference has been started
  2279. * @param $iObjectId
  2280. * @param $iSenderId
  2281. * @param $iReceiverId
  2282. * @param string $sType
  2283. * @return false
  2284. */
  2285. private function sendProfilePush($iObjectId, $iSenderId, $iReceiverId, $sType = 'bx_persons')
  2286. {
  2287. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  2288. $sLanguage = $oLanguage->getCurrentLangName(false);
  2289. $oObject = BxDolProfile::getInstance($iObjectId);
  2290. if (!$oObject)
  2291. return false;
  2292. $oReceiver = BxDolProfile::getInstance($iReceiverId);
  2293. if (!$oReceiver)
  2294. return false;
  2295. $oSender = BxDolProfile::getInstance($iSenderId);
  2296. if (!$oSender)
  2297. return false;
  2298. $sMessage = _t("_bx_messenger_public_vc_{$sType}_message", $sType === 'bx_persons' ? $oSender->getDisplayName() : $oObject->getDisplayName());
  2299. $aContent = array(
  2300. 'en' => $sMessage,
  2301. );
  2302. if (!isset($aContent[$sLanguage]))
  2303. $aContent[$sLanguage] = $sMessage;
  2304. $aHeadings = array($sLanguage => _t("_bx_messenger_push_vc_{$sType}_message_title"));
  2305. $aInfo = array(
  2306. 'contents' => $aContent,
  2307. 'headings' => $aHeadings,
  2308. 'url' => $oObject->getUrl()
  2309. );
  2310. BxDolPush::getObjectInstance()->send(
  2311. $iReceiverId,
  2312. array_merge($aInfo, array(
  2313. 'icon' => $sType === 'bx_persons' ?
  2314. $oSender->getThumb() :
  2315. $oObject->getThumb()
  2316. )),
  2317. true
  2318. );
  2319. }
  2320. public function actionGetVideoConferenceForm($iProfileId)
  2321. {
  2322. $oProfile = BxDolProfile::getInstance($iProfileId);
  2323. header('Content-type: text/html; charset=utf-8');
  2324. $sContent = '';
  2325. if (!$oProfile)
  2326. $sContent = MsgBox(_t('_bx_messenger_profile_not_found'));
  2327. else {
  2328. $aInfo = $oProfile->getInfo();
  2329. if ($aInfo['type'] == 'bx_organizations') {
  2330. $aFans = BxDolConnection::getObjectInstance("{$aInfo['type']}_fans")->getConnectedContent($iProfileId, true);
  2331. if (!empty($aFans)) {
  2332. foreach ($aFans as &$iPart)
  2333. $this->sendProfilePush($iProfileId, $this->_iProfileId, $iPart, $aInfo['type']);
  2334. }
  2335. } else if ($this->_iProfileId != $iProfileId)
  2336. $this->sendProfilePush($iProfileId, $this->_iProfileId, $iProfileId, $aInfo['type']);
  2337. $CNF = &$this->_oConfig->CNF;
  2338. $sIdent = $this->getReferrerPageIdent();
  2339. $sRoom = $this->_oConfig->getRoomId($sIdent);
  2340. $aRoom = $this->_oDb->getPublicVideoRoom($sRoom);
  2341. if (empty($aRoom) || !(int) $aRoom[$CNF['FPJVC_STATUS']]) {
  2342. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_IM_VC, $this->_iProfileId);
  2343. if ($mixedResult !== true)
  2344. $sContent = $mixedResult;
  2345. } else
  2346. if (!empty($aRoom) && (int) $aRoom[$CNF['FPJVC_STATUS']] && (int) $iProfileId !== $this->_iProfileId) {
  2347. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_IM_VC, $this->_iProfileId);
  2348. if ($mixedResult !== true)
  2349. $sContent = $mixedResult;
  2350. }
  2351. }
  2352. echo $sContent ? $sContent : $this->_oTemplate->getPublicJitsi($this->_iProfileId, $sIdent, $oProfile->getDisplayName(), 'bx-messenger-vc-call');
  2353. exit;
  2354. }
  2355. private function getReferrerPageIdent()
  2356. {
  2357. $sPath = '';
  2358. if (isset($_SERVER['HTTP_REFERER'])) {
  2359. $aPath = parse_url($_SERVER['HTTP_REFERER']);
  2360. if (isset($aPath['path']))
  2361. $sPath = $aPath['path'];
  2362. if (isset($aPath['query']))
  2363. $sPath .= '?' . $aPath['query'];
  2364. }
  2365. return $sPath;
  2366. }
  2367. public function actionUpdatePublicJitsiVideoConference()
  2368. {
  2369. $sRoom = bx_get('room');
  2370. $sAction = bx_get('action');
  2371. if (!$sRoom || !$this->_iProfileId || !$sAction)
  2372. return false;
  2373. if ($sAction == BX_JOT_PUBLIC_JITSI_JOIN)
  2374. return $this->_oDb->createPublicVideoRoom($sRoom, $this->_iProfileId);
  2375. return $this->_oDb->updatePublicVideoRoom($sRoom, $this->_iProfileId, $sAction);
  2376. }
  2377. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  2378. {
  2379. $iCountNew = $this->serviceGetUpdatedLotsNum();
  2380. if ($iCountNew == $iCount)
  2381. return false;
  2382. return array(
  2383. 'count' => $iCountNew,
  2384. 'method' => 'bx_menu_show_live_update(oData)',
  2385. 'data' => array(
  2386. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  2387. 'content' => '{count}'
  2388. )),
  2389. 'mi_parent' => $aMenuItemParent,
  2390. 'mi_child' => $aMenuItemChild
  2391. ),
  2392. );
  2393. }
  2394. public function serviceGetLiveVcUpdates($iCount = 0)
  2395. {
  2396. $iCount = 0;
  2397. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  2398. $sPageUrl = $this->getReferrerPageIdent();
  2399. /* ADD ABILITY TO call members when they on any page of the site and have incoming call */
  2400. if (preg_match('/.*page\/(.*)\?id=(\d+).*/', $sPageUrl, $aParams)) {
  2401. if (!empty($aParams) && isset($aParams[1]) && isset($aParams[2])) {
  2402. $iPageOwner = $this->_oDb->findThePageOwner(array('i' => $aParams[1], 'id' => $aParams[2]));
  2403. //if ($this->_iProfileId == $iPageOwner)
  2404. }
  2405. }
  2406. $aParticipants = $this->_oDb->getPublicRoomParticipants($this->_oConfig->getRoomId($sPageUrl));
  2407. $iCount = count($aParticipants);
  2408. }
  2409. return array(
  2410. 'count' => $iCount,
  2411. 'method' => 'oMUtils.addBubble(oData)',
  2412. 'data' => array(
  2413. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  2414. 'content' => '{count}'
  2415. )),
  2416. 'selector' => '.bx-menu-item-public-vc-messenger a.bx-btn'
  2417. ),
  2418. );
  2419. }
  2420. /**
  2421. * @param int $iObjectId item's id
  2422. * @param strign $sType name of te module
  2423. * @param int $iProfileId comments' author, if this param is empty, then get all comments
  2424. * @param int $iStart get comments from position
  2425. * @param int $iPerPage number of the comments to get at once
  2426. * @return array comments list
  2427. */
  2428. public function serviceGetLiveComments($iObjectId, $sType, $iProfileId = 0, $iStart = 0, $iPerPage = 0)
  2429. {
  2430. $aComments = $this->_oDb->getLiveComments($iObjectId, $sType, $iProfileId, $iStart, $iPerPage);
  2431. if (!(int) $aComments['total'])
  2432. return array(
  2433. 'items' => array(),
  2434. 'id' => $iObjectId,
  2435. 'type' => $sType,
  2436. 'total' => 0
  2437. );
  2438. $aResult = array();
  2439. foreach ($aComments['result'] as $iKey => $aValue) {
  2440. $oProfile = $this->_oTemplate->getObjectUser($aValue[$this->_oConfig->CNF['FIELD_LCMTS_AUTHOR']]);
  2441. $aResult[$aValue[$this->_oConfig->CNF['FIELD_LCMTS_ID']]] = array(
  2442. 'text' => $aValue[$this->_oConfig->CNF['FIELD_LCMTS_TEXT']],
  2443. 'author' => array(
  2444. 'id' => $aValue[$this->_oConfig->CNF['FIELD_LCMTS_AUTHOR']],
  2445. 'name' => $oProfile->getDisplayName(),
  2446. 'icon' => $oProfile->getAvatar(),
  2447. 'url' => $oProfile->getUrl()
  2448. ),
  2449. 'id' => $aValue[$this->_oConfig->CNF['FIELD_LCMTS_ID']],
  2450. 'date' => $aValue[$this->_oConfig->CNF['FIELD_LCMTS_DATE']],
  2451. );
  2452. }
  2453. return array('items' => $aResult, 'id' => $iObjectId, 'type' => $sType, 'total' => (int) $aComments['total']);
  2454. }
  2455. /**
  2456. * @param string $sText text of the comment
  2457. * @param int $iObjectId item's id
  2458. * @param string $sType $sType name of te module
  2459. * @param int $iProfileId comment's author
  2460. * @param int $iDate client's time when message is sent
  2461. * @return int added object's id
  2462. */
  2463. public function servicePostLiveComments($sText, $iObjectId, $sType, $iProfileId, $iDate = 0)
  2464. {
  2465. if (!$sText || !$iObjectId || !$sType || !$iProfileId)
  2466. return 0;
  2467. $aResult = array('code' => 1);
  2468. if ($iId = $this->_oDb->addLiveComment($sText, $iObjectId, $sType, $iProfileId, $iDate))
  2469. $aResult = array('code' => 0, 'id' => $iId, 'date' => $iDate ? $iDate : time());
  2470. return $aResult;
  2471. }
  2472. public function serviceRemoveLiveComment($iObjectId, $iProfileId)
  2473. {
  2474. if (!$iObjectId || !$iProfileId)
  2475. return false;
  2476. $aResult = array('code' => 0, 'id' => $iObjectId);
  2477. return $this->_oDb->removeLiveComment($iObjectId, $iProfileId) ? $aResult : array('code' => 1);
  2478. }
  2479. public function actionGetFilesList($iLotId)
  2480. {
  2481. if (!$iLotId || !$this->_iProfileId || !$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  2482. return false;
  2483. $aFiles = $this->_oDb->getLotFiles($iLotId);
  2484. if (empty($aFiles))
  2485. return array();
  2486. $aResult = array();
  2487. $CNF = &$this->_oConfig->CNF;
  2488. foreach ($aFiles as $iKey => $aValue)
  2489. $aResult[$aValue[$CNF['FIELD_ST_ID']]] = $this->_oTemplate->getFileContent($aValue);
  2490. return $aResult;
  2491. }
  2492. public function actionGetLotTabs($iLotId, $sAction)
  2493. {
  2494. if (!$iLotId || !$this->_iUserId || !$this->_oDb->isParticipant($iLotId, $this->_iUserId))
  2495. return echoJson(array('code' => 1, 'msg' => _t('_bx_messenger_no_permissions')));
  2496. $sHTML = '';
  2497. switch ($sAction) {
  2498. case 'files':
  2499. $aFiles = $this->actionGetFilesList($iLotId);
  2500. if (empty($aFiles))
  2501. $sHTML = MsgBox(_t('_bx_messenger_txt_msg_no_results'));
  2502. else
  2503. $sHTML = $this->_oTemplate->parseHtmlByName('files-feeds.html', array('bx_repeat:files' => $aFiles));
  2504. break;
  2505. }
  2506. header('Content-type: text/html; charset=utf-8');
  2507. echo $sHTML;
  2508. exit;
  2509. }
  2510. /************ REACT Jot Integration *************************************/
  2511. public function serviceGetLotsList($iProfileId)
  2512. {
  2513. if (!$iProfileId)
  2514. return '';
  2515. $aLots = $this->_oDb->getMyLots($iProfileId);
  2516. if (empty($aLots))
  2517. return array();
  2518. $aLotsList = array();
  2519. foreach ($aLots as &$aLot) {
  2520. $aParticipantsList = $this->_oDb->getParticipantsList($aLot[$this->_oConfig->CNF['FIELD_ID']], true, $iProfileId);
  2521. $iParticipantsCount = count($aParticipantsList);
  2522. $aParticipantsList = $iParticipantsCount ? array_slice($aParticipantsList, 0, $this->_oConfig->CNF['PARAM_ICONS_NUMBER']) : array($iProfileId);
  2523. $aNickNames = array();
  2524. foreach ($aParticipantsList as $iParticipant) {
  2525. $oProfile = $this->_oTemplate->getObjectUser($iParticipant);
  2526. if ($oProfile) {
  2527. $aNickNames[] = array(
  2528. 'name' => $oProfile->getDisplayName(),
  2529. 'url' => $oProfile->getUrl(),
  2530. 'thumb' => $oProfile->getThumb()
  2531. );
  2532. }
  2533. }
  2534. if (!empty($aLot[$this->_oConfig->CNF['FIELD_TITLE']]))
  2535. $sTitle = _t($aLot[$this->_oConfig->CNF['FIELD_TITLE']]);
  2536. else {
  2537. $aTitle = array();
  2538. $aTmpNickNames = $iParticipantsCount > 3 ? array_slice($aNickNames, 0, $this->_oConfig->CNF['PARAM_ICONS_NUMBER']) : $aNickNames;
  2539. foreach ($aTmpNickNames as &$aValue)
  2540. $aTitle[] = $aValue['name'];
  2541. $sTitle = implode(', ', $aTitle);
  2542. }
  2543. $sStatus = '';
  2544. if ($iParticipantsCount == 1 && $oProfile && empty($aLot[$this->_oConfig->CNF['FIELD_TITLE']]))
  2545. $sStatus = (int) (method_exists($oProfile, 'isOnline') ? $oProfile->isOnline() : false);
  2546. else
  2547. $sStatus = $iParticipantsCount;
  2548. $aLatestJots = $this->_oDb->getLatestJot($aLot[$this->_oConfig->CNF['FIELD_ID']]);
  2549. $iTime = $aLot[$this->_oConfig->CNF['FIELD_ADDED']];
  2550. $oSender = $sSender = $sMessage = '';
  2551. if (!empty($aLatestJots)) {
  2552. if (isset($aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE']])) {
  2553. $sMessage = $aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE']];
  2554. if ($aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_AT_TYPE']] == BX_ATT_TYPE_REPOST) {
  2555. $sMessage = $this->_oConfig->cleanRepostLinks($sMessage, $aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_AT']]);
  2556. $sMessage = $sMessage ? $sMessage : _t('_bx_messenger_repost_message');
  2557. }
  2558. $sMessage = BxTemplFunctions::getInstance()->getStringWithLimitedLength($sMessage, $this->_oConfig->CNF['MAX_PREV_JOTS_SYMBOLS']);
  2559. }
  2560. if (!$sMessage && $aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_AT_TYPE']] == BX_ATT_TYPE_FILES)
  2561. $sMessage = _t('_bx_messenger_attached_files_message', $this->_oDb->getJotFiles($aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_ID']], true));
  2562. $sSender = '';
  2563. if ($oSender = $this->_oTemplate->getObjectUser($aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_AUTHOR']]))
  2564. $sSender = $oSender->id() == $iProfileId ? _t('_bx_messenger_you_username_title') : $oSender->getDisplayName();
  2565. $iTime = $aLatestJots[$this->_oConfig->CNF['FIELD_MESSAGE_ADDED']];
  2566. }
  2567. $aLotsList[] = array(
  2568. 'participants_count' => $iParticipantsCount,
  2569. 'participants_list' => $aNickNames,
  2570. 'title' => $sTitle,
  2571. 'lot_id' => $aLot[$this->_oConfig->CNF['FIELD_ID']],
  2572. 'status' => $sStatus,
  2573. 'latest_jot_message' => $sMessage,
  2574. 'latest_jot_message_author' => $sSender,
  2575. 'latest_jot_message_date' => $iTime,
  2576. 'latest_jot_message_author_thumb' => $oSender ? $oSender->getThumb() : '',
  2577. );
  2578. }
  2579. return $aLotsList;
  2580. }
  2581. public function servicePerformConnAction($iInitiator, $iContent, $sAction = 'add', $sConnectionType = 'follow')
  2582. {
  2583. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  2584. if (!$iInitiator || !$iContent)
  2585. return array();
  2586. $oConnections = BxDolConnection::getObjectInstance($sConnectionType === 'follow' ? 'sys_profiles_subscriptions' : 'sys_profiles_friends');
  2587. $sMethod = "{$sAction}Connection";
  2588. if (empty($oConnections) || !method_exists($oConnections, $sMethod))
  2589. return array();
  2590. return array(
  2591. 'result' => (int) $oConnections->$sMethod($iInitiator, $iContent),
  2592. 'content_id' => (int) $iContent,
  2593. 'action' => $sAction
  2594. );
  2595. }
  2596. public function serviceProfileConnections($sType = 'friends', $iContentId = 0, $iStart = 0, $iPerPage = 10, $iMutual = false)
  2597. {
  2598. $iProfileId = $iContentId ? $iContentId : $this->_iUserId;
  2599. if (!$iProfileId)
  2600. return array();
  2601. $aMembers = array();
  2602. $oFriends = BxDolConnection::getObjectInstance('sys_profiles_friends');
  2603. $oFollowers = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  2604. switch ($sType) {
  2605. case 'friends':
  2606. if (!$oFriends)
  2607. return array();
  2608. $aMembers = $oFriends->getConnectedContent($iProfileId, $iMutual, $iStart, $iPerPage);
  2609. break;
  2610. case 'connections':
  2611. if (!$oFollowers)
  2612. return array();
  2613. $aMembers = $oFollowers->getConnectedContent($iProfileId, $iMutual, $iStart, $iPerPage);
  2614. break;
  2615. case 'recent':
  2616. case 'active':
  2617. case 'online':
  2618. case 'recommended':
  2619. case 'top':
  2620. if (BxDolRequest::serviceExists('bx_persons', 'get_members')) {
  2621. $aPersons = BxDolService::call('bx_persons', 'get_members', array($sType, $iStart, $iPerPage));
  2622. foreach ($aPersons as &$aPerson)
  2623. $aMembers[$aPerson['author']] = $aPerson;
  2624. }
  2625. break;
  2626. }
  2627. if (empty($aMembers))
  2628. return $aMembers;
  2629. $aRet = array();
  2630. foreach ($aMembers as $iId => $aInfo) {
  2631. if (($oProfile = BxDolProfile::getInstance($iId))) {
  2632. $oAccount = $oProfile->getAccountObject();
  2633. $aRet[$iId] = array(
  2634. 'name' => $oProfile->getDisplayName(),
  2635. 'profile_display_name' => $oProfile->getDisplayName(),
  2636. 'email' => $oAccount->getEmail(),
  2637. 'id' => $iId,
  2638. 'url' => $oProfile->getUrl(),
  2639. 'info' => $oProfile->getInfo(),
  2640. 'thumb' => $oProfile->getThumb(),
  2641. 'picture' => $oProfile->getPicture(),
  2642. 'avatar' => $oProfile->getAvatar(),
  2643. 'is_online' => $oProfile->isOnline(),
  2644. 'cover' => $oProfile->getUnitCover(),
  2645. 'status' => $oProfile->getStatus(),
  2646. 'followers' => (int) $oFollowers->getConnectedInitiatorsCount($iId),
  2647. 'friends' => (int) $oFriends->getConnectedContentCount($iId, true),
  2648. 'is_friend' => (int) $oFriends->isConnected($iProfileId, $iId, true),
  2649. 'is_follow' => (int) $oFollowers->isConnected($iProfileId, $iId)
  2650. );
  2651. }
  2652. }
  2653. return $aRet;
  2654. }
  2655. public function serviceGetModulesList()
  2656. {
  2657. $aResult = array();
  2658. if (!$this->isLogged())
  2659. return $aResult;
  2660. $aModules = $this->_oDb->getAllCmtsModule();
  2661. foreach ($aModules as $sKey => $aModule) {
  2662. if (!$aModule['icon']) {
  2663. $aResult[$sKey] = array(
  2664. 'icon' => 'world',
  2665. 'icon_color' => 'green'
  2666. );
  2667. continue;
  2668. }
  2669. preg_match('/([a-z]+)(?:-[a-z]+)?\s+col-([a-z]+)/', $aModule['icon'], $aMatches);
  2670. $aResult[$sKey] = array(
  2671. 'icon' => $aMatches[1],
  2672. 'icon_color' => $aMatches[2]
  2673. );
  2674. }
  2675. return $aResult;
  2676. }
  2677. public function serviceSwitchProfile($iSwitchToProfileId)
  2678. {
  2679. $aResult = array('code' => 1);
  2680. if ($iSwitchToProfileId) {
  2681. $oProfile = BxDolProfile::getInstance($iSwitchToProfileId);
  2682. if ($oProfile && BxDolService::call($oProfile->getModule(), 'act_as_profile')) {
  2683. $iViewerAccountId = getLoggedId();
  2684. $iSwitchToAccountId = $oProfile->getAccountId();
  2685. $bCanSwitch = $iSwitchToAccountId == $iViewerAccountId;
  2686. bx_alert('account', 'check_switch_context', $iSwitchToAccountId, bx_get_logged_profile_id(), array('switch_to_profile' => $iSwitchToProfileId, 'viewer_account' => $iViewerAccountId, 'override_result' => &$bCanSwitch));
  2687. if ($bCanSwitch) {
  2688. $oAccount = BxDolAccount::getInstance();
  2689. if ($oAccount->updateProfileContext($iSwitchToProfileId))
  2690. $aResult = array('code' => 0, 'msg' => _t('_sys_txt_account_profile_context_changed_success', $oProfile->getDisplayName()));
  2691. }
  2692. }
  2693. }
  2694. return $aResult;
  2695. }
  2696. public function serviceGetLotInfo($iProfileId, $iLotId, $iJotsLimit = 0, $iStart = BX_IM_EMPTY, $sType = 'new')
  2697. {
  2698. $aResult = array('code' => 1);
  2699. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  2700. if (!$iLotId || empty($aLotInfo))
  2701. return $aResult;
  2702. if ($iProfileId !== $this->_iUserId || !$this->_oDb->isParticipant($iLotId, $this->_iUserId))
  2703. return array('code' => 1, _t('_bx_messenger_not_participant'));
  2704. $CNF = $this->_oConfig->CNF;
  2705. $iJotsLimit = $iJotsLimit ?: $CNF['MAX_JOTS_LOAD_HISTORY'];
  2706. $aJotsList = $this->_oDb->getJotsByLotId(['lot' => $iLotId, 'start' => $iStart, 'mode' => $sType, 'limit' => $iJotsLimit]);
  2707. $aJots = array();
  2708. foreach ($aJotsList as &$aJot) {
  2709. $aAuthor = BxDolProfile::getInstance($aJot[$CNF['FIELD_MESSAGE_AUTHOR']]);
  2710. $aAttachments = $this->_oTemplate->getAttachment($aJot);
  2711. $aJots[] = array_merge($aJot, array(
  2712. 'thumb' => $aAuthor->getThumb(),
  2713. 'name' => $aAuthor->getDisplayName(),
  2714. 'files' => $aAttachments[BX_ATT_GROUPS_ATTACH] ?? ''
  2715. ));
  2716. }
  2717. return array('lot_id' => $iLotId, 'jots' => $aJots);
  2718. }
  2719. public function prepareMessageToDb($sMessage)
  2720. {
  2721. if (!$sMessage)
  2722. return '';
  2723. $aReplacements = [
  2724. '/\<p>\<br[^>]*>\<\/p>/i' => '<br/>',
  2725. '/\<p>/i' => '',
  2726. '/\<\/p>/i' => '<br/>',
  2727. '/<pre.*>/i' => '<pre>'
  2728. ];
  2729. $sNewMessage = preg_replace(array_keys($aReplacements), $aReplacements, $sMessage);
  2730. return preg_replace('/(?:(?:\s*)?<br\/>)+$/', '', $sNewMessage);
  2731. }
  2732. function actionGetGiphy()
  2733. {
  2734. if (!$this->isLogged())
  2735. return '';
  2736. $aContent = $this->_oTemplate->getGiphyItems(bx_get('action'), urlencode(bx_get('filter')), (float) bx_get('height'), (int) bx_get('start'));
  2737. if (isset($aContent['content']) && $aContent['content'])
  2738. return echoJson(array('code' => 0, 'html' => $aContent['content'], 'total' => isset($aContent['pagination']) ? $aContent['pagination']['total_count'] : (int) bx_get('start')));
  2739. return echoJson(array('code' => 1, 'message' => MsgBox(_t('_bx_messenger_giphy_gifs_nothing_found'))));
  2740. }
  2741. function serviceGetTalksList($aOptions = [])
  2742. {
  2743. $mixedGroup = isset($aOptions['group']) ? $aOptions['group'] : '';
  2744. $iId = isset($aOptions['id']) ? (int) $aOptions['id'] : 0;
  2745. $iLotId = isset($aOptions['lot']) ? (int) $aOptions['lot'] : 0;
  2746. $aParams = ['start' => isset($aOptions['count']) ? (int) $aOptions['count'] : 0, 'lot' => $iLotId];
  2747. $CNF = &$this->_oConfig->CNF;
  2748. $aLotInfo = $iLotId ? $this->_oDb->getLotInfoById($iLotId) : [];
  2749. if (!empty($aLotInfo) && ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId)))
  2750. return ['code' => 1, 'message' => _t('_bx_messenger_not_participant')];
  2751. $sTitle = _t('_bx_messenger_nav_menu_item_title_' . ($mixedGroup ? $mixedGroup : 'inbox'));
  2752. $aLotsList = [];
  2753. switch ($mixedGroup) {
  2754. case BX_MSG_TALK_TYPE_THREADS:
  2755. $aParams['threads'] = true;
  2756. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
  2757. break;
  2758. case 'public':
  2759. $aParams['type'] = array(BX_IM_TYPE_SETS, BX_IM_TYPE_PUBLIC);
  2760. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
  2761. break;
  2762. case BX_MSG_TALK_TYPE_FILES:
  2763. break;
  2764. case BX_MSG_TALK_TYPE_MR:
  2765. $aLotsList = $this->_oDb->getReactionsMentionsLots($this->_iProfileId, $aParams);
  2766. break;
  2767. case BX_MSG_TALK_TYPE_REPLIES:
  2768. $aLotsList = $this->_oDb->getLotsWithReplies($this->_iProfileId, $aParams);
  2769. break;
  2770. case BX_MSG_TALK_TYPE_SAVED:
  2771. $aLotsList = $this->_oDb->getSavedJotInLots($this->_iProfileId, $aParams);
  2772. break;
  2773. case BX_MSG_TALK_TYPE_FAVORITES:
  2774. // Talks marked with a star by the viewer, the same filter as the star toggle on the site.
  2775. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['star' => true]));
  2776. break;
  2777. case BX_MSG_TALK_TYPE_GROUPS:
  2778. $aParams['group'] = $iId;
  2779. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
  2780. if (empty($aLotsList))
  2781. break;
  2782. $aGroupInfo = $this->_oDb->getGroupById($iId);
  2783. $sTitle = !empty($aGroupInfo) ? $aGroupInfo[$CNF['FMG_NAME']] : current($aLotsList)['title'];
  2784. break;
  2785. case BX_MSG_TALK_TYPE_DIRECT:
  2786. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['type' => BX_IM_TYPE_PRIVATE]));
  2787. break;
  2788. default:
  2789. $aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['inbox' => true]));
  2790. }
  2791. $bNext = false;
  2792. if (isset($aParams['per_page']) && count($aLotsList) === (int) $aParams['per_page'])
  2793. $bNext = true;
  2794. else if (count($aLotsList) === $CNF['MAX_LOTS_NUMBER'])
  2795. $bNext = true;
  2796. return ['title' => $sTitle, 'list' => $aLotsList, 'code' => 0, 'next' => $bNext];
  2797. }
  2798. function actionGetTalksList()
  2799. {
  2800. if (!$this->isLogged())
  2801. return echoJson(['code' => 1, 'reload' => 1]);
  2802. $aParams = ['group' => bx_get('group'), 'id' => (int) bx_get('id'), 'count' => (int) bx_get('count'), 'lot' => (int) bx_get('lot')];
  2803. $iExcludeConvo = (int) bx_get('exclude_convo');
  2804. $aData = $this->serviceGetTalksList($aParams);
  2805. if (isset($aData['code']) && (int) $aData['code'])
  2806. return echoJson(['code' => 1, 'msg' => $aData['message']]);
  2807. $sTitle = isset($aData['title']) ? $aData['title'] : '';
  2808. $aLotsList = isset($aData['list']) ? $aData['list'] : [];
  2809. if (!empty($aLotsList)) {
  2810. if ($iExcludeConvo) {
  2811. $mixedKey = array_search($iExcludeConvo, array_column($aLotsList, $this->_oConfig->CNF['FIELD_ID']));
  2812. if ($mixedKey !== false)
  2813. unset($aLotsList[$mixedKey]);
  2814. }
  2815. $sContent = $this->_oTemplate->getLotsPreview($this->_iProfileId, $aLotsList);
  2816. return echoJson(array(
  2817. 'code' => 0,
  2818. 'html' => $sContent,
  2819. 'title' => _t($sTitle)
  2820. ));
  2821. }
  2822. return echoJson(['code' => 1, 'html' => MsgBox(_t('_bx_messenger_txt_msg_no_results')), 'title' => _t($sTitle)]);
  2823. }
  2824. public function actionGetTopMenuItems()
  2825. {
  2826. $CNF = &$this->_oConfig->CNF;
  2827. $oLeftMainMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_NAV_LEFT_MENU']);
  2828. $oLeftGroupsMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_GROUPS_MENU']);
  2829. return ['top' => $oLeftMainMenu->getMenuItems(), 'groups' => $oLeftGroupsMenu->getMenuItems()];
  2830. }
  2831. public function serviceGetLotStat($mixedLotId = '')
  2832. {
  2833. if (is_numeric($mixedLotId) && (int) $mixedLotId)
  2834. $aLotInfo = $this->_oDb->getLotInfoById($mixedLotId);
  2835. else {
  2836. $sUrl = $mixedLotId ? $this->getPreparedUrl($mixedLotId) : $this->_oConfig->getPageIdent($mixedLotId);
  2837. $aLotInfo = $this->_oDb->getLotByUrl($sUrl);
  2838. }
  2839. if (empty($aLotInfo))
  2840. return $aLotInfo;
  2841. $CNF = &$this->_oConfig->CNF;
  2842. $aMessages = $this->_oDb->getLotJotsCount($aLotInfo[$CNF['FIELD_ID']]);
  2843. return array(
  2844. 'title' => $aLotInfo[$CNF['FIELD_TITLE']],
  2845. 'created' => $aLotInfo[$CNF['FIELD_ADDED']],
  2846. 'participants' => $aLotInfo[$CNF['FIELD_PARTICIPANTS']] ? explode(',', $aLotInfo[$CNF['FIELD_PARTICIPANTS']]) : array(),
  2847. 'author' => $aLotInfo[$CNF['FIELD_AUTHOR']],
  2848. 'url' => $aLotInfo[$CNF['FIELD_URL']],
  2849. 'type' => $aLotInfo[$CNF['FIELD_TYPE']],
  2850. 'messages' => array_merge($aMessages, array(
  2851. 'total' => array_sum($aMessages)
  2852. ))
  2853. );
  2854. }
  2855. public function serviceGetUnreadLots($iProfileId)
  2856. {
  2857. if (!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  2858. return array();
  2859. $aLots = $this->_oDb->getLotsWithUnreadMessages($iProfileId);
  2860. $aLotsInfo = array();
  2861. foreach ($aLots as $iLotId => $iCount) {
  2862. $aLotsInfo[$iLotId] = array(
  2863. 'count' => $iCount,
  2864. 'participants' => $this->_oDb->getParticipantsList($iLotId),
  2865. 'new_messages' => $this->_oDb->getUnreadMessages($iProfileId, $iLotId)
  2866. );
  2867. }
  2868. return $aLotsInfo;
  2869. }
  2870. public function actionGetJitsiConferenceForm($iLotId = 0)
  2871. {
  2872. if (!$this->_iProfileId) {
  2873. echo MsgBox(_t('_bx_messenger_jitsi_err_can_join_conference'));
  2874. exit;
  2875. }
  2876. header('Content-type: text/html; charset=utf-8');
  2877. if (!$iLotId) {
  2878. $sUrl = bx_get('url');
  2879. $sTitle = bx_get('title');
  2880. $aLot = $this->_oDb->getLotByUrl($sUrl);
  2881. if (empty($aLot)) {
  2882. $this->_oDb->createNewLot($this->_iProfileId, array('title' => $sTitle, 'type' => BX_IM_TYPE_PRIVATE, 'url' => $sUrl));
  2883. $this->onCreateLot($iLotId);
  2884. } else
  2885. $iLotId = $aLot[$this->_oConfig->CNF['FIELD_ID']];
  2886. }
  2887. $CNF = &$this->_oConfig->CNF;
  2888. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  2889. $bIsParticipant = $this->_oDb->isParticipant($iLotId, $this->_iProfileId);
  2890. if (!$bIsParticipant) {
  2891. if ($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE) {
  2892. echo MsgBox(_t('_bx_messenger_jitsi_err_can_join_conference'));
  2893. exit;
  2894. }
  2895. if (!empty($aLotInfo))
  2896. $this->_oDb->addMemberToParticipantsList($iLotId, $this->_iProfileId);
  2897. }
  2898. $aParams['audio_only'] = bx_get('startAudioOnly') ? 1 : 0;
  2899. $mixedAuthor = $this->_oDb->getActiveJVCItem($iLotId, $CNF['FJVCT_AUTHOR_ID']);
  2900. if (($mixedAuthor === false || ((int) $mixedAuthor == $this->_iProfileId)) && $this->_oConfig->isJitsiAllowed($aLotInfo[$CNF['FIELD_TYPE']])) {
  2901. echo $this->_oTemplate->getJitsi($iLotId, $this->_iProfileId, $aParams);
  2902. exit;
  2903. }
  2904. $sMessage = _t('_bx_messenger_jitsi_err_cant_type_use');
  2905. if ($mixedAuthor !== false && ((int) $mixedAuthor != $this->_iProfileId) && ($mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC))) {
  2906. if ($mixedResult !== true)
  2907. $sMessage = $mixedResult;
  2908. else {
  2909. echo $this->_oTemplate->getJitsi($iLotId, $this->_iProfileId, $aParams);
  2910. exit;
  2911. }
  2912. }
  2913. echo MsgBox($sMessage, 2.5);
  2914. exit;
  2915. }
  2916. public function onCreateVC($iLotId)
  2917. {
  2918. bx_alert($this->_oConfig->getObject('alert'), 'create_vc', $iLotId, $this->_iUserId);
  2919. }
  2920. public function onJoinVC($iLotId)
  2921. {
  2922. $aItem = $this->_oDb->getActiveJVCItem($iLotId);
  2923. bx_alert($this->_oConfig->getObject('alert'), 'join_vc', $iLotId, $this->_iUserId, array('object_author_id' => $aItem[$this->_oConfig->CNF['FJVCT_AUTHOR_ID']]));
  2924. }
  2925. public function actionCreateJitsiVideoConference()
  2926. {
  2927. $CNF = $this->_oConfig->CNF;
  2928. $iLotId = (int) bx_get('lot_id');
  2929. if (!$iLotId)
  2930. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_found')));
  2931. if (!$this->isLogged())
  2932. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_jitsi_err_can_join_conference')));
  2933. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  2934. if (empty($aLotInfo))
  2935. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_found')));
  2936. $bIsParticipant = $this->_oDb->isParticipant($iLotId, $this->_iProfileId);
  2937. if (!$bIsParticipant) {
  2938. if ($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
  2939. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_jitsi_err_can_join_conference')));
  2940. $this->_oDb->addMemberToParticipantsList($iLotId, $this->_iProfileId);
  2941. }
  2942. $mixedAuthor = $this->_oDb->getActiveJVCItem($iLotId, $CNF['FJVCT_AUTHOR_ID']);
  2943. if ($mixedAuthor != $this->_iProfileId && ($mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC))) {
  2944. if ($mixedResult !== true)
  2945. return echoJson(array('code' => 1, 'message' => $mixedResult));
  2946. }
  2947. $aJVC = $this->_oDb->getJVC($iLotId);
  2948. $iParticipants = count($this->_oDb->getParticipantsList($iLotId));
  2949. $aResult = array('code' => 0, 'parts' => $iParticipants);
  2950. if (!empty($aJVC) && (int) $aJVC[$CNF['FJVC_ACTIVE']]) {
  2951. if ($this->_oDb->joinToActiveJVC($iLotId, $this->_iProfileId)) {
  2952. $aResult['jot_id'] = $this->_oDb->getJotIdByJitsiItem((int) $aJVC[$CNF['FJVC_ACTIVE']]);
  2953. $aResult[$CNF['FJVC_ROOM']] = $aJVC[$CNF['FJVC_ROOM']];
  2954. $this->onJoinVC($iLotId);
  2955. return echoJson($aResult);
  2956. } else
  2957. $this->_oDb->closeJVC($iLotId);
  2958. }
  2959. $this->onCreateVC($iLotId);
  2960. return echoJson(
  2961. array_merge(
  2962. array('new' => 1),
  2963. $aResult,
  2964. $this->_oDb->createJVC($iLotId, $this->_iProfileId)
  2965. )
  2966. );
  2967. }
  2968. public function actionStopJvc()
  2969. {
  2970. $iLotId = (int) bx_get('lot_id');
  2971. if (!$iLotId)
  2972. return echoJson(array('code' => 1, 'msg' => _t('_bx_messenger_not_found')));
  2973. if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  2974. return echoJson(array('code' => 1));
  2975. if ($this->_oDb->stopJVC($iLotId, $this->_iProfileId))
  2976. return echoJson(array('code' => 0));
  2977. return echoJson(array('code' => 1));
  2978. }
  2979. public function actionLeaveJvc()
  2980. {
  2981. if (!isLogged())
  2982. return '';
  2983. $iLotId = (int) bx_get('lot_id');
  2984. $this->_oDb->leaveJVC($iLotId, $this->_iProfileId);
  2985. }
  2986. function actionGetTalkFiles()
  2987. {
  2988. $iLotId = (int) bx_get('lot_id');
  2989. $iNumber = (int) bx_get('number');
  2990. $CNF = $this->_oConfig->CNF;
  2991. if (!$iLotId)
  2992. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_found'))));
  2993. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  2994. if (empty($aLotInfo) || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId)))
  2995. return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
  2996. if (!($iTotal = $this->_oDb->getLotFilesCount($iLotId)))
  2997. return echoJson(array('code' => 0, 'html' => MsgBox(_t('_bx_messenger_txt_msg_no_results'))));
  2998. $sContent = $this->_oTemplate->getTalkFiles($this->_iProfileId, $iLotId, $iNumber);
  2999. return echoJson(array('code' => 0, 'html' => ($iTotal !== $iNumber ? $sContent : ''), 'total' => $iTotal));
  3000. }
  3001. function actionGetCallPopup()
  3002. {
  3003. $iLotId = (int) bx_get('lot');
  3004. $aResult = array('code' => 1, 'msg' => _t('_bx_messenger_not_found'));
  3005. if (!$iLotId || !isLogged())
  3006. return echoJson($aResult);
  3007. if ($this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC) !== true)
  3008. $aResult = array('code' => 1);
  3009. else {
  3010. $mixedContent = $this->_oTemplate->getCallPopup($iLotId, $this->_iProfileId);
  3011. $aResult = array('code' => +($mixedContent === false), 'popup' => $mixedContent);
  3012. }
  3013. return echoJson($aResult);
  3014. }
  3015. function actionGetLotSettings()
  3016. {
  3017. $iLotId = (int) bx_get('lot');
  3018. $aResult = array('code' => 1, 'msg' => _t('_bx_messenger_not_found'));
  3019. if (!$iLotId || !isLogged())
  3020. return echoJson($aResult);
  3021. $aResult = array('code' => 1, 'msg' => _t('_bx_messenger_lot_can_change_settings'));
  3022. $bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true;
  3023. if (!($this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $bCheckAction))
  3024. return echoJson($aResult);
  3025. $mixedContent = $this->_oTemplate->getLotSettingsForm($iLotId);
  3026. $aResult = array(
  3027. 'code' => +($mixedContent === false),
  3028. 'popup' => array(
  3029. 'html' => $mixedContent,
  3030. 'options' => array(
  3031. 'closeOnOuterClick' => true,
  3032. 'removeOnClose' => true
  3033. )
  3034. )
  3035. );
  3036. return echoJson($aResult);
  3037. }
  3038. function actionSaveLotSettings()
  3039. {
  3040. $iLotId = (int) bx_get('lot');
  3041. $aOptions = bx_get('options');
  3042. $aResult = ['code' => 1, 'msg' => _t('_bx_messenger_not_found')];
  3043. if (!$iLotId || !isLogged() || !($aLotInfo = $this->_oDb->getLotInfoById($iLotId)))
  3044. return echoJson($aResult);
  3045. $aResult = ['code' => 1, 'msg' => _t('_bx_messenger_lot_can_change_settings')];
  3046. $bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true;
  3047. if (!($this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $bCheckAction))
  3048. return echoJson($aResult);
  3049. $aResult = ['code' => 1, 'msg' => _t('_bx_messenger_nothing_has_been_saved')];
  3050. if ($this->_oDb->saveLotSettings($iLotId, $aOptions))
  3051. return echoJson(['code' => 0]);
  3052. return echoJson($aResult);
  3053. }
  3054. function serviceSearch($sText = '', $iStart = 0, $iLimit = 10, $sOrder = 'DESC', $iLotId = 0)
  3055. {
  3056. $CNF = $this->_oConfig->CNF;
  3057. if (($iLotId && !isLogged()) || (!isAdmin() && !$iLotId))
  3058. return _t('_bx_messenger_no_permissions');
  3059. $sResult = $this->_oDb->findInHistory($iLotId, $sText, $iStart, $iLimit, $sOrder);
  3060. if (empty($sResult))
  3061. return array();
  3062. $aMessages = array();
  3063. foreach ($sResult as &$aMessage) {
  3064. $iType = $aMessage[$CNF['FIELD_TYPE']];
  3065. $sTitle = $aMessage[$CNF['FIELD_TITLE']] ?
  3066. _t($aMessage[$CNF['FIELD_TITLE']]) : _t('_bx_messenger_lots_private_lot');
  3067. $aMessages[] = array_merge($aMessage, array(
  3068. $CNF['FIELD_TITLE'] => $sTitle,
  3069. 'message_url' => $this->_oConfig->getRepostUrl($aMessage['message_id']),
  3070. $CNF['FIELD_PARTICIPANTS'] => explode(',', $aMessage[$CNF['FIELD_PARTICIPANTS']])
  3071. ));
  3072. }
  3073. return $aMessages;
  3074. }
  3075. function serviceGetVideoConference($sRoomIdent, $iInitiator, $sTitle = '', $sId = '', $aInterfaceConfig = array())
  3076. {
  3077. if (!$iInitiator)
  3078. if (!$sRoomIdent)
  3079. $sRoomIdent = $this->_iProfileId;
  3080. return $this->_oTemplate->getPublicJitsi($iInitiator, $sRoomIdent, $sTitle, $sId, $aInterfaceConfig);
  3081. }
  3082. function serviceGetPublicRoomInfo($sRoomId)
  3083. {
  3084. return $this->_oDb->getPublicVideoRoom($sRoomId);
  3085. }
  3086. function serviceGenerateRoomName($sIdent)
  3087. {
  3088. return $this->_oConfig->getRoomId($sIdent);
  3089. }
  3090. function actionGetEmojiPicker()
  3091. {
  3092. if (!$this->_iProfileId)
  3093. return;
  3094. echo $this->_oTemplate->getEmojiCode();
  3095. exit;
  3096. }
  3097. function actionUpdateUploadedFiles()
  3098. {
  3099. $iJotId = bx_get('jot_id');
  3100. $aFiles = bx_get('files');
  3101. if (!$this->isLogged())
  3102. return echoJson(['code' => 1, 'message' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1]);
  3103. if (!$iJotId || empty($aFiles))
  3104. return echoJson(['code' => 1, 'message' => MsgBox(_t('_bx_messenger_files_can_not_be_uploaded'))]);
  3105. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_SEND_MESSAGE, $this->_iProfileId);
  3106. if ($mixedResult !== true)
  3107. return echoJson(array('code' => 1, 'message' => $mixedResult));
  3108. $CNF = &$this->_oConfig->CNF;
  3109. $aLotInfo = $this->_oDb->getLotByJotId($iJotId, false);
  3110. if (
  3111. empty($aLotInfo) || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE &&
  3112. !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId))
  3113. )
  3114. return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
  3115. $aUploadedFiles = $this->_oDb->getJotFiles($iJotId);
  3116. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  3117. $aSuccessfulFiles = array();
  3118. foreach ($aFiles as &$aFile) {
  3119. $sRealName = $aFile['realname'];
  3120. if (
  3121. !empty($aUploadedFiles) && array_filter($aUploadedFiles, function ($aF) use ($sRealName, $CNF) {
  3122. return $aF[$CNF['FIELD_ST_NAME']] == $sRealName;
  3123. })
  3124. )
  3125. continue;
  3126. if (!$this->_oConfig->isValidToUpload($aFile['name'])) {
  3127. $this->_oDb->deleteJot($iJotId, $this->_iProfileId, true);
  3128. return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_bad_request')));
  3129. }
  3130. $sFile = BX_DIRECTORY_PATH_TMP . basename($aFile['name']);
  3131. $sExt = $oStorage->getFileExt($sFile);
  3132. $iFileId = $oStorage->storeFileFromPath($sFile, $aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE, $this->_iProfileId, (int) $iJotId);
  3133. $sFilename = $oStorage->getFileTitle($sRealName) . ".{$sExt}";
  3134. if ($iFileId) {
  3135. $oStorage->afterUploadCleanup($iFileId, $this->_iProfileId);
  3136. $this->_oDb->updateFiles($iFileId, array(
  3137. $CNF['FIELD_ST_JOT'] => $iJotId,
  3138. $CNF['FIELD_ST_NAME'] => $sFilename
  3139. ));
  3140. $aSuccessfulFiles[] = $sFilename;
  3141. @unlink($sFile);
  3142. }
  3143. }
  3144. if (count($aSuccessfulFiles)) {
  3145. $aFilesData = [];
  3146. $aJotInfo = $this->_oDb->getJotById($iJotId);
  3147. if (!empty($aJotInfo[$CNF['FIELD_MESSAGE_AT']])) {
  3148. $aFilesData = @unserialize($aJotInfo[$CNF['FIELD_MESSAGE_AT']]);
  3149. if (!is_array($aFilesData))
  3150. $aFilesData = [];
  3151. if (isset($aFilesData[BX_ATT_TYPE_FILES_UPLOADING]))
  3152. unset($aFilesData[BX_ATT_TYPE_FILES_UPLOADING]);
  3153. }
  3154. $aStoredFiles = [];
  3155. if (!empty($aFilesData[BX_ATT_TYPE_FILES])) {
  3156. if (is_array($aFilesData[BX_ATT_TYPE_FILES]))
  3157. $aStoredFiles = $aFilesData[BX_ATT_TYPE_FILES];
  3158. elseif (is_string($aFilesData[BX_ATT_TYPE_FILES])) {
  3159. $aStoredFiles = @unserialize($aFilesData[BX_ATT_TYPE_FILES]);
  3160. if (!is_array($aStoredFiles))
  3161. $aStoredFiles = [];
  3162. }
  3163. }
  3164. $aFilesData[BX_ATT_TYPE_FILES] = $aStoredFiles + $aSuccessfulFiles;
  3165. $this->_oDb->updateJot($iJotId, $CNF['FIELD_MESSAGE_AT'], @serialize($aFilesData));
  3166. }
  3167. echoJson(array('code' => 0));
  3168. }
  3169. function serviceGetSearchOptions()
  3170. {
  3171. $aResult = array();
  3172. $CNF = &$this->_oConfig->CNF;
  3173. foreach ($CNF['SEARCH-CRITERIA'] as &$sItem)
  3174. $aResult[$sItem] = _t("_bx_messenger_search_item_{$sItem}");
  3175. return $aResult;
  3176. }
  3177. function serviceSetTalkAvatar($iLotId, $mixedFile)
  3178. {
  3179. if (!$mixedFile)
  3180. return false;
  3181. return $this->setLotAvatar($iLotId, $mixedFile);
  3182. }
  3183. function setLotAvatar($iLotId, $mixedFile = null)
  3184. {
  3185. if (!$this->isLogged() || $mixedFile === null)
  3186. return false;
  3187. if (!($aLotInfo = $this->_oDb->getLotInfoById($iLotId)))
  3188. return false;
  3189. $CNF = &$this->_oConfig->CNF;
  3190. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  3191. if (is_array($mixedFile) && isset($mixedFile['name'])) {
  3192. $sFile = BX_DIRECTORY_PATH_TMP . basename($mixedFile['name']);
  3193. $iFileId = $oStorage->storeFileFromPath($sFile, false, $this->_iProfileId, $iLotId);
  3194. } elseif (filter_var($mixedFile, FILTER_VALIDATE_URL) !== false)
  3195. $iFileId = $oStorage->storeFileFromUrl($mixedFile, false, $this->_iProfileId, $iLotId);
  3196. elseif ((int) $mixedFile)
  3197. $iFileId = (int) $mixedFile;
  3198. if ($iFileId) {
  3199. $oStorage->afterUploadCleanup($iFileId, $this->_iProfileId);
  3200. $this->_oDb->saveLotSettings($iLotId, $iFileId, $CNF['FLS_ICON']);
  3201. }
  3202. }
  3203. function actionGetCreateGroupForm()
  3204. {
  3205. $iGroupId = (int) bx_get('group');
  3206. if (!$this->isLogged())
  3207. return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
  3208. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_GROUPS, $this->_iProfileId);
  3209. if ($mixedResult !== true)
  3210. return echoJson(array('code' => 1, 'msg' => _t($mixedResult)));
  3211. $aResult = array('code' => 0, 'popup' => $this->_oTemplate->getCreateGroupsForm($this->_iProfileId, $iGroupId));
  3212. echoJson($aResult);
  3213. }
  3214. function actionSaveGroup()
  3215. {
  3216. if (!$this->isLogged())
  3217. return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
  3218. $mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_GROUPS, $this->_iProfileId);
  3219. if ($mixedResult !== true)
  3220. return echoJson(array('code' => 1, 'msg' => _t($mixedResult)));
  3221. if ($this->_oDb->addGroup($this->_iProfileId, $_POST))
  3222. return echoJson(array('code' => 0));
  3223. choJson(array('code' => 1, 'msg' => _t('_bx_messenger_nothing_has_been_saved')));
  3224. }
  3225. function actionLoadGroupsList()
  3226. {
  3227. if (!$this->isLogged())
  3228. return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
  3229. echoJson(array('code' => 0, 'content' => $this->_oTemplate->getNavGroupsMenu($this->_iProfileId)));
  3230. }
  3231. function serviceIsBlockVersion()
  3232. {
  3233. return $this->_isBlockMessenger;
  3234. }
  3235. function actionAddMembersToGroups()
  3236. {
  3237. $CNF = &$this->_oConfig->CNF;
  3238. if (!$this->isLogged())
  3239. return false;
  3240. $aGroups = $this->_oDb->getAll("SELECT
  3241. `l`.*, `g`.`{$CNF['FMG_MODULE']}`, `g`.`{$CNF['FMG_NAME']}`, `gl`.`{$CNF['FMGL_GROUP_ID']}`,
  3242. IF (`l`.`{$CNF['FIELD_UPDATED']}` = 0, `l`.`{$CNF['FIELD_ADDED']}`, `l`.`{$CNF['FIELD_UPDATED']}`) as `order`
  3243. FROM `{$CNF['TABLE_ENTRIES']}` as `l`
  3244. LEFT JOIN `{$CNF['TABLE_GROUPS_LOTS']}` as `gl` ON `gl`.`{$CNF['FMGL_LOT_ID']}` = `l`.`{$CNF['FIELD_ID']}`
  3245. LEFT JOIN `{$CNF['TABLE_GROUPS']}` as `g` ON `g`.`{$CNF['FMG_ID']}` = `gl`.`{$CNF['FMGL_GROUP_ID']}`
  3246. WHERE `l`.`{$CNF['FIELD_TYPE']}` > 3
  3247. ORDER BY `g`.`{$CNF['FMG_MODULE']}`"); //AND (`gl`.`{$CNF['FMGL_GROUP_ID']}` IS NULL OR `gl`.`{$CNF['FMGL_GROUP_ID']}` = 0)
  3248. foreach ($aGroups as $aGroup) {
  3249. if ($aGroup[$CNF['FIELD_URL']])
  3250. $this->_oDb->registerGroup($aGroup[$CNF['FIELD_URL']], $aGroup[$CNF['FIELD_ID']]);
  3251. }
  3252. }
  3253. function actionUpdateGroups()
  3254. {
  3255. if (!isLogged())
  3256. return false;
  3257. $aModules = bx_srv('system', 'get_modules_by_type', ['context']);
  3258. bx_import('BxDolConnection');
  3259. foreach ($aModules as &$aModule)
  3260. if (!bx_srv($aModule['name'], 'act_as_profile')) {
  3261. $aMyItems = bx_srv($aModule['name'], 'get_participating_profiles', [$this->_iProfileId]);
  3262. foreach ($aMyItems as &$iItem) {
  3263. $oGroupProfile = BxDolProfile::getInstance($iItem);
  3264. if ($oGroupProfile) {
  3265. $aItemInfo = bx_srv($aModule['name'], 'get_info', array($oGroupProfile->getContentId(), false));
  3266. $oModule = BxDolModule::getInstance($aModule['name']);
  3267. $CNF = &$oModule->_oConfig->CNF;
  3268. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
  3269. if (!$oConnection)
  3270. continue;
  3271. $aFans = $oConnection->getConnectedInitiators($iItem);
  3272. if (!empty($aFans)) {
  3273. $aObject = BxDolPageQuery::getPageObject($CNF['OBJECT_PAGE_VIEW_ENTRY']);
  3274. $sUrl = "i={$aObject['uri']}&id=" . $oGroupProfile->getContentId();
  3275. $aLotInfo = $this->_oDb->getLotByUrl($sUrl);
  3276. if (empty($aLotInfo)) {
  3277. $iLotId = $this->_oDb->createLot($aItemInfo[$CNF['FIELD_AUTHOR']], $sUrl, $aItemInfo[$CNF['FIELD_TITLE']], BX_IM_TYPE_GROUPS, $aFans);
  3278. $this->_oDb->registerGroup($sUrl, $iLotId);
  3279. }
  3280. }
  3281. }
  3282. }
  3283. }
  3284. }
  3285. function actionMediaAccordion()
  3286. {
  3287. $bShown = (bool) bx_get('hidden');
  3288. $iFileId = (int) bx_get('id');
  3289. if (!$this->isLogged() || !$iFileId)
  3290. return false;
  3291. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
  3292. if (!($aFile = $oStorage->getFile($iFileId)))
  3293. return false;
  3294. $iLotId = $this->_oDb->getLotByJotId($aFile[$this->_oConfig->CNF['FIELD_ST_JOT']]);
  3295. if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  3296. return false;
  3297. if (!$bShown)
  3298. $this->_oDb->addMediaTrack($iFileId, $this->_iProfileId);
  3299. else
  3300. $this->_oDb->removeMediaTracker($iFileId, $this->_iProfileId);
  3301. }
  3302. function serviceConvertToReply()
  3303. {
  3304. $CNF = &$this->_oConfig->CNF;
  3305. $aJots = $this->_oDb->getAll("SELECT * FROM `{$CNF['TABLE_MESSAGES']}`
  3306. WHERE `{$CNF['FIELD_MESSAGE_AT_TYPE']}`=:type OR `{$CNF['FIELD_MESSAGE_AT']}` LIKE '%\"reply\"%'", array('type' => BX_ATT_TYPE_REPLY));
  3307. foreach ($aJots as &$aJot) {
  3308. if ($aJot[$CNF['FIELD_MESSAGE_AT_TYPE']] == BX_ATT_TYPE_REPLY && (int) $aJot[$CNF['FIELD_MESSAGE_AT']])
  3309. $this->_oDb->query(
  3310. "UPDATE `{$CNF['TABLE_MESSAGES']}` SET `{$CNF['FIELD_MESSAGE_REPLY']}`=:reply WHERE `{$CNF['FIELD_MESSAGE_ID']}`=:id",
  3311. array('id' => $aJot[$CNF['FIELD_MESSAGE_ID']], 'reply' => (int) $aJot[$CNF['FIELD_MESSAGE_AT']])
  3312. );
  3313. elseif (!$aJot[$CNF['FIELD_MESSAGE_AT_TYPE']] && $aJot[$CNF['FIELD_MESSAGE_AT']]) {
  3314. $aAttachment = @unserialize($aJot[$CNF['FIELD_MESSAGE_AT']]);
  3315. if (!empty($aAttachment[BX_ATT_TYPE_REPLY]))
  3316. $this->_oDb->query("UPDATE `{$CNF['TABLE_MESSAGES']}` SET `{$CNF['FIELD_MESSAGE_REPLY']}`=:reply
  3317. WHERE `{$CNF['FIELD_MESSAGE_ID']}`=:id", array('id' => $aJot[$CNF['FIELD_MESSAGE_ID']], 'reply' => $aAttachment[BX_ATT_TYPE_REPLY]));
  3318. }
  3319. }
  3320. }
  3321. function serviceUpdateMemberships()
  3322. {
  3323. $aMembershipLevels = BxDolAcl::getInstance()->getMemberships();
  3324. $sModuleName = $this->getName();
  3325. $iActionId = BxDolAcl::getInstance()->getMembershipActionId('administrate messages', $sModuleName);
  3326. $iActionEditId = BxDolAcl::getInstance()->getMembershipActionId('edit messages', $sModuleName);
  3327. $iActionDeleteId = BxDolAcl::getInstance()->getMembershipActionId('delete messages', $sModuleName);
  3328. if (!($iActionId && $iActionEditId && $iActionDeleteId) || empty($aMembershipLevels))
  3329. return false;
  3330. foreach ($aMembershipLevels as $iMembershipId => $sMembership) {
  3331. $aAction = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionId);
  3332. $aActionEdit = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionEditId);
  3333. $aActionDelete = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionDeleteId);
  3334. if (is_null($aAction['id']) || !is_null($aActionEdit['id']) || !is_null($aActionDelete['id']))
  3335. continue;
  3336. $this->_oDb->query("INSERT INTO `sys_acl_matrix` SET `IDLevel`=:level, `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionDeleteId));
  3337. $this->_oDb->query("INSERT INTO `sys_acl_matrix` SET `IDLevel`=:level, `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionEditId));
  3338. $this->_oDb->query("DELETE FROM `sys_acl_matrix` WHERE `IDLevel`=:level AND `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionId));
  3339. }
  3340. return $iActionId;
  3341. }
  3342. function servicePublicToPages()
  3343. {
  3344. $CNF = &$this->_oConfig->CNF;
  3345. $aLots = $this->_oDb->getAll("SELECT * FROM `{$CNF['TABLE_ENTRIES']}`
  3346. WHERE `{$CNF['FIELD_TYPE']}`!=:type AND `{$CNF['FIELD_URL']}` != ''", array('type' => BX_IM_TYPE_PRIVATE));
  3347. foreach ($aLots as &$aLot) {
  3348. $iGroupId = $this->_oDb->getGroupIdByLotId($aLot[$CNF['FIELD_ID']]);
  3349. if ($iGroupId)
  3350. continue;
  3351. $sUrl = $aLot[$CNF['FIELD_URL']];
  3352. if ($sUrl === 'index.php')
  3353. $sUrl = "i=index";
  3354. else
  3355. if (stripos($sUrl, 'i=') === FALSE)
  3356. $sUrl = "i=" . str_replace('r=', '', $aLot[$CNF['FIELD_URL']]);
  3357. $aGroup = $this->_oDb->getGroupByUrl($sUrl);
  3358. if (empty($aGroup))
  3359. $this->_oDb->registerGroup($sUrl, $aLot[$CNF['FIELD_ID']], true);
  3360. else
  3361. $this->_oDb->addLotToGroup($aLot[$CNF['FIELD_ID']], $aGroup[$CNF['FMG_ID']]);
  3362. }
  3363. }
  3364. public function serviceGetSafeServices()
  3365. {
  3366. return [
  3367. 'FindConvo' => 'BxMessengerServices',
  3368. 'LeaveConvo' => 'BxMessengerServices',
  3369. 'DeleteConvo' => 'BxMessengerServices',
  3370. 'ClearConvo' => 'BxMessengerServices',
  3371. 'MuteConvo' => 'BxMessengerServices',
  3372. 'StarConvo' => 'BxMessengerServices',
  3373. 'SaveJot' => 'BxMessengerServices',
  3374. 'SearchConvoMessages' => 'BxMessengerServices',
  3375. 'GetConvoFiles' => 'BxMessengerServices',
  3376. 'GetConvoSettings' => 'BxMessengerServices',
  3377. 'SaveConvoSettings' => 'BxMessengerServices',
  3378. 'GetConvo' => 'BxMessengerServices',
  3379. 'GetConvoUrl' => 'BxMessengerServices',
  3380. 'GetConvosList' => 'BxMessengerServices',
  3381. 'GetConvoMessages' => 'BxMessengerServices',
  3382. 'GetSendForm' => 'BxMessengerServices',
  3383. 'RemoveJot' => 'BxMessengerServices',
  3384. 'SearchUsers' => 'BxMessengerServices',
  3385. 'SearchLots' => 'BxMessengerServices',
  3386. 'GetPartsList' => 'BxMessengerServices',
  3387. 'SavePartsList' => 'BxMessengerServices',
  3388. 'GetBlockContacts' => 'BxMessengerServices',
  3389. //--- Aren't used in App for now.
  3390. 'GetConvoMessage' => '',
  3391. 'GetConferenceInfoByUid' => 'BxMessengerServices',
  3392. 'GetConvoItem' => '',
  3393. 'ClearGhost' => '',
  3394. ];
  3395. }
  3396. /*
  3397. * Should be moved to Services if it's needed, otherwise removed.
  3398. */
  3399. public function serviceClearGhost($sParams)
  3400. {
  3401. $aOptions = json_decode($sParams, true);
  3402. if (!isset($aOptions['id']))
  3403. return [];
  3404. $CNF = &$this->_oConfig->CNF;
  3405. $aJotInfo = $this->_oDb->getJotById($aOptions['id']);
  3406. if (empty($aJotInfo) || !$this->isAvailable($aJotInfo[$CNF['FIELD_MESSAGE_FK']]))
  3407. return [];
  3408. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  3409. if (!($aFiles = $this->_oDb->getJotFiles($aOptions['id'])))
  3410. return [];
  3411. $aResultFiles = array_map(function ($aFile) use ($CNF) {
  3412. return $aFile[$CNF['FIELD_ST_ID']];
  3413. }, $aFiles);
  3414. $oStorage->afterUploadCleanup($aResultFiles, $this->_iProfileId);
  3415. }
  3416. /*
  3417. * Moved to Services
  3418. */
  3419. public function serviceSavePartsList($sParams)
  3420. {
  3421. $aOptions = json_decode($sParams, true);
  3422. $aResult = ['code' => 1];
  3423. if (!$sParams || !isset($aOptions['parts']))
  3424. return $aResult;
  3425. $aResult = $this->saveParticipantsList($aOptions['parts'], (isset($aOptions['id']) ? $aOptions['id'] : 0));
  3426. if (isset($aResult['lot'])) {
  3427. $CNF = &$this->_oConfig->CNF;
  3428. $aLotInfo = $this->_oDb->getLotInfoById($aResult['lot']);
  3429. $aItem = $this->_oTemplate->getLotsPreview($this->_iProfileId, [$aLotInfo]);
  3430. if (!empty($aItem)) {
  3431. $aItem = current($aItem);
  3432. $sImageUrl = bx_api_get_relative_url($aItem['bx_if:user']['content']['icon']);
  3433. $aResult['convo'] = [
  3434. 'author_data' => (int) $aItem[$CNF['FIELD_AUTHOR']] ? BxDolProfile::getInstance()->getData($aItem[$CNF['FIELD_AUTHOR']]) : [
  3435. 'id' => 0,
  3436. 'display_type' => 'unit',
  3437. 'display_name' => $aItem['bx_if:user']['content']['talk_type'],
  3438. 'url' => $sImageUrl,
  3439. 'url_avatar' => $sImageUrl,
  3440. 'module' => isset($aItem['author_module']) ? $aItem['author_module'] : 'bx_pages',
  3441. ],
  3442. 'title' => $aItem[$CNF['FIELD_TITLE']],
  3443. 'message' => $aItem['bx_if:user']['content']['message'],
  3444. 'date' => $aItem['bx_if:timer']['content']['time'],
  3445. 'id' => $aLotInfo[$CNF['FIELD_HASH']],
  3446. 'total_messages' => $this->_oDb->getJotsNumber($aItem[$CNF['FIELD_ID']], 0)
  3447. ];
  3448. $aResult['lot'] = $aLotInfo[$CNF['FIELD_HASH']];
  3449. }
  3450. }
  3451. return $aResult;
  3452. }
  3453. /*
  3454. * Moved to Services
  3455. */
  3456. public function serviceSearchUsers($sParams)
  3457. {
  3458. $aOptions = json_decode($sParams, true);
  3459. $aResult = ['code' => 1];
  3460. $aUsers = [];
  3461. if (!$sParams || !isset($aOptions['term']))
  3462. return $aResult;
  3463. $aFoundProfile = $this->searchProfiles($aOptions['term'], isset($aOptions['except']) ? $aOptions['except'] : []);
  3464. if (!empty($aFoundProfile)) {
  3465. foreach ($aFoundProfile as &$aProfile) {
  3466. $oModule = BxDolModule::getInstance($aProfile['module']);
  3467. $oPCNF = &$oModule->_oConfig->CNF;
  3468. $aData = $oModule->_oDb->getContentInfoById($aProfile['id']);
  3469. $oProfile = BxDolProfile::getInstanceByContentAndType($aProfile['id'], $aProfile['module']);
  3470. $aUsers[] = array_merge($aProfile, [
  3471. 'id' => $oProfile->id(),
  3472. 'image' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_PICTURE']]),
  3473. 'cover' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_COVER']])
  3474. ]);
  3475. }
  3476. }
  3477. return $aUsers;
  3478. }
  3479. /*
  3480. * Moved to Services.
  3481. */
  3482. public function serviceFindConvo($sParams)
  3483. {
  3484. $aOptions = json_decode($sParams, true);
  3485. $aResult = ['code' => 1];
  3486. if (!isset($aOptions['param']))
  3487. return $aResult;
  3488. if ($aOptions['param']) {
  3489. $oProfile = null;
  3490. $aData = BxDolPageQuery::getSeoLink($this->_oConfig->getName(), 'messenger', ['param_name' => 'profile_id', 'uri' => $aOptions['param']]);
  3491. if (!empty($aData) && isset($aData['param_value']))
  3492. $oProfile = BxDolProfile::getInstance($aData['param_value']);
  3493. if (!$oProfile) {
  3494. $aPersonsData = BxDolPageQuery::getSeoLink('bx_persons', 'view-persons-profile', ['param_name' => 'id', 'uri' => $aOptions['param']]);
  3495. if (!empty($aPersonsData) && isset($aPersonsData['param_value']))
  3496. $oProfile = BxDolProfile::getInstanceByContentAndType($aPersonsData['param_value'], 'bx_persons');
  3497. }
  3498. if (!$oProfile) {
  3499. $aOrgData = BxDolPageQuery::getSeoLink('bx_organizations', 'view-organization-profile', ['param_name' => 'id', 'uri' => $aOptions['param']]);
  3500. if (!empty($aOrgData) && isset($aOrgData['param_value']))
  3501. $oProfile = BxDolProfile::getInstanceByContentAndType($aOrgData['param_value'], 'bx_organizations');
  3502. }
  3503. if ($oProfile) {
  3504. if ($aExistedTalk = $this->_oDb->getLotsByParticipantsList([$oProfile->id(), $this->_iProfileId], BX_IM_TYPE_PRIVATE))
  3505. return ['convo' => $aExistedTalk];
  3506. return ['profile' => BxDolProfile::getInstance()->getData($oProfile->id())];
  3507. }
  3508. }
  3509. return [];
  3510. }
  3511. /*
  3512. * Should be moved to Services if it's needed, otherwise removed.
  3513. */
  3514. public function serviceGetConvoItem($sParams)
  3515. {
  3516. $aOptions = json_decode($sParams, true);
  3517. $aResult = array('code' => 1);
  3518. if (!isset($aOptions['id']) || !($aLotInfo = $this->_oDb->getLotInfoById($aOptions['id'])))
  3519. return $aResult;
  3520. $iLotId = $aOptions['id'];
  3521. if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
  3522. return ['code' => 1, _t('_bx_messenger_not_participant')];
  3523. $CNF = $this->_oConfig->CNF;
  3524. return array_merge($aLotInfo, ['author_data' => BxDolProfile::getInstance()->getData($aLotInfo[$CNF['FIELD_AUTHOR']])]);
  3525. }
  3526. /*
  3527. * Moved to Services
  3528. */
  3529. function serviceRemoveJot($sParams = '')
  3530. {
  3531. $aOptions = json_decode($sParams, true);
  3532. $iJotId = isset($aOptions['jot_id']) ? (int) $aOptions['jot_id'] : 0;
  3533. $iLotId = isset($aOptions['lot_id']) ? $aOptions['lot_id'] : 0;
  3534. if (!$iJotId)
  3535. return [];
  3536. $this->pusherData('convo_' . $iLotId, ['convo' => $iLotId, 'action' => 'deleted', 'data' => $iJotId]);
  3537. return $this->serviceDeleteJot($iJotId, true);
  3538. }
  3539. /*
  3540. * Moved to Services
  3541. */
  3542. function serviceGetConvosList($sParams = '')
  3543. {
  3544. $aOptions = json_decode($sParams, true);
  3545. $aData = $this->serviceGetTalksList($aOptions);
  3546. $aList = $aData['list'];
  3547. if (isset($aData['code']) && !(int) $aData['code'] && !empty($aData['list']))
  3548. $aList = $this->_oTemplate->getLotsPreview($this->_iProfileId, $aData['list']);
  3549. $CNF = &$this->_oConfig->CNF;
  3550. $aResult = [];
  3551. if (!empty($aList)) {
  3552. foreach ($aList as $iKey => $aItem) {
  3553. $sImageUrl = bx_api_get_relative_url($aItem['bx_if:user']['content']['icon']);
  3554. $aResult[] = [
  3555. 'author_data' => (int) $aItem[$CNF['FIELD_AUTHOR']] ? BxDolProfile::getInstance()->getData($aItem[$CNF['FIELD_AUTHOR']]) : [
  3556. 'id' => 0,
  3557. 'display_type' => 'unit',
  3558. 'display_name' => $aItem['bx_if:user']['content']['talk_type'],
  3559. 'url' => $sImageUrl,
  3560. 'url_avatar' => $sImageUrl,
  3561. 'module' => isset($aItem['author_module']) ? $aItem['author_module'] : 'bx_pages',
  3562. ],
  3563. 'title' => $aItem[$CNF['FIELD_TITLE']],
  3564. 'message' => $aItem['bx_if:user']['content']['message'],
  3565. 'date' => $aItem['bx_if:timer']['content']['time'],
  3566. 'id' => $aData['list'][$iKey][$CNF['FIELD_HASH']],
  3567. 'id2' => $aItem[$CNF['FIELD_ID']],
  3568. 'unread' => $aItem['count'],
  3569. 'total_messages' => $this->_oDb->getJotsNumber($aItem[$CNF['FIELD_ID']], 0)
  3570. ];
  3571. }
  3572. }
  3573. return $aResult;
  3574. }
  3575. public function checkAllowedDeleteAnyEntryForProfile($isPerformAction = false, $iProfileId = false)
  3576. {
  3577. return true;
  3578. }
  3579. public function checkAllowedEditAnyEntryForProfile($isPerformAction = false, $iProfileId = false)
  3580. {
  3581. return true;
  3582. }
  3583. /**
  3584. * Moved to Services
  3585. */
  3586. public function unitAPI($iProfileId, $aParams = [])
  3587. {
  3588. $CNF = &$this->_oConfig->CNF;
  3589. $oProfile = BxDolProfile::getInstance($iProfileId);
  3590. if (!$oProfile)
  3591. return '';
  3592. $sModule = $oProfile->getModule();
  3593. $iContentId = $oProfile->getContentId();
  3594. $oModule = BxDolModule::getInstance($sModule);
  3595. $aData = $oModule->_oDb->getContentInfoById($iContentId);
  3596. $oPCNF = &$oModule->_oConfig->CNF;
  3597. // get profile's url
  3598. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $oPCNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId));
  3599. $aResult = [
  3600. 'id' => $iContentId,
  3601. 'module' => $sModule,
  3602. 'added' => $aData[$oPCNF['FIELD_ADDED']],
  3603. 'title' => $aData[$oPCNF['FIELD_TITLE']],
  3604. 'url' => bx_api_get_relative_url($sUrl),
  3605. 'image' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_PICTURE']]),
  3606. 'cover' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_COVER']])
  3607. ];
  3608. $oPrivacy = BxDolPrivacy::getObjectInstance($oPCNF['OBJECT_PRIVACY_VIEW']);
  3609. $bPrivacy = $oPrivacy !== false;
  3610. $sKey = 'OBJECT_MENU_SNIPPET_META';
  3611. if (!empty($CNF[$sKey]) && ($oMetaMenu = BxDolMenu::getObjectInstance($CNF[$sKey], $oModule->_oTemplate)) !== false) {
  3612. $bPublic = !$bPrivacy || $oPrivacy->check($iContentId) || $oPrivacy->isPartiallyVisible($aData[$CNF['FIELD_ALLOW_VIEW_TO']]);
  3613. $oMetaMenu->setContentModule($sModule);
  3614. $oMetaMenu->setContentId($iContentId);
  3615. $oMetaMenu->setContentPublic($bPublic);
  3616. $aResult['meta'] = $oMetaMenu->getCodeAPI();
  3617. }
  3618. return $aResult;
  3619. }
  3620. function serviceGetBlockContactsMessenger($mixedParams = [])
  3621. {
  3622. if (bx_is_api())
  3623. return bx_srv($this->getName(), 'get_block_contacts', [$mixedParams], 'Services');
  3624. return $this->_oTemplate->getContacts($this->_iProfileId, $mixedParams);
  3625. }
  3626. public function serviceGetBroadcastFields($aInputsAdd = array())
  3627. {
  3628. $aFields = $this->_oTemplate->_getBroadcastFields();
  3629. $aResult = [];
  3630. foreach ($aFields as $sName => $aField) {
  3631. if (isset($aField['skip']) || !isset($aField['caption']))
  3632. continue;
  3633. $aResult[$sName] = $aField['caption'];
  3634. }
  3635. return $aResult;
  3636. }
  3637. private function queueBroadcastDelivery($iConvoId, $iJotId, $iAuthorId, $aNotificationsType = [], $iAfterUserId = 0)
  3638. {
  3639. if (!$iConvoId || !$iJotId || !$iAuthorId)
  3640. return false;
  3641. $sStamp = str_replace('.', '', sprintf('%.6F', microtime(true)));
  3642. $sJobName = sprintf('%s_broadcast_%d_%d_%s', $this->_oConfig->getName(), $iConvoId, $iAfterUserId, $sStamp);
  3643. return BxDolBackgroundJobs::getInstance()->add($sJobName, [
  3644. $this->_oConfig->getName(),
  3645. 'process_broadcast_queue',
  3646. [$iConvoId, $iJotId, $iAuthorId, (array) $aNotificationsType, $iAfterUserId],
  3647. 'Module'
  3648. ], 5);
  3649. }
  3650. public function serviceProcessBroadcastQueue($iConvoId, $iJotId, $iAuthorId, $aNotificationsType = [], $iAfterUserId = 0)
  3651. {
  3652. if (!$iConvoId || !$iJotId || !$iAuthorId)
  3653. return false;
  3654. $CNF = &$this->_oConfig->CNF;
  3655. $aConvoInfo = $this->_oDb->getLotInfoById($iConvoId);
  3656. $aJotInfo = $this->_oDb->getJotById($iJotId);
  3657. if (empty($aConvoInfo) || empty($aJotInfo) || (int) $aConvoInfo[$CNF['FIELD_TYPE']] !== BX_IM_TYPE_BROADCAST)
  3658. return false;
  3659. $sModule = $this->_oConfig->getObject('alert');
  3660. for ($iBatch = 0; $iBatch < self::BROADCAST_BATCHES_PER_RUN; $iBatch++) {
  3661. $aParticipants = $this->_oDb->getBroadcastParticipants($iConvoId, self::BROADCAST_BATCH_SIZE + 1, $iAfterUserId);
  3662. if (empty($aParticipants))
  3663. return true;
  3664. $bHasMore = count($aParticipants) > self::BROADCAST_BATCH_SIZE;
  3665. if ($bHasMore)
  3666. $aParticipants = array_slice($aParticipants, 0, self::BROADCAST_BATCH_SIZE);
  3667. $this->_oDb->markAsNewJot($aParticipants, $iConvoId, $iJotId, false);
  3668. foreach ($aParticipants as $iPart) {
  3669. bx_alert($sModule, 'got_broadcast_ntfs', $iConvoId, $iAuthorId, [
  3670. 'object_author_id' => $iPart,
  3671. 'recipient_id' => $iPart,
  3672. 'subobject_id' => $iJotId,
  3673. 'source' => $sModule . '_' . $iJotId,
  3674. 'source_mac' => $sModule . '_' . $iAuthorId . '_' . $iJotId,
  3675. ] + (array) $aNotificationsType);
  3676. }
  3677. $iAfterUserId = (int) end($aParticipants);
  3678. if (!$bHasMore)
  3679. return true;
  3680. }
  3681. return $this->queueBroadcastDelivery($iConvoId, $iJotId, $iAuthorId, $aNotificationsType, $iAfterUserId);
  3682. }
  3683. private function getProfilesByCriteria($aData, $aOptions = [])
  3684. {
  3685. $bCountOnly = !empty($aOptions['count_only']);
  3686. $aExcludeProfiles = $aOptions['exclude_profiles'] ?? [];
  3687. $aIncludeProfiles = $aOptions['include_profiles'] ?? [];
  3688. if (isset($aData['connection_type'])) {
  3689. if ($oConnection = $this->_oConfig->getConnectionByType($aData['connection_type'])) {
  3690. if ($bCountOnly && empty($aExcludeProfiles) && empty($aIncludeProfiles))
  3691. return (int)$oConnection->getConnectedContentCount($this->_iProfileId, $aData['connection_type'] === 'friends');
  3692. $aProfiles = $oConnection->getConnectedContent($this->_iProfileId, $aData['connection_type'] === 'friends');
  3693. if (!empty($aExcludeProfiles))
  3694. $aProfiles = array_values(array_diff($aProfiles, $aExcludeProfiles));
  3695. if (!empty($aIncludeProfiles))
  3696. $aProfiles = array_unique(array_merge($aProfiles, $aIncludeProfiles));
  3697. return $bCountOnly ? count($aProfiles) : $aProfiles;
  3698. }
  3699. }
  3700. $CNF = &$this->_oConfig->CNF;
  3701. if (!($oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_FILTER'], $CNF['OBJECT_FORM_FILTER_DISPLAY'], $this->_oTemplate)))
  3702. return false;
  3703. $oForm->filteredForm();
  3704. $aFields = [];
  3705. foreach ($oForm->aInputs as &$aInput) {
  3706. if (!isset($aData[$aInput['name']]) || isset($aInput['skip']) || $aInput['name'] === 'convo_type')
  3707. continue;
  3708. if (!empty($aInput['values']))
  3709. $aFields[$aInput['name']] = array_filter($aData[$aInput['name']], function ($sVal) use ($aInput) {
  3710. return in_array($sVal, array_keys($aInput['values']));
  3711. });
  3712. else
  3713. $aFields[$aInput['name']] = $aData[$aInput['name']];
  3714. }
  3715. $aParams = [];
  3716. if (!empty($aExcludeProfiles))
  3717. $aParams['exclude_profiles'] = $aExcludeProfiles;
  3718. if (!empty($aIncludeProfiles))
  3719. $aParams['include_profiles'] = $aIncludeProfiles;
  3720. $aProfiles = $this->_oDb->getProfilesByCriteria($aFields, $aParams);
  3721. bx_alert($this->_oConfig->getObject('alert'), 'broadcast_profiles_list', 0, 0, [
  3722. 'profiles' => &$aProfiles
  3723. ]);
  3724. if ($bCountOnly)
  3725. return count($aProfiles);
  3726. return $aProfiles;
  3727. }
  3728. function actionCalculateProfiles()
  3729. {
  3730. $aData = bx_get('data');
  3731. $aManuall = array_values(array_unique(array_filter(array_map('intval', (array)bx_get('manually')), function ($iProfileId) {
  3732. return $iProfileId > 0;
  3733. })));
  3734. $iConvoType = isset($aData['convo_type']) ? $aData['convo_type'] : 0;
  3735. if (!$this->isLogged() || (empty($aData) && empty($aManuall)) || !$iConvoType)
  3736. return false;
  3737. $aParams['count_only'] = true;
  3738. if (!empty($aManuall))
  3739. $aParams['include_profiles'] = $aManuall;
  3740. $iTotal = (int)$this->getProfilesByCriteria($aData, $aParams);
  3741. echoJson(['msg' => _t('_bx_messenger_broadcast_total_profiles', $iTotal)]);
  3742. }
  3743. function serviceGetBroadcastCard($iJotId)
  3744. {
  3745. if (!$iJotId)
  3746. return '';
  3747. $aLotInfo = $this->_oDb->getLotByJotId($iJotId, false);
  3748. $aJotInfo = $this->_oDb->getJotById($iJotId);
  3749. return $this->_oTemplate->parseHtmlByName('broadcast-message.html', ['title' => $aLotInfo['title'], 'content' => $aJotInfo['message']]);
  3750. }
  3751. function actionGetFilterCriteria()
  3752. {
  3753. if (!$this->isLogged() && $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_BROADCASTS) !== true)
  3754. return echoJson(['code' => 1]);
  3755. $CNF = &$this->_oConfig->CNF;
  3756. $sType = bx_get('type');
  3757. $sHtml = '';
  3758. switch ($sType) {
  3759. case BX_MSG_TALK_TYPE_BROADCAST:
  3760. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_FILTER'], $CNF['OBJECT_FORM_FILTER_DISPLAY'], $this->_oTemplate);
  3761. if ($oForm->filteredForm())
  3762. $sHtml = $oForm->getCode();
  3763. else
  3764. $sHtml = MsgBox(_t('_bx_messenger_no_criteria_available'));
  3765. break;
  3766. case 'followers':
  3767. case 'friends':
  3768. $sHtml = $this->_oTemplate->getConnectionsForm($sType, $this->_iProfileId);
  3769. break;
  3770. /*default:
  3771. $aForm = [
  3772. 'form_attrs' => [
  3773. 'method' => 'post',
  3774. 'id' => $CNF['OBJECT_FORM_ENTRY'],
  3775. 'class' => 'space-y-4 max-h-60 overflow-y-auto'
  3776. ],
  3777. 'inputs' => $this->_oTemplate->getNotificationFormData()
  3778. ];
  3779. $oForm = new BxTemplFormView($aForm);
  3780. $sHtml = $oForm -> getCode();*/
  3781. }
  3782. echoJson(['code' => 0, 'html' => $sHtml]);
  3783. }
  3784. public function actionAjaxGetRecipients()
  3785. {
  3786. $sTerm = bx_get('term');
  3787. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  3788. header('Content-Type:text/javascript; charset=utf-8');
  3789. echo (json_encode($a));
  3790. }
  3791. function actionGetTalkInfo()
  3792. {
  3793. $iLotId = (int) bx_get('lot_id');
  3794. if (!$iLotId)
  3795. return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_found'))));
  3796. if (!$this->isAvailable($iLotId))
  3797. return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
  3798. $sContent = $this->_oTemplate->getInfoBlockContent($iLotId);
  3799. return echoJson(array('code' => 0, 'html' => $sContent));
  3800. }
  3801. public function broadcastNewJot($iLotId, $iJotId)
  3802. {
  3803. $iLotId = (int)$iLotId;
  3804. $iJotId = (int)$iJotId;
  3805. if (!$iLotId || !$iJotId)
  3806. return false;
  3807. $CNF = &$this->_oConfig->CNF;
  3808. $aLotInfo = $this->_oDb->getLotInfoById($iLotId);
  3809. if (empty($aLotInfo) || empty($aLotInfo[$CNF['FIELD_HASH']]))
  3810. return false;
  3811. $sHash = mb_strtolower($aLotInfo[$CNF['FIELD_HASH']]);
  3812. // NEO JotItem expects Services API shape (menu.items)
  3813. bx_import('Services', $this->_aModule);
  3814. $oServices = new BxMessengerServices();
  3815. $this->pusherData('convo_' . $sHash, [
  3816. 'convo' => $iLotId,
  3817. 'action' => 'added',
  3818. 'data' => $oServices->serviceGetConvoMessages([
  3819. 'load' => 'ids',
  3820. 'lot' => $sHash,
  3821. 'jot' => $iJotId,
  3822. ]),
  3823. ]);
  3824. $aParticipantsList = $this->_oDb->getParticipantsList($iLotId, true);
  3825. foreach ($aParticipantsList as $iProfile) {
  3826. $this->pusherData('profile_' . $iProfile, ['convo' => $iLotId]);
  3827. }
  3828. return true;
  3829. }
  3830. /**
  3831. * Moved to Services
  3832. */
  3833. private function pusherData($sAction, $aData = [])
  3834. {
  3835. $oSockets = BxDolSockets::getInstance();
  3836. if (isset($aData['convo']) && (int) $aData['convo']) {
  3837. $CNF = &$this->_oConfig->CNF;
  3838. $aLotInfo = $this->_oDb->getLotInfoById($aData['convo']);
  3839. $aData['id'] = mb_strtolower($aLotInfo[$CNF['FIELD_HASH']]);
  3840. }
  3841. $aData['user_id'] = $this->_iProfileId;
  3842. if ($oSockets->isEnabled() && $sAction && !empty($aData)) {
  3843. $oSockets->sendEvent('bx', 'messenger', $sAction, $aData);
  3844. }
  3845. }
  3846. }
  3847. /** @} */