Fix submit inserts #34

Merged
MrPetovan merged 3 commits from bug/fix-submit-inserts into master 2017-11-01 08:11:04 +01:00
4 changed files with 432 additions and 450 deletions

View file

@ -140,10 +140,12 @@ function q($sql)
$ret = null; $ret = null;
if ($db) { if ($db) {
$ret = $db->q(vsprintf($sql, $args)); $final_sql = vsprintf($sql, $args);
$ret = $db->q($final_sql);
if ($db->db->errno) { if ($db->db->errno) {
logger('dba: ' . $db->db->error); logger('dba: ' . $db->db->error . ' sql: ' . $final_sql);
} }
} else { } else {
error_log(__FILE__ . ':' . __LINE__ . ' $db has gone'); error_log(__FILE__ . ':' . __LINE__ . ' $db has gone');

View file

@ -1,5 +1,4 @@
<?php <?php
/* /*
Based on a submitted URL, take note of the site it mentions. Based on a submitted URL, take note of the site it mentions.
Ensures that the site health will be tracked if it wasn't already. Ensures that the site health will be tracked if it wasn't already.
@ -7,11 +6,10 @@
Do not enable it unless you have enough execution time to do so. Do not enable it unless you have enough execution time to do so.
But when you do, it's better to check for health whenever a site submits something. But when you do, it's better to check for health whenever a site submits something.
After all, the highest chance for the server to be online is when it submits activity. After all, the highest chance for the server to be online is when it submits activity.
*/ */
if(! function_exists('notice_site')){ if (!function_exists('notice_site')) {
function notice_site($url, $check_health=false) function notice_site($url, $check_health = false)
{ {
global $a; global $a;
//Parse the domain from the URL. //Parse the domain from the URL.
@ -24,31 +22,27 @@ function notice_site($url, $check_health=false)
); );
//If it exists, see if we need to update any flags / statuses. //If it exists, see if we need to update any flags / statuses.
if(!empty($result) && isset($result[0])){ if (!empty($result) && isset($result[0])) {
$entry = $result[0]; $entry = $result[0];
//If we are allowed to do health checks... //If we are allowed to do health checks...
if($check_health){ if ($check_health) {
//And the site is in bad health currently, do a check now. //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. //This is because you have a high certainty the site may perform better now.
if($entry['health_score'] < -40){ if ($entry['health_score'] < -40) {
run_site_probe($entry['id'], $entry); run_site_probe($entry['id'], $entry);
} }
//Or if the site has not been probed for longer than the minimum delay. //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. //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']){ elseif (strtotime($entry['dt_last_probed']) < time() - $a->config['site-health']['min_probe_delay']) {
run_site_probe($entry['id'], $entry); run_site_probe($entry['id'], $entry);
} }
} }
} }
//If it does not exist. //If it does not exist.
else{ else {
//Add it and make sure it is ready for probing. //Add it and make sure it is ready for probing.
q( q(
@ -57,37 +51,34 @@ function notice_site($url, $check_health=false)
); );
//And in case we should probe now, do so. //And in case we should probe now, do so.
if($check_health){ if ($check_health) {
$result = q( $result = q(
"SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", "SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1",
dbesc($site) dbesc($site)
); );
if(!empty($result) && isset($result[0])){ if (!empty($result) && isset($result[0])) {
$entry = $result[0]; $entry = $result[0];
run_site_probe($result[0]['id'], $entry); run_site_probe($result[0]['id'], $entry);
} }
} }
} }
//Give other scripts the site health. //Give other scripts the site health.
return isset($entry) ? $entry : false; return isset($entry) ? $entry : false;
}
}} }
//Extracts the site from a given URL. //Extracts the site from a given URL.
if(! function_exists('parse_site_from_url')){ if (!function_exists('parse_site_from_url')) {
function parse_site_from_url($url) function parse_site_from_url($url)
{ {
//Currently a simple implementation, but may improve over time. //Currently a simple implementation, but may improve over time.
#TODO: support subdirectories? #TODO: support subdirectories?
$urlMeta = parse_url($url); $urlMeta = parse_url($url);
return $urlMeta['scheme'].'://'.$urlMeta['host']; return $urlMeta['scheme'] . '://' . $urlMeta['host'];
}
}} }
//Performs a ping to the given site ID //Performs a ping to the given site ID
//You may need to notice the site first before you know it's ID. //You may need to notice the site first before you know it's ID.
@ -97,10 +88,9 @@ function parse_site_from_url($url)
// 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 // 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. // f.e. using the ssl_grade field. In the next run, results of these sites could be fetched.
if(! function_exists('run_site_probe')){ if (!function_exists('run_site_probe')) {
function run_site_probe($id, &$entry_out) function run_site_probe($id, &$entry_out)
{ {
global $a; global $a;
//Get the site information from the DB, based on the ID. //Get the site information from the DB, based on the ID.
@ -110,40 +100,35 @@ function run_site_probe($id, &$entry_out)
); );
//Abort the probe if site is not known. //Abort the probe if site is not known.
if(!$result || !isset($result[0])){ if (!$result || !isset($result[0])) {
logger('Unknown site-health ID being probed: '.$id); logger('Unknown site-health ID being probed: ' . $id);
throw new \Exception('Unknown site-health ID being probed: '.$id); throw new \Exception('Unknown site-health ID being probed: ' . $id);
} }
//Shortcut. //Shortcut.
$entry = $result[0]; $entry = $result[0];
$base_url = $entry['base_url']; $base_url = $entry['base_url'];
$probe_location = $base_url.'/friendica/json'; $probe_location = $base_url . '/friendica/json';
//Prepare the CURL call. //Prepare the CURL call.
$handle = curl_init(); $handle = curl_init();
$options = array( $options = array(
//Timeouts //Timeouts
CURLOPT_TIMEOUT => max($a->config['site-health']['probe_timeout'], 1), //Minimum of 1 second timeout. CURLOPT_TIMEOUT => max($a->config['site-health']['probe_timeout'], 1), //Minimum of 1 second timeout.
CURLOPT_CONNECTTIMEOUT => 1, CURLOPT_CONNECTTIMEOUT => 1,
//Redirecting //Redirecting
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 8, CURLOPT_MAXREDIRS => 8,
//SSL //SSL
CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYPEER => true,
// CURLOPT_VERBOSE => true, // CURLOPT_VERBOSE => true,
// CURLOPT_CERTINFO => true, // CURLOPT_CERTINFO => true,
CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
//Basic request //Basic request
CURLOPT_USERAGENT => 'friendica-directory-probe-1.0', CURLOPT_USERAGENT => 'friendica-directory-probe-1.0',
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $probe_location CURLOPT_URL => $probe_location
); );
curl_setopt_array($handle, $options); curl_setopt_array($handle, $options);
@ -160,8 +145,7 @@ function run_site_probe($id, &$entry_out)
)); ));
//When it's the certificate that doesn't work. //When it's the certificate that doesn't work.
if($sslcert_issues){ if ($sslcert_issues) {
//Probe again, without strict SSL. //Probe again, without strict SSL.
$options[CURLOPT_SSL_VERIFYPEER] = false; $options[CURLOPT_SSL_VERIFYPEER] = false;
@ -177,7 +161,6 @@ function run_site_probe($id, &$entry_out)
//Store new status. //Store new status.
$curl_statuscode = curl_errno($handle); $curl_statuscode = curl_errno($handle);
} }
//Gather more meta. //Gather more meta.
@ -193,9 +176,9 @@ function run_site_probe($id, &$entry_out)
$effective_base_url = parse_site_from_url($info['url']); $effective_base_url = parse_site_from_url($info['url']);
$wrong_base_url = $effective_base_url !== $entry['base_url']; $wrong_base_url = $effective_base_url !== $entry['base_url'];
try{ try {
$data = json_decode($probe_data); $data = json_decode($probe_data);
}catch(\Exception $ex){ } catch (\Exception $ex) {
$data = false; $data = false;
} }
@ -205,22 +188,21 @@ function run_site_probe($id, &$entry_out)
logger('Effective Base URL: ' . $effective_base_url); logger('Effective Base URL: ' . $effective_base_url);
if($wrong_base_url){ if ($wrong_base_url) {
$parsedDataQuery .= sprintf( $parsedDataQuery .= sprintf(
"`effective_base_url` = '%s',", "`effective_base_url` = '%s',",
dbesc($effective_base_url) dbesc($effective_base_url)
); );
}else{ } else {
$parsedDataQuery .= "`effective_base_url` = NULL,"; $parsedDataQuery .= "`effective_base_url` = NULL,";
} }
if(!$parse_failed){ if (!$parse_failed) {
$given_base_url_match = $data->url == $base_url; $given_base_url_match = $data->url == $base_url;
//Record the probe speed in a probes table. //Record the probe speed in a probes table.
q( q(
"INSERT INTO `site-probe` (`site_health_id`, `dt_performed`, `request_time`)". "INSERT INTO `site-probe` (`site_health_id`, `dt_performed`, `request_time`)" .
"VALUES (%u, NOW(), %u)", "VALUES (%u, NOW(), %u)",
$entry['id'], $entry['id'],
$time $time
@ -248,17 +230,16 @@ function run_site_probe($id, &$entry_out)
//Did we use HTTPS? //Did we use HTTPS?
$urlMeta = parse_url($probe_location); $urlMeta = parse_url($probe_location);
if($urlMeta['scheme'] == 'https'){ if ($urlMeta['scheme'] == 'https') {
$parsedDataQuery .= sprintf("`ssl_state` = b'%u',", $sslcert_issues ? '0' : '1'); $parsedDataQuery .= sprintf("`ssl_state` = b'%u',", $sslcert_issues ? '0' : '1');
} else { } else {
$parsedDataQuery .= "`ssl_state` = NULL,"; $parsedDataQuery .= "`ssl_state` = NULL,";
} }
//Do we have a no scrape supporting node? :D //Do we have a no scrape supporting node? :D
if(isset($data->no_scrape_url)){ if (isset($data->no_scrape_url)) {
$parsedDataQuery .= sprintf("`no_scrape_url` = '%s',", dbesc($data->no_scrape_url)); $parsedDataQuery .= sprintf("`no_scrape_url` = '%s',", dbesc($data->no_scrape_url));
} }
} }
//Get the new health. //Get the new health.
@ -282,80 +263,92 @@ function run_site_probe($id, &$entry_out)
); );
//Return updated entry data. //Return updated entry data.
if($result && isset($result[0])){ if ($result && isset($result[0])) {
$entry_out = $result[0]; $entry_out = $result[0];
} }
}
}} }
//Determines the new health score after a probe has been executed. //Determines the new health score after a probe has been executed.
if(! function_exists('health_score_after_probe')){ if (!function_exists('health_score_after_probe')) {
function health_score_after_probe($current, $probe_success, $time=null, $version=null, $ssl_issues=null) function health_score_after_probe($current, $probe_success, $time = null, $version = null, $ssl_issues = null)
{ {
//Probe failed, costs you 30 points. //Probe failed, costs you 30 points.
if(!$probe_success) return max($current-30, -100); if (!$probe_success) {
return max($current - 30, -100);
}
//A good probe gives you 20 points. //A good probe gives you 20 points.
$current += 20; $current += 20;
//Speed scoring. //Speed scoring.
if(intval($time) > 0){ if (intval($time) > 0) {
//Pentaly / bonus points. //Pentaly / bonus points.
if ($time > 800) $current -= 10; //Bad speed. if ($time > 800) {
elseif ($time > 400) $current -= 5; //Still not good. $current -= 10; //Bad speed.
elseif ($time > 250) $current += 0; //This is normal. } elseif ($time > 400) {
elseif ($time > 120) $current += 5; //Good speed. $current -= 5; //Still not good.
else $current += 10; //Excellent speed. } elseif ($time > 250) {
$current += 0; //This is normal.
} elseif ($time > 120) {
$current += 5; //Good speed.
} else {
$current += 10; //Excellent speed.
}
//Cap for bad speeds. //Cap for bad speeds.
if ($time > 800) $current = min(40, $current); if ($time > 800) {
elseif ($time > 400) $current = min(60, $current); $current = min(40, $current);
} elseif ($time > 400) {
$current = min(60, $current);
}
} }
//Version check. //Version check.
if(!empty($version)){ if (!empty($version)) {
$versionParts = explode('.', $version); $versionParts = explode('.', $version);
//Older than 3.x.x? //Older than 3.x.x?
//Your score can not go above 30 health. //Your score can not go above 30 health.
if(intval($versionParts[0]) < 3){ if (intval($versionParts[0]) < 3) {
$current = min($current, 30); $current = min($current, 30);
} }
//Older than 3.3.x? //Older than 3.3.x?
elseif(intval($versionParts[1] < 3)){ elseif (intval($versionParts[1] < 3)) {
$current -= 5; //Somewhat outdated. $current -= 5; //Somewhat outdated.
} }
#TODO: See if this needs to be more dynamic. #TODO: See if this needs to be more dynamic.
#TODO: See if this is a proper indicator of health. #TODO: See if this is a proper indicator of health.
} }
//SSL problems? That's a big deal. //SSL problems? That's a big deal.
if($ssl_issues === true){ if ($ssl_issues === true) {
$current -= 10; $current -= 10;
} }
//Don't go beyond +100 or -100. //Don't go beyond +100 or -100.
return max(min(100, $current), -100); return max(min(100, $current), -100);
}
}} }
//Changes a score into a name. Used for classes and such. //Changes a score into a name. Used for classes and such.
if(! function_exists('health_score_to_name')){ if (!function_exists('health_score_to_name')) {
function health_score_to_name($score) function health_score_to_name($score)
{ {
if ($score < -50) {
if ($score < -50) return 'very-bad'; return 'very-bad';
elseif ($score < 0) return 'bad'; } elseif ($score < 0) {
elseif ($score < 30) return 'neutral'; return 'bad';
elseif ($score < 50) return 'ok'; } elseif ($score < 30) {
elseif ($score < 80) return 'good'; return 'neutral';
else return 'perfect'; } elseif ($score < 50) {
return 'ok';
}} } elseif ($score < 80) {
return 'good';
} else {
return 'perfect';
}
}
}

