dieYoung("Error: «{$argv[$i]}» is not a known option; use «-h» or «--help» to display help.\n",1);
}
} elseif (is_null($confFP)) {
$confFP=$argv[$i];
} else {
dieYoung("Error: could not interpret «{$argv[$i]}» (configuration file has already been set to «{$confFP}»); use «-h» or «--help» to display help.\n",1);
if ($res['content']===false) dieYoung("Error: could not connect to «{$url}».\n",1);
if ($res['httpcode']!='200') dieYoung("Error: got http code «{$res['httpcode']}» from «{$url}».\n",1);
$res=explode("\r\n",$res['content']);
// alb|sqi|sq|Albanian|albanais
// tup|||Tupi languages|tupi, langues
foreach ($res as $val)
if (preg_match('#^[a-z]{3}\|([a-z]{3})?\|([a-z]{2})\|.+\|.+$#',$val,$matches)===1)
$langCodes[]=$matches[2];
$count=count($langCodes);
if (@file_put_contents($langsFP,implode("\n",$langCodes)."\n")===false) dieYoung("Error: could not save the {$count} ISO 639-1 language code(s) i got from «{$url}» into «{$langsFP}».\n",1);
echo "Info: successfully saved the {$count} ISO 639-1 language code(s) i got from «{$url}» into «{$langsFP}».\n";
exit(0);
}
if (($langs=@file($langsFP,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES))===false) dieYoung("Error: could not load ISO 639-1 language codes from «{$langsFP}».\n",1);
if (is_null($confFP)) dieYoung("Error: you have not specified a configuration file; use «-h» or «--help» to display help.\n",1);
echo "Info: trying to load configuration file «{$confFP}» from directory «".getcwd()."».\n";
if (!file_exists($confFP)) dieYoung("Error: «{$confFP}» does not exist.\n",1);
if (!is_file($confFP)) dieYoung("Error: «{$confFP}» is not a file.\n",1);
if (!is_readable($confFP)) dieYoung("Error: «{$confFP}» is not readable.\n",1);
getConf($conf,$confFP);
if (preg_match('#^/.*$#',$conf['state_file_absolute_path'])!==1) dieYoung("Error: in configuration file: «state_file_absolute_path» must be an absolute path.\n",1);
if (!in_array($conf['posts_language'],$langs)) dieYoung("Error: in configuration file: «posts_language»: «{$conf['posts_language']}» is not a known language code.\n",1);
if (!in_array($conf['posts_visibility'],['public', 'unlisted', 'private', 'direct'])) dieYoung("Error: in configuration file: «posts_visibility» must be one of «public», «unlisted», «private» or «direct».\n",1);
if (!is_null($conf['max_post_length'])) {
if (preg_match('#^\d+$#',$conf['max_post_length'])!==1 || $conf['max_post_length']+0<10) dieYoung("Error: configuration file: «max_post_length» must be an integer greater than or equal to 10.\n",1);
$conf['max_post_length']+=0;
echo "Info: got «{$conf['max_post_length']}» as «max_post_length» from configuration file.\n";
if ($res['httpcode']!='200') dieYoung("Error: got http code «{$res['httpcode']}»{$buff} from «{$url}».\n",1);
if (!isset($res['content']['configuration']['media_attachments']['image_size_limit'])) dieYoung("Error: JSON from «{$url}» doesn’t declare «image_size_limit».\n",1);
if (!is_int($res['content']['configuration']['media_attachments']['image_size_limit'])) dieYoung("Error: JSON from «{$url}» declares «image_size_limit» with an unexpected format.\n",1);
echo "Info: got «{$conf['max_image_size']}» as «max_image_size» from «{$url}».\n";
if (!isset($res['content']['configuration']['statuses']['max_characters'])) dieYoung("Error: JSON from «{$url}» doesn’t declare «max_characters».\n",1);
if (!is_int($res['content']['configuration']['statuses']['max_characters'])) dieYoung("Error: JSON from «{$url}» declares «max_characters» with an unexpected format.\n",1);
if ($feed===false) dieYoung("Error: got no valid XML from «{$conf['feed_url']}».\n",1);
//print_r($feed);
if (!isset($feed->channel->item) || !is_iterable($feed->channel->item) || !is_countable($feed->channel->item)) dieYoung("Error: feed from «{$conf['feed_url']}» had unexpected format.\n",1);
$itemsCount=$feed->channel->item->count();
if ($itemsCount==0) exitYoung("Info: feed from «{$conf['feed_url']}» was empty, bye.\n");
echo "Info: got good feed from «{$conf['feed_url']}».\n";
$guids=[];
echo "Info: trying to load GUIDs of already posted events from state file «{$conf['state_file_absolute_path']}».\n";
if (!file_exists($conf['state_file_absolute_path']) && is_null($opts['do-post'])) dieYoung("Error: state file «{$conf['state_file_absolute_path']}» does not exist: this seems to be a first run on feed «{$conf['feed_url']}», so you should explicitly declare whether you want {$SNAME} to post all of its events or not, setting option «-p» or «--do-post» to «y» («yes») or «n» («no»); use «-h» or «--help» to display help.\n",1);
if (is_null($opts['do-post'])) $opts['do-post']=true;
if (file_exists($conf['state_file_absolute_path'])) {
if (!is_file($conf['state_file_absolute_path'])) dieYoung("Error: «{$conf['state_file_absolute_path']}» exists but it’s not a file.\n",1);
if (!is_readable($conf['state_file_absolute_path'])) dieYoung("Error: «{$conf['state_file_absolute_path']}» exists but it’s not readable.\n",1);
if (preg_match('#^(\d+)\t(\S+)$#',$val,$matches)===1) {
if ($matches[1]+0>=$graceLine) {
fwrite($fh,"{$matches[1]}\t{$matches[2]}\n");
$guids[$matches[2]]=$matches[1];
} else {
$i++;
}
} else {
fwrite(STDERR,"Warning: in state file «{$conf['state_file_absolute_path']}», line ".($key+1)." had unexpected format.\n");
}
}
fclose($fh);
echo 'Info: got '.count($guids)." GUID(s) for already posted event(s) from state file «{$conf['state_file_absolute_path']}»; removed {$i} line(s) older than one year.\n";
}
if (($fh=@fopen($conf['state_file_absolute_path'],'a'))===false) dieYoung("Error: could not open «{$conf['state_file_absolute_path']}» in «append» mode.\n",1);
$newItemsCount=0;
$goodPostsCount=0;
$index=0;
foreach ($feed->channel->item as $item) {
$index++;
// print_r($item);
if (!isset($item->guid) || ($guid=$item->guid->__toString())=='') {
fwrite(STDERR,"Warning: event #{$index} has no GUID, skipping.\n");
} elseif (!array_key_exists($guid,$guids)) {
$newItemsCount++;
$file=null;
if (isset($item->enclosure[0]['url']) && isset($item->enclosure[0]['type']) && isset($item->enclosure[0]['length']))
fwrite(STDERR,"Warning: ignoring attachment of event «{$guid}» because its size is greater than image upload max size on «{$conf['fedi_hostname']}».\n");
} else {
$res=curl($file['url']);
if ($res['content']===false) {
fwrite(STDERR,"Warning: could not connect to «{$file['url']}» (error: «{$res['error']}»); won’t post file as attachment.\n");
} elseif ($res['httpcode']!='200') {
fwrite(STDERR,"Warning: «{$file['url']}» returned http code «{$res['httpcode']}»; won’t post file as attachment.\n");