diff --git a/classes/profile.php b/classes/profile.php index 6e718af..2979194 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -79,6 +79,21 @@ class profile extends persistent { /** Cron tasks */ const SCRIPTTYPE_TASK = 4; + /** Profile fields to ignore during imports */ + const IMPORT_IGNORE = [ + 'id', + 'domain', + ]; + + /** Profile fields that are site specific and should be locked behind a site check during imports */ + const IMPORT_CHECK_SITE = [ + 'referer', + 'userid', + 'courseid', + 'lockreason', + 'lockwaiturl', + ]; + /** * Custom setter to set the flame data. * @@ -144,22 +159,18 @@ public function get_uncompressed_json(string $fieldname): string { /** * Gets the JSON for an export. * - * @param mixed $removesitedata whether to remove site specific data * @return string JSON */ - protected function get_export_json($removesitedata = true): string { + protected function get_export_json(): string { + global $CFG; + $data = new \stdClass(); - // Remove unneeded properties from the export. - $allproperties = array_keys(static::properties_definition()); - $removeproperties = ['id']; - if ($removesitedata) { - // Also remove data that is site specific or contains full urls. - $removeproperties += ['referer', 'userid', 'courseid', 'sessionid', 'lockreason', 'lockwaiturl']; - } - $properties = array_diff($allproperties, $removeproperties); + // Add domain so imports can match sites. + $data->domain = parse_url($CFG->wwwroot, PHP_URL_HOST); // Load data the same way as to_record(), but use get() to transform d3 data. + $properties = array_keys(static::properties_definition()); foreach ($properties as $property) { $data->$property = $this->get($property); } @@ -188,13 +199,28 @@ public function download(): void { * @return bool|int the id of the imported profile, or false if unsuccessful */ public static function import(string $json) { - global $DB; + global $CFG, $DB; $profile = new profile(); $data = json_decode($json); - // Don't mark this as slow on external sites. - $data->reason = self::REASON_IMPORT; + // Remove properties that shouldn't be imported. + $removeproperties = self::IMPORT_IGNORE; + + // If uploading to a different site, we should also remove data that is site specific. + $domain = parse_url($CFG->wwwroot, PHP_URL_HOST); + if (!isset($data->domain) || $data->domain !== $domain) { + // Remove ids, locks and full urls. + $removeproperties = array_merge($removeproperties, self::IMPORT_CHECK_SITE); + + // Don't copy the reason to external sites. + $data->reason = 0; + } + + // Add import to reasons. + if (isset($data->reason) && $data->reason !== self::REASON_IMPORT) { + $data->reason += self::REASON_IMPORT; + } // Convert flamedatad3 to flame_node. if (isset($data->flamedatad3)) { @@ -202,7 +228,7 @@ public static function import(string $json) { } foreach ($data as $property => $value) { - if (isset($value)) { + if (isset($value) && !in_array($property, $removeproperties)) { $profile->set($property, $value); } }