Skip to content

Commit ca62193

Browse files
committed
add install function
1 parent 2e7f136 commit ca62193

File tree

5 files changed

+899
-8
lines changed

5 files changed

+899
-8
lines changed

TeStore/Action.php

+79-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php ! defined('__TYPECHO_ROOT_DIR__') and exit();
22

3+
include_once 'unzip.php';
4+
35
class TeStore_Action extends Typecho_Widget {
46

57
private $server;
@@ -9,6 +11,8 @@ class TeStore_Action extends Typecho_Widget {
911

1012
private $cacheDir;
1113

14+
private $pluginRoot;
15+
1216
private $pluginInfo;
1317

1418
public function __construct($request, $response, $params = NULL)
@@ -19,6 +23,10 @@ public function __construct($request, $response, $params = NULL)
1923
$this->server = $pluginOpts->server;
2024
$this->cacheTime = $pluginOpts->cache_time;
2125
$this->cacheDir = dirname(__FILE__) . '/data/';
26+
$this->pluginRoot = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/';
27+
28+
$this->getAppData();
29+
2230
define('TYPEHO_ADMIN_PATH', __TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__.'/');
2331
}
2432

@@ -74,7 +82,7 @@ private function getCacheInfo()
7482
*/
7583
private function parseRemoteData()
7684
{
77-
$html = file_get_contents($this->server);
85+
@$html = file_get_contents($this->server);
7886
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);
7987
array_shift($this->pluginInfo);
8088

@@ -99,7 +107,7 @@ private function formatePluginInfo()
99107
$fieldName = array('zipFile', 'pluginName', 'desc', 'version', 'site', 'author', 'pluginUrl', 'source');
100108
foreach( range(0, count($this->pluginInfo[0]) - 1 ) as $plugIdx ){
101109
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];
103111
}
104112
}
105113

@@ -115,19 +123,82 @@ private function cachePluginInfo()
115123
file_put_contents($this->cacheDir . time() . '.json', $pluginInfo);
116124
}
117125

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)
119139
{
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+
}
124146
}
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);
125157

126158
include_once 'views/market.php';
127159
}
128160

129161
public function install()
130162
{
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;
132203
}
133204
}

TeStore/sticky/close.png

271 Bytes
Loading

TeStore/sticky/sticky.css

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
div.sticky-queue
2+
{
3+
position:fixed;
4+
background:#fff;
5+
border:1px solid #999;
6+
box-shadow:0px 0px 5px #bbb;
7+
-moz-box-shadow:0px 0px 5px #bbb;
8+
-webkit-box-shadow:0px 0px 5px #bbb;
9+
width:200px;
10+
}
11+
div.sticky-note
12+
{ padding-right:20px; }
13+
div.sticky
14+
{
15+
font-size:12px;
16+
color:#555;
17+
display:none;
18+
padding:10px;
19+
position:relative;
20+
}
21+
img.sticky-close
22+
{
23+
position:absolute;
24+
top:10px;
25+
right:10px;
26+
height:14px;
27+
width:14px;
28+
cursor:pointer;
29+
}
30+
31+
32+
.top-right
33+
{ right:20px; }
34+
.top-left
35+
{ left:20px; }
36+
.top-right, .top-left
37+
{
38+
top:-2px;
39+
border-bottom-right-radius:6px;
40+
border-bottom-left-radius:6px;
41+
-moz-border-radius-bottomright:6px;
42+
-moz-border-radius-bottomleft:6px;
43+
-webkit-border-bottom-right-radius:6px;
44+
-webkit-border-bottom-left-radius:6px;
45+
}
46+
.bottom-right
47+
{ right:20px; }
48+
.bottom-left
49+
{ left:20px; }
50+
.bottom-right, .bottom-left
51+
{
52+
bottom:-2px;
53+
border-top-right-radius:6px;
54+
border-top-left-radius:6px;
55+
-moz-border-radius-topright:6px;
56+
-moz-border-radius-topleft:6px;
57+
-webkit-border-top-right-radius:6px;
58+
-webkit-border-top-left-radius:6px;
59+
}
60+
61+
.border-top-right, .border-top-left
62+
{ border-top:1px solid #999; }
63+
.border-bottom-right, .border-bottom-left
64+
{ border-bottom:1px solid #999; }

TeStore/sticky/sticky.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Sticky v1.0 by Daniel Raftery
2+
// http://thrivingkings.com/sticky
3+
//
4+
// http://twitter.com/ThrivingKings
5+
6+
(function( $ )
7+
{
8+
9+
// Using it without an object
10+
$.sticky = function(note, options, callback) { return $.fn.sticky(note, options, callback); };
11+
12+
$.fn.sticky = function(note, options, callback)
13+
{
14+
// Default settings
15+
var position = 'top-right'; // top-left, top-right, bottom-left, or bottom-right
16+
17+
var settings =
18+
{
19+
'speed' : 'fast', // animations: fast, slow, or integer
20+
'duplicates' : true, // true or false
21+
'autoclose' : 5000, // integer or false
22+
'img' : 'close.png'
23+
};
24+
25+
// Passing in the object instead of specifying a note
26+
if(!note)
27+
{ note = this.html(); }
28+
29+
if(options)
30+
{ $.extend(settings, options); }
31+
32+
// Variables
33+
var display = true;
34+
var duplicate = 'no';
35+
36+
// Somewhat of a unique ID
37+
var uniqID = Math.floor(Math.random()*99999);
38+
39+
// Handling duplicate notes and IDs
40+
$('.sticky-note').each(function()
41+
{
42+
if($(this).html() == note && $(this).is(':visible'))
43+
{
44+
duplicate = 'yes';
45+
if(!settings['duplicates'])
46+
{ display = false; }
47+
}
48+
if($(this).attr('id')==uniqID)
49+
{ uniqID = Math.floor(Math.random()*9999999); }
50+
});
51+
52+
// Make sure the sticky queue exists
53+
if(!$('body').find('.sticky-queue').html())
54+
{ $('body').append('<div class="sticky-queue ' + position + '"></div>'); }
55+
56+
// Can it be displayed?
57+
if(display)
58+
{
59+
// Building and inserting sticky note
60+
$('.sticky-queue').prepend('<div class="sticky border-' + position + '" id="' + uniqID + '"></div>');
61+
$('#' + uniqID).append('<img src="' + settings['img'] + '" class="sticky-close" rel="' + uniqID + '" title="Close" />');
62+
$('#' + uniqID).append('<div class="sticky-note" rel="' + uniqID + '">' + note + '</div>');
63+
64+
// Smoother animation
65+
var height = $('#' + uniqID).height();
66+
$('#' + uniqID).css('height', height);
67+
68+
$('#' + uniqID).slideDown(settings['speed']);
69+
display = true;
70+
}
71+
72+
// Listeners
73+
$('.sticky').ready(function()
74+
{
75+
// If 'autoclose' is enabled, set a timer to close the sticky
76+
if(settings['autoclose'])
77+
{ $('#' + uniqID).delay(settings['autoclose']).fadeOut(settings['speed']); }
78+
});
79+
// Closing a sticky
80+
$('.sticky-close').click(function()
81+
{ $('#' + $(this).attr('rel')).dequeue().fadeOut(settings['speed']); });
82+
83+
84+
// Callback data
85+
var response =
86+
{
87+
'id' : uniqID,
88+
'duplicate' : duplicate,
89+
'displayed' : display,
90+
'position' : position
91+
}
92+
93+
// Callback function?
94+
if(callback)
95+
{ callback(response); }
96+
else
97+
{ return(response); }
98+
99+
}
100+
})( jQuery );

0 commit comments

Comments
 (0)