Subversion Repositories Sites.tela-botanica.org

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 david 1
<?php
2
 
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2005                                                *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
10
 *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
 
13
 
14
//
15
// Ce fichier ne sera execute qu'une fois
16
if (defined("_ECRIRE_INC_IMPORT")) return;
17
define("_ECRIRE_INC_IMPORT", "1");
18
 
19
 
20
include_ecrire ("inc_acces.php3");
21
 
22
 
23
function xml_fetch_tag($f, &$before, $gz=false) {
24
	global $buf, $pos, $abs_pos;
25
	static $buf_len = 1000;
26
	$_fread = ($gz) ? gzread : fread;
27
	$_feof = ($gz) ? gzeof : feof;
28
	$_ftell = ($gz) ? gztell : ftell;
29
	$p = $pos;
30
 
31
	$q = @strpos($buf, '<', $p);
32
	while (!$q AND substr($buf, $p, 1) != '<') {
33
		if ($_feof($f)) return false;
34
		$before .= substr($buf, $p);
35
		$buf = $_fread($f, $buf_len);
36
		$p = 0;
37
		$q = strpos($buf, '<');
38
	}
39
	$before .= substr($buf, $p, $q - $p);
40
	$tag = '';
41
	$p = ++$q;
42
	$q = @strpos($buf, '>', $p);
43
	while (!$q AND substr($buf, $p, 1) != '>') {
44
		if ($_feof($f)) return false;
45
		$tag .= substr($buf, $p);
46
		$buf = $_fread($f, $buf_len);
47
		$p = 0;
48
		$q = strpos($buf, '>');
49
	}
50
	$pos = $q + 1;
51
	$tag .= substr($buf, $p, $q - $p);
52
	$before = str_replace('&amp;', '&', str_replace('&lt;', '<', $before));
53
	$abs_pos = $_ftell($f) - strlen($buf);
54
	return $tag;
55
}
56
 
57
 
58
function xml_parse_tag($texte) {
59
	list($tag, $atts) = split('[[:space:]]+', $texte, 2);
60
	$result[0] = $tag;
61
	$result[1] = '';
62
	if (!$atts) return $result;
63
	while (ereg('^([^[:space:]]+)[[:space:]]*=[[:space:]]*"([^"]*)"([[:space:]]+(.*))?', $atts, $regs)) {
64
		$result[1][$regs[1]] = $regs[2];
65
		$atts = $regs[4];
66
	}
67
	return $result;
68
}
69
 
70
 
71
function import_debut($f, $gz=false) {
72
	$b = "";
73
	while ($t = xml_fetch_tag($f, $b, $gz)) {
74
		$r = xml_parse_tag($t);
75
		if ($r[0] == '?xml' AND $r[1]['encoding'])
76
			ecrire_meta('charset_restauration', strtolower($r[1]['encoding']));
77
		if ($r[0] == "SPIP") return $r;
78
		$b = "";
79
	}
80
	return false;
81
}
82
 
83
 
84
//
85
// $f = handle fichier
86
// $gz = flag utilisation zlib
87
//
88
// importe un objet depuis le fichier, retourne true si ok, false si erreur ou fin de fichier
89
//
90
 
