Conditions | 41 |
Paths | > 20000 |
Total Lines | 272 |
Code Lines | 171 |
Lines | 32 |
Ratio | 11.76 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
566 | public function startInstall(Array $DB = null) |
||
567 | { |
||
568 | $query = $uninst = $dbSetup = []; |
||
569 | |||
570 | // Check table prefix |
||
571 | $dbSetup['dbPrefix'] = $sqltblpre = PMF_Filter::filterInput(INPUT_POST, 'sqltblpre', FILTER_SANITIZE_STRING, ''); |
||
572 | if ('' !== $dbSetup['dbPrefix']) { |
||
573 | PMF_Db::setTablePrefix($dbSetup['dbPrefix']); |
||
574 | } |
||
575 | |||
576 | // Check database entries |
||
577 | $dbSetup['dbType'] = PMF_Filter::filterInput(INPUT_POST, 'sql_type', FILTER_SANITIZE_STRING); |
||
578 | if (!is_null($dbSetup['dbType'])) { |
||
579 | $dbSetup['dbType'] = trim($dbSetup['dbType']); |
||
580 | if (! file_exists(PMF_ROOT_DIR . '/setup/assets/sql/' . $dbSetup['dbType'] . '.sql.php')) { |
||
581 | printf( |
||
582 | '<p class="alert alert-danger"><strong>Error:</strong> Invalid server type: %s</p>', |
||
583 | $dbSetup['dbType'] |
||
584 | ); |
||
585 | PMF_System::renderFooter(true); |
||
586 | } |
||
587 | } else { |
||
588 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please select a database type.</p>\n"; |
||
589 | PMF_System::renderFooter(true); |
||
590 | } |
||
591 | |||
592 | $dbSetup['dbServer'] = PMF_Filter::filterInput(INPUT_POST, 'sql_server', FILTER_SANITIZE_STRING); |
||
593 | View Code Duplication | if (is_null($dbSetup['dbServer']) && ! PMF_System::isSqlite($dbSetup['dbType'])) { |
|
594 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database server.</p>\n"; |
||
595 | PMF_System::renderFooter(true); |
||
596 | } |
||
597 | |||
598 | $dbSetup['dbPort'] = PMF_Filter::filterInput(INPUT_POST, 'sql_port', FILTER_VALIDATE_INT); |
||
599 | View Code Duplication | if (is_null($dbSetup['dbPort']) && ! PMF_System::isSqlite($dbSetup['dbType'])) { |
|
600 | echo "<p class=\"alert alert-error\"><strong>Error:</strong> Please add a valid database port.</p>\n"; |
||
601 | PMF_System::renderFooter(true); |
||
602 | } |
||
603 | |||
604 | $dbSetup['dbUser'] = PMF_Filter::filterInput(INPUT_POST, 'sql_user', FILTER_SANITIZE_STRING); |
||
605 | View Code Duplication | if (is_null($dbSetup['dbUser']) && ! PMF_System::isSqlite($dbSetup['dbType'])) { |
|
606 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database username.</p>\n"; |
||
607 | PMF_System::renderFooter(true); |
||
608 | } |
||
609 | |||
610 | $dbSetup['dbPassword'] = PMF_Filter::filterInput(INPUT_POST, 'sql_passwort', FILTER_UNSAFE_RAW); |
||
611 | if (is_null($dbSetup['dbPassword']) && ! PMF_System::isSqlite($dbSetup['dbType'])) { |
||
612 | // Password can be empty... |
||
613 | $dbSetup['dbPassword'] = ''; |
||
614 | } |
||
615 | |||
616 | $dbSetup['dbDatabaseName'] = PMF_Filter::filterInput(INPUT_POST, 'sql_db', FILTER_SANITIZE_STRING); |
||
617 | View Code Duplication | if (is_null($dbSetup['dbDatabaseName']) && ! PMF_System::isSqlite($dbSetup['dbType'])) { |
|
618 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database name.</p>\n"; |
||
619 | PMF_System::renderFooter(true); |
||
620 | } |
||
621 | |||
622 | if (PMF_System::isSqlite($dbSetup['dbType'])) { |
||
623 | $dbSetup['dbServer'] = PMF_Filter::filterInput(INPUT_POST, 'sql_sqlitefile', FILTER_SANITIZE_STRING); |
||
624 | if (is_null($dbSetup['dbServer'])) { |
||
625 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a SQLite database filename.</p>\n"; |
||
626 | PMF_System::renderFooter(true); |
||
627 | } |
||
628 | } |
||
629 | |||
630 | // check database connection |
||
631 | PMF_Db::setTablePrefix($dbSetup['dbPrefix']); |
||
632 | $db = PMF_Db::factory($dbSetup['dbType']); |
||
633 | $db->connect($dbSetup['dbServer'], $dbSetup['dbUser'], $dbSetup['dbPassword'], $dbSetup['dbDatabaseName']); |
||
634 | if (!$db) { |
||
635 | printf("<p class=\"alert alert-danger\"><strong>DB Error:</strong> %s</p>\n", $db->error()); |
||
636 | PMF_System::renderFooter(true); |
||
637 | } |
||
638 | |||
639 | $configuration = new PMF_Configuration($db); |
||
640 | |||
641 | // check LDAP if available |
||
642 | $ldapEnabled = PMF_Filter::filterInput(INPUT_POST, 'ldap_enabled', FILTER_SANITIZE_STRING); |
||
643 | if (extension_loaded('ldap') && !is_null($ldapEnabled)) { |
||
644 | |||
645 | $ldapSetup = []; |
||
646 | |||
647 | // check LDAP entries |
||
648 | $ldapSetup['ldapServer'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_server', FILTER_SANITIZE_STRING); |
||
649 | if (is_null($ldapSetup['ldapServer'])) { |
||
650 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP server.</p>\n"; |
||
651 | PMF_System::renderFooter(true); |
||
652 | } |
||
653 | |||
654 | $ldapSetup['ldapPort'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_port', FILTER_VALIDATE_INT); |
||
655 | if (is_null($ldapSetup['ldapPort'])) { |
||
656 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP port.</p>\n"; |
||
657 | PMF_System::renderFooter(true); |
||
658 | } |
||
659 | |||
660 | $ldapSetup['ldapBase'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_base', FILTER_SANITIZE_STRING); |
||
661 | if (is_null($ldapSetup['ldapBase'])) { |
||
662 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP base search DN.</p>\n"; |
||
663 | PMF_System::renderFooter(true); |
||
664 | } |
||
665 | |||
666 | // LDAP User and LDAP password are optional |
||
667 | $ldapSetup['ldapUser'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_user', FILTER_SANITIZE_STRING, ''); |
||
668 | $ldapSetup['ldapPassword'] = PMF_Filter::filterInput(INPUT_POST, 'ldap_password', FILTER_SANITIZE_STRING, ''); |
||
669 | |||
670 | // check LDAP connection |
||
671 | require PMF_ROOT_DIR . "/inc/PMF/Ldap.php"; |
||
672 | $ldap = new PMF_Ldap($configuration); |
||
673 | $ldap->connect( |
||
674 | $ldapSetup['ldapServer'], |
||
675 | $ldapSetup['ldapPort'], |
||
676 | $ldapSetup['ldapBase'], |
||
677 | $ldapSetup['ldapUser'], |
||
678 | $ldapSetup['ldapPassword'] |
||
679 | ); |
||
680 | if (!$ldap) { |
||
681 | echo "<p class=\"alert alert-danger\"><strong>LDAP Error:</strong> " . $ldap->error() . "</p>\n"; |
||
682 | PMF_System::renderFooter(true); |
||
683 | } |
||
684 | } |
||
685 | |||
686 | // check loginname |
||
687 | $loginname = PMF_Filter::filterInput(INPUT_POST, 'loginname', FILTER_SANITIZE_STRING); |
||
688 | if (is_null($loginname)) { |
||
689 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a loginname for your account.</p>'; |
||
690 | PMF_System::renderFooter(true); |
||
691 | } |
||
692 | |||
693 | // check user entries |
||
694 | $password = PMF_Filter::filterInput(INPUT_POST, 'password', FILTER_SANITIZE_STRING); |
||
695 | if (is_null($password)) { |
||
696 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a password for the your account.</p>'; |
||
697 | PMF_System::renderFooter(true); |
||
698 | } |
||
699 | |||
700 | $password_retyped = PMF_Filter::filterInput(INPUT_POST, 'password_retyped', FILTER_SANITIZE_STRING); |
||
701 | if (is_null($password_retyped)) { |
||
702 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a retyped password.</p>'; |
||
703 | PMF_System::renderFooter(true); |
||
704 | } |
||
705 | |||
706 | if (strlen($password) <= 5 || strlen($password_retyped) <= 5) { |
||
707 | echo '<p class="alert alert-danger"><strong>Error:</strong> Your password and retyped password are too short.' . |
||
708 | ' Please set your password and your retyped password with a minimum of 6 characters.</p>'; |
||
709 | PMF_System::renderFooter(true); |
||
710 | } |
||
711 | if ($password != $password_retyped) { |
||
712 | echo '<p class="alert alert-danger"><strong>Error:</strong> Your password and retyped password are not equal.' . |
||
713 | ' Please check your password and your retyped password.</p>'; |
||
714 | PMF_System::renderFooter(true); |
||
715 | } |
||
716 | |||
717 | $language = PMF_Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_STRING, 'en'); |
||
718 | $realname = PMF_Filter::filterInput(INPUT_POST, 'realname', FILTER_SANITIZE_STRING, ''); |
||
719 | $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL, ''); |
||
720 | $permLevel = PMF_Filter::filterInput(INPUT_POST, 'permLevel', FILTER_SANITIZE_STRING, 'basic'); |
||
721 | |||
722 | $instanceSetup = new PMF_Instance_Setup(); |
||
723 | $instanceSetup->setRootDir(PMF_ROOT_DIR); |
||
724 | |||
725 | // Write the DB variables in database.php |
||
726 | if (! $instanceSetup->createDatabaseFile($dbSetup)) { |
||
727 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Setup cannot write to ./config/database.php.</p>"; |
||
728 | $this->_system->cleanInstallation(); |
||
729 | PMF_System::renderFooter(true); |
||
730 | } |
||
731 | |||
732 | // check LDAP if available |
||
733 | View Code Duplication | if (extension_loaded('ldap') && !is_null($ldapEnabled)) { |
|
734 | if (! $instanceSetup->createLdapFile($ldapSetup, '')) { |
||
735 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Setup cannot write to ./config/ldap.php.</p>"; |
||
736 | $this->_system->cleanInstallation(); |
||
737 | PMF_System::renderFooter(true); |
||
738 | } |
||
739 | } |
||
740 | |||
741 | // connect to the database using config/database.php |
||
742 | require PMF_ROOT_DIR . '/config/database.php'; |
||
743 | $db = PMF_Db::factory($dbSetup['dbType']); |
||
744 | $db->connect($DB['server'], $DB['user'], $DB['password'], $DB['db']); |
||
745 | if (!$db) { |
||
746 | echo "<p class=\"alert alert-danger\"><strong>DB Error:</strong> ".$db->error()."</p>\n"; |
||
747 | $this->_system->cleanInstallation(); |
||
748 | PMF_System::renderFooter(true); |
||
749 | } |
||
750 | |||
751 | require PMF_ROOT_DIR . '/setup/assets/sql/' . $dbSetup['dbType'] . '.sql.php'; // CREATE TABLES |
||
752 | require PMF_ROOT_DIR . '/setup/assets/sql/stopwords.sql.php'; // INSERTs for stopwords |
||
753 | |||
754 | $this->_system->setDatabase($db); |
||
755 | |||
756 | echo '<p>'; |
||
757 | |||
758 | // Erase any table before starting creating the required ones |
||
759 | if (! PMF_System::isSqlite($dbSetup['dbType'])) { |
||
760 | $this->_system->dropTables($uninst); |
||
761 | } |
||
762 | |||
763 | // Start creating the required tables |
||
764 | $count = 0; |
||
765 | foreach ($query as $executeQuery) { |
||
766 | $result = @$db->query($executeQuery); |
||
767 | if (!$result) { |
||
768 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please install your version of phpMyFAQ once again or send |
||
769 | us a <a href=\"http://www.phpmyfaq.de\" target=\"_blank\">bug report</a>.</p>'; |
||
770 | printf('<p class="alert alert-danger"><strong>DB error:</strong> %s</p>', $db->error()); |
||
771 | printf('<code>%s</code>', htmlentities($executeQuery)); |
||
772 | $this->_system->dropTables($uninst); |
||
773 | $this->_system->cleanInstallation(); |
||
774 | PMF_System::renderFooter(true); |
||
775 | } |
||
776 | usleep(2500); |
||
777 | $count++; |
||
778 | if (!($count % 10)) { |
||
779 | echo '| '; |
||
780 | } |
||
781 | } |
||
782 | |||
783 | $link = new PMF_Link(null, $configuration); |
||
784 | |||
785 | // add main configuration, add personal settings |
||
786 | $this->_mainConfig['main.metaPublisher'] = $realname; |
||
787 | $this->_mainConfig['main.administrationMail'] = $email; |
||
788 | $this->_mainConfig['main.language'] = $language; |
||
789 | $this->_mainConfig['security.permLevel'] = $permLevel; |
||
790 | |||
791 | foreach ($this->_mainConfig as $name => $value) { |
||
792 | $configuration->add($name, $value); |
||
793 | } |
||
794 | |||
795 | $configuration->update(array('main.referenceURL' => $link->getSystemUri('/setup/index.php'))); |
||
796 | $configuration->add('security.salt', md5($configuration->get('main.referenceURL'))); |
||
797 | |||
798 | // add admin account and rights |
||
799 | $admin = new PMF_User($configuration); |
||
800 | View Code Duplication | if (! $admin->createUser($loginname, $password, 1)) { |
|
801 | printf( |
||
802 | "<p class=\"alert alert-danger\"><strong>Fatal installation error:</strong><br>" . |
||
803 | "Couldn't create the admin user: %s</p>\n", |
||
804 | $admin->error() |
||
805 | ); |
||
806 | $this->_system->cleanInstallation(); |
||
807 | PMF_System::renderFooter(true); |
||
808 | } |
||
809 | $admin->setStatus('protected'); |
||
810 | $adminData = array( |
||
811 | 'display_name' => $realname, |
||
812 | 'email' => $email |
||
813 | ); |
||
814 | $admin->setUserData($adminData); |
||
815 | |||
816 | // add default rights |
||
817 | foreach ($this->_mainRights as $right) { |
||
818 | $admin->perm->grantUserRight(1, $admin->perm->addRight($right)); |
||
819 | } |
||
820 | |||
821 | // Add anonymous user account |
||
822 | $instanceSetup->createAnonymousUser($configuration); |
||
823 | |||
824 | // Add master instance |
||
825 | $instanceData = array( |
||
826 | 'url' => $link->getSystemUri($_SERVER['SCRIPT_NAME']), |
||
827 | 'instance' => $link->getSystemRelativeUri('setup/index.php'), |
||
828 | 'comment' => 'phpMyFAQ ' . PMF_System::getVersion() |
||
829 | ); |
||
830 | $faqInstance = new PMF_Instance($configuration); |
||
831 | $faqInstance->addInstance($instanceData); |
||
832 | |||
833 | $faqInstanceMaster = new PMF_Instance_Master($configuration); |
||
834 | $faqInstanceMaster->createMaster($faqInstance); |
||
835 | |||
836 | echo '</p>'; |
||
837 | } |
||
838 | |||
889 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.