View file

@ -1,14 +1,16 @@
<?php <?php
require_once 'datetime.php';
require_once 'site-health.php';
require_once 'Scrape.php';
require_once 'Photo.php';
require_once('datetime.php'); function run_submit($url)
require_once('site-health.php'); {
function run_submit($url) {
global $a; global $a;
if(! strlen($url)) if (!strlen($url)) {
return false; return false;
}
logger('Updating: ' . $url); logger('Updating: ' . $url);
@ -17,7 +19,7 @@ function run_submit($url) {
$submit_start = microtime(true); $submit_start = microtime(true);
$nurl = str_replace(array('https:','//www.'), array('http:','//'), $url); $nurl = str_replace(array('https:', '//www.'), array('http:', '//'), $url);
$profile_exists = false; $profile_exists = false;
@ -26,24 +28,23 @@ function run_submit($url) {
dbesc($nurl) dbesc($nurl)
); );
if(count($r)) { $profile_id = null;
if (count($r)) {
$profile_exists = true; $profile_exists = true;
$profile_id = $r[0]['id']; $profile_id = $r[0]['id'];
$r = q("UPDATE `profile` SET $r = q("UPDATE `profile` SET
`available` = 0, `available` = 0,
`updated` = '%s' `updated` = '%s'
WHERE `id` = %d LIMIT 1", WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($profile_id)
dbesc(datetime_convert()),
intval($profile_id)
); );
} }
//Remove duplicates. //Remove duplicates.
if(count($r) > 1){ if (count($r) > 1) {
for($i=1; $i<count($r); $i++){ for ($i = 1; $i < count($r); $i++) {
logger('Removed duplicate profile '.intval($r[$i]['id'])); logger('Removed duplicate profile ' . intval($r[$i]['id']));
q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1", q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1",
intval($r[$i]['id']) intval($r[$i]['id'])
); );
@ -53,72 +54,65 @@ function run_submit($url) {
} }
} }
require_once('Scrape.php');
//Skip the scrape? :D //Skip the scrape? :D
$noscrape = $site_health && $site_health['no_scrape_url']; $noscrape = $site_health && $site_health['no_scrape_url'];
if($noscrape){ if ($noscrape) {
//Find out who to look up. //Find out who to look up.
$which = str_replace($site_health['base_url'], '', $url); $which = str_replace($site_health['base_url'], '', $url);
$noscrape = preg_match('~/profile/([^/]+)~', $which, $matches) === 1; $noscrape = preg_match('~/profile/([^/]+)~', $which, $matches) === 1;
//If that did not fail... //If that did not fail...
if($noscrape){ if ($noscrape) {
$parms = noscrape_dfrn($site_health['no_scrape_url'].'/'.$matches[1]); $params = noscrape_dfrn($site_health['no_scrape_url'] . '/' . $matches[1]);
$noscrape = !!$parms; //If the result was false, do a scrape after all. $noscrape = !!$params; //If the result was false, do a scrape after all.
}
} }
if (!$noscrape) {
$params = scrape_dfrn($url);
} }
if(!$noscrape){ // Empty result is due to an offline site.
$parms = scrape_dfrn($url); if (!count($params) > 1) {
}
//Empty result is due to an offline site.
if(!count($parms) > 1){
//For large sites this could lower the health too quickly, so don't track health. //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. //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.'); logger('Nuked bad health record.');
nuke_record($url); nuke_record($url);
} }
return false; return false;
} elseif (x($params, 'explicit-hide') && $profile_exists) {
} // We don't care about valid dfrn if the user indicates to be hidden.
//We don't care about valid dfrn if the user indicates to be hidden.
elseif($parms['explicit-hide'] && $profile_exists) {
logger('User opted out of the directory.'); logger('User opted out of the directory.');
nuke_record($url); nuke_record($url);
return true; //This is a good update. return true; //This is a good update.
} }
if((x($parms,'hide')) || (! (x($parms,'fn')) && (x($parms,'photo')))) { if ((x($params, 'hide')) || (!(x($params, 'fn')) && (x($params, 'photo')))) {
if($profile_exists) { if ($profile_exists) {
logger('Profile inferred to be opted out of the directory.'); logger('Profile inferred to be opted out of the directory.');
nuke_record($url); nuke_record($url);
} }
return true; //This is a good update. return true; //This is a good update.
} }
//This is most likely a problem with the site configuration. Ignore. // This is most likely a problem with the site configuration. Ignore.
if(validate_dfrn($parms)) { if (validate_dfrn($params)) {
logger('Site is unavailable'); logger('Site is unavailable');
return false; return false;
} }
$photo = $parms['photo']; $photo = $params['photo'];
dbesc_array($parms); dbesc_array($params);
if(x($parms,'comm')) if (x($params, 'comm')) {
$parms['comm'] = intval($parms['comm']); $params['comm'] = intval($params['comm']);
}
if($profile_exists) { if ($profile_exists) {
$r = q("UPDATE `profile` SET $r = q("UPDATE `profile` SET
`name` = '%s', `name` = '%s',
`pdesc` = '%s', `pdesc` = '%s',
@ -133,50 +127,48 @@ function run_submit($url) {
`available` = 1, `available` = 1,
`updated` = '%s' `updated` = '%s'
WHERE `id` = %d LIMIT 1", WHERE `id` = %d LIMIT 1",
$params['fn'],
$parms['fn'], $params['pdesc'],
$parms['pdesc'], $params['locality'],
$parms['locality'], $params['region'],
$parms['region'], $params['postal-code'],
$parms['postal-code'], $params['country-name'],
$parms['country-name'],
dbesc($url), dbesc($url),
dbesc($nurl), dbesc($nurl),
intval($parms['comm']), intval($params['comm']),
$parms['tags'], $params['tags'],
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($profile_id) intval($profile_id)
); );
logger('Update returns: ' . $r); 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` ) $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' )",
$parms['fn'], $params['fn'],
$parms['pdesc'], x($params, 'pdesc') ? $params['pdesc'] : '',
$parms['locality'], x($params, 'locality') ? $params['locality'] : '',
$parms['region'], x($params, 'region') ? $params['region'] : '',
$parms['postal-code'], x($params, 'postal-code') ? $params['postal-code'] : '',
$parms['country-name'], x($params, 'country-name') ? $params['country-name'] : '',
dbesc($url), dbesc($url),
dbesc($nurl), dbesc($nurl),
intval($parms['comm']), intval($params['comm']),
$parms['tags'], x($params, 'tags') ? $params['tags'] : '',
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()) dbesc(datetime_convert())
); );
logger('Insert returns: ' . $r); 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($url),
dbesc($nurl) dbesc($nurl)
); );
if(count($r)) if (count($r)) {
$profile_id = $r[count($r) - 1]['id']; $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", q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1",
intval($r[0]['id']) intval($r[0]['id'])
); );
@ -184,22 +176,20 @@ function run_submit($url) {
intval($r[0]['id']) intval($r[0]['id'])
); );
} }
} }
if($parms['tags']) { if ($params['tags']) {
$arr = explode(' ', $parms['tags']); $arr = explode(' ', $params['tags']);
if(count($arr)) { foreach ($arr as $t) {
foreach($arr as $t) {
$t = strip_tags(trim($t)); $t = strip_tags(trim($t));
$t = substr($t,0,254); $t = substr($t, 0, 254);
if(strlen($t)) { if (strlen($t)) {
$r = q("SELECT `id` FROM `tag` WHERE `term` = '%s' and `nurl` = '%s' LIMIT 1", $r = q("SELECT `id` FROM `tag` WHERE `term` = '%s' and `nurl` = '%s' LIMIT 1",
dbesc($t), dbesc($t),
dbesc($nurl) dbesc($nurl)
); );
if(! count($r)) { if (!count($r)) {
$r = q("INSERT INTO `tag` (`term`, `nurl`) VALUES ('%s', '%s') ", $r = q("INSERT INTO `tag` (`term`, `nurl`) VALUES ('%s', '%s') ",
dbesc($t), dbesc($t),
dbesc($nurl) dbesc($nurl)
@ -208,30 +198,25 @@ function run_submit($url) {
} }
} }
} }
}
$submit_photo_start = microtime(true); $submit_photo_start = microtime(true);
require_once("Photo.php");
$photo_failure = false; $photo_failure = false;
$status = false; $status = false;
if($profile_id) { if ($profile_id) {
$img_str = fetch_url($photo,true); $img_str = fetch_url($photo, true);
$img = new Photo($img_str); $img = new Photo($img_str);
if($img) { if ($img) {
$img->scaleImageSquare(80); $img->scaleImageSquare(80);
$r = $img->store($profile_id); $r = $img->store($profile_id);
} }
$r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1", $r = q("UPDATE `profile` SET `photo` = '%s' WHERE `id` = %d LIMIT 1", dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'),
dbesc($a->get_baseurl() . '/photo/' . $profile_id . '.jpg'),
intval($profile_id) intval($profile_id)
); );
$status = true; $status = true;
} } else {
else{
nuke_record($url); nuke_record($url);
return false; return false;
} }
@ -241,32 +226,32 @@ function run_submit($url) {
$time = round(($submit_end - $submit_start) * 1000); $time = round(($submit_end - $submit_start) * 1000);
//Record the scrape speed in a scrapes table. //Record the scrape speed in a scrapes table.
if($site_health && $status) q( if ($site_health && $status) {
"INSERT INTO `site-scrape` (`site_health_id`, `dt_performed`, `request_time`, `scrape_time`, `photo_time`, `total_time`)". 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)", "VALUES (%u, NOW(), %u, %u, %u, %u)",
$site_health['id'], $site_health['id'],
$parms['_timings']['fetch'], $params['_timings']['fetch'],
$parms['_timings']['scrape'], $params['_timings']['scrape'],
$photo_time, $photo_time,
$time $time
); );
}
return $status; return $status;
} }
function nuke_record($url)
function nuke_record($url) { {
$nurl = str_replace(array('https:', '//www.'), array('http:', '//'), $url);
$nurl = str_replace(array('https:','//www.'), array('http:','//'), $url);
$r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' OR `nurl` = '%s' ) ", $r = q("SELECT `id` FROM `profile` WHERE ( `homepage` = '%s' OR `nurl` = '%s' ) ",
dbesc($url), dbesc($url),
dbesc($nurl) dbesc($nurl)
); );
if(count($r)) { if (count($r)) {
foreach($r as $rr) { foreach ($r as $rr) {
q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1", q("DELETE FROM `photo` WHERE `profile-id` = %d LIMIT 1",
intval($rr['id']) intval($rr['id'])
); );

View file

@ -1,10 +1,12 @@
<?php <?php
require_once('include/submit.php'); require_once 'include/submit.php';
require_once('include/sync.php'); require_once 'include/sync.php';
function submit_content(&$a) { use Friendica\Directory\App;
function submit_content(App &$a)
{
//Decode the URL. //Decode the URL.
$url = hex2bin(notags(trim($_GET['url']))); $url = hex2bin(notags(trim($_GET['url'])));
@ -13,6 +15,6 @@ function submit_content(&$a) {
//Run the submit sequence. //Run the submit sequence.
run_submit($url); run_submit($url);
exit;
exit;
} }