123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- <?php
- class SignupService {
- //settings
- protected $configRaw = array();
- protected $optionCitySelectable = true;
- protected $optionStreetSelectable = true;
- protected $optionCityDisplay = true;
- protected $optionEmailDisplay = true;
- protected $optionSpamTraps = true;
- protected $optionCaching = false;
- protected $optionConfCaching=false;
- protected $optionServices = '';
- protected $optionTariffs = '';
- protected $optionIspName = '';
- protected $optionIspUrl = '';
- protected $optionIspLogo = '';
- protected $optionSidebarText = '';
- protected $optionGreetingText = '';
- protected $optionHideouts = '';
- protected $cachingTime=3600;
- //caching
- const CACHE_PATH = 'cache/';
- protected $cachigTime = 3600;
- //other properties
- protected $cities = array();
- protected $streets = array();
- protected $services = array();
- protected $tariffs = array();
- protected $hideouts = array();
- protected $spamTraps = array('surname', 'lastname', 'seenoevil', 'mobile');
- protected $required = array('street', 'build', 'realname', 'phone');
- protected $important = '';
- public function __construct($confcache=0,$cachetimeout=3600) {
- if ($confcache) {
- $this->optionConfCaching=true;
- }
- $this->cachingTime=$cachetimeout;
- $this->important = ' ' . la_tag('sup') . '*' . la_tag('sup', true);
- $this->loadConfig();
- $this->configPreprocess();
- $this->loadServices();
- $this->loadTariffs();
- $this->loadHideouts();
- $this->setTemplateData();
- }
- /**
- * Loads sigreqconf config from database
- *
- * @return void
- */
- protected function loadConfig() {
- if ($this->optionConfCaching) {
- $cacheTime = $this->cachingTime;
- $cacheTime = time() - $cacheTime;
- $cacheName = self::CACHE_PATH . 'config.dat';
- $updateCache = false;
- if (file_exists($cacheName)) {
- $updateCache = false;
- if ((filemtime($cacheName) > $cacheTime)) {
- $updateCache = false;
- } else {
- $updateCache = true;
- }
- } else {
- $updateCache = true;
- }
- if (!$updateCache) {
- //read data directly from cache
- $result = array();
- $rawData = file_get_contents($cacheName);
- if (!empty($rawData)) {
- $rawData= base64_decode($rawData);
- $result = unserialize($rawData);
-
- }
- $this->configRaw = $result;
- } else {
- //updating cache
- $query = "SELECT * from `sigreqconf`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->configRaw[$each['key']] = $each['value'];
- }
- }
- $cacheStoreData = serialize($this->configRaw);
- $cacheStoreData = base64_encode($cacheStoreData);
- file_put_contents($cacheName, $cacheStoreData);
- }
- } else {
- $query = "SELECT * from `sigreqconf`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->configRaw[$each['key']] = $each['value'];
- }
- }
- }
- }
- /**
- * preprocessing of raw config into private triggers
- *
- * @return void
- */
- protected function configPreprocess() {
- //preprocess data
- $this->optionIspName = (isset($this->configRaw['ISP_NAME'])) ? $this->configRaw['ISP_NAME'] : '';
- $this->optionIspUrl = (isset($this->configRaw['ISP_URL'])) ? $this->configRaw['ISP_URL'] : '';
- $this->optionIspLogo = (isset($this->configRaw['ISP_LOGO'])) ? $this->configRaw['ISP_LOGO'] : '';
- $this->optionSidebarText = (isset($this->configRaw['SIDEBAR_TEXT'])) ? $this->configRaw['SIDEBAR_TEXT'] : '';
- $this->optionGreetingText = (isset($this->configRaw['GREETING_TEXT'])) ? $this->configRaw['GREETING_TEXT'] : '';
- $this->optionServices = (isset($this->configRaw['SERVICES'])) ? $this->configRaw['SERVICES'] : '';
- $this->optionTariffs = (isset($this->configRaw['TARIFFS'])) ? $this->configRaw['TARIFFS'] : '';
- $this->optionHideouts = (isset($this->configRaw['HIDEOUTS'])) ? $this->configRaw['HIDEOUTS'] : '';
- $this->optionCitySelectable = (isset($this->configRaw['CITY_SELECTABLE'])) ? true : false;
- $this->optionCityDisplay = (isset($this->configRaw['CITY_DISPLAY'])) ? true : false;
- $this->optionStreetSelectable = (isset($this->configRaw['STREET_SELECTABLE'])) ? true : false;
- $this->optionEmailDisplay = (isset($this->configRaw['EMAIL_DISPLAY'])) ? true : false;
- $this->optionSpamTraps = (isset($this->configRaw['SPAM_TRAPS'])) ? true : false;
- $this->optionCaching = (isset($this->configRaw['CACHING'])) ? true : false;
- }
- /**
- * sets ISP name and others propertys to external scope
- *
- * @return void
- */
- public function setTemplateData() {
- global $templateData;
- $templateData['ISP_NAME'] = $this->optionIspName;
- $templateData['ISP_URL'] = $this->optionIspUrl;
- $templateData['ISP_LOGO'] = $this->optionIspLogo;
- if ((!empty($this->optionIspName)) AND ( !empty($this->optionIspUrl)) AND ( !empty($this->optionIspLogo))) {
- $templateData['ISP_LINK'] = la_Link($this->optionIspUrl, la_img($this->optionIspLogo, $this->optionIspName), false);
- } else {
- $templateData['ISP_LINK'] = '';
- }
- $templateData['SIDEBAR_TEXT'] = $this->optionSidebarText;
- $templateData['GREETING_TEXT'] = $this->optionGreetingText;
- }
- /**
- * loads cities from database into private data property
- *
- * @return void
- */
- protected function loadCities() {
- if ($this->optionCaching) {
- $cacheTime = $this->cachingTime;
- $cacheTime = time() - $cacheTime;
- $cacheName = self::CACHE_PATH . 'city.dat';
- $updateCache = false;
- if (file_exists($cacheName)) {
- $updateCache = false;
- if ((filemtime($cacheName) > $cacheTime)) {
- $updateCache = false;
- } else {
- $updateCache = true;
- }
- } else {
- $updateCache = true;
- }
- if (!$updateCache) {
- //read data directly from cache
- $result = array();
- $rawData = file_get_contents($cacheName);
- if (!empty($rawData)) {
- $result = unserialize($rawData);
- }
- $this->cities = $result;
- } else {
- //updating cache
- $query = "SELECT * from `city` ORDER BY `id` ASC";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->cities[$each['id']] = $each['cityname'];
- }
- }
- $cacheStoreData = serialize($this->cities);
- file_put_contents($cacheName, $cacheStoreData);
- }
- } else {
- $query = "SELECT * from `city` ORDER BY `id` ASC";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->cities[$each['id']] = $each['cityname'];
- }
- }
- }
- }
- /**
- * prepares services for service selector inputs
- *
- * @return void
- */
- protected function loadServices() {
- if (!empty($this->optionServices)) {
- $tmpArr = explode(',', $this->optionServices);
- if (!empty($tmpArr)) {
- foreach ($tmpArr as $io => $each) {
- $this->services[trim($each)] = trim($each);
- }
- }
- }
- }
- /**
- * prepares tariffs if available, for tariffs selector inputs
- *
- * @return void
- */
- protected function loadTariffs() {
- if (!empty($this->optionTariffs)) {
- $tmpArr = explode(',', $this->optionTariffs);
- if (!empty($tmpArr)) {
- foreach ($tmpArr as $io => $each) {
- $this->tariffs[trim($each)] = trim($each);
- }
- }
- }
- }
- /**
- * prepares hideouts if available, for excluding in city and streets lists
- *
- * @return void
- */
- protected function loadHideouts() {
- if (!empty($this->optionHideouts)) {
- $this->hideouts = explode(',', $this->optionHideouts);
- }
- }
- /**
- * loads streets from database into private data property
- *
- * @return void
- */
- protected function loadStreets() {
- if ($this->optionCaching) {
- $cacheTime = $this->cachingTime;
- $cacheTime = time() - $cacheTime;
- $cacheName = self::CACHE_PATH . 'street.dat';
- $updateCache = false;
- if (file_exists($cacheName)) {
- $updateCache = false;
- if ((filemtime($cacheName) > $cacheTime)) {
- $updateCache = false;
- } else {
- $updateCache = true;
- }
- } else {
- $updateCache = true;
- }
- if (!$updateCache) {
- //read data directly from cache
- $result = array();
- $rawData = file_get_contents($cacheName);
- if (!empty($rawData)) {
- $result = unserialize($rawData);
- }
- $this->streets = $result;
- } else {
- //updating cache
- $query = "SELECT * from `street`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->streets[$each['id']] = $each['streetname'];
- }
- }
- $cacheStoreData = serialize($this->streets);
- file_put_contents($cacheName, $cacheStoreData);
- }
- } else {
- //cache disabled
- $query = "SELECT * from `street`";
- $all = simple_queryall($query);
- if (!empty($all)) {
- foreach ($all as $io => $each) {
- $this->streets[$each['id']] = $each['streetname'];
- }
- }
- }
- }
- /**
- * returns city input depends selectable option
- *
- * @return string
- */
- protected function cityInput() {
- $result = '';
- if ($this->optionCitySelectable) {
- $this->loadCities();
- if (!empty($this->cities)) {
- $cityNames = array();
- foreach ($this->cities as $io => $each) {
- $cityNames[$each] = $each;
- }
- //hideouts processing
- if (!empty($this->hideouts)) {
- foreach ($this->hideouts as $ia => $hideout) {
- if (isset($cityNames[$hideout])) {
- unset($cityNames[$hideout]);
- }
- }
- }
- $result = la_JuiComboBox('city', $cityNames, __('Town') . $this->important, '', false);
- }
- } else {
- $result = la_TextInput('city', __('Town') . $this->important, '', false, 15);
- }
- return ($result);
- }
- /**
- * returns street input depends options
- *
- * @return string
- */
- protected function streetInput() {
- $result = '';
- if ($this->optionStreetSelectable) {
- $this->loadStreets();
- if (!empty($this->streets)) {
- $streetNames = array();
- foreach ($this->streets as $io => $each) {
- $streetNames[$each] = $each;
- }
- if (!empty($streetNames)) {
- natsort($streetNames);
- }
- $sortedStreets = array('' => __('Select one'));
- $sortedStreets = array_merge($sortedStreets, $streetNames);
- //hideouts processing
- if (!empty($this->hideouts)) {
- foreach ($this->hideouts as $ia => $hideout) {
- if (isset($sortedStreets[$hideout])) {
- unset($sortedStreets[$hideout]);
- }
- }
- }
- $result = la_JuiComboBox('street', $sortedStreets, __('Street') . $this->important, '', false);
- }
- } else {
- $result = la_TextInput('street', __('Street') . $this->important, '', false, 25);
- }
- return ($result);
- }
- /**
- * returns build input
- *
- * @return string
- */
- protected function buildInput() {
- $result = la_TextInput('build', __('Build') . $this->important, '', false, '5');
- return ($result);
- }
- /**
- * returns apartment input
- *
- * @return string
- */
- protected function aptInput() {
- $result = la_TextInput('apt', __('Apartment') . la_tag('sup') . ' ' . la_tag('sup', true), '', false, '5'); //vertical align ugly hack
- return ($result);
- }
- /**
- * returns realname input
- *
- * @return string
- */
- protected function realnameInput() {
- $result = la_TextInput('realname', __('Real name') . $this->important, '', false, '25');
- return ($result);
- }
- /**
- * returns phone number input
- *
- * @return string
- */
- protected function phoneInput() {
- $result = la_TextInput('phone', __('Phone') . $this->important, '', false, '25');
- return ($result);
- }
- /**
- * returns phone number input
- *
- * @return string
- */
- protected function emailInput() {
- $result = la_TextInput('email', __('Email'), '', false, '25');
- return ($result);
- }
- /**
- * returns services select input
- *
- * @return string
- */
- protected function serviceInput() {
- $result = '';
- if (!empty($this->services)) {
- $result = la_JuiComboBox('service', $this->services, __('Service') . $this->important, '', false);
- }
- return ($result);
- }
- /**
- * returns tariffs select input
- *
- * @return string
- */
- protected function tariffsInput() {
- $result = '';
- if (!empty($this->tariffs)) {
- $result = la_JuiComboBox('tariff', $this->tariffs, __('Tariff'), '', false);
- }
- return ($result);
- }
- /**
- * anti spam bots dirty magic inputs ;)
- *
- * @rerutn string
- */
- protected function spambotsTrap() {
- $result = la_tag('input', false, 'somemagic', 'type="text" name="surname"');
- $result.= la_tag('input', false, '', 'type="text" name="lastname" style="display:none;"');
- $result.= la_tag('input', false, 'somemagic', 'type="text" name="seenoevil"');
- $result.= la_tag('input', false, 'somemagic', 'type="text" name="mobile"');
- return ($result);
- }
- /**
- * returns signup notes input
- *
- * @return string
- */
- protected function notesInput() {
- $result = la_TextArea('notes', __('Notes'), '', false, '50x5');
- return ($result);
- }
- /**
- * returns signup service main form
- *
- * @retun string
- */
- public function renderForm() {
- $inputs = '';
- $inputs.=la_HiddenInput('createrequest', 'true');
- //greeting text
- $inputs.=$this->optionGreetingText;
- //optional city selector
- if ($this->optionCityDisplay) {
- $inputs.=$this->cityInput();
- $inputs.=la_tag('br');
- }
- //street selector
- $inputs.= $this->streetInput();
- //build and apt inputs
- $baCells = la_TableCell($this->buildInput());
- $baCells.= la_TableCell($this->aptInput());
- $baRows = la_TableRow($baCells);
- $inputs.=la_TableBody($baRows, '', 0, '');
- //realname input
- $inputs.= $this->realnameInput();
- $inputs.= la_tag('br');
- //dirty magic here
- if ($this->optionSpamTraps) {
- $inputs.=$this->spambotsTrap();
- }
- //phone input
- $inputs.= $this->phoneInput();
- //email optional input
- if ($this->optionEmailDisplay) {
- $inputs.= $this->emailInput();
- $inputs.= la_tag('br');
- }
- //service combo selector
- if (!empty($this->services)) {
- $inputs.=$this->serviceInput();
- $inputs.= la_tag('br');
- }
- //optional tariffs selector
- if (!empty($this->tariffs)) {
- $inputs.=$this->tariffsInput();
- $inputs.= la_tag('br');
- }
- //notes text area
- $inputs.=$this->notesInput();
- $inputs.= la_tag('br');
- $inputs.=la_tag('small') . __('All fields marked with an asterisk (*) are required') . la_tag('small', true);
- $inputs.= la_tag('br');
- $inputs.= la_tag('br');
- $inputs.= la_Submit(__('Send signup request'));
- $result = la_tag('div', false, '', 'id="signup_form"');
- $result.= la_Form("", 'POST', $inputs, '');
- $result.= la_tag('div', true);
- return ($result);
- }
- /**
- * filters input data
- *
- * @param string $data data to filter
- *
- * @return string
- */
- protected function filter($data) {
- $data = trim($data);
- $data = strip_tags($data);
- $data = mysql_real_escape_string($data);
- return ($data);
- }
- /**
- * checks spam fields availability
- *
- * @return bool
- */
- protected function spamCheck() {
- $result = true;
- if ($this->optionSpamTraps) {
- foreach ($this->spamTraps as $eachTrap) {
- if (la_CheckPost(array($eachTrap))) {
- return (false);
- }
- }
- }
- return ($result);
- }
- /**
- * creates signup request in database
- *
- * @return bool
- */
- public function createRequest() {
- $date = date("Y-m-d H:i:s");
- $ip = $_SERVER['REMOTE_ADDR'];
- $state = 0;
- $result = true;
- if (la_CheckPost($this->required)) {
- //all of required fields filled
- $street = '';
- if (la_CheckPost(array('city'))) {
- $street.=$this->filter($_POST['city']) . ' ';
- }
- $street.=$this->filter($_POST['street']);
- $build = $this->filter($_POST['build']);
- if (la_CheckPost(array('apt'))) {
- $apt = $this->filter($_POST['apt']);
- } else {
- $apt = 0;
- }
- $realname = $this->filter($_POST['realname']);
- $phone = $this->filter($_POST['phone']);
- if (la_CheckPost(array('email'))) {
- $email = 'Email: ' . $this->filter($_POST['email']) . "\n";
- } else {
- $email = '';
- }
- if (la_CheckPost(array('service'))) {
- $service = $this->filter($_POST['service']);
- } else {
- $service = 'No';
- }
- if (la_CheckPost(array('tariff'))) {
- $tariff = 'Tariff: ' . $this->filter($_POST['tariff']) . "\n";
- } else {
- $tariff = '';
- }
- $notes = '';
- if (la_CheckPost(array('notes'))) {
- $notes.=$this->filter($_POST['notes']) . "\n";
- }
- $notes.=$tariff;
- $notes.=$email;
- $query = "INSERT INTO `sigreq` (
- `id` ,
- `date` ,
- `state` ,
- `ip` ,
- `street` ,
- `build` ,
- `apt` ,
- `realname` ,
- `phone` ,
- `service` ,
- `notes`
- )
- VALUES (
- NULL ,
- '" . $date . "',
- '" . $state . "',
- '" . $ip . "',
- '" . $street . "',
- '" . $build . "',
- '" . $apt . "',
- '" . $realname . "',
- '" . $phone . "',
- '" . $service . "',
- '" . $notes . "'
- );
- ";
- //silent spam check
- if ($this->spamCheck()) {
- nr_query($query);
- }
- } else {
- $result = false;
- }
- return ($result);
- }
- }
- ?>
|