From be5939e02fd890977c0cbf75f406f925b5a9316f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 1 Nov 2017 02:29:31 -0400 Subject: [PATCH 1/3] Code style cleanup --- include/site-health.php | 649 ++++++++++++++++++++-------------------- include/submit.php | 197 ++++++------ mod/submit.php | 28 +- 3 files changed, 427 insertions(+), 447 deletions(-) diff --git a/include/site-health.php b/include/site-health.php index bf94ad35..0acfd14f 100644 --- a/include/site-health.php +++ b/include/site-health.php @@ -1,5 +1,4 @@ config['site-health']['min_probe_delay']){ - run_site_probe($entry['id'], $entry); - } - - } - - } - - //If it does not exist. - else{ - - //Add it and make sure it is ready for probing. - q( - "INSERT INTO `site-health` (`base_url`, `dt_first_noticed`) VALUES ('%s', NOW())", - dbesc($site) - ); - - //And in case we should probe now, do so. - if($check_health){ - - $result = q( - "SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", - dbesc($site) - ); - if(!empty($result) && isset($result[0])){ - $entry = $result[0]; - run_site_probe($result[0]['id'], $entry); - } - - } - - } - - //Give other scripts the site health. - return isset($entry) ? $entry : false; - -}} + */ +if (!function_exists('notice_site')) { + function notice_site($url, $check_health = false) + { + global $a; + + //Parse the domain from the URL. + $site = parse_site_from_url($url); + + //Search for it in the site-health table. + $result = q( + "SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", + dbesc($site) + ); + + //If it exists, see if we need to update any flags / statuses. + if (!empty($result) && isset($result[0])) { + $entry = $result[0]; + + //If we are allowed to do health checks... + if ($check_health) { + //And the site is in bad health currently, do a check now. + //This is because you have a high certainty the site may perform better now. + if ($entry['health_score'] < -40) { + run_site_probe($entry['id'], $entry); + } + + //Or if the site has not been probed for longer than the minimum delay. + //This is to make sure not everything is postponed to the batches. + elseif (strtotime($entry['dt_last_probed']) < time() - $a->config['site-health']['min_probe_delay']) { + run_site_probe($entry['id'], $entry); + } + } + } + + //If it does not exist. + else { + + //Add it and make sure it is ready for probing. + q( + "INSERT INTO `site-health` (`base_url`, `dt_first_noticed`) VALUES ('%s', NOW())", + dbesc($site) + ); + + //And in case we should probe now, do so. + if ($check_health) { + + $result = q( + "SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", + dbesc($site) + ); + if (!empty($result) && isset($result[0])) { + $entry = $result[0]; + run_site_probe($result[0]['id'], $entry); + } + } + } + + //Give other scripts the site health. + return isset($entry) ? $entry : false; + } +} //Extracts the site from a given URL. -if(! function_exists('parse_site_from_url')){ -function parse_site_from_url($url) -{ - - //Currently a simple implementation, but may improve over time. - #TODO: support subdirectories? - $urlMeta = parse_url($url); - return $urlMeta['scheme'].'://'.$urlMeta['host']; - -}} +if (!function_exists('parse_site_from_url')) { + function parse_site_from_url($url) + { + //Currently a simple implementation, but may improve over time. + #TODO: support subdirectories? + $urlMeta = parse_url($url); + return $urlMeta['scheme'] . '://' . $urlMeta['host']; + } +} //Performs a ping to the given site ID //You may need to notice the site first before you know it's ID. -//TODO: Probe server location using IP address or using the info the friendica server provides (preferred). +//TODO: Probe server location using IP address or using the info the friendica server provides (preferred). // If IP needs to be used only provide country information. //TODO: Check SSLLabs Grade -// Check needs to be asynchronous, meaning that the check at SSLLabs will be initiated in one run while +// Check needs to be asynchronous, meaning that the check at SSLLabs will be initiated in one run while // the results must be fetched later. It might be good to mark sites, where a check has been inititated // f.e. using the ssl_grade field. In the next run, results of these sites could be fetched. -if(! function_exists('run_site_probe')){ -function run_site_probe($id, &$entry_out) -{ - - global $a; - - //Get the site information from the DB, based on the ID. - $result = q( - "SELECT * FROM `site-health` WHERE `id`= %u ORDER BY `id` ASC LIMIT 1", - intval($id) - ); - - //Abort the probe if site is not known. - if(!$result || !isset($result[0])){ - logger('Unknown site-health ID being probed: '.$id); - throw new \Exception('Unknown site-health ID being probed: '.$id); - } - - //Shortcut. - $entry = $result[0]; - $base_url = $entry['base_url']; - $probe_location = $base_url.'/friendica/json'; - - //Prepare the CURL call. - $handle = curl_init(); - $options = array( - - //Timeouts - CURLOPT_TIMEOUT => max($a->config['site-health']['probe_timeout'], 1), //Minimum of 1 second timeout. - CURLOPT_CONNECTTIMEOUT => 1, - - //Redirecting - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_MAXREDIRS => 8, - - //SSL - CURLOPT_SSL_VERIFYPEER => true, - // CURLOPT_VERBOSE => true, - // CURLOPT_CERTINFO => true, - CURLOPT_SSL_VERIFYHOST => 2, - CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, - - //Basic request - CURLOPT_USERAGENT => 'friendica-directory-probe-1.0', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_URL => $probe_location - - ); - curl_setopt_array($handle, $options); - - //Probe the site. - $probe_start = microtime(true); - $probe_data = curl_exec($handle); - $probe_end = microtime(true); - - //Check for SSL problems. - $curl_statuscode = curl_errno($handle); - $sslcert_issues = in_array($curl_statuscode, array( - 60, //Could not authenticate certificate with known CA's - 83 //Issuer check failed - )); - - //When it's the certificate that doesn't work. - if($sslcert_issues){ - - //Probe again, without strict SSL. - $options[CURLOPT_SSL_VERIFYPEER] = false; - - //Replace the handle. - curl_close($handle); - $handle = curl_init(); - curl_setopt_array($handle, $options); - - //Probe. - $probe_start = microtime(true); - $probe_data = curl_exec($handle); - $probe_end = microtime(true); - - //Store new status. - $curl_statuscode = curl_errno($handle); - - } - - //Gather more meta. - $time = round(($probe_end - $probe_start) * 1000); - $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); - $type = curl_getinfo($handle, CURLINFO_CONTENT_TYPE); - $info = curl_getinfo($handle); - - //Done with CURL now. - curl_close($handle); - - #TODO: if the site redirects elsewhere, notice this site and record an issue. - $effective_base_url = parse_site_from_url($info['url']); - $wrong_base_url = $effective_base_url !== $entry['base_url']; - - try{ - $data = json_decode($probe_data); - }catch(\Exception $ex){ - $data = false; - } - - $parse_failed = !$data; - - $parsedDataQuery = ''; - - logger('Effective Base URL: ' . $effective_base_url); - - if($wrong_base_url){ - $parsedDataQuery .= sprintf( - "`effective_base_url` = '%s',", - dbesc($effective_base_url) - ); - }else{ - $parsedDataQuery .= "`effective_base_url` = NULL,"; - } - - if(!$parse_failed){ - - $given_base_url_match = $data->url == $base_url; - - //Record the probe speed in a probes table. - q( - "INSERT INTO `site-probe` (`site_health_id`, `dt_performed`, `request_time`)". - "VALUES (%u, NOW(), %u)", - $entry['id'], - $time - ); - - //Update any health calculations or otherwise processed data. - $parsedDataQuery .= sprintf( - "`dt_last_seen` = NOW(), +if (!function_exists('run_site_probe')) { + function run_site_probe($id, &$entry_out) + { + global $a; + + //Get the site information from the DB, based on the ID. + $result = q( + "SELECT * FROM `site-health` WHERE `id`= %u ORDER BY `id` ASC LIMIT 1", + intval($id) + ); + + //Abort the probe if site is not known. + if (!$result || !isset($result[0])) { + logger('Unknown site-health ID being probed: ' . $id); + throw new \Exception('Unknown site-health ID being probed: ' . $id); + } + + //Shortcut. + $entry = $result[0]; + $base_url = $entry['base_url']; + $probe_location = $base_url . '/friendica/json'; + + //Prepare the CURL call. + $handle = curl_init(); + $options = array( + //Timeouts + CURLOPT_TIMEOUT => max($a->config['site-health']['probe_timeout'], 1), //Minimum of 1 second timeout. + CURLOPT_CONNECTTIMEOUT => 1, + //Redirecting + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 8, + //SSL + CURLOPT_SSL_VERIFYPEER => true, + // CURLOPT_VERBOSE => true, + // CURLOPT_CERTINFO => true, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, + //Basic request + CURLOPT_USERAGENT => 'friendica-directory-probe-1.0', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_URL => $probe_location + ); + curl_setopt_array($handle, $options); + + //Probe the site. + $probe_start = microtime(true); + $probe_data = curl_exec($handle); + $probe_end = microtime(true); + + //Check for SSL problems. + $curl_statuscode = curl_errno($handle); + $sslcert_issues = in_array($curl_statuscode, array( + 60, //Could not authenticate certificate with known CA's + 83 //Issuer check failed + )); + + //When it's the certificate that doesn't work. + if ($sslcert_issues) { + //Probe again, without strict SSL. + $options[CURLOPT_SSL_VERIFYPEER] = false; + + //Replace the handle. + curl_close($handle); + $handle = curl_init(); + curl_setopt_array($handle, $options); + + //Probe. + $probe_start = microtime(true); + $probe_data = curl_exec($handle); + $probe_end = microtime(true); + + //Store new status. + $curl_statuscode = curl_errno($handle); + } + + //Gather more meta. + $time = round(($probe_end - $probe_start) * 1000); + $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); + $type = curl_getinfo($handle, CURLINFO_CONTENT_TYPE); + $info = curl_getinfo($handle); + + //Done with CURL now. + curl_close($handle); + + #TODO: if the site redirects elsewhere, notice this site and record an issue. + $effective_base_url = parse_site_from_url($info['url']); + $wrong_base_url = $effective_base_url !== $entry['base_url']; + + try { + $data = json_decode($probe_data); + } catch (\Exception $ex) { + $data = false; + } + + $parse_failed = !$data; + + $parsedDataQuery = ''; + + logger('Effective Base URL: ' . $effective_base_url); + + if ($wrong_base_url) { + $parsedDataQuery .= sprintf( + "`effective_base_url` = '%s',", + dbesc($effective_base_url) + ); + } else { + $parsedDataQuery .= "`effective_base_url` = NULL,"; + } + + if (!$parse_failed) { + $given_base_url_match = $data->url == $base_url; + + //Record the probe speed in a probes table. + q( + "INSERT INTO `site-probe` (`site_health_id`, `dt_performed`, `request_time`)" . + "VALUES (%u, NOW(), %u)", + $entry['id'], + $time + ); + + //Update any health calculations or otherwise processed data. + $parsedDataQuery .= sprintf( + "`dt_last_seen` = NOW(), `name` = '%s', `version` = '%s', `plugins` = '%s', @@ -237,125 +219,136 @@ function run_site_probe($id, &$entry_out) `admin_name` = '%s', `admin_profile` = '%s', ", - dbesc($data->site_name), - dbesc($data->version), - dbesc(implode("\r\n", $data->plugins)), - dbesc($data->register_policy), - dbesc($data->info), - dbesc($data->admin->name), - dbesc($data->admin->profile) - ); - - //Did we use HTTPS? - $urlMeta = parse_url($probe_location); - if($urlMeta['scheme'] == 'https'){ - $parsedDataQuery .= sprintf("`ssl_state` = b'%u',", $sslcert_issues ? '0' : '1'); - } else { - $parsedDataQuery .= "`ssl_state` = NULL,"; - } - - //Do we have a no scrape supporting node? :D - if(isset($data->no_scrape_url)){ - $parsedDataQuery .= sprintf("`no_scrape_url` = '%s',", dbesc($data->no_scrape_url)); - } - - } - - //Get the new health. - $version = $parse_failed ? '' : $data->version; - $health = health_score_after_probe($entry['health_score'], !$parse_failed, $time, $version, $sslcert_issues); - - //Update the health. - q("UPDATE `site-health` SET + dbesc($data->site_name), + dbesc($data->version), + dbesc(implode("\r\n", $data->plugins)), + dbesc($data->register_policy), + dbesc($data->info), + dbesc($data->admin->name), + dbesc($data->admin->profile) + ); + + //Did we use HTTPS? + $urlMeta = parse_url($probe_location); + if ($urlMeta['scheme'] == 'https') { + $parsedDataQuery .= sprintf("`ssl_state` = b'%u',", $sslcert_issues ? '0' : '1'); + } else { + $parsedDataQuery .= "`ssl_state` = NULL,"; + } + + //Do we have a no scrape supporting node? :D + if (isset($data->no_scrape_url)) { + $parsedDataQuery .= sprintf("`no_scrape_url` = '%s',", dbesc($data->no_scrape_url)); + } + } + + //Get the new health. + $version = $parse_failed ? '' : $data->version; + $health = health_score_after_probe($entry['health_score'], !$parse_failed, $time, $version, $sslcert_issues); + + //Update the health. + q("UPDATE `site-health` SET `health_score` = '%d', $parsedDataQuery `dt_last_probed` = NOW() WHERE `id` = %d LIMIT 1", - $health, - $entry['id'] - ); - - //Get the site information from the DB, based on the ID. - $result = q( - "SELECT * FROM `site-health` WHERE `id`= %u ORDER BY `id` ASC LIMIT 1", - $entry['id'] - ); - - //Return updated entry data. - if($result && isset($result[0])){ - $entry_out = $result[0]; - } - -}} + $health, + $entry['id'] + ); + + //Get the site information from the DB, based on the ID. + $result = q( + "SELECT * FROM `site-health` WHERE `id`= %u ORDER BY `id` ASC LIMIT 1", + $entry['id'] + ); + + //Return updated entry data. + if ($result && isset($result[0])) { + $entry_out = $result[0]; + } + } +} //Determines the new health score after a probe has been executed. -if(! function_exists('health_score_after_probe')){ -function health_score_after_probe($current, $probe_success, $time=null, $version=null, $ssl_issues=null) -{ - - //Probe failed, costs you 30 points. - if(!$probe_success) return max($current-30, -100); - - //A good probe gives you 20 points. - $current += 20; - - //Speed scoring. - if(intval($time) > 0){ - - //Pentaly / bonus points. - if ($time > 800) $current -= 10; //Bad speed. - elseif ($time > 400) $current -= 5; //Still not good. - elseif ($time > 250) $current += 0; //This is normal. - elseif ($time > 120) $current += 5; //Good speed. - else $current += 10; //Excellent speed. - - //Cap for bad speeds. - if ($time > 800) $current = min(40, $current); - elseif ($time > 400) $current = min(60, $current); - - } - - //Version check. - if(!empty($version)){ - - $versionParts = explode('.', $version); - - //Older than 3.x.x? - //Your score can not go above 30 health. - if(intval($versionParts[0]) < 3){ - $current = min($current, 30); - } - - //Older than 3.3.x? - elseif(intval($versionParts[1] < 3)){ - $current -= 5; //Somewhat outdated. - } - - #TODO: See if this needs to be more dynamic. - #TODO: See if this is a proper indicator of health. - - } - - //SSL problems? That's a big deal. - if($ssl_issues === true){ - $current -= 10; - } - - //Don't go beyond +100 or -100. - return max(min(100, $current), -100); - -}} +if (!function_exists('health_score_after_probe')) { + function health_score_after_probe($current, $probe_success, $time = null, $version = null, $ssl_issues = null) + { + //Probe failed, costs you 30 points. + if (!$probe_success) { + return max($current - 30, -100); + } + + //A good probe gives you 20 points. + $current += 20; + + //Speed scoring. + if (intval($time) > 0) { + //Pentaly / bonus points. + if ($time > 800) { + $current -= 10; //Bad speed. + } elseif ($time > 400) { + $current -= 5; //Still not good. + } elseif ($time > 250) { + $current += 0; //This is normal. + } elseif ($time > 120) { + $current += 5; //Good speed. + } else { + $current += 10; //Excellent speed. + } + + //Cap for bad speeds. + if ($time > 800) { + $current = min(40, $current); + } elseif ($time > 400) { + $current = min(60, $current); + } + } + + //Version check. + if (!empty($version)) { + $versionParts = explode('.', $version); + + //Older than 3.x.x? + //Your score can not go above 30 health. + if (intval($versionParts[0]) < 3) { + $current = min($current, 30); + } + + //Older than 3.3.x? + elseif (intval($versionParts[1] < 3)) { + $current -= 5; //Somewhat outdated. + } + + #TODO: See if this needs to be more dynamic. + #TODO: See if this is a proper indicator of health. + } + + //SSL problems? That's a big deal. + if ($ssl_issues === true) { + $current -= 10; + } + + //Don't go beyond +100 or -100. + return max(min(100, $current), -100); + } +} //Changes a score into a name. Used for classes and such. -if(! function_exists('health_score_to_name')){ -function health_score_to_name($score) -{ - - if ($score < -50) return 'very-bad'; - elseif ($score < 0) return 'bad'; - elseif ($score < 30) return 'neutral'; - elseif ($score < 50) return 'ok'; - elseif ($score < 80) return 'good'; - else return 'perfect'; - -}} +if (!function_exists('health_score_to_name')) { + function health_score_to_name($score) + { + if ($score < -50) { + return 'very-bad'; + } elseif ($score < 0) { + return 'bad'; + } elseif ($score < 30) { + return 'neutral'; + } elseif ($score < 50) { + return 'ok'; + } elseif ($score < 80) { + return 'good'; + } else { + return 'perfect'; + } + } +} diff --git a/include/submit.php b/include/submit.php index c454a356..a53a23cd 100644 --- a/include/submit.php +++ b/include/submit.php @@ -1,14 +1,16 @@ 1){ - for($i=1; $i 1) { + for ($i = 1; $i < count($r); $i++) { + logger('Removed duplicate profile ' . intval($r[$i]['id'])); q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1", intval($r[$i]['id']) ); @@ -53,72 +54,65 @@ function run_submit($url) { } } - require_once('Scrape.php'); - //Skip the scrape? :D $noscrape = $site_health && $site_health['no_scrape_url']; - if($noscrape){ - + if ($noscrape) { //Find out who to look up. $which = str_replace($site_health['base_url'], '', $url); $noscrape = preg_match('~/profile/([^/]+)~', $which, $matches) === 1; //If that did not fail... - if($noscrape){ - $parms = noscrape_dfrn($site_health['no_scrape_url'].'/'.$matches[1]); - $noscrape = !!$parms; //If the result was false, do a scrape after all. + if ($noscrape) { + $params = noscrape_dfrn($site_health['no_scrape_url'] . '/' . $matches[1]); + $noscrape = !!$params; //If the result was false, do a scrape after all. } - } - if(!$noscrape){ - $parms = scrape_dfrn($url); + if (!$noscrape) { + $params = scrape_dfrn($url); } - //Empty result is due to an offline site. - if(!count($parms) > 1){ - + // Empty result is due to an offline site. + if (!count($params) > 1) { //For large sites this could lower the health too quickly, so don't track health. //But for sites that are already in bad status. Do a cleanup now. - if($profile_exists && $site_health['health_score'] < $a->config['maintenance']['remove_profile_health_threshold']){ + if ($profile_exists && $site_health['health_score'] < $a->config['maintenance']['remove_profile_health_threshold']) { logger('Nuked bad health record.'); nuke_record($url); } return false; - - } - - //We don't care about valid dfrn if the user indicates to be hidden. - elseif($parms['explicit-hide'] && $profile_exists) { + } elseif (x($params, 'explicit-hide') && $profile_exists) { + // We don't care about valid dfrn if the user indicates to be hidden. logger('User opted out of the directory.'); nuke_record($url); return true; //This is a good update. } - if((x($parms,'hide')) || (! (x($parms,'fn')) && (x($parms,'photo')))) { - if($profile_exists) { + if ((x($params, 'hide')) || (!(x($params, 'fn')) && (x($params, 'photo')))) { + if ($profile_exists) { logger('Profile inferred to be opted out of the directory.'); nuke_record($url); } return true; //This is a good update. } - //This is most likely a problem with the site configuration. Ignore. - if(validate_dfrn($parms)) { + // This is most likely a problem with the site configuration. Ignore. + if (validate_dfrn($params)) { logger('Site is unavailable'); return false; } - $photo = $parms['photo']; + $photo = $params['photo']; - dbesc_array($parms); + dbesc_array($params); - if(x($parms,'comm')) - $parms['comm'] = intval($parms['comm']); + if (x($params, 'comm')) { + $params['comm'] = intval($params['comm']); + } - if($profile_exists) { + if ($profile_exists) { $r = q("UPDATE `profile` SET `name` = '%s', `pdesc` = '%s', @@ -133,50 +127,48 @@ function run_submit($url) { `available` = 1, `updated` = '%s' WHERE `id` = %d LIMIT 1", - - $parms['fn'], - $parms['pdesc'], - $parms['locality'], - $parms['region'], - $parms['postal-code'], - $parms['country-name'], + $params['fn'], + $params['pdesc'], + $params['locality'], + $params['region'], + $params['postal-code'], + $params['country-name'], dbesc($url), dbesc($nurl), - intval($parms['comm']), - $parms['tags'], + intval($params['comm']), + $params['tags'], dbesc(datetime_convert()), intval($profile_id) ); logger('Update returns: ' . $r); - - } - else { + } else { $r = q("INSERT INTO `profile` ( `name`, `pdesc`, `locality`, `region`, `postal-code`, `country-name`, `homepage`, `nurl`, `comm`, `tags`, `created`, `updated` ) VALUES ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )", - $parms['fn'], - $parms['pdesc'], - $parms['locality'], - $parms['region'], - $parms['postal-code'], - $parms['country-name'], + $params['fn'], + x($params, 'pdesc') ? $params['pdesc'] : '', + x($params, 'locality') ? $params['locality'] : '', + x($params, 'region') ? $params['region'] : '', + x($params, 'postal-code') ? $params['postal-code'] : '', + x($params, 'country-name') ? $params['country-name'] : '', dbesc($url), dbesc($nurl), - intval($parms['comm']), - $parms['tags'], + intval($params['comm']), + x($params, 'tags') ? $params['tags'] : '', dbesc(datetime_convert()), dbesc(datetime_convert()) ); logger('Insert returns: ' . $r); - $r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' or `nurl` = '%s' ) order by id asc", + $r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' or `nurl` = '%s' ) ORDER BY `id` ASC", dbesc($url), dbesc($nurl) ); - if(count($r)) + if (count($r)) { $profile_id = $r[count($r) - 1]['id']; + } - if(count($r) > 1) { + if (count($r) > 1) { q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1", intval($r[0]['id']) ); @@ -184,27 +176,24 @@ function run_submit($url) { intval($r[0]['id']) ); } - } - if($parms['tags']) { - $arr = explode(' ', $parms['tags']); - if(count($arr)) { - foreach($arr as $t) { - $t = strip_tags(trim($t)); - $t = substr($t,0,254); + if ($params['tags']) { + $arr = explode(' ', $params['tags']); + foreach ($arr as $t) { + $t = strip_tags(trim($t)); + $t = substr($t, 0, 254); - if(strlen($t)) { - $r = q("SELECT `id` FROM `tag` WHERE `term` = '%s' and `nurl` = '%s' LIMIT 1", + if (strlen($t)) { + $r = q("SELECT `id` FROM `tag` WHERE `term` = '%s' and `nurl` = '%s' LIMIT 1", + dbesc($t), + dbesc($nurl) + ); + if (!count($r)) { + $r = q("INSERT INTO `tag` (`term`, `nurl`) VALUES ('%s', '%s') ", dbesc($t), dbesc($nurl) ); - if(! count($r)) { - $r = q("INSERT INTO `tag` (`term`, `nurl`) VALUES ('%s', '%s') ", - dbesc($t), - dbesc($nurl) - ); - } } } } @@ -212,26 +201,22 @@ function run_submit($url) { $submit_photo_start = microtime(true); - require_once("Photo.php"); - $photo_failure = false; $status = false; - if($profile_id) { - $img_str = fetch_url($photo,true); + if ($profile_id) { + $img_str = fetch_url($photo, true); $img = new Photo($img_str); - if($img) { + if ($img) { $img->scaleImageSquare(80); $r = $img->store($profile_id); } - $r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1", - dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'), + $r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1", dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'), intval($profile_id) ); $status = true; - } - else{ + } else { nuke_record($url); return false; } @@ -241,32 +226,32 @@ function run_submit($url) { $time = round(($submit_end - $submit_start) * 1000); //Record the scrape speed in a scrapes table. - if($site_health && $status) q( - "INSERT INTO `site-scrape` (`site_health_id`, `dt_performed`, `request_time`, `scrape_time`, `photo_time`, `total_time`)". - "VALUES (%u, NOW(), %u, %u, %u, %u)", - $site_health['id'], - $parms['_timings']['fetch'], - $parms['_timings']['scrape'], - $photo_time, - $time - ); + if ($site_health && $status) { + q( + "INSERT INTO `site-scrape` (`site_health_id`, `dt_performed`, `request_time`, `scrape_time`, `photo_time`, `total_time`)" . + "VALUES (%u, NOW(), %u, %u, %u, %u)", + $site_health['id'], + $params['_timings']['fetch'], + $params['_timings']['scrape'], + $photo_time, + $time + ); + } return $status; - } - -function nuke_record($url) { - - $nurl = str_replace(array('https:','//www.'), array('http:','//'), $url); +function nuke_record($url) +{ + $nurl = str_replace(array('https:', '//www.'), array('http:', '//'), $url); $r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' OR `nurl` = '%s' ) ", dbesc($url), dbesc($nurl) ); - if(count($r)) { - foreach($r as $rr) { + if (count($r)) { + foreach ($r as $rr) { q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1", intval($rr['id']) ); diff --git a/mod/submit.php b/mod/submit.php index feb36341..d1ae1145 100644 --- a/mod/submit.php +++ b/mod/submit.php @@ -1,18 +1,20 @@ Date: Wed, 1 Nov 2017 02:29:47 -0400 Subject: [PATCH 2/3] Add debug opportunity in q() --- include/dba.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/dba.php b/include/dba.php index b0f2c5d7..7f98fc80 100644 --- a/include/dba.php +++ b/include/dba.php @@ -140,10 +140,12 @@ function q($sql) $ret = null; if ($db) { - $ret = $db->q(vsprintf($sql, $args)); + $final_sql = vsprintf($sql, $args); + + $ret = $db->q($final_sql); if ($db->db->errno) { - logger('dba: ' . $db->db->error); + logger('dba: ' . $db->db->error . ' sql: ' . $final_sql); } } else { error_log(__FILE__ . ':' . __LINE__ . ' $db has gone'); From 99d182915d41a4c051d281a32045d934840aa8d9 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 1 Nov 2017 02:30:09 -0400 Subject: [PATCH 3/3] Fix invalid sprintf string in submit process --- include/submit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/submit.php b/include/submit.php index a53a23cd..da083c96 100644 --- a/include/submit.php +++ b/include/submit.php @@ -143,7 +143,7 @@ function run_submit($url) logger('Update returns: ' . $r); } else { $r = q("INSERT INTO `profile` ( `name`, `pdesc`, `locality`, `region`, `postal-code`, `country-name`, `homepage`, `nurl`, `comm`, `tags`, `created`, `updated` ) - VALUES ( '%s', '%s', '%s', '%s' , '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )", + VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s' )", $params['fn'], x($params, 'pdesc') ? $params['pdesc'] : '', x($params, 'locality') ? $params['locality'] : '',