Skip to content

Commit

Permalink
Update plugin from file
Browse files Browse the repository at this point in the history
  • Loading branch information
bloatware committed May 20, 2019
1 parent 819814e commit 30a4d71
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
32 changes: 9 additions & 23 deletions textpattern/include/txp_plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ function plugin_upload()
if($_FILES["theplugin"]["name"]) {
$filename = $_FILES["theplugin"]["name"];
$source = $_FILES["theplugin"]["tmp_name"];
$target_path = txpath.DS.'plugins'.DS.$filename;
$target_path = rtrim(get_pref('temp_dir', txpath.DS.'plugins'), DS).DS.$filename;

if(move_uploaded_file($source, $target_path)) {
$name = pathinfo($target_path, PATHINFO_FILENAME);
Expand All @@ -598,29 +598,9 @@ function plugin_upload()
}
}

$dir = txpath.DS.'plugins'.DS.$name;
$zip->extractTo(empty($makedir) ? txpath.DS.'plugins' : $dir);
$zip->extractTo(txpath.DS.'plugins'.(empty($makedir) ? '' : DS.$name));
$zip->close();
$plugin['name'] = $name;

if (@$info = file_get_contents($dir.'/manifest.json')) {
$plugin += json_decode($info, true);
}

if (@$code = file_get_contents($dir.'/'.$name.'.php')) {
$code = preg_replace('/^\s*<\?(?:php)?\s*|\s*\?>\s*$/i', '', $code);
$plugin['code'] = $code;
}

if (@$textpack = file_get_contents($dir.'/textpack.txp')) {
$plugin['textpack'] = $textpack;
}

if (@$help = file_get_contents($dir.'/help.html')) {
$plugin['help'] = $help;
} elseif (@$help = file_get_contents($dir.'/help.textile')) {
$plugin['help_raw'] = $help;
}
$plugin = Txp::get('\Textpattern\Plugin\Plugin')->read($name);
}

unlink($target_path);
Expand Down Expand Up @@ -714,6 +694,7 @@ function plugin_multiedit_form($page, $sort, $dir, $crit, $search_method)
'html' => $orders,
),
'delete' => gTxt('delete'),
'update' => gTxt('update_from_disk'),
);

return multi_edit($methods, 'plugin', 'plugin_multi_edit', $page, $sort, $dir, $crit, $search_method);
Expand Down Expand Up @@ -750,6 +731,11 @@ function plugin_multi_edit()
$plugin->changeOrder($name, ps('order'));
}
break;
case 'update':
foreach ($selected as $name) {
$plugin->install($plugin->read($name));
}
break;
}

$message = gTxt('plugin_'.($method == 'delete' ? 'deleted' : 'updated'), array('{name}' => join(', ', $selected)));
Expand Down
47 changes: 47 additions & 0 deletions textpattern/vendors/Textpattern/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,53 @@ public function extract($plugin, $normalize = true)
return $plugin;
}

/**
* Update a plugin from file.
*
* @param string $name Plugin name
* @param boolean $normalize Check/normalize some fields
* @return array
*/

public function read($name, $normalize = true)
{
$name = sanitizeForFile($name);
$dir = txpath.DS.'plugins'.DS.$name;

if (!is_dir($dir)) {
return false;
}

$plugin = array('name' => $name);

if (@$info = file_get_contents($dir.'/manifest.json')) {
$plugin += json_decode($info, true);
}

if (@$code = file_get_contents($dir.'/'.$name.'.php')) {
$code = preg_replace('/^\s*<\?(?:php)?\s*|\s*\?>\s*$/i', '', $code);
$plugin['code'] = $code;
}

if (@$textpack = file_get_contents($dir.'/textpack.txp')) {
$plugin['textpack'] = $textpack;
}

if (@$help = file_get_contents($dir.'/help.html')) {
$plugin['help'] = $help;
} elseif (@$help = file_get_contents($dir.'/help.textile')) {
$plugin['help_raw'] = $help;
}

if ($normalize) {
$plugin['type'] = empty($plugin['type']) ? 0 : min(max(intval($plugin['type']), 0), 5);
$plugin['order'] = empty($plugin['order']) ? 5 : min(max(intval($plugin['order']), 1), 9);
$plugin['flags'] = empty($plugin['flags']) ? 0 : intval($plugin['flags']);
}

return $plugin;
}

/**
* Delete plugin from the database.
*
Expand Down

0 comments on commit 30a4d71

Please sign in to comment.