From: Platonides Date: Tue, 18 Sep 2018 22:19:07 +0000 (+0200) Subject: Simplify settings X-Git-Url: http://gitweb.wikimedia.es/?a=commitdiff_plain;h=01433da7812ddbe27bc14389e5c93a4df0b13629;p=limesurvey%2FAuthWMESbyDB Simplify settings --- diff --git a/AuthWMESbyDB.php b/AuthWMESbyDB.php index 2f291e8..64c24e6 100644 --- a/AuthWMESbyDB.php +++ b/AuthWMESbyDB.php @@ -25,37 +25,33 @@ class AuthWMESbyDB extends \ls\pluginmanager\AuthPluginBase static protected $description = 'A plugin to authenticate user via WordPress DB (v1.1)'; static protected $name = 'AuthWMESbyDB'; + static protected $configurableDBSettings = false; protected $settings = array( - 'authwp_dir' => array( - 'type' => 'string', - 'label' => 'The directory where WP is (If found : no need to configure DB, example if you put limesurvey in a sub directory : ../).', - 'default' => ''// Don't set default : preferred methode : same DB than LS with prefix to wp_ - ), 'authwp_dbhost' => array( 'type' => 'string', - 'label' => 'WordPress DB Host (default to LimeSurvey DB Host)' + 'label' => 'DB Host (default to LimeSurvey DB Host)' ), 'authwp_dbport' => array( 'type' => 'string', - 'label' => 'WordPress DB Port (default to LimeSurvey DB Port or 3306 id name or host is define)' + 'label' => 'DB Port (default to LimeSurvey DB Port or 3306 id name or host is define)' ), 'authwp_dbname' => array( 'type' => 'string', - 'label' => 'WordPress DB Name (default to LimeSurvey DB Name)' + 'label' => 'DB Name (default to LimeSurvey DB Name)' ), 'authwp_dbuser' => array( 'type' => 'string', - 'label' => 'WordPress DB User (default to LimeSurvey DB User)' + 'label' => 'DB User (default to LimeSurvey DB User)' ), 'authwp_dbpassword' => array( 'type' => 'string', - 'label' => 'WordPress DB User password (default to LimeSurvey DB User)' + 'label' => 'DB User password (default to LimeSurvey DB User)' ), 'authwp_dbprefix' => array( 'type' => 'string', - 'label' => 'WordPress DB prefix', - 'default' => 'wp_' + 'label' => 'DB prefix', + 'default' => 'auth_' ), 'authwp_default' => array( 'type' => 'checkbox', @@ -198,7 +194,7 @@ class AuthWMESbyDB extends \ls\pluginmanager\AuthPluginBase { if($this->addWpDb()) { - $aUser = Yii::app()->wpdb->createCommand() + $aUser = Yii::app()->wmesdb->createCommand() ->select('user_login,user_pass,user_nicename,user_email,display_name,ul.meta_value as user_level') ->from('{{users}}') ->leftJoin('{{usermeta}} ul', 'ID = ul.user_id AND ul.meta_key="wp_user_level"') @@ -224,56 +220,35 @@ class AuthWMESbyDB extends \ls\pluginmanager\AuthPluginBase /** * Add the db from plugin configuration in new Yii db **/ - private function addWpDb() + private function addWDb() { static $bValid=NULL; if(!is_null($bValid)) return $bValid; - $bWpFileConfig=false; - // Start by loading wp-config if we can - $sWPdirectory = $this->get('authwp_dir'); - if(is_file($sWPdirectory."wp-config.php") && is_readable($sWPdirectory."wp-config.php")){ - $bWpFileConfig=true; - }elseif(is_file(Yii::app()->getConfig('rootdir').DIRECTORY_SEPARATOR.$sWPdirectory."wp-config.php") && is_readable(Yii::app()->getConfig('rootdir').DIRECTORY_SEPARATOR.$sWPdirectory."wp-config.php")){ - $sWPdirectory=Yii::app()->getConfig('rootdir').DIRECTORY_SEPARATOR.$sWPdirectory; - $bWpFileConfig=true; - } - if($bWpFileConfig){ - define('ABSPATH',dirname(__FILE__) . '/'); // Define absolute path to remove inclusion of wp-settings.php - require_once $sWPdirectory."wp-config.php"; - $sWpDbHost = DB_HOST; - $sWpDbPort = "3306"; // TODO : fix specific port @link http://codex.wordpress.org/Editing_wp-config.php#MySQL_Alternate_Port - $sWpDbName = DB_NAME; - $sWpDbUser = DB_USER; - $sWpDbPassword = DB_PASSWORD; - $sWpDbPrefix = $table_prefix; - $sWpDbCharset = DB_CHARSET; - $sConnectionString="mysql:host={$sWpDbHost};port={$sWpDbPort};dbname={$sWpDbName}"; - }else{ - $sWpDbHost = $this->get('authwp_dbhost'); - $sWpDbPort = $this->get('authwp_dbport'); - $sWpDbName = $this->get('authwp_dbname'); - $sWpDbUser = $this->get('authwp_dbuser'); - $sWpDbPassword = $this->get('authwp_dbpassword'); - $sWpDbPrefix = $this->get('authwp_dbprefix'); - if(is_null($sWpDbPrefix)){ - $sWpDbPrefix='wp_'; - } - $sWpDbCharset = "utf8"; - if($sWpDbHost || $sWpDbPort || $sWpDbName){ - if(!$sWpDbPort) - $sWpDbPort="3306"; - $sConnectionString="mysql:host={$sWpDbHost};port={$sWpDbPort};dbname={$sWpDbName}"; - }else{ - $sConnectionString=Yii::app()->db->connectionString; - } - if(!$sWpDbUser) - $sWpDbUser=Yii::app()->db->username; - if(!$sWpDbPassword) - $sWpDbPassword=Yii::app()->db->password; - } - // Test if we have a connexion and if this have users and usermeta table. + $sWpDbHost = self::$configurableDBSettings ? $this->get('authwp_dbhost') : null; + $sWpDbPort = self::$configurableDBSettings ? $this->get('authwp_dbport') : null; + $sWpDbName = self::$configurableDBSettings ? $this->get('authwp_dbname') : null; + $sWpDbUser = self::$configurableDBSettings ? $this->get('authwp_dbuser') : null; + $sWpDbPassword = self::$configurableDBSettings ? $this->get('authwp_dbpassword') : null; + $sWpDbPrefix = self::$configurableDBSettings ? $this->get('authwp_dbprefix') : null; + if(is_null($sWpDbPrefix)){ + $sWpDbPrefix='auth_'; + } + $sWpDbCharset = "utf8"; + if($sWpDbHost || $sWpDbPort || $sWpDbName){ + if(!$sWpDbPort) + $sWpDbPort="3306"; + $sConnectionString="mysql:host={$sWpDbHost};port={$sWpDbPort};dbname={$sWpDbName}"; + }else{ + $sConnectionString=Yii::app()->db->connectionString; + } + if(!$sWpDbUser) + $sWpDbUser=Yii::app()->db->username; + if(!$sWpDbPassword) + $sWpDbPassword=Yii::app()->db->password; + + // Test if we have a connection and we have users table. $oConnection=new CDbConnection($sConnectionString,$sWpDbUser,$sWpDbPassword); try { $wpdb = Yii::createComponent(array( @@ -285,8 +260,8 @@ class AuthWMESbyDB extends \ls\pluginmanager\AuthPluginBase 'emulatePrepare' => true, 'tablePrefix' => $sWpDbPrefix, )); - Yii::app()->setComponent('wpdb', $wpdb); - if(in_array($sWpDbPrefix.'users',Yii::app()->wpdb->schema->getTableNames()) && in_array($sWpDbPrefix.'usermeta',Yii::app()->wpdb->schema->getTableNames()) ){ + Yii::app()->setComponent('wmesdb', $wpdb); + if(in_array($sWpDbPrefix.'users',Yii::app()->wmesdb->schema->getTableNames())){ $bValid=true; }else{ $bValid=false;