123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- defined('STATUSNET') || die();
- require_once INSTALLDIR . '/lib/util/deletetree.php';
- class PlugininstallAction extends Action
- {
- public $pluginfile = null;
-
- public function prepare(array $args = [])
- {
- parent::prepare($args);
-
-
- if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-
-
- $this->clientError(_('This action only accepts POST requests.'));
- }
-
- $token = $this->trimmed('token');
- if (!$token || $token != common_session_token()) {
-
- $this->clientError(_m('There was a problem with your session token.'.
- ' Try again, please.'));
- }
-
- $this->user = common_current_user();
- if (empty($this->user)) {
-
- $this->clientError(_m('Not logged in.'));
- }
- if (!AdminPanelAction::canAdmin('plugins')) {
-
- $this->clientError(_m('You cannot administer plugins.'));
- }
- if (!is_writable(INSTALLDIR . '/local/plugins/') ||
- !is_writable(PUBLICDIR . '/local/plugins/')) {
- $this->clientError(_m("No permissions to write on the third party plugin upload directory(ies)."));
- }
- return true;
- }
-
- protected function handle()
- {
- if ($this->trimmed('upload')) {
- $this->uploadPlugin();
- $url = common_local_url('pluginsadminpanel');
- common_redirect($url, 303);
- } else {
-
- throw new ClientException(_('Unexpected form submission.'));
- }
- }
-
- public function uploadPlugin(): void
- {
-
- $form_name = 'pluginfile';
- if (!isset($_FILES[$form_name]) || !isset($_FILES[$form_name]['error'])) {
- throw new NoUploadedMediaException($form_name);
- }
- if ($_FILES[$form_name]['error'] != UPLOAD_ERR_OK) {
- throw new ClientException(_m('System error uploading file.'));
- }
- $filename = basename($_FILES[$form_name]['name']);
- $ext = null;
- if (preg_match('/^.+?\.([A-Za-z0-9]+)$/', $filename, $matches) === 1) {
-
- $ext = mb_strtolower($matches[1]);
- $plugin_name = basename($filename, '.'.$ext);
- $temp_path = INSTALLDIR.DIRECTORY_SEPARATOR.ltrim($_FILES[$form_name]['tmp_name'], '/tmp/').'.'.$ext;
- if (!in_array($ext, ['tar', 'zip'])) {
- $ext = null;
- }
- }
- if (is_null($ext)) {
-
- @unlink($_FILES[$form_name]['tmp_name']);
- throw new ClientException(_m('Invalid plugin package extension. Must be either tar or zip.'));
- }
-
- move_uploaded_file($_FILES[$form_name]['tmp_name'], $temp_path);
- $this->extractPlugin($temp_path, $plugin_name);
- }
-
- public function extractPlugin($temp_path, $plugin_name): void
- {
- $phar = new PharData($temp_path);
- $dest_installdir = INSTALLDIR . '/local/plugins/'.$plugin_name;
- $dest_publicdir = PUBLICDIR . '/local/plugins/'.$plugin_name;
- try {
- $phar->extractTo($dest_installdir, 'includes', false);
- $phar->extractTo($dest_publicdir, 'public', false);
- } catch (PharException $e) {
-
- @unlink($temp_path);
-
- deleteTree($dest_installdir);
- deleteTree($dest_publicdir);
-
- throw new ClientException($e->getMessage());
- }
- foreach ([$dest_installdir.'includes',
- $dest_publicdir.'public'] as $source) {
- $files = scandir("source");
- foreach ($files as $file) {
- if (in_array($file, ['.', '..'])) {
- continue;
- }
- rename($source . $file, dirname($source) . $file);
- }
- }
- rmdir($dest_installdir.'includes');
- rmdir($dest_publicdir.'public');
-
- @unlink($temp_path);
- }
- }
|