source = $source; } public function import() { $fp = gzopen($this->source, 'rb'); $b = gzread($fp, 2); // Import v2 if ($b == '##') { $current = false; $ecrit = null; $forum = null; $file = null; while (!gzeof($fp)) { $line = gzgets($fp, 4096); if (preg_match('!^##lencrier\.backup\.(.*)$!', $line, $match)) { if ($ecrit) { if (!$this->addEcrit($ecrit['title'], $ecrit['content'], $ecrit['date'], $ecrit['status'])) { return false; } $ecrit = null; } $current = explode('.', $match[1]); if ($current[1] == 'ecrit') { $ecrit = null; } continue; } elseif (!$current || $current[0] != 'section') { continue; } if ($current[1] == 'journal') { // Inutilisé pour le moment } elseif ($current[1] == 'ecrit') { if (is_null($ecrit)) $ecrit = array(); if ($current[3] == 'metas') { if (preg_match('!^title="(.*)"$!', $line, $match)) { $ecrit['titre'] = $match[1]; } elseif (preg_match('!^date="(.*)"$!', $line, $match)) { $d = DateTime::createFromFormat(DATE_W3C, $match[1]); $ecrit['date'] = $d->getTimestamp(); } elseif (preg_match('!^status="(.*)"$!', $line, $match)) { $ecrit['status'] = (int) $match[1]; } } elseif ($current[3] == 'content' && $current[4] == 'html') { if (!isset($ecrit['content'])) { $ecrit['content'] = ''; } $ecrit['content'] .= $line; } } elseif ($current[1] == 'forum') { if (is_null($forum)) { $forum = array(); } if ($current[3] == 'metas') { // FIXME } elseif ($current[3] == 'content' && $current[4] == 'html') { if (!isset($forum['content'])) { $forum['content'] = ''; } $forum['content'] .= $line; } } elseif ($current[1] == 'file') { if (is_null($file)) $file = array(); if ($current[3] == 'metas') { if (preg_match('!^name="(.*)"$!', $line, $match)) { $file['name'] = $match[1]; } elseif (preg_match('!^directory="(.*)"$!', $line, $match)) { $file['directory'] = $match[1]; } elseif (preg_match('!^hash="(.*)"$!', $line, $match)) { $file['hash'] = $match[1]; } } elseif ($current[3] == 'content') { if (!isset($file['p'])) { //$file['p'] = fopen(utils::getJournalDatasPath($this->id) . // FIXME } } } } } else { fclose($fp); return $this->importV1(); } } 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; } } ?>