Merge pull request #660 from fabrixxm/moveme

uimport: check table columns before import
This commit is contained in:
friendica 2013-04-03 17:36:40 -07:00
commit 1e54f0022f

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* import account file exported from mod/uexport * import account file exported from mod/uexport
* args: * args:
@ -10,7 +11,8 @@ define("IMPORT_DEBUG", False);
function last_insert_id() { function last_insert_id() {
global $db; global $db;
if (IMPORT_DEBUG) return 1; if (IMPORT_DEBUG)
return 1;
if ($db->mysqli) { if ($db->mysqli) {
$thedb = $db->getdb(); $thedb = $db->getdb();
return $thedb->insert_id; return $thedb->insert_id;
@ -24,13 +26,45 @@ function last_insert_id(){
return $db->error; return $db->error;
} }
/**
* Remove columns from array $arr that aren't in table $table
*
* @param string $table Table name
* @param array &$arr Column=>Value array from json (by ref)
*/
function check_cols($table, &$arr) {
$query = sprintf("SHOW COLUMNS IN `%s`", dbesc($table));
logger("uimport: $query", LOGGER_DEBUG);
$r = q($query);
$tcols = array();
// get a plain array of column names
foreach ($r as $tcol) {
$tcols[] = $tcol['Field'];
}
// remove inexistent columns
foreach ($arr as $icol => $ival) {
if (!in_array($icol, $tcols)) {
unset($arr[$icol]);
}
}
}
/**
* Import data into table $table
*
* @param string $table Table name
* @param array $arr Column=>Value array from json
*/
function db_import_assoc($table, $arr) { function db_import_assoc($table, $arr) {
if (IMPORT_DEBUG) return true; if (isset($arr['id']))
if (isset($arr['id'])) unset($arr['id']); unset($arr['id']);
check_cols($table, $arr);
$cols = implode("`,`", array_map('dbesc', array_keys($arr))); $cols = implode("`,`", array_map('dbesc', array_keys($arr)));
$vals = implode("','", array_map('dbesc', array_values($arr))); $vals = implode("','", array_map('dbesc', array_values($arr)));
$query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')"; $query = "INSERT INTO `$table` (`$cols`) VALUES ('$vals')";
logger("uimport: $query", LOGGER_TRACE); logger("uimport: $query", LOGGER_TRACE);
if (IMPORT_DEBUG)
return true;
return q($query); return q($query);
} }
@ -42,7 +76,6 @@ function import_cleanup($newuid) {
q("DELETE FROM `group` WHERE uid = %d", $newuid); q("DELETE FROM `group` WHERE uid = %d", $newuid);
q("DELETE FROM `group_member` WHERE uid = %d", $newuid); q("DELETE FROM `group_member` WHERE uid = %d", $newuid);
q("DELETE FROM `pconfig` WHERE uid = %d", $newuid); q("DELETE FROM `pconfig` WHERE uid = %d", $newuid);
} }
function import_account(&$a, $file) { function import_account(&$a, $file) {
@ -213,17 +246,9 @@ function import_account(&$a, $file) {
$p = new Photo($photo['data'], $photo['type']); $p = new Photo($photo['data'], $photo['type']);
$r = $p->store( $r = $p->store(
$photo['uid'], $photo['uid'], $photo['contact-id'], //0
$photo['contact-id'], //0 $photo['resource-id'], $photo['filename'], $photo['album'], $photo['scale'], $photo['profile'], //1
$photo['resource-id'], $photo['allow_cid'], $photo['allow_gid'], $photo['deny_cid'], $photo['deny_gid']
$photo['filename'],
$photo['album'],
$photo['scale'],
$photo['profile'], //1
$photo['allow_cid'],
$photo['allow_gid'],
$photo['deny_cid'],
$photo['deny_gid']
); );
if ($r === false) { if ($r === false) {
@ -244,6 +269,4 @@ function import_account(&$a, $file) {
info(t("Done. You can now login with your username and password")); info(t("Done. You can now login with your username and password"));
goaway($a->get_baseurl() . "/login"); goaway($a->get_baseurl() . "/login");
} }