initialisation automatique avec ini.php
[bazdig.git] / lib / pagecopy.php
blob6a7d397d55b5aab0f683b20676363d749f6374a1
1 <?php
2 require_once "localresource.php";
4 class PageCopy extends LocalResource
6 var $date;
7 var $origin;
8 var $endOfHTML = '';
9 var $startOfHEAD = '';
10 var $endOfHEAD = '';
12 function __construct($name, $origin, $root)
14 $this->date = date("c");
15 $this->origin = $origin;
16 $c = $root->get($name);
17 $this->file = $c->get_file();
18 $this->url = $c->get_url();
21 function update()
23 $ch = curl_init();
24 $timeout = 5; // set to zero for no timeout
25 curl_setopt ($ch, CURLOPT_URL, $this->origin);
26 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
27 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
28 $file_contents = curl_exec($ch);
29 curl_close($ch);
31 if ($content = $file_contents) {
33 if (eregi("<head>", $content)) {
34 $content = eregi_replace("<head>", "<head>". $this->startOfHEAD , $content);
35 } else {
36 $content = eregi_replace("^", "<head>". $this->startOfHEAD , $content);
38 if (eregi("</head>", $content)) {
39 $content = eregi_replace("</head>", $this->endOfHEAD . "</head>", $content);
40 } else {
41 $content = eregi_replace("^", $this->endOfHEAD . "</head>", $content);
43 if (eregi("</html>", $content)) {
44 $content = eregi_replace("</html>", $this->endOfHTML . "</html>", $content);
45 } else {
46 $content = eregi_replace("$", $this->endOfHTML . "</html>", $content);
48 $copy = fopen($this->file, "w");
49 if (! fwrite($copy, $content)) {
50 throw new Exception("Err: Ecriture");
55 function add_to_html($s)
57 $this->endOfHTML .= $s;
60 function add_to_head($s)
62 $this->endOfHEAD .= $s;
65 function add_to_head_start($s)
67 $this->startOfHEAD .= $s;
71 function url2fileName($url)
73 $fileName = $url;
74 $forbidden = array('/',':','?','=','&','+','%');
75 $fileName = ereg_replace('^http://', '', $fileName); //remove http://
76 $fileName = ereg_replace('#.*$', '', $fileName); //remove fragment id
77 $fileName = str_replace($forbidden, '_', $fileName);
79 return $fileName;