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