1
1
<?php ! defined ('__TYPECHO_ROOT_DIR__ ' ) and exit ();
2
2
3
+ include_once 'unzip.php ' ;
4
+
3
5
class TeStore_Action extends Typecho_Widget {
4
6
5
7
private $ server ;
@@ -9,6 +11,8 @@ class TeStore_Action extends Typecho_Widget {
9
11
10
12
private $ cacheDir ;
11
13
14
+ private $ pluginRoot ;
15
+
12
16
private $ pluginInfo ;
13
17
14
18
public function __construct ($ request , $ response , $ params = NULL )
@@ -19,6 +23,10 @@ public function __construct($request, $response, $params = NULL)
19
23
$ this ->server = $ pluginOpts ->server ;
20
24
$ this ->cacheTime = $ pluginOpts ->cache_time ;
21
25
$ this ->cacheDir = dirname (__FILE__ ) . '/data/ ' ;
26
+ $ this ->pluginRoot = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/ ' ;
27
+
28
+ $ this ->getAppData ();
29
+
22
30
define ('TYPEHO_ADMIN_PATH ' , __TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ .'/ ' );
23
31
}
24
32
@@ -74,7 +82,7 @@ private function getCacheInfo()
74
82
*/
75
83
private function parseRemoteData ()
76
84
{
77
- $ html = file_get_contents ($ this ->server );
85
+ @ $ html = file_get_contents ($ this ->server );
78
86
preg_match_all ("/<a href= \"(.+zip).+ \">(.+?)<\/a>.+\s*<td.*>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td><a href= \"(.+) \">(.+)<\/a><\/td>\s*<td><a href= \"(.+) \">(.+)<\/a>/ " , $ html , $ this ->pluginInfo );
79
87
array_shift ($ this ->pluginInfo );
80
88
@@ -99,7 +107,7 @@ private function formatePluginInfo()
99
107
$ fieldName = array ('zipFile ' , 'pluginName ' , 'desc ' , 'version ' , 'site ' , 'author ' , 'pluginUrl ' , 'source ' );
100
108
foreach ( range (0 , count ($ this ->pluginInfo [0 ]) - 1 ) as $ plugIdx ){
101
109
foreach ( range (0 , $ fieldNum - 1 ) as $ fieldIdx ) {
102
- $ pluginData [$ plugIdx ][ $ fieldName [$ fieldIdx] ] = $ this ->pluginInfo [$ fieldIdx ][$ plugIdx ];
110
+ $ pluginData [$ plugIdx ]-> $ fieldName [$ fieldIdx ] = $ this ->pluginInfo [$ fieldIdx ][$ plugIdx ];
103
111
}
104
112
}
105
113
@@ -115,19 +123,82 @@ private function cachePluginInfo()
115
123
file_put_contents ($ this ->cacheDir . time () . '.json ' , $ pluginInfo );
116
124
}
117
125
118
- public function market ()
126
+ private function getActivePlugins ()
127
+ {
128
+ $ activatedPlugins = Typecho_Plugin::export ();
129
+ return array_keys ( $ activatedPlugins ['activated ' ] );
130
+ }
131
+
132
+ /**
133
+ * 获取目录下的文件夹
134
+ *
135
+ * @access private
136
+ * @return array
137
+ */
138
+ private static function getDir ($ targetDir )
119
139
{
120
- $ pluginInfo = $ this ->getAppData ();
121
- if ( $ pluginInfo ){
122
- $ activatedPlugins = Typecho_Plugin::export ();
123
- $ activatedPlugins = array_keys ( $ activatedPlugins ['activated ' ] );
140
+ $ dirs = array ();
141
+ $ files = scandir ($ targetDir );
142
+ foreach ($ files as $ file ){
143
+ if ( is_dir ($ targetDir . '/ ' . $ file ) && !in_array ($ file , array ('. ' , '.. ' )) ){
144
+ $ dirs [] = $ file ;
145
+ }
124
146
}
147
+ return $ dirs ;
148
+ }
149
+
150
+ public function market ()
151
+ {
152
+ $ options = Helper::options ();
153
+ $ pluginPath = Typecho_Common::url ('TeStore ' , $ options ->pluginUrl );
154
+ $ pluginInfo = $ this ->pluginInfo ;
155
+ $ activatedPlugins = $ this ->getActivePlugins ();
156
+ $ installPlugins = self ::getDir ($ this ->pluginRoot );
125
157
126
158
include_once 'views/market.php ' ;
127
159
}
128
160
129
161
public function install ()
130
162
{
131
-
163
+ $ plugin = $ this ->request ->get ('plugin ' );
164
+ $ pluginPath = $ this ->pluginRoot . '/ ' . $ plugin . '/ ' ;
165
+ $ pluginInfo = $ this ->getPluginByName ($ plugin );
166
+ $ tempFile = dirname (__FILE__ ) . "/.tmp/ " . $ pluginInfo ->pluginName . '.zip ' ;
167
+ $ activatedPlugins = $ this ->getActivePlugins ();
168
+
169
+ $ ret = array (
170
+ 'status ' => false ,
171
+ 'error ' => '' ,
172
+ );
173
+
174
+ if ( in_array ($ plugin , $ activatedPlugins ) ){
175
+ $ ret ['error ' ] = '无法安装已经安装的插件 ' ;
176
+ }else {
177
+ if ( false !== $ pluginInfo ){
178
+ $ zipFile = file_get_contents ($ pluginInfo ->zipFile );
179
+ file_put_contents ( $ tempFile , $ zipFile );
180
+
181
+ @mkdir ($ pluginPath );
182
+ $ unzip = new Unzip ();
183
+ if ( ! $ unzip ->extract ($ tempFile , $ this ->pluginRoot ) ){
184
+ $ ret ['error ' ] = $ unzip ->error_string ();
185
+ }else {
186
+ $ ret ['status ' ] = true ;
187
+ }
188
+
189
+ chmod ($ tempFile , 0755 );
190
+ @unlink ($ tempFile );
191
+ echo json_encode ($ ret );
192
+ }
193
+ }
194
+ }
195
+
196
+ public function getPluginByName ($ name )
197
+ {
198
+ foreach ($ this ->pluginInfo as $ plugin ) {
199
+ if ( $ plugin ->pluginName == $ name )
200
+ return $ plugin ;
201
+ }
202
+ return false ;
132
203
}
133
204
}
0 commit comments