91
function import_objet_1_2($f, $gz=false) {
92
	global $import_ok, $pos, $abs_pos;
93
	static $time_javascript;
94
 
95
	if (time() - $time_javascript > 3) {	// 3 secondes
96
		affiche_progression_javascript($abs_pos);
97
		$time_javascript = time();
98
	}
99
 
100
	static $tables;
101
	if (!$tables) $tables = array(
102
		'article' => 'spip_articles',
103
		'auteur' => 'spip_auteurs',
104
		'breve' => 'spip_breves',
105
		'document' => 'spip_documents',
106
		'forum' => 'spip_forum',
107
		'groupe_mots' => 'spip_groupes_mots',
108
		'message' => 'spip_messages',
109
		'mot' => 'spip_mots',
110
		'petition' => 'spip_petitions',
111
		'rubrique' => 'spip_rubriques',
112
		'signature' => 'spip_signatures',
113
		'syndic' => 'spip_syndic',
114
		'syndic_article' => 'spip_syndic_articles',
115
		'type_document' => 'spip_types_documents'
116
	);
117
 
118
	$import_ok = false;
119
	$b = '';
120
	// Lire le type d'objet
121
	if (!($type = xml_fetch_tag($f, $b, $gz))) return false;
122
	if ($type == '/SPIP') return !($import_ok = true);
123
	$id = "id_$type";
124
	$id_objet = 0;
125
 
126
	// Lire les champs de l'objet
127
	for (;;) {
128
		$b = '';
129
		if (!($col = xml_fetch_tag($f, $b, $gz))) return false;
130
		if ($col == '/'.$type) break;
131
		$value = '';
132
		if (!xml_fetch_tag($f, $value, $gz)) return false;
133
		if (substr($col, 0, 5) == 'lien:') {
134
			$type_lien = substr($col, 5);
135
			$liens[$type_lien][] = '('.$id_objet.','.$value.')';
136
		}
137
		else if ($col != 'maj') {
138
			// tentative de restauration d'une base sauvegardee avec le champ 'images' ; d'experience, ca arrive...
139
			// mieux vaut accepter que canner silencieusement...
140
			if (($type == 'article') && ($col == 'images'))
141
			{
142
				if ($value) {		// ne pas afficher de message si on a un champ suppl mais vide
143
					echo "--><br><font color='red'><b>"._T('avis_erreur_sauvegarde', array('type' => $type, 'id_objet' => $id_objet))."</b></font>\n<font color='black'>"._T('avis_colonne_inexistante', array('col' => $col));
144
					if ($col == 'images') echo _T('info_verifier_image');
145
					echo "</font>\n<!--";
146
					$GLOBALS['erreur_restauration'] = true;
147
				}
148
			}
149
			else {
150
				$cols[] = $col;
151
				$values[] = '"'.addslashes($value).'"';
152
				if ($col == $id) $id_objet = $value;
153
			}
154
		}
155
	}
156
 
157
	$table = $tables[$type];
158
	$query = "REPLACE $table (" . join(',', $cols) . ') VALUES (' . join(',', $values) . ')';
159
	if (!spip_query($query)) {
160
		echo "--><br><font color='red'><b>"._T('avis_erreur_mysql')."</b></font>\n<font color='black'><tt>".spip_sql_error()."</tt></font>\n<!--";
161
		$GLOBALS['erreur_restauration'] = true;
162
	}
163
 
164
	if ($type == 'article') {
165
		spip_query("DELETE FROM spip_auteurs_articles WHERE id_article=$id_objet");
166
		spip_query("DELETE FROM spip_documents_articles WHERE id_article=$id_objet");
167
	}
168
	else if ($type == 'rubrique') {
169
		spip_query("DELETE FROM spip_auteurs_rubriques WHERE id_rubrique=$id_objet");
170
		spip_query("DELETE FROM spip_documents_rubriques WHERE id_rubrique=$id_objet");
171
	}
172
	else if ($type == 'breve') {
173
		spip_query("DELETE FROM spip_documents_breves WHERE id_breve=$id_objet");
174
	}
175
	else if ($type == 'mot') {
176
		spip_query("DELETE FROM spip_mots_articles WHERE id_mot=$id_objet");
177
		spip_query("DELETE FROM spip_mots_breves WHERE id_mot=$id_objet");
178
		spip_query("DELETE FROM spip_mots_forum WHERE id_mot=$id_objet");
179
		spip_query("DELETE FROM spip_mots_rubriques WHERE id_mot=$id_objet");
180
		spip_query("DELETE FROM spip_mots_syndic WHERE id_mot=$id_objet");
181
	}
182
	else if ($type == 'auteur') {
183
		spip_query("DELETE FROM spip_auteurs_rubriques WHERE id_auteur=$id_objet");
184
	}
185
	else if ($type == 'message') {
186
		spip_query("DELETE FROM spip_auteurs_messages WHERE id_message=$id_objet");
187
	}
188
	if ($liens) {
189
		reset($liens);
190
		while (list($type_lien, $t) = each($liens)) {
191
			if ($type == 'auteur' OR $type == 'mot' OR $type == 'document')
192
				if ($type_lien == 'syndic' OR $type_lien == 'forum') $table_lien = 'spip_'.$type.'s_'.$type_lien;
193
				else $table_lien = 'spip_'.$type.'s_'.$type_lien.'s';
194
			else
195
				$table_lien = 'spip_'.$type_lien.'s_'.$type.'s';
196
			$query = "INSERT INTO $table_lien ($id, id_$type_lien) VALUES ".join(',', $t);
197
			spip_query($query);
198
		}
199
	}
200
 
201
	$p = $pos + $abs_pos;
202
	ecrire_meta("status_restauration", "$p");
203
 
204
	return $import_ok = true;
205
}
206
 
