-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.php
1453 lines (1349 loc) · 66.9 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* install.php
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: http://www.niushop.com.cn
*
* 非授权用户允许商用,严禁去除Niushop相关的版权信息。
* 请尊重Niushop开发人员劳动成果,严禁使用本系统转卖、销售或二次开发后转卖、销售等商业行为。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
* @author : niuteam
* @date : 2015.1.17
* @version : v1.0.0.0
*/
define('IN_IA', true);
error_reporting(0);
@set_time_limit(0);
//@set_magic_quotes_runtime(0);
ob_start();
define('IA_ROOT', str_replace("\\",'/', dirname(__FILE__)));
if($_GET['res']) {
$res = $_GET['res'];
$reses = tpl_resources();
if(array_key_exists($res, $reses)) {
if($res == 'css') {
header('content-type:text/css');
} else {
header('content-type:image/png');
}
echo base64_decode($reses[$res]);
exit();
}
}
if($_GET['action']){
$dbserver = $_GET['dbserver'];
$dbusername = $_GET['dbusername'];
$dbpassword = $_GET['dbpassword'];
$dbname = $_GET['dbname'];
$link = mysql_connect($dbserver, $dbusername, $dbpassword);
$query = mysql_query("SHOW DATABASES LIKE '{$dbname}';");
// var_dump($query);
if(mysql_fetch_assoc($query) != false){
//说明数据库已经存在
echo 1;
exit();
}else{
echo 0;
exit();
}
}
$actions = array('license', 'env', 'db', 'finish');
$action = $_COOKIE['action'];
$action = in_array($action, $actions) ? $action : 'license';
$ispost = strtolower($_SERVER['REQUEST_METHOD']) == 'post';
if(file_exists(IA_ROOT . '/install.lock') && $action != 'finish') {
header('location: ./index.php/shop');
exit;
}
header('content-type: text/html; charset=utf-8');
if($action == 'license') {
if($ispost) {
setcookie('action', 'env');
header('location: ?refresh');
exit;
}
tpl_install_license();
}
if($action == 'env') {
if($ispost) {
setcookie('action', $_POST['do'] == 'continue' ? 'db' : 'license');
header('location: ?refresh');
exit;
}
$ret = array();
$ret['server']['os']['value'] = php_uname();
if(PHP_SHLIB_SUFFIX == 'dll') {
$ret['server']['os']['remark'] = '建议使用 Linux 系统以提升程序性能';
$ret['server']['os']['class'] = 'warning';
}
$ret['server']['sapi']['value'] = $_SERVER['SERVER_SOFTWARE'];
if(PHP_SAPI == 'isapi') {
$ret['server']['sapi']['remark'] = '建议使用 Apache 或 Nginx 以提升程序性能';
$ret['server']['sapi']['class'] = 'warning';
}
$ret['server']['php']['value'] = PHP_VERSION;
$ret['server']['dir']['value'] = IA_ROOT;
if(function_exists('disk_free_space')) {
$ret['server']['disk']['value'] = floor(disk_free_space(IA_ROOT) / (1024*1024)).'M';
} else {
$ret['server']['disk']['value'] = 'unknow';
}
$ret['server']['upload']['value'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknow';
$ret['php']['version']['value'] = PHP_VERSION;
$ret['php']['version']['class'] = 'success';
if(version_compare(PHP_VERSION, '5.4.0') == -1) {
$ret['php']['version']['class'] = 'danger';
$ret['php']['version']['failed'] = true;
$ret['php']['version']['remark'] = 'PHP版本必须为 5.4.0 以上.';
}
if(strstr(PHP_VERSION, '7.'))
{
$ret['php']['mysql']['ok'] = function_exists('mysqli_connect');
var_dump($ret['php']['mysql']['ok']);
if($ret['php']['mysql']['ok']) {
$ret['php']['mysql']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
} else {
$ret['php']['pdo']['failed'] = true;
$ret['php']['mysql']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
}
$ret['php']['pdo']['ok'] = extension_loaded('pdo') && extension_loaded('pdo_mysql');
if($ret['php']['pdo']['ok']) {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['pdo']['class'] = 'success';
if(!$ret['php']['mysql']['ok']) {
$ret['php']['pdo']['remark'] = '您的PHP环境不支持 mysql_connect,请开启此扩展. ';
}
} else {
$ret['php']['pdo']['failed'] = true;
if($ret['php']['mysql']['ok']) {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-remove text-warning"></span>';
$ret['php']['pdo']['class'] = 'warning';
$ret['php']['pdo']['remark'] = '您的PHP环境不支持PDO, 请开启此扩展. ';
} else {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['pdo']['class'] = 'danger';
$ret['php']['pdo']['remark'] = '您的PHP环境不支持PDO, 也不支持 mysql_connect, 系统无法正常运行. ';
}
}
}else{
$ret['php']['mysql']['ok'] = function_exists('mysqli_connect');
if($ret['php']['mysql']['ok']) {
$ret['php']['mysql']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
} else {
$ret['php']['pdo']['failed'] = true;
$ret['php']['mysql']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
}
$ret['php']['pdo']['ok'] = extension_loaded('pdo') && extension_loaded('pdo_mysql');
if($ret['php']['pdo']['ok']) {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['pdo']['class'] = 'success';
if(!$ret['php']['mysql']['ok']) {
$ret['php']['pdo']['remark'] = '您的PHP环境不支持 mysqli_connect,请开启此扩展. ';
}
} else {
$ret['php']['pdo']['failed'] = true;
if($ret['php']['mysql']['ok']) {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-remove text-warning"></span>';
$ret['php']['pdo']['class'] = 'warning';
$ret['php']['pdo']['remark'] = '您的PHP环境不支持PDO, 请开启此扩展. ';
} else {
$ret['php']['pdo']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['pdo']['class'] = 'danger';
$ret['php']['pdo']['remark'] = '您的PHP环境不支持PDO, 也不支持 mysqli_connect, 系统无法正常运行. ';
}
}
}
$ret['php']['fopen']['ok'] = @ini_get('allow_url_fopen') && function_exists('fsockopen');
if($ret['php']['fopen']['ok']) {
$ret['php']['fopen']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
} else {
$ret['php']['fopen']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
}
//$ret['php']['curl']['ok'] = extension_loaded('curl') && function_exists('curl_init');
$ret['php']['curl']['ok'] = 1;
if($ret['php']['curl']['ok']) {
$ret['php']['curl']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['curl']['class'] = 'success';
if(!$ret['php']['fopen']['ok']) {
$ret['php']['curl']['remark'] = '您的PHP环境虽然不支持 allow_url_fopen, 但已经支持了cURL, 这样系统是可以正常高效运行的, 不需要额外处理. ';
}
} else {
if($ret['php']['fopen']['ok']) {
$ret['php']['curl']['value'] = '<span class="glyphicon glyphicon-remove text-warning"></span>';
$ret['php']['curl']['class'] = 'warning';
$ret['php']['curl']['remark'] = '您的PHP环境不支持cURL, 但支持 allow_url_fopen, 这样系统虽然可以运行, 但还是建议你开启cURL以提升程序性能和系统稳定性. ';
} else {
$ret['php']['curl']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['curl']['class'] = 'danger';
$ret['php']['curl']['remark'] = '您的PHP环境不支持cURL, 也不支持 allow_url_fopen, 系统无法正常运行. ';
$ret['php']['curl']['failed'] = true;
}
}
// $ret['php']['ssl']['ok'] = extension_loaded('openssl');
// if($ret['php']['ssl']['ok']) {
// $ret['php']['ssl']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
// $ret['php']['ssl']['class'] = 'success';
// } else {
// $ret['php']['ssl']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
// $ret['php']['ssl']['class'] = 'danger';
// $ret['php']['ssl']['failed'] = true;
// $ret['php']['ssl']['remark'] = '没有启用OpenSSL, 将无法访问公众平台的接口, 系统无法正常运行. ';
// }
$ret['php']['gd']['ok'] = extension_loaded('gd');
if($ret['php']['gd']['ok']) {
$ret['php']['gd']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['gd']['class'] = 'success';
} else {
$ret['php']['gd']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['gd']['class'] = 'danger';
$ret['php']['gd']['failed'] = true;
$ret['php']['gd']['remark'] = '没有启用GD, 将无法正常上传和压缩图片, 系统无法正常运行. ';
}
$ret['php']['dom']['ok'] = class_exists('DOMDocument');
if($ret['php']['dom']['ok']) {
$ret['php']['dom']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['dom']['class'] = 'success';
} else {
$ret['php']['dom']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['dom']['class'] = 'danger';
$ret['php']['dom']['failed'] = true;
$ret['php']['dom']['remark'] = '没有启用DOMDocument, 将无法正常安装使用模块, 系统无法正常运行. ';
}
$ret['php']['session']['ok'] = ini_get('session.auto_start');
if($ret['php']['session']['ok'] == 0 || strtolower($ret['php']['session']['ok']) == 'off') {
$ret['php']['session']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['session']['class'] = 'success';
} else {
$ret['php']['session']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['session']['class'] = 'danger';
$ret['php']['session']['failed'] = true;
$ret['php']['session']['remark'] = '系统session.auto_start开启, 将无法正常注册会员, 系统无法正常运行. ';
}
$ret['php']['asp_tags']['ok'] = ini_get('asp_tags');
if(empty($ret['php']['asp_tags']['ok']) || strtolower($ret['php']['asp_tags']['ok']) == 'off') {
$ret['php']['asp_tags']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['php']['asp_tags']['class'] = 'success';
} else {
$ret['php']['asp_tags']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['php']['asp_tags']['class'] = 'danger';
$ret['php']['asp_tags']['failed'] = true;
$ret['php']['asp_tags']['remark'] = '请禁用可以使用ASP 风格的标志,配置php.ini中asp_tags = Off';
}
$ret['write']['bottom']['ok'] = local_writeable(dirname(__FILE__));
if($ret['write']['bottom']['ok']) {
$ret['write']['bottom']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['write']['bottom']['class'] = 'success';
} else {
$ret['write']['bottom']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['write']['bottom']['class'] = 'danger';
$ret['write']['bottom']['failed'] = true;
$ret['write']['bottom']['remark'] = '项目根目录无法写入,系统将无法正常运行. ';
}
$ret['write']['root']['ok'] = local_writeable(IA_ROOT . '/upload');
if($ret['write']['root']['ok']) {
$ret['write']['root']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['write']['root']['class'] = 'success';
} else {
$ret['write']['root']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['write']['root']['class'] = 'danger';
$ret['write']['root']['failed'] = true;
$ret['write']['root']['remark'] = 'upload无法写入, 将无法使用自动更新功能, 系统无法正常运行. ';
}
$ret['write']['data']['ok'] = local_writeable(IA_ROOT . '/runtime');
if($ret['write']['data']['ok']) {
$ret['write']['data']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['write']['data']['class'] = 'success';
} else {
$ret['write']['data']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['write']['data']['class'] = 'danger';
$ret['write']['data']['failed'] = true;
$ret['write']['data']['remark'] = 'runtime目录无法写入, 将无法写入配置文件, 系统无法正常安装. ';
}
$ret['write']['database']['ok'] = local_writeable(IA_ROOT . '/application');
if($ret['write']['database']['ok']) {
$ret['write']['database']['value'] = '<span class="glyphicon glyphicon-ok text-success"></span>';
$ret['write']['database']['class'] = 'success';
} else {
$ret['write']['database']['value'] = '<span class="glyphicon glyphicon-remove text-danger"></span>';
$ret['write']['database']['class'] = 'danger';
$ret['write']['database']['failed'] = true;
$ret['write']['database']['remark'] = 'application目录无法写入, 将无法写入配置文件, 系统无法正常安装. ';
}
$ret['continue'] = true;
foreach($ret['php'] as $opt) {
if($opt['failed']) {
$ret['continue'] = false;
break;
}
}
foreach($ret['write'] as $v){
if($v['failed']) {
$ret['continue'] = false;
}
}
tpl_install_env($ret);
}
if($action == 'db') {
if($ispost) {
if($_POST['do'] != 'continue') {
setcookie('action', 'env');
header('location: ?refresh');
exit();
}
$family = $_POST['family'] == 'x' ? 'x' : 'v';
$db = $_POST['db'];
$user = $_POST['user'];
// 针对php7版本数据库安装
if(strstr(PHP_VERSION, '7.'))
{
$link = mysqli_connect($db['server'], $db['username'], $db['password']);
if(!$link) {
$error = mysqli_connect_error();
if (strpos($error, 'Access denied for user') !== false) {
$error = '您的数据库访问用户名或是密码错误. <br />';
} else {
$error = iconv('gbk', 'utf8', $error);
}
} else {
mysqli_query($link, "SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysqli_query($link, "SET sql_mode=''");
if(mysqli_errno($link)) {
$error = mysqli_error($link);
} else {
$query = mysqli_query($link, "SHOW DATABASES LIKE '{$db['name']}';");
if (!mysqli_fetch_assoc($query)) {
if(mysqli_get_server_info() > '4.1') {
mysqli_query($link, "CREATE DATABASE IF NOT EXISTS `{$db['name']}` DEFAULT CHARACTER SET utf8");
} else {
mysqli_query($link, "CREATE DATABASE IF NOT EXISTS `{$db['name']}`");
}
}
$query = mysqli_query($link, "SHOW DATABASES LIKE '{$db['name']}';");
if (!mysqli_fetch_assoc($query)) {
$error .= "数据库不存在且创建数据库失败. <br />";
}
if(mysqli_errno($link)) {
$error .= mysqli_error($link);
}
}
}
if(empty($error)) {
mysqli_select_db($link, $db['name']);
$query = mysqli_query($link,"SHOW TABLES LIKE '{$db['prefix']}%';");
if (mysqli_fetch_assoc($query)) {
//$error = '您的数据库不为空,请重新建立数据库或是清空该数据库!';
die('<script type="text/javascript">alert("您的数据库不为空,请重新建立数据库或是清空该数据库.");history.back();</script>');
}
}
if(empty($error)) {
$pieces = explode(':', $db['server']);
$db['port'] = !empty($pieces[1]) ? $pieces[1] : '3306';
$config = db_config();
$cookiepre = local_salt(4) . '_';
$authkey = local_salt(8);
$config = str_replace(array(
'{db-server}', '{db-username}', '{db-password}', '{db-port}','{db-name}'
), array(
$db['server'], $db['username'], $db['password'], $db['port'], $db['name']
), $config);
mysqli_close($link);
//循环添加数据
if(file_exists(IA_ROOT . '/niushop_b2c.sql')){
$link = mysqli_connect($db['server'], $db['username'], $db['password'], $db['name']);
mysqli_query($link, "SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysqli_query($link, "SET sql_mode=''");
if(!$link){
die('<script type="text/javascript">alert("连接不到数据库, 请稍后重试!");history.back();</script>');
}
$sql = file_get_contents(IA_ROOT . '/niushop_b2c.sql');
$sql = str_replace("\r", "\n", $sql);
$sql = explode(";\n", $sql);
foreach ($sql as $k =>$item) {
$item = trim($item);
if(empty($item)) continue;
preg_match('/CREATE TABLE `([^ ]*)`/', $item, $matches);
if($matches) {
mysqli_select_db($db['name']);
$table_name = $matches[1];
$result = mysqli_query($link, $item);
} else {
mysqli_select_db($db['name']);
$result = mysqli_query($link, $item);
//$db->execute($item);
}
}
}else{
die('<script type="text/javascript">alert("安装包不正确, 数据安装脚本缺失.");history.back();</script>');
}
//删除商品sku错误信息 和 商品属性错误信息
mysqli_query($link, "DELETE FROM ns_goods_sku WHERE goods_id NOT IN (SELECT goods_id FROM ns_goods)");
mysqli_query($link, "DELETE FROM ns_goods_attribute WHERE goods_id NOT IN ( SELECT goods_id FROM ns_goods)");
//添加用户管理员
$password = md5($user['password']);
$datetime =date('Y-m-d H:i:s', time());
mysqli_query($link, "DELETE FROM sys_user WHERE user_name = '{$user['username']}'");
$insert_error = mysqli_query($link, "INSERT INTO sys_user (user_name, user_password, is_system, is_member, reg_time)
VALUES('{$user['username']}', '{$password}', '1', '1','" . $datetime . "')");
if($insert_error){
$insert_id = mysqli_insert_id($link);
$member_level_result=mysqli_query($link, "SELECT * FROM ns_member_level WHERE is_default=1;");
$member_default_level=0;
while ($row=mysqli_fetch_array($member_level_result))
{
$member_default_level =$row["level_id"];
}
mysqli_query($link, "INSERT INTO ns_member ( uid, member_name, member_level, reg_time, memo) VALUES (".$insert_id .",'{$user['username']}', ".$member_default_level.", '".$datetime."', '');");
//添加管理员用户组
$group_list = array();
$group_string = "";
$result= mysqli_query($link, "SELECT * FROM sys_module where is_control_auth = 1");
while ($row=mysqli_fetch_array($result))
{
$group_string .=",".$row["module_id"];
}
if($group_string != ''){
$group_string = substr($group_string, 1);
}
$group_error = mysqli_query($link, "INSERT INTO sys_user_group (group_name,instance_id, is_system, module_id_array, create_time)
VALUES('管理员组','0', '1','{$group_string}','" . $datetime . "')");
if($group_error){
$group_insert_id = mysqli_insert_id($link);
//给用户添加管理员权限
mysqli_query($link, "INSERT INTO sys_user_admin (uid, admin_name, group_id_array, is_admin, admin_status)
VALUES('{$insert_id}', '管理员','{$group_insert_id}', '1', '1')");
}else{
die('<script type="text/javascript">alert("管理员账户注册失败.");history.back();</script>');
}
}
}else{
die('<script type="text/javascript">alert("'.$error.'");history.back();</script>');
}
}else{
$link = mysql_connect($db['server'], $db['username'], $db['password']);
if(empty($link)) {
$error = mysql_error();
if (strpos($error, 'Access denied for user') !== false) {
$error = '您的数据库访问用户名或是密码错误';
} else {
$error = iconv('gbk', 'utf8', $error);
}
} else {
mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysql_query("SET sql_mode=''");
if(mysql_errno()) {
$error = mysql_error();
} else {
$query = mysql_query("SHOW DATABASES LIKE '{$db['name']}';");
// var_dump($query);
if (!mysql_fetch_assoc($query)) {
if(mysql_get_server_info() > '4.1') {
mysql_query("CREATE DATABASE IF NOT EXISTS `{$db['name']}` DEFAULT CHARACTER SET utf8", $link);
} else {
mysql_query("CREATE DATABASE IF NOT EXISTS `{$db['name']}`", $link);
}
}
$query = mysql_query("SHOW DATABASES LIKE '{$db['name']}';");
if (!mysql_fetch_assoc($query)) {
$error .= "数据库不存在且创建数据库失败";
}
if(mysql_errno()) {
$error .= mysql_error();
}
}
}
if(empty($error)) {
mysql_select_db($db['name']);
$query = mysql_query("SHOW TABLES LIKE '{$db['prefix']}%';");
if (mysql_fetch_assoc($query)) {
//$error = '您的数据库不为空,请重新建立数据库或是清空该数据库!';
die('<script type="text/javascript">alert("您的数据库不为空,请重新建立数据库或是清空该数据库.");history.back();</script>');
}
}
if(empty($error)) {
$pieces = explode(':', $db['server']);
$db['port'] = !empty($pieces[1]) ? $pieces[1] : '3306';
$config = db_config();
$cookiepre = local_salt(4) . '_';
$authkey = local_salt(8);
$config = str_replace(array(
'{db-server}', '{db-username}', '{db-password}', '{db-port}','{db-name}'
), array(
$db['server'], $db['username'], $db['password'], $db['port'], $db['name']
), $config);
mysql_close($link);
$link = mysql_connect($db['server'], $db['username'], $db['password']);
if(!$link){
die('<script type="text/javascript">alert("连接不到服务器, 请稍后重试!");history.back();</script>');
}
$mysql_db = mysql_select_db($db['name']);
if(!$mysql_db){
die('<script type="text/javascript">alert("连接不到数据库, 请稍后重试!");history.back();</script>');
}
mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysql_query("SET sql_mode=''");
//循环添加数据
if(file_exists(IA_ROOT . '/niushop_b2c.sql')){
$sql = file_get_contents(IA_ROOT . '/niushop_b2c.sql');
$sql = str_replace("\r", "\n", $sql);
$sql = explode(";\n", $sql);
foreach ($sql as $item) {
$item = trim($item);
if(empty($item)) continue;
preg_match('/CREATE TABLE `([^ ]*)`/', $item, $matches);
if($matches) {
$table_name = $matches[1];
mysql_query($item, $link);
} else {
mysql_close($link);
$link = mysql_connect($db['server'], $db['username'], $db['password']);
mysql_select_db($db['name']);
mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysql_query("SET sql_mode=''");
mysql_query($item, $link);
//$db->execute($item);
}
}
}else{
die('<script type="text/javascript">alert("安装包不正确, 数据安装脚本缺失.");history.back();</script>');
}
//添加用户管理员
mysql_close($link);
$link = mysql_connect($db['server'], $db['username'], $db['password']);
mysql_select_db($db['name']);
mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
mysql_query("SET sql_mode=''");
//删除商品sku错误信息 和 商品属性错误信息
mysql_query("DELETE FROM ns_goods_sku WHERE goods_id NOT IN (SELECT goods_id FROM ns_goods)");
mysql_query("DELETE FROM ns_goods_attribute WHERE goods_id NOT IN ( SELECT goods_id FROM ns_goods)");
$password = md5($user['password']);
$datetime =date('Y-m-d H:i:s', time());
mysql_query("DELETE FROM sys_user WHERE user_name = '{$user['username']}'");
$insert_error = mysql_query("INSERT INTO sys_user (user_name, user_password, is_system, is_member, reg_time, nick_name)
VALUES('{$user['username']}', '{$password}', '1', '1','" . $datetime . "', '{$user['username']}')");
if($insert_error){
$insert_id = mysql_insert_id();
$member_level_result=mysql_query("SELECT * FROM ns_member_level WHERE is_default=1;", $link);
$member_default_level=0;
while ($row=mysql_fetch_array($member_level_result))
{
$member_default_level =$row["level_id"];
}
mysql_query("INSERT INTO ns_member ( uid, member_name, member_level, reg_time, memo) VALUES (".$insert_id .",'{$user['username']}', ".$member_default_level.", '".$datetime."', '');", $link);
//添加管理员用户组
$group_list = array();
$group_string = "";
$result= mysql_query("SELECT * FROM sys_module where is_control_auth = 1", $link);
while ($row=mysql_fetch_array($result))
{
$group_string .=",".$row["module_id"];
}
if($group_string != ''){
$group_string = substr($group_string, 1);
}
$group_error = mysql_query("INSERT INTO sys_user_group (group_name,instance_id, is_system, module_id_array, create_time)
VALUES('管理员组','0', '1','{$group_string}','" . $datetime . "')", $link);
if($group_error){
$group_insert_id = mysql_insert_id();
//给用户添加管理员权限
mysql_query("INSERT INTO sys_user_admin (uid, admin_name, group_id_array, is_admin, admin_status)
VALUES('{$insert_id}', '管理员','{$group_insert_id}', '1', '1')", $link);
}else{
die('<script type="text/javascript">alert("管理员账户注册失败.");history.back();</script>');
}
}else{
die('<script type="text/javascript">alert("管理员账户注册失败.");history.back();</script>');
}
}else{
die('<script type="text/javascript">alert("'.$error.'");history.back();</script>');
}
}
//配置数据库
file_put_contents(IA_ROOT . '/application/database.php', $config);
touch(IA_ROOT . '/install.lock');
setcookie('action', 'finish');
header('location: ?refresh');
exit();
}
tpl_install_db($error);
}
if($action == 'finish') {
//setcookie('action', '', -10);
// $dbfile = IA_ROOT . '/data/db.php';
// @unlink($dbfile);
// define('IN_SYS', true);
// require IA_ROOT . '/framework/bootstrap.inc.php';
// require IA_ROOT . '/web/common/bootstrap.sys.inc.php';
// $_W['uid'] = $_W['isfounder'] = 1;
// load()->web('common');
// load()->web('template');
// load()->model('setting');
// load()->model('cache');
// cache_build_frame_menu();
// cache_build_setting();
// cache_build_users_struct();
// cache_build_module_subscribe_type();
tpl_install_finish();
}
function local_writeable($dir) {
$writeable = 0;
if(!is_dir($dir)) {
@mkdir($dir, 0777);
}
if(is_dir($dir)) {
if($fp = fopen("$dir/test.txt", 'w')) {
fclose($fp);
unlink("$dir/test.txt");
$writeable = 1;
} else {
$writeable = 0;
}
}
return $writeable;
}
function local_salt($length = 8) {
$result = '';
while(strlen($result) < $length) {
$result .= sha1(uniqid('', true));
}
return substr($result, 0, $length);
}
function local_config() {
$cfg = <<<EOF
<?php
defined('IN_IA') or exit('Access Denied');
\$config = array();
\$config['db']['master']['host'] = '{db-server}';
\$config['db']['master']['username'] = '{db-username}';
\$config['db']['master']['password'] = '{db-password}';
\$config['db']['master']['port'] = '{db-port}';
\$config['db']['master']['database'] = '{db-name}';
\$config['db']['master']['charset'] = 'utf8';
\$config['db']['master']['pconnect'] = 0;
\$config['db']['master']['tablepre'] = '{db-tablepre}';
\$config['db']['slave_status'] = false;
\$config['db']['slave']['1']['host'] = '';
\$config['db']['slave']['1']['username'] = '';
\$config['db']['slave']['1']['password'] = '';
\$config['db']['slave']['1']['port'] = '3307';
\$config['db']['slave']['1']['database'] = '';
\$config['db']['slave']['1']['charset'] = 'utf8';
\$config['db']['slave']['1']['pconnect'] = 0;
\$config['db']['slave']['1']['tablepre'] = 'ims_';
\$config['db']['slave']['1']['weight'] = 0;
\$config['db']['common']['slave_except_table'] = array('core_sessions');
// -------------------------- CONFIG COOKIE --------------------------- //
\$config['cookie']['pre'] = '{cookiepre}';
\$config['cookie']['domain'] = '';
\$config['cookie']['path'] = '/';
// -------------------------- CONFIG SETTING --------------------------- //
\$config['setting']['charset'] = 'utf-8';
\$config['setting']['cache'] = 'mysql';
\$config['setting']['timezone'] = 'Asia/Shanghai';
\$config['setting']['memory_limit'] = '256M';
\$config['setting']['filemode'] = 0644;
\$config['setting']['authkey'] = '{authkey}';
\$config['setting']['founder'] = '1';
\$config['setting']['development'] = 0;
\$config['setting']['referrer'] = 0;
\$config['setting']['https'] = 0;
// -------------------------- CONFIG UPLOAD --------------------------- //
\$config['upload']['image']['extentions'] = array('gif', 'jpg', 'jpeg', 'png');
\$config['upload']['image']['limit'] = 5000;
\$config['upload']['attachdir'] = '{attachdir}';
\$config['upload']['audio']['extentions'] = array('mp3');
\$config['upload']['audio']['limit'] = 5000;
// -------------------------- CONFIG MEMCACHE --------------------------- //
\$config['setting']['memcache']['server'] = '';
\$config['setting']['memcache']['port'] = 11211;
\$config['setting']['memcache']['pconnect'] = 1;
\$config['setting']['memcache']['timeout'] = 30;
\$config['setting']['memcache']['session'] = 1;
// -------------------------- CONFIG PROXY --------------------------- //
\$config['setting']['proxy']['host'] = '';
\$config['setting']['proxy']['auth'] = '';
EOF;
return trim($cfg);
}
function db_config(){
$cfg = <<<EOF
<?php
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '{db-server}',
// 数据库名
'database' => '{db-name}',
// 用户名
'username' => '{db-username}',
// 密码
'password' => '{db-password}',
// 端口
'hostport' => '{db-port}',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型 array 数组 collection Collection对象
'resultset_type' => 'array',
// 是否自动写入时间戳字段
'auto_timestamp' => false,
// 是否需要进行SQL性能分析
'sql_explain' => false,
];
EOF;
return trim($cfg);
}
function local_mkdirs($path) {
if(!is_dir($path)) {
local_mkdirs(dirname($path));
mkdir($path);
}
return is_dir($path);
}
function local_run($sql) {
global $link, $db;
if(!isset($sql) || empty($sql)) return;
$sql = str_replace("\r", "\n", str_replace('', ' '.$db['prefix'], $sql));
$sql = str_replace("\r", "\n", str_replace('', ' `'.$db['prefix'], $sql));
$ret = array();
$num = 0;
foreach(explode(";\n", trim($sql)) as $query) {
$ret[$num] = '';
$queries = explode("\n", trim($query));
foreach($queries as $query) {
$ret[$num] .= (isset($query[0]) && $query[0] == '#') || (isset($query[1]) && isset($query[1]) && $query[0].$query[1] == '--') ? '' : $query;
}
$num++;
}
unset($sql);
foreach($ret as $query) {
$query = trim($query);
if($query) {
if(!mysql_query($query, $link)) {
echo mysql_errno() . ": " . mysql_error() . "<br />";
exit($query);
}
}
}
}
function local_create_sql($schema) {
$pieces = explode('_', $schema['charset']);
$charset = $pieces[0];
$engine = $schema['engine'];
$sql = "CREATE TABLE IF NOT EXISTS `{$schema['tablename']}` (\n";
foreach ($schema['fields'] as $value) {
if(!empty($value['length'])) {
$length = "({$value['length']})";
} else {
$length = '';
}
$signed = empty($value['signed']) ? ' unsigned' : '';
if(empty($value['null'])) {
$null = ' NOT NULL';
} else {
$null = '';
}
if(isset($value['default'])) {
$default = " DEFAULT '" . $value['default'] . "'";
} else {
$default = '';
}
if($value['increment']) {
$increment = ' AUTO_INCREMENT';
} else {
$increment = '';
}
$sql .= "`{$value['name']}` {$value['type']}{$length}{$signed}{$null}{$default}{$increment},\n";
}
foreach ($schema['indexes'] as $value) {
$fields = implode('`,`', $value['fields']);
if($value['type'] == 'index') {
$sql .= "KEY `{$value['name']}` (`{$fields}`),\n";
}
if($value['type'] == 'unique') {
$sql .= "UNIQUE KEY `{$value['name']}` (`{$fields}`),\n";
}
if($value['type'] == 'primary') {
$sql .= "PRIMARY KEY (`{$fields}`),\n";
}
}
$sql = rtrim($sql);
$sql = rtrim($sql, ',');
$sql .= "\n) ENGINE=$engine DEFAULT CHARSET=$charset;\n\n";
return $sql;
}
function __remote_install_headers($ch = '', $header = '') {
static $hash;
if(!empty($header)) {
$pieces = explode(':', $header);
if(trim($pieces[0]) == 'hash') {
$hash = trim($pieces[1]);
}
}
if($ch == '' && $header == '') {
return $hash;
}
return strlen($header);
}
function __remote_download_headers($ch = '', $header = '') {
static $hash;
if(!empty($header)) {
$pieces = explode(':', $header);
if(trim($pieces[0]) == 'hash') {
$hash = trim($pieces[1]);
}
}
if($ch == '' && $header == '') {
return $hash;
}
return strlen($header);
}
function tpl_frame() {
global $action, $actions;
$action = $_COOKIE['action'];
$step = array_search($action, $actions);
$steps = array();
for($i = 0; $i <= $step; $i++) {
if($i == $step) {
$steps[$i] = ' list-group-item-info';
} else {
$steps[$i] = ' list-group-item-success';
}
}
$progress = $step * 25 + 25;
$content = ob_get_contents();
ob_clean();
$tpl = <<<EOF
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>安装系统 - Niushop开源商城</title>
<link rel="stylesheet" href="./public/install/css/bootstrap.min.css">
<style>
html,body{font-size:13px;font-family:"Microsoft YaHei UI", "微软雅黑", "宋体";}
.pager li.previous a{margin-right:10px;}
.header a{color:#FFF;}
.header a:hover{color:#428bca;}
.footer{padding:10px;}
.footer a,.footer{color:#eee;font-size:14px;line-height:25px;}
</style>
</head>
<body style="background-color:#28b0e4;">
<div class="container">
<div class="header" style="margin:15px auto;">
<ul class="nav nav-pills pull-right" role="tablist">
<li role="presentation" class="active"><a href="javascript:;">安装Niushop开源商城</a></li>
<li role="presentation"><a target = "_blank" href="http://www.niushop.com.cn">Niushop开源商城官网</a></li>
<li role="presentation"><a target = "_blank" href="http://www.niushop.com.cn/forummain.html">访问论坛</a></li>
</ul>
<img src="?res=logo" />
</div>
<div class="row well" style="margin:auto 0;">
<div class="col-xs-3">
<div class="progress" title="安装进度">
<div class="progress-bar progress-bar-info progress-bar-striped active" role="progressbar" aria-valuenow="{$progress}" aria-valuemin="0" aria-valuemax="100" style="width: {$progress}%;">
{$progress}%
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
安装步骤
</div>
<ul class="list-group">
<a href="javascript:;" class="list-group-item{$steps[0]}"><span class="glyphicon glyphicon-copyright-mark"></span> 许可协议</a>
<a href="javascript:;" class="list-group-item{$steps[1]}"><span class="glyphicon glyphicon-eye-open"></span> 环境监测</a>
<a href="javascript:;" class="list-group-item{$steps[2]}"><span class="glyphicon glyphicon-cog"></span> 参数配置</a>
<a href="javascript:;" class="list-group-item{$steps[3]}"><span class="glyphicon glyphicon-ok"></span> 成功</a>
</ul>
</div>
</div>
<div class="col-xs-9">
{$content}
</div>
</div>
<div class="footer" style="margin:15px auto;">
<div class="text-center">
<a target = "_blank" href="http://www.niushop.com.cn">Niushop开源商城官网</a> <a target = "_blank" href="http://www.niushop.com.cn/forummain.html">Niushop开源商城论坛</a> <a target = "_blank" href="http://www.niushop.com.cn/authorization.html">购买授权</a>
</div>
<div class="text-center">
Powered by <a target = "_blank" href="http://www.niushop.com.cn"><b>Niushop开源商城</b></a> niu_version © 2015-2025 <a target = "_blank" href="http://www.niushop.com.cn">www.niushop.com.cn</a>
</div>
</div>
</div>
<script src="./public/install/js/jquery.min.js"></script>
<script src="./public/install/js/bootstrap.min.js"></script>
</body>
</html>
EOF;
include "version.php";
$niu_version = NIU_VERSION;
$tpl=str_replace("niu_version", $niu_version, $tpl);
echo trim($tpl);
}
function tpl_install_license() {
echo <<<EOF
<div class="panel panel-default">
<div class="panel-heading">阅读许可协议</div>
<div class="panel-body" style="overflow-y:scroll;max-height:400px;line-height:20px;">
<h3>版权所有 (c)2016,Niushop开源商城团队保留所有权利。 </h3>
<p>