#!/usr/bin/php -q tag within PLUGIN-FILE. If the attribute exists, its value (a string) is output and the command exit status is 0. If the attribute does not exist, command exit status is 1. The plugin manager recognizes this set of attributes for the tag: name - MANDATORY plugin name, e.g., "myplugin" or "my-plugin" etc. This tag defines the name of the plugin. The name should omit embedded information such as architecture, version, author, etc. The plugin should create a directory under `/usr/local/emhttp/plugins` named after the plugin, e.g., `/usr/local/emhttp/plugins/myplugin`. Any webGui pages, icons, README files, etc, should be created inside this directory. The plugin should also create a directory under `/boot/config/plugins` named after the plugin, e.g., `/boot/config/plugins/myplugin`. Here is where you store plugin-specific files such as a configuration file and icon file. Note that this directory exists on the users USB Flash device and writes should be minimized. Upon successful installation, the plugin manager will copy the input plugin file to `/boot/config/plugins` on the users USB Flash device, and create a symlink in `/var/log/plugins` also named after the plugin, e.g., `/var/log/plugins/myplugin`. Each time the unRaid server is re-booted, all plugins stored in `/boot/config/plugins` are automatically installed; plugin authors should be aware of this behavior. author - OPTIONAL Whatever you put here will show up under the **Author** column in the Plugin List. If this attribute is omitted we display "anonymous". version - MANDATORY Use a string suitable for comparison to determine if one version is older/same/newer than another version. Any format is acceptable but LimeTech uses "YYYY.MM.DD", e.g., "2014.02.18" (if multiple versions happen to get posted on the same day we add a letter suffix, e.g., "2014.02.18a"). pluginURL - OPTIONAL but MANDATORY if you want "check for updates" to work with your plugin This is the URL of the plugin file to download and extract the **version** attribute from to determine if this is a new version. More attributes may be defined in the future. Here is the set of directories and files used by the plugin system: /boot/config/plugins/ This directory contains the plugin files for plugins to be (re)installed at boot-time. Upon successful `plugin install`, the plugin file is copied here (if not here already). Upon successful `plugin remove`, the plugin file is deleted from here. /boot/config/plugins-error/ This directory contains plugin files that failed to install. /boot/config/plugins-removed/ This directory contains plugin files that have been removed. /boot/config/plugins-stale/ This directory contains plugin files that failed to install because a newer version of the same plugin is already installed. /tmp/plugins/ This directory is used as a target for downloaded plugin files. The `plugin check` operation downloads the plugin file here and the `plugin update` operation looks for the plugin to update here. /var/log/plugins/ This directory contains a symlink named after the plugin name (not the plugin file name) which points to the actual plugin file used to install the plugin. The existence of this file indicates successful install of the plugin. EOF; // Error code to description (wget) // ref: https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html // function error_desc($code) { switch($code) { case 0: return 'No errors'; case -1: return 'Generic error'; case 1: return 'Generic error'; case 2: return 'Parse error'; case 3: return 'File I/O error'; case 4: return 'Network failure'; case 5: return 'SSL verification failure'; case 6: return 'Username/password authentication failure'; case 7: return 'Protocol errors'; case 8: return 'Invalid URL / Server error response'; default: return 'Error code '.$code; } } // Download a file from a URL. // Returns TRUE if success else FALSE and fills in error. // function download($url, $name, &$error) { if ($file = popen("wget --compression=auto --no-cache --progress=dot -O $name $url 2>&1", 'r')) { echo "plugin: downloading: $url ...\r"; $level = -1; while (!feof($file)) { if (preg_match('/\d+%/', fgets($file), $matches)) { $percentage = substr($matches[0],0,-1); if ($percentage > $level) { echo "plugin: downloading: $url ... $percentage%\r"; $level = $percentage; } } } if (($perror = pclose($file)) == 0) { echo "plugin: downloading: $url ... done\n"; return true; } else { echo "plugin: downloading: $url ... failed (".error_desc($perror).")\n"; $error = "$url download failure (".error_desc($perror).")"; return false; } } else { $error = "$url failed to open"; return false; } } // Deal with logging message. // function logger($message) { // echo "$message\n"; shell_exec("logger $message"); } // Interpret a plugin file // Returns TRUE if success, else FALSE and fills in error string. // // If a FILE element does not have a Method attribute, we treat as though Method is "install". // A FILE Method attribute can list multiple methods separated by spaces in which case that file // is processed for any of those methods. // function plugin($method, $plugin_file, &$error) { global $unraid; $methods = ['install', 'remove']; // parse plugin definition XML file $xml = file_exists($plugin_file) ? simplexml_load_file($plugin_file, NULL, LIBXML_NOCDATA) : false; if ($xml === false) { $error = "XML file doesn't exist or xml parse error"; return false; } // dump if ($method == 'dump') { // echo $xml->asXML(); echo print_r($xml); return true; } // release notes if ($method == 'changes') { if (!$xml->CHANGES) return false; return trim($xml->CHANGES); } // check if $method is an attribute if (!in_array($method, $methods)) { foreach ($xml->attributes() as $key => $value) { if ($method == $key) return $value; } $error = "$method attribute not present"; return false; } // Process FILE elements in order // foreach ($xml->FILE as $file) { // skip if not our $method if (isset($file->attributes()->Method)) { if (!in_array($method, explode(" ", $file->attributes()->Method))) continue; } elseif ($method != 'install') continue; $name = $file->attributes()->Name; // bergware - check Unraid version dependency (if present) $min = $file->attributes()->Min; if ($min && version_compare($unraid['version'],$min,'<')) { echo "plugin: skipping: ".basename($name)." - Unraid version too low, requires at least version $min\n"; continue; } $max = $file->attributes()->Max; if ($max && version_compare($unraid['version'],$max,'>')) { echo "plugin: skipping: ".basename($name)." - Unraid version too high, requires at most version $max\n"; continue; } // Name can be missing but only makes sense if Run attribute is present if ($name) { // Ensure parent directory exists // if (!file_exists(dirname($name))) { if (!mkdir(dirname($name), 0770, true)) { $error = "unable to create parent directory for $name"; return false; } } // If file already exists, do not overwrite // if (file_exists($name)) { logger("plugin: skipping: $name already exists"); } elseif ($file->LOCAL) { // Create the file // // for local file, just copy it logger("plugin: creating: $name - copying LOCAL file $file->LOCAL"); if (!copy($file->LOCAL, $name)) { $error = "unable to copy LOCAL file: $name"; @unlink($name); return false; } } elseif ($file->INLINE) { // for inline file, create with inline contents logger("plugin: creating: $name - from INLINE content"); $contents = trim($file->INLINE).PHP_EOL; if ($file->attributes()->Type == 'base64') { logger("plugin: decoding: $name as base64"); $contents = base64_decode($contents); if ($contents === false) { $error = "unable to decode inline base64: $name"; return false; } } if (!file_put_contents($name, $contents)) { $error = "unable to create file: $name"; @unlink($name); return false; } } elseif ($file->URL) { // for download file, download and maybe verify the file MD5 logger("plugin: creating: $name - downloading from URL $file->URL"); if (download($file->URL, $name, $error) === false) { @unlink($name); return false; } if ($file->MD5) { logger("plugin: checking: $name - MD5"); if (md5_file($name) != $file->MD5) { $error = "bad file MD5: $name"; unlink($name); return false; } } } // Maybe change the file mode // if ($file->attributes()->Mode) { // if file has 'Mode' attribute, apply it $mode = $file->attributes()->Mode; logger("plugin: setting: $name - mode to $mode"); if (!chmod($name, octdec($mode))) { $error = "chmod failure: $name"; return false; } } } // Maybe "run" the file now // if ($file->attributes()->Run) { $command = $file->attributes()->Run; if ($name) { logger("plugin: running: $name"); system("$command $name", $retval); } elseif ($file->LOCAL) { logger("plugin: running: $file->LOCAL"); system("$command $file->LOCAL", $retval); } elseif ($file->INLINE) { logger("plugin: running: 'anonymous'"); $inline = escapeshellarg($file->INLINE); passthru("echo $inline | $command", $retval); } if ($retval) { $error = "run failed: $command retval: $retval"; return false; } } } return true; } function move($src_file, $tar_dir) { @mkdir($tar_dir); return rename($src_file, $tar_dir."/".basename($src_file)); } // In following code, // $plugin - is a basename of a plugin, eg, "myplugin.plg" // $plugin_file - is an absolute path, eg, "/boot/config/plugins/myplugin.plg" // // MAIN - single argument if ($argc < 2) { echo $usage; exit(1); } $boot = '/boot/config/plugins'; $plugins = '/var/log/plugins'; $tmp = '/tmp/plugins'; $method = $argv[1]; $builtin = ['unRAIDServer','unRAIDServer-']; // plugin checkall // check all installed plugins, except built-in // if ($method == 'checkall') { foreach (glob("$plugins/*.plg", GLOB_NOSORT) as $link) { // skip OS related plugins if (in_array(basename($link,'.plg'),$builtin)) continue; // only consider symlinks $installed_plugin_file = @readlink($link); if ($installed_plugin_file === false) continue; if (plugin('pluginURL', $installed_plugin_file, $error) === false) continue; $plugin = basename($installed_plugin_file); echo "plugin: checking $plugin ...\n"; exec(realpath($argv[0])." check $plugin >/dev/null"); } exit(0); } // plugin updateall // update all installed plugins, which have a update available // if ($method == 'updateall') { foreach (glob("$plugins/*.plg", GLOB_NOSORT) as $link) { // skip OS related plugins if (in_array(basename($link,'.plg'),$builtin)) continue; // only consider symlinks $installed_plugin_file = @readlink($link); if ($installed_plugin_file === false) continue; if (plugin('pluginURL', $installed_plugin_file, $error) === false) continue; $version = plugin('version', $installed_plugin_file, $error); $plugin = basename($installed_plugin_file); $latest = plugin('version', "$tmp/$plugin", $error); // update only when newer if (strcmp($latest,$version) > 0) { echo "plugin: updating $plugin ...\n"; exec(realpath($argv[0])." update $plugin >/dev/null"); } } exit(0); } // plugin checkos // check built-in only // if ($method == 'checkos') { foreach ($builtin as $link) { // only consider symlinks $installed_plugin_file = @readlink("$plugins/$link.plg"); if ($installed_plugin_file === false) continue; if (plugin("pluginURL", $installed_plugin_file, $error) === false) continue; $plugin = basename($installed_plugin_file); echo "plugin: checking $plugin ...\n"; exec(realpath($argv[0])." check $plugin >/dev/null"); } exit(0); } // MAIN - two or three arguments if ($argc < 3) { echo $usage; exit(1); } // plugin install [plugin_file] // cases: // a) dirname of [plugin_file] is /boot/config/plugins (system startup) // b) [plugin_file] is a URL // c) dirname of [plugin_file] is not /boot/config/plugins // $unraid = parse_ini_file('/etc/unraid-version'); if ($method == 'install') { $argv[2] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $argv[2]); echo "plugin: installing: {$argv[2]}\n"; // check for URL if ((strpos($argv[2], "http://") === 0) || (strpos($argv[2], "https://") === 0)) { $pluginURL = $argv[2]; echo "plugin: downloading $pluginURL\n"; $plugin_file = "$tmp/".basename($pluginURL); if (!download($pluginURL, $plugin_file, $error)) { echo "plugin: $error\n"; @unlink($plugin_file); exit(1); } } else $plugin_file = realpath($argv[2]); // bergware - check Unraid version dependency (if present) $min = plugin('min', $plugin_file, $error); if ($min && version_compare($unraid['version'], $min, '<')) { echo "plugin: installed Unraid version is too low, require at least version $min\n"; exit(1); } $max = plugin('max', $plugin_file, $error); if (empty($max)) $max = plugin('Unraid', $plugin_file, $error); if ($max && version_compare($unraid['version'], $max, '>')) { echo "plugin: installed Unraid version is too high, require at most version $max\n"; exit(1); } $plugin = basename($plugin_file); // check for re-install $installed_plugin_file = @readlink("$plugins/$plugin"); if ($installed_plugin_file !== false) { if ($plugin_file == $installed_plugin_file) { echo "plugin: not re-installing same plugin\n"; exit(1); } // must have version attributes for re-install $version = plugin('version', $plugin_file, $error); if ($version === false) { echo "plugin: $error\n"; exit(1); } $installed_version = plugin('version', $installed_plugin_file, $error); if ($installed_version === false) { echo "plugin: $error\n"; exit(1); } // check version installation? $forced = $argc==4 ? $argv[3] : false; if (!$forced) { // do not re-install if same plugin already installed or has higher version if (strcmp($version, $installed_version) < 0) { echo "plugin: not installing older version\n"; exit(1); } if (strcmp($version, $installed_version) == 0) { echo "plugin: not reinstalling same version\n"; exit(1); } } if (plugin('install', $plugin_file, $error) === false) { echo "plugin: $error\n"; if (dirname($plugin_file) == "$boot") { move($plugin_file, "$boot-error"); } exit(1); } unlink("$plugins/$plugin"); } else { // fresh install if (plugin('install', $plugin_file, $error) === false) { echo "plugin: $error\n"; if (dirname($plugin_file) == "$boot") { move($plugin_file, "$boot-error"); } exit(1); } } // register successful install $target = "$boot/$plugin"; if (!plugin('noInstall', $plugin_file, $error)) { if ($target != $plugin_file) copy($plugin_file, $target); symlink($target, "$plugins/$plugin"); echo "plugin: $plugin installed\n"; } else { echo "script: $plugin executed\n"; } exit(0); } // plugin check [plugin] // We use the pluginURL attribute to download the latest plg file into the "/tmp/plugins/" // directory. // if ($method == 'check') { $plugin = $argv[2]; echo "plugin: checking: $plugin\n"; $installed_plugin_file = @readlink("$plugins/$plugin"); if ($installed_plugin_file === false) { echo "plugin: not installed\n"; exit(1); } $installed_pluginURL = plugin('pluginURL', $installed_plugin_file, $error); if ($installed_pluginURL === false) { echo "plugin: $error\n"; exit(1); } $plugin_file = "$tmp/$plugin"; if (!download($installed_pluginURL, $plugin_file, $error)) { echo "plugin: $error\n"; @unlink($plugin_file); exit(1); } $version = plugin('version', $plugin_file, $error); if ($version === false) { echo "plugin: $error\n"; exit(1); } echo "$version\n"; exit(0); } // plugin update [plugin] // [plugin] is the plg file we are going to be replacing, eg, "old.plg". // We assume a "check" has already been done, ie, "/tmp/plugins/new.plg" already exists. // We execute the "install" method of new.plg. If this fails, then we mark old.plg "not installed"; // the plugin manager will recognize this as an install error. // If install new.plg succeeds, then we remove old.plg and copy new.plg in place. // Finally we mark the new.plg "installed". // if ($method == 'update') { $plugin = $argv[2]; echo "plugin: updating: $plugin\n"; $installed_plugin_file = @readlink("$plugins/$plugin"); if ($installed_plugin_file === false) { echo "plugin: $plugin not installed\n"; exit(1); } // get old support link $previousSupportLink = plugin('support', "$plugins/$plugin", $error); // verify previous check has been done $plugin_file = "$tmp/$plugin"; if (!file_exists($plugin_file)) { echo "plugin: $plugin_file does not exist, check for updates first\n"; exit (1); } // bergware - check Unraid version dependency (if present) $min = plugin('min', $plugin_file, $error); if ($min && version_compare($unraid['version'], $min, '<')) { echo "plugin: installed Unraid version is too low, require at least version $min\n"; exit(1); } $max = plugin('max', $plugin_file, $error); if (empty($max)) $max = plugin('Unraid', $plugin_file, $error); if ($max && version_compare($unraid['version'], $max, '>')) { echo "plugin: installed Unraid version is too high, require at most version $max\n"; exit(1); } // check for a reinstall of same version if (strcmp(plugin('version', $installed_plugin_file,$error), plugin('version', $plugin_file,$error1)) == 0) { echo "Not reinstalling same version\n"; exit(1); } // install the updated plugin if (plugin('install', $plugin_file, $error) === false) { echo "plugin: $error\n"; exit(1); } // install was successful, save the updated plugin so it installs again next boot unlink("$plugins/$plugin"); // re-inject the old support link if the updated plugin doesn't have one if (!plugin('support', $plugin_file,$error) && $previousSupportLink) { echo "\n\nUpdating Support Link\n"; $pluginXML = simplexml_load_file($plugin_file); $pluginXML->addAttribute('support', $previousSupportLink); $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($pluginXML->asXML()); file_put_contents($plugin_file,$dom->saveXML()); } copy($plugin_file,"$boot/$plugin"); symlink("$boot/$plugin", "$plugins/$plugin"); echo "plugin: $plugin updated\n"; exit(0); } // plugin remove [plugin] // only .plg files should have a remove method // if ($method == 'remove') { $plugin = $argv[2]; echo "plugin: removing: $plugin\n"; $installed_plugin_file = @readlink("$plugins/$plugin"); if ($installed_plugin_file !== false) { // remove the symlink unlink("$plugins/$plugin"); @unlink("$tmp/$plugin"); if (plugin('remove', $installed_plugin_file, $error) === false) { // but if can't remove, restore the symlink symlink($installed_plugin_file, "$plugins/$plugin"); echo "plugin: $error\n"; exit(1); } } // remove the plugin file move($installed_plugin_file, "$boot-removed"); echo "plugin: $plugin removed\n"; exec("/usr/local/sbin/update_cron"); exit(0); } // // $plugin_file = $argv[2]; $value = plugin($method, $plugin_file, $error); if ($value === false) { echo "plugin: $error\n"; exit(1); } echo "$value\n"; exit(0); ?>