📜 ⬆️ ⬇️

Convenient classes for getting IM statuses in PHP

Inspired by the topic of receiving status of instant messengers in PHP, I decided to structure the code for obtaining statuses, and so my small project was born.

The basic concept of my project is:
1. OOP
2. Use of universal and comfortable class bikes for basic and auxiliary functions.
3. Getting the status as described in the enum code, but not by text or other message, the separation of the form from the controller.

The mechanism for obtaining a status is honestly stolen from the research of the author of the original article and, in general, does not differ. As a getting mechanism, a ready-made download class is used, which is based both on cUrl and PHP's native file_get_contents; you can download the content of the page with or without HTTP headers by entering the appropriate parameter.
')
As described above, status codes are contained in a special enum, error codes are located side by side. Not every status recipient can use all types of statuses, but all of the possible ones are listed here:
class enmIMStatus { const imsOffline = 0x00; const imsOnline = 0x01; const imsAway = 0x02; const imsDoNotDisturb = 0x03; const imsNotAvailable = 0x04; const imsFreeForChat = 0x05; } class enmImError { const imeNoError = 0x00; const imeBadIdentity = 0x01; const imeUnknownStatus = 0x02; const imeConnectionErr = 0x03; } 


The main class for obtaining the status of tBasicIMGetter is abstract, it contains the general functionality for each of the status recipients - the designer, creating a copy of the basic page-reader and creating an array of predicted statuses. Such an array will be needed for dirty code, where the function of getting a class can be called several times.
The statuses can be preloaded (for example, from the database) by the preloadStatuses ($ aStatuses) function, the $ aStatuses array should be presented in id_Messager format => status_code.
The status is obtained by the getImStatus function ($ aIdentity), the parameter of which is the messenger ID, for ICQ it is the number, for Mail.ru Agent is the mailbox address ...
The base class has two abstract functions — direct status acquisition and ID verification for the type of messenger. These functions form the basis of each of the specialized status recipients.
abstract protected function doUpdateImStatus($aIdentity);
abstract protected function checkImIdentity($aIdentity);


Currently, the project supports the following types of messengers: ICQ, Skype, Jabber, VK, Mail.Ru Agent. Each of the status recipients is implemented as a class inherited from the base tBasicIMGetter.
On the example of ICQ, such a class will look like this:
 class tICQStatusGetter extends tBasicIMGetter { protected function checkImIdentity($aIdentity) { return !empty($aIdentity) && is_numeric($aIdentity) && (intval($aIdentity) > 10000) ? intval($aIdentity) : false; } protected function doUpdateImStatus($aIdentity) { $lContents = $this->fCDownloader->getURLContents('http://status.icq.com/online.gif?icq=' . $aIdentity . '&img=27', true); if(!empty($lContents)) { $lGotStatus = false; if(strstr($lContents, 'online1')) $lGotStatus = enmIMStatus::imsOnline; elseif(strstr($lContents, 'online0')) $lGotStatus = enmIMStatus::imsOffline; elseif(strstr($lContents, 'online2')) $lGotStatus = enmIMStatus::imsAway; if($lGotStatus !== false) { $this->fLastError = enmImError::imeNoError; $this->doUpdateCachedStatus($aIdentity, $lGotStatus); } else $this->fLastError = enmImError::imeUnknownStatus; } else $this->fLastError = enmImError::imeConnectionErr; } } 


After receiving the content of the downloaded document, it is parsed and the corresponding status is set relative to the specified IM id.
A bit of code for quick functions and error checking. If something does not work, the output will indicate that the user with the specified ID is not online.

As an example of obtaining statuses, I sketched the following code, demonstrating the functionality of each of the status recipients:
 $lIMStatusesInText = array( enmIMStatus::imsOffline => 'Offline', enmIMStatus::imsOnline => 'Online', enmIMStatus::imsAway => 'Away', enmIMStatus::imsDoNotDisturb => 'Do not disturb', enmIMStatus::imsNotAvailable => 'Available', enmIMStatus::imsFreeForChat => 'Free for chat' ); $lImStatusGetters = array(); $lImStatusGetters['icq'] = new tICQStatusGetter(); $lImStatusGetters['jabber'] = new tJabberStatusGetter(); $lImStatusGetters['mail.ru agent'] = new tMRAStatusGetter(); $lImStatusGetters['skype'] = new tSkypeStatusGetter(); $lImStatusGetters['vkontakte'] = new tVKStatusGetter(); $lImIdentificators = array( 'icq' => '_icq', 'jabber' => '__jid', 'mail.ru agent' => '__mail.ru', 'skype' => '_skype', 'vkontakte' => 'id___', ); foreach($lImStatusGetters as $lKey => &$lGetter) echo $lKey, ': ', $lIMStatusesInText[$lGetter->getImStatus($lImIdentificators[$lKey])], '<br>', PHP_EOL; 


In accordance with the rules of good tone, the project code is available on github: github.com/Urvin/IM-Statuses-for-PHP
I hope someone will be useful.

Source: https://habr.com/ru/post/150724/


All Articles