private function test_connection() { if (!self::ping($this->wpdb->dbh)) { MainWPLogger::Instance()->info('Trying to reconnect Wordpress DB Connection'); $this->wpdb->db_connect(); } }
public static function renderActionLogs() { self::renderHeader('Action logs'); if (isset($_REQUEST['actionlogs_status'])) { if ($_REQUEST['actionlogs_status'] != MainWPLogger::DISABLED) { MainWPLogger::Instance()->setLogPriority($_REQUEST['actionlogs_status']); } MainWPLogger::Instance()->log('Action logs set to: ' . MainWPLogger::Instance()->getLogText($_REQUEST['actionlogs_status']), MainWPLogger::LOG); if ($_REQUEST['actionlogs_status'] == MainWPLogger::DISABLED) { MainWPLogger::Instance()->setLogPriority($_REQUEST['actionlogs_status']); } MainWPUtility::update_option('mainwp_actionlogs', $_REQUEST['actionlogs_status']); } $enabled = get_option('mainwp_actionlogs'); if ($enabled === false) { $enabled = MainWPLogger::DISABLED; } ?> <div class="postbox" id="mainwp-code-display"> <h3 class="hndle" style="padding: 8px 12px; font-size: 14px;"><span>Action logs</span></h3> <div style="padding: 1em;"><form method="POST" action=""> Status: <select name="actionlogs_status"> <option value="<?php echo MainWPLogger::DISABLED; ?> " <?php if (MainWPLogger::DISABLED == $enabled) { echo 'selected'; } ?> >Disabled</option> <option value="<?php echo MainWPLogger::WARNING; ?> " <?php if (MainWPLogger::WARNING == $enabled) { echo 'selected'; } ?> >Warning</option> <option value="<?php echo MainWPLogger::INFO; ?> " <?php if (MainWPLogger::INFO == $enabled) { echo 'selected'; } ?> >Info</option> <option value="<?php echo MainWPLogger::DEBUG; ?> " <?php if (MainWPLogger::DEBUG == $enabled) { echo 'selected'; } ?> >Debug</option> </select> <input type="submit" class="button button-primary" value="Save" /> </form></div> <div style="padding: 1em;"><?php MainWPLogger::showLog(); ?> </div> </div> <?php self::renderFooter('Action logs'); }
function deactivation() { wp_clear_scheduled_hook('mainwp_cron_action'); delete_option('mainwp_requests'); try { MainWPUtility::http_post("do=deactivation&url=" . urlencode(get_home_url()), "mainwp.com", "/versioncontrol/rqst.php", 80, 'main', true); } catch (Exception $e) { MainWPLogger::Instance()->warning('An error occured when trying to reach the MainWP server: ' . $e->getMessage()); } }
static function _fetchUrl(&$website, $url, $postdata, $checkConstraints = false, $pForceFetch = false, $verifyCertificate = null, $http_user = null, $http_pass = null) { $agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'; if (!$pForceFetch) { //todo: RS: //check if offline } $identifier = null; if ($checkConstraints) { $semLock = '103218'; //SNSyncLock //Lock $identifier = MainWPUtility::getLockIdentifier($semLock); //Check the delays //In MS $minimumDelay = get_option('mainwp_minimumDelay') === false ? 200 : get_option('mainwp_minimumDelay'); if ($minimumDelay > 0) { $minimumDelay = $minimumDelay / 1000; } $minimumIPDelay = get_option('mainwp_minimumIPDelay') === false ? 1000 : get_option('mainwp_minimumIPDelay'); if ($minimumIPDelay > 0) { $minimumIPDelay = $minimumIPDelay / 1000; } MainWPUtility::endSession(); $delay = true; while ($delay) { MainWPUtility::lock($identifier); if ($minimumDelay > 0) { //Check last request overall $lastRequest = MainWPDB::Instance()->getLastRequestTimestamp(); if ($lastRequest > microtime(true) - $minimumDelay) { //Delay! MainWPUtility::release($identifier); usleep(($minimumDelay - (microtime(true) - $lastRequest)) * 1000 * 1000); continue; } } if ($minimumIPDelay > 0 && $website != null) { //Get ip of this site url $ip = MainWPDB::Instance()->getWPIp($website->id); if ($ip != null && $ip != '') { //Check last request for this site $lastRequest = MainWPDB::Instance()->getLastRequestTimestamp($ip); //Check last request for this subnet? if ($lastRequest > microtime(true) - $minimumIPDelay) { //Delay! MainWPUtility::release($identifier); usleep(($minimumIPDelay - (microtime(true) - $lastRequest)) * 1000 * 1000); continue; } } } $delay = false; } //Check the simultaneous requests $maximumRequests = get_option('mainwp_maximumRequests') === false ? 4 : get_option('mainwp_maximumRequests'); $maximumIPRequests = get_option('mainwp_maximumIPRequests') === false ? 1 : get_option('mainwp_maximumIPRequests'); $first = true; $delay = true; while ($delay) { if (!$first) { MainWPUtility::lock($identifier); } else { $first = false; } //Clean old open requests (may have timed out or something..) MainWPDB::Instance()->closeOpenRequests(); if ($maximumRequests > 0) { $nrOfOpenRequests = MainWPDB::Instance()->getNrOfOpenRequests(); if ($nrOfOpenRequests >= $maximumRequests) { //Delay! MainWPUtility::release($identifier); //Wait 200ms usleep(200000); continue; } } if ($maximumIPRequests > 0 && $website != null) { //Get ip of this site url $ip = MainWPDB::Instance()->getWPIp($website->id); if ($ip != null && $ip != '') { $nrOfOpenRequests = MainWPDB::Instance()->getNrOfOpenRequests($ip); if ($nrOfOpenRequests >= $maximumIPRequests) { //Delay! MainWPUtility::release($identifier); //Wait 200ms usleep(200000); continue; } } } $delay = false; } } if ($website != null) { //Log the start of this request! MainWPDB::Instance()->insertOrUpdateRequestLog($website->id, null, microtime(true), null); } if ($identifier != null) { //Unlock MainWPUtility::release($identifier); } $dirs = self::getMainWPDir(); $cookieDir = $dirs[0] . 'cookies'; if (!@is_dir($cookieDir)) { @mkdir($cookieDir, 0777, true); } if (!file_exists($cookieDir . '/.htaccess')) { $file_htaccess = @fopen($cookieDir . '/.htaccess', 'w+'); @fwrite($file_htaccess, 'deny from all'); @fclose($file_htaccess); } if (!file_exists($cookieDir . '/index.php')) { $file_index = @fopen($cookieDir . '/index.php', 'w+'); @fclose($file_index); } $ch = curl_init(); if ($website != null) { $cookieFile = $cookieDir . '/' . sha1(sha1('mainwp' . LOGGED_IN_SALT . $website->id) . NONCE_SALT . 'WP_Cookie'); if (!file_exists($cookieFile)) { @file_put_contents($cookieFile, ''); } if (file_exists($cookieFile)) { @curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); @curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); } } @curl_setopt($ch, CURLOPT_URL, $url); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); @curl_setopt($ch, CURLOPT_POST, true); @curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); @curl_setopt($ch, CURLOPT_USERAGENT, $agent); if (!empty($http_user) && !empty($http_pass)) { @curl_setopt($ch, CURLOPT_USERPWD, "{$http_user}:{$http_pass}"); } $ssl_verifyhost = false; if ($verifyCertificate !== null) { if ($verifyCertificate == 1) { $ssl_verifyhost = true; } else { if ($verifyCertificate == 2) { // use global setting if (get_option('mainwp_sslVerifyCertificate') === false || get_option('mainwp_sslVerifyCertificate') == 1) { $ssl_verifyhost = true; } } } } else { if (get_option('mainwp_sslVerifyCertificate') === false || get_option('mainwp_sslVerifyCertificate') == 1) { $ssl_verifyhost = true; } } if ($ssl_verifyhost) { @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); } else { @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } $timeout = 20 * 60 * 60; //20 minutes @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if (!ini_get('safe_mode')) { @set_time_limit($timeout); } @ini_set('max_execution_time', $timeout); MainWPUtility::endSession(); $disabled_functions = ini_get('disable_functions'); if (empty($disabled_functions) || stristr($disabled_functions, 'curl_multi_exec') === false) { $mh = @curl_multi_init(); @curl_multi_add_handle($mh, $ch); $lastRun = 0; do { if (time() - $lastRun > 20) { @set_time_limit($timeout); //reset timer.. $lastRun = time(); } @curl_multi_exec($mh, $running); //Execute handlers //$ready = curl_multi_select($mh); while ($info = @curl_multi_info_read($mh)) { $data = @curl_multi_getcontent($info['handle']); $http_status = @curl_getinfo($info['handle'], CURLINFO_HTTP_CODE); $err = @curl_error($info['handle']); $real_url = @curl_getinfo($info['handle'], CURLINFO_EFFECTIVE_URL); @curl_multi_remove_handle($mh, $info['handle']); } usleep(10000); } while ($running > 0); @curl_multi_close($mh); } else { $data = @curl_exec($ch); $http_status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); $err = @curl_error($ch); $real_url = @curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); } $host = parse_url($real_url, PHP_URL_HOST); $ip = gethostbyname($host); if ($website != null) { MainWPDB::Instance()->insertOrUpdateRequestLog($website->id, $ip, null, microtime(true)); } if ($data === false && $http_status == 0) { MainWPLogger::Instance()->debugForWebsite($website, 'fetchUrl', '[' . $url . '] HTTP Error: [status=0][' . $err . ']'); throw new MainWPException('HTTPERROR', $err); } else { if (empty($data) && !empty($err)) { MainWPLogger::Instance()->debugForWebsite($website, 'fetchUrl', '[' . $url . '] HTTP Error: [status=' . $http_status . '][' . $err . ']'); throw new MainWPException('HTTPERROR', $err); } else { if (preg_match('/<mainwp>(.*)<\\/mainwp>/', $data, $results) > 0) { $result = $results[1]; $information = unserialize(base64_decode($result)); return $information; } else { MainWPLogger::Instance()->debugForWebsite($website, 'fetchUrl', '[' . $url . '] Result was: [' . $data . ']'); throw new MainWPException('NOMAINWP', $url); } } } }
public static function getUpgradeInformation($pSlug) { $username = get_option("mainwp_api_username"); $password = MainWPUtility::decrypt(get_option('mainwp_api_password'), 'MainWPAPI'); try { $responseArray = MainWPUtility::http_post('do=getUpgradeInformationV2&slugs=' . $pSlug . '&username='******'An error occured when trying to reach the MainWP server: ' . $e->getMessage()); return null; } if (stripos($responseArray[1], 'error') !== false && stripos($responseArray[1], 'error') == 0) { return null; } $rslt = unserialize($responseArray[1]); $rslt->slug = MainWPSystem::Instance()->slug; return $rslt; }
public static function syncInformationArray(&$pWebsite, &$information, $sync_errors = '', $offline_check_result = 1, $error = false, $pAllowDisconnect = true) { $emptyArray = json_encode(array()); $websiteValues = array('directories' => $emptyArray, 'plugin_upgrades' => $emptyArray, 'theme_upgrades' => $emptyArray, 'securityIssues' => $emptyArray, 'themes' => $emptyArray, 'plugins' => $emptyArray, 'users' => $emptyArray, 'categories' => $emptyArray, 'pluginConflicts' => $emptyArray, 'themeConflicts' => $emptyArray, 'offline_check_result' => $offline_check_result); $websiteSyncValues = array('uptodate' => 0, 'sync_errors' => $sync_errors, 'version' => 0); $done = false; if (isset($information['siteurl'])) { $websiteValues['siteurl'] = $information['siteurl']; $done = true; } if (isset($information['version'])) { $websiteSyncValues['version'] = $information['version']; $done = true; } if (isset($information['directories']) && is_array($information['directories'])) { $websiteValues['directories'] = @json_encode($information['directories']); $done = true; } else { if (isset($information['directories'])) { $websiteValues['directories'] = $information['directories']; $done = true; } } if (isset($information['wp_updates']) && $information['wp_updates'] != null) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'wp_upgrades', @json_encode(array('current' => $information['wpversion'], 'new' => $information['wp_updates']))); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'wp_upgrades', $emptyArray); } if (isset($information['plugin_updates'])) { $websiteValues['plugin_upgrades'] = @json_encode($information['plugin_updates']); $done = true; } if (isset($information['theme_updates'])) { $websiteValues['theme_upgrades'] = @json_encode($information['theme_updates']); $done = true; } if (isset($information['premium_updates'])) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'premium_upgrades', @json_encode($information['premium_updates'])); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'premium_upgrades', $emptyArray); } if (isset($information['securityIssues']) && MainWPUtility::ctype_digit($information['securityIssues']) && $information['securityIssues'] >= 0) { $websiteValues['securityIssues'] = $information['securityIssues']; $done = true; } if (isset($information['recent_comments'])) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_comments', @json_encode($information['recent_comments'])); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_comments', $emptyArray); } if (isset($information['recent_posts'])) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_posts', @json_encode($information['recent_posts'])); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_posts', $emptyArray); } if (isset($information['recent_pages'])) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_pages', @json_encode($information['recent_pages'])); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'recent_pages', $emptyArray); } if (isset($information['themes'])) { $websiteValues['themes'] = @json_encode($information['themes']); $done = true; } if (isset($information['plugins'])) { $websiteValues['plugins'] = @json_encode($information['plugins']); $done = true; } if (isset($information['users'])) { $websiteValues['users'] = @json_encode($information['users']); $done = true; } if (isset($information['categories'])) { $websiteValues['categories'] = @json_encode($information['categories']); $done = true; } if (isset($information['totalsize'])) { $websiteSyncValues['totalsize'] = $information['totalsize']; $done = true; } if (isset($information['dbsize'])) { $websiteSyncValues['dbsize'] = $information['dbsize']; $done = true; } if (isset($information['extauth'])) { $websiteSyncValues['extauth'] = $information['extauth']; $done = true; } if (isset($information['pluginConflicts'])) { $websiteValues['pluginConflicts'] = @json_encode($information['pluginConflicts']); $done = true; } if (isset($information['themeConflicts'])) { $websiteValues['themeConflicts'] = @json_encode($information['themeConflicts']); $done = true; } if (isset($information['last_post_gmt'])) { $websiteSyncValues['last_post_gmt'] = $information['last_post_gmt']; $done = true; } if (isset($information['mainwpdir'])) { $websiteValues['mainwpdir'] = $information['mainwpdir']; $done = true; } if (isset($information['uniqueId'])) { $websiteValues['uniqueId'] = $information['uniqueId']; $done = true; } if (isset($information['faviIcon'])) { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'favi_icon', trim($information['faviIcon'])); $done = true; } else { MainWPDB::Instance()->updateWebsiteOption($pWebsite, 'favi_icon', ""); } if (!$done) { if (isset($information['wpversion'])) { $websiteSyncValues['uptodate'] = 1; $done = true; } else { if (isset($information['error'])) { MainWPLogger::Instance()->warningForWebsite($pWebsite, 'SYNC ERROR', '[' . $information['error'] . ']'); $error = true; $done = true; $websiteSyncValues['sync_errors'] = __('Error - ', 'mainwp') . $information['error']; } else { if (!empty($sync_errors)) { MainWPLogger::Instance()->warningForWebsite($pWebsite, 'SYNC ERROR', '[' . $sync_errors . ']'); $error = true; if (!$pAllowDisconnect) { $sync_errors = ''; } $websiteSyncValues['sync_errors'] = $sync_errors; } else { MainWPLogger::Instance()->warningForWebsite($pWebsite, 'SYNC ERROR', '[Undefined error]'); $error = true; if ($pAllowDisconnect) { $websiteSyncValues['sync_errors'] = __('Undefined error - please reinstall the MainWP Child Plugin on the client site', 'mainwp'); } } } } } if ($done) { $websiteSyncValues['dtsSync'] = time(); } MainWPDB::Instance()->updateWebsiteSyncValues($pWebsite->id, $websiteSyncValues); MainWPDB::Instance()->updateWebsiteValues($pWebsite->id, $websiteValues); //Sync action if (!$error) { do_action('mainwp-site-synced', $pWebsite, $information); } return !$error; }
public static function render() { $news = get_option('mainwp_news'); $newstimestamp = get_option('mainwp_news_timestamp'); if ($newstimestamp === false || time() - $newstimestamp > 60 * 60 * 24) { try { $result = MainWPUtility::http_post("do=news", "mainwp.com", "/versioncontrol/rqst.php", 80, 'main', true); } catch (Exception $e) { MainWPLogger::Instance()->warning('An error occured when trying to reach the MainWP server: ' . $e->getMessage()); } //get news.. if (isset($result[1])) { $news = json_decode($result[1], true); MainWPUtility::update_option('mainwp_news', $news); MainWPUtility::update_option('mainwp_news_timestamp', time()); } } if (!is_array($news) || count($news) == 0) { //No news.. ?> <div>No news items found.</div> <?php return; } ?> <div> <div id="mainwp-news-tabs" class="mainwp-row" style="border-top: 0px"> <?php $newsCategories = array(); foreach ($news as $newsItem) { if (!in_array($newsItem['category'], $newsCategories)) { $newsCategories[] = $newsItem['category']; } } for ($i = 0; $i < count($newsCategories); $i++) { $category = $newsCategories[$i]; /** @var $class string */ if (count($newsCategories) == 1) { $class = 'single'; } else { if ($i == count($newsCategories) - 1) { $class = 'right'; } else { if ($i == 0) { $class = 'left'; } else { $class = 'mid'; } } } if ($category == 'MainWP') { $class .= ' mainwp_action_down'; } ?> <a class="mainwp_action <?php echo $class; ?> mainwp-news-tab" href="#" name="<?php echo MainWPUtility::sanitize($category); ?> "><?php _e($category, 'mainwp'); ?> </a><?php } ?> </div> <div id="mainwp-news-list"> <?php $category = ''; foreach ($news as $newsItem) { if ($category != $newsItem['category']) { if ($category != '') { echo '</div>'; } echo '<div class="mainwp-news-items" name="' . MainWPUtility::sanitize($newsItem['category']) . '" ' . ($newsItem['category'] == 'MainWP' ? '' : 'style="display: none;"') . '>'; } ?> <div class="mainwp-news-item" title="<?php echo MainWPUtility::sanitize($newsItem['title']); ?> "> <strong><?php echo $newsItem['title']; ?> </strong><br /> <?php echo $newsItem['body']; ?> <div style="text-align: right"><em>Submitted <?php if (isset($newsItem['submitter']) && $newsItem['submitter'] != '') { echo 'by <strong>' . $newsItem['submitter'] . '</strong> '; } ?> at <strong><?php echo MainWPUtility::formatTimestamp(MainWPUtility::getTimestamp($newsItem['timestamp'])); ?> </strong></em></div> </div> <?php if ($category != $newsItem['category']) { $category = $newsItem['category']; } } if ($category != '') { echo '</div>'; } ?> </div> </div> <?php }
public static function showLog() { $logFile = MainWPLogger::Instance()->getLogFile(); $fh = @fopen($logFile, 'r'); if ($fh === false) { return; } $previousColor = ''; //self::LOG_COLOR; $fontOpen = false; $firstLinePassedProcessed = false; while (($line = fgets($fh)) !== false) { $currentColor = $previousColor; if (stristr($line, '[DEBUG]')) { $currentColor = self::DEBUG_COLOR; $firstLinePassed = true; } else { if (stristr($line, '[INFO]')) { $currentColor = self::INFO_COLOR; $firstLinePassed = true; } else { if (stristr($line, '[WARNING]')) { $currentColor = self::WARNING_COLOR; $firstLinePassed = true; } else { if (stristr($line, '[LOG]')) { $currentColor = self::LOG_COLOR; $firstLinePassed = true; } else { $firstLinePassed = false; } } } } if ($firstLinePassedProcessed && !$firstLinePassed) { echo ' <strong><font color="green">[multiline, click to read full]</font></strong></div><div style="display: none;">'; } else { echo '<br />'; } $firstLinePassedProcessed = $firstLinePassed; if ($currentColor != $previousColor) { if ($fontOpen) { echo '</div></font>'; } echo '<font color="' . $currentColor . '"><div class="mainwpactionlogsline">'; $fontOpen = true; } // process the line read. echo htmlentities($line); } if ($fontOpen) { echo '</div></font>'; } @fclose($fh); }