207
 
208
function import_objet_0_0($f, $gz=false) {
209
	global $import_ok, $pos, $abs_pos;
210
 
211
	$import_ok = false;
212
	$b = '';
213
	if (!($type = xml_fetch_tag($f, $b, $gz))) return false;
214
	if ($type == '/SPIP') return !($import_ok = true);
215
	$is_art = ($type == 'article');
216
	$is_mot = ($type == 'mot');
217
	for (;;) {
218
		$b = '';
219
		if (!($col = xml_fetch_tag($f, $b, $gz))) return false;
220
		if ($col == ("/$type")) break;
221
		$value = '';
222
		if (!xml_fetch_tag($f, $value, $gz)) return false;
223
		if ($is_art AND $col == 'id_auteur') {
224
			$auteurs[] = $value;
225
		}
226
		else if ($is_mot AND $col == 'id_article') {
227
			$articles[] = $value;
228
		}
229
		else if ($is_mot AND $col == 'id_breve') {
230
			$breves[] = $value;
231
		}
232
		else if ($is_mot AND $col == 'id_forum') {
233
			$forums[] = $value;
234
		}
235
		else if ($is_mot AND $col == 'id_rubrique') {
236
			$rubriques[] = $value;
237
		}
238
		else if ($is_mot AND $col == 'id_syndic') {
239
			$syndics[] = $value;
240
		}
241
		else if ($col != 'maj') {
242
			$cols[] = $col;
243
			$values[] = '"'.addslashes($value).'"';
244
			if ($is_art && ($col == 'id_article')) $id_article = $value;
245
			if ($is_mot && ($col == 'id_mot')) $id_mot = $value;
246
		}
247
	}
248
 
249
	$table = "spip_$type";
250
	if ($type != 'forum' AND $type != 'syndic') $table .= 's';
251
	$query = "REPLACE $table (" . join(",", $cols) . ") VALUES (" . join(",", $values) . ")";
252
	spip_query($query);
253
 
254
	if ($is_art && $id_article) {
255
		$query = "DELETE FROM spip_auteurs_articles WHERE id_article=$id_article";
256
		spip_query($query);
257
		if ($auteurs) {
258
			reset ($auteurs);
259
			while (list(, $auteur) = each($auteurs)) {
260
				$query = "INSERT INTO spip_auteurs_articles (id_auteur, id_article) VALUES ($auteur, $id_article)";
261
				spip_query($query);
262
			}
263
		}
264
	}
265
	if ($is_mot && $id_mot) {
266
		$query = "DELETE FROM spip_mots_articles WHERE id_mot=$id_mot";
267
		spip_query($query);
268
		$query = "DELETE FROM spip_mots_breves WHERE id_mot=$id_mot";
269
		spip_query($query);
270
		$query = "DELETE FROM spip_mots_forum WHERE id_mot=$id_mot";
271
		spip_query($query);
272
		$query = "DELETE FROM spip_mots_rubriques WHERE id_mot=$id_mot";
273
		spip_query($query);
274
		$query = "DELETE FROM spip_mots_syndic WHERE id_mot=$id_mot";
275
		spip_query($query);
276
		if ($articles) {
277
			reset ($articles);
278
			while (list(, $article) = each($articles)) {
279
				$query = "INSERT INTO spip_mots_articles (id_mot, id_article) VALUES ($id_mot, $article)";
280
				spip_query($query);
281
			}
282
		}
283
		if ($breves) {
284
			reset ($breves);
285
			while (list(, $breve) = each($breves)) {
286
				$query = "INSERT INTO spip_mots_breves (id_mot, id_breve) VALUES ($id_mot, $breve)";
287
				spip_query($query);
288
			}
289
		}
290
		if ($forums) {
291
			reset ($forums);
292
			while (list(, $forum) = each($forums)) {
293
				$query = "INSERT INTO spip_mots_forum (id_mot, id_forum) VALUES ($id_mot, $forum)";
294
				spip_query($query);
295
			}
296
		}
297
		if ($rubriques) {
298
			reset ($rubriques);
299
			while (list(, $rubrique) = each($rubriques)) {
300
				$query = "INSERT INTO spip_mots_rubriques (id_mot, id_rubrique) VALUES ($id_mot, $id_rubrique)";
301
				spip_query($query);
302
			}
303
		}
304
		if ($syndics) {
305
			reset ($syndics);
306
			while (list(, $syndic) = each($syndics)) {
307
				$query = "INSERT INTO spip_mots_syndic (id_mot, id_syndic) VALUES ($id_mot, $syndic)";
308
				spip_query($query);
309
			}
310
		}
311
	}
312
 
313
	$p = $pos + $abs_pos;
314
	ecrire_meta("status_restauration", "$p");
315
//	ecrire_metas();
316
 
317
	return $import_ok = true;
318
}
319
 
