Source of file api.ubim.php
Size: 14,437 Bytes - Last Modified: 2024-04-27T15:26:05+03:00
/tmp/current_snapshot/api/libs/api.ubim.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 | <?php /* * Ubilling instant messenger API */ /** * Creates message for some admin user * * @param string $to admin login * @param string $text message text * * @return void */ function im_CreateMessage($to, $text) { $to = mysql_real_escape_string($to); $text = mysql_real_escape_string($text); $text = strip_tags($text); $from = whoami(); $date = curdatetime(); $read = 0; $query = "INSERT INTO `ub_im` ( `id` , `date` , `from` , `to` , `text` , `read` ) VALUES ( NULL , '" . $date . "', '" . $from . "', '" . $to . "', '" . $text . "', '" . $read . "' ); "; nr_query($query); log_register("UBIM SEND FROM {" . $from . "} TO {" . $to . "}"); } /** * Deletes message by its id * * @param int $msgid message id from `ub_im` * * @return void */ function im_DeleteMessage($msgid) { $msgid = vf($msgid, 3); $query = "DELETE from `ub_im` WHERE `id`='" . $msgid . "'"; nr_query($query); log_register("UBIM DELETE [" . $msgid . "]"); } /** * Shows avatar control form * * @return string */ function im_AvatarControlForm() { $me = whoami(); $mail = gravatar_GetUserEmail($me); $cells = wf_TableCell(wf_tag('h1') . $me . wf_tag('h1', true), '', '', 'align="center"'); $rows = wf_TableRow($cells); $cells = wf_TableCell(gravatar_ShowAdminAvatar($me, '256'), '', '', 'align="center"'); $rows .= wf_TableRow($cells); $cells = wf_TableCell(wf_tag('h3') . __('Your email') . ': ' . $mail . wf_tag('h3', true), '', '', 'align="center"'); $rows .= wf_TableRow($cells); $cells = wf_TableCell(wf_Link("http://gravatar.com/emails/", __('Change my avatar at gravatar.com')), false, '', 'align="center"'); $rows .= wf_TableRow($cells); $result = wf_TableBody($rows, '100%', '0', 'glamour'); $result .= wf_BackLink("?module=ubim&checknew=true", __('Back'), false, 'ubButton'); return ($result); } /** * Check is message created by me? * * @param int $msgid message id from `ub_im` * * @return bool */ function im_IsMineMessage($msgid) { $msgid = vf($msgid, 3); $me = whoami(); $query = "SELECT `from` FROM `ub_im` WHERE `id`='" . $msgid . "'"; $data = simple_query($query); if (!empty($data)) { if ($data['from'] == $me) { //message created by me return (true); } else { //or not return (false); } } else { //message not exists return (false); } } /** * mark thread as read by sender * * @param string $sender sender login * * @return void */ function im_ThreadMarkAsRead($sender) { $sender = mysql_real_escape_string($sender); $me = whoami(); $query = "UPDATE `ub_im` SET `read` = '1' WHERE `to` = '" . $me . "' AND `from`='" . $sender . "' AND `read`='0'"; nr_query($query); } /** * Return unread messages count for each contact * * @param string $username framework admin username * * @return string */ function im_CheckForUnreadMessagesByUser($username) { $username = mysql_real_escape_string($username); $me = whoami(); $query = "SELECT COUNT(`id`) from `ub_im` WHERE `to`='" . $me . "' AND `from`='" . $username . "' AND `read`='0'"; $data = simple_query($query); $result = $data['COUNT(`id`)']; return ($result); } /** * Returns array of users from which we have some unread messages as login=>count * * @return array */ function im_GetAllMyUnreadMessagesUsers() { $result = array(); $me = whoami(); $query = "SELECT `from`,`id` from `ub_im` WHERE `read`='0' AND `to`='" . $me . "'"; $all = simple_queryall($query); if (!empty($all)) { foreach ($all as $io => $each) { if (isset($result[$each['from']])) { $result[$each['from']] ++; } else { $result[$each['from']] = 1; } } } return($result); } /** * Return contact list * * @return string */ function im_ContactList() { $me = whoami(); @$employeeNames = unserialize(ts_GetAllEmployeeLoginsCached()); $alladmins = rcms_scandir(DATA_PATH . "users/"); $activeAdmins = im_GetActiveAdmins(); $haveUnread = im_GetAllMyUnreadMessagesUsers(); $result = ''; $rows = ''; if (!empty($alladmins)) { foreach ($alladmins as $eachadmin) { if ($eachadmin != $me) { //need checks for unread messages for each user $contactClass = 'ubimcontact'; if (wf_CheckGet(array('checknew'))) { $unreadCounter = (isset($haveUnread[$eachadmin])) ? $haveUnread[$eachadmin] : 0; if ($unreadCounter != 0) { $contactClass = 'ubimcontactincome'; } } if (isset($activeAdmins[$eachadmin])) { $aliveFlag = web_bool_led(true); } else { $aliveFlag = web_bool_led(false); } $conatactAvatar = gravatar_ShowAdminAvatar($eachadmin, '32') . ' '; $adminName = (isset($employeeNames[$eachadmin])) ? $employeeNames[$eachadmin] : $eachadmin; $threadLink = wf_AjaxLink("?module=ubim&showthread=" . $eachadmin, $adminName, 'threadContainer', false, $contactClass); $cells = wf_TableCell($aliveFlag, '', '', 'valign="center" align="center"'); $cells .= wf_TableCell($conatactAvatar, '35', '', 'valign="center" align="left"'); $cells .= wf_TableCell($threadLink, '', '', 'valign="center" align="left"'); $rows .= wf_TableRow($cells, ''); } } $result = wf_TableBody($rows, '100%', '0', 'glamour'); $myAva = gravatar_ShowAdminAvatar($me, '16'); $result .= wf_CleanDiv(); $result .= wf_delimiter() . wf_Link("?module=ubim&avatarcontrol=true", $myAva . ' ' . __('Avatar control'), false, 'ubButton'); } return ($result); } /** * Return UB im main window grid * * @return string */ function im_MainWindow() { $contactList = wf_AjaxLoader(); $contactList .= im_ContactList(); $gridcells = wf_TableCell($contactList, '25%', '', 'valign="top"'); $threadContainer = wf_tag('div', false, 'ubimchat', 'id="threadContainer"'); $threadContainer .= wf_tag('div', true); $gridcells .= wf_TableCell($threadContainer, '75%', '', 'valign="top"'); $gridrows = wf_TableRow($gridcells); $result = wf_TableBody($gridrows, '100%', '0'); return ($result); } /** * Return conversation form for some thread * * @param string $to - thread username * * @return string */ function im_ConversationForm($to) { $inputs = wf_HiddenInput('im_message_to', $to); $inputs .= wf_TextArea('im_message_text', '', '', true, '60x4'); $inputs .= wf_Submit('Send message'); $result = wf_Form("", 'POST', $inputs, 'glamour'); return ($result); } /** * Shows thread for me with some user * * @param string $threadUser user to show thread * * @return string */ function im_ThreadShow($threadUser) { global $ubillingConfig; $altCfg = $ubillingConfig->getAlter(); $me = whoami(); @$employeeNames = unserialize(ts_GetAllEmployeeLoginsCached()); $threadUser = mysql_real_escape_string($threadUser); $adminName = (isset($employeeNames[$threadUser])) ? $employeeNames[$threadUser] : $threadUser; $result = __('No conversations with') . ' ' . $adminName . ' ' . __('yet') . wf_delimiter(); $rows = ''; $query = "SELECT * from `ub_im` WHERE (`to`='" . $me . "' AND `from`='" . $threadUser . "') OR (`to`='" . $threadUser . "' AND `from`='" . $me . "') ORDER BY `date` DESC"; $alldata = simple_queryall($query); if (!empty($alldata)) { foreach ($alldata as $io => $each) { //read icon $readIcon = ($each['read'] == '0') ? wf_img("skins/icon_inactive.gif", __('Unread message')) : ''; $fromName = (isset($employeeNames[$each['from']])) ? $employeeNames[$each['from']] : $each['from']; $cells = wf_TableCell(wf_tag('b') . $fromName . wf_tag('b', true), '20%', '', 'align="center"'); $cells .= wf_TableCell($each['date'] . ' ' . $readIcon, '80%'); $rows .= wf_TableRow($cells, 'row2'); $messageText = nl2br($each['text']); if (!isset($altCfg['UBIM_NO_LINKIFY'])) { $messageText = im_linkify($messageText); } else { if (!$altCfg['UBIM_NO_LINKIFY']) { $messageText = im_linkify($messageText); } } $cells = wf_TableCell(gravatar_ShowAdminAvatar($each['from'], '64'), '', 'row3', 'align="center"'); $cells .= wf_TableCell($messageText, '', 'row3'); $rows .= wf_TableRow($cells); } $result = wf_TableBody($rows, '100%', '0'); //mark all unread messages as read now im_ThreadMarkAsRead($threadUser); } return ($result); } /** * Loads some thread after message posted into standard grid * @param string $threadUser thread username * * @return string */ function im_ThreadLoad($threadUser) { $result = wf_tag('script', false, '', 'type="text/javascript"'); $result .= 'goajax(\'?module=ubim&showthread=' . $threadUser . '\',\'threadContainer\');'; $result .= wf_tag('script', true); return ($result); } /** * Checks how many unread messages we have? * * @return string */ function im_CheckForUnreadMessages() { $me = whoami(); $result = 0; $query = "SELECT COUNT(`id`) from `ub_im` WHERE `to`='" . $me . "' AND `read`='0'"; $data = simple_query($query); if (!empty($data)) { $result = $data['COUNT(`id`)']; } return ($result); } /** * Draw update container and refresh into in some code * * @return void */ function im_RefreshContainer($timeout) { $timeout = $timeout * 1000; //in seconds $jstimer = wf_AjaxLoader() . " <script type=\"text/javascript\"> $(function() { var alertedMessagesCount = 0; $(window).everyTime(" . $timeout . ", function() { $.ajax({ url: '?module=ubim&timecheckunread=true', dataType: 'json',success: function(data) { if(data.messagesCount > 0) { if(alertedMessagesCount != data.messagesCount) { // You have new message // if(!alert.visible) { // alert(data.messagesCount+' '+'" . __('new message received') . "'); // } $(document).ready(function() { var position = 'top-right'; var settings = { 'speed' : 'fast', 'duplicates' : true, 'autoclose' : 5000 }; $.sticky(data.messagesCount+' '+'" . __('new message received') . "'); }); alertedMessagesCount = data.messagesCount; } } } }); }); }); </script> "; $container = wf_tag('span', false, '', 'id="refreshcontainer"'); $container .= wf_tag('span', true); $container .= $jstimer; show_window('', $container); } /** * Returns array of "active" administrators * * @return array */ function im_GetActiveAdmins() { $result = array(); $timeout = 10; $query = "SELECT DISTINCT `admin` from `weblogs` WHERE `date` > DATE_SUB(NOW(), INTERVAL " . $timeout . " MINUTE);"; $all = simple_queryall($query); if (!empty($all)) { foreach ($all as $io => $each) { $result[$each['admin']] = $each['admin']; } } return ($result); } /** * Turn all URLs in clickable links. * * @param string $value * @param array $protocols http/https, ftp, mail, twitter * @param array $attributes * @param string $mode normal or all * @return string */ function im_linkify($value, $protocols = array('http', 'mail'), array $attributes = array(), $mode = 'normal') { // Link attributes $attr = ''; foreach ($attributes as $key => $val) { $attr = ' ' . $key . '="' . htmlentities($val) . '"'; } $links = array(); // Extract existing links and tags $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value); // Extract text links for each protocol foreach ((array) $protocols as $protocol) { switch ($protocol) { case 'http': case 'https': $value = preg_replace_callback($mode != 'all' ? '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i' : '~([^\s<]+\.[^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { if ($match[1]) $protocol = $match[1]; $link = $match[2] ?: $match[3]; return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">' . $link . '</a>') . '>'; }, $value); break; case 'mail': $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; case 'twitter': $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="https://twitter.com/' . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . '">' . $match[0] . '</a>') . '>'; }, $value); break; default: $value = preg_replace_callback($mode != 'all' ? '~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i' : '~([^\s<]+)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) { return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $match[1] . '">' . $match[1] . '</a>') . '>'; }, $value); break; } } // Insert all link return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); } ?> |