-
Notifications
You must be signed in to change notification settings - Fork 3
/
Manager.php
executable file
·176 lines (142 loc) · 6.26 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace WPDemo;
require_once(dirname(__FILE__) . '/Generator.php');
require_once(dirname(__FILE__) . '/Instance.php');
require_once(dirname(__FILE__) . '/Config.php');
class Manager
{
protected $config;
protected static $init;
public function init()
{
if (Manager::$init != true) {
Manager::$init = true;
add_filter( 'plugin_action_links_wpdemo/wpdemo.php', array($this, 'addLinks') );
add_action( 'admin_notices', array( $this, 'addNotices' ) );
add_action( 'admin_post_wpdemo_remove_instances', array( $this, 'removeInstancesNow' ) );
}
}
public function __construct($config) {
$this->config = $config;
}
/**
* Checks that the current MySQL user has all the required permissions.
* @return boolean True if user has all the permissions needed, false otherwise.
*/
public static function hasPermissions()
{
$sql = "SHOW GRANTS for test;";
$generator = new Generator();
$pdo = $generator->getPDO();
$query = $pdo->query($sql);
$results = array_pop(($query->fetch(\PDO::FETCH_NUM)));
return
strpos($results, 'GRANT ALL PRIVILEGES ON') !== false ||
strpos($results, 'SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON') !== false;
}
/**
* Run when the plugin is activated.
* @return bool Returns true.
*/
public function setup()
{
$standin = $this;
//try to create the uploads dir automatically
$dir = explode('/', $this->config->uploadDir);
@mkdir (ABSPATH . '/' . $dir);
add_action('WPDemo.cleanupInstances', function() use ($standin) {
$standin->cleanup();
});
register_activation_hook(__FILE__, function() {
wp_schedule_event(time(), 'hourly', 'WPDemo.cleanupInstances');
});
return true;
}
/**
* Adds the remove instances link to the Plugins page. This wipes all existing demo instances.
* @param array $links Merged links array.
*/
public function addLinks($links)
{
$generator = new Generator();
$count = $generator->countInstances();
$myLinks = array();
$nonce = wp_create_nonce('wpdemo_remove_instances');
$myLinks = array(
vsprintf('<a href="%s">Remove Instances (%s)</a>', array(admin_url('admin-post.php?action=wpdemo_remove_instances&_wpnonce='.$nonce), $count))
);
return array_merge($links, $myLinks);
}
/**
* Adds notices in case of insufficient file and database permissions to the Plugins page.
*/
public function addNotices()
{
$cloneDir = ABSPATH . $this->config->cloneDir;
$folderExists = '<div class="updated"><p><strong>WPDemo could not create folder "%s" which is needed for storing clones of uploads dir. Please create it manually.</strong></p></div>';
$filePermissions = '<div class="updated"><p><strong>WPDemo may not function properly because the "%s" directory is not writable.</strong></p></div>';
$tablePermissions = '<div class="updated"><p><strong>WPDemo may not function properly because the current database user ("%s") does not have all the required MySQL permissions ("grants").</strong></p></div>';
if (file_exists($cloneDir) === false) {
echo vsprintf($folderExists, array($cloneDir));
}
if (file_exists($cloneDir) === true && is_writable($cloneDir) === false) {
echo vsprintf($filePermissions, array($cloneDir));
}
if (Manager::hasPermissions() === false) {
echo vsprintf($tablePermissions, array(DB_USER));
}
}
/**
* Run when the plugin is deactivated. Removes hooks and destroys all demo instances.
* @return bool Returns true.
*/
public function remove()
{
$this->cleanup(0);
return true;
}
public function removeInstancesNow()
{
$nonce = $_GET['_wpnonce'];
if (current_user_can('activate_plugins') === true && wp_verify_nonce($nonce, 'wpdemo_remove_instances') === 1 ) {
$this->cleanup(0);
$location = admin_url('plugins.php');
wp_redirect($location);
} else {
//do nothing
}
}
/**
* Removes stale demo instances - instances that are older than the allowed lifetime.
* @param int $lifetime The max lifetime, in minutes. Default is null (so the value in Config is used). A value of zero means that all instances are destroyed immediately.
* @return int Number of instances destroyed.
*/
public function cleanup($lifetime = null)
{
$generator = new Generator();
$start = $generator->countInstances();
$lifetime = $lifetime === null ? $generator->getConfig()->lifetime : $lifetime;
$generator->destroyInstances(
$lifetime,
$generator->getConfig()->instancePrefix,
$generator->getConfig()->cloneDir . '/' . $generator->getConfig()->instancePrefix
);
$end = $generator->countInstances();
return abs($start-$end);
}
/**
* Handles exceptions, either outputting them to the browers or storing them in a debug log.
* @param Exception $e The exception raised.
* @return void
*/
public static function writeLog($e)
{
$message = vsprintf("WPDemo: %s \n", array($e->getMessage()));
if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG === true ) {
error_log($message, 3, ABSPATH . '/wp-content/debug.log');
exit();
} else {
exit($message);
}
}
}