320
function import_objet($f, $gz = false) {
321
	return import_objet_1_2($f, $gz);
322
}
323
 
324
function import_fin() {
325
	// Effacer l'ancien acces admin
326
	$query = "DELETE FROM spip_auteurs WHERE id_auteur=0";
327
	spip_query($query);
328
 
329
	if ($charset = lire_meta('charset_restauration'))
330
		ecrire_meta('charset', $charset);
331
	effacer_meta("charset_restauration");
332
	effacer_meta("status_restauration");
333
	effacer_meta("debut_restauration");
334
	effacer_meta("date_optimisation");
335
	ecrire_meta('calculer_rubriques', 'oui');
336
	ecrire_metas();
337
}
338
 
339
function import_abandon() {
340
	// Probleme pour restaurer l'ancien acces admin : il conserve un id_auteur = 0
341
 
342
	effacer_meta("charset_restauration");
343
	effacer_meta("status_restauration");
344
	effacer_meta("debut_restauration");
345
	effacer_meta("date_optimisation");
346
	ecrire_metas();
347
}
348
 
349
 
350
function import_all($f, $gz=false) {
351
	global $import_ok;
352
	global $auth_htaccess;
353
	global $connect_id_auteur;
354
	$_fseek = ($gz) ? gzseek : fseek;
355
 
356
	// utiliser une version fraiche des metas (ie pas le cache)
357
	include_ecrire('inc_meta.php3');
358
	lire_metas();
359
 
360
	$my_date = lire_meta_maj("debut_restauration");
361
	if (!$my_date) return false;
362
 
363
	$my_pos = lire_meta("status_restauration");
364
 
365
	if (!$my_pos) {
366
		// Debut de l'importation
367
		ecrire_meta('charset_restauration', 'iso-8859-1');
368
		if (!($r = import_debut($f, $gz))) {
369
			ecrire_meta("erreur", _T('avis_archive_incorrect'));
370
			return false;
371
		}
372
		else {
373
			// Bidouille pour garder l'acces admin actuel pendant toute la restauration
374
			$query = "UPDATE spip_auteurs SET id_auteur=0 WHERE id_auteur=$connect_id_auteur";
375
			spip_query($query);
376
 
377
			$version_archive = $r[1]['version_archive'];
378
			ecrire_meta('version_archive_restauration', $version_archive);
379
		}
380
	}
381
	else {
382
		// Reprise de l'importation
383
		$_fseek($f, $my_pos);
384
		$version_archive = lire_meta('version_archive_restauration');
385
	}
386
 
387
	// Restauration des entrees du fichier
388
 
389
	switch ($version_archive) {
390
	case '1.2':
391
		while (import_objet_1_2($f, $gz));
392
		break;
393
	default:
394
		while (import_objet_0_0($f, $gz));
395
		break;
396
	}
397
	if (!$import_ok) {
398
		ecrire_meta("erreur", _T('avis_archive_invalide'));
399
		return false;
400
	}
401
 
402
	// Mise a jour du fichier htpasswd
403
 
404
	ecrire_acces();
405
 
406
	// Destruction des entrees non restaurees
407
 
408
	$query = "DELETE FROM spip_rubriques WHERE UNIX_TIMESTAMP(maj) < $my_date";
409
	spip_query($query);
410
	$query = "DELETE FROM spip_breves WHERE UNIX_TIMESTAMP(maj) < $my_date";
411
	spip_query($query);
412
	$query = "DELETE FROM spip_auteurs WHERE UNIX_TIMESTAMP(maj) < $my_date";
413
	spip_query($query);
414
	$query = "DELETE FROM spip_articles WHERE UNIX_TIMESTAMP(maj) < $my_date";
415
	spip_query($query);
416
	$query = "DELETE FROM spip_documents WHERE UNIX_TIMESTAMP(maj) < $my_date";
417
	spip_query($query);
418
	$query = "DELETE FROM spip_types_documents WHERE UNIX_TIMESTAMP(maj) < $my_date";
419
	spip_query($query);
420
	$query = "DELETE FROM spip_forum WHERE UNIX_TIMESTAMP(maj) < $my_date";
421
	spip_query($query);
422
	$query = "DELETE FROM spip_mots WHERE UNIX_TIMESTAMP(maj) < $my_date";
423
	spip_query($query);
424
	$query = "DELETE FROM spip_groupes_mots WHERE UNIX_TIMESTAMP(maj) < $my_date";
425
	spip_query($query);
426
	$query = "DELETE FROM spip_petitions WHERE UNIX_TIMESTAMP(maj) < $my_date";
427
	spip_query($query);
428
	$query = "DELETE FROM spip_signatures WHERE UNIX_TIMESTAMP(maj) < $my_date";
429
	spip_query($query);
430
	$query = "DELETE FROM spip_visites WHERE UNIX_TIMESTAMP(maj) < $my_date";
431
	spip_query($query);
432
 
433
	import_fin();
434
 
435
	affiche_progression_javascript('100 %');
436
 
437
	return true;
438
}
439
 
