mirror of
https://github.com/e107inc/e107.git
synced 2025-08-04 13:47:31 +02:00
Merge pull request #891 from lonalore/master
prepared drupal 7 user import, it saves avatars to e107 filesystem now
This commit is contained in:
@@ -1,224 +1,463 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/**
|
||||||
* e107 website system
|
* e107 website system
|
||||||
*
|
*
|
||||||
* Copyright (C) 2008-2015 e107 Inc (e107.org)
|
* Copyright (C) 2008-2015 e107 Inc (e107.org)
|
||||||
* Released under the terms and conditions of the
|
* Released under the terms and conditions of the
|
||||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||||
*
|
*
|
||||||
|
* @file
|
||||||
|
* Each import file has an identifier which must be the same for:
|
||||||
|
* a) This file name - add '_class.php' to get the file name
|
||||||
|
* b) The array index of certain variables.
|
||||||
*/
|
*/
|
||||||
// Each import file has an identifier which must be the same for:
|
|
||||||
// a) This file name - add '_class.php' to get the file name
|
|
||||||
// b) The array index of certain variables
|
|
||||||
|
|
||||||
|
|
||||||
require_once('import_classes.php');
|
require_once('import_classes.php');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class drupal_import.
|
||||||
|
*/
|
||||||
class drupal_import extends base_import_class
|
class drupal_import extends base_import_class
|
||||||
{
|
{
|
||||||
|
|
||||||
public $title = 'Drupal';
|
public $title = 'Drupal';
|
||||||
public $description = 'Basic import';
|
public $description = 'Basic import';
|
||||||
public $supported = array('users'); // array('users', 'news','page','links'); //XXX Modify to enable copyNewsData() etc.
|
// array('users', 'news','page','links'); //XXX Modify to enable copyNewsData() etc.
|
||||||
public $mprefix = false;
|
public $supported = array('users');
|
||||||
|
public $mprefix = false;
|
||||||
private $version = null;
|
|
||||||
private $baseUrl = null;
|
private $version = 7;
|
||||||
private $basePath = null;
|
private $baseUrl = null;
|
||||||
|
private $basePath = '/';
|
||||||
|
|
||||||
|
|
||||||
function init()
|
function init()
|
||||||
{
|
{
|
||||||
if(!empty($_POST['version']))
|
if (!empty($_POST['version']))
|
||||||
{
|
{
|
||||||
$this->version = $_POST['version'];
|
$this->version = $_POST['version'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!empty($_POST['baseUrl']))
|
if (!empty($_POST['baseUrl']))
|
||||||
{
|
{
|
||||||
$this->baseUrl =$_POST['baseUrl'];
|
$this->baseUrl = $_POST['baseUrl'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!empty($_POST['basePath']))
|
if (!empty($_POST['basePath']))
|
||||||
{
|
{
|
||||||
$this->basePath = $_POST['basePath'];
|
$this->basePath = $_POST['basePath'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!empty($_POST))
|
if (!empty($_POST))
|
||||||
{
|
{
|
||||||
e107::getMessage()->addDebug(print_a($_POST, true));
|
e107::getMessage()->addDebug(print_a($_POST, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Altering import form. We append additional form elements to it.
|
||||||
|
*
|
||||||
|
* @return array $frmElements
|
||||||
|
*/
|
||||||
function config()
|
function config()
|
||||||
{
|
{
|
||||||
$frm = e107::getForm();
|
$frm = e107::getForm();
|
||||||
|
$frmElements = array();
|
||||||
$var[0]['caption'] = "Drupal Version";
|
|
||||||
$var[0]['html'] = $frm->text('version', $this->version, 50, 'required=1');
|
|
||||||
|
|
||||||
$var[1]['caption'] = "Drupal Base URL";
|
$versions = array(
|
||||||
$var[1]['html'] =$frm->text('baseUrl', $this->baseUrl, 50, 'required=1');
|
'6' => 'Drupal 6',
|
||||||
|
'7' => 'Drupal 7',
|
||||||
|
'8' => 'Drupal 8',
|
||||||
|
);
|
||||||
|
|
||||||
$var[2]['caption'] = "Drupal Base Path";
|
$dscVersion = 'The version of targeted Drupal.';
|
||||||
$var[2]['html'] =$frm->text('basePath', $this->basePath, 50, 'required=1');
|
$frmElements[] = array(
|
||||||
|
'caption' => "Drupal Version",
|
||||||
|
'html' => $frm->select('version', $versions, $this->version, 50, array(
|
||||||
|
'required' => 1,
|
||||||
|
)) . '<div class="field-help">' . $dscVersion . '</div>',
|
||||||
|
);
|
||||||
|
|
||||||
return $var;
|
$dscBaseUrl = 'The base URL of Drupal website (e.g., http://mydrupalsite.com).';
|
||||||
|
$frmElements[] = array(
|
||||||
|
'caption' => "Drupal Base URL",
|
||||||
|
'html' => $frm->text('baseUrl', $this->baseUrl, 50, array(
|
||||||
|
'required' => 1,
|
||||||
|
)) . '<div class="field-help">' . $dscBaseUrl . '</div>',
|
||||||
|
);
|
||||||
|
|
||||||
|
$dscBasePath = 'The base URL path (i.e., directory) of the Drupal installation (e.g., /drupal/).';
|
||||||
|
$frmElements[] = array(
|
||||||
|
'caption' => "Drupal Base Path",
|
||||||
|
'html' => $frm->text('basePath', $this->basePath, 50, array(
|
||||||
|
'required' => 1,
|
||||||
|
)) . '<div class="field-help">' . $dscBasePath . '</div>',
|
||||||
|
);
|
||||||
|
|
||||||
|
return $frmElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Set up a query for the specified task.
|
/**
|
||||||
// Returns TRUE on success. false on error
|
* Set up a query for the specified task if we have a valid connection to
|
||||||
// If $blank_user is true, certain cross-referencing user info is to be zeroed
|
* Drupal database.
|
||||||
function setupQuery($task, $blank_user=false)
|
*
|
||||||
|
* @param string $task
|
||||||
|
* Name of the current task.
|
||||||
|
*
|
||||||
|
* @param bool $blank_user
|
||||||
|
* If $blank_user is true, certain cross-referencing user info is to be
|
||||||
|
* zeroed.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* Returns TRUE on success. false on error.
|
||||||
|
*/
|
||||||
|
function setupQuery($task, $blank_user = false)
|
||||||
{
|
{
|
||||||
if ($this->ourDB == NULL) return false;
|
$result = false;
|
||||||
|
|
||||||
switch ($task)
|
// Check the connection to Drupal database.
|
||||||
|
if ($this->ourDB == null)
|
||||||
{
|
{
|
||||||
case 'users' :
|
return $result;
|
||||||
$result = $this->ourDB->gen("SELECT * FROM {$this->DBPrefix}users WHERE `status`=1");
|
}
|
||||||
if ($result === false) return false;
|
|
||||||
break;
|
// Set up a query for the specified task.
|
||||||
|
switch ($task)
|
||||||
case 'news' :
|
{
|
||||||
return false;
|
case 'users':
|
||||||
break;
|
$result = $this->_setupQueryUsers();
|
||||||
|
$this->copyUserInfo = !$blank_user;
|
||||||
case 'page' :
|
break;
|
||||||
return false;
|
|
||||||
break;
|
case 'news':
|
||||||
|
break;
|
||||||
case 'links' :
|
|
||||||
return false;
|
case 'page':
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default :
|
case 'links':
|
||||||
return false;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($result === false)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->copyUserInfo = !$blank_user;
|
|
||||||
$this->currentTask = $task;
|
$this->currentTask = $task;
|
||||||
|
|
||||||
return TRUE;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
//------------------------------------
|
* Helper function to setup a query for the 'users' task.
|
||||||
// Internal functions below here
|
*/
|
||||||
//------------------------------------
|
function _setupQueryUsers()
|
||||||
|
{
|
||||||
// Copy data read from the DB into the record to be returned.
|
switch ((int) $this->version)
|
||||||
function copyUserData(&$target, &$source) // http://drupal.org/files/er_db_schema_drupal_7.png
|
{
|
||||||
|
case 6:
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 7:
|
||||||
|
$fields = array(
|
||||||
|
'u.*',
|
||||||
|
'fm.uri',
|
||||||
|
'public.value AS public_file_path',
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = "SELECT " . implode(',', $fields) . " FROM " . $this->DBPrefix . "users AS u ";
|
||||||
|
$query .= "LEFT JOIN " . $this->DBPrefix . "file_managed AS fm ON u.picture = fm.fid ";
|
||||||
|
$query .= "LEFT JOIN " . $this->DBPrefix . "variable AS public ON public.name = 'file_public_path'";
|
||||||
|
$query .= "WHERE u.status = 1 AND u.uid > 1 ";
|
||||||
|
|
||||||
|
return $this->ourDB->gen($query);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 8:
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy data read from the DB into the record to be returned.
|
||||||
|
*
|
||||||
|
* @param array $target
|
||||||
|
* Default e107 target values for e107_user table.
|
||||||
|
*
|
||||||
|
* @param array $source
|
||||||
|
* Drupal table data.
|
||||||
|
*
|
||||||
|
* @return array $target
|
||||||
|
*/
|
||||||
|
function copyUserData(&$target, &$source)
|
||||||
{
|
{
|
||||||
if ($this->copyUserInfo)
|
if ($this->copyUserInfo)
|
||||||
{
|
{
|
||||||
$target['user_id'] = $source['uid'];
|
// int(10)
|
||||||
$target['user_name'] = $source['name'];
|
$target['user_id'] = $source['uid'];
|
||||||
$target['user_loginname'] = $source['name'];
|
|
||||||
$target['user_password'] = $source['pass'];
|
// varchar(100)
|
||||||
$target['user_email'] = $source['mail'];
|
$target['user_name'] = $source['name'];
|
||||||
$target['user_signature'] = $source['signature'];
|
|
||||||
$target['user_join'] = $source['created'];
|
// varchar(100)
|
||||||
$target['user_lastvisit'] = $source['login']; // Could use $source['access']
|
$target['user_loginname'] = $source['name'];
|
||||||
$target['user_image'] = $source['picture'];
|
|
||||||
// $source['init'] is email address used to sign up from
|
// varchar(50)
|
||||||
$target['user_timezone'] = $source['timezone']; // May need conversion varchar(8)
|
$target['user_password'] = $source['pass'];
|
||||||
$target['user_language'] = $source['language']; // May need conversion varchar(12)
|
|
||||||
|
// varchar(100)
|
||||||
|
$target['user_email'] = $source['mail'];
|
||||||
|
|
||||||
|
// text
|
||||||
|
$target['user_signature'] = $source['signature'];
|
||||||
|
|
||||||
|
// int(10)
|
||||||
|
$target['user_join'] = (int) $source['created'];
|
||||||
|
|
||||||
|
// int(10)
|
||||||
|
$target['user_lastvisit'] = (int) $source['login'];
|
||||||
|
|
||||||
|
// May need conversion varchar(8)
|
||||||
|
$target['user_timezone'] = $source['timezone'];
|
||||||
|
|
||||||
|
// May need conversion varchar(12)
|
||||||
|
$target['user_language'] = $source['language'];
|
||||||
|
|
||||||
|
$user_image = $this->fileSaveAvatar($source);
|
||||||
|
|
||||||
|
// varchar(100)
|
||||||
|
$target['user_image'] = $user_image;
|
||||||
|
|
||||||
return $target;
|
return $target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example Copy News.
|
* Example Copy News.
|
||||||
* @param $target array - default e107 target values for e107_page table.
|
*
|
||||||
* @param $source array - Drupal table data
|
* @param array $target
|
||||||
|
* Default e107 target values for e107_page table.
|
||||||
|
*
|
||||||
|
* @param array $source
|
||||||
|
* Drupal table data.
|
||||||
|
*
|
||||||
|
* @return array $target
|
||||||
*/
|
*/
|
||||||
function copyNewsData(&$target, &$source)
|
function copyNewsData(&$target, &$source)
|
||||||
{
|
{
|
||||||
$target = array(
|
$target = array(
|
||||||
'news_id' => 1,
|
'news_id' => 1,
|
||||||
'news_title' => 'Welcome to e107',
|
'news_title' => 'Welcome to e107',
|
||||||
'news_sef' => 'welcome-to-e107',
|
'news_sef' => 'welcome-to-e107',
|
||||||
'news_body' => '[html]<p>Welcome to your new website!</p>[/html]',
|
'news_body' => '[html]<p>Welcome to your new website!</p>[/html]',
|
||||||
'news_extended' => '',
|
'news_extended' => '',
|
||||||
'news_meta_keywords' => '',
|
'news_meta_keywords' => '',
|
||||||
'news_meta_description' => '',
|
'news_meta_description' => '',
|
||||||
'news_datestamp' => '1355612400', // time()
|
'news_datestamp' => '1355612400', // time()
|
||||||
'news_author' => 1,
|
'news_author' => 1,
|
||||||
'news_category' => 1,
|
'news_category' => 1,
|
||||||
'news_allow_comments' => 0,
|
'news_allow_comments' => 0,
|
||||||
'news_start' => 0, // time()
|
'news_start' => 0, // time()
|
||||||
'news_end' => 0, // time()
|
'news_end' => 0, // time()
|
||||||
'news_class' => 0,
|
'news_class' => 0,
|
||||||
'news_render_type' => 0,
|
'news_render_type' => 0,
|
||||||
'news_comment_total' => 1,
|
'news_comment_total' => 1,
|
||||||
'news_summary' => 'summary text',
|
'news_summary' => 'summary text',
|
||||||
'news_thumbnail' => '', // full path with {e_MEDIA_IMAGE} constant.
|
'news_thumbnail' => '', // full path with {e_MEDIA_IMAGE} constant.
|
||||||
'news_sticky' => 0
|
'news_sticky' => 0
|
||||||
);
|
);
|
||||||
|
|
||||||
return $target;
|
return $target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Example copy e107 Page Table
|
* Example copy e107 Page Table.
|
||||||
* @param $target array - default e107 target values for e107_page table.
|
*
|
||||||
* @param $source array - Drupal table data
|
* @param array $target
|
||||||
|
* Default e107 target values for e107_page table.
|
||||||
|
*
|
||||||
|
* @param array $source
|
||||||
|
* Drupal table data.
|
||||||
|
*
|
||||||
|
* @return array $target
|
||||||
*/
|
*/
|
||||||
function copyPageData(&$target, &$source)
|
function copyPageData(&$target, &$source)
|
||||||
{
|
{
|
||||||
$target = array(
|
$target = array(
|
||||||
'page_id' => 1,
|
'page_id' => 1,
|
||||||
'page_title' => 'string',
|
'page_title' => 'string',
|
||||||
'page_sef' => 'string',
|
'page_sef' => 'string',
|
||||||
'page_chapter' => 0,
|
'page_chapter' => 0,
|
||||||
'page_metakeys' => 'string',
|
'page_metakeys' => 'string',
|
||||||
'page_metadscr' => '',
|
'page_metadscr' => '',
|
||||||
'page_text' => '',
|
'page_text' => '',
|
||||||
'page_author' => 0, // e107 user_id
|
'page_author' => 0, // e107 user_id
|
||||||
'page_datestamp' => '1371420000', // time()
|
'page_datestamp' => '1371420000', // time()
|
||||||
'page_rating_flag' => 0,
|
'page_rating_flag' => 0,
|
||||||
'page_comment_flag' => 0, // boolean
|
'page_comment_flag' => 0, // boolean
|
||||||
'page_password' => '', // plain text
|
'page_password' => '', // plain text
|
||||||
'page_class' => 0, // e107 userclass
|
'page_class' => 0, // e107 userclass
|
||||||
'page_ip_restrict' => '',
|
'page_ip_restrict' => '',
|
||||||
'page_template' => 'default',
|
'page_template' => 'default',
|
||||||
'page_order' => 0,
|
'page_order' => 0,
|
||||||
'menu_name' => 'jumbotron-menu-1', // no spaces, all lowercase
|
'menu_name' => 'jumbotron-menu-1', // no spaces, all lowercase
|
||||||
'menu_title' => 'string',
|
'menu_title' => 'string',
|
||||||
'menu_text' => '',
|
'menu_text' => '',
|
||||||
'menu_image' => '',
|
'menu_image' => '',
|
||||||
'menu_icon' => '',
|
'menu_icon' => '',
|
||||||
'menu_template' => 'button',
|
'menu_template' => 'button',
|
||||||
'menu_class' => 0,
|
'menu_class' => 0,
|
||||||
'menu_button_url' => '',
|
'menu_button_url' => '',
|
||||||
'menu_button_text' => ''
|
'menu_button_text' => ''
|
||||||
);
|
);
|
||||||
|
|
||||||
return $target;
|
return $target;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: resizing avatar...?
|
||||||
|
* TODO: process_uploaded_files() should be used?
|
||||||
|
*
|
||||||
|
* Save avatar picture from Drupal filesystem.
|
||||||
|
* a) create remote URL to stream file contents
|
||||||
|
* b) get remote file contents
|
||||||
|
* c) save file to e_AVATAR_UPLOAD
|
||||||
|
*
|
||||||
|
* @param array $row
|
||||||
|
* Full database row contains details of user.
|
||||||
|
*
|
||||||
|
* @return string $local_path
|
||||||
|
* Local path, where the file has been saved to, or empty string.
|
||||||
|
*/
|
||||||
|
function fileSaveAvatar($row)
|
||||||
|
{
|
||||||
|
// Set default return value.
|
||||||
|
$local_path = '';
|
||||||
|
|
||||||
|
$src_uri = $row['uri'];
|
||||||
|
$src_pth = unserialize($row['public_file_path']);
|
||||||
|
$src_pth = $this->fileCreateUrl($src_uri, $src_pth);
|
||||||
|
|
||||||
|
// If $src_pth is empty, we cannot save remote file, so return...
|
||||||
|
if (empty($src_pth))
|
||||||
|
{
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try remote file to open for reading.
|
||||||
|
if ($stream = fopen($src_pth, 'r'))
|
||||||
|
{
|
||||||
|
$file_contents = stream_get_contents($stream);
|
||||||
|
fclose($stream);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no contents, return...
|
||||||
|
if (!$file_contents)
|
||||||
|
{
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get upload directory.
|
||||||
|
$uploaddir = e_AVATAR_UPLOAD;
|
||||||
|
$uploaddir = realpath($uploaddir);
|
||||||
|
|
||||||
|
if (!is_dir($uploaddir))
|
||||||
|
{
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tp = isset($tp) ? $tp : new e_parse();
|
||||||
|
|
||||||
|
$base_name = basename($src_pth);
|
||||||
|
$base_name = preg_replace("/[^\w\pL.-]/u", '', str_replace(' ', '_', str_replace('%20', '_', $tp->ustrtolower($base_name))));
|
||||||
|
$file_name = 'ap_' . $tp->leadingZeros($row['uid'], 7) . '_' . $base_name;
|
||||||
|
|
||||||
|
$uploaded = file_put_contents($uploaddir . '/' . $file_name, $file_contents);
|
||||||
|
|
||||||
|
if ($uploaded === false)
|
||||||
|
{
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$local_path = '-upload-' . $file_name;
|
||||||
|
|
||||||
|
return $local_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a web-accessible URL for a stream to a Drupal local file.
|
||||||
|
*
|
||||||
|
* @param string $uri
|
||||||
|
* The URI to a file for which we need an external URL.
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* Base URL path (i.e., directory) of the Drupal installation.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* A string containing a URL that may be used to access the file.
|
||||||
|
*/
|
||||||
|
function fileCreateUrl($uri, $path)
|
||||||
|
{
|
||||||
|
if (empty($uri) || !(substr($uri, 0, 9) == 'public://'))
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
$base_url = $this->httpCheck($this->baseUrl);
|
||||||
|
// Strip a slash from the end of URL.
|
||||||
|
$base_url = rtrim($base_url, '/');
|
||||||
|
// Strip slashes from the beginning and end of path.
|
||||||
|
$base_path = trim($this->basePath, '/');
|
||||||
|
// Append slashes to the beginning and end of path if it's not empty.
|
||||||
|
$base_path = !empty($base_path) ? '/' . $base_path . '/' : '/';
|
||||||
|
// Replace file schema with the real path.
|
||||||
|
$file_path = str_replace("public://", $path . '/', $uri);
|
||||||
|
|
||||||
|
$url = $base_url . $base_path . $file_path;
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check string contains 'http://' or 'https://' and prepend 'http://' if
|
||||||
|
* it's necessary.
|
||||||
|
*
|
||||||
|
* @param string $url
|
||||||
|
* String of URL.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* String of URL.
|
||||||
|
*/
|
||||||
|
function httpCheck($url)
|
||||||
|
{
|
||||||
|
$return = $url;
|
||||||
|
if ((!(substr($url, 0, 7) == 'http://')) && (!(substr($url, 0, 8) == 'https://')))
|
||||||
|
{
|
||||||
|
$return = 'http://' . $url;
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
||||||
|
Reference in New Issue
Block a user