2012-04-05 15 views
0

htmlで単純な「アカウントを作成」フォームを作成しました。これは、ダブルオプトインメソッドのためにいくつかのJavaでインタースパイされています。また、このフォームをJoomlaで作成し、アカウントの作成ボタンをクリックしてログインすることもできます。Joomla 2.5の新規登録ユーザーphpスクリプト(dblオプトインJava)

ダブルオプトイン機能はうまく動作しますが、joomla 2.5の新しいユーザースクリプトは動作しません。エラーは発生しませんが、ユーザーは登録されません。私は、新しいユーザーを生成するためにstackoverflowに見つかったPHPスクリプト(下記参照)を配置しようとしましたが、それは動作していません。

この2つのタイプのスクリプトを1つのフォームで一緒に実行できますか?もしそうなら、どこが間違っていますか?ありがとう!

require_once (JPATH_BASE .DS.'includes'.DS.'defines.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php'); 

$mainframe =& JFactory::getApplication('site'); 
$mainframe->initialise(); 

//Check for request forgeries, we comment this out since tokens are not generated in the html page 
//JRequest::checkToken() or jexit('Invalid Token'); 

//Get required system objects 

$user   = clone(JFactory::getUser()); 
$pathway   = & $mainframe->getPathway(); 
$config  = & JFactory::getConfig(); 
$authorize  = & JFactory::getACL(); 
$document  = & JFactory::getDocument(); 

//If user registration is not allowed, show 403 not authorized(Not needed) 

$usersConfig = &JComponentHelper::getParams('com_users'); 
if ($usersConfig->get('allowUserRegistration') == '0') 
    { 
     JError::raiseError(403, JText::_('Access Forbidden')); 
     return; 
    } 

//Initialize new usertype setting 

$newUsertype = $usersConfig->get('new_usertype'); 
if (!$newUsertype) 
    { 
     $newUsertype = 'Registered'; 
    } 

//Bind the post array to the user object 
if (!$user->bind(JRequest::get('post'), 'usertype')) 
    { 
     JError::raiseError(500, $user->getError()); 
    } 

//Set some initial user values 

$user->set('id', 0); 
$user->set('usertype', ''); 
$user->set('gid', $authorize->get_group_id('', $newUsertype, 'ARO')); 

$date =& JFactory::getDate(); 
$user->set('registerDate', $date->toMySQL()); 

//If user activation is turned on, we need to set the activation information(Not needed) 

$useractivation = $usersConfig->get('useractivation'); 
if ($useractivation == '1') 
    { 
     jimport('joomla.user.helper'); 
     $user->set('activation', md5(JUserHelper::genRandomPassword())); 
     $user->set('block', '1'); 
    } 

//Save the details of the user 

$user->save(); 

答えて

2

さまざまな環境からユーザーをJoomlaに登録するAPIを作成しようとしていると思います。あなたのコードはJoomla 1.5/1.6ではうまく動作しますが、1.7以降ではうまく動作しません。以下のスニペットは私のために働きました。

<?php 
/* 
* Created on 13-Apr-12 
* 
* To change the template for this generated file go to 
* Window - Preferences - PHPeclipse - PHP - Code Templates 
*/ 
/* 
    * loading Joomla environment 
    */ 
define('_JEXEC', 1); 
$JUnit_home = $_SERVER['SCRIPT_FILENAME']; 
//define('JPATH_BASE', dirname(__FILE__));//this is when we are in the root 


define('DS', DIRECTORY_SEPARATOR); 

require_once (JPATH_BASE .DS.'includes'.DS.'defines.php'); 
require_once (JPATH_BASE .DS.'includes'.DS.'framework.php'); 

function register_user ($email, $password){ 

$firstname = $email; // generate $firstname 
$lastname = ''; // generate $lastname 
$username = $email; // username is the same as email 


/* 
I handle this code as if it is a snippet of a method or function!! 

First set up some variables/objects  */ 


//$acl =& JFactory::getACL(); Acl will work only in Joomla1.5/1.6  

/* get the com_user params */ 
$mainframe =& JFactory::getApplication('site'); 
$mainframe->initialise(); 

$usersParams = &JComponentHelper::getParams('com_users'); // load the Params 

// "generate" a new JUser Object 
$user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded 

$data = array(); // array for all user settings 


//original logic of name creation 
//$data['name'] = $firstname.' '.$lastname; // add first- and lastname 
$data['name'] = $firstname.$lastname; // add first- and lastname 

$data['username'] = $username; // add username 
$data['email'] = $email; // add email 
//there's no gid field in #__users table from Joomla_1.7/2.5 

$usertype = 'Registered';//this is not necessary!!! 
jimport('joomla.application.component.helper'); 
/* this part of the snippet from here: /plugins/user/joomla/joomla.php*/ 
$config = JComponentHelper::getParams('com_users'); 
    // Default to Registered. 
$defaultUserGroup = $config->get('new_usertype', 2); 
//default to defaultUserGroup i.e.,Registered 
$data['groups']=array($defaultUserGroup); 
$data['password'] = $password; // set the password 
$data['password2'] = $password; // confirm the password 
$data['sendEmail'] = 1; // should the user receive system mails? 

/* Now we can decide, if the user will need an activation */ 

$useractivation = $usersParams->get('useractivation'); // in this example, we load the config-setting 
//echo $useractivation;exit(); 
if ($useractivation == 1) { // yeah we want an activation 

jimport('joomla.user.helper'); // include libraries/user/helper.php 
$data['block'] = 1; // block the User 
$data['activation'] =JUtility::getHash(JUserHelper::genRandomPassword()); // set activation hash (don't forget to send an activation email) 

} 
else { // no we need no activation 

$data['block'] = 1; // don't block the user 

} 

if (!$user->bind($data)) { // now bind the data to the JUser Object, if it not works.... 
JError::raiseWarning('', JText::_($user->getError())); // ...raise an Warning 
    return false; // if you're in a method/function return false 

} 

if (!$user->save()) { // if the user is NOT saved... 
JError::raiseWarning('', JText::_($user->getError())); // ...raise an Warning 

return false; // if you're in a method/function return false 

} 

return $user; // else return the new JUser object 

} 

$email = JRequest::getVar('email'); 
$password = JRequest::getVar('password'); 

//echo 'User registration...'.'<br/>'; 
if(!register_user($email, $password)) 
{ 
$data['status']="failure"; 
echo json_encode($data); 
} 
else 
{ 
$data['status']="success"; 
echo json_encode($data); 
} 
//echo '<br/>'.'User registration is completed'.'<br/>'; 
?> 

P.S. 、#_ ユーザ(ユーザ詳細)、# _book_user_usergroup_map(マップのグループIDとユーザーID)をチェックテーブルが登録

+0

小さな修正後の影響を受け、グループキーテーブルを保持している#__usergroups、他の「ブロックしないでくださいユーザーは "$ data ['block'] = 0 – Maksee

+0

"アクティベーションハッシュを設定してください(アクティベーション電子メールを送信することを忘れないでください) " **コードでアクティベーション電子メールを送信するにはどうしたらいいですか?** – dawoodman71

関連する問題