1
0
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:
Cameron
2015-02-13 19:01:03 -08:00

View File

@@ -1,30 +1,34 @@
<?php
/*
/**
* e107 website system
*
* Copyright (C) 2008-2015 e107 Inc (e107.org)
* Released under the terms and conditions of the
* 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');
/**
* Class drupal_import.
*/
class drupal_import extends base_import_class
{
public $title = 'Drupal';
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 $supported = array('users');
public $mprefix = false;
private $version = null;
private $version = 7;
private $baseUrl = null;
private $basePath = null;
private $basePath = '/';
function init()
@@ -51,96 +55,208 @@ class drupal_import extends base_import_class
}
/**
* Altering import form. We append additional form elements to it.
*
* @return array $frmElements
*/
function config()
{
$frm = e107::getForm();
$frmElements = array();
$var[0]['caption'] = "Drupal Version";
$var[0]['html'] = $frm->text('version', $this->version, 50, 'required=1');
$versions = array(
'6' => 'Drupal 6',
'7' => 'Drupal 7',
'8' => 'Drupal 8',
);
$var[1]['caption'] = "Drupal Base URL";
$var[1]['html'] =$frm->text('baseUrl', $this->baseUrl, 50, 'required=1');
$dscVersion = 'The version of targeted Drupal.';
$frmElements[] = array(
'caption' => "Drupal Version",
'html' => $frm->select('version', $versions, $this->version, 50, array(
'required' => 1,
)) . '<div class="field-help">' . $dscVersion . '</div>',
);
$var[2]['caption'] = "Drupal Base Path";
$var[2]['html'] =$frm->text('basePath', $this->basePath, 50, 'required=1');
$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>',
);
return $var;
$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
// If $blank_user is true, certain cross-referencing user info is to be zeroed
/**
* Set up a query for the specified task if we have a valid connection to
* Drupal database.
*
* @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;
// Check the connection to Drupal database.
if ($this->ourDB == null)
{
return $result;
}
// Set up a query for the specified task.
switch ($task)
{
case 'users':
$result = $this->ourDB->gen("SELECT * FROM {$this->DBPrefix}users WHERE `status`=1");
if ($result === false) return false;
$result = $this->_setupQueryUsers();
$this->copyUserInfo = !$blank_user;
break;
case 'news':
return false;
break;
case 'page':
return false;
break;
case 'links':
break;
default:
break;
}
if ($result === false)
{
return false;
}
$this->currentTask = $task;
return true;
}
/**
* Helper function to setup a query for the 'users' task.
*/
function _setupQueryUsers()
{
switch ((int) $this->version)
{
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;
}
$this->copyUserInfo = !$blank_user;
$this->currentTask = $task;
return TRUE;
}
//------------------------------------
// Internal functions below here
//------------------------------------
// Copy data read from the DB into the record to be returned.
function copyUserData(&$target, &$source) // http://drupal.org/files/er_db_schema_drupal_7.png
/**
* 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)
{
// int(10)
$target['user_id'] = $source['uid'];
// varchar(100)
$target['user_name'] = $source['name'];
// varchar(100)
$target['user_loginname'] = $source['name'];
// varchar(50)
$target['user_password'] = $source['pass'];
// varchar(100)
$target['user_email'] = $source['mail'];
// text
$target['user_signature'] = $source['signature'];
$target['user_join'] = $source['created'];
$target['user_lastvisit'] = $source['login']; // Could use $source['access']
$target['user_image'] = $source['picture'];
// $source['init'] is email address used to sign up from
$target['user_timezone'] = $source['timezone']; // May need conversion varchar(8)
$target['user_language'] = $source['language']; // May need conversion varchar(12)
// 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;
}
}
/**
* 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)
{
@@ -170,13 +286,16 @@ class drupal_import extends base_import_class
}
/**
* Example copy e107 Page Table
* @param $target array - default e107 target values for e107_page table.
* @param $source array - Drupal table data
* Example copy e107 Page Table.
*
* @param array $target
* Default e107 target values for e107_page table.
*
* @param array $source
* Drupal table data.
*
* @return array $target
*/
function copyPageData(&$target, &$source)
{
@@ -209,16 +328,136 @@ class drupal_import extends base_import_class
);
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;
}
}