index.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. if (cfr('ROOT')) {
  3. set_time_limit (0);
  4. function web_MigrationUploadForm() {
  5. $delimiters=array(
  6. ';'=>';',
  7. '|'=>'|',
  8. ','=>','
  9. );
  10. $encodings=array(
  11. 'utf-8'=>'utf-8',
  12. 'windows-1251'=>'windows-1251',
  13. 'koi8-u'=>'koi8-u',
  14. 'cp866'=>'cp866'
  15. );
  16. $uploadinputs=wf_HiddenInput('uploaduserbase','true');
  17. $uploadinputs.=__('Upload userbase').' <input id="fileselector" type="file" name="uluserbase" size="10" /><br>';
  18. $uploadinputs.=wf_Selector('delimiter', $delimiters, __('Delimiter'), '', true);
  19. $uploadinputs.=wf_Selector('encoding', $encodings, __('Encoding'), '', true);
  20. $uploadinputs.=wf_Submit('Upload');
  21. $uploadform=bs_UploadFormBody('', 'POST', $uploadinputs, 'glamour');
  22. return ($uploadform);;
  23. }
  24. function migrate_UploadFile() {
  25. //путь сохранения
  26. $uploaddir = 'exports/';
  27. //белый лист расширений
  28. $allowedExtensions = array("txt","csv");
  29. //по умолчанию надеемся на худшее
  30. $result=false;
  31. //проверяем точно ли текстовку нам подсовывают
  32. foreach ($_FILES as $file) {
  33. if ($file['tmp_name'] > '') {
  34. if (@!in_array(end(explode(".",strtolower($file['name']))),$allowedExtensions)) {
  35. $errormessage='Wrong file type';
  36. die($errormessage);
  37. }
  38. }
  39. }
  40. $filename=vf($_FILES['uluserbase']['name']);
  41. $uploadfile = $uploaddir . $filename;
  42. if (move_uploaded_file($_FILES['uluserbase']['tmp_name'], $uploadfile)) {
  43. $result=$filename;
  44. }
  45. return ($result);
  46. }
  47. function web_MigrationPreprocessing($filename,$delimiter,$encoding) {
  48. $path='exports/';
  49. $raw_data= file_get_contents($path.$filename);
  50. $parsed_data=array();
  51. if ($encoding!='utf-8') {
  52. $raw_data= iconv($encoding, 'utf-8', $raw_data);
  53. }
  54. $raw_data= explodeRows($raw_data);
  55. if (!empty($raw_data)) {
  56. foreach ($raw_data as $eachrow) {
  57. if (!empty($eachrow)) {
  58. $parsed_data[]= explode($delimiter, $eachrow);
  59. }
  60. }
  61. }
  62. if (sizeof($parsed_data)>1) {
  63. $col_count= sizeof($parsed_data[0]);
  64. $cells= wf_TableCell(__('Column number'));
  65. $cells.= wf_TableCell(__('Column content'));
  66. $rows= wf_TableRow($cells, 'row1');
  67. foreach ($parsed_data[0] as $col_num=>$col_data) {
  68. $cells= wf_TableCell($col_num);
  69. $cells.= wf_TableCell($col_data);
  70. $rows.= wf_TableRow($cells, 'row3');
  71. }
  72. $first_row= wf_TableBody($rows, '100%', '0', '');
  73. show_window(__('Found count of data columns'),$col_count);
  74. show_window(__('First of imported data rows'), $first_row);
  75. //construct of data processing form
  76. $rowNumArr=array();
  77. for ($i=0;$i<$col_count;$i++) {
  78. $rowNumArr[$i]=$i;
  79. }
  80. $login_arr=$rowNumArr+array('RANDOM'=>__('Generate Random'));
  81. $password_arr=$rowNumArr+array('RANDOM'=>__('Generate Random'));
  82. $ip_arr=$rowNumArr;
  83. $mac_arr=$rowNumArr+array('RANDOM'=>__('Generate Random'));
  84. $tariff_arr=$rowNumArr;
  85. $cash_arr=$rowNumArr;
  86. $credit_arr=$rowNumArr+array('ZERO'=>__('Set to zero'));
  87. $creditex_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  88. $phone_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  89. $mobile_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  90. $email_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  91. $address_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  92. $realname_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  93. $contract_arr=$rowNumArr+array('NONE'=>__('Set to none'));
  94. $ao_arr=$rowNumArr+array('AO_1'=>__('AlwaysOnline=1'));
  95. $down_arr=$rowNumArr+array('DOWN_0'=>__('Down=0'));
  96. $passive_arr=$rowNumArr+array('PASSIVE_0'=>__('Passive=0'));
  97. $regtype_arr=array('SQL'=>'Show SQL dump','UB'=>'Ubilling live register');
  98. //data column setting form
  99. $inputs= wf_Selector('login_col', $login_arr, __('User login'), '', true);
  100. $inputs.= wf_Selector('password_col', $password_arr, __('User password'), '', true);
  101. $inputs.= wf_Selector('ip_col', $ip_arr, __('User IP'), '', true);
  102. $inputs.= wf_Selector('mac_col', $mac_arr, __('User MAC'), '', true);
  103. $inputs.= wf_Selector('tariff_col', $tariff_arr, __('User tariff'), '', true);
  104. $inputs.= wf_Selector('cash_col', $cash_arr, __('User cash'), '', true);
  105. $inputs.= wf_Selector('credit_col', $credit_arr, __('User credit limit'), '', true);
  106. $inputs.= wf_Selector('creditex_col', $creditex_arr, __('User credit expire date'), '', true);
  107. $inputs.= wf_Selector('phone_col', $phone_arr, __('User phone'), '', true);
  108. $inputs.= wf_Selector('mobile_col', $mobile_arr, __('User mobile'), '', true);
  109. $inputs.= wf_Selector('email_col', $email_arr, __('User email'), '', true);
  110. $inputs.= wf_Selector('address_col', $address_arr, __('User address'), '', true);
  111. $inputs.= wf_Selector('realname_col', $realname_arr, __('User realname'), '', true);
  112. $inputs.= wf_Selector('contract_col', $contract_arr, __('User contract'), '', true);
  113. $inputs.= wf_Selector('ao_col', $ao_arr, __('User AlwaysOnline state'), '', true);
  114. $inputs.= wf_Selector('down_col', $down_arr, __('User Down state'), '', true);
  115. $inputs.= wf_Selector('passive_col', $passive_arr, __('User Passive state'), '', true);
  116. $inputs.= wf_Selector('regtype', $regtype_arr, __('User registration mode'), '', true);
  117. $inputs.= multinet_network_selector().__('Target network'). wf_delimiter();
  118. $inputs.= wf_HiddenInput('import_rawdata', base64_encode(serialize($parsed_data)));
  119. $inputs.=wf_Submit('Save this column pointers and continue import');
  120. $colform= wf_Form("?module=migration&setpointers=true", 'POST', $inputs, 'glamour');
  121. show_window(__('Select data columns and their values'),$colform);
  122. } else {
  123. show_error(__('Parsing error'));
  124. }
  125. }
  126. function web_MigrationPrepare($import_rawdata,$import_opts) {
  127. $import_rawdata= unserialize(base64_decode($import_rawdata));
  128. $import_opts= unserialize(base64_decode($import_opts));
  129. $cells = wf_TableCell('#');
  130. $cells.= wf_TableCell('[login]');
  131. $cells.= wf_TableCell('[password]');
  132. $cells.= wf_TableCell('[ip]');
  133. $cells.= wf_TableCell('[mac]');
  134. $cells.= wf_TableCell('[tariff]');
  135. $cells.= wf_TableCell('[cash]');
  136. $cells.= wf_TableCell('[phone]');
  137. $cells.= wf_TableCell('[mobile]');
  138. $cells.= wf_TableCell('[email]');
  139. $cells.= wf_TableCell('[credit]');
  140. $cells.= wf_TableCell('[creditex]');
  141. $cells.= wf_TableCell('[address]');
  142. $cells.= wf_TableCell('[realname]');
  143. $cells.= wf_TableCell('[contract]');
  144. $cells.= wf_TableCell('[ao]');
  145. $cells.= wf_TableCell('[down]');
  146. $cells.= wf_TableCell('[passive]');
  147. $rows=wf_TableRow($cells, 'row1');
  148. $regdata=array();
  149. $i=0;
  150. foreach ($import_rawdata as $eachrow) {
  151. $i++;
  152. $cells = wf_TableCell($i);
  153. if ($import_opts['login_col']!='RANDOM') {
  154. $login=$eachrow[$import_opts['login_col']];
  155. } else {
  156. $login= 'mi_'.zb_rand_string(8);
  157. }
  158. $cells.= wf_TableCell($login);
  159. if ($import_opts['password_col']!='RANDOM') {
  160. $password=$eachrow[$import_opts['password_col']];
  161. } else {
  162. $password= zb_rand_string(10);
  163. }
  164. $cells.= wf_TableCell($password);
  165. $ip=$eachrow[$import_opts['ip_col']];
  166. $cells.= wf_TableCell($ip);
  167. if ($import_opts['mac_col']!='RANDOM') {
  168. $mac=$eachrow[$import_opts['mac_col']];
  169. } else {
  170. $mac='14:'.'88'.':'.rand(10,99).':'.rand(10,99).':'.rand(10,99).':'.rand(10,99);
  171. }
  172. $cells.= wf_TableCell($mac);
  173. $tariff=$eachrow[$import_opts['tariff_col']];
  174. $cells.= wf_TableCell($tariff);
  175. $cash=$eachrow[$import_opts['cash_col']];
  176. $cells.= wf_TableCell($cash);
  177. if ($import_opts['phone_col']!='NONE') {
  178. $phone=$eachrow[$import_opts['phone_col']];
  179. } else {
  180. $phone='';
  181. }
  182. $cells.= wf_TableCell($phone);
  183. if ($import_opts['mobile_col']!='NONE') {
  184. $mobile=$eachrow[$import_opts['mobile_col']];
  185. } else {
  186. $mobile='';
  187. }
  188. $cells.= wf_TableCell($mobile);
  189. if ($import_opts['email_col']!='NONE') {
  190. $email=$eachrow[$import_opts['email_col']];
  191. } else {
  192. $email='';
  193. }
  194. $cells.= wf_TableCell($email);
  195. if ($import_opts['credit_col']!='ZERO') {
  196. $credit=$eachrow[$import_opts['credit_col']];
  197. } else {
  198. $credit=0;
  199. }
  200. $cells.= wf_TableCell($credit);
  201. if ($import_opts['creditex_col']!='NONE') {
  202. $creditex=$eachrow[$import_opts['creditex_col']];
  203. } else {
  204. $creditex='0';
  205. }
  206. $cells.= wf_TableCell($creditex);
  207. if ($import_opts['address_col']!='NONE') {
  208. $address=$eachrow[$import_opts['address_col']];
  209. } else {
  210. $address='';
  211. }
  212. $cells.= wf_TableCell($address);
  213. if ($import_opts['realname_col']!='NONE') {
  214. $realname=$eachrow[$import_opts['realname_col']];
  215. } else {
  216. $realname='';
  217. }
  218. $cells.= wf_TableCell($realname);
  219. if ($import_opts['contract_col']!='NONE') {
  220. $contract=$eachrow[$import_opts['contract_col']];
  221. } else {
  222. $contract='';
  223. }
  224. $cells.= wf_TableCell($contract);
  225. if ($import_opts['ao_col']!='AO_1') {
  226. $ao=$eachrow[$import_opts['ao_col']];
  227. } else {
  228. $ao=1;
  229. }
  230. $cells.= wf_TableCell($ao);
  231. if ($import_opts['down_col']!='DOWN_0') {
  232. $down=$eachrow[$import_opts['down_col']];
  233. } else {
  234. $down=0;
  235. }
  236. $cells.= wf_TableCell($down);
  237. if ($import_opts['passive_col']!='PASSIVE_0') {
  238. $passive=$eachrow[$import_opts['passive_col']];
  239. } else {
  240. $passive=0;
  241. }
  242. $cells.= wf_TableCell($passive);
  243. $rows.=wf_TableRow($cells, 'row3');
  244. // filling userreg array
  245. $regdata[$login]['login']=$login;
  246. $regdata[$login]['password']=$password;
  247. $regdata[$login]['ip']=$ip;
  248. $regdata[$login]['mac']=$mac;
  249. $regdata[$login]['tariff']=$tariff;
  250. $regdata[$login]['cash']=$cash;
  251. $regdata[$login]['phone']=$phone;
  252. $regdata[$login]['mobile']=$mobile;
  253. $regdata[$login]['email']=$email;
  254. $regdata[$login]['credit']=$credit;
  255. $regdata[$login]['creditex']=$creditex;
  256. $regdata[$login]['address']=$address;
  257. $regdata[$login]['realname']=$realname;
  258. $regdata[$login]['contract']=$contract;
  259. $regdata[$login]['ao']=$ao;
  260. $regdata[$login]['down']=$down;
  261. $regdata[$login]['passive']=$passive;
  262. }
  263. $regdata_save= serialize($regdata);
  264. $regdata_save= base64_encode($regdata_save);
  265. zb_StorageSet('IMPORT_REGDATA', $regdata_save);
  266. $preparse= wf_TableBody($rows, '100%', '0', '');
  267. show_window(__('All correct').'?',$preparse);
  268. $inputs= wf_Link('?module=migration', 'No I want to try another import settings', false, 'ubButton');
  269. $inputs.= wf_Link('?module=migration&setpointers=true&goregister=ok', 'Yes proceed registeration of this users', false, 'ubButton');
  270. show_window('', $inputs);
  271. }
  272. if (!wf_CheckGet(array('setpointers'))) {
  273. if(!wf_CheckPost(array('uploaduserbase'))) {
  274. //show upload form
  275. show_window(__('User database import from text file'), web_MigrationUploadForm());
  276. } else {
  277. //upload file and show preprocessing form
  278. $upload_done= migrate_UploadFile();
  279. if ($upload_done) {
  280. $delimiter= $_POST['delimiter'];
  281. $encoding= $_POST['encoding'];
  282. web_MigrationPreprocessing($upload_done, $delimiter, $encoding);
  283. }
  284. }
  285. } else {
  286. //some pointers already set, load raw data into database for processing
  287. if (wf_CheckPost(array('import_rawdata'))) {
  288. $import_rawdata=$_POST['import_rawdata'];
  289. zb_StorageSet('IMPORT_RAWDATA', $import_rawdata);
  290. $import_opts=array(
  291. 'login_col'=> $_POST['login_col'],
  292. 'password_col'=> $_POST['password_col'],
  293. 'ip_col'=> $_POST['ip_col'],
  294. 'mac_col'=> $_POST['mac_col'],
  295. 'tariff_col'=> $_POST['tariff_col'],
  296. 'cash_col'=> $_POST['cash_col'],
  297. 'phone_col'=> $_POST['phone_col'],
  298. 'mobile_col'=> $_POST['mobile_col'],
  299. 'email_col'=> $_POST['email_col'],
  300. 'credit_col'=> $_POST['credit_col'],
  301. 'creditex_col'=> $_POST['creditex_col'],
  302. 'address_col'=> $_POST['address_col'],
  303. 'realname_col'=> $_POST['realname_col'],
  304. 'contract_col'=> $_POST['contract_col'],
  305. 'ao_col'=> $_POST['ao_col'],
  306. 'down_col'=> $_POST['down_col'],
  307. 'passive_col'=> $_POST['passive_col'],
  308. 'netid'=> $_POST['networkselect'],
  309. 'regtype'=>$_POST['regtype']
  310. );
  311. $import_opts= serialize($import_opts);
  312. $import_opts= base64_encode($import_opts);
  313. zb_StorageSet('IMPORT_OPTS', $import_opts);
  314. } else {
  315. $import_rawdata= zb_StorageGet('IMPORT_RAWDATA');
  316. $import_opts= zb_StorageGet('IMPORT_OPTS');
  317. }
  318. //last checks
  319. if (!wf_CheckGet(array('goregister'))) {
  320. web_MigrationPrepare($import_rawdata, $import_opts);
  321. } else {
  322. //register imported users
  323. $regdata_raw= zb_StorageGet('IMPORT_REGDATA');
  324. $regdata= unserialize(base64_decode($regdata_raw));
  325. $querybuff='';
  326. if (!empty($regdata)) {
  327. foreach ($regdata as $io=>$user) {
  328. debarr($user);
  329. //typical register of each user
  330. $login=vf($user['login']);
  331. $password=vf($user['password']);
  332. $ip=$user['ip'];
  333. $iopts= unserialize(base64_decode($import_opts));
  334. $netid=$iopts['netid'];
  335. //Ubilling normal registration mode
  336. if ($iopts['regtype']=='UB') {
  337. $billing->createuser($login);
  338. log_register("StgUser REGISTER ".$login);
  339. $billing->setpassword($login,$password);
  340. log_register("StgUser PASSWORD ".$password);
  341. $billing->setip($login,$ip);
  342. log_register("StgUser IP ".$ip);
  343. multinet_add_host($netid, $ip);
  344. zb_UserCreateRealName($login, $user['realname']);
  345. zb_UserCreatePhone($login, $user['phone'], $user['mobile']);
  346. zb_UserCreateContract($login, $user['contract']);
  347. zb_UserCreateEmail($login, $user['email']);
  348. zb_UserCreateSpeedOverride($login, 0);
  349. multinet_change_mac($ip, $user['mac']);
  350. multinet_rebuild_all_handlers();
  351. $billing->setao($login,$user['ao']);
  352. $dstat=1;
  353. $billing->setdstat($login,$dstat);
  354. $billing->setdown($login,$user['down']);
  355. $billing->setpassive($login,$user['passive']);
  356. $billing->settariff($login,$user['tariff']);
  357. $billing->setcredit($login,$user['credit']);
  358. $billing->setcash($login,$user['cash']);
  359. zb_UserCreateNotes($login, $user['address']);
  360. }
  361. if ($iopts['regtype']=='SQL') {
  362. $querybuff.="
  363. INSERT INTO `users` (
  364. `login`,
  365. `Password`,
  366. `Passive`,
  367. `Down`,
  368. `DisabledDetailStat`,
  369. `AlwaysOnline`,
  370. `Tariff`,
  371. `Address`,
  372. `Phone`,
  373. `Email`,
  374. `Note`,
  375. `RealName`,
  376. `StgGroup`,
  377. `Credit`,
  378. `TariffChange`,
  379. `Userdata0`,
  380. `Userdata1`,
  381. `Userdata2`,
  382. `Userdata3`,
  383. `Userdata4`,
  384. `Userdata5`,
  385. `Userdata6`,
  386. `Userdata7`,
  387. `Userdata8`,
  388. `Userdata9`,
  389. `CreditExpire`,
  390. `IP`,
  391. `D0`,
  392. `U0`,
  393. `D1`,
  394. `U1`,
  395. `D2`,
  396. `U2`,
  397. `D3`,
  398. `U3`,
  399. `D4`,
  400. `U4`,
  401. `D5`,
  402. `U5`,
  403. `D6`,
  404. `U6`,
  405. `D7`,
  406. `U7`,
  407. `D8`,
  408. `U8`,
  409. `D9`,
  410. `U9`,
  411. `Cash`,
  412. `FreeMb`,
  413. `LastCashAdd`,
  414. `LastCashAddTime`,
  415. `PassiveTime`,
  416. `LastActivityTime`,
  417. `NAS`)
  418. VALUES (
  419. '".$login."',
  420. '".$password."',
  421. '".$user['passive']."',
  422. '".$user['down']."',
  423. '1',
  424. '".$user['ao']."',
  425. '".$user['tariff']."',
  426. '',
  427. '',
  428. '',
  429. '',
  430. '',
  431. '',
  432. '".$user['credit']."',
  433. '',
  434. '',
  435. '',
  436. '',
  437. '',
  438. '',
  439. '',
  440. '',
  441. '',
  442. '',
  443. '',
  444. '".$user['creditex']."',
  445. '".$ip."',
  446. '0',
  447. '0',
  448. '0',
  449. '0',
  450. '0',
  451. '0',
  452. '0',
  453. '0',
  454. '0',
  455. '0',
  456. '0',
  457. '0',
  458. '0',
  459. '0',
  460. '0',
  461. '0',
  462. '0',
  463. '0',
  464. '0',
  465. '0',
  466. '".$user['cash']."',
  467. '0',
  468. '0',
  469. '0',
  470. '0',
  471. '0',
  472. '');
  473. "."\n";
  474. //multinet
  475. $querybuff.="INSERT INTO `nethosts` (`id`,`netid`,`ip`,`mac`,`option`) VALUES ('', '".$netid."' ,'".$ip."', '".$user['mac']."', '');"."\n";
  476. //realname
  477. $querybuff.="INSERT INTO `realname` (`id`,`login`,`realname`) VALUES (NULL, '".$login."','".$user['realname']."');"."\n";
  478. //phone & mobile
  479. $querybuff.="INSERT INTO `phones` (`id`,`login`,`phone`,`mobile`) VALUES (NULL, '".$login."','".$user['phone']."','".$user['mobile']."');"."\n";
  480. //contract
  481. $querybuff.="INSERT INTO `contracts` (`id`,`login`,`contract`) VALUES (NULL, '".$login."','".$user['contract']."');"."\n";
  482. //email
  483. $querybuff.="INSERT INTO `emails` (`id`,`login`,`email`) VALUES (NULL, '".$login."','".$user['email']."');"."\n";
  484. //speedoverride
  485. $querybuff.="INSERT INTO `userspeeds` (`id`,`login`,`speed`) VALUES (NULL, '".$login."','0');"."\n";
  486. //notes
  487. $querybuff.="INSERT INTO `notes` (`id`,`login`,`note`) VALUES ('', '".$login."','".$user['address']."');"."\n";
  488. }
  489. }
  490. show_window(__('Generated SQL dump'), wf_TextArea('sqldump', '', $querybuff, true, '120x20'));
  491. }
  492. }
  493. }
  494. } else {
  495. show_error(__('Access denied'));
  496. }
  497. ?>