#!/usr/bin/php $users_array) { foreach ($users_array['users'] as $user_id => $user_array) { $username = $user_array['username']; $dbQuery = "SELECT user_id FROM rc_users WHERE username LIKE '$username'"; $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows == 0) { echo "adding $username\n"; $dbQuery = "INSERT INTO rc_users SET"; foreach ($user_array as $kusers=>$vusers) { if (!is_array($vusers)) { if ($vusers == 'NULL') { $dbQuery .= " `$kusers` = $vusers,"; } else { $vusers = mysqli_escape_string($dbLink, $vusers); $dbQuery .= " `$kusers` = '$vusers',"; } } } $dbQuery = rtrim($dbQuery, ","); $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows != '1') { echo "ERROR: $dbResult = $dbAffectedRows\n"; } $user_id_new = mysqli_insert_id($dbLink); /* identities */ if (!empty($users_array['users'][$user_id]['identities'])) { foreach ($users_array['users'][$user_id]['identities'] as $identity_id => $identity_array) { $identity_array['user_id'] = $user_id_new; $dbQuery = "INSERT INTO rc_identities SET"; foreach ($identity_array as $kidentities=>$videntities) { if (!is_array($videntities)) { if ($videntities == 'NULL') { $dbQuery .= " `$kidentities` = $videntities,"; } else { $videntities = mysqli_escape_string($dbLink, $videntities); $dbQuery .= " `$kidentities` = '$videntities',"; } } } $dbQuery = rtrim($dbQuery, ","); $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows != '1') { echo "ERROR: $dbResult = $dbAffectedRows\n"; } $identity_id_new = mysqli_insert_id($dbLink); } } /* contacts */ if (!empty($users_array['users'][$user_id]['contacts'])) { foreach ($users_array['users'][$user_id]['contacts'] as $contacts_id => $contacts_array) { $contacts_array['user_id'] = $user_id_new; $dbQuery = "INSERT INTO rc_contacts SET"; foreach ($contacts_array as $kcontacts=>$vcontacts) { if (!is_array($vcontacts)) { if ($vcontacts == 'NULL') { $dbQuery .= " `$kidentities` = $vcontacts,"; } else { $vcontacts = mysqli_escape_string($dbLink, $vcontacts); $dbQuery .= " `$kcontacts` = '$vcontacts',"; } } } $dbQuery = rtrim($dbQuery, ","); $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows != '1') { echo "ERROR: $dbResult = $dbAffectedRows\n"; } $contact_id_new = mysqli_insert_id($dbLink); $contact_id_array_map[$contacts_id] = $contact_id_new; } } /* contactgroups */ if (!empty($users_array['users'][$user_id]['contactgroups'])) { foreach ($users_array['users'][$user_id]['contactgroups'] as $contactgroups_id => $contactgroups_array) { $contactgroups_array['user_id'] = $user_id_new; $dbQuery = "INSERT INTO rc_contactgroups SET"; foreach ($contactgroups_array as $kcontactgroups=>$vcontactgroups) { if (!is_array($vcontactgroups)) { if ($vcontactgroups == 'NULL') { $dbQuery .= " `$kidentities` = $vcontactgroups,"; } else { $vcontactgroups = mysqli_escape_string($dbLink, $vcontactgroups); $dbQuery .= " `$kcontactgroups` = \"$vcontactgroups\","; } } } $dbQuery = rtrim($dbQuery, ","); $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows != '1') { echo "ERROR: $dbResult = $dbAffectedRows\n"; } $contactgoup_id_new = mysqli_insert_id($dbLink); /* contactgroupmembers */ if (!empty($users_array['users'][$user_id]['contactgroups'][$contactgroups_id]['contactgroupmembers'])) { foreach ($users_array['users'][$user_id]['contactgroups'][$contactgroups_id]['contactgroupmembers'] as $contactgroupmembers_id => $contactgroupmembers_array) { $contactgroupmembers_array['contactgroup_id'] = $contactgoup_id_new; $contactgroupmembers_array['contact_id'] = $contact_id_array_map[$contactgroupmembers_array['contact_id']]; $dbQuery = "INSERT INTO rc_contactgroupmembers SET"; foreach ($contactgroupmembers_array as $kcontactgroupmembers=>$vcontactgroupmembers) { if (!is_array($vcontactgroupmembers)) { $vcontactgroupmembers = mysqli_escape_string($dbLink, $vcontactgroupmembers); $dbQuery .= " `$kcontactgroupmembers` = \"$vcontactgroupmembers\","; } } $dbQuery = rtrim($dbQuery, ","); $dbResult = mysqli_query($dbLink, $dbQuery); $dbAffectedRows = mysqli_affected_rows($dbLink); if ($dbAffectedRows != '1') { echo "ERROR: $dbResult = $dbAffectedRows\n"; } } } } } } else { echo "$username already exists, skipping\n"; } } }