-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwizard.php
2386 lines (2088 loc) · 95 KB
/
wizard.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
if (!defined( "PLOG_CLASS_PATH" )) {
define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
}
set_time_limit (5 * 3600);
//
// enable this for debugging purposes
//
define( "DB_WIZARD_DEBUG", false );
//
// in case you're having problems with time outs while upgrading (probably too
// many records) lower this figure
//
define( "WIZARD_MAX_RECORDS_PER_STEP", 75 );
//
// minimum php version required
//
define( "MIN_PHP_VERSION", "4.2.0" );
//
// whether data transformers should fail on error by default
// It might be convenient to set this to 'false' if we're running
// the wizard on top of an already updated installation
//
define( "DATABASE_DATA_TRANSFORMER_FAIL_ON_ERROR_DEFAULT", true );
// many hosts don't have this enabled and we, for the time being, need it...
ini_set("arg_seperator.output", "&");
include_once( PLOG_CLASS_PATH."class/bootstrap.php" );
lt_include( PLOG_CLASS_PATH."class/controller/controller.class.php" );
lt_include( PLOG_CLASS_PATH."class/template/templateservice.class.php" );
lt_include( PLOG_CLASS_PATH."class/action/action.class.php" );
lt_include( PLOG_CLASS_PATH."class/database/db.class.php" );
lt_include( PLOG_CLASS_PATH."class/template/template.class.php" );
lt_include( PLOG_CLASS_PATH."class/view/view.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/validator/usernamevalidator.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/validator/passwordvalidator.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
lt_include( PLOG_CLASS_PATH."class/misc/version.class.php" );
lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
lt_include( PLOG_CLASS_PATH."class/file/finder/filefinder.class.php" );
lt_include( PLOG_CLASS_PATH."class/gallery/resizers/gddetector.class.php" );
lt_include( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );
lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
lt_include( PLOG_CLASS_PATH."class/locale/ltlocales.class.php" );
lt_include( PLOG_CLASS_PATH."class/locale/ltlocalefinder.class.php" );
lt_include( PLOG_CLASS_PATH."class/template/templatesets/templatesets.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/mylinkscategories.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/blogcategories.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/globalarticlecategories.class.php" );
lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbums.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/permissions.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/permission.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userpermission.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
lt_include( PLOG_CLASS_PATH."class/misc/integritychecker.class.php" );
// table schemas
include( PLOG_CLASS_PATH."install/dbschemas.properties.php" );
// default configuration values for 1.1
include( PLOG_CLASS_PATH."install/defaultconfig.properties.php" );
define( "TEMP_FOLDER", "./tmp" );
// maps used to map requests with actions
$_actionMap["Checks"] = "WizardChecks";
$_actionMap["Default"] = "WizardChecks";
$_actionMap["Intro"] = "WizardIntro";
$_actionMap["Step1"] = "WizardStepOne";
$_actionMap["Step2"] = "WizardStepTwo";
$_actionMap["Step3"] = "WizardStepThree";
$_actionMap["Step4"] = "WizardStepFour";
$_actionMap["Step5"] = "WizardStepFive";
$_actionMap["Update1"] = "UpdateStepOne";
$_actionMap["Update2"] = "UpdateStepTwo";
$_actionMap["Update3"] = "UpdateStepThree";
$_actionMap["Fix120"] = "Fix120StepOne";
/**
* Open a connection to the database
*/
function connectDb( $ignoreError = false , $selectDatabase = true )
{
$config = new ConfigFileStorage();
// open a connection to the database
//$db = NewADOConnection('mysql');
$db = PDb::getDriver('mysql');
if ( $selectDatabase ) {
$res = $db->Connect($config->getValue( "db_host" ), $config->getValue( "db_username" ), $config->getValue( "db_password" ), $config->getValue( "db_database" ), $config->getValue( "db_character_set" ));
} else {
$res = $db->Connect($config->getValue( "db_host" ), $config->getValue( "db_username" ), $config->getValue( "db_password" ), null, $config->getValue( "db_character_set" ));
}
if( DB_WIZARD_DEBUG )
$db->debug = true;
// return error
if( $ignoreError )
return $db;
if( !$res )
return false;
return $db;
}
/**
* Returns the database prefix
*/
function getDbPrefix()
{
$config = new ConfigFileStorage();
return $config->getValue( "db_prefix" );
}
/**
* some useful little functions
*/
class WizardTools
{
/**
* returns true if plog has already been installed before or
* false otherwise
*/
function isNewInstallation()
{
$configFile = new ConfigFileStorage();
// if plog hasn't been installed, this file will have empty settings
if( $configFile->getValue( "db_host") == "" && $configFile->getValue( "db_username") == "" &&
$configFile->getValue( "db_database") == "" && $configFile->getValue( "db_prefix" ) == "" &&
$configFile->getValue( "db_password" ) == "" )
$isNew = true;
else
$isNew = false;
return( $isNew );
}
/**
* Clean up the default temporary folder
*/
function cleanTmpFolder()
{
// remove the files recursively, but only files, do not do anything to directories
File::deleteDir( TEMP_FOLDER, true, true, array(".svn", ".htaccess") );
}
}
/**
* Renders a template file.
*/
class WizardView extends View
{
var $_templateName;
function WizardView( $templateName )
{
$this->View();
$this->_templateName = $templateName;
}
function render()
{
// build the file name
$templateFileName = "wizard/".$this->_templateName.".template";
//$t = new Template( $templateFileName, "" );
$t = new Smarty();
$v = new Version();
$this->_params->setValue( "version", $v->getVersion());
$this->_params->setValue( "projectPage", $v->getProjectPage());
$this->_params->setValue( "safeMode", ini_get("safe_mode"));
$t->assign( $this->_params->getAsArray());
$t->template_dir = "./templates";
$t->compile_dir = TEMP_FOLDER;
$t->cache_dir = TEMP_FOLDER;
$t->use_sub_dirs = false;
$t->caching = false;
print $t->fetch( $templateFileName );
}
}
class WizardAction extends Action
{
function WizardAction( $actionInfo, $request )
{
$this->Action( $actionInfo, $request );
}
}
class WizardValidator
{
var $_desc;
var $_critical;
var $_valid;
var $_solution;
function WizardValidator( $desc = "", $solution = "", $critical = true )
{
$this->_desc = $desc;
$this->_critical = $critical;
$this->_valid = false;
$this->_solution = $solution;
}
function isCritical()
{
return( $this->_critical );
}
function getDesc()
{
return( $this->_desc );
}
function isValid()
{
return( $this->_valid );
}
function getSolution()
{
return( $this->_solution );
}
function validate()
{
return( $this->_valid );
}
}
class WizardPhpVersionValidator extends WizardValidator
{
function WizardPhpVersionValidator( $minVersion = MIN_PHP_VERSION )
{
$this->WizardValidator( "Checking if the installed <b>PHP</b> version is at least $minVersion",
"Please upgrade your version of PHP to $minVersion or newer",
true );
$this->_minVersion = $minVersion;
}
function validate()
{
$this->_valid = version_compare( phpversion(), $this->_minVersion ) >= 0;
return( parent::validate());
}
}
class WizardWritableFileValidator extends WizardValidator
{
var $_file;
function WizardWritableFileValidator( $file )
{
$this->WizardValidator( "Checking if file/folder <b>$file</b> is writable",
"Please make sure that the file is writable by the web server",
true );
$this->_file = $file;
}
function validate()
{
$this->_valid = File::isWritable( $this->_file );
return( parent::validate());
}
}
class WizardSessionFunctionsAvailableValidator extends WizardValidator
{
function WizardSessionFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>session</b> functions are available",
"LifeType requires support for sessions to be part of your PHP installation",
true );
}
function validate()
{
$this->_valid = function_exists( "session_start" ) &&
function_exists( "session_destroy" ) &&
function_exists( "session_cache_limiter" ) &&
function_exists( "session_name" ) &&
function_exists( "session_set_cookie_params" ) &&
function_exists( "session_save_path" );
return( parent::validate());
}
}
class WizardSessionSettingsValidator extends WizardValidator
{
function WizardSessionSettingsValidator()
{
$this->WizardValidator( "Checking if <b>session.auto_start</b> is disabled",
"LifeType can only run when session.auto_start is disabled.",
true );
}
function validate()
{
$this->_valid = (ini_get( "session.auto_start" ) == "0");
return( parent::validate());
}
}
class WizardMySQLFunctionsAvailableValidator extends WizardValidator
{
function WizardMySQLFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>MySQL</b> functions are available",
"LifeType requires support for MySQL to be part of your PHP installation",
true );
}
function validate()
{
$this->_valid = function_exists( "mysql_select_db" ) &&
function_exists( "mysql_query" ) &&
function_exists( "mysql_connect" ) &&
function_exists( "mysql_fetch_assoc" ) &&
function_exists( "mysql_num_rows" ) &&
function_exists( "mysql_free_result" );
return( parent::validate());
}
}
class WizardXmlFunctionsAvailableValidator extends WizardValidator
{
function WizardXmlFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>XML</b> functions are available",
"LifeType requires support for XML to be part of your PHP installation",
true );
}
function validate()
{
$this->_valid = function_exists( "xml_set_object" ) &&
function_exists( "xml_set_element_handler" ) &&
function_exists( "xml_parser_create" ) &&
function_exists( "xml_parser_set_option" ) &&
function_exists( "xml_parse" ) &&
function_exists( "xml_parser_free" );
return( parent::validate());
}
}
class WizardTokenizerFunctionsAvailableValidator extends WizardValidator
{
function WizardTokenizerFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>Tokenizer</b> functions are available",
"LifeType requires support for the Tokenizer to be part of your PHP installation",
true );
}
function validate()
{
$this->_valid = function_exists( "token_get_all" );
return( parent::validate());
}
}
class WizardSafeModeValidator extends WizardValidator
{
function WizardSafeModeValidator()
{
$this->WizardValidator( "Checking if <b>safe mode</b> is disabled",
"LifeType can run when PHP's safe mode is enabled, but it may cause some problems.",
false );
}
function validate()
{
$this->_valid = (ini_get( "safe_mode" ) == "");
return( parent::validate());
}
}
class WizardIconvFunctionsAvailableValidator extends WizardValidator
{
function WizardIconvFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>iconv</b> functions are available",
"LifeType requires support for some resource metadata conversion and some LifeType plugins requires support for multi-byte language encoding/decoding.",
false );
}
function validate()
{
$this->_valid = function_exists( "iconv" );
return( parent::validate());
}
}
class WizardMbstringFunctionsAvailableValidator extends WizardValidator
{
function WizardMbstringFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>mbstring</b> functions are available",
"Some LifeType plugins requires support for multi-byte language encoding/decoding.",
false );
}
function validate()
{
$this->_valid = function_exists( "mb_convert_encoding" );
return( parent::validate());
}
}
class WizardGdFunctionsAvailableValidator extends WizardValidator
{
function WizardGdFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>gd</b> or <b>gd2</b> functions are available",
"LifeType requires support for generating image thumbnail.",
false );
}
function validate()
{
$this->_valid = function_exists( "imagecopyresampled" ) &&
function_exists( "imagecopyresized" );
return( parent::validate());
}
}
class WizardFileUploadsValidator extends WizardValidator
{
function WizardFileUploadsValidator()
{
$this->WizardValidator( "Checking if <b>file_uploads</b> is enabled",
"LifeType requires support for uploading resources.",
true );
}
function validate()
{
$this->_valid = (ini_get( "file_uploads" ) == 1);
return( parent::validate());
}
}
class WizardFileIntegrityValidator extends WizardValidator
{
function WizardFileIntegrityValidator()
{
$this->WizardValidator( "Checking that all files have been correctly uploaded",
"will be set later on...",
false );
}
function validate()
{
include( PLOG_CLASS_PATH."install/files.properties.php");
$result = IntegrityChecker::checkIntegrity(
$data
);
$this->_valid = ( count( $result ) == 0 );
if( !$this->_valid ) {
/* let's modify a private attribute... */
$fileList = implode( "<br/>", array_keys( $result ));
$this->_solution = "The current version of the following is not the expected one. Installation can proceed but please make sure that all files were uploaded correctly:"."<br/>".$fileList;
}
return( parent::validate());
}
}
class WizardCtypeFunctionsAvailableValidator extends WizardValidator
{
function WizardCtypeFunctionsAvailableValidator()
{
$this->WizardValidator( "Checking if <b>ctype</b> functions are available",
"Some LifeType plugins requires support for variable type validation.",
false );
}
function validate()
{
$this->_valid = function_exists( "ctype_digit" );
return( parent::validate());
}
}
class WizardChecks extends WizardAction
{
function perform()
{
// build the array with checks
$checkGroups['File checks'] = Array(
"writeConfigFile" => new WizardWritableFileValidator( "config/config.properties.php" ),
"writeTmpFolder" => new WizardWritableFileValidator( "tmp" ),
"writeGalleryFolder" => new WizardWritableFileValidator( "gallery" ),
"fileVersionCheck" => new WizardFileIntegrityValidator()
);
$checkGroups['PHP version checking'] = Array(
"php" => new WizardPhpVersionValidator()
);
$checkGroups['PHP configuration checking'] = Array(
"sessionSettings" => new WizardSessionSettingsValidator(),
"safemode" => new WizardSafeModeValidator(),
"fileUploads" => new WizardFileUploadsValidator()
);
$checkGroups['PHP functions availability checking'] = Array(
"sessions" => new WizardSessionFunctionsAvailableValidator(),
"mysql" => new WizardMySQLFunctionsAvailableValidator(),
"xml" => new WizardXmlFunctionsAvailableValidator(),
"tokenizer" => new WizardTokenizerFunctionsAvailableValidator(),
"iconv" => new WizardIconvFunctionsAvailableValidator(),
"mbstring" => new WizardMbstringFunctionsAvailableValidator(),
"gd" => new WizardGdFunctionsAvailableValidator(),
"ctype" => new WizardCtypeFunctionsAvailableValidator()
);
// run the checks
$ok = true;
foreach( $checkGroups as $checkGroup => $checks ) {
foreach( $checks as $id => $check ) {
$valid = $checkGroups[$checkGroup][$id]->validate();
// if it doesn't validate but it's not critical, then we can proced too
if( !$checkGroups[$checkGroup][$id]->isCritical())
$valid = true;
$ok = ($ok && $valid);
}
}
// create the view and pass the results
$this->_view = new WizardView( "checks" );
$this->_view->setValue( "ok", $ok );
$this->_view->setValue( "checkGroups", $checkGroups );
if( WizardTools::isNewInstallation())
$this->_view->setValue( "mode", "install" );
else
$this->_view->setValue( "mode", "update" );
return true;
}
}
class WizardPagedAction extends WizardAction
{
var $willRefresh;
function WizardPagedAction( $actionInfo, $request )
{
$this->WizardAction( $actionInfo, $request );
$this->willRefresh = false;
}
/**
* @private
*/
function getPageFromRequest()
{
lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php");
// get the value from the request
$page = HttpVars::getRequestValue( "page" );
// but first of all, validate it
$val = new IntegerValidator();
if( !$val->validate( $page ))
$page = 1;
return $page;
}
/**
* @private
*/
function willRefresh()
{
return( $this->willRefresh );
}
}
/**
* Gets the information about the database from the user.
*/
class WizardIntro extends WizardAction
{
function WizardIntro( $actionInfo, $request )
{
$this->WizardAction( $actionInfo, $request );
}
function perform()
{
WizardTools::cleanTmpFolder();
// we can detect whether plog is already installed or not and direct users to the right
// place
if( WizardTools::isNewInstallation())
$this->_view = new WizardView( "intro" );
else {
Controller::setForwardAction( "Update1" );
return false;
}
$this->setCommonData();
return true;
}
}
/**
*
* Saves data to the configuration file
*
*/
class WizardStepOne extends WizardAction
{
var $_dbServer;
var $_dbUser;
var $_dbPassword;
var $_dbName;
var $_dbPrefix;
var $_connection;
function WizardStepOne( $actionInfo, $request )
{
$this->WizardAction( $actionInfo, $request );
// data validation
$this->registerFieldValidator( "dbServer", new StringValidator());
$this->registerFieldValidator( "dbUser", new StringValidator());
$this->registerFieldValidator( "dbPassword", new StringValidator(), true );
$this->registerFieldValidator( "dbName", new StringValidator());
$this->registerFieldValidator( "dbPrefix", new StringValidator(), true );
$errorView = new WizardView( "intro" );
$errorView->setErrorMessage( "Some data was incorrect or missing." );
$this->setValidationErrorView( $errorView );
}
function perform()
{
// fetch the data needed from the request
$this->_dbServer = $this->_request->getValue( "dbServer" );
$this->_dbUser = $this->_request->getValue( "dbUser" );
$this->_dbPassword = $this->_request->getValue( "dbPassword" );
$this->_dbName = $this->_request->getValue( "dbName" );
$this->_skipThis = $this->_request->getValue( "skipDbInfo" );
$this->_dbPrefix = $this->_request->getValue( "dbPrefix", DEFAULT_DB_PREFIX );
// we should now save the data to the configuration file, just before
// we read it
$configFile = new ConfigFileStorage();
// we expect everything to be fine
$errors = false;
// before doing anything, we should check of the configuration file is
// writable by this script, or else, throw an error and bail out gracefully
$configFileName = $configFile->getConfigFileName();
if( !File::exists( $configFileName )) {
if (! File::touch( $configFileName ) ) {
$this->_view = new WizardView( "intro" );
$message = "Could not create the LifeType configuration file $configFileName. Please make sure
that the file can be created by the user running the webserver. It is needed to
store the database configuration settings.";
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
} else {
ConfigFileStorage::createConfigFile( $configFileName );
}
}
if( File::exists( $configFileName ) && !File::isWritable( $configFileName )) {
$this->_view = new WizardView( "intro" );
$message = "Please make sure that the file $configFileName can be written by this script during
the installation process. It is needed to store the database configuration settings. Once the
installation is complete, please revert the permissions to no writing possible.";
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
}
// continue if everything went fine
if( !$configFile->saveValue( "db_username", $this->_dbUser ) ||
!$configFile->saveValue( "db_password", $this->_dbPassword ) ||
!$configFile->saveValue( "db_host", $this->_dbServer ) ||
!$configFile->saveValue( "db_database", $this->_dbName ) ||
!$configFile->saveValue( "db_prefix", $this->_dbPrefix )) {
$errors = true;
}
if( $errors ) {
$message = "Could not save values to the configuration file. Please make sure it is available and
that has write permissions for the user under your web server is running.";
$this->_view = new WizardView( "intro" );
$this->_view->setErrorMessage( $message );
return( false );
}
else {
$connectionEsablished = false;
$this->_connection = @mysql_connect( $this->_dbServer, $this->_dbUser, $this->_dbPassword );
if( $this->_connection ) {
$connectionEsablished = true;
} else {
$connectionEsablished = false;
$message = "There was an error connecting to the database. Please check your settings.";
}
if ( !$connectionEsablished ) {
$this->_view = new WizardView( "step1" );
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
} else {
$this->_view = new WizardView( "step1" );
$availableCharacterSets = $this->getAvailableCharacterSets();
$defaultCharacterSet = $this->getDatabaseCharacterSet();
$createDatabase = false;
if( empty( $defaultCharacterSet ) )
{
$defaultCharacterSet = $this->getServerCharacterSet();
$createDatabase = true;
}
$this->_view->setValue( "availableCharacterSets", $availableCharacterSets );
$this->_view->setValue( "defaultCharacterSet", $defaultCharacterSet );
$this->_view->setValue( "createDatabase", $createDatabase );
// now we better read the information from the config file to make sure that
// it has been correctly saved
$this->setCommonData( true );
return true;
}
}
}
function getAvailableCharacterSets()
{
// check mysql version first. Version lower than 4.1 doesn't support utf8
$serverVersion = mysql_get_server_info( $this->_connection );
$version = explode( '.', $serverVersion );
if ( $version[0] < 4 ) return false;
if ( ( $version[0] == 4 ) && ( $version[1] == 0 ) ) return false;
// check if utf8 support was compiled in
$result = mysql_query( "SHOW CHARACTER SET", $this->_connection );
if( $result )
{
if( mysql_num_rows($result) > 0 ) {
// iterate through resultset
$availableCharacterSets = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
array_push( $availableCharacterSets, $row['Charset'] );
}
return $availableCharacterSets;
}
}
return false;
}
function getDatabaseCharacterSet()
{
if( !@mysql_select_db( $this->_dbName, $this->_connection ) ) {
return false;
}
// We use a SHOW CREATE DATABASE command to show the original
// SQL character set when DB was created.
$result = mysql_query( "SHOW CREATE DATABASE `".$this->_dbName."`", $this->_connection );
if( $result )
{
if( mysql_num_rows( $result ) < 0 ) {
// The specified db name is wrong!
return false;
}
$dbInfo = mysql_fetch_row( $result );
$pattern = '/40100 DEFAULT CHARACTER SET (\w+) /';
if( ( preg_match( $pattern, $dbInfo[1], $match ) > 0 ) ) {
return $match[1];
}
}
return false;
}
function getServerCharacterSet(){
// We use a SHOW CREATE DATABASE command to show the original
// SQL character set when DB was created.
$result = mysql_query( "SHOW VARIABLES LIKE 'character_set_server'", $this->_connection );
if( $result )
{
if( mysql_num_rows( $result ) > 0 ) {
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
return $row['Value'];
}
}
return false;
}
}
/**
*
* Second step where we connect to the database and create the tables.
*
*/
class WizardStepTwo extends WizardAction
{
var $_db;
var $_database;
var $_dbCharacterSet;
var $_createDatabase;
function setDbConfigValues( &$view )
{
$configFile = new ConfigFileStorage();
$configFile->reload();
$view->setValue( "dbUser", $configFile->getValue( "db_username" ));
$view->setValue( "dbPassword", $configFile->getValue( "db_password" ));
$view->setValue( "dbServer", $configFile->getValue( "db_host" ));
$view->setValue( "dbName", $configFile->getValue( "db_database" ));
$view->setValue( "dbPrefix", $configFile->getValue( "db_prefix" ));
$view->setValue( "dbCharacterSet", $configFile->getValue( "db_character_set" ));
return true;
}
function perform()
{
global $Tables;
global $Inserts;
$this->_dbCharacterSet = $this->_request->getValue( "dbCharacterSet" );
$configFile = new ConfigFileStorage();
$configFileName = $configFile->getConfigFileName();
if( File::exists( $configFileName ) && !File::isWritable( $configFileName )) {
$this->_view = new WizardView( "step1" );
$message = "Please make sure that the file $configFileName can be written by this script during
the installation process. It is needed to store the database configuration settings. Once the
installation is complete, please revert the permissions to no writing possible.";
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
}
// continue if everything went fine
if( !$configFile->saveValue( "db_character_set", $this->_dbCharacterSet ) ) {
$message = "Could not save values to the configuration file. Please make sure it is available and
that has write permissions for the user under your web server is running.";
$this->_view = new WizardView( "step1" );
$this->_view->setErrorMessage( $message );
return false;
}
$createDb = $this->_request->getValue( "createDatabase" );
$message = '';
// only check for errors in case the database table should already exist!
if( !$createDb ) {
$connectionEsablished = false;
// Lets check the 'everything is fine' case first..
$this->_db = connectDb();
if( $this->_db ) {
$connectionEsablished = true;
} else {
$connectionEsablished = false;
$message = "There was an error selecting the database. Please verify the database was already created or check the 'Create database' checkbox.";
}
// We were unable to connect to the db and select the right db.. lets try
// just to connect.. maybe the database needs to be created (even though the
// user did not check the appropriate box).
if ( !$connectionEsablished ) {
$this->_db = connectDb( true, false );
if( !$this->_db ) {
$message = "There was an error connecting to the database. Please check your settings.";
}
}
if ( !$connectionEsablished ) {
$this->_view = new WizardView( "step1" );
$this->setDbConfigValues( $this->_view );
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
}
}
$config = new ConfigFileStorage();
$this->_database = $config->getValue( "db_database" );
$this->_dbPrefix = $config->getValue( "db_prefix" );
// create the database
if( $createDb ) {
$this->_db = connectDb( false, false );
if( !$this->_db ) {
$this->_view = new WizardView( "step1" );
$this->setDbConfigValues( $this->_view );
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
}
if( !$this->_db->Execute( "CREATE DATABASE ".$this->_database )) {
$message = "Error creating the database: ".$this->_db->ErrorMsg();
$message .= "<br/><br/>If the database already exists, go back to Step 2 and use a new database name.";
$this->_view = new WizardView( "step1" );
$this->setDbConfigValues( $this->_view );
$this->_view->setErrorMessage( $message );
$this->setCommonData( true );
return false;
} else {
$message = "Database created successfully.<br/>";
}
}
// reconnect using the new database.
$config = new ConfigFileStorage();
$this->_db->Connect( $config->getValue( "db_host" ),
$config->getValue( "db_username" ),
$config->getValue( "db_password" ),
$config->getValue( "db_database" ));
// create a data dictionary to give us the right sql code needed to create the tables
$dict = NewPDbDataDictionary( $this->_db );
// create the tables
$errors = false;
foreach( $Tables as $name => $table ) {
$upperName = $dict->upperName;
$tableSchema = $table["schema"];
if ( isset( $table["options"] ) )
{
$tableOptions = $table["options"];
$options = array ( $upperName => $tableOptions );
} else {
$options = array ();
}
$sqlarray = $dict->CreateTableSQL( $this->_dbPrefix.$name, $tableSchema, $options );
// each table may need more than one sql query because of indexes, triggers, etc...
$ok = true;
foreach( $sqlarray as $sql ) {
$ok = ( $ok && $this->_db->Execute( $sql ));
}
if( $ok )
$message .= "Table <strong>$name</strong> created successfully.<br/>";
else {
$message .= "Error creating table $name: ".$this->_db->ErrorMsg()."<br/>";
$errors = true;
}
}
if( $errors ) {
$message = "There was an error creating the tables in the database. Please make sure that the user chosen to connect to the database has enough permissions to create tables.<br/><br/>$message";
$this->_view = new WizardView( "step1" );
$this->_view->setErrorMessage( $message );
$this->setDbConfigValues( $this->_view );
$this->setCommonData();
return false;
}
// try to guess the url where plog is running
$httpProtocol = (array_key_exists("HTTPS", $_SERVER) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$httpHost = $_SERVER["HTTP_HOST"];
$requestUrl = $_SERVER["REQUEST_URI"];
$requestUrl = str_replace( "/wizard.php", "", $requestUrl );
$plogUrl = $httpProtocol.$httpHost.$requestUrl;
// Find some of the tools we are going to need (last one is for os x, with fink installed)
// TBD: support for Windows specific directories
$folders = Array( "/bin/", "/usr/bin/", "/usr/local/bin/", "/sw/bin/" );
$finder = new FileFinder();
$pathToUnzip = $finder->findBinary( "unzip", $folders );