Conditions | 55 |
Paths | > 20000 |
Total Lines | 376 |
Lines | 28 |
Ratio | 7.45 % |
Changes | 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 |
||
670 | public function startInstall(array $setup = null) |
||
671 | { |
||
672 | $query = $uninst = $dbSetup = []; |
||
673 | |||
674 | // Check table prefix |
||
675 | $dbSetup['dbPrefix'] = Filter::filterInput(INPUT_POST, 'sqltblpre', FILTER_SANITIZE_STRING, ''); |
||
676 | if ('' !== $dbSetup['dbPrefix']) { |
||
677 | Db::setTablePrefix($dbSetup['dbPrefix']); |
||
678 | } |
||
679 | |||
680 | // Check database entries |
||
681 | $dbSetup['dbType'] = Filter::filterInput(INPUT_POST, 'sql_type', FILTER_SANITIZE_STRING, $setup['dbType']); |
||
682 | if (!is_null($dbSetup['dbType'])) { |
||
683 | $dbSetup['dbType'] = trim($dbSetup['dbType']); |
||
684 | if (!file_exists(PMF_SRC_DIR.'/phpMyFAQ/Instance/Database/'.ucfirst($dbSetup['dbType']).'.php')) { |
||
685 | printf( |
||
686 | '<p class="alert alert-danger"><strong>Error:</strong> Invalid server type: %s</p>', |
||
687 | $dbSetup['dbType'] |
||
688 | ); |
||
689 | System::renderFooter(true); |
||
690 | } |
||
691 | } else { |
||
692 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please select a database type.</p>\n"; |
||
693 | System::renderFooter(true); |
||
694 | } |
||
695 | |||
696 | $dbSetup['dbServer'] = Filter::filterInput(INPUT_POST, 'sql_server', FILTER_SANITIZE_STRING, ''); |
||
697 | View Code Duplication | if (is_null($dbSetup['dbServer']) && !System::isSqlite($dbSetup['dbType'])) { |
|
698 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database server.</p>\n"; |
||
699 | System::renderFooter(true); |
||
700 | } |
||
701 | |||
702 | $dbSetup['dbUser'] = Filter::filterInput(INPUT_POST, 'sql_user', FILTER_SANITIZE_STRING, ''); |
||
703 | View Code Duplication | if (is_null($dbSetup['dbUser']) && !System::isSqlite($dbSetup['dbType'])) { |
|
704 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database username.</p>\n"; |
||
705 | System::renderFooter(true); |
||
706 | } |
||
707 | |||
708 | $dbSetup['dbPassword'] = Filter::filterInput(INPUT_POST, 'sql_password', FILTER_UNSAFE_RAW, ''); |
||
709 | if (is_null($dbSetup['dbPassword']) && !System::isSqlite($dbSetup['dbType'])) { |
||
710 | // Password can be empty... |
||
711 | $dbSetup['dbPassword'] = ''; |
||
712 | } |
||
713 | |||
714 | $dbSetup['dbDatabaseName'] = Filter::filterInput(INPUT_POST, 'sql_db', FILTER_SANITIZE_STRING); |
||
715 | View Code Duplication | if (is_null($dbSetup['dbDatabaseName']) && !System::isSqlite($dbSetup['dbType'])) { |
|
716 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a database name.</p>\n"; |
||
717 | System::renderFooter(true); |
||
718 | } |
||
719 | |||
720 | if (System::isSqlite($dbSetup['dbType'])) { |
||
721 | $dbSetup['dbServer'] = Filter::filterInput( |
||
722 | INPUT_POST, |
||
723 | 'sql_sqlitefile', |
||
724 | FILTER_SANITIZE_STRING, |
||
725 | $setup['dbServer'] |
||
726 | ); |
||
727 | if (is_null($dbSetup['dbServer'])) { |
||
728 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a SQLite database filename.</p>\n"; |
||
729 | System::renderFooter(true); |
||
730 | } |
||
731 | } |
||
732 | |||
733 | // check database connection |
||
734 | Db::setTablePrefix($dbSetup['dbPrefix']); |
||
735 | $db = Db::factory($dbSetup['dbType']); |
||
736 | try { |
||
737 | $db->connect($dbSetup['dbServer'], $dbSetup['dbUser'], $dbSetup['dbPassword'], $dbSetup['dbDatabaseName']); |
||
738 | } catch (Exception $e) { |
||
739 | printf("<p class=\"alert alert-danger\"><strong>DB Error:</strong> %s</p>\n", $e->getMessage()); |
||
740 | } |
||
741 | if (!$db) { |
||
742 | System::renderFooter(true); |
||
743 | } |
||
744 | |||
745 | $configuration = new Configuration($db); |
||
746 | |||
747 | // |
||
748 | // Check LDAP if enabled |
||
749 | // |
||
750 | $ldapEnabled = Filter::filterInput(INPUT_POST, 'ldap_enabled', FILTER_SANITIZE_STRING); |
||
751 | if (extension_loaded('ldap') && !is_null($ldapEnabled)) { |
||
752 | $ldapSetup = []; |
||
753 | |||
754 | // check LDAP entries |
||
755 | $ldapSetup['ldapServer'] = Filter::filterInput(INPUT_POST, 'ldap_server', FILTER_SANITIZE_STRING); |
||
756 | if (is_null($ldapSetup['ldapServer'])) { |
||
757 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP server.</p>\n"; |
||
758 | System::renderFooter(true); |
||
759 | } |
||
760 | |||
761 | $ldapSetup['ldapPort'] = Filter::filterInput(INPUT_POST, 'ldap_port', FILTER_VALIDATE_INT); |
||
762 | if (is_null($ldapSetup['ldapPort'])) { |
||
763 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP port.</p>\n"; |
||
764 | System::renderFooter(true); |
||
765 | } |
||
766 | |||
767 | $ldapSetup['ldapBase'] = Filter::filterInput(INPUT_POST, 'ldap_base', FILTER_SANITIZE_STRING); |
||
768 | if (is_null($ldapSetup['ldapBase'])) { |
||
769 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add a LDAP base search DN.</p>\n"; |
||
770 | System::renderFooter(true); |
||
771 | } |
||
772 | |||
773 | // LDAP User and LDAP password are optional |
||
774 | $ldapSetup['ldapUser'] = Filter::filterInput(INPUT_POST, 'ldap_user', FILTER_SANITIZE_STRING, ''); |
||
775 | $ldapSetup['ldapPassword'] = Filter::filterInput(INPUT_POST, 'ldap_password', FILTER_SANITIZE_STRING, ''); |
||
776 | |||
777 | // set LDAP Config to prevent DB query |
||
778 | foreach ($this->_mainConfig as $configKey => $configValue) { |
||
779 | if (strpos($configKey, 'ldap.') !== false) { |
||
780 | $configuration->config[$configKey] = $configValue; |
||
781 | } |
||
782 | } |
||
783 | |||
784 | // check LDAP connection |
||
785 | $ldap = new Ldap($configuration); |
||
786 | $ldap->connect( |
||
787 | $ldapSetup['ldapServer'], |
||
788 | $ldapSetup['ldapPort'], |
||
789 | $ldapSetup['ldapBase'], |
||
790 | $ldapSetup['ldapUser'], |
||
791 | $ldapSetup['ldapPassword'] |
||
792 | ); |
||
793 | if (!$ldap) { |
||
794 | echo '<p class="alert alert-danger"><strong>LDAP Error:</strong> '.$ldap->error()."</p>\n"; |
||
795 | System::renderFooter(true); |
||
796 | } |
||
797 | } |
||
798 | |||
799 | // |
||
800 | // Check Elasticsearch if enabled |
||
801 | // |
||
802 | $esEnabled = Filter::filterInput(INPUT_POST, 'elasticsearch_enabled', FILTER_SANITIZE_STRING); |
||
803 | if (!is_null($esEnabled)) { |
||
804 | $esSetup = []; |
||
805 | $esHostFilter = [ |
||
806 | 'elasticsearch_server' => [ |
||
807 | 'filter' => FILTER_SANITIZE_STRING, |
||
808 | 'flags' => FILTER_REQUIRE_ARRAY |
||
809 | ] |
||
810 | ]; |
||
811 | |||
812 | // ES hosts |
||
813 | $esHosts = Filter::filterInputArray(INPUT_POST, $esHostFilter); |
||
814 | if (is_null($esHosts)) { |
||
815 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add at least one Elasticsearch host.</p>\n"; |
||
816 | System::renderFooter(true); |
||
817 | } |
||
818 | |||
819 | $esSetup['hosts'] = $esHosts['elasticsearch_server']; |
||
820 | |||
821 | // ES Index name |
||
822 | $esSetup['index'] = Filter::filterInput(INPUT_POST, 'elasticsearch_index', FILTER_SANITIZE_STRING); |
||
823 | if (is_null($esSetup['index'])) { |
||
824 | echo "<p class=\"alert alert-danger\"><strong>Error:</strong> Please add an Elasticsearch index name.</p>\n"; |
||
825 | System::renderFooter(true); |
||
826 | } |
||
827 | |||
828 | $psr4Loader = new ClassLoader(); |
||
829 | $psr4Loader->addPsr4('Elasticsearch\\', PMF_SRC_DIR.'/libs/elasticsearch/src/Elasticsearch'); |
||
830 | $psr4Loader->addPsr4('GuzzleHttp\\Ring\\', PMF_SRC_DIR.'/libs/guzzlehttp/ringphp/src'); |
||
831 | $psr4Loader->addPsr4('Monolog\\', PMF_SRC_DIR.'/libs/monolog/src/Monolog'); |
||
832 | $psr4Loader->addPsr4('Psr\\', PMF_SRC_DIR.'/libs/psr/log/Psr'); |
||
833 | $psr4Loader->addPsr4('React\\Promise\\', PMF_SRC_DIR.'/libs/react/promise/src'); |
||
834 | $psr4Loader->register(); |
||
835 | |||
836 | // check LDAP connection |
||
837 | $esHosts = array_values($esHosts['elasticsearch_server']); |
||
838 | $esClient = ClientBuilder::create() |
||
839 | ->setHosts($esHosts) |
||
840 | ->build(); |
||
841 | |||
842 | if (!$esClient) { |
||
843 | echo '<p class="alert alert-danger"><strong>Elasticsearch Error:</strong> No connection.</p>'; |
||
844 | System::renderFooter(true); |
||
845 | } |
||
846 | } else { |
||
847 | $esSetup = []; |
||
848 | } |
||
849 | |||
850 | // check loginname |
||
851 | $loginname = Filter::filterInput(INPUT_POST, 'loginname', FILTER_SANITIZE_STRING, $setup['loginname']); |
||
852 | if (is_null($loginname)) { |
||
853 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a loginname for your account.</p>'; |
||
854 | System::renderFooter(true); |
||
855 | } |
||
856 | |||
857 | // check user entries |
||
858 | $password = Filter::filterInput(INPUT_POST, 'password', FILTER_SANITIZE_STRING, $setup['password']); |
||
859 | if (is_null($password)) { |
||
860 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a password for the your account.</p>'; |
||
861 | System::renderFooter(true); |
||
862 | } |
||
863 | |||
864 | $password_retyped = Filter::filterInput( |
||
865 | INPUT_POST, |
||
866 | 'password_retyped', |
||
867 | FILTER_SANITIZE_STRING, |
||
868 | $setup['password_retyped'] |
||
869 | ); |
||
870 | if (is_null($password_retyped)) { |
||
871 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please add a retyped password.</p>'; |
||
872 | System::renderFooter(true); |
||
873 | } |
||
874 | |||
875 | if (strlen($password) <= 5 || strlen($password_retyped) <= 5) { |
||
876 | echo '<p class="alert alert-danger"><strong>Error:</strong> Your password and retyped password are too short.'. |
||
877 | ' Please set your password and your retyped password with a minimum of 6 characters.</p>'; |
||
878 | System::renderFooter(true); |
||
879 | } |
||
880 | if ($password != $password_retyped) { |
||
881 | echo '<p class="alert alert-danger"><strong>Error:</strong> Your password and retyped password are not equal.'. |
||
882 | ' Please check your password and your retyped password.</p>'; |
||
883 | System::renderFooter(true); |
||
884 | } |
||
885 | |||
886 | $language = Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_STRING, 'en'); |
||
887 | $realname = Filter::filterInput(INPUT_POST, 'realname', FILTER_SANITIZE_STRING, ''); |
||
888 | $email = Filter::filterInput(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL, ''); |
||
889 | $permLevel = Filter::filterInput(INPUT_POST, 'permLevel', FILTER_SANITIZE_STRING, 'basic'); |
||
890 | |||
891 | $rootDir = isset($setup['rootDir']) ? $setup['rootDir'] : PMF_ROOT_DIR; |
||
892 | |||
893 | $instanceSetup = new Setup(); |
||
894 | $instanceSetup->setRootDir($rootDir); |
||
895 | |||
896 | // Write the DB variables in database.php |
||
897 | if (!$instanceSetup->createDatabaseFile($dbSetup)) { |
||
898 | echo '<p class="alert alert-danger"><strong>Error:</strong> Setup cannot write to ./config/database.php.</p>'; |
||
899 | $this->system->cleanInstallation(); |
||
900 | System::renderFooter(true); |
||
901 | } |
||
902 | |||
903 | // check LDAP is enabled |
||
904 | if (extension_loaded('ldap') && !is_null($ldapEnabled) && count($ldapSetup)) { |
||
905 | if (!$instanceSetup->createLdapFile($ldapSetup, '')) { |
||
906 | echo '<p class="alert alert-danger"><strong>Error:</strong> Setup cannot write to ./config/ldap.php.</p>'; |
||
907 | $this->system->cleanInstallation(); |
||
908 | System::renderFooter(true); |
||
909 | } |
||
910 | } |
||
911 | |||
912 | // check if Elasticsearch is enabled |
||
913 | View Code Duplication | if (!is_null($esEnabled) && count($esSetup)) { |
|
914 | if (!$instanceSetup->createElasticsearchFile($esSetup, '')) { |
||
915 | echo '<p class="alert alert-danger"><strong>Error:</strong> Setup cannot write to ./config/elasticsearch.php.</p>'; |
||
916 | $this->system->cleanInstallation(); |
||
917 | System::renderFooter(true); |
||
918 | } |
||
919 | } |
||
920 | |||
921 | // connect to the database using config/database.php |
||
922 | require $rootDir.'/config/database.php'; |
||
923 | try { |
||
924 | $db = Db::factory($dbSetup['dbType']); |
||
925 | } catch (Exception $exception) { |
||
926 | printf("<p class=\"alert alert-danger\"><strong>DB Error:</strong> %s</p>\n", $exception->getMessage()); |
||
927 | $this->system->cleanInstallation(); |
||
928 | System::renderFooter(true); |
||
929 | } |
||
930 | |||
931 | $db->connect($DB['server'], $DB['user'], $DB['password'], $DB['db']); |
||
932 | if (!$db) { |
||
933 | printf("<p class=\"alert alert-danger\"><strong>DB Error:</strong> %s</p>\n", $db->error()); |
||
934 | $this->system->cleanInstallation(); |
||
935 | System::renderFooter(true); |
||
936 | } |
||
937 | try { |
||
938 | $databaseInstaller = Database::factory($configuration, $dbSetup['dbType']); |
||
939 | } catch (Exception $exception) { |
||
940 | printf("<p class=\"alert alert-danger\"><strong>DB Error:</strong> %s</p>\n", $exception->getMessage()); |
||
941 | $this->system->cleanInstallation(); |
||
942 | System::renderFooter(true); |
||
943 | } |
||
944 | |||
945 | $databaseInstaller->createTables($dbSetup['dbPrefix']); |
||
946 | |||
947 | $stopwords = new Stopwords($configuration); |
||
948 | $stopwords->executeInsertQueries($dbSetup['dbPrefix']); |
||
949 | |||
950 | $this->system->setDatabase($db); |
||
951 | |||
952 | // Erase any table before starting creating the required ones |
||
953 | if (!System::isSqlite($dbSetup['dbType'])) { |
||
954 | $this->system->dropTables($uninst); |
||
955 | } |
||
956 | |||
957 | // Start creating the required tables |
||
958 | $count = 0; |
||
959 | foreach ($query as $executeQuery) { |
||
960 | $result = @$db->query($executeQuery); |
||
961 | if (!$result) { |
||
962 | echo '<p class="alert alert-danger"><strong>Error:</strong> Please install your version of phpMyFAQ once again or send |
||
963 | us a <a href=\"https://www.phpmyfaq.de\" target=\"_blank\">bug report</a>.</p>'; |
||
964 | printf('<p class="alert alert-danger"><strong>DB error:</strong> %s</p>', $db->error()); |
||
965 | printf('<code>%s</code>', htmlentities($executeQuery)); |
||
966 | $this->system->dropTables($uninst); |
||
967 | $this->system->cleanInstallation(); |
||
968 | System::renderFooter(true); |
||
969 | } |
||
970 | usleep(1000); |
||
971 | ++$count; |
||
972 | if (!($count%10)) { |
||
973 | echo '| '; |
||
974 | } |
||
975 | } |
||
976 | |||
977 | $link = new Link(null, $configuration); |
||
978 | |||
979 | // add main configuration, add personal settings |
||
980 | $this->_mainConfig['main.metaPublisher'] = $realname; |
||
981 | $this->_mainConfig['main.administrationMail'] = $email; |
||
982 | $this->_mainConfig['main.language'] = $language; |
||
983 | $this->_mainConfig['security.permLevel'] = $permLevel; |
||
984 | |||
985 | foreach ($this->_mainConfig as $name => $value) { |
||
986 | $configuration->add($name, $value); |
||
987 | } |
||
988 | |||
989 | $configuration->update(['main.referenceURL' => $link->getSystemUri('/setup/index.php')]); |
||
990 | $configuration->add('security.salt', md5($configuration->getDefaultUrl())); |
||
991 | |||
992 | // add admin account and rights |
||
993 | $admin = new User($configuration); |
||
994 | View Code Duplication | if (!$admin->createUser($loginname, $password, null, 1)) { |
|
995 | printf( |
||
996 | '<p class="alert alert-danger"><strong>Fatal installation error:</strong><br>'. |
||
997 | "Couldn't create the admin user: %s</p>\n", |
||
998 | $admin->error() |
||
999 | ); |
||
1000 | $this->system->cleanInstallation(); |
||
1001 | System::renderFooter(true); |
||
1002 | } |
||
1003 | $admin->setStatus('protected'); |
||
1004 | $adminData = [ |
||
1005 | 'display_name' => $realname, |
||
1006 | 'email' => $email, |
||
1007 | ]; |
||
1008 | $admin->setUserData($adminData); |
||
1009 | |||
1010 | // add default rights |
||
1011 | foreach ($this->mainRights as $right) { |
||
1012 | $admin->perm->grantUserRight(1, $admin->perm->addRight($right)); |
||
1013 | } |
||
1014 | |||
1015 | // Add anonymous user account |
||
1016 | $instanceSetup->createAnonymousUser($configuration); |
||
1017 | |||
1018 | // Add master instance |
||
1019 | $instanceData = [ |
||
1020 | 'url' => $link->getSystemUri($_SERVER['SCRIPT_NAME']), |
||
1021 | 'instance' => $link->getSystemRelativeUri('setup/index.php'), |
||
1022 | 'comment' => 'phpMyFAQ '.System::getVersion(), |
||
1023 | ]; |
||
1024 | $faqInstance = new Instance($configuration); |
||
1025 | $faqInstance->addInstance($instanceData); |
||
1026 | |||
1027 | $faqInstanceMaster = new Master($configuration); |
||
1028 | $faqInstanceMaster->createMaster($faqInstance); |
||
1029 | |||
1030 | // connect to Elasticsearch if enabled |
||
1031 | if (!is_null($esEnabled) && is_file($rootDir.'/config/elasticsearch.php')) { |
||
1032 | require $rootDir.'/config/elasticsearch.php'; |
||
1033 | |||
1034 | $configuration->setElasticsearchConfig($PMF_ES); |
||
1035 | |||
1036 | $esClient = ClientBuilder::create() |
||
1037 | ->setHosts($PMF_ES['hosts']) |
||
1038 | ->build(); |
||
1039 | |||
1040 | $configuration->setElasticsearch($esClient); |
||
1041 | |||
1042 | $faqInstanceElasticsearch = new Elasticsearch($configuration); |
||
1043 | $faqInstanceElasticsearch->createIndex(); |
||
1044 | } |
||
1045 | } |
||
1046 | |||
1065 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: