|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugins Management |
|
4
|
|
|
* @author Joe Huss <[email protected]> |
|
5
|
|
|
* @copyright 2017 |
|
6
|
|
|
* @package MyAdmin |
|
7
|
|
|
* @category Plugins |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* MyAdmin Installer Plugin |
|
12
|
|
|
* @link https://github.com/composer/composer/blob/master/src/Composer/Plugin/PluginInterface.php |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace MyAdmin\Plugins; |
|
16
|
|
|
|
|
17
|
|
|
use Composer\Composer; |
|
18
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
|
19
|
|
|
use Composer\IO\IOInterface; |
|
20
|
|
|
use Composer\Plugin\PluginInterface; |
|
21
|
|
|
use Composer\Plugin\Capable; |
|
22
|
|
|
use Composer\Plugin\PluginEvents; |
|
23
|
|
|
use Composer\Plugin\PreFileDownloadEvent; |
|
24
|
|
|
use Composer\Script\Event; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* MyAdmin Installer Plugin |
|
28
|
|
|
*/ |
|
29
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface, Capable { |
|
30
|
|
|
protected $composer; |
|
31
|
|
|
protected $io; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Apply plugin modifications to Composer |
|
35
|
|
|
* |
|
36
|
|
|
* @param Composer $composer |
|
37
|
|
|
* @param IOInterface $io |
|
38
|
|
|
*/ |
|
39
|
|
|
public function activate(Composer $composer, IOInterface $io) { |
|
40
|
|
|
$this->composer = $composer; |
|
41
|
|
|
$this->io = $io; |
|
42
|
|
|
print 'Hello peoples...'; |
|
43
|
|
|
$installer = new Installer($this->io, $this->composer); |
|
44
|
|
|
$this->composer->getInstallationManager()->addInstaller($installer); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
|
|
public function getCapabilities() { |
|
51
|
|
|
return [ |
|
52
|
|
|
'Composer\Plugin\Capability\CommandProvider' => 'MyAdmin\Plugins\CommandProvider' |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* |
|
58
|
|
|
* Events |
|
59
|
|
|
* |
|
60
|
|
|
* Command Events Composer\Script\Event |
|
61
|
|
|
* |
|
62
|
|
|
* pre-install-cmd occurs before the install command is executed with a lock file present. |
|
63
|
|
|
* post-install-cmd occurs after the install command has been executed with a lock file present. |
|
64
|
|
|
* pre-update-cmd occurs before the update command is executed, or before the install command is executed without a lock file present. |
|
65
|
|
|
* post-update-cmd occurs after the update command has been executed, or after the install command has been executed without a lock file present. |
|
66
|
|
|
* post-status-cmd occurs after the status command has been executed. |
|
67
|
|
|
* pre-archive-cmd occurs before the archive command is executed. |
|
68
|
|
|
* post-archive-cmd occurs after the archive command has been executed. |
|
69
|
|
|
* pre-autoload-dump occurs before the autoloader is dumped, either during install/update, or via the dump-autoload command. |
|
70
|
|
|
* post-autoload-dump occurs after the autoloader has been dumped, either during install/update, or via the dump-autoload command. |
|
71
|
|
|
* post-root-package-install occurs after the root package has been installed, during the create-project command. |
|
72
|
|
|
* post-create-project-cmd occurs after the create-project command has been executed. |
|
73
|
|
|
* |
|
74
|
|
|
* Installer Events Composer\Installer\InstallerEvent |
|
75
|
|
|
* |
|
76
|
|
|
* pre-dependencies-solving occurs before the dependencies are resolved. |
|
77
|
|
|
* post-dependencies-solving occurs after the dependencies have been resolved. |
|
78
|
|
|
* |
|
79
|
|
|
* Package Events Composer\Installer\PackageEvent |
|
80
|
|
|
* |
|
81
|
|
|
* pre-package-install occurs before a package is installed. |
|
82
|
|
|
* post-package-install occurs after a package has been installed. |
|
83
|
|
|
* pre-package-update occurs before a package is updated. |
|
84
|
|
|
* post-package-update occurs after a package has been updated. |
|
85
|
|
|
* pre-package-uninstall occurs before a package is uninstalled. |
|
86
|
|
|
* post-package-uninstall occurs after a package has been uninstalled. |
|
87
|
|
|
* |
|
88
|
|
|
* Plugin Events Composer\Plugin\PluginEvents |
|
89
|
|
|
* |
|
90
|
|
|
* init occurs after a Composer instance is done being initialized. |
|
91
|
|
|
* command occurs before any Composer Command is executed on the CLI. It provides you with access to the input and output objects of the program. |
|
92
|
|
|
* pre-file-download occurs before files are downloaded and allows you to manipulate the RemoteFilesystem object prior to downloading files based on the URL to be downloaded. |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getSubscribedEvents() { |
|
95
|
|
|
return [ |
|
96
|
|
|
PluginEvents::PRE_FILE_DOWNLOAD => [ |
|
97
|
|
|
['onPreFileDownload', 0] |
|
98
|
|
|
] |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param PreFileDownloadEvent $event |
|
104
|
|
|
*/ |
|
105
|
|
|
public function onPreFileDownload(PreFileDownloadEvent $event) { |
|
106
|
|
|
/*$protocol = parse_url($event->getProcessedUrl(), PHP_URL_SCHEME); |
|
|
|
|
|
|
107
|
|
|
if ($protocol === 's3') { |
|
108
|
|
|
$awsClient = new AwsClient($this->io, $this->composer->getConfig()); |
|
109
|
|
|
$s3RemoteFilesystem = new S3RemoteFilesystem($this->io, $event->getRemoteFilesystem()->getOptions(), $awsClient); |
|
110
|
|
|
$event->setRemoteFilesystem($s3RemoteFilesystem); |
|
111
|
|
|
}*/ |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function setPermissions(Event $event) { |
|
115
|
|
|
if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) { |
|
116
|
|
|
$event->getIO()->write('<info>No permissions setup is required on Windows.</info>'); |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
$event->getIO()->write('Setting up permissions.'); |
|
120
|
|
|
try { |
|
121
|
|
|
self::setPermissionsSetfacl($event); |
|
122
|
|
|
return; |
|
123
|
|
|
} catch (\Exception $setfaclException) { |
|
124
|
|
|
$event->getIO()->write(sprintf('<error>%s</error>', $setfaclException->getMessage())); |
|
125
|
|
|
$event->getIO()->write('<info>Trying chmod...</info>'); |
|
126
|
|
|
} |
|
127
|
|
|
try { |
|
128
|
|
|
self::setPermissionsChmod($event); |
|
129
|
|
|
return; |
|
130
|
|
|
} catch (\Exception $chmodException) { |
|
131
|
|
|
$event->getIO()->write(sprintf('<error>%s</error>', $chmodException->getMessage())); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public static function getWritableDirs(Event $event) { |
|
|
|
|
|
|
136
|
|
|
$configuration = $event->getComposer()->getPackage()->getExtra(); |
|
137
|
|
|
if (!isset($configuration['writable-dirs'])) |
|
138
|
|
|
throw new \Exception('The writable-dirs must be specified in composer arbitrary extra data.'); |
|
139
|
|
|
if (!is_array($configuration['writable-dirs'])) |
|
140
|
|
|
throw new \Exception('The writable-dirs must be an array.'); |
|
141
|
|
|
return $configuration['writable-dirs']; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public static function setPermissionsSetfacl(Event $event) { |
|
145
|
|
|
foreach (self::getWritableDirs($event) as $path) |
|
146
|
|
|
self::SetfaclPermissionsSetter($event, $path); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function setPermissionsChmod(Event $event) { |
|
150
|
|
|
foreach (self::getWritableDirs($event) as $path) |
|
151
|
|
|
self::ChmodPermissionsSetter($event, $path); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function SetfaclPermissionsSetter(Event $event, $path) { |
|
|
|
|
|
|
155
|
|
|
if (!is_dir($path)) |
|
156
|
|
|
mkdir($path, 0777, true); |
|
157
|
|
|
if (!is_dir($path)) |
|
158
|
|
|
throw new \Exception('Path Not Found: '.$path); |
|
159
|
|
|
$http_user = self::getHttpdUser($event); |
|
160
|
|
|
self::runProcess($event, 'setfacl -m u:"'.$http_user.'":rwX -m u:'.$_SERVER['USER'].':rwX '.$path); |
|
161
|
|
|
self::runProcess($event, 'setfacl -d -m u:"'.$http_user.'":rwX -m u:'.$_SERVER['USER'].':rwX '.$path); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public static function ChmodPermissionsSetter(Event $event, $path) { |
|
|
|
|
|
|
165
|
|
|
if (!is_dir($path)) |
|
166
|
|
|
mkdir($path, 0777, true); |
|
167
|
|
|
if (!is_dir($path)) |
|
168
|
|
|
throw new \Exception('Path Not Found: '.$path); |
|
169
|
|
|
$http_user = self::getHttpdUser($event); |
|
170
|
|
|
self::runProcess($event, 'chmod +a "'.$http_user.' allow delete,write,append,file_inherit,directory_inherit" '.$path); |
|
171
|
|
|
self::runProcess($event, 'chmod +a "'.$_SERVER['USER'].' allow delete,write,append,file_inherit,directory_inherit" '.$path); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public static function getHttpdUser(Event $event) { |
|
175
|
|
|
$ps = self::runProcess($event, 'ps aux'); |
|
176
|
|
|
preg_match_all('/^.*([a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx)$/m', $ps, $matches); |
|
177
|
|
|
foreach ($matches[0] as $match) { |
|
178
|
|
|
$user = substr($match, 0, strpos($match, ' ')); |
|
179
|
|
|
if ($user != 'root') |
|
180
|
|
|
return $user; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public static function runProcess(Event $event, $commandline) { |
|
185
|
|
|
if ($event->getIO()->isVerbose() === TRUE) |
|
186
|
|
|
$event->getIO()->write(sprintf('Running <info>%s</info>', $commandline)); |
|
187
|
|
|
exec($commandline, $output, $return); |
|
188
|
|
|
if ($return != 0) |
|
|
|
|
|
|
189
|
|
|
throw new \Exception('Returned Error Code '.$return); |
|
190
|
|
|
return implode(PHP_EOL, $output); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.