api.signup.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. class SignupService {
  3. //settings
  4. protected $configRaw = array();
  5. protected $optionCitySelectable = true;
  6. protected $optionStreetSelectable = true;
  7. protected $optionCityDisplay = true;
  8. protected $optionEmailDisplay = true;
  9. protected $optionSpamTraps = true;
  10. protected $optionCaching = false;
  11. protected $optionConfCaching=false;
  12. protected $optionServices = '';
  13. protected $optionTariffs = '';
  14. protected $optionIspName = '';
  15. protected $optionIspUrl = '';
  16. protected $optionIspLogo = '';
  17. protected $optionSidebarText = '';
  18. protected $optionGreetingText = '';
  19. protected $optionHideouts = '';
  20. protected $cachingTime=3600;
  21. //caching
  22. const CACHE_PATH = 'cache/';
  23. protected $cachigTime = 3600;
  24. //other properties
  25. protected $cities = array();
  26. protected $streets = array();
  27. protected $services = array();
  28. protected $tariffs = array();
  29. protected $hideouts = array();
  30. protected $spamTraps = array('surname', 'lastname', 'seenoevil', 'mobile');
  31. protected $required = array('street', 'build', 'realname', 'phone');
  32. protected $important = '';
  33. public function __construct($confcache=0,$cachetimeout=3600) {
  34. if ($confcache) {
  35. $this->optionConfCaching=true;
  36. }
  37. $this->cachingTime=$cachetimeout;
  38. $this->important = ' ' . la_tag('sup') . '*' . la_tag('sup', true);
  39. $this->loadConfig();
  40. $this->configPreprocess();
  41. $this->loadServices();
  42. $this->loadTariffs();
  43. $this->loadHideouts();
  44. $this->setTemplateData();
  45. }
  46. /**
  47. * Loads sigreqconf config from database
  48. *
  49. * @return void
  50. */
  51. protected function loadConfig() {
  52. if ($this->optionConfCaching) {
  53. $cacheTime = $this->cachingTime;
  54. $cacheTime = time() - $cacheTime;
  55. $cacheName = self::CACHE_PATH . 'config.dat';
  56. $updateCache = false;
  57. if (file_exists($cacheName)) {
  58. $updateCache = false;
  59. if ((filemtime($cacheName) > $cacheTime)) {
  60. $updateCache = false;
  61. } else {
  62. $updateCache = true;
  63. }
  64. } else {
  65. $updateCache = true;
  66. }
  67. if (!$updateCache) {
  68. //read data directly from cache
  69. $result = array();
  70. $rawData = file_get_contents($cacheName);
  71. if (!empty($rawData)) {
  72. $rawData= base64_decode($rawData);
  73. $result = unserialize($rawData);
  74. }
  75. $this->configRaw = $result;
  76. } else {
  77. //updating cache
  78. $query = "SELECT * from `sigreqconf`";
  79. $all = simple_queryall($query);
  80. if (!empty($all)) {
  81. foreach ($all as $io => $each) {
  82. $this->configRaw[$each['key']] = $each['value'];
  83. }
  84. }
  85. $cacheStoreData = serialize($this->configRaw);
  86. $cacheStoreData = base64_encode($cacheStoreData);
  87. file_put_contents($cacheName, $cacheStoreData);
  88. }
  89. } else {
  90. $query = "SELECT * from `sigreqconf`";
  91. $all = simple_queryall($query);
  92. if (!empty($all)) {
  93. foreach ($all as $io => $each) {
  94. $this->configRaw[$each['key']] = $each['value'];
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * preprocessing of raw config into private triggers
  101. *
  102. * @return void
  103. */
  104. protected function configPreprocess() {
  105. //preprocess data
  106. $this->optionIspName = (isset($this->configRaw['ISP_NAME'])) ? $this->configRaw['ISP_NAME'] : '';
  107. $this->optionIspUrl = (isset($this->configRaw['ISP_URL'])) ? $this->configRaw['ISP_URL'] : '';
  108. $this->optionIspLogo = (isset($this->configRaw['ISP_LOGO'])) ? $this->configRaw['ISP_LOGO'] : '';
  109. $this->optionSidebarText = (isset($this->configRaw['SIDEBAR_TEXT'])) ? $this->configRaw['SIDEBAR_TEXT'] : '';
  110. $this->optionGreetingText = (isset($this->configRaw['GREETING_TEXT'])) ? $this->configRaw['GREETING_TEXT'] : '';
  111. $this->optionServices = (isset($this->configRaw['SERVICES'])) ? $this->configRaw['SERVICES'] : '';
  112. $this->optionTariffs = (isset($this->configRaw['TARIFFS'])) ? $this->configRaw['TARIFFS'] : '';
  113. $this->optionHideouts = (isset($this->configRaw['HIDEOUTS'])) ? $this->configRaw['HIDEOUTS'] : '';
  114. $this->optionCitySelectable = (isset($this->configRaw['CITY_SELECTABLE'])) ? true : false;
  115. $this->optionCityDisplay = (isset($this->configRaw['CITY_DISPLAY'])) ? true : false;
  116. $this->optionStreetSelectable = (isset($this->configRaw['STREET_SELECTABLE'])) ? true : false;
  117. $this->optionEmailDisplay = (isset($this->configRaw['EMAIL_DISPLAY'])) ? true : false;
  118. $this->optionSpamTraps = (isset($this->configRaw['SPAM_TRAPS'])) ? true : false;
  119. $this->optionCaching = (isset($this->configRaw['CACHING'])) ? true : false;
  120. }
  121. /**
  122. * sets ISP name and others propertys to external scope
  123. *
  124. * @return void
  125. */
  126. public function setTemplateData() {
  127. global $templateData;
  128. $templateData['ISP_NAME'] = $this->optionIspName;
  129. $templateData['ISP_URL'] = $this->optionIspUrl;
  130. $templateData['ISP_LOGO'] = $this->optionIspLogo;
  131. if ((!empty($this->optionIspName)) AND ( !empty($this->optionIspUrl)) AND ( !empty($this->optionIspLogo))) {
  132. $templateData['ISP_LINK'] = la_Link($this->optionIspUrl, la_img($this->optionIspLogo, $this->optionIspName), false);
  133. } else {
  134. $templateData['ISP_LINK'] = '';
  135. }
  136. $templateData['SIDEBAR_TEXT'] = $this->optionSidebarText;
  137. $templateData['GREETING_TEXT'] = $this->optionGreetingText;
  138. }
  139. /**
  140. * loads cities from database into private data property
  141. *
  142. * @return void
  143. */
  144. protected function loadCities() {
  145. if ($this->optionCaching) {
  146. $cacheTime = $this->cachingTime;
  147. $cacheTime = time() - $cacheTime;
  148. $cacheName = self::CACHE_PATH . 'city.dat';
  149. $updateCache = false;
  150. if (file_exists($cacheName)) {
  151. $updateCache = false;
  152. if ((filemtime($cacheName) > $cacheTime)) {
  153. $updateCache = false;
  154. } else {
  155. $updateCache = true;
  156. }
  157. } else {
  158. $updateCache = true;
  159. }
  160. if (!$updateCache) {
  161. //read data directly from cache
  162. $result = array();
  163. $rawData = file_get_contents($cacheName);
  164. if (!empty($rawData)) {
  165. $result = unserialize($rawData);
  166. }
  167. $this->cities = $result;
  168. } else {
  169. //updating cache
  170. $query = "SELECT * from `city` ORDER BY `id` ASC";
  171. $all = simple_queryall($query);
  172. if (!empty($all)) {
  173. foreach ($all as $io => $each) {
  174. $this->cities[$each['id']] = $each['cityname'];
  175. }
  176. }
  177. $cacheStoreData = serialize($this->cities);
  178. file_put_contents($cacheName, $cacheStoreData);
  179. }
  180. } else {
  181. $query = "SELECT * from `city` ORDER BY `id` ASC";
  182. $all = simple_queryall($query);
  183. if (!empty($all)) {
  184. foreach ($all as $io => $each) {
  185. $this->cities[$each['id']] = $each['cityname'];
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * prepares services for service selector inputs
  192. *
  193. * @return void
  194. */
  195. protected function loadServices() {
  196. if (!empty($this->optionServices)) {
  197. $tmpArr = explode(',', $this->optionServices);
  198. if (!empty($tmpArr)) {
  199. foreach ($tmpArr as $io => $each) {
  200. $this->services[trim($each)] = trim($each);
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * prepares tariffs if available, for tariffs selector inputs
  207. *
  208. * @return void
  209. */
  210. protected function loadTariffs() {
  211. if (!empty($this->optionTariffs)) {
  212. $tmpArr = explode(',', $this->optionTariffs);
  213. if (!empty($tmpArr)) {
  214. foreach ($tmpArr as $io => $each) {
  215. $this->tariffs[trim($each)] = trim($each);
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * prepares hideouts if available, for excluding in city and streets lists
  222. *
  223. * @return void
  224. */
  225. protected function loadHideouts() {
  226. if (!empty($this->optionHideouts)) {
  227. $this->hideouts = explode(',', $this->optionHideouts);
  228. }
  229. }
  230. /**
  231. * loads streets from database into private data property
  232. *
  233. * @return void
  234. */
  235. protected function loadStreets() {
  236. if ($this->optionCaching) {
  237. $cacheTime = $this->cachingTime;
  238. $cacheTime = time() - $cacheTime;
  239. $cacheName = self::CACHE_PATH . 'street.dat';
  240. $updateCache = false;
  241. if (file_exists($cacheName)) {
  242. $updateCache = false;
  243. if ((filemtime($cacheName) > $cacheTime)) {
  244. $updateCache = false;
  245. } else {
  246. $updateCache = true;
  247. }
  248. } else {
  249. $updateCache = true;
  250. }
  251. if (!$updateCache) {
  252. //read data directly from cache
  253. $result = array();
  254. $rawData = file_get_contents($cacheName);
  255. if (!empty($rawData)) {
  256. $result = unserialize($rawData);
  257. }
  258. $this->streets = $result;
  259. } else {
  260. //updating cache
  261. $query = "SELECT * from `street`";
  262. $all = simple_queryall($query);
  263. if (!empty($all)) {
  264. foreach ($all as $io => $each) {
  265. $this->streets[$each['id']] = $each['streetname'];
  266. }
  267. }
  268. $cacheStoreData = serialize($this->streets);
  269. file_put_contents($cacheName, $cacheStoreData);
  270. }
  271. } else {
  272. //cache disabled
  273. $query = "SELECT * from `street`";
  274. $all = simple_queryall($query);
  275. if (!empty($all)) {
  276. foreach ($all as $io => $each) {
  277. $this->streets[$each['id']] = $each['streetname'];
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * returns city input depends selectable option
  284. *
  285. * @return string
  286. */
  287. protected function cityInput() {
  288. $result = '';
  289. if ($this->optionCitySelectable) {
  290. $this->loadCities();
  291. if (!empty($this->cities)) {
  292. $cityNames = array();
  293. foreach ($this->cities as $io => $each) {
  294. $cityNames[$each] = $each;
  295. }
  296. //hideouts processing
  297. if (!empty($this->hideouts)) {
  298. foreach ($this->hideouts as $ia => $hideout) {
  299. if (isset($cityNames[$hideout])) {
  300. unset($cityNames[$hideout]);
  301. }
  302. }
  303. }
  304. $result = la_JuiComboBox('city', $cityNames, __('Town') . $this->important, '', false);
  305. }
  306. } else {
  307. $result = la_TextInput('city', __('Town') . $this->important, '', false, 15);
  308. }
  309. return ($result);
  310. }
  311. /**
  312. * returns street input depends options
  313. *
  314. * @return string
  315. */
  316. protected function streetInput() {
  317. $result = '';
  318. if ($this->optionStreetSelectable) {
  319. $this->loadStreets();
  320. if (!empty($this->streets)) {
  321. $streetNames = array();
  322. foreach ($this->streets as $io => $each) {
  323. $streetNames[$each] = $each;
  324. }
  325. if (!empty($streetNames)) {
  326. natsort($streetNames);
  327. }
  328. $sortedStreets = array('' => __('Select one'));
  329. $sortedStreets = array_merge($sortedStreets, $streetNames);
  330. //hideouts processing
  331. if (!empty($this->hideouts)) {
  332. foreach ($this->hideouts as $ia => $hideout) {
  333. if (isset($sortedStreets[$hideout])) {
  334. unset($sortedStreets[$hideout]);
  335. }
  336. }
  337. }
  338. $result = la_JuiComboBox('street', $sortedStreets, __('Street') . $this->important, '', false);
  339. }
  340. } else {
  341. $result = la_TextInput('street', __('Street') . $this->important, '', false, 25);
  342. }
  343. return ($result);
  344. }
  345. /**
  346. * returns build input
  347. *
  348. * @return string
  349. */
  350. protected function buildInput() {
  351. $result = la_TextInput('build', __('Build') . $this->important, '', false, '5');
  352. return ($result);
  353. }
  354. /**
  355. * returns apartment input
  356. *
  357. * @return string
  358. */
  359. protected function aptInput() {
  360. $result = la_TextInput('apt', __('Apartment') . la_tag('sup') . '&nbsp' . la_tag('sup', true), '', false, '5'); //vertical align ugly hack
  361. return ($result);
  362. }
  363. /**
  364. * returns realname input
  365. *
  366. * @return string
  367. */
  368. protected function realnameInput() {
  369. $result = la_TextInput('realname', __('Real name') . $this->important, '', false, '25');
  370. return ($result);
  371. }
  372. /**
  373. * returns phone number input
  374. *
  375. * @return string
  376. */
  377. protected function phoneInput() {
  378. $result = la_TextInput('phone', __('Phone') . $this->important, '', false, '25');
  379. return ($result);
  380. }
  381. /**
  382. * returns phone number input
  383. *
  384. * @return string
  385. */
  386. protected function emailInput() {
  387. $result = la_TextInput('email', __('Email'), '', false, '25');
  388. return ($result);
  389. }
  390. /**
  391. * returns services select input
  392. *
  393. * @return string
  394. */
  395. protected function serviceInput() {
  396. $result = '';
  397. if (!empty($this->services)) {
  398. $result = la_JuiComboBox('service', $this->services, __('Service') . $this->important, '', false);
  399. }
  400. return ($result);
  401. }
  402. /**
  403. * returns tariffs select input
  404. *
  405. * @return string
  406. */
  407. protected function tariffsInput() {
  408. $result = '';
  409. if (!empty($this->tariffs)) {
  410. $result = la_JuiComboBox('tariff', $this->tariffs, __('Tariff'), '', false);
  411. }
  412. return ($result);
  413. }
  414. /**
  415. * anti spam bots dirty magic inputs ;)
  416. *
  417. * @rerutn string
  418. */
  419. protected function spambotsTrap() {
  420. $result = la_tag('input', false, 'somemagic', 'type="text" name="surname"');
  421. $result.= la_tag('input', false, '', 'type="text" name="lastname" style="display:none;"');
  422. $result.= la_tag('input', false, 'somemagic', 'type="text" name="seenoevil"');
  423. $result.= la_tag('input', false, 'somemagic', 'type="text" name="mobile"');
  424. return ($result);
  425. }
  426. /**
  427. * returns signup notes input
  428. *
  429. * @return string
  430. */
  431. protected function notesInput() {
  432. $result = la_TextArea('notes', __('Notes'), '', false, '50x5');
  433. return ($result);
  434. }
  435. /**
  436. * returns signup service main form
  437. *
  438. * @retun string
  439. */
  440. public function renderForm() {
  441. $inputs = '';
  442. $inputs.=la_HiddenInput('createrequest', 'true');
  443. //greeting text
  444. $inputs.=$this->optionGreetingText;
  445. //optional city selector
  446. if ($this->optionCityDisplay) {
  447. $inputs.=$this->cityInput();
  448. $inputs.=la_tag('br');
  449. }
  450. //street selector
  451. $inputs.= $this->streetInput();
  452. //build and apt inputs
  453. $baCells = la_TableCell($this->buildInput());
  454. $baCells.= la_TableCell($this->aptInput());
  455. $baRows = la_TableRow($baCells);
  456. $inputs.=la_TableBody($baRows, '', 0, '');
  457. //realname input
  458. $inputs.= $this->realnameInput();
  459. $inputs.= la_tag('br');
  460. //dirty magic here
  461. if ($this->optionSpamTraps) {
  462. $inputs.=$this->spambotsTrap();
  463. }
  464. //phone input
  465. $inputs.= $this->phoneInput();
  466. //email optional input
  467. if ($this->optionEmailDisplay) {
  468. $inputs.= $this->emailInput();
  469. $inputs.= la_tag('br');
  470. }
  471. //service combo selector
  472. if (!empty($this->services)) {
  473. $inputs.=$this->serviceInput();
  474. $inputs.= la_tag('br');
  475. }
  476. //optional tariffs selector
  477. if (!empty($this->tariffs)) {
  478. $inputs.=$this->tariffsInput();
  479. $inputs.= la_tag('br');
  480. }
  481. //notes text area
  482. $inputs.=$this->notesInput();
  483. $inputs.= la_tag('br');
  484. $inputs.=la_tag('small') . __('All fields marked with an asterisk (*) are required') . la_tag('small', true);
  485. $inputs.= la_tag('br');
  486. $inputs.= la_tag('br');
  487. $inputs.= la_Submit(__('Send signup request'));
  488. $result = la_tag('div', false, '', 'id="signup_form"');
  489. $result.= la_Form("", 'POST', $inputs, '');
  490. $result.= la_tag('div', true);
  491. return ($result);
  492. }
  493. /**
  494. * filters input data
  495. *
  496. * @param string $data data to filter
  497. *
  498. * @return string
  499. */
  500. protected function filter($data) {
  501. $data = trim($data);
  502. $data = strip_tags($data);
  503. $data = mysql_real_escape_string($data);
  504. return ($data);
  505. }
  506. /**
  507. * checks spam fields availability
  508. *
  509. * @return bool
  510. */
  511. protected function spamCheck() {
  512. $result = true;
  513. if ($this->optionSpamTraps) {
  514. foreach ($this->spamTraps as $eachTrap) {
  515. if (la_CheckPost(array($eachTrap))) {
  516. return (false);
  517. }
  518. }
  519. }
  520. return ($result);
  521. }
  522. /**
  523. * creates signup request in database
  524. *
  525. * @return bool
  526. */
  527. public function createRequest() {
  528. $date = date("Y-m-d H:i:s");
  529. $ip = $_SERVER['REMOTE_ADDR'];
  530. $state = 0;
  531. $result = true;
  532. if (la_CheckPost($this->required)) {
  533. //all of required fields filled
  534. $street = '';
  535. if (la_CheckPost(array('city'))) {
  536. $street.=$this->filter($_POST['city']) . ' ';
  537. }
  538. $street.=$this->filter($_POST['street']);
  539. $build = $this->filter($_POST['build']);
  540. if (la_CheckPost(array('apt'))) {
  541. $apt = $this->filter($_POST['apt']);
  542. } else {
  543. $apt = 0;
  544. }
  545. $realname = $this->filter($_POST['realname']);
  546. $phone = $this->filter($_POST['phone']);
  547. if (la_CheckPost(array('email'))) {
  548. $email = 'Email: ' . $this->filter($_POST['email']) . "\n";
  549. } else {
  550. $email = '';
  551. }
  552. if (la_CheckPost(array('service'))) {
  553. $service = $this->filter($_POST['service']);
  554. } else {
  555. $service = 'No';
  556. }
  557. if (la_CheckPost(array('tariff'))) {
  558. $tariff = 'Tariff: ' . $this->filter($_POST['tariff']) . "\n";
  559. } else {
  560. $tariff = '';
  561. }
  562. $notes = '';
  563. if (la_CheckPost(array('notes'))) {
  564. $notes.=$this->filter($_POST['notes']) . "\n";
  565. }
  566. $notes.=$tariff;
  567. $notes.=$email;
  568. $query = "INSERT INTO `sigreq` (
  569. `id` ,
  570. `date` ,
  571. `state` ,
  572. `ip` ,
  573. `street` ,
  574. `build` ,
  575. `apt` ,
  576. `realname` ,
  577. `phone` ,
  578. `service` ,
  579. `notes`
  580. )
  581. VALUES (
  582. NULL ,
  583. '" . $date . "',
  584. '" . $state . "',
  585. '" . $ip . "',
  586. '" . $street . "',
  587. '" . $build . "',
  588. '" . $apt . "',
  589. '" . $realname . "',
  590. '" . $phone . "',
  591. '" . $service . "',
  592. '" . $notes . "'
  593. );
  594. ";
  595. //silent spam check
  596. if ($this->spamCheck()) {
  597. nr_query($query);
  598. }
  599. } else {
  600. $result = false;
  601. }
  602. return ($result);
  603. }
  604. }
  605. ?>