-
Notifications
You must be signed in to change notification settings - Fork 1
/
Git_Deploy.php
219 lines (188 loc) · 6 KB
/
Git_Deploy.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
class Git_Deploy {
const SLUG = 'git-deploy';
const NONCE_ACTION = 'git_deploy_nonce_action';
const NONCE_NAME = 'git_deploy_nonce_name';
/** @var Git_Deploy */
private static $instance;
private function add_hooks() {
add_action( 'muplugins_loaded', array( $this, 'check_for_request' ) );
add_action( 'plugins_loaded', array( $this, 'check_for_request' ) ); // just in case this isn't a muplugin
if ( is_multisite() ) {
add_action( 'network_admin_menu', array($this, 'register_network_admin_page'), 10, 0);
add_action( 'network_admin_edit_'.self::SLUG, array($this, 'save_network_admin_page'), 10, 0);
} else {
add_action( 'admin_menu', array( $this, 'register_network_admin_page' ), 10, 0 );
}
}
public function register_network_admin_page() {
add_submenu_page(
is_multisite()?'settings.php':'options-general.php',
__('Git Deployment', 'git-info'),
__('Deployment', 'git-info'),
is_multisite()?'manage_network':'manage_options',
self::SLUG,
array($this, 'display_network_admin_page')
);
add_settings_section(
'git-deployment-settings',
__('Deployment Settings', 'git-info'),
array($this, 'display_settings_section'),
self::SLUG
);
add_settings_field(
'git-deployment-ip-addresses',
__('Permitted IP Address', 'git-info'),
array($this, 'display_ip_field'),
self::SLUG,
'git-deployment-settings'
);
add_settings_section(
'git-deploynow',
__('Deploy Now', 'git-info'),
array($this, 'display_deploy_now_settings_section'),
self::SLUG
);
if ( !is_multisite() ) {
register_setting(
'git-deployment-ip-addresses',
self::SLUG
);
}
}
public function display_network_admin_page() {
$title = __('Git Deployment', 'git-info');
require_once( 'views/network-admin.php' );
}
public function save_network_admin_page() {
// settings API doesn't work at the network level, so we save it ourselves
if ( !isset($_POST[self::NONCE_NAME]) || !wp_verify_nonce($_POST[self::NONCE_NAME], self::NONCE_ACTION) ) {
return;
}
$this->save_ip_field();
wp_redirect(add_query_arg(array('page' => self::SLUG, 'updated' => 'true'), network_admin_url('settings.php')));
exit();
}
public function display_settings_section() {
// nothing to do
}
public function display_deploy_now_settings_section() {
$url = add_query_arg(self::SLUG, 1, home_url());
printf(
'<p>%s</p>',
sprintf(
__('Hit the <a href="%s" target="_blank">deployment endpoint</a> manually to force an update to the latest version.', 'git-info'),
$url
)
);
}
public function display_ip_field() {
$ip = implode("\n", $this->allowed_ip_addresses());
printf('<textarea name="%s" rows="6" cols="30">%s</textarea>', 'git-deployment-ip-addresses', esc_textarea($ip));
echo '<p class="description">';
_e('Include one IP address per line. Only logged in users will be able to deploy from other IP addresses.', 'git-info');
echo '</p>';
}
private function save_ip_field() {
if ( isset($_POST['git-deployment-ip-addresses']) ) {
$addresses = explode("\n", $_POST['git-deployment-ip-addresses']);
$save = array();
foreach ( $addresses as $a ) {
$a = trim($a);
if ( preg_match('!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!', $a) ) {
$save[] = $a;
}
}
$this->set_option('git-deployment-ip-addresses', $save);
}
}
private function allowed_ip_addresses() {
return (array)$this->get_option('git-deployment-ip-addresses', array('127.0.0.1'));
}
private function get_option( $option, $default = FALSE ) {
if ( defined('BLOG_ID_CURRENT_SITE') ) {
return get_blog_option(BLOG_ID_CURRENT_SITE, $option, $default);
} else {
return get_option($option, $default);
}
}
private function set_option( $option, $value ) {
if ( defined('BLOG_ID_CURRENT_SITE') ) {
update_blog_option(BLOG_ID_CURRENT_SITE, $option, $value);
} else {
update_option($option, $value);
}
}
public function check_for_request() {
if ( !empty($_GET[self::SLUG]) ) {
if ( $this->authenticate_remote_deployment() ) {
$message = $this->do_pull();
wp_die($message, 'Complete', array('response' => 200));
} else {
wp_die("I'm telling mom!");
}
}
// we only need to run this once. If triggered by muplugins_loaded,
// remove the subsequent trigger. If triggered by plugins_loaded,
// this doesn't matter
remove_action( 'plugins_loaded', array( $this, 'check_for_request' ) );
}
private function authenticate_remote_deployment() {
if ( !defined('AUTH_COOKIE') ) {
// ensure we have the functions/constants we need for authentication
if ( is_multisite() ) {
ms_cookie_constants( );
}
wp_cookie_constants( );
require_once( ABSPATH . WPINC . '/vars.php' );
require_once( ABSPATH . WPINC . '/pluggable.php' );
}
// if a logged-in super admin is hitting the URL with a browser, allow it
if ( is_super_admin() ) {
return TRUE;
}
// otherwise, restrict to a list of IP addresses
if ( in_array( $_SERVER['REMOTE_ADDR'], $this->allowed_ip_addresses() ) ) {
return TRUE;
}
return FALSE;
}
private function do_pull() {
$path = ABSPATH;
$cmd = 'cd '.$path.'; git fetch; git reset --hard $(git rev-parse --symbolic-full-name @{u}); git submodule update --recursive --init;';
$message = "$ $cmd<br />";
exec( $cmd, $output, $return );
$message .= implode('<br />', $output);
return $message;
}
/********** Singleton *************/
/**
* Create the instance of the class
*
* @static
* @return void
*/
public static function init() {
self::$instance = self::get_instance();
}
/**
* Get (and instantiate, if necessary) the instance of the class
* @static
* @return Git_Deploy
*/
public static function get_instance() {
if ( !is_a( self::$instance, __CLASS__ ) ) {
self::$instance = new self();
}
return self::$instance;
}
final public function __clone() {
trigger_error( "No cloning allowed!", E_USER_ERROR );
}
final public function __sleep() {
trigger_error( "No serialization allowed!", E_USER_ERROR );
}
protected function __construct() {
$this->add_hooks();
}
}