1 | <?php |
---|
2 | |
---|
3 | class dcRepository |
---|
4 | { |
---|
5 | public $core; |
---|
6 | public $modules; |
---|
7 | |
---|
8 | protected $xml_url; |
---|
9 | protected $data; |
---|
10 | |
---|
11 | public function __construct(dcModules $modules, $xml_url) |
---|
12 | { |
---|
13 | $this->core = $modules->core; |
---|
14 | $this->modules = $modules |
---|
15 | } |
---|
16 | |
---|
17 | protected function check($force=false) |
---|
18 | { |
---|
19 | if (!$this->xml_url) { |
---|
20 | return false; |
---|
21 | } |
---|
22 | if (($parser = dcModulesReader::quickParse($this->xml_url, DC_TPL_CACHE, $force)) === false) { |
---|
23 | return false; |
---|
24 | } |
---|
25 | |
---|
26 | $raw_datas = $parser->getModules(); |
---|
27 | |
---|
28 | uasort($raw_datas, array('self','sort')); |
---|
29 | |
---|
30 | $skipped = array_keys($this->modules->getDisabledModules()); |
---|
31 | foreach ($skipped as $p_id) { |
---|
32 | if (isset($raw_datas[$p_id])) { |
---|
33 | unset($raw_datas[$p_id]); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | $updates = array(); |
---|
38 | $current = $this->modules->getModules(); |
---|
39 | foreach ($current as $p_id => $p_infos) { |
---|
40 | if (isset($raw_datas[$p_id])) { |
---|
41 | if (self::compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) { |
---|
42 | $updates[$p_id] = $raw_datas[$p_id]; |
---|
43 | $updates[$p_id]['root'] = $p_infos['root']; |
---|
44 | $updates[$p_id]['root_writable'] = $p_infos['root_writable']; |
---|
45 | $updates[$p_id]['current_version'] = $p_infos['version']; |
---|
46 | } |
---|
47 | unset($raw_datas[$p_id]); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | $this->data = array( |
---|
52 | 'new' => $raw_datas, |
---|
53 | 'update' => $updates |
---|
54 | ); |
---|
55 | |
---|
56 | return true; |
---|
57 | } |
---|
58 | |
---|
59 | public function get($update=false) |
---|
60 | { |
---|
61 | return $this->data[$update ? 'update' : 'new']; |
---|
62 | } |
---|
63 | |
---|
64 | public function search($search) |
---|
65 | { |
---|
66 | $result = array(); |
---|
67 | |
---|
68 | foreach ($this->data['new'] as $module) |
---|
69 | { |
---|
70 | if ( preg_match('/'.$search.'/i',$module['id']) || |
---|
71 | preg_match('/'.$search.'/i',$module['name']) || |
---|
72 | preg_match('/'.$search.'/i',$module['desc'])) |
---|
73 | { |
---|
74 | $result[] = $module; |
---|
75 | } |
---|
76 | } |
---|
77 | return $result; |
---|
78 | } |
---|
79 | |
---|
80 | public function process($url, $dest) |
---|
81 | { |
---|
82 | try { |
---|
83 | $client = netHttp::initClient($url, $path); |
---|
84 | $client->setUserAgent(self::agent()); |
---|
85 | $client->useGzip(false); |
---|
86 | $client->setPersistReferers(false); |
---|
87 | $client->setOutput($dest); |
---|
88 | $client->get($path); |
---|
89 | } |
---|
90 | catch (Exception $e) { |
---|
91 | throw new Exception(__('An error occurred while downloading the file.')); |
---|
92 | } |
---|
93 | |
---|
94 | unset($client); |
---|
95 | $ret_code = dcModules::installPackage($dest, $this->modules); |
---|
96 | |
---|
97 | return $ret_code; |
---|
98 | } |
---|
99 | |
---|
100 | public static function agent() |
---|
101 | { |
---|
102 | return sprintf('Dotclear/%s)', DC_VERSION); |
---|
103 | } |
---|
104 | |
---|
105 | private static function compare($v1,$v2,$op) |
---|
106 | { |
---|
107 | $v1 = preg_replace('!-r(\d+)$!','-p$1',$v1); |
---|
108 | $v2 = preg_replace('!-r(\d+)$!','-p$1',$v2); |
---|
109 | return version_compare($v1,$v2,$op); |
---|
110 | } |
---|
111 | |
---|
112 | private static function sort($a,$b) |
---|
113 | { |
---|
114 | $c = strtolower($a['id']); |
---|
115 | $d = strtolower($b['id']); |
---|
116 | if ($c == $d) { |
---|
117 | return 0; |
---|
118 | } |
---|
119 | return ($c < $d) ? -1 : 1; |
---|
120 | } |
---|
121 | } |
---|