├── README.md
├── plugin.php
└── screenshot2.png
/README.md:
--------------------------------------------------------------------------------
1 | Download Plugin
2 | ================
3 |
4 | Download Yourls Plugin Quickly
5 |
6 | Just copy an url and click download, plugin will be installed.
7 |
8 | ## Screenshot
9 |
10 | 
11 |
12 | ## Required
13 |
14 | `php-zip` extension for unzip
15 |
16 | ## Limit
17 |
18 | - current Only support GitHub URL
19 |
20 | - current Only Download From Branch not Release
21 |
--------------------------------------------------------------------------------
/plugin.php:
--------------------------------------------------------------------------------
1 | Download Plugin plugin need zip extension!");
15 | }
16 |
17 | yourls_add_action('plugins_loaded', 'kriss_download_plugin_settings');
18 | function kriss_download_plugin_settings()
19 | {
20 | yourls_register_plugin_page('download_plugin_settings', 'Download Plugin', 'kriss_download_plugin_page');
21 | }
22 |
23 | function kriss_download_plugin_page()
24 | {
25 | $msg = '';
26 |
27 | if (isset($_POST['github_url'])) {
28 | yourls_verify_nonce('download_plugin_settings');
29 | list($is, $txt) = kriss_download_plugin();
30 | $info = $is ? ['txt' => 'success', 'color' => 'green'] : ['text' => 'fail', 'color' => 'red'];
31 | $msg = "
download {$info['txt']}: {$txt}
";
32 | }
33 |
34 | $nonce = yourls_create_nonce('download_plugin_settings');
35 | echo <<
37 | Download Plugin
38 | plugin list
39 | {$msg}
40 |
64 |
65 | HTML;
66 | }
67 |
68 | function kriss_download_plugin()
69 | {
70 | $url = $_POST['github_url'];
71 | $branch = $_POST['github_branch'];
72 | $name = $_POST['download_name'];
73 |
74 | // parse url
75 | if (strpos($url, 'https://github.com/') === 0) {
76 | list($downloadUrl, $unzipFolderName) = kriss_parse_github_url($url, $branch);
77 | } else {
78 | return [false, 'url not support'];
79 | }
80 |
81 | $downloadName = $name ?: basename($url) . '.zip';
82 | $filepath = __DIR__ . '/../' . $downloadName;
83 | $unzipPath = __DIR__ . '/../';
84 | $unzipFolderName = __DIR__ . '/../' . $unzipFolderName . '/';
85 |
86 | // download file
87 | if (file_exists($filepath)) {
88 | return [false, 'file ' . $filepath . ' existed'];
89 | }
90 | $content = file_get_contents($downloadUrl);
91 | file_put_contents($filepath, $content);
92 |
93 | // unzip
94 | $zip = new ZipArchive();
95 | $unzipOk = false;
96 | if ($zip->open($filepath) === true) {
97 | $zip->extractTo($unzipPath);
98 | $zip->close();
99 | $unzipOk = true;
100 | }
101 |
102 | // auto detect plugin root
103 | $pluginPath = kriss_auto_detect_plugin_root($unzipFolderName, 'plugin.php');
104 | if ($pluginPath === false) {
105 | unlink($filepath);
106 | return [false, 'no plugin.php find in zip'];
107 | }
108 | if ($unzipFolderName . 'plugin.php' !== $pluginPath) {
109 | copy($pluginPath, $unzipFolderName . 'plugin.php');
110 | }
111 |
112 | // delete file
113 | if (isset($_POST['delete_after_unzip']) && $_POST['delete_after_unzip']) {
114 | unlink($filepath);
115 | }
116 |
117 | if (!$unzipOk) {
118 | return [false, 'unzip failed'];
119 | }
120 |
121 | return [true, $downloadName];
122 | }
123 |
124 | function kriss_parse_github_url($url, $branch)
125 | {
126 | $downloadUrl = "$url/archive/refs/heads/{$branch}.zip";
127 | $unzipFolderName = basename($url) . '-' . $branch;
128 |
129 | return [$downloadUrl, $unzipFolderName];
130 | }
131 |
132 | function kriss_auto_detect_plugin_root($path, $pluginName)
133 | {
134 | $path = rtrim($path, '/');
135 | if (file_exists($path . '/'. $pluginName)) {
136 | return $path . '/'. $pluginName;
137 | }
138 | foreach (glob($path . '/*', GLOB_ONLYDIR) as $dir) {
139 | return kriss_auto_detect_plugin_root($dir, $pluginName);
140 | }
141 | return false;
142 | }
143 |
--------------------------------------------------------------------------------
/screenshot2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/krissss/yourls-download-plugin/d945d6929394b158e686aa7ab944e6496faa13d7/screenshot2.png
--------------------------------------------------------------------------------