api.globalsearch.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Ubilling user search implementation
  4. */
  5. class GlobalSearch {
  6. /**
  7. * Contains requred javascripts code
  8. *
  9. * @var string
  10. */
  11. protected $jsRuntime = '';
  12. /**
  13. * Contains some styles for search controls
  14. *
  15. * @var string
  16. */
  17. protected $styles = '';
  18. /**
  19. * Contains default search input placeholder
  20. *
  21. * @var string
  22. */
  23. protected $placeholder = '';
  24. /**
  25. * Contains system alter config as key=>value
  26. *
  27. * @var array
  28. */
  29. protected $alterConf = array();
  30. /**
  31. * Contains raw user data for further usage
  32. *
  33. * @var array
  34. */
  35. protected $rawData = array();
  36. /**
  37. * Contains configurable search fields list
  38. *
  39. * @var array
  40. */
  41. protected $fields = array();
  42. /**
  43. * UbillingConfig object placeholder
  44. *
  45. * @var null
  46. */
  47. protected $ubConfig = null;
  48. /**
  49. * Path to globalsearch cache file
  50. */
  51. const CACHE_NAME = 'exports/globalsearchcache.dat';
  52. /**
  53. * Some exceptions here
  54. */
  55. const EX_NO_SEARCHTYPE = 'SEARCHTYPE_NOT_DETECTED';
  56. /**
  57. * Creates new globalsearch instance
  58. *
  59. * @return void
  60. */
  61. public function __construct($stylesPath = '') {
  62. global $ubillingConfig;
  63. $this->ubConfig = $ubillingConfig;
  64. $this->loadAlter();
  65. $this->setPlaceholder();
  66. $this->setStyles($stylesPath);
  67. $this->setJsRuntime();
  68. }
  69. /**
  70. * Loads system alter config into protected prop
  71. *
  72. * @return void
  73. */
  74. protected function loadAlter() {
  75. $this->alterConf = $this->ubConfig->getAlter();
  76. }
  77. /**
  78. * Sets javascript runtime
  79. *
  80. * @return void
  81. */
  82. protected function setJsRuntime() {
  83. if (@$this->alterConf['SPHINX_SEARCH_ENABLED']) {
  84. $searchLib = 'sphinxsearch.js';
  85. } else {
  86. $searchLib = 'glsearch.js';
  87. }
  88. $libPath = '';
  89. if (file_exists(CUR_SKIN_PATH . $searchLib)) {
  90. $libPath = CUR_SKIN_PATH . $searchLib;
  91. } else {
  92. $libPath = 'modules/jsc/' . $searchLib;
  93. }
  94. $this->jsRuntime = wf_tag('script', false, '', 'type="text/javascript" language="javascript" src="' . $libPath . '"');
  95. $this->jsRuntime .= wf_tag('script', true);
  96. }
  97. /**
  98. * Sets CSS input styling
  99. *
  100. * @param string $stylesPath custom css location path
  101. *
  102. * @return void
  103. */
  104. protected function setStyles($stylesPath = '') {
  105. if (@$this->alterConf['SPHINX_SEARCH_ENABLED']) {
  106. $searchCss = 'sphinxsearch.css';
  107. } else {
  108. $searchCss = 'glsearch.css';
  109. }
  110. if (empty($stylesPath)) {
  111. $fullPath = 'skins/' . $searchCss;
  112. } else {
  113. $fullPath = $stylesPath . $searchCss;
  114. }
  115. $this->styles = wf_tag('link', false, '', 'rel="stylesheet" href="' . $fullPath . '" type="text/css" media="screen""');
  116. $this->styles .= wf_tag('link', true);
  117. }
  118. /**
  119. * Sets input placeholder
  120. *
  121. * @return void
  122. */
  123. protected function setPlaceholder() {
  124. $this->placeholder = ' value="' . __('User search') . '" onfocus="if(!this._haschanged){this.value=\'\'};this._haschanged=true;"';
  125. }
  126. /**
  127. * Renders search form
  128. *
  129. * @param string $appendClass
  130. *
  131. * @return string
  132. */
  133. public function renderSearchInput($appendClass = '') {
  134. $result = '';
  135. if (!empty($appendClass)) {
  136. $appendClass = ' ' . $appendClass;
  137. }
  138. if ($this->alterConf['GLOBALSEARCH_ENABLED']) {
  139. $result .= $this->styles;
  140. $result .= $this->jsRuntime;
  141. if (@$this->alterConf['SPHINX_SEARCH_ENABLED']) {
  142. //render SphinxSearch input
  143. $result .= wf_tag('input', false, 'sphinxsearch-input' . $appendClass, 'type="text" name="globalsearchquery" autocomplete="off" id="sphinxsearchinput" oninput="querySearch(this.value)"' . $this->placeholder);
  144. $result .= wf_HiddenInput('globalsearch_type', 'full');
  145. $result .= wf_tag('ul', false, 'ui-menu ui-widget ui-autocomplete ui-front sphinxsearchcontainer', 'id="ssearchcontainer" style="display: none;"');
  146. $result .= wf_tag('ul', true);
  147. } else {
  148. //render standard GlobalSearch input
  149. $result .= wf_tag('input', false, '.ui-autocomplete' . $appendClass, 'type="text" id="globalsearch" name="globalsearchquery"' . $this->placeholder);
  150. $result .= wf_tag('input', false, '', 'type="hidden" id="globalsearch_type" name="globalsearch_type" value=""');
  151. }
  152. } else {
  153. $result = wf_tag('input', false, '', 'type="text" name="partialaddr"' . $this->placeholder);
  154. }
  155. $result .= '';
  156. return ($result);
  157. }
  158. /**
  159. * Prepares data array to json encoding
  160. *
  161. * @param array $data data array to transform
  162. * @param string $category data category
  163. * @param string $type globalsearch type
  164. * @return array
  165. */
  166. protected function transformArray($data, $category, $type) {
  167. $result = array();
  168. if (!empty($data)) {
  169. foreach ($data as $io => $each) {
  170. if (!empty($each)) {
  171. $result[zb_rand_string(8)] = array(
  172. 'label' => $each,
  173. 'lower' => strtolower_utf8($each),
  174. 'category' => $category,
  175. 'type' => $type
  176. );
  177. }
  178. }
  179. }
  180. return ($result);
  181. }
  182. /**
  183. * Preloads raw data for searchable user fields and controls caching
  184. *
  185. * @return void
  186. */
  187. protected function loadRawdata($forceCache = false) {
  188. $cacheTime = $this->alterConf['GLOBALSEARCH_CACHE'];
  189. $cacheTime = time() - ($cacheTime * 60); //in minutes
  190. $addressExtendedOn = $this->ubConfig->getAlterParam('ADDRESS_EXTENDED_ENABLED');
  191. //extracting user fields types to load
  192. if (!empty($this->alterConf['GLOBALSEARCH_FIELDS'])) {
  193. $this->fields = explode(',', $this->alterConf['GLOBALSEARCH_FIELDS']);
  194. $this->fields = array_flip($this->fields);
  195. }
  196. $updateCache = false;
  197. if (file_exists(self::CACHE_NAME)) {
  198. $updateCache = false;
  199. if ((filemtime(self::CACHE_NAME) > $cacheTime)) {
  200. $updateCache = false;
  201. } else {
  202. $updateCache = true;
  203. }
  204. } else {
  205. $updateCache = true;
  206. }
  207. //force cache parameter
  208. if ($forceCache) {
  209. $updateCache = true;
  210. }
  211. //updating rawdata cache
  212. if ($updateCache) {
  213. //loading needed fields
  214. if (isset($this->fields['realname'])) {
  215. $this->rawData = $this->rawData + $this->transformArray(zb_UserGetAllRealnames(), __('Real Name'), 'realname');
  216. }
  217. if (isset($this->fields['address'])) {
  218. $this->rawData = $this->rawData + $this->transformArray(zb_AddressGetFulladdresslist(), __('Full address'), 'address');
  219. }
  220. if ($addressExtendedOn and isset($this->fields['address_extend'])) {
  221. $this->rawData = $this->rawData + $this->transformArray(zb_AddressExtenGetList(), __('Extended address info'), 'address_extend');
  222. }
  223. if (isset($this->fields['contract'])) {
  224. $allContracts = zb_UserGetAllContracts();
  225. $allContracts = array_flip($allContracts);
  226. $this->rawData = $this->rawData + $this->transformArray($allContracts, __('Contract'), 'contract');
  227. }
  228. if ((isset($this->fields['phone'])) OR ( isset($this->fields['mobile']))) {
  229. $allPhonedata = zb_UserGetAllPhoneData();
  230. if (isset($this->fields['phone'])) {
  231. if (!empty($allPhonedata)) {
  232. $allPhones = array();
  233. foreach ($allPhonedata as $io => $each) {
  234. $allPhones[$io] = $each['phone'];
  235. }
  236. $this->rawData = $this->rawData + $this->transformArray($allPhones, __('Phone'), 'phone');
  237. }
  238. }
  239. if (isset($this->fields['mobile'])) {
  240. if (!empty($allPhonedata)) {
  241. $allMobiles = array();
  242. foreach ($allPhonedata as $io => $each) {
  243. $allMobiles[$io] = $each['mobile'];
  244. }
  245. $this->rawData = $this->rawData + $this->transformArray($allMobiles, __('Mobile'), 'mobile');
  246. }
  247. }
  248. }
  249. if (isset($this->fields['ip'])) {
  250. $this->rawData = $this->rawData + $this->transformArray(zb_UserGetAllIPs(), __('IP'), 'ip');
  251. }
  252. if (isset($this->fields['mac'])) {
  253. $this->rawData = $this->rawData + $this->transformArray(zb_UserGetAllIpMACs(), __('MAC address'), 'mac');
  254. }
  255. if (isset($this->fields['login'])) {
  256. $allLogins = zb_UserGetAllStargazerLogins();
  257. $this->rawData = $this->rawData + $this->transformArray($allLogins, __('Login'), 'login');
  258. }
  259. if (isset($this->fields['seal'])) {
  260. $conDet = new ConnectionDetails();
  261. $allSeals = $conDet->getAllSeals();
  262. $this->rawData = $this->rawData + $this->transformArray($allSeals, __('Cable seal'), 'seal');
  263. }
  264. if (isset($this->fields['paymentid'])) {
  265. if ($this->alterConf['OPENPAYZ_SUPPORT']) {
  266. if ($this->alterConf['OPENPAYZ_REALID']) {
  267. $allPayIds_q = "SELECT * from `op_customers`";
  268. $allPayIds = simple_queryall($allPayIds_q);
  269. $tmpArrPayids = array();
  270. if (!empty($allPayIds)) {
  271. foreach ($allPayIds as $io => $each) {
  272. $tmpArrPayids[$each['realid']] = $each['virtualid'];
  273. }
  274. }
  275. $this->rawData = $this->rawData + $this->transformArray($tmpArrPayids, __('Payment ID'), 'payid');
  276. } else {
  277. $allPayIds_q = "SELECT `login`,`IP` from `users`";
  278. $allPayIds = simple_queryall($allPayIds_q);
  279. $tmpArrPayids = array();
  280. if (!empty($allPayIds)) {
  281. foreach ($allPayIds as $io => $each) {
  282. $tmpArrPayids[$each['login']] = ip2int($each['IP']);
  283. }
  284. }
  285. $this->rawData = $this->rawData + $this->transformArray($tmpArrPayids, __('Payment ID'), 'payid');
  286. }
  287. }
  288. }
  289. file_put_contents(self::CACHE_NAME, serialize($this->rawData));
  290. } else {
  291. $this->rawData = file_get_contents(self::CACHE_NAME);
  292. $this->rawData = unserialize($this->rawData);
  293. }
  294. }
  295. /**
  296. * Returns json encoded data for input autocomplete
  297. *
  298. * @return void
  299. */
  300. public function ajaxCallback($forceCache = false) {
  301. $this->loadRawdata($forceCache);
  302. $data = array();
  303. if (!empty($this->rawData)) {
  304. $term = (wf_CheckGet(array('term'))) ? strtolower_utf8($_GET['term']) : '';
  305. foreach ($this->rawData as $io => $each) {
  306. if ($term) {
  307. if (ispos($each['lower'], $term)) {
  308. $data[] = $each;
  309. }
  310. } else {
  311. $data[] = $each;
  312. }
  313. }
  314. }
  315. if (!$forceCache) {
  316. //output not needed
  317. die(json_encode($data));
  318. }
  319. }
  320. /**
  321. * Detects searchtype by search query fragment
  322. *
  323. * @param string $term
  324. * @return string
  325. */
  326. public function detectSearchType($term) {
  327. $result = '';
  328. $term = trim($term);
  329. if (!empty($term)) {
  330. $term = strtolower_utf8($term);
  331. $this->loadRawdata();
  332. if (!empty($this->rawData)) {
  333. foreach ($this->rawData as $io => $each) {
  334. if (ispos($each['lower'], $term)) {
  335. $result = $each['type'];
  336. break;
  337. }
  338. }
  339. }
  340. }
  341. return ($result);
  342. }
  343. }
  344. ?>