Project:
Habari-Extras
Code Location:
git://github.com/habari-extras/pluginloader.gitmaster
Outline
-
>
C
PluginLoader
- F CORE_SVN_REPO
- M action_init()
- M filter_plugin_loader($existing_loader , Theme $theme)
- M scrape_repo($repo)
- M get_links($url)
- M get_repos()
- M filter_admin_access_tokens(array $require_any , $page)
- M action_admin_theme_get_plugin_download(AdminHandler $handler , Theme $theme)
- M download($source , $destination , $as = null)
pluginloader.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php include_once(dirname(__FILE__) . '/simple_html_dom.php'); class PluginLoader extends Plugin { const CORE_SVN_REPO = 'http://svn.habariproject.org/habari-extras/plugins/'; public function action_init() { $this->add_template('plugins.loader', dirname(__FILE__) . '/plugins.loader.php'); } public function filter_plugin_loader($existing_loader, Theme $theme) { $infos = array(); foreach($this->get_repos() as $repo) { $infos = array_merge($infos, $this->scrape_repo($repo)); } //Utils::debug($theme->inactive_plugins); $plugins = array(); $id = 0; foreach($infos as $key => $info) { $xinfo = reset($info); $id = sprintf( '%x', crc32( $key ) ); $plugins[$id] = array( 'plugind_id' => $id, 'file' => '', 'debug' => false, 'info' => new SimpleXMLElement($xinfo), 'active' => false, 'verb' => 'Download', ); foreach($info as $version => $xinfo) { $plugins[$id]['actions'][$version] = array( 'url' => URL::get( 'admin', 'page=plugin_download&plugin_id=' . $id . '&version=' . $version ), 'caption' => _t('Get %s', array($version)), 'action' => 'download-', ); } } $theme->loadable = $plugins; $loader = $theme->fetch('plugins.loader'); return $existing_loader . $loader; } public function scrape_repo($repo) { if(Cache::has('plugin.loader.repo.infos.' . $repo)) { $hrefs = Cache::get('plugin.loader.repo.infos.' . $repo); } else { $hrefs = array(); } if(Cache::has('plugin.loader.repo.dirs.' . $repo)) { $donedirs = Cache::get('plugin.loader.repo.dirs.' . $repo); } else { $donedirs = array(); } // Get the list of plugins foreach($this->get_links($repo) as $href) { $plugindirs[] = $href; } $ct = 0; foreach($plugindirs as $plugindir) { if(in_array($plugindir, $donedirs)) continue; if(++$ct > 5) break; // Only do 5 at a time foreach($this->get_links($repo . '/' . $plugindir . '/trunk') as $href) { if(preg_match('#\.plugin\.xml$#i', $href)) { // This is a plugin's info and the current directory is the plugin directory $info = RemoteRequest::get_contents($repo . '/' . $plugindir . '/trunk/' . $href); $hrefs[$repo . $plugindir] = is_array($hrefs[$repo. $plugindir]) ? $hrefs[$repo . $plugindir] : array(); $hrefs[$repo . $plugindir]['trunk'] = $info; } } foreach($this->get_links($repo . '/' . $plugindir . '/tags') as $tag) { foreach($this->get_links($repo . '/' . $plugindir . '/tags/' . $tag) as $href) { if(preg_match('#\.plugin\.xml$', $href)) { // This is a plugin's info in a tag and the current directory is the plugin directory $info = RemoteRequest::get_contents($repo . '/' . $plugindir . '/trunk/' . $href); $hrefs[$repo . $plugindir] = is_array($hrefs[$repo . $plugindir]) ? $hrefs[$repo . $plugindir] : array(); $hrefs[$repo . $plugindir][$tag] = $info; } } } $donedirs[] = $plugindir; } if($ct > 0) { Cache::set('plugin.loader.repo.infos.' . $repo, $hrefs); Cache::set('plugin.loader.repo.dirs.' . $repo, $donedirs); } return $hrefs; } function get_links($url) { $html = RemoteRequest::get_contents($url); $dom = str_get_html($html); $as = $dom->find('a'); $hrefs = array(); foreach($as as $a) { $href = rtrim($a->getAttribute('href'), '/'); if(strpos($href, '..') !== false || strpos($href, '/') !== false) { } else { $hrefs[] = $href; } } $dom->clear(); return $hrefs; } public function get_repos() { return array('http://svn.habariproject.org/habari-extras/plugins/'); } public function filter_admin_access_tokens( array $require_any, $page ) { switch ( $page ) { case 'plugin_download': $require_any = array( 'manage_plugins' => true ); break; } return $require_any; } public function action_admin_theme_get_plugin_download( AdminHandler $handler, Theme $theme ) { $theme->page_content = ''; $plugin_id = $handler->handler_vars['plugin_id']; $version = $handler->handler_vars['version']; foreach($this->get_repos() as $repo) { $infos = $this->scrape_repo($repo); foreach($infos as $infokey => $info) { if(sprintf( '%x', crc32( $infokey ) ) == $plugin_id) { $downloadurl = $infokey; if($version == 'trunk') { $downloadurl .= '/trunk/'; } else { $downloadurl .= '/tags/' . $version . '/'; } if($this->download($downloadurl, HABARI_PATH . '/user/plugins/', basename($infokey))) { Session::notice(_t('Downloaded the "%s" plugin. It must now be activated before use.', array(basename($infokey)))); } else { Session::notice(_t('There was an error downloading the "%s" plugin', array(basename($infokey)))); } //Utils::debug($downloadurl);die(); Utils::redirect(URL::get('admin', 'page=plugins')); } } } } public function download($source, $destination, $as = null) { echo '<pre>'; var_dump($source, $destination); $rr = new RemoteRequest($source); if($rr->execute()) { $response = $rr->get_response_body(); $headers = $rr->get_response_headers(); if(isset($headers['Location']) && $headers['Location'] != $source) { // This should probably count redirects and bail after some max value return $this->download($headers['Location'], $destination); } $basename = basename($source); if(isset($as)) { $basename = $as; } if(strpos($headers['Content-Type'], 'text/html') !== false) { if(file_exists($destination . $basename) || mkdir($destination . $basename)) { //Session::notice(_t('Created the "%s" directory', array($basename))); $dom = str_get_html($response); $as = $dom->find('a'); $hrefs = array(); foreach($as as $a) { $href = rtrim($a->getAttribute('href'), '/'); if(strpos($href, '..') !== false || strpos($href, '/') !== false) { } else { $this->download($source . $href, $destination . $basename . '/'); } } $dom->clear(); } else { Session::error(_t('Could not create the directory for the plugin')); return false; } } else { //Session::notice(_t('Downloaded "%s" to the plugin directory', array($basename))); file_put_contents($destination . $basename, $response); //file_put_contents($destination . $basename . '.header', print_r($headers,1)); } } return true; } } ?>
