/** * Prepares an archive containing all of this site's data. * @return string */ static function exportToFolder($dir = false) { set_time_limit(0); // Switch off the time limit for PHP site()->currentPage()->setPermalink(true); // Prepare a unique name for the archive $name = md5(time() . rand(0, 9999) . site()->config()->getURL()); // If $folder is false or doesn't exist, use the temporary directory and ensure it has a slash on the end of it if (!is_dir($dir)) { $dir = site()->config()->getTempDir(); } // Make the temporary directory, or fail out if (!@mkdir($dir . $name)) { return false; } $json_path = $dir . $name . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR; if (!@mkdir($json_path)) { return false; } $html_path = $dir . $name . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR; if (!@mkdir($html_path)) { return false; } $file_path = $dir . $name . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; if (!@mkdir($file_path)) { return false; } if (!@mkdir($file_path . 'readable', 0777, true)) { return false; } if (!@mkdir($file_path . 'uploads', 0777, true)) { return false; } // If we've made it here, we've created a temporary directory with the hash name $config = array('url' => site()->config()->getURL(), 'title' => site()->config()->getTitle()); file_put_contents($dir . $name . DIRECTORY_SEPARATOR . 'known.json', json_encode($config)); $all_in_one_json = ''; // Let's export everything. $fields = array(); $query_parameters = array('entity_subtype' => array('$not' => array('$in' => array('Idno\\Entities\\ActivityStreamPost')))); $collection = 'entities'; if ($results = site()->db()->getRecords($fields, $query_parameters, 99999, 0, $collection)) { foreach ($results as $id => $row) { $object = site()->db()->rowToEntity($row); if (!empty($object->_id) && $object instanceof Entity) { $object_name = $object->_id; $attachments = $object->attachments; if (empty($attachments)) { $attachments = []; } foreach (['thumbnail', 'thumbnail_large'] as $thumbnail) { if (!empty($object->{$thumbnail})) { if (preg_match('/file\\/([a-zA-Z0-9]+)\\//', $object->{$thumbnail}, $matches)) { $attachments[] = ['url' => $object->{$thumbnail}, '_id' => $matches[1]]; } } } if (!empty($attachments)) { foreach ($attachments as $key => $attachment) { if ($data = File::getFileDataFromAttachment($attachment)) { $filename = $attachment['_id']; $id = $attachment['_id']; if ($ext = pathinfo($attachment['url'], PATHINFO_EXTENSION)) { $filename .= '.' . $ext; } if (!empty($attachment['mime-type'])) { $mime_type = $attachment['mime-type']; } else { $mime_type = 'application/octet-stream'; } file_put_contents($file_path . 'readable/' . $filename, $data); $attachments[$key]['url'] = '../files/' . site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id[3] . '/' . $id . '.file'; //$filename; $data_file = $file_path . 'uploads/' . \Idno\Core\site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id[3] . '/' . $id . '.data'; foreach (array($file_path . 'uploads/' . \Idno\Core\site()->config()->pathHost(), $file_path . \Idno\Core\site()->config()->pathHost() . '/' . $id[0], $file_path . 'uploads/' . \Idno\Core\site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1], $file_path . 'uploads/' . \Idno\Core\site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1] . '/' . $id[2], $file_path . 'uploads/' . \Idno\Core\site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id[3]) as $up_path) { if (!is_dir($up_path)) { $result = mkdir($up_path, 0777, true); } } file_put_contents($file_path . 'uploads/' . site()->config()->pathHost() . '/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id[3] . '/' . $id . '.file', $data); file_put_contents($data_file, json_encode(['filename' => $filename, 'mime_type' => $mime_type])); } } $object->attachments = $attachments; } $activityStreamPost = new \Idno\Entities\ActivityStreamPost(); if ($owner = $object->getOwner()) { $activityStreamPost->setOwner($owner); $activityStreamPost->setActor($owner); $activityStreamPost->setTitle(sprintf("%s posted %s", $owner->getTitle(), $object->getTitle())); } $activityStreamPost->setVerb('post'); $activityStreamPost->setObject($object); $json_object = json_encode($object); file_put_contents($json_path . $object_name . '.json', $json_object); $all_in_one_json[] = json_decode($json_object); if (is_callable(array($object, 'draw'))) { //file_put_contents($html_path . $object_name . '.html', $activityStreamPost->draw()); file_put_contents($html_path . $object_name . '.html', $object->draw()); } //unset($results[$id]); //unset($object); gc_collect_cycles(); // Clean memory } } } if ($exported_records = \Idno\Core\site()->db()->exportRecords()) { if (site()->config()->database == 'mysql' || site()->config()->database == 'postgres') { $export_ext = 'sql'; } else { $export_ext = 'json'; } file_put_contents($dir . $name . DIRECTORY_SEPARATOR . 'exported_data.' . $export_ext, $exported_records); } file_put_contents($dir . $name . DIRECTORY_SEPARATOR . 'entities.json', json_encode($all_in_one_json)); // As we're successful, return the unique name of the archive return $dir . $name; }
/** * Add this entity to the feed * @param string $verb Verb to use (default: post) * @param string $title Title to use. First variable is always subject; second is always title. Default: '%s posted %s' * @return bool */ function addToFeed($verb = 'post', $title = '%s posted %s') { $activityStreamPost = new \Idno\Entities\ActivityStreamPost(); $owner = $this->getOwner(); $activityStreamPost->setOwner($owner); $activityStreamPost->setActor($owner); $activityStreamPost->setTitle(sprintf($title, $owner->getTitle(), $this->getTitle())); $activityStreamPost->setVerb('post'); $activityStreamPost->setObject($this); return $activityStreamPost->save(); }