Dotclear

source: plugins/pacKman/index.php @ 3240

Revision 3240, 8.5 KB checked in by JcDenis, 10 years ago (diff)
  • Switch to Dotclear 2.6
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of pacKman, a plugin for Dotclear 2.
5#
6# Copyright (c) 2009-2013 Jean-Christian Denis and contributors
7# contact@jcdenis.fr
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
15if (!defined('DC_CONTEXT_ADMIN')) {
16     return null;
17}
18
19dcPage::checkSuper();
20
21# Queries
22$p_url = 'plugin.php?p=pacKman';
23$action = isset($_POST['action']) ? $_POST['action'] : '';
24$type = isset($_POST['type']) && in_array($_POST['type'],
25     array('plugins','themes','repository')) ? $_POST['type'] : '';
26
27# Settings
28$core->blog->settings->addNamespace('pacKman');
29$s = $core->blog->settings->pacKman;
30
31# Modules
32if (!isset($core->themes)) {
33     $core->themes = new dcThemes($core);
34     $core->themes->loadModules($core->blog->themes_path,null);
35}
36$themes = $core->themes;
37$plugins = $core->plugins;
38
39# Paths
40$ppexp = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
41$pppop = array_pop($ppexp);
42$plugins_path = path::real($pppop);
43$themes_path = $core->blog->themes_path;
44$repo_path = $s->packman_pack_repository;
45
46# Rights
47$is_writable = libPackman::is_writable(
48     $s->packman_pack_repository,
49     $s->packman_pack_filename
50);
51$is_editable =
52     !empty($type)
53     && !empty($_POST['modules']) 
54     && is_array($_POST['modules']);
55
56$is_configured = libPackman::is_configured(
57     $core,
58     $s->packman_pack_repository,
59     $s->packman_pack_filename,
60     $s->packman_secondpack_filename
61);
62
63# Actions
64try
65{
66     # Download
67     if (isset($_REQUEST['package']) && empty($type)) {
68
69          $modules = array();
70          if ($type == 'plugins') {
71               $modules = dcPackman::getPackages($core, $plugins_path);
72          }
73          elseif ($type == 'themes') {
74               $modules = dcPackman::getPackages($core, $themes_path);
75          }
76          else {
77               $modules = array_merge(
78                    dcPackman::getPackages($core, dirname($repo_path.'/'.$s->packman_pack_filename)),
79                    dcPackman::getPackages($core, dirname($repo_path.'/'.$s->packman_secondpack_filename))
80               );
81          }
82
83          foreach($modules as $f) {
84
85               if (preg_match('/'.preg_quote($_REQUEST['package']).'$/', $f['root'])
86                && is_file($f['root']) && is_readable($f['root'])
87               ) {
88
89                    # --BEHAVIOR-- packmanBeforeDownloadPackage
90                    $core->callBehavior('packmanBeforeDownloadPackage', $f, $type);
91
92                    header('Content-Type: application/zip');
93                    header('Content-Length: '.filesize($f['root']));
94                    header('Content-Disposition: attachment; filename="'.basename($f['root']).'"');
95                    readfile($f['root']);
96
97                    # --BEHAVIOR-- packmanAfterDownloadPackage
98                    $core->callBehavior('packmanAfterDownloadPackage', $f, $type);
99
100                    exit;
101               }
102          }
103
104          # Not found
105          header('Content-Type: text/plain');
106          http::head(404,'Not Found');
107          exit;
108     }
109     elseif (!empty($action) && !$is_editable) {
110          throw new Exception('No selected modules');
111     }
112
113     # Pack
114     elseif ($action == 'packup') {
115
116          $modules = array_keys($_POST['modules']);
117
118          foreach ($modules as $id) {
119
120               if (!${$type}->moduleExists($id)) {
121                    throw new Exception('No such module');
122               }
123
124               $module = ${$type}->getModules($id);
125               $module['id'] = $id;
126               $module['type'] = $type == 'themes' ? 'theme' : 'plugin';
127
128               $root = $s->packman_pack_repository;
129               $files = array(
130                    $s->packman_pack_filename,
131                    $s->packman_secondpack_filename
132               );
133               $nocomment = $s->packman_pack_nocomment;
134               $overwrite = $s->packman_pack_overwrite;
135               $exclude = explode(',', $s->packman_pack_excludefiles);
136
137               # --BEHAVIOR-- packmanBeforeCreatePackage
138               $core->callBehavior('packmanBeforeCreatePackage', $core, $module);
139
140               dcPackman::pack($module, $root, $files, $overwrite, $exclude, $nocomment);
141
142               # --BEHAVIOR-- packmanAfterCreatePackage
143               $core->callBehavior('packmanAfterCreatePackage', $core, $module);
144
145          }
146
147          dcPage::addSuccessNotice(
148               __('Package successfully created.')
149          );
150          http::redirect(empty($_POST['redir']) ? 
151               $p_url.'#packman-'.$type : $_POST['redir']
152          );
153     }
154
155     # Delete
156     elseif ($action == 'delete') {
157
158          if ($type == 'plugins') {
159               $proot = $plugins_path;
160          }
161          elseif ($type == 'themes') {
162               $proot == $themes_path;
163          }
164          else {
165               $proot == 'repository';
166          }
167
168          foreach ($_POST['modules'] as $id => $root) {
169               if (!file_exists($root) || !files::isDeletable($root)) {
170                    throw new Exception('Undeletable file: '.$root);
171               }
172
173               unlink($root);
174          }
175
176          dcPage::addSuccessNotice(
177               __('Package successfully deleted.')
178          );
179          http::redirect(
180               $p_url.'#packman-repository-'.$type
181          );
182     }
183
184     # Install
185     elseif ($action == 'install') {
186
187          foreach ($_POST['modules'] as $id => $root) {
188
189               # --BEHAVIOR-- packmanBeforeInstallPackage
190               $core->callBehavior('packmanBeforeInstallPackage', $type, $id, $root);
191
192               if ($type == 'plugins') {
193                    $plugins->installPackage($root, $plugins);
194               }
195               if ($type == 'themes') {
196                    $themes->installPackage($root, $themes);
197               }
198
199               # --BEHAVIOR-- packmanAfterInstallPackage
200               $core->callBehavior('packmanAfterInstallPackage', $type, $id, $root);
201
202          }
203
204          dcPage::addSuccessNotice(
205               __('Package successfully installed.')
206          );
207          http::redirect(
208               $p_url.'#packman-repository-'.$type
209          );
210     }
211
212     # Copy
213     elseif (strpos($action, 'copy_to_') !== false) {
214
215          if ($action == 'copy_to_plugins') {
216               $dest = $plugins_path;
217          }
218          elseif ($action == 'copy_to_themes') {
219               $dest = $themes_path;
220          }
221          elseif ($action == 'copy_to_repository') {
222               $dest = $repo_path;
223          }
224
225          foreach ($_POST['modules'] as $id => $root) {
226               file_put_contents(
227                    $dest.'/'.basename($root),
228                    file_get_contents($root)
229               );
230          }
231
232          dcPage::addSuccessNotice(
233               __('Package successfully copied.')
234          );
235          http::redirect(
236               $p_url.'#packman-repository-'.$type
237          );
238     }
239
240     # Move
241     elseif (strpos($action, 'move_to_') !== false) {
242
243          if ($action == 'move_to_plugins') {
244               $dest = $plugins_path;
245          }
246          elseif ($action == 'move_to_themes') {
247               $dest = $themes_path;
248          }
249          elseif ($action == 'move_to_repository') {
250               $dest = $repo_path;
251          }
252
253          foreach ($_POST['modules'] as $id => $root) {
254               file_put_contents(
255                    $dest.'/'.basename($root),
256                    file_get_contents($root)
257               );
258               unlink($root);
259          }
260
261          dcPage::addSuccessNotice(
262               __('Package successfully moved.')
263          );
264          http::redirect(
265               $p_url.'#packman-repository-'.$type
266          );
267     }
268}
269catch(Exception $e) {
270     $core->error->add($e->getMessage());
271}
272
273# Display
274echo 
275'<html><head><title>'.__('pacKman').'</title>'.
276dcPage::jsPageTabs().
277dcPage::jsLoad('index.php?pf=pacKman/js/packman.js');
278
279# --BEHAVIOR-- packmanAdminHeader
280$core->callBehavior('packmanAdminHeader', $core);
281
282echo 
283'</head><body>'.
284
285dcPage::breadcrumb(
286     array(
287          __('Plugins') => '',
288          __('pacKman') => ''
289     )
290).
291dcPage::notices();
292
293if ($core->error->flag()) {
294     echo
295     '<p class="warning">'.__('pacKman is not well configured.').' '.
296     '<a href="plugins.php?module=pacKman&amp;conf=1&amp;redir='.
297     urlencode('plugin.php?p=pacKman').'">'.__('Configuration').'</a>'.
298     '</p>';
299}
300else {
301
302     $repo_path_modules = array_merge(
303          dcPackman::getPackages(
304               $core,
305               dirname($repo_path.'/'.$s->packman_pack_filename)
306          ),
307          dcPackman::getPackages(
308               $core,
309               dirname($repo_path.'/'.$s->packman_secondpack_filename)
310          )
311     );
312     $plugins_path_modules = dcPackman::getPackages(
313          $core,
314          $plugins_path
315     );
316     $themes_path_modules = dcPackman::getPackages(
317          $core,
318          $themes_path
319     );
320
321     libPackman::modules(
322          $core,
323          $plugins->getModules(),
324          'plugins',
325          __('Installed plugins')
326     );
327
328     libPackman::modules(
329          $core,
330          $themes->getModules(),
331          'themes',
332          __('Installed themes')
333     );
334
335     libPackman::repository(
336          $core,
337          $plugins_path_modules,
338          'plugins',
339          __('Plugins root')
340     );
341
342     libPackman::repository(
343          $core,
344          $themes_path_modules,
345          'themes',
346          __('Themes root')
347     );
348
349     libPackman::repository(
350          $core,
351          $repo_path_modules,
352          'repository',
353          __('Packages repository')
354     );
355}
356
357# --BEHAVIOR-- packmanAdminTabs
358$core->callBehavior('packmanAdminTabs', $core);
359
360dcPage::helpBlock('pacKman');
361
362echo 
363'<hr class="clear"/><p class="right modules">
364<a class="module-config" '.
365'href="plugins.php?module=pacKman&amp;conf=1&amp;redir='.
366urlencode('plugin.php?p=pacKman').'">'.__('Configuration').'</a> -
367pacKman - '.$core->plugins->moduleInfo('pacKman', 'version').'&nbsp;
368<img alt="'.__('pacKman').'" src="index.php?pf=pacKman/icon.png" />
369</p>
370</body></html>';
Note: See TracBrowser for help on using the repository browser.

Sites map