1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of tinyPacker, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2009-2016 Jean-Christian Denis and contributors |
---|
7 | # contact@jcdenis.fr http://jcdenis.net |
---|
8 | # |
---|
9 | # Licensed under the GPL version 2.0 license. |
---|
10 | # A copy of this license is available in LICENSE file or at |
---|
11 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
12 | # |
---|
13 | # -- END LICENSE BLOCK ------------------------------------ |
---|
14 | |
---|
15 | if (!defined('DC_CONTEXT_ADMIN')) { |
---|
16 | |
---|
17 | return null; |
---|
18 | } |
---|
19 | |
---|
20 | if (!tinyPacker::repositoryDir($core)) { |
---|
21 | |
---|
22 | return null; |
---|
23 | } |
---|
24 | |
---|
25 | $core->addBehavior( |
---|
26 | 'adminModulesListGetActions', |
---|
27 | array('tinyPacker', 'adminModulesGetActions') |
---|
28 | ); |
---|
29 | $core->addBehavior( |
---|
30 | 'adminModulesListDoActions', |
---|
31 | array('tinyPacker', 'adminModulesDoActions') |
---|
32 | ); |
---|
33 | |
---|
34 | /** |
---|
35 | * @ingroup DC_PLUGIN_TINYPACKER |
---|
36 | * @brief Quick create packages of modules from admin to public dir. |
---|
37 | * @since 2.6 |
---|
38 | */ |
---|
39 | class tinyPacker |
---|
40 | { |
---|
41 | /** |
---|
42 | * Blog's public sub-directory where to put packages |
---|
43 | * @var string |
---|
44 | */ |
---|
45 | public static $sub_dir = 'packages'; |
---|
46 | |
---|
47 | /** |
---|
48 | * Add button to create package to modules lists |
---|
49 | * @param object $list adminModulesList instance |
---|
50 | * @param string $id Module id |
---|
51 | * @param arrray $_ Module properties |
---|
52 | * @return string HTML submit button |
---|
53 | */ |
---|
54 | public static function adminModulesGetActions($list, $id, $_) |
---|
55 | { |
---|
56 | if ($list->getList() != 'plugin-activate' |
---|
57 | && $list->getList() != 'theme-activate') { |
---|
58 | |
---|
59 | return null; |
---|
60 | } |
---|
61 | |
---|
62 | return |
---|
63 | '<input type="submit" name="tinypacker['. |
---|
64 | html::escapeHTML($id).']" value="Pack" />'; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Create package on modules lists action |
---|
69 | * @param object $list adminModulesList instance |
---|
70 | * @param array $modules Selected modules ids |
---|
71 | * @param string $type List type (plugins|themes) |
---|
72 | * @throws Exception If no public dir or module |
---|
73 | * @return null Null |
---|
74 | */ |
---|
75 | public static function adminModulesDoActions($list, $modules, $type) |
---|
76 | { |
---|
77 | # Pack action |
---|
78 | if (empty($_POST['tinypacker']) |
---|
79 | || !is_array($_POST['tinypacker'])) { |
---|
80 | |
---|
81 | return null; |
---|
82 | } |
---|
83 | |
---|
84 | $modules = array_keys($_POST['tinypacker']); |
---|
85 | $id = $modules[0]; |
---|
86 | |
---|
87 | # Repository directory |
---|
88 | if (($root = self::repositoryDir($list->core)) === false) { |
---|
89 | throw new Exception( |
---|
90 | __('Destination directory is not writable.') |
---|
91 | ); |
---|
92 | } |
---|
93 | |
---|
94 | # Module to pack |
---|
95 | if (!$list->modules->moduleExists($id)) { |
---|
96 | throw new Exception(__('No such module.')); |
---|
97 | } |
---|
98 | $module = $list->modules->getModules($id); |
---|
99 | |
---|
100 | # Excluded files and dirs |
---|
101 | $exclude = array( |
---|
102 | '\.', |
---|
103 | '\.\.', |
---|
104 | '__MACOSX', |
---|
105 | '\.svn', |
---|
106 | '\.hg.*?', |
---|
107 | '\.git.*?', |
---|
108 | 'CVS', |
---|
109 | '\.directory', |
---|
110 | '\.DS_Store', |
---|
111 | 'Thumbs\.db' |
---|
112 | ); |
---|
113 | |
---|
114 | # Packages names |
---|
115 | $files = array( |
---|
116 | $type.'-'.$id.'.zip', |
---|
117 | $type.'-'.$id.'-'.$module['version'].'.zip' |
---|
118 | ); |
---|
119 | |
---|
120 | # Create zip |
---|
121 | foreach($files as $f) { |
---|
122 | |
---|
123 | @set_time_limit(300); |
---|
124 | $fp = fopen($root.'/'.$f, 'wb'); |
---|
125 | |
---|
126 | $zip = new fileZip($fp); |
---|
127 | |
---|
128 | foreach($exclude AS $e) { |
---|
129 | $zip->addExclusion(sprintf( |
---|
130 | '#(^|/)(%s)(/|$)#', |
---|
131 | $e |
---|
132 | )); |
---|
133 | } |
---|
134 | |
---|
135 | $zip->addDirectory($module['root'], $id, true); |
---|
136 | $zip->write(); |
---|
137 | $zip->close(); |
---|
138 | unset($zip); |
---|
139 | } |
---|
140 | |
---|
141 | dcPage::addSuccessNotice( |
---|
142 | __('Task successfully executed.') |
---|
143 | ); |
---|
144 | http::redirect($list->getURL()); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Check and create directories used by packer |
---|
149 | * @param object $core dcCore instance |
---|
150 | * @return string|boolean Cleaned path or false on error |
---|
151 | */ |
---|
152 | public static function repositoryDir($core) |
---|
153 | { |
---|
154 | $dir = path::real( |
---|
155 | $core->blog->public_path.'/'.tinyPacker::$sub_dir, |
---|
156 | false |
---|
157 | ); |
---|
158 | |
---|
159 | try { |
---|
160 | if (!is_dir($dir)) { |
---|
161 | files::makeDir($dir, true); |
---|
162 | } |
---|
163 | if (is_writable($dir)) { |
---|
164 | |
---|
165 | return $dir; |
---|
166 | } |
---|
167 | } |
---|
168 | catch(Exception $e) {} |
---|
169 | |
---|
170 | return false; |
---|
171 | } |
---|
172 | } |
---|