[問題] 關於用 CVS 檔案將會員資料 import 到會員檔裡

phpBB 2 MOD Support
無論是官方或非官方認證之外掛,安裝與使用問題討論。
(發表文章請按照公告格式發表,違者砍文)

版主: 版主管理群

主題已鎖定
momoC
星球公民
星球公民
文章: 167
註冊時間: 2004-09-15 22:39
聯繫:

[問題] 關於用 CVS 檔案將會員資料 import 到會員檔裡

文章 momoC »

參考主題:Import CSV [2.0.8/EM]
參考連結:http://mods.db9.dk/viewtopic.php?t=4743

我在學校需要定期連絡上百位同學。同學的資料可以以電腦檔案取得,但要使用一般寄 email 方式群發時很不方便。所以想將這些同學的 e-mail 用 cvs 檔直接傳入資料庫,再從 phpbb 的會員群發功能寄學校新聞給他們。

但這支外掛只能傳 e-mail (單一)欄位上去,我是希望能連會員名稱也傳上去,卻不知道要怎麼改程式。可否請知道的大大指點一下?

外掛中使用上傳功能的程式是 admin_import_CVS.php,謝謝!

程式內容如下:

代碼: 選擇全部

<?php
/***************************************************************************
*                             admin_import_CVS.php
*                              -------------------
*   begin                : Thu May 05, 2004
*   author               : Niels Chr. Denmark <ncr@db9.dk> (http://mods.db9.dk)
*
* version 0.9.0
*
* History:
*   0.9.0. - initial BETA
*
* a fully phpBB2 integrated import CVS ACP interface
*
*
****************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
	$filename = basename(__FILE__);
	$module['Users']['Import_CVS'] = $filename;
	return;
}

//
// Load default header
//
$no_page_header = TRUE;\r
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);

//
// Set VERBOSE to 1  for debugging info..
//
define("VERBOSE", 0);

//
// Increase maximum execution time, but don't complain about it if it isn't
// allowed.
//
@set_time_limit(1200);

// -----------------------
// Page specific functions
//
function gen_rand_string($hash=false)
{
	$chars = array( '1', '2', '3', '4', '5', '6', '7', '8', '9');
	
	$max_chars = count($chars) - 1;
	srand( (double) microtime()*1000000);
	
	$rand_str = '';
	for($i = 0; $i < 8; $i++)
	{
		$rand_str .= $chars[rand(0, $max_chars)];
	}
	return ( $hash ) ? md5($rand_str) : $rand_str;
}

function add_user($username, $user_email, $password)
{
	global $lang, $db, $board_config,$phpbb_root_path,$phpEx, $userdata;
	$unhtml_specialchars_match = array('#>#', '#<#', '#"#', '#&#');
	$unhtml_specialchars_replace = array('>', '<', '"', '&');

$script_name = preg_replace('/^\/?(.*?)\/?$/', '\1', trim($board_config['script_path']));
$script_name = ( $script_name != '' ) ? $script_name . '/profile.'.$phpEx : 'profile.'.$phpEx;
$server_name = trim($board_config['server_name']);
$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
$server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';

$server_url = $server_protocol . $server_name . $server_port . $script_name;

	$sql = "SELECT MAX(user_id) AS total
	FROM " . USERS_TABLE;
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
	}
	if ( !($row = $db->sql_fetchrow($result)) )
	{
		message_die(GENERAL_ERROR, 'Could not obtain next user_id information', '', __LINE__, __FILE__, $sql);
	}
	$user_id = $row['total'] + 1;
	$new_password = md5($password);
	
	$sql = "INSERT INTO " . USERS_TABLE . "	(user_id, username, user_regdate, user_password, user_email, user_viewemail, user_timezone, user_dateformat, user_lang, user_style, user_level, user_active, user_actkey)
				VALUES ($user_id, '" . str_replace("\'", "''", $username) . "', " . time() . ", '" . str_replace("\'", "''", $new_password) . "', '" . str_replace("\'", "''", $user_email) . "', '0', '".$board_config['board_timezone']."', '" . $board_config['default_dateformat'] . "', '" . $board_config['default_lang']. "', '".$board_config['default_style']."', 0, ";
	if ( $board_config['require_activation'] == USER_ACTIVATION_SELF || $board_config['require_activation'] == USER_ACTIVATION_ADMIN )
	{
		$user_actkey = gen_rand_string(true);
		$key_len = 54 - (strlen($server_url));
		$key_len = ( $key_len > 6 ) ? $key_len : 6;
		$user_actkey = substr($user_actkey, 0, $key_len);
		$sql .= "0, '" . str_replace("\'", "''", $user_actkey) . "')";
	}
	else
	{
		$sql .= "1, '')";
	}

	if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
	{
		message_die(GENERAL_ERROR, 'Could not insert data into users table', '', __LINE__, __FILE__, $sql);
	}

	$sql = "INSERT INTO " . GROUPS_TABLE . " (group_name, group_description, group_single_user, group_moderator)
		VALUES ('', 'Personal User', 1, 0)";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not insert data into groups table', '', __LINE__, __FILE__, $sql);
	}

	$group_id = $db->sql_nextid();
	$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
		VALUES ($user_id, $group_id, 0)";
	if( !($result = $db->sql_query($sql, END_TRANSACTION)) )
	{
		message_die(GENERAL_ERROR, 'Could not insert data into user_group table', '', __LINE__, __FILE__, $sql);
	}
	if ( $board_config['require_activation'] == USER_ACTIVATION_SELF )
	{
		$message = $lang['Account_inactive'];
		$email_template = 'user_welcome_inactive';
	}
	else if ( $board_config['require_activation'] == USER_ACTIVATION_ADMIN )
	{
		$message = $lang['Account_inactive_admin'];
		$email_template = 'admin_welcome_inactive';
	}
	else
	{
		$message = $lang['Account_added'];
		$email_template = 'user_welcome';
	}

	include_once($phpbb_root_path . 'includes/emailer.'.$phpEx);
	$emailer = new emailer($board_config['smtp_delivery']);

	$emailer->from($board_config['board_email']);
	$emailer->replyto($board_config['board_email']);

	$emailer->use_template($email_template, $board_config['default_lang']);
	$emailer->email_address($user_email);
	$emailer->set_subject(sprintf($lang['Welcome_subject'], $board_config['sitename']));
	$emailer->assign_vars(array(
			'SITENAME' => $board_config['sitename'],
			'WELCOME_MSG' => sprintf($lang['Welcome_subject'], $board_config['sitename']),
			'USERNAME' => preg_replace($unhtml_specialchars_match, $unhtml_specialchars_replace, substr(str_replace("\'", "'", $username), 0, 25)),
			'PASSWORD' => $password,
			'EMAIL_SIG' => str_replace('<br />', "
", "-- 
" . $board_config['board_email_sig']),

			'U_ACTIVATE' => $server_url . '?mode=activate&' . POST_USERS_URL . '=' . $user_id . '&act_key=' . $user_actkey)
		);
	$emailer->send();
	$emailer->reset();
	if ( $board_config['require_activation'] == USER_ACTIVATION_ADMIN )
	{
		$sql = "SELECT user_email, user_lang 
			FROM " . USERS_TABLE . "
			WHERE user_level = " . ADMIN;
				
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not select Administrators', '', __LINE__, __FILE__, $sql);
		}
				
		while ($row = $db->sql_fetchrow($result))
		{
			$emailer->from($board_config['board_email']);
			$emailer->replyto($board_config['board_email']);
			
			$emailer->email_address(trim($row['user_email']));
			$emailer->use_template("admin_activate", $row['user_lang']);
			$emailer->set_subject($lang['New_account_subject']);

			$emailer->assign_vars(array(
				'USERNAME' => preg_replace($unhtml_specialchars_match, $unhtml_specialchars_replace, substr(str_replace("\'", "'", $username), 0, 25)),
				'EMAIL_SIG' => str_replace('<br />', "
", "-- 
" . $board_config['board_email_sig']),
				'U_ACTIVATE' => $server_url . '?mode=activate&' . POST_USERS_URL . '=' . $user_id . '&act_key=' . $user_actkey)
			);
			$emailer->send();
			$emailer->reset();
		}
	}
	$db->sql_freeresult($result);
}

//
// End page specific functions
// ---------------------------

//
// Begin program proper
//
if(!isset($HTTP_POST_VARS['import_start']))
{
	include('./page_header_admin.'.$phpEx);
	$template->set_filenames(array(
		"body" => "admin/import_cvs_body.tpl")
	);
	$s_hidden_fields = "";
	$template->assign_vars(array(
		"L_IMPORT" => $lang['Database_Utilities'] . " : " . $lang['Import_users'],
		"L_IMPORT_EXPLAIN" => $lang['Import_explain'],
		"L_SELECT_FILE" => $lang['Select_file'],
		"L_START_IMPORT" => $lang['Start_Import'],
		"S_IMPORT_ACTION" => append_sid("admin_import_CVS.$phpEx"),
		"S_HIDDEN_FIELDS" => $s_hidden_fields)
	);
	$template->pparse("body");
} else
{
	//
	// Handle the file upload ....
	// If no file was uploaded report an error...
	//
	$import_file_name = (!empty($HTTP_POST_FILES['import_file']['name'])) ? $HTTP_POST_FILES['import_file']['name'] : "";
	$import_file_tmpname = ($HTTP_POST_FILES['import_file']['tmp_name'] != "none") ? $HTTP_POST_FILES['import_file']['tmp_name'] : "";
	$import_file_type = (!empty($HTTP_POST_FILES['import_file']['type'])) ? $HTTP_POST_FILES['import_file']['type'] : "";
	
	if($import_file_tmpname == "" || $import_file_name == "")
	{
		message_die(GENERAL_MESSAGE, $lang['Import_Error_no_file']);
	}

	//
	// If I file was actually uploaded, check to make sure that we
	// are actually passed the name of an uploaded file, and not
	// a hackers attempt at getting us to process a local system
	// file.
	//
	if( file_exists(phpbb_realpath($import_file_tmpname)) )
	{
		if (! $import_lines = file($import_file_tmpname) )
		{
			message_die(GENERAL_ERROR, $lang['Import_Error_uploading']);
		}
	} else
	{
		message_die(GENERAL_ERROR, $lang['Import_Error_uploading']);
	}
	// import users here
	$num_users = 0;
	$num_users_not_import = 0;
	$num_users_not_import = 0;
	$error_msg ="";
	include_once($phpbb_root_path . 'includes/functions_validate.'.$phpEx);

	foreach($import_lines as $users )
	{
		list($user_email) = split (";",$users);

		$user_email = trim($user_email);
		list ($username,$userdomain) = split("@",$user_email);
		$username = trim($username);
		
		$result = validate_email($user_email);
		if ( $result['error'] )
		{
			$num_users_not_import++;
			$error_msg .= "<br/><i>Skipped: $user_email</i>";
		} else
		{
			$original_username = $username;
			$i=0;
			do
			{
				if (strtolower($username) == strtolower($userdata['username']))
				{
					$result['error'] = true;
				} else
				{
					$result = validate_username($username);
				}
				if ( $result['error'] )
				{
					// the username is not valid, we will try generate a new based on the old
					$username = $original_username.$i++;
				} else
				{

					//we have now found a username to use.
					$error_msg .= "<br/><b>".(( $i==0 ) ? "Username: $username</b>" : " Username: $original_username -> $username</b>" );
					$num_users++;
				}
				
			} while ( $result['error'] );

			//
			// go ahead import the user
			//

			$user_password = gen_rand_string();
			$user_password = $username.substr($user_password, 0, 2);

			$error_msg .= " pass: ".$user_password;
			add_user($username,$user_email,$user_password);
		
		}
	}
	
	include('./page_header_admin.'.$phpEx);
	$template->set_filenames(array(
		"body" => "admin/admin_message_body.tpl")
	);
	$message = sprintf($lang['Import_success'],$num_users,$num_users_not_import);
	if (VERBOSE)
	{
	$message .= $error_msg;	
	}
	$template->assign_vars(array(
		"MESSAGE_TITLE" => $lang['Database_Utilities'] . " : " . $lang['Import'],
		"MESSAGE_TEXT" => $message)
	);

	$template->pparse("body");
}
include('./page_footer_admin.'.$phpEx);

?>
● 架設主機作業系統:IIS 6.0
● 上網方式:DSL
● 虛擬空間 http://www.securemate.com
● 安裝程式:php 4 + MySql 2.5.4
● phpBB2 版本:phpBB 2.0.22
● domain: http://www.genderwars.org
● phpBB 連結: http://phpbb.genderwars.org
~倉木麻衣~
竹貓忠實會員
竹貓忠實會員
文章: 1405
註冊時間: 2004-03-21 21:00

文章 ~倉木麻衣~ »

如果是用outlook之類的, 也可以利用群組方式來發信吧\r
不過我已經好幾年沒在用outlook了(ro06)

要用Import CVS這個外掛來達成要求的話, 可以試看看這樣子的修改\r
CVS檔的格式為「使用者名稱 信箱」, 名稱及信箱之間請用一個空格隔開, 例如

代碼: 選擇全部

#
#--------[ OPEN ]----------------
#
admin/admin_import_CVS.php

#
#--------[ FIND ]----------------
#
		list ($username,$userdomain) = split("@",$user_email);

#
#--------[ REPLACE WITH ]----------------
#
		list ($username,$user_email) = split(" ",$user_email);
		list ($user_mailname,$userdomain) = split("@",$user_email);

#
#--------[ FIND ]----------------
#
		$username = trim($username);

#
#--------[ AFTER, ADD ]----------------
#
		$user_mailname = trim($user_mailname);

#
#--------[ FIND ]----------------
#
			$user_password = $username.substr($user_password, 0, 2);

#
#--------[ REPLACE WITH ]----------------
#
			$user_password = $user_mailname.substr($user_password, 0, 2);

#
#--------[ SAVE/CLOSE ALL FILES ]----------------
#
# EoM
要注意的是 可能還是會有許功蓋這類衝碼字的問題
momoC
星球公民
星球公民
文章: 167
註冊時間: 2004-09-15 22:39
聯繫:

文章 momoC »

~倉木麻衣~ 寫:如果是用outlook之類的, 也可以利用群組方式來發信吧\r
不過我已經好幾年沒在用outlook了(ro06)

要用Import CVS這個外掛來達成要求的話, 可以試看看這樣子的修改\r
CVS檔的格式為「使用者名稱 信箱」, 名稱及信箱之間請用一個空格隔開
要注意的是 可能還是會有許功蓋這類衝碼字的問題
感謝!!!馬上就去試試!
咦?是用空格隔開嗎?不是用 tab 哦,有趣\r
我的 outlook 不能寄給許多人,大概是因為是免費 mailserver 的關係...
唯一不必怕的是許功蓋的問題,因為都是英文名字--不幸中的大幸
真的很謝謝!
~倉木麻衣~
竹貓忠實會員
竹貓忠實會員
文章: 1405
註冊時間: 2004-03-21 21:00

文章 ~倉木麻衣~ »

momoC 寫:咦?是用空格隔開嗎?不是用 tab 哦,有趣
用tab分隔也是可以
改這句就好了

小更新一下程式代碼

代碼: 選擇全部

list ($username,$user_email) = split("[ |	]+",$user_email);
split後頭接的是"[空白鍵|tab鍵]+"
這樣就可以不用去管分隔的是空白鍵或tab鍵了

ps.
程式不讓我用\s來通吃空白及tab鍵, 只好用這種矬矬的寫法了v_v"
謝絕所有私人訊息詢問外掛相關問題
有問題請直接於版上發表, 集思廣議絕對比專挑特定人士詢問來的好

竹貓禁止發表含破解相關的軟體, 違者砍文
不要跟我講別的地方都可以發, 為什麼竹貓就不行
免費不等於破解, 傻傻的搞不清楚
momoC
星球公民
星球公民
文章: 167
註冊時間: 2004-09-15 22:39
聯繫:

文章 momoC »

可以了! (ro16) 感謝老師!為什麼你這麼厲害呢???真是羨慕!

真高興,有點可惜外個人的名字中間有空格\r
好像只要有空格就無法上傳,所以先傳名字沒有傳姓,但已經粉幸福的感覺了

啊!有一點要提醒以後想用這功能的人\r
這支程式會自動發系統歡迎信,如果系統設定需要啟用帳號,連啟用帳號的邀請函也會一起寄出去哦!(真是服務太週到了)
還好我是在自己電腦上試,所以產生錯誤訊息。不然這內部作業忽然丟出一堆信,我就糗了!
主題已鎖定

回到「外掛問題討論」