Source of file api.telepathy.php
Size: 15,577 Bytes - Last Modified: 2024-04-27T15:26:05+03:00
/tmp/current_snapshot/api/libs/api.telepathy.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 | <?php /** * Prophetic guessing login by the address/surname/realname */ class Telepathy { /** * Contains system alter config as key=>value * * @var array */ protected $altCfg = array(); /** * Contains all available user address * * @var array */ protected $alladdress = array(); /** * Contains all available users realnames * * @var array */ protected $allrealnames = array(); /** * Contains preprocessed users surnames * * @var array */ protected $allsurnames = array(); /** * Contains all available user mobiles * * @var array */ protected $allMobiles = array(); /** * Contains all available additional user mobiles * * @var array */ protected $allExtMobiles = array(); /** * Contains all available user phones * * @var array */ protected $allPhones = array(); /** * Contains all available user mobiles with doubles * * @var array */ protected $allMobilesFull = array(); /** * Contains all available additional user mobiles with doubles * * @var array */ protected $allExtMobilesFull = array(); /** * Contains all available user phones with doubles * * @var array */ protected $allPhonesFull = array(); /** * Case sensitivity flag * * @var bool */ protected $caseSensitive = false; /** * Cached address usage flag * * @var bool */ protected $cachedAddress = true; /** * Use phones caching or not? * * @var bool */ protected $cachedPhones = false; /** * Return only uniq login when telepaty by phones * * @var bool */ protected $uniqLogin = false; /** * City display flag * * @var array */ protected $citiesAddress = false; /** * System caching object placeholder * * @var object */ protected $cache = ''; /** * Contains users previously detected by phone number as number=>login * * @var array */ protected $phoneTelepathyCache = array(); /** * Contains phone data caching time in seconds */ const PHONE_CACHE_TIME = 86400; /** * Creates new telepathy instance * * @param bool $caseSensitive * @param bool $cachedAddress * @param bool $citiesAddress * @param bool $cachedPhones * @param bool $uniqLogin * * @return void */ public function __construct($caseSensitive = false, $cachedAddress = true, $citiesAddress = false, $cachedPhones = false, $uniqLogin = false) { $this->caseSensitive = $caseSensitive; $this->cachedAddress = $cachedAddress; $this->citiesAddress = $citiesAddress; $this->cachedPhones = $cachedPhones; $this->uniqLogin = $uniqLogin; $this->loadConfig(); $this->initCache(); $this->loadAddress(); if (!$this->caseSensitive) { $this->addressToLowerCase(); } if (!empty($this->alladdress)) { $this->alladdress = array_flip($this->alladdress); } } /** * Loads system alter.ini config into protected property for further usage * * @global object $ubillingConfig * * @return void */ protected function loadConfig() { global $ubillingConfig; $this->altCfg = $ubillingConfig->getAlter(); } /** * Inits system cache * * @return void */ protected function initCache() { $this->cache = new UbillingCache(); } /** * Loads cached address data to private data property * * @return void */ protected function loadAddress() { if (!$this->citiesAddress) { if ($this->cachedAddress) { $this->alladdress = zb_AddressGetFulladdresslistCached(); } else { $this->alladdress = zb_AddressGetFulladdresslist(); } } else { $this->alladdress = zb_AddressGetFullCityaddresslist(); } } /** * Loads all user realnames from database into private prop * * @return void */ protected function loadRealnames() { $this->allrealnames = zb_UserGetAllRealnames(); } /** * Normalizes mobile number to +380 format. * May be not acceptable for countries other than Ukraine. * * @param string $mobile * * @return string/void on error */ protected function normalizePhoneFormat($mobile) { $mobile = vf($mobile, 3); $len = strlen($mobile); //all is ok if ($len != 12) { switch ($len) { case 11: $mobile = '3' . $mobile; break; case 10: $mobile = '38' . $mobile; break; case 9: $mobile = '380' . $mobile; break; } } $newLen = strlen($mobile); if ($newLen == 12) { $mobile = '+' . $mobile; } else { $mobile = ''; } return ($mobile); } /** * Loads all existing phone data into protected props for further usage * * @return void */ public function usePhones() { //init previously detected phones cache $this->phoneTelepathyCache = $this->cache->get('PHONETELEPATHY', self::PHONE_CACHE_TIME); if (empty($this->phoneTelepathyCache)) { $this->phoneTelepathyCache = array(); } //loading user phones data if ($this->cachedPhones) { $allPhoneData = $this->cache->get('PHONEDATA', self::PHONE_CACHE_TIME); if (empty($allPhoneData)) { $allPhoneData = zb_UserGetAllPhoneData(); $this->cache->set('PHONEDATA', $allPhoneData, self::PHONE_CACHE_TIME); } } else { $allPhoneData = zb_UserGetAllPhoneData(); } if (!empty($allPhoneData)) { foreach ($allPhoneData as $login => $each) { $cleanMobile = vf($each['mobile'], 3); if (!empty($cleanMobile)) { if ($this->uniqLogin) { $this->allMobilesFull[$cleanMobile][] = $login; } else { $this->allMobiles[$cleanMobile] = $login; } } $cleanPhone = vf($each['phone'], 3); if (!empty($cleanPhone)) { if ($this->uniqLogin) { $this->allMobilesFull[$cleanPhone][] = $login; } else { $this->allMobiles[$cleanPhone] = $login; } } } } //additional mobiles loading if enabled if ($this->altCfg['MOBILES_EXT']) { if ($this->cachedPhones) { $allExtTmp = $this->cache->get('EXTMOBILES', self::PHONE_CACHE_TIME); if (empty($allExtTmp)) { $extMob = new MobilesExt(); $allExtTmp = $extMob->getAllMobilesUsers(); $this->cache->set('EXTMOBILES', $allExtTmp, self::PHONE_CACHE_TIME); } } else { $extMob = new MobilesExt(); $allExtTmp = $extMob->getAllMobilesUsers(); } if (!empty($allExtTmp)) { foreach ($allExtTmp as $eachExtMobile => $login) { $cleanExtMobile = vf($eachExtMobile, 3); if ($this->uniqLogin) { $this->allMobilesFull[$cleanExtMobile][] = $login; } else { $this->allMobiles[$cleanExtMobile] = $login; } } } } } /** * Preprocess all user surnames into usable data * * @return void */ protected function surnamesExtract() { if (!empty($this->allrealnames)) { foreach ($this->allrealnames as $login => $realname) { $raw = explode(' ', $realname); if (!empty($raw)) { $this->allsurnames[$login] = $raw[0]; } } } } /** * external passive constructor for name realname login detection * * @return void */ public function useNames() { $this->loadRealnames(); $this->surnamesExtract(); if (!empty($this->allrealnames)) { $this->allrealnames = array_flip($this->allrealnames); } if (!empty($this->allrealnames)) { $this->allsurnames = array_flip($this->allsurnames); } } /** * preprocess available address data into lower case * * @return void */ protected function addressToLowerCase() { global $ubillingConfig; $alterconf = $ubillingConfig->getAlter(); $cacheTime = $alterconf['ADDRESS_CACHE_TIME']; $cacheTime = time() - ($cacheTime * 60); if (!$this->citiesAddress) { $cacheName = 'exports/fulladdresslistlowercache.dat'; } else { $cacheName = 'exports/fullcityaddresslistlowercache.dat'; } $updateCache = false; if (file_exists($cacheName)) { $updateCache = false; if ((filemtime($cacheName) > $cacheTime)) { $updateCache = false; } else { $updateCache = true; } } else { $updateCache = true; } if (($alterconf['ADDRESS_CACHE_TIME']) AND ( $this->cachedAddress)) { if ($updateCache) { $tmpArr = array(); if (!empty($this->alladdress)) { foreach ($this->alladdress as $eachlogin => $eachaddress) { $tmpArr[$eachlogin] = strtolower_utf8($eachaddress); } $this->alladdress = $tmpArr; $tmpArr = array(); } //store property to cache $cacheStoreData = serialize($this->alladdress); file_put_contents($cacheName, $cacheStoreData); $cacheStoreData = ''; } else { $rawCacheData = file_get_contents($cacheName); $rawCacheData = unserialize($rawCacheData); $this->alladdress = $rawCacheData; $rawCacheData = array(); } } else { $tmpArr = array(); if (!empty($this->alladdress)) { foreach ($this->alladdress as $eachlogin => $eachaddress) { $tmpArr[$eachlogin] = strtolower_utf8($eachaddress); } $this->alladdress = $tmpArr; $tmpArr = array(); } } } /** * detects user login by its address * * @param string $address address to guess * * @return string */ public function getLogin($address) { if (!$this->caseSensitive) { $address = strtolower_utf8($address); } if (isset($this->alladdress[$address])) { return ($this->alladdress[$address]); } else { return(false); } } /** * returns user login by surname * * @param string $surname * * @return string */ public function getBySurname($surname) { if (isset($this->allsurnames[$surname])) { return ($this->allsurnames[$surname]); } else { return(false); } } /** * Get user login by some phone number * * @param string $phoneNumber * @param bool $onlyMobile * @param bool $normalizeMobile * * @return string */ public function getByPhone($phoneNumber, $onlyMobile = false, $normalizeMobile = false) { $result = ''; /** * Come with us speeding through the night * As fast as any bird in flight * Silhouettes against the Mother Moon * We will be there * I think it's a bad idea to normalize the phone by code, since this piece of code works current for Ukraine */ $phoneNumber = ($normalizeMobile) ? $this->normalizePhoneFormat($phoneNumber) : $phoneNumber; if (!empty($phoneNumber)) { if (!$onlyMobile) { if (!empty($this->allPhones) and ! $this->uniqLogin) { foreach ($this->allPhones as $baseNumber => $userLogin) { if (ispos((string) $phoneNumber, (string) $baseNumber)) { $result = $userLogin; } } } } if (!empty($this->allExtMobiles) and ! $this->uniqLogin) { foreach ($this->allExtMobiles as $baseNumber => $userLogin) { if (ispos((string) $phoneNumber, (string) $baseNumber)) { $result = $userLogin; } } } if (!empty($this->allMobiles) and ! $this->uniqLogin) { foreach ($this->allMobiles as $baseNumber => $userLogin) { if (ispos((string) $phoneNumber, (string) $baseNumber)) { $result = $userLogin; } } } if ($this->uniqLogin) { $resultTempUniq = array_merge_recursive($this->allPhonesFull, $this->allExtMobilesFull, $this->allMobilesFull); // Try remove duplicate phone and mobile from one users foreach ($resultTempUniq as $phone => $dataArr) { $rawArr = array_unique($dataArr); if (count($rawArr) == 1 and substr($phone, -10) == substr($phoneNumber, -10)) { $result = $rawArr[0]; return ($result); } } } } return ($result); } /** * Get user login by some phone number. After all calls you must finalize cache with savePhoneTelepathyCache(). * * @param string $phoneNumber * @param bool $onlyMobile * @param bool $normalizeMobile * * @return string */ public function getByPhoneFast($phoneNumber, $onlyMobile = false, $normalizeMobile = false) { $result = ''; if (isset($this->phoneTelepathyCache[$phoneNumber])) { $result = $this->phoneTelepathyCache[$phoneNumber]; } else { $detectedLogin = $this->getByPhone($phoneNumber, $onlyMobile, $normalizeMobile); $result = $detectedLogin; $this->phoneTelepathyCache[$phoneNumber] = $detectedLogin; } return ($result); } /** * Saves previously detected by phone logins cache * * @return void */ public function savePhoneTelepathyCache() { $this->cache->set('PHONETELEPATHY', $this->phoneTelepathyCache, self::PHONE_CACHE_TIME); } /** * Cleans phone telepathy cache * * @return void */ public function flushPhoneTelepathyCache() { $this->cache->delete('PHONETELEPATHY'); } } ?> |