source = $source; } public function import() { $fp = gzopen($this->source, 'rb'); $v2 = null; $section = null; $content = ''; $id = null; $part = null; while (!gzeof($fp)) { $line = gzgets($fp, 4096); if (!$v2) { if (trim($line) != '##lencrier.backup.header.v2') { fclose($fp); return $this->importV1(); } $v2 = true; continue; } if (preg_match('!^##lencrier\.backup\.section\.(\w+)(?:\.(\d+)\.([\w\.]+))?$!', trim($line), $match)) { if ($section) { $this->$section($content, $id, $part); } $content = ''; $section = $match[1]; $id = !empty($match[2]) ? (int) $match[2] : null; $part = !empty($match[3]) ? $match[3] : null; } else { $content .= $line; } } fclose($fp); if ($section) { $this->$section($content, $id, $part); } } protected function journal(string $content) { $content = parse_ini_string($content); if (empty($content['title'])) { return; } $journal = new Journal($this->id); $journal->set('titre', $content['title']); $journal->save(); } protected function ecrit(string $content, int $id, string $part) { static $metas = null; if ($part == 'metas') { $metas = parse_ini_string($content); return; } if (empty($metas['title']) || empty($content)) { throw new \RuntimeException('Incorrect file order metas/content'); } Ecrit::addNew($this->id, $metas['title'], $content, $metas['status'] ?? 0, strtotime($metas['date'])); $metas = null; } protected function forum(string $content, int $id, string $part) { // FIXME TODO return; } protected function file(string $content, int $id, string $part) { static $metas = null; if ($part == 'metas') { $metas = parse_ini_string($content); return; } if (empty($metas['name']) || empty($content)) { throw new \RuntimeException('Incorrect file order metas/content'); } if (md5(trim($content)) != $metas['hash']) { return; } $files = new Journal_Files($this->id); $path = ''; if ($metas['directory'] == 'images') { $files->createDir('', 'images'); $path = 'images'; } $path = $files->storeFromString($path, $metas['name'], $content); } protected function importV1() { $tar = new tar; if (!$tar->openTAR($this->source)) throw new technicalException("File ".$this->source." is invalid"); $base = LENCRIER_DATA_ROOT . '/tmp/' . md5(uniqid(rand())); mkdir($base); if ($tar->numDirectories > 0) { foreach($tar->directories as $id => $information) { if(!mkdir($base . '/' . $information['name'])) { throw new technicalException("Can't create ".$base.'/'.$information['name']); } } } $ecrits = array(); if($tar->numFiles > 0) { foreach($tar->files as $id => $file) { if (preg_match('!ecrit_([0-9]+)\.metas$!', $file['name'], $match)) { $nb = $match[1]; if (!array_key_exists($nb, $ecrits)) $ecrits[$nb] = array(); $metas = explode("\n", $file['file']); foreach ($metas as $meta) { $meta = trim($meta); if (preg_match('!^title\s*=\s*"(.*)"$!', $meta, $match)) $ecrits[$nb]['title'] = str_replace('\\"', '"', $match[1]); elseif (preg_match('!^date\s*=\s*"(.*)"$!', $meta, $match)) $ecrits[$nb]['date'] = (int) $match[1]; elseif (preg_match('!^status\s*=\s*"(.*)"$!', $meta, $match)) $ecrits[$nb]['status'] = $match[1]; } } elseif (preg_match('!ecrit_([0-9]+)\.html$!', $file['name'], $match)) { $nb = $match[1]; if (!array_key_exists($nb, $ecrits)) $ecrits[$nb] = array(); $ecrits[$nb]['content'] = $file['file']; } elseif (!file_put_contents($base . '/' . $file['name'], $file['file'])) { throw new technicalException("Can't create ".$base.'/'.$information['name']); } } } unset($tar); foreach ($ecrits as &$ecrit) { if (!$this->addEcrit($ecrit['title'], $ecrit['content'], $ecrit['date'], $ecrit['status'])) return false; } utils::recurseCopy($base . '/documents', LENCRIER_DATA_ROOT . '/documents/' . $this->id); utils::recurseDelete($base); return true; } }