| 1 |
<?php |
| 2 |
|
| 3 |
class MainWP_Sync { |
| 4 |
|
| 5 |
public static function syncSite( &$pWebsite = null, $pForceFetch = false, $pAllowDisconnect = true ) { |
| 6 |
if ( $pWebsite == null ) { |
| 7 |
return false; |
| 8 |
} |
| 9 |
$userExtension = MainWP_DB::Instance()->getUserExtensionByUserId( $pWebsite->userid ); |
| 10 |
if ( $userExtension == null ) { |
| 11 |
return false; |
| 12 |
} |
| 13 |
|
| 14 |
MainWP_Utility::endSession(); |
| 15 |
|
| 16 |
try { |
| 17 |
|
| 18 |
$cloneEnabled = apply_filters( 'mainwp_clone_enabled', false ); |
| 19 |
$cloneSites = array(); |
| 20 |
if ( $cloneEnabled ) { |
| 21 |
$disallowedCloneSites = get_option( 'mainwp_clone_disallowedsites' ); |
| 22 |
if ( $disallowedCloneSites === false ) { |
| 23 |
$disallowedCloneSites = array(); |
| 24 |
} |
| 25 |
$websites = MainWP_DB::Instance()->query( MainWP_DB::Instance()->getSQLWebsitesForCurrentUser() ); |
| 26 |
if ( $websites ) { |
| 27 |
while ( $websites && ( $website = @MainWP_DB::fetch_object( $websites ) ) ) { |
| 28 |
if ( in_array( $website->id, $disallowedCloneSites ) ) { |
| 29 |
continue; |
| 30 |
} |
| 31 |
if ( $website->id == $pWebsite->id ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
$cloneSites[ $website->id ] = array( |
| 36 |
'name' => $website->name, |
| 37 |
'url' => $website->url, |
| 38 |
'extauth' => $website->extauth, |
| 39 |
'size' => $website->totalsize, |
| 40 |
); |
| 41 |
} |
| 42 |
@MainWP_DB::free_result( $websites ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$primaryBackup = MainWP_Utility::get_primary_backup(); |
| 47 |
|
| 48 |
$othersData = apply_filters( 'mainwp-sync-others-data', array(), $pWebsite ); |
| 49 |
$information = MainWP_Utility::fetchUrlAuthed( $pWebsite, 'stats', array( |
| 50 |
'optimize' => ( ( get_option( 'mainwp_optimize' ) == 1 ) ? 1 : 0 ), |
| 51 |
'heatMap' => 0, |
| 52 |
'cloneSites' => (!$cloneEnabled ? 0 : urlencode( json_encode( $cloneSites ) ) ), |
| 53 |
'othersData' => json_encode( $othersData ), |
| 54 |
'server' => get_admin_url(), |
| 55 |
'numberdaysOutdatePluginTheme' => get_option( 'mainwp_numberdays_Outdate_Plugin_Theme', 365 ), |
| 56 |
'primaryBackup' => $primaryBackup |
| 57 |
), true, $pForceFetch |
| 58 |
); |
| 59 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'primary_lasttime_backup', isset( $information[ 'primaryLasttimeBackup' ] ) ? $information[ 'primaryLasttimeBackup' ] : 0 ); |
| 60 |
$return = self::syncInformationArray( $pWebsite, $information, '', 1, false, $pAllowDisconnect ); |
| 61 |
|
| 62 |
return $return; |
| 63 |
} catch ( MainWP_Exception $e ) { |
| 64 |
$sync_errors = ''; |
| 65 |
$offline_check_result = 1; |
| 66 |
|
| 67 |
if ( $e->getMessage() == 'HTTPERROR' ) { |
| 68 |
$sync_errors = __( 'HTTP error', 'mainwp' ) . ( $e->getMessageExtra() != null ? ' - ' . $e->getMessageExtra() : '' ); |
| 69 |
$offline_check_result = - 1; |
| 70 |
} else if ( $e->getMessage() == 'NOMAINWP' ) { |
| 71 |
$sync_errors = __( 'MainWP Child plugin not detected', 'mainwp' ); |
| 72 |
$offline_check_result = 1; |
| 73 |
} |
| 74 |
|
| 75 |
return self::syncInformationArray( $pWebsite, $information, $sync_errors, $offline_check_result, true, $pAllowDisconnect ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public static function syncInformationArray( &$pWebsite, &$information, $sync_errors = '', $offline_check_result = 1, |
| 80 |
$error = false, $pAllowDisconnect = true ) { |
| 81 |
$emptyArray = json_encode( array() ); |
| 82 |
$websiteValues = array( |
| 83 |
'directories' => $emptyArray, |
| 84 |
'plugin_upgrades' => $emptyArray, |
| 85 |
'theme_upgrades' => $emptyArray, |
| 86 |
'translation_upgrades' => $emptyArray, |
| 87 |
'securityIssues' => $emptyArray, |
| 88 |
'themes' => $emptyArray, |
| 89 |
'plugins' => $emptyArray, |
| 90 |
'users' => $emptyArray, |
| 91 |
'categories' => $emptyArray, |
| 92 |
'offline_check_result' => $offline_check_result, |
| 93 |
); |
| 94 |
$websiteSyncValues = array( |
| 95 |
'uptodate' => 0, |
| 96 |
'sync_errors' => $sync_errors, |
| 97 |
'version' => 0, |
| 98 |
); |
| 99 |
|
| 100 |
$done = false; |
| 101 |
|
| 102 |
$information = apply_filters( 'mainwp_before_save_sync_result', $information, $pWebsite ); |
| 103 |
|
| 104 |
if ( isset( $information[ 'siteurl' ] ) ) { |
| 105 |
$websiteValues[ 'siteurl' ] = $information[ 'siteurl' ]; |
| 106 |
$done = true; |
| 107 |
} |
| 108 |
|
| 109 |
if ( isset( $information[ 'version' ] ) ) { |
| 110 |
$websiteSyncValues[ 'version' ] = $information[ 'version' ]; |
| 111 |
$done = true; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
$phpversion = ''; |
| 116 |
if ( isset( $information[ 'site_info' ] ) && $information[ 'site_info' ] != null ) { |
| 117 |
if ( is_array( $information[ 'site_info' ] ) && isset( $information[ 'site_info' ][ 'phpversion' ] ) ) { |
| 118 |
$phpversion = $information[ 'site_info' ][ 'phpversion' ]; |
| 119 |
} |
| 120 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'site_info', @json_encode( $information[ 'site_info' ] ) ); |
| 121 |
$done = true; |
| 122 |
} else { |
| 123 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'site_info', $emptyArray ); |
| 124 |
} |
| 125 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'phpversion', $phpversion ); |
| 126 |
|
| 127 |
|
| 128 |
if ( isset( $information[ 'directories' ] ) && is_array( $information[ 'directories' ] ) ) { |
| 129 |
$websiteValues[ 'directories' ] = @json_encode( $information[ 'directories' ] ); |
| 130 |
$done = true; |
| 131 |
} else if ( isset( $information[ 'directories' ] ) ) { |
| 132 |
$websiteValues[ 'directories' ] = $information[ 'directories' ]; |
| 133 |
$done = true; |
| 134 |
} |
| 135 |
|
| 136 |
if ( isset( $information[ 'wp_updates' ] ) && $information[ 'wp_updates' ] != null ) { |
| 137 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'wp_upgrades', @json_encode( array( |
| 138 |
'current' => $information[ 'wpversion' ], |
| 139 |
'new' => $information[ 'wp_updates' ], |
| 140 |
) ) ); |
| 141 |
$done = true; |
| 142 |
} else { |
| 143 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'wp_upgrades', $emptyArray ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( isset( $information[ 'plugin_updates' ] ) ) { |
| 147 |
$websiteValues[ 'plugin_upgrades' ] = @json_encode( $information[ 'plugin_updates' ] ); |
| 148 |
$done = true; |
| 149 |
} |
| 150 |
|
| 151 |
if ( isset( $information[ 'theme_updates' ] ) ) { |
| 152 |
$websiteValues[ 'theme_upgrades' ] = @json_encode( $information[ 'theme_updates' ] ); |
| 153 |
$done = true; |
| 154 |
} |
| 155 |
|
| 156 |
if ( isset( $information[ 'translation_updates' ] ) ) { |
| 157 |
$websiteValues[ 'translation_upgrades' ] = @json_encode( $information[ 'translation_updates' ] ); |
| 158 |
$done = true; |
| 159 |
} |
| 160 |
|
| 161 |
if ( isset( $information[ 'premium_updates' ] ) ) { |
| 162 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'premium_upgrades', @json_encode( $information[ 'premium_updates' ] ) ); |
| 163 |
$done = true; |
| 164 |
} else { |
| 165 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'premium_upgrades', $emptyArray ); |
| 166 |
} |
| 167 |
|
| 168 |
if ( isset( $information[ 'securityIssues' ] ) && MainWP_Utility::ctype_digit( $information[ 'securityIssues' ] ) && $information[ 'securityIssues' ] >= 0 ) { |
| 169 |
$websiteValues[ 'securityIssues' ] = $information[ 'securityIssues' ]; |
| 170 |
$done = true; |
| 171 |
} |
| 172 |
|
| 173 |
if ( isset( $information[ 'recent_comments' ] ) ) { |
| 174 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_comments', @json_encode( $information[ 'recent_comments' ] ) ); |
| 175 |
$done = true; |
| 176 |
} else { |
| 177 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_comments', $emptyArray ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( isset( $information[ 'recent_posts' ] ) ) { |
| 181 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_posts', @json_encode( $information[ 'recent_posts' ] ) ); |
| 182 |
$done = true; |
| 183 |
} else { |
| 184 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_posts', $emptyArray ); |
| 185 |
} |
| 186 |
|
| 187 |
if ( isset( $information[ 'recent_pages' ] ) ) { |
| 188 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_pages', @json_encode( $information[ 'recent_pages' ] ) ); |
| 189 |
$done = true; |
| 190 |
} else { |
| 191 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'recent_pages', $emptyArray ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( isset( $information[ 'themes' ] ) ) { |
| 195 |
$websiteValues[ 'themes' ] = @json_encode( $information[ 'themes' ] ); |
| 196 |
$done = true; |
| 197 |
} |
| 198 |
|
| 199 |
if ( isset( $information['plugins'] ) ) { |
| 200 |
$websiteValues['plugins'] = MainWP_Utility::safe_json_encode($information['plugins']); |
| 201 |
$done = true; |
| 202 |
} |
| 203 |
|
| 204 |
if ( isset( $information[ 'users' ] ) ) { |
| 205 |
$websiteValues[ 'users' ] = @json_encode( $information[ 'users' ] ); |
| 206 |
$done = true; |
| 207 |
} |
| 208 |
|
| 209 |
if ( isset( $information[ 'categories' ] ) ) { |
| 210 |
$websiteValues[ 'categories' ] = @json_encode( $information[ 'categories' ] ); |
| 211 |
$done = true; |
| 212 |
} |
| 213 |
|
| 214 |
if ( isset( $information[ 'totalsize' ] ) ) { |
| 215 |
$websiteSyncValues[ 'totalsize' ] = $information[ 'totalsize' ]; |
| 216 |
$done = true; |
| 217 |
} |
| 218 |
|
| 219 |
if ( isset( $information[ 'dbsize' ] ) ) { |
| 220 |
$websiteSyncValues[ 'dbsize' ] = $information[ 'dbsize' ]; |
| 221 |
$done = true; |
| 222 |
} |
| 223 |
|
| 224 |
if ( isset( $information[ 'extauth' ] ) ) { |
| 225 |
$websiteSyncValues[ 'extauth' ] = $information[ 'extauth' ]; |
| 226 |
$done = true; |
| 227 |
} |
| 228 |
|
| 229 |
if ( isset( $information[ 'nossl' ] ) ) { |
| 230 |
$websiteValues[ 'nossl' ] = $information[ 'nossl' ]; |
| 231 |
$done = true; |
| 232 |
} |
| 233 |
|
| 234 |
if ( isset( $information[ 'wpe' ] ) ) { |
| 235 |
$websiteValues[ 'wpe' ] = $information[ 'wpe' ]; |
| 236 |
$done = true; |
| 237 |
} |
| 238 |
|
| 239 |
if ( isset( $information[ 'last_post_gmt' ] ) ) { |
| 240 |
$websiteSyncValues[ 'last_post_gmt' ] = $information[ 'last_post_gmt' ]; |
| 241 |
$done = true; |
| 242 |
} |
| 243 |
|
| 244 |
if ( isset( $information[ 'mainwpdir' ] ) ) { |
| 245 |
$websiteValues[ 'mainwpdir' ] = $information[ 'mainwpdir' ]; |
| 246 |
$done = true; |
| 247 |
} |
| 248 |
|
| 249 |
if ( isset( $information[ 'uniqueId' ] ) ) { |
| 250 |
$websiteValues[ 'uniqueId' ] = $information[ 'uniqueId' ]; |
| 251 |
$done = true; |
| 252 |
} |
| 253 |
|
| 254 |
if ( isset( $information['admin_nicename'] ) ) { |
| 255 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'admin_nicename', trim( $information['admin_nicename'] ) ); |
| 256 |
$done = true; |
| 257 |
} |
| 258 |
|
| 259 |
if ( isset( $information['admin_useremail'] ) ) { |
| 260 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'admin_useremail', trim( $information['admin_useremail'] ) ); |
| 261 |
$done = true; |
| 262 |
} |
| 263 |
|
| 264 |
if ( isset( $information[ 'plugins_outdate_info' ] ) ) { |
| 265 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'plugins_outdate_info', @json_encode( $information[ 'plugins_outdate_info' ] ) ); |
| 266 |
$done = true; |
| 267 |
} else { |
| 268 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'plugins_outdate_info', $emptyArray ); |
| 269 |
} |
| 270 |
|
| 271 |
if ( isset( $information[ 'themes_outdate_info' ] ) ) { |
| 272 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'themes_outdate_info', @json_encode( $information[ 'themes_outdate_info' ] ) ); |
| 273 |
$done = true; |
| 274 |
} else { |
| 275 |
MainWP_DB::Instance()->updateWebsiteOption( $pWebsite, 'themes_outdate_info', $emptyArray ); |
| 276 |
} |
| 277 |
|
| 278 |
if ( !$done ) { |
| 279 |
if ( isset( $information[ 'wpversion' ] ) ) { |
| 280 |
$websiteSyncValues[ 'uptodate' ] = 1; |
| 281 |
$done = true; |
| 282 |
} else if ( isset( $information[ 'error' ] ) ) { |
| 283 |
MainWP_Logger::Instance()->warningForWebsite( $pWebsite, 'SYNC ERROR', '[' . $information[ 'error' ] . ']' ); |
| 284 |
$error = true; |
| 285 |
$done = true; |
| 286 |
$websiteSyncValues[ 'sync_errors' ] = __( 'ERROR: ', 'mainwp' ) . $information[ 'error' ]; |
| 287 |
} else if ( !empty( $sync_errors ) ) { |
| 288 |
MainWP_Logger::Instance()->warningForWebsite( $pWebsite, 'SYNC ERROR', '[' . $sync_errors . ']' ); |
| 289 |
|
| 290 |
$error = true; |
| 291 |
if ( !$pAllowDisconnect ) { |
| 292 |
$sync_errors = ''; |
| 293 |
} |
| 294 |
|
| 295 |
$websiteSyncValues[ 'sync_errors' ] = $sync_errors; |
| 296 |
} else { |
| 297 |
MainWP_Logger::Instance()->warningForWebsite( $pWebsite, 'SYNC ERROR', '[Undefined error]' ); |
| 298 |
$error = true; |
| 299 |
if ( $pAllowDisconnect ) { |
| 300 |
$websiteSyncValues[ 'sync_errors' ] = __( 'Undefined error! Please, reinstall the MainWP Child plugin on the child site.', 'mainwp' ); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if ( $done ) { |
| 306 |
$websiteSyncValues[ 'dtsSync' ] = time(); |
| 307 |
} |
| 308 |
MainWP_DB::Instance()->updateWebsiteSyncValues( $pWebsite->id, $websiteSyncValues ); |
| 309 |
MainWP_DB::Instance()->updateWebsiteValues( $pWebsite->id, $websiteValues ); |
| 310 |
|
| 311 |
//Sync action |
| 312 |
if ( !$error ) { |
| 313 |
do_action( 'mainwp-site-synced', $pWebsite, $information ); |
| 314 |
} |
| 315 |
|
| 316 |
return (!$error ); |
| 317 |
} |
| 318 |
|
| 319 |
public static function statsUpdate( $pSite = null ) { |
| 320 |
//todo: implement |
| 321 |
} |
| 322 |
|
| 323 |
public static function offlineCheck( $pSite = null ) { |
| 324 |
//todo: implement |
| 325 |
} |
| 326 |
|
| 327 |
} |
| 328 |
|