dir/mod/admin.php

211 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2012-05-16 07:31:36 +02:00
<?php
function admin_content(&$a) {
2014-07-11 00:30:36 +02:00
2012-05-16 07:31:36 +02:00
if(! $_SESSION['uid']) {
notice("Permission denied.");
2014-07-11 00:30:36 +02:00
goaway($a->get_baseurl());
2012-05-16 07:31:36 +02:00
}
2014-07-11 00:30:36 +02:00
//Get 100 flagged entries.
$r = q("SELECT `flag`.*, `profile`.`name`, `profile`.`homepage`
FROM `flag` JOIN `profile` ON `flag`.`pid`=`profile`.`id`
ORDER BY `total` DESC LIMIT 100"
);
2012-05-16 07:31:36 +02:00
if(count($r)) {
2014-07-11 00:30:36 +02:00
$flagged = '';
2012-05-16 07:31:36 +02:00
foreach($r as $rr) {
if($rr['reason'] == 1)
2014-07-11 01:35:30 +02:00
$str = 'Adult';
2012-05-16 07:31:36 +02:00
if($rr['reason'] == 2)
2014-07-11 00:30:36 +02:00
$str = 'Dead';
2014-07-11 01:35:30 +02:00
$flagged .= '<a href="' . 'moderate/' . $rr['pid'] . '/' . $rr['reason'] . '">'.
"{$rr['total']}x $str - [#{$rr['pid']}] {$rr['name']} ({$rr['homepage']})</a><br />";
2012-05-16 07:31:36 +02:00
}
2014-07-11 00:30:36 +02:00
} else {
$flagged = 'No entries.';
2012-05-16 07:31:36 +02:00
}
2014-07-11 00:30:36 +02:00
//Get the maintenance backlog size.
$res = q("SELECT count(*) as `count` FROM `profile` WHERE `updated` < '%s'",
2014-07-11 00:30:36 +02:00
dbesc(date('Y-m-d H:i:s', time()-$a->config['maintenance']['min_scrape_delay'])));
$maintenance_backlog = 'unknown';
if(count($res)){ $maintenance_backlog = $res[0]['count'].' entries'; }
//Get the pulling backlog size.
$res = q("SELECT count(*) as `count` FROM `sync-pull-queue`");
$pulling_backlog = 'unknown';
if(count($res)){ $pulling_backlog = $res[0]['count'].' entries'; }
2014-07-11 00:30:36 +02:00
$tpl = file_get_contents('view/admin.tpl');
return replace_macros($tpl, array(
'$present' => is_file('.htimport') ? ' (present)' : '',
'$flagged' => $flagged,
'$maintenance_backlog' => $maintenance_backlog,
'$pulling_backlog' => $pulling_backlog,
2014-07-11 00:30:36 +02:00
'$maintenance_size' => $a->config['maintenance']['max_scrapes'].' items per maintenance call.'
));
}
2012-05-16 07:31:36 +02:00
2014-07-11 00:30:36 +02:00
function admin_post(&$a)
{
//Submit a profile URL.
if($_POST['submit_url']){
goaway($a->get_baseurl().'/submit?url='.bin2hex($_POST['submit_url']));
}
//Get our input.
$url = $_POST['dir_import_url'];
$page = intval($_POST['dir_page']);
$batch = $_POST['batch_submit'];
//Directory
$file = realpath(__DIR__.'/..').'/.htimport';
//Per batch setting.
$perPage = 200;
$perBatch = 2;
if($batch){
require_once('include/submit.php');
2014-07-11 01:35:30 +02:00
require_once('include/site-health.php');
2014-07-11 00:30:36 +02:00
//First get all data from file.
$data = file_get_contents($file);
$list = explode("\r\n", $data);
//Fresh batch?
if(!isset($_SESSION['import_progress'])){
$_SESSION['import_progress'] = true;
$_SESSION['import_success'] = 0;
$_SESSION['import_failed'] = 0;
$_SESSION['import_total'] = 0;
notice("Started new batch. ");
}
//Make sure we can use try catch for all sorts of errors.
set_error_handler(function($errno, $errstr='', $errfile='', $errline='', $context=array()){
if((error_reporting() & $errno) == 0){ return; }
throw new \Exception($errstr, $errno);
});
for($i=0; $i<$perBatch; $i++){
if($url = array_shift($list)){
set_time_limit(15);
$_SESSION['import_total']++;
$_SESSION['import_failed']++;
try{
2014-07-11 01:35:30 +02:00
//A site may well turn 'sour' during the import.
//Check the health again for this reason.
$site = parse_site_from_url($url);
$r = q("SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", $site);
if(count($r) && intval($r[0]['health_score']) < $a->config['site-health']['skip_import_threshold']){
continue;
}
//Do the submit if health is ok.
2014-07-11 00:30:36 +02:00
if(run_submit($url)){
$_SESSION['import_failed']--;
$_SESSION['import_success']++;
}
2014-07-11 01:35:30 +02:00
2014-07-11 00:30:36 +02:00
}catch(\Exception $ex){/* We tried... */}
}
else break;
}
$left = count($list);
2014-07-11 01:35:30 +02:00
$success = $_SESSION['import_success'];
$skipped = $_SESSION['import_skipped'];
2014-07-11 00:30:36 +02:00
$total = $_SESSION['import_total'];
$errors = $_SESSION['import_failed'];
if($left > 0){
2014-07-11 01:35:30 +02:00
notice("$left items left in batch...<br>$success updated profiles.<br>$errors import errors.");
2014-07-11 00:30:36 +02:00
file_put_contents($file, implode("\r\n", $list));
$fid = uniqid('autosubmit_');
echo '<form method="POST" id="'.$fid.'"><input type="hidden" name="batch_submit" value="1"></form>'.
2014-07-11 01:35:30 +02:00
'<script type="text/javascript">setTimeout(function(){ document.getElementById("'.$fid.'").submit(); }, 300);</script>';
2014-07-11 00:30:36 +02:00
} else {
2014-07-11 01:35:30 +02:00
notice("Completed batch! $success updated. $errors errors.");
2014-07-11 00:30:36 +02:00
unlink($file);
unset($_SESSION['import_progress']);
}
return;
}
2014-07-11 01:35:30 +02:00
//Doing a poll from the directory?
2014-07-11 00:30:36 +02:00
elseif($url){
2014-07-11 01:35:30 +02:00
require_once('include/site-health.php');
2014-07-11 00:30:36 +02:00
$result = fetch_url($url."/lsearch?p=$page&n=$perPage&search=.*");
if($result)
$data = json_decode($result);
else
$data = false;
if($data){
$rows = '';
foreach($data->results as $profile){
2014-07-11 01:35:30 +02:00
//Skip known profiles.
$purl = $profile->url;
$nurl = str_replace(array('https:','//www.'), array('http:','//'), $purl);
$r = q("SELECT count(*) as `matched` FROM `profile` WHERE (`homepage` = '%s' OR `nurl` = '%s') LIMIT 1",
dbesc($purl),
dbesc($nurl)
);
if(count($r) && $r[0]['matched']){
continue;
}
//Find out site health.
else{
$site = parse_site_from_url($purl);
$r = q("SELECT * FROM `site-health` WHERE `base_url`= '%s' ORDER BY `id` ASC LIMIT 1", $site);
if(count($r) && intval($r[0]['health_score']) < $a->config['site-health']['skip_import_threshold']){
continue;
}
}
2014-07-11 00:30:36 +02:00
$rows .= $profile->url."\r\n";
2014-07-11 01:35:30 +02:00
2014-07-11 00:30:36 +02:00
}
2014-07-11 01:35:30 +02:00
2014-07-11 00:30:36 +02:00
file_put_contents($file, $rows, $page > 0 ? FILE_APPEND : 0);
$progress = min((($page+1) * $perPage), $data->total);
notice("Imported ".$progress."/".$data->total." URLs.");
if($progress !== $data->total){
$fid = uniqid('autosubmit_');
echo
'<form method="POST" id="'.$fid.'">'.
'<input type="hidden" name="dir_import_url" value="'.$url.'">'.
'<input type="hidden" name="dir_page" value="'.($page+1).'">'.
'</form>'.
'<script type="text/javascript">setTimeout(function(){ document.getElementById("'.$fid.'").submit(); }, 500);</script>';
} else {
2014-07-11 01:35:30 +02:00
goaway($a->get_baseurl().'/admin');
2014-07-11 00:30:36 +02:00
}
}
}
2012-05-16 07:31:36 +02:00
}