api.migration.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. <?php
  2. /**
  3. * And again one more database interconnection abstraction class
  4. */
  5. class simpleOverlay {
  6. /**
  7. * Placeholder for DB driver.
  8. *
  9. * @var string
  10. */
  11. protected $databaseDriver = '';
  12. /**
  13. * DB link object.
  14. *
  15. * @var object
  16. */
  17. protected $databaseLink = '';
  18. public function __construct() {
  19. if (!extension_loaded('mysql')) {
  20. $this->databaseDriver = 'mysqli';
  21. } else {
  22. $this->databaseDriver = 'legacy';
  23. }
  24. }
  25. /**
  26. * Connect to MySQL using proper driver.
  27. *
  28. * @param string $db_host
  29. * @param string $db_user
  30. * @param string $db_pass
  31. * @param string $db_name
  32. *
  33. * @return object
  34. */
  35. public function connect($db_host, $db_user, $db_pass, $db_name) {
  36. if ($this->databaseDriver == 'mysqli') {
  37. $this->databaseLink = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
  38. if (!$this->databaseLink) {
  39. die("MySQL Connection error: " . mysqli_connect_error());
  40. }
  41. mysqli_set_charset($this->databaseLink, 'utf8');
  42. }
  43. if ($this->databaseDriver == 'legacy') {
  44. $this->databaseLink = mysql_connect($db_host, $db_user, $db_pass);
  45. if (!$this->databaseLink) {
  46. die("MySQL Connection error: " . mysql_error());
  47. }
  48. mysql_select_db($db_name);
  49. mysql_set_charset('utf8');
  50. }
  51. return $this->databaseLink;
  52. }
  53. /**
  54. * Close DB connection using proper driver.
  55. *
  56. * @param object $connection
  57. */
  58. public function close($connection) {
  59. if ($this->databaseDriver == 'mysqli') {
  60. mysqli_close($connection);
  61. }
  62. if ($this->databaseDriver == 'legacy') {
  63. mysql_close($connection);
  64. }
  65. }
  66. /**
  67. * Escated unwanted chars.
  68. *
  69. * @param string $string
  70. * @return string Escaped string
  71. */
  72. public function escapeString($string) {
  73. if ($this->databaseDriver == 'mysqli') {
  74. return mysqli_real_escape_string($this->databaseLink, $string);
  75. }
  76. if ($this->databaseDriver == 'legacy') {
  77. return mysql_real_escape_string($string);
  78. }
  79. }
  80. /**
  81. * Fetching data from DB.
  82. *
  83. * @param string $query
  84. * @return array
  85. */
  86. public function simple_queryall($query) {
  87. $result = array();
  88. if ($this->databaseDriver == 'mysqli') {
  89. $queried = mysqli_query($this->databaseLink, $query) or die('wrong data input: ' . $query);
  90. while ($row = mysqli_fetch_assoc($queried)) {
  91. $result[] = $row;
  92. }
  93. }
  94. if ($this->databaseDriver == 'legacy') {
  95. $queried = mysql_query($query) or die('wrong data input: ' . $query);
  96. while ($row = mysql_fetch_assoc($queried)) {
  97. $result[] = $row;
  98. }
  99. }
  100. return($result);
  101. }
  102. }
  103. /**
  104. * Mikbill migration class
  105. */
  106. class mikbill {
  107. /**
  108. * Placeholder for DB object.
  109. *
  110. * @var object
  111. */
  112. protected $dbLoader = '';
  113. /**
  114. * Placeholder for string that should be fixed or at least try to fix.
  115. *
  116. * @var string
  117. */
  118. protected $stringToFix = '';
  119. /**
  120. *
  121. * @var string
  122. */
  123. protected $stringFixed = '';
  124. /**
  125. *
  126. * @var array
  127. */
  128. protected $loginToFix = '';
  129. /**
  130. * Stores fixed login string.
  131. *
  132. * @var string
  133. */
  134. protected $fixedLogin = '';
  135. /**
  136. * Placeholder for avarice.
  137. *
  138. * @var object
  139. */
  140. protected $beggar = '';
  141. protected $usersData = array();
  142. protected $freezedData = array();
  143. protected $blockedData = array();
  144. protected $tariffsData = array();
  145. protected $cityData = array();
  146. protected $streetData = array();
  147. protected $housesData = array();
  148. protected $netsData = array();
  149. protected $loginPoint = '';
  150. protected $passwordPoint = '';
  151. protected $gridPoint = '';
  152. protected $ipPoint = '';
  153. protected $macPoint = '';
  154. protected $cashPoint = '';
  155. protected $downPoint = '';
  156. protected $realnamePoint = '';
  157. protected $tariffPoint = '';
  158. protected $speedPoint = '';
  159. protected $phonePoint = '';
  160. protected $mobilePoint = '';
  161. protected $addressPoint = '';
  162. public function __construct() {
  163. $this->greed = new Avarice();
  164. $this->beggar = $this->greed->runtime('MIKMIGR');
  165. $this->loginPoint = $this->beggar['INF']['login'];
  166. $this->passwordPoint = $this->beggar['INF']['password'];
  167. $this->gridPoint = $this->beggar['INF']['grid'];
  168. $this->ipPoint = $this->beggar['INF']['ip'];
  169. $this->macPoint = $this->beggar['INF']['mac'];
  170. $this->cashPoint = $this->beggar['INF']['cash'];
  171. $this->downPoint = $this->beggar['INF']['down'];
  172. $this->realnamePoint = $this->beggar['INF']['realname'];
  173. $this->tariffPoint = $this->beggar['INF']['tariff'];
  174. $this->speedPoint = $this->beggar['INF']['speed'];
  175. $this->phonePoint = $this->beggar['INF']['phone'];
  176. $this->mobilePoint = $this->beggar['INF']['mobile'];
  177. $this->addressPoint = $this->beggar['INF']['address'];
  178. $this->dbLoader = new simpleOverlay();
  179. ini_set('max_execution_time', 1800);
  180. }
  181. /**
  182. *
  183. * @param type $string
  184. */
  185. public function translit($string) {
  186. $result = zb_TranslitString($string);
  187. return (str_replace(array(' ', '*'), '_', $result));
  188. }
  189. /**
  190. * Fix string encoding when broken while convertion to utf8.
  191. */
  192. private function fixEncode($string) {
  193. $replace_array[9557] = 'і';
  194. $replace_array[9570] = 'Е';
  195. $replace_array[9572] = 'І';
  196. $replace_array[9555] = 'є';
  197. $replace_array[9558] = 'ї';
  198. $array = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
  199. foreach ($array as &$each) {
  200. $converted = $this->_uniord($each);
  201. if (isset($replace_array[$converted])) {
  202. $each = $replace_array[$converted];
  203. }
  204. }
  205. $string = implode("", $array);
  206. $result = strtr($string, $replace_array);
  207. return ($result);
  208. }
  209. protected function fixLogin() {
  210. $this->loginToFix = strtolower($this->loginToFix);
  211. $this->loginToFix = str_replace('-', '', $this->loginToFix);
  212. $this->fixedLogin = trim($this->loginToFix);
  213. }
  214. /**
  215. * Returns char's byte number.
  216. *
  217. * @param type $c
  218. * @return boolean|int
  219. */
  220. private function _uniord($c) {
  221. if (ord($c[0]) >= 0 && ord($c[0]) <= 127)
  222. return ord($c[0]);
  223. if (ord($c[0]) >= 192 && ord($c[0]) <= 223)
  224. return (ord($c[0]) - 192) * 64 + (ord($c[1]) - 128);
  225. if (ord($c[0]) >= 224 && ord($c[0]) <= 239)
  226. return (ord($c[0]) - 224) * 4096 + (ord($c[1]) - 128) * 64 + (ord($c[2]) - 128);
  227. if (ord($c[0]) >= 240 && ord($c[0]) <= 247)
  228. return (ord($c[0]) - 240) * 262144 + (ord($c[1]) - 128) * 4096 + (ord($c[2]) - 128) * 64 + (ord($c[3]) - 128);
  229. if (ord($c[0]) >= 248 && ord($c[0]) <= 251)
  230. return (ord($c[0]) - 248) * 16777216 + (ord($c[1]) - 128) * 262144 + (ord($c[2]) - 128) * 4096 + (ord($c[3]) - 128) * 64 + (ord($c[4]) - 128);
  231. if (ord($c[0]) >= 252 && ord($c[0]) <= 253)
  232. return (ord($c[0]) - 252) * 1073741824 + (ord($c[1]) - 128) * 16777216 + (ord($c[2]) - 128) * 262144 + (ord($c[3]) - 128) * 4096 + (ord($c[4]) - 128) * 64 + (ord($c[5]) - 128);
  233. if (ord($c[0]) >= 254 && ord($c[0]) <= 255) // error
  234. return FALSE;
  235. return 0;
  236. }
  237. public function web_MikbillMigrationNetworksForm() {
  238. $period = array('day' => __('day'), 'month' => __('month'));
  239. $inputs = wf_TextInput('db_user', __('Database user'), '', true, 20);
  240. $inputs .= wf_TextInput('db_pass', __('Database password'), '', true, 20);
  241. $inputs .= wf_TextInput('db_host', __('Database host'), '', true, 20);
  242. $inputs .= wf_TextInput('db_name', __('Database name'), 'mikbill', true, 20);
  243. $inputs .= wf_Selector('tariff_period', $period, __('Tariff period'), '', true);
  244. $inputs .= wf_CheckInput('login_as_pass', __('Use login as password'), true, false);
  245. $inputs .= wf_CheckInput('contract_as_uid', __('Use contract same as UID'), true, false);
  246. $inputs .= wf_delimiter();
  247. $inputs .= wf_Submit(__('Send'));
  248. $form = wf_Form("", 'POST', $inputs, 'glamour');
  249. return($form);
  250. }
  251. public function web_MikbillMigrationNetnumForm() {
  252. $inputs = wf_TextInput('netnum', __('networks number'), '', true, 20);
  253. $inputs .= wf_Submit(__('Save'));
  254. $form = wf_Form("", 'POST', $inputs, 'glamour');
  255. return($form);
  256. }
  257. protected function get_netid($user_arr, $your_networks) {
  258. $net_id = array();
  259. foreach ($user_arr as $each_user => $io) {
  260. $ip = $io[$this->beggar['INF']['ip']];
  261. $id = $io[$this->beggar['INF']['id']];
  262. $usr_split = explode(".", $ip);
  263. if (isset($usr_split[1])) {
  264. $ip = $usr_split[0] . '.' . $usr_split[1] . '.' . $usr_split[2];
  265. foreach ($your_networks as $each_net => $ia) {
  266. if ($ip == $ia['net']) {
  267. $net_id[$id] = $each_net + 1;
  268. }
  269. }
  270. }
  271. }
  272. return($net_id);
  273. }
  274. protected function get_lastcityid() {
  275. $query = "SELECT * FROM `city` ORDER BY `id` DESC LIMIT 1";
  276. $data = simple_query($query);
  277. $result = $data['id'];
  278. if (empty($result)) {
  279. return 1;
  280. }
  281. return $result;
  282. }
  283. protected function get_laststreetid() {
  284. $query = "SELECT * FROM `street` ORDER BY `id` DESC LIMIT 1";
  285. $data = simple_query($query);
  286. $result = $data['id'];
  287. if (empty($result)) {
  288. return 1;
  289. }
  290. return $result;
  291. }
  292. protected function get_lasthouseid() {
  293. $query = "SELECT * FROM `build` ORDER BY `id` DESC LIMIT 1";
  294. $data = simple_query($query);
  295. $result = $data['id'];
  296. if (empty($result)) {
  297. return 1;
  298. }
  299. return $result;
  300. }
  301. protected function get_aptid() {
  302. $query = "SELECT * FROM `apt` ORDER BY `id` DESC LIMIT 1";
  303. $data = simple_query($query);
  304. $result = $data['id'];
  305. if (empty($result)) {
  306. return 1;
  307. }
  308. return $result;
  309. }
  310. protected function get_aptnum($buildid) {
  311. $query = "SELECT * FROM `apt` WHERE buildid='" . $buildid . "'";
  312. $data = simple_query($query);
  313. $result = $data['apt'] + 1;
  314. if (empty($result)) {
  315. return 1;
  316. }
  317. return $result;
  318. }
  319. protected function cidr_match($ip, $network, $cidr) {
  320. if ((ip2int($ip) & ~((1 << (32 - $cidr)) - 1) ) == ip2int($network)) {
  321. return true;
  322. }
  323. return false;
  324. }
  325. protected function loadDbData($db_host, $db_user, $db_pass, $db_name) {
  326. $db_link = $this->dbLoader->connect($db_host, $db_user, $db_pass, $db_name);
  327. // sql queries to find needed data
  328. $users = $this->beggar['INF']['users'];
  329. $tariffs = $this->beggar['INF']['tariffs'];
  330. $freezed = 'SELECT * FROM `usersfreeze`';
  331. $blocked = 'SELECT * FROM `usersblok`';
  332. $city = "SELECT * FROM `lanes_settlements`";
  333. $street = "SELECT * FROM `lanes`";
  334. $houses = "SELECT * FROM `lanes_houses`";
  335. $nets = "(SELECT DISTINCT SUBSTRING_INDEX(`local_ip`,'.',3) AS `net` FROM `users`) UNION (SELECT DISTINCT SUBSTRING_INDEX(`framed_ip`,'.',3) AS `net` FROM `users`)";
  336. //sql data
  337. $this->usersData = $this->dbLoader->simple_queryall($users);
  338. $this->freezedData = $this->dbLoader->simple_queryall($freezed);
  339. $this->blockedData = $this->dbLoader->simple_queryall($blocked);
  340. $this->tariffsData = $this->dbLoader->simple_queryall($tariffs);
  341. $this->cityData = $this->dbLoader->simple_queryall($city);
  342. $this->streetData = $this->dbLoader->simple_queryall($street);
  343. $this->housesData = $this->dbLoader->simple_queryall($houses);
  344. $this->netsData = $this->dbLoader->simple_queryall($nets);
  345. $this->dbLoader->close($db_link);
  346. if (!($db_config = @parse_ini_file('config/' . 'mysql.ini'))) {
  347. print('Cannot load mysql configuration');
  348. exit;
  349. }
  350. $this->dbLoader->connect($db_config['server'], $db_config['username'], $db_config['password'], $db_config['db']);
  351. }
  352. protected function initIncrement($tablename = '') {
  353. $j = 0;
  354. if (!empty($tablename)) {
  355. $j = simple_get_lastid('nethosts');
  356. }
  357. if (empty($j)) {
  358. $j = 0;
  359. }
  360. return $j;
  361. }
  362. public function ConvertMikBill($db_user, $db_pass, $db_host, $db_name, $tariff_period, $login_as_pass, $contract_as_uid) {
  363. $this->loadDbData($db_host, $db_user, $db_pass, $db_name);
  364. //eval($beggar['INF']['text']);
  365. $user_arr = array();
  366. $i = 0;
  367. $new_city_data = array();
  368. $new_street_data = array();
  369. $new_house_data = array();
  370. $allIP = array();
  371. $duplicateIP = array();
  372. $net_counts = count($this->netsData);
  373. $j = $this->initIncrement('nethosts');
  374. foreach ($this->usersData as $eachuser => $io) {
  375. $this->loginToFix = $io[$this->beggar['DAT']['login']];
  376. $this->fixLogin();
  377. $user_arr[$this->fixedLogin][$this->loginPoint] = $this->fixedLogin; //0
  378. $user_arr[$this->fixedLogin][$this->passwordPoint] = $io[$this->beggar['DAT']['password']]; //1
  379. $user_arr[$this->fixedLogin][$this->gridPoint] = $io[$this->beggar['DAT']['grid']]; //2
  380. if ($io['real_ip']) {
  381. $eachIp = $io['framed_ip'];
  382. } else {
  383. $eachIp = $io['local_ip'];
  384. }
  385. $user_arr[$this->fixedLogin][$this->ipPoint] = $eachIp; //3
  386. $user_arr[$this->fixedLogin][$this->macPoint] = $io[$this->beggar['DAT']['mac']]; //4
  387. $user_arr[$this->fixedLogin][$this->cashPoint] = $io[$this->beggar['DAT']['cash']]; //5
  388. $user_arr[$this->fixedLogin][$this->downPoint] = $io[$this->beggar['DAT']['down']]; //6
  389. $user_arr[$this->fixedLogin][$this->realnamePoint] = $this->fixEncode($io[$this->beggar['DAT']['realname']]); //7
  390. foreach ($this->tariffsData as $eachtariff => $ia) {
  391. if ($io[$this->gridPoint] == $ia[$this->beggar['DAT']['grid']]) {
  392. $user_arr[$this->fixedLogin][$this->tariffPoint] = $ia[$this->beggar['DAT']['tariff']]; //8
  393. $user_arr[$this->fixedLogin][$this->speedPoint] = $ia[$this->beggar['DAT']['speed']]; //9
  394. }
  395. }
  396. $user_arr[$this->fixedLogin][$this->beggar['INF']['id']] = $this->beggar['UDATA'] + $j++; //10
  397. $user_arr[$this->fixedLogin][$this->phonePoint] = $io[$this->beggar['DAT']['phone']]; //11
  398. $user_arr[$this->fixedLogin][$this->mobilePoint] = $io[$this->beggar['DAT']['mobile']]; //12
  399. $user_arr[$this->fixedLogin][$this->addressPoint] = $this->fixEncode($io[$this->beggar['DAT']['address']]); //13
  400. $user_arr[$this->fixedLogin]['buildid'] = $io['houseid'];
  401. $user_arr[$this->fixedLogin]['aptnum'] = $io['app'];
  402. $user_arr[$this->fixedLogin]['note'] = $this->fixEncode($io['prim']);
  403. $user_arr[$this->fixedLogin]['credit'] = $io['credit'];
  404. $user_arr[$this->fixedLogin]['entrance'] = $io['porch'];
  405. $user_arr[$this->fixedLogin]['floor'] = $io['floor'];
  406. $user_arr[$this->fixedLogin]['freeze'] = 0;
  407. $user_arr[$this->fixedLogin]['uid'] = $io['uid'];
  408. $allIP[$eachIp] = $this->fixedLogin;
  409. }
  410. foreach ($this->blockedData as $eachuser => $io) {
  411. $this->stringToFix = $io[$this->beggar['DAT']['login']];
  412. $this->fixLogin();
  413. if ($io['real_ip']) {
  414. $eachIp = $io['framed_ip'];
  415. } else {
  416. $eachIp = $io['local_ip'];
  417. }
  418. if (!isset($allIP[$eachIp])) {
  419. $user_arr[$this->fixedLogin][$this->loginPoint] = $this->fixedLogin; //0
  420. $user_arr[$this->fixedLogin][$this->passwordPoint] = $io[$this->beggar['DAT']['password']]; //1
  421. $user_arr[$this->fixedLogin][$this->gridPoint] = $io[$this->beggar['DAT']['grid']]; //2
  422. $user_arr[$this->fixedLogin][$this->ipPoint] = $eachIp; //3
  423. $user_arr[$this->fixedLogin][$this->macPoint] = $io[$this->beggar['DAT']['mac']]; //4
  424. $user_arr[$this->fixedLogin][$this->cashPoint] = $io[$this->beggar['DAT']['cash']]; //5
  425. $user_arr[$this->fixedLogin][$this->downPoint] = 1; //6
  426. $user_arr[$this->fixedLogin][$this->realnamePoint] = $this->fixEncode($io[$this->beggar['DAT']['realname']]); //7
  427. foreach ($this->tariffsData as $eachtariff => $ia) {
  428. if ($io[$this->gridPoint] == $ia[$this->beggar['DAT']['grid']]) {
  429. $user_arr[$this->fixedLogin][$this->tariffPoint] = $ia[$this->beggar['DAT']['tariff']]; //8
  430. $user_arr[$this->fixedLogin][$this->speedPoint] = $ia[$this->beggar['DAT']['speed']]; //9
  431. }
  432. }
  433. $user_arr[$this->fixedLogin][$this->beggar['INF']['id']] = $this->beggar['UDATA'] + $j++; //10
  434. $user_arr[$this->fixedLogin][$this->phonePoint] = $io[$this->beggar['DAT']['phone']]; //11
  435. $user_arr[$this->fixedLogin][$this->mobilePoint] = $io[$this->beggar['DAT']['mobile']]; //12
  436. $user_arr[$this->fixedLogin][$this->addressPoint] = $this->fixEncode($io[$this->beggar['DAT']['address']]); //13
  437. $user_arr[$this->fixedLogin]['buildid'] = $io['houseid'];
  438. $user_arr[$this->fixedLogin]['aptnum'] = $io['app'];
  439. $user_arr[$this->fixedLogin]['note'] = $this->fixEncode($io['prim']);
  440. $user_arr[$this->fixedLogin]['credit'] = $io['credit'];
  441. $user_arr[$this->fixedLogin]['entrance'] = $io['porch'];
  442. $user_arr[$this->fixedLogin]['floor'] = $io['floor'];
  443. $user_arr[$this->fixedLogin]['freeze'] = 1;
  444. $user_arr[$this->fixedLogin]['uid'] = $io['uid'];
  445. $allIP[$eachIp] = $this->fixedLogin;
  446. } else {
  447. $duplicateIP[$this->fixedLogin] = $allIP[$eachIp];
  448. }
  449. }
  450. foreach ($this->freezedData as $eachuser => $io) {
  451. $this->stringToFix = $io[$this->beggar['DAT']['login']];
  452. $this->fixLogin();
  453. if ($io['real_ip']) {
  454. $eachIp = $io['framed_ip'];
  455. } else {
  456. $eachIp = $io['local_ip'];
  457. }
  458. if (!isset($allIP[$eachIp])) {
  459. $user_arr[$this->fixedLogin][$this->loginPoint] = $this->fixedLogin; //0
  460. $user_arr[$this->fixedLogin][$this->passwordPoint] = $io[$this->beggar['DAT']['password']]; //1
  461. $user_arr[$this->fixedLogin][$this->gridPoint] = $io[$this->beggar['DAT']['grid']]; //2
  462. $user_arr[$this->fixedLogin][$this->ipPoint] = $eachIp; //3
  463. $user_arr[$this->fixedLogin][$this->macPoint] = $io[$this->beggar['DAT']['mac']]; //4
  464. $user_arr[$this->fixedLogin][$this->cashPoint] = $io[$this->beggar['DAT']['cash']]; //5
  465. $user_arr[$this->fixedLogin][$this->downPoint] = $io[$this->beggar['DAT']['down']]; //6
  466. $user_arr[$this->fixedLogin][$this->realnamePoint] = $this->fixEncode($io[$this->beggar['DAT']['realname']]); //7
  467. foreach ($this->tariffsData as $eachtariff => $ia) {
  468. if ($io[$this->gridPoint] == $ia[$this->beggar['DAT']['grid']]) {
  469. $user_arr[$this->fixedLogin][$this->tariffPoint] = $ia[$this->beggar['DAT']['tariff']]; //8
  470. $user_arr[$this->fixedLogin][$this->speedPoint] = $ia[$this->beggar['DAT']['speed']]; //9
  471. }
  472. }
  473. $user_arr[$this->fixedLogin][$this->beggar['INF']['id']] = $this->beggar['UDATA'] + $j++; //10
  474. $user_arr[$this->fixedLogin][$this->phonePoint] = $io[$this->beggar['DAT']['phone']]; //11
  475. $user_arr[$this->fixedLogin][$this->mobilePoint] = $io[$this->beggar['DAT']['mobile']]; //12
  476. $user_arr[$this->fixedLogin][$this->addressPoint] = $this->fixEncode($io[$this->beggar['DAT']['address']]); //13
  477. $user_arr[$this->fixedLogin]['buildid'] = $io['houseid'];
  478. $user_arr[$this->fixedLogin]['aptnum'] = $io['app'];
  479. $user_arr[$this->fixedLogin]['note'] = $this->fixEncode($io['prim']);
  480. $user_arr[$this->fixedLogin]['credit'] = $io['credit'];
  481. $user_arr[$this->fixedLogin]['entrance'] = $io['porch'];
  482. $user_arr[$this->fixedLogin]['floor'] = $io['floor'];
  483. $user_arr[$this->fixedLogin]['freeze'] = 1;
  484. $user_arr[$this->fixedLogin]['uid'] = $io['uid'];
  485. $allIP[$eachIp] = $this->fixedLogin;
  486. } else {
  487. $duplicateIP[$this->fixedLogin] = $allIP[$eachIp];
  488. }
  489. }
  490. $val = array_keys($user_arr);
  491. $val = array_unique($val);
  492. $user_count = count($user_arr);
  493. //creating table users
  494. fpc_start($this->beggar['DUMP'], "users");
  495. foreach ($user_arr as $eachUser => $io) {
  496. $login = $io[$this->loginPoint];
  497. if ($login_as_pass) {
  498. $password = $io[$this->loginPoint];
  499. } else {
  500. $password = $io[$this->passwordPoint];
  501. }
  502. $ip = $io[$this->ipPoint];
  503. $cash = $io[$this->cashPoint];
  504. $down = $io[$this->downPoint];
  505. $tariff = $this->translit($io[$this->tariffPoint]);
  506. $credit = $io['credit'];
  507. $freeze = $io['freeze'];
  508. if ($i < ($user_count - 1)) {
  509. file_put_contents($this->beggar['DUMP'], "('" . $login . "','" . $password . "',$freeze,$down,1,1,'" . $tariff . "','','','','','',''," . $credit . ", '', '', '', '', '', '', '', '', '', '', '', 0, '" . $ip . "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $cash, 0, 0, 0, 86400, 1441152420, ''), ", FILE_APPEND);
  510. $i++;
  511. } else {
  512. file_put_contents($this->beggar['DUMP'], "('" . $login . "', '" . $password . "', $freeze, $down, 1, 1, '" . $tariff . "', '', '', '', '', '', '', " . $credit . ", '', '', '', '', '', '', '', '', '', '', '', 0, '" . $ip . "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $cash, 0, 0, 0, 86400, 1441152420, '');\n", FILE_APPEND);
  513. }
  514. }
  515. fpc_end($this->beggar['DUMP'], "users");
  516. //creating table tariffs
  517. $tariffs_count = count($this->tariffsData);
  518. $i = $this->initIncrement();
  519. fpc_start($this->beggar['DUMP'], "tariffs");
  520. foreach ($this->tariffsData as $eachtariff => $io) {
  521. $tariff_name = $this->translit($io['packet']);
  522. $fee = $io['fixed_cost'];
  523. if ($i < ($tariffs_count - 1)) {
  524. file_put_contents($this->beggar['DUMP'], "('" . $tariff_name . "', 0, 0, 0, 0, 0, '0:0-0:0', 1, 1, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, $fee, 0, 'up+down', '" . $tariff_period . "'),\n", FILE_APPEND);
  525. $i++;
  526. } else {
  527. file_put_contents($this->beggar['DUMP'], "('" . $tariff_name . "', 0, 0, 0, 0, 0, '0:0-0:0', 1, 1, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, 0, 0, 0, 0, '0:0-0:0', 0, 0, 0, $fee, 0, 'up+down', '" . $tariff_period . "');\n", FILE_APPEND);
  528. }
  529. }
  530. fpc_end($this->beggar['DUMP'], "tariffs");
  531. //create table contracts
  532. $i = $this->initIncrement();
  533. fpc_start($this->beggar['DUMP'], "contracts");
  534. foreach ($user_arr as $eachUser => $io) {
  535. $login = $io[$this->loginPoint];
  536. if ($contract_as_uid) {
  537. $contract = $io['uid'];
  538. } else {
  539. $contract = $login;
  540. }
  541. $id = $io[$this->beggar['INF']['id']];
  542. if ($i < ($user_count - 1)) {
  543. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $contract . "'),\n", FILE_APPEND);
  544. $i++;
  545. } else {
  546. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $contract . "');\n", FILE_APPEND);
  547. }
  548. }
  549. fpc_end($this->beggar['DUMP'], "contracts");
  550. //create table networks
  551. $i = $this->initIncrement();
  552. $j = $this->initIncrement();
  553. fpc_start($this->beggar['DUMP'], "networks");
  554. //foreach ($your_networks as $each_net => $io) {
  555. foreach ($this->netsData as $each_net => $io) {
  556. $start_ip = $io['net'] . '.0';
  557. $last_ip = $io['net'] . '.254';
  558. $net = $io['net'] . '.0/24';
  559. $net_type = 'dhcpstatic';
  560. $radius = 0;
  561. $j += $this->beggar['UDATA'];
  562. if ($i < ($net_counts - 1)) {
  563. file_put_contents($this->beggar['DUMP'], "($j, '" . $start_ip . "', '" . $last_ip . "', '" . $net . "', '" . $net_type . "', $radius),\n", FILE_APPEND);
  564. $i++;
  565. } else {
  566. file_put_contents($this->beggar['DUMP'], "($j, '" . $start_ip . "', '" . $last_ip . "', '" . $net . "', '" . $net_type . "', $radius);\n", FILE_APPEND);
  567. }
  568. }
  569. fpc_end($this->beggar['DUMP'], "networks");
  570. //create table nethosts
  571. $i = $this->initIncrement();
  572. $net_id = $this->get_netid($user_arr, $this->netsData);
  573. fpc_start($this->beggar['DUMP'], "nethosts");
  574. foreach ($user_arr as $each_user => $io) {
  575. $login = $io[$this->loginPoint];
  576. $ip = $io[$this->ipPoint];
  577. $mac = strtolower($io[$this->macPoint]);
  578. $id = $io[$this->beggar['INF']['id']];
  579. if ($i < ($user_count - 1)) {
  580. if (!isset($net_id[$id])) {
  581. //echo $login . '<br />';
  582. } else {
  583. file_put_contents($this->beggar['DUMP'], "($id, $net_id[$id], '" . $ip . "', '" . $mac . "', 'NULL'),\n", FILE_APPEND);
  584. }
  585. $i++;
  586. } else {
  587. if (!isset($net_id[$id])) {
  588. $net_id[$id] = 1;
  589. }
  590. file_put_contents($this->beggar['DUMP'], "($id, $net_id[$id], '" . $ip . "', '" . $mac . "', 'NULL'); \n", FILE_APPEND);
  591. }
  592. }
  593. fpc_end($this->beggar['DUMP'], "nethosts");
  594. //create table phones
  595. $i = $this->initIncrement();
  596. fpc_start($this->beggar['DUMP'], "phones");
  597. foreach ($user_arr as $each_user => $io) {
  598. $login = $io[$this->loginPoint];
  599. $id = $io[$this->beggar['INF']['id']];
  600. $phone = $io[$this->phonePoint];
  601. $mobile = $io[$this->mobilePoint];
  602. if ($i < ($user_count - 1)) {
  603. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $phone . "', '" . $mobile . "'),\n", FILE_APPEND);
  604. $i++;
  605. } else {
  606. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $phone . "', '" . $mobile . "');\n", FILE_APPEND);
  607. }
  608. }
  609. fpc_end($this->beggar['DUMP'], "phones");
  610. //create table services
  611. $i = $this->initIncrement();
  612. fpc_start($this->beggar['DUMP'], "services");
  613. foreach ($this->netsData as $each_net => $io) {
  614. $t_net_id = $each_net + 1;
  615. if ($i < ($net_counts - 1)) {
  616. file_put_contents($this->beggar['DUMP'], "($t_net_id, $t_net_id, '" . $t_net_id . "'),\n ", FILE_APPEND);
  617. $i++;
  618. } else {
  619. file_put_contents($this->beggar['DUMP'], "($t_net_id, $t_net_id, '" . $t_net_id . "');\n", FILE_APPEND);
  620. }
  621. }
  622. fpc_end($this->beggar['DUMP'], "services");
  623. //create table realname
  624. $i = $this->initIncrement();
  625. fpc_start($this->beggar['DUMP'], "realname");
  626. foreach ($user_arr as $each_user => $io) {
  627. $login = $io[$this->loginPoint];
  628. $id = $io[$this->beggar['INF']['id']];
  629. $search[] = "'";
  630. $search[] = "\\";
  631. $search[] = "/";
  632. $fio = str_replace($search, '', $io[$this->realnamePoint]);
  633. if ($i < ($user_count - 1)) {
  634. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $fio . "'), ", FILE_APPEND);
  635. $i++;
  636. } else {
  637. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', '" . $fio . "'); \n", FILE_APPEND);
  638. }
  639. }
  640. fpc_end($this->beggar['DUMP'], "realname");
  641. //create table speeds
  642. $i = $this->initIncrement();
  643. fpc_start($this->beggar['DUMP'], "speeds");
  644. foreach ($this->tariffsData as $eachtariff => $io) {
  645. $tariff_name = $this->translit($io['packet']);
  646. $tariff_speed = $io['speed_rate'];
  647. if ($i < ($tariffs_count - 1)) {
  648. file_put_contents($this->beggar['DUMP'], "(NULL, '" . $tariff_name . "', '" . $tariff_speed . "', '" . $tariff_speed . "', NULL, NULL, NULL, NULL), \n", FILE_APPEND);
  649. $i++;
  650. } else {
  651. file_put_contents($this->beggar['DUMP'], "(NULL, '" . $tariff_name . "', '" . $tariff_speed . "', '" . $tariff_speed . "', NULL, NULL, NULL, NULL); \n", FILE_APPEND);
  652. }
  653. }
  654. fpc_end($this->beggar['DUMP'], "speeds");
  655. //create table userspeeds
  656. $i = $this->initIncrement();
  657. fpc_start($this->beggar['DUMP'], "userspeeds");
  658. foreach ($user_arr as $each_user => $io) {
  659. $login = $io[$this->loginPoint];
  660. $id = $io[$this->beggar['INF']['id']];
  661. if ($i < ($user_count - 1)) {
  662. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', 0),\n", FILE_APPEND);
  663. $i++;
  664. } else {
  665. file_put_contents($this->beggar['DUMP'], "($id, '" . $login . "', 0);\n", FILE_APPEND);
  666. }
  667. }
  668. fpc_end($this->beggar['DUMP'], "userspeeds");
  669. //create table notes for addresses
  670. $i = $this->initIncrement();
  671. $j = $this->initIncrement('nethosts');
  672. if (empty($j)) {
  673. $j = 0;
  674. }
  675. fpc_start($this->beggar['DUMP'], "notes");
  676. foreach ($user_arr as $each_user => $io) {
  677. $login = $io[$this->loginPoint];
  678. $note = $this->dbLoader->escapeString($io['note']);
  679. $j += $this->beggar['UDATA'];
  680. if ($i < ($user_count - 1)) {
  681. file_put_contents($this->beggar['DUMP'], "($j, '" . $login . "', '" . $note . "'),\n", FILE_APPEND);
  682. $i++;
  683. } else {
  684. file_put_contents($this->beggar['DUMP'], "($j, '" . $login . "', '" . $note . "');\n", FILE_APPEND);
  685. }
  686. }
  687. fpc_end($this->beggar['DUMP'], "notes");
  688. //city and address section
  689. $i = $this->initIncrement();
  690. $j = $this->get_lastcityid();
  691. $city_count = count($this->cityData);
  692. fpc_start($this->beggar['DUMP'], "city");
  693. foreach ($this->cityData as $index => $eachCity) {
  694. $city_name = $this->fixEncode($eachCity['settlementname']);
  695. $j += $this->beggar['UDATA'];
  696. $new_city_data[$eachCity['settlementid']] = $j;
  697. if ($i < ($city_count - 1)) {
  698. file_put_contents($this->beggar['DUMP'], "($j, '" . $city_name . "', ''),\n", FILE_APPEND);
  699. $i++;
  700. } else {
  701. file_put_contents($this->beggar['DUMP'], "($j, '" . $city_name . "', '');\n", FILE_APPEND);
  702. }
  703. }
  704. fpc_end($this->beggar['DUMP'], "city");
  705. $i = $this->initIncrement();
  706. $j = $this->get_laststreetid();
  707. $street_count = count($this->streetData);
  708. fpc_start($this->beggar['DUMP'], "street");
  709. foreach ($this->streetData as $index => $eachStreet) {
  710. $street_name = $this->fixEncode(str_replace($search, '', $eachStreet['lane']));
  711. $settlementid = $eachStreet['settlementid'];
  712. $city_id = $new_city_data[$settlementid];
  713. $j += $this->beggar['UDATA'];
  714. $new_street_data[$eachStreet['laneid']] = $j;
  715. if ($i < ($street_count - 1)) {
  716. file_put_contents($this->beggar['DUMP'], "($j, $city_id, '" . $street_name . "', ''),\n", FILE_APPEND);
  717. $i++;
  718. } else {
  719. file_put_contents($this->beggar['DUMP'], "($j, $city_id, '" . $street_name . "', '');\n", FILE_APPEND);
  720. }
  721. }
  722. fpc_end($this->beggar['DUMP'], "street");
  723. $i = $this->initIncrement();
  724. $j = $this->get_lasthouseid();
  725. $house_count = count($this->housesData);
  726. fpc_start($this->beggar['DUMP'], "build");
  727. foreach ($this->housesData as $index => $eachHouse) {
  728. $build_num = $eachHouse['house'];
  729. $street_id = $new_street_data[$eachHouse['laneid']];
  730. $j += $this->beggar['UDATA'];
  731. $new_house_data[$eachHouse['houseid']] = $j;
  732. if ($i < ($house_count - 1)) {
  733. file_put_contents($this->beggar['DUMP'], "( $j, $street_id, '" . $build_num . "', NULL),\n", FILE_APPEND);
  734. $i++;
  735. } else {
  736. file_put_contents($this->beggar['DUMP'], "($j, $street_id, '" . $build_num . "', NULL);\n", FILE_APPEND);
  737. }
  738. }
  739. fpc_end($this->beggar['DUMP'], "build");
  740. $i = $this->initIncrement();
  741. $j = $this->get_aptid();
  742. fpc_start($this->beggar['DUMP'], "apt");
  743. foreach ($user_arr as $each_user => $io) {
  744. $build_id = str_replace($search, '', $new_house_data[$io['buildid']]);
  745. $j += $this->beggar['UDATA'];
  746. $addr[$io[$this->loginPoint]] = $j;
  747. if (empty($io['aptnum'])) {
  748. $io['aptnum'] = 0;
  749. }
  750. if ($i < ($user_count - 1)) {
  751. file_put_contents($this->beggar['DUMP'], "($j, $build_id, '" . $io['entrance'] . "', '" . $io['floor'] . "', '" . $io['aptnum'] . "'),\n", FILE_APPEND);
  752. $i++;
  753. } else {
  754. file_put_contents($this->beggar['DUMP'], "($j, $build_id, '" . $io['entrance'] . "', '" . $io['floor'] . "', '" . $io['aptnum'] . "');\n", FILE_APPEND);
  755. }
  756. }
  757. fpc_end($this->beggar['DUMP'], "apt");
  758. $i = $this->initIncrement();
  759. fpc_start($this->beggar['DUMP'], "address");
  760. foreach ($user_arr as $each_user => $io) {
  761. $j += $this->beggar['UDATA'];
  762. if ($i < ($user_count - 1)) {
  763. file_put_contents($this->beggar['DUMP'], "(NULL, '" . $io[$this->loginPoint] . "', " . $addr[$io[$this->loginPoint]] . "),\n", FILE_APPEND);
  764. $i++;
  765. } else {
  766. file_put_contents($this->beggar['DUMP'], "(NULL, '" . $io[$this->loginPoint] . "', " . $addr[$io[$this->loginPoint]] . ");\n", FILE_APPEND);
  767. }
  768. }
  769. fpc_end($this->beggar['DUMP'], "address");
  770. return $duplicateIP;
  771. }
  772. }