Dotclear

source: plugins/daInstaller/inc/class.da.modules.reader.php @ 1046:24b905e118ac

Revision 1046:24b905e118ac, 4.1 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

Add daInstaller plugin to the distribution, closes #1359

Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of daInstaller, a plugin for DotClear2.
4# Copyright (c) 2008-2011 Tomtom, Pep and contributors, for DotAddict.org.
5# All rights reserved.
6#
7# ***** END LICENSE BLOCK *****
8
9class daModulesReader extends netHttp
10{
11     protected $user_agent         = 'DotAddict.org RepoBrowser/0.1';
12     protected $timeout            = 5;
13     protected $validators         = null;                  ///< <b>array</b>   HTTP Cache validators
14     protected $cache_dir          = null;                  ///< <b>string</b>  Cache temporary directory
15     protected $cache_file_prefix  = 'daorg';               ///< <b>string</b>  Cache file prefix
16     protected $cache_ttl          = '-30 minutes';         ///< <b>string</b>  Cache TTL
17     protected $force              = false;
18     
19     public function __construct()
20     {
21          parent::__construct('');
22          $this->setUserAgent(daInstaller::getUserAgent());
23     }
24     
25     public function parse($url)
26     {
27          $this->validators = array();
28          if ($this->cache_dir)
29          {
30               return $this->withCache($url);
31          }
32          else
33          {
34               if (!$this->getModulesXML($url)) {
35                    return false;
36               }
37               
38               if ($this->getStatus() != '200') {
39                    return false;
40               }
41               
42               return new daModulesParser($this->getContent());
43          }
44     }
45     
46     public static function quickParse($url,$cache_dir = null,$force = false)
47     {
48          $parser = new self();
49          if ($cache_dir) {
50               $parser->setCacheDir($cache_dir);
51          }
52          if ($force) {
53               $parser->setForce($force);
54          }
55         
56          return $parser->parse($url);
57     }
58     
59     public function setCacheDir($dir)
60     {
61          $this->cache_dir = null;
62         
63          if (!empty($dir) && is_dir($dir) && is_writeable($dir))
64          {
65               $this->cache_dir = $dir;
66               return true;
67          }
68         
69          return false;
70     }
71     
72     public function setCacheTTL($str)
73     {
74          $str = trim($str);
75          if (!empty($str))
76          {
77               if (substr($str,0,1) != '-') {
78                    $str = '-'.$str;
79               }
80               $this->cache_ttl = $str;
81          }
82     }
83     
84     public function setForce($force)
85     {
86          $this->force = $force;
87     }
88     
89     protected function getModulesXML($url)
90     {
91          if (!self::readURL($url,$ssl,$host,$port,$path,$user,$pass)) {
92               return false;
93          }
94          $this->setHost($host,$port);
95          $this->useSSL($ssl);
96          $this->setAuthorization($user,$pass);
97         
98          return $this->get($path);
99     }
100     
101     protected function withCache($url)
102     {
103          $url_md5 = md5($url);
104          $cached_file = sprintf('%s/%s/%s/%s/%s.ser',
105               $this->cache_dir,
106               $this->cache_file_prefix,
107               substr($url_md5,0,2),
108               substr($url_md5,2,2),
109               $url_md5
110          );
111         
112          $may_use_cached = false;
113         
114          if (@file_exists($cached_file) && !$this->force)
115          {
116               $may_use_cached = true;
117               $ts = @filemtime($cached_file);
118               if ($ts > strtotime($this->cache_ttl))
119               {
120                    # Direct cache
121                    return unserialize(file_get_contents($cached_file));
122               }
123               $this->setValidator('IfModifiedSince', $ts);
124          }
125         
126          if (!$this->getModulesXML($url))
127          {
128               if ($may_use_cached)
129               {
130                    # connection failed - fetched from cache
131                    return unserialize(file_get_contents($cached_file));
132               }
133               return false;
134          }
135         
136          switch ($this->getStatus())
137          {
138               case '304':
139                    @files::touch($cached_file);
140                    return unserialize(file_get_contents($cached_file));
141               case '200':
142                    if ($modules = new daModulesParser($this->getContent()))
143                    {
144                         try {
145                              files::makeDir(dirname($cached_file),true);
146                         } catch (Exception $e) {
147                              return $modules;
148                         }
149                         
150                         if (($fp = @fopen($cached_file, 'wb')))
151                         {
152                              fwrite($fp, serialize($modules));
153                              fclose($fp);
154                              files::inheritChmod($cached_file);
155                         }
156                         return $modules;
157                    }
158          }
159         
160          return false;
161     }
162     
163     protected function buildRequest()
164     {
165          $headers = parent::buildRequest();
166         
167          # Cache validators
168          if (!empty($this->validators))
169          {
170               if (isset($this->validators['IfModifiedSince'])) {
171                    $headers[] = 'If-Modified-Since: '.$this->validators['IfModifiedSince'];
172               }
173               if (isset($this->validators['IfNoneMatch'])) {
174                    if (is_array($this->validators['IfNoneMatch'])) {
175                         $etags = implode(',',$this->validators['IfNoneMatch']);
176                    } else {
177                         $etags = $this->validators['IfNoneMatch'];
178                    }
179                    $headers[] = '';
180               }
181          }
182         
183          return $headers;
184     }
185     
186     private function setValidator($key,$value)
187     {
188          if ($key == 'IfModifiedSince') {
189               $value = gmdate('D, d M Y H:i:s',$value).' GMT';
190          }
191         
192          $this->validators[$key] = $value;
193     }
194}
195
196?>
Note: See TracBrowser for help on using the repository browser.

Sites map