440
 
441
function affiche_progression_javascript($abs_pos) {
442
	global $affiche_progression_pourcent;
443
	include_ecrire('inc_charsets.php3');
444
	flush();
445
	echo " -->\n<script type='text/javascript'><!--\n";
446
 
447
	if ($abs_pos == '100 %') {
448
		$taille = $abs_pos;
449
		if ($GLOBALS['erreur_restauration'])
450
			echo "document.progression.recharge.value='".str_replace("'", "\\'", unicode_to_javascript(_T('avis_erreur')))."';\n";
451
		else
452
			echo "document.progression.recharge.value='".str_replace("'", "\\'", unicode_to_javascript(_T('info_fini')))."';\n";
453
	}
454
	else if (! $affiche_progression_pourcent)
455
		$taille = ereg_replace("&nbsp;", " ", taille_en_octets($abs_pos));
456
	else
457
		$taille = floor(100 * $abs_pos / $affiche_progression_pourcent)." %";
458
 
459
	echo "document.progression.taille.value='$taille';\n";
460
	echo "//--></script>\n<!--\n";
461
	flush();
462
}
463
 
464
function import_init()
465
{
466
  global $meta, $flag_gz, $buf, $pos, $abs_pos;
467
	$archive = $meta["fichier_restauration"];
468
	$my_pos = $meta["status_restauration"];
469
	$ok = @is_readable($archive);
470
 
471
	if ($ok) {
472
		if (ereg("\.gz$", $archive)) {
473
			$affiche_progression_pourcent = false;
474
			$taille = taille_en_octets($my_pos);
475
			$gz = true;
476
		}
477
		else {
478
			$affiche_progression_pourcent = filesize($archive);
479
			$taille = floor(100 * $my_pos / $affiche_progression_pourcent)." %";
480
			$gz = false;
481
		}
482
		$texte_boite = _T('info_base_restauration')."<p>
483
		<form name='progression'><center><input type='text' size=10 style='text-align:center;' name='taille' value='$taille'><br>
484
		<input type='text' class='forml' name='recharge' value='"._T('info_recharger_page')."'></center></form>";
485
	}
486
	else {
487
		$texte_boite = _T('info_erreur_restauration');
488
	}
489
 
490
	debut_boite_alerte();
491
	echo "<font FACE='Verdana,Arial,Sans,sans-serif' SIZE=4 color='black'><B>$texte_boite</B></font>";
492
	fin_boite_alerte();
493
	fin_page("jimmac");
494
	echo "</HTML><font color='white'>\n<!--";
495
	@flush();
496
 
497
	if ($ok) {
498
		$_fopen = ($gz) ? gzopen : fopen;
499
		$f = $_fopen($archive, "rb");
500
		$pos = 0;
501
		$buf = "";
502
		if (!import_all($f, $gz)) import_abandon();
503
	}
504
	else {
505
		import_fin();
506
	}
507
}
508
 
509
?>