This Help forum is for issues relates to the latest release (1.7.2). For issues related to beta or github version please use their own Help forum.
Before asking for help please read "How to request help" by clicking on that tab above here."
  • Page:
  • 1

TOPIC:

Accessing webtrees with cron job 5 years 5 months ago #1

  • tc_discoz
  • tc_discoz's Avatar Topic Author
  • Visitor
  • Visitor
Dear all,

I am developing a feature where I want to send out a mail in respect to a dedicated event in webtrees.
For testing purposes I created a module which is returning the text-elements I need for the mail.
But this is only working when I login to webtrees via browser and manually call that module.

What I am looking for is to access webtrees not via webbrowser but to do some kind of API access via a cron-job.
Can someone please guide me how to setup a basic webtree-object that represents a valid webtree-session and where I can attach my code to ?

Thanks in advance,

TC

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 5 months ago #2

  • ddrury
  • ddrury's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 310
Something like this?

the line path = <path to webtrees root folder> goes in yourmodule_cli.ini

This goes in yourmodule_cli.php
use Fisharebest\webtrees\I18N;
use Fisharebest\webtrees\Mail;
use Fisharebest\webtrees\Module;
use Fisharebest\webtrees\User;
use Zend_Session;
define('WT_SCRIPT_NAME', basename(__FILE__));
define('MODULE_NAME', basename(__FILE__, '.php'));
// Ensure a few key variables are set
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
	$_SERVER['HTTP_USER_AGENT'] = PHP_SAPI;
}
if (!isset($_SERVER['REMOTE_ADDR'])) {
	$_SERVER['REMOTE_ADDR'] = gethostbyname(gethostname());
}
if (!isset($_SERVER['SERVER_PROTOCOL'])) {
	$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
}
if (!isset($_SERVER['SERVER_NAME'])) {
	$_SERVER['SERVER_NAME'] = PHP_SAPI;
}
$ini  = parse_ini_file(MODULE_NAME . '.ini');
$path = rtrim($ini['path'], '\/') . '/';
// switch to webtrees root directory ...
chdir($path);
// ... and initialise a lot of webtrees stuff
require './includes/session.php';
Zend_Session::writeClose();
$mod = Module::getModuleByName(MODULE_NAME);
if (!$mod) {
	Mail::systemMessage($WT_TREE, User::findByIdentifier(PHP_SAPI), I18N::translate('Aborting'), I18N::translate('The %s module is not available', MODULE_NAME));
	exit(1);
}
yourstuff goes here

BTW you also have to add a site access rule
--
Dave

Local: Win 10 Pro/WSL Apache 2.4.41, PHP 7.4.16/8.0.3, MariaDB 10.4.10
Production: Apache 2.2, PHP 7.3.27, 10.3.18-MariaDB

Please Log in or Create an account to join the conversation.

Last edit: by ddrury.

Accessing webtrees with cron job 5 years 5 months ago #3

  • fisharebest
  • fisharebest's Avatar
  • Offline
  • Administrator
  • Administrator
  • Posts: 14515
If I understand you correctly, your cron-job is not logged in, and hence cannot access details of living individuals.

I guess this shows the privacy/access functions are working ;-)

I haven't tried this, but you might be able to "fake" a login by adding something like this near the start of your cron script.

$user = User::findByIdentifier('username_or_email_of_account_with_suitable_access');
Auth::login($user);
Greg Roach - This email address is being protected from spambots. You need JavaScript enabled to view it. - fisharebest.webtrees.net

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 5 months ago #4

  • ddrury
  • ddrury's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 310
Yeah,

Should also have said, I created a dummy user with minimum privileges and it's email address is used for any messages the module generates
--
Dave

Local: Win 10 Pro/WSL Apache 2.4.41, PHP 7.4.16/8.0.3, MariaDB 10.4.10
Production: Apache 2.2, PHP 7.3.27, 10.3.18-MariaDB

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 5 months ago #5

  • tc_discoz
  • tc_discoz's Avatar Topic Author
  • Visitor
  • Visitor
Should I put the lines ...

$user = User::findByIdentifier('username_or_email_of_account_with_suitable_access');
Auth::login($user);

...

as beginning for the part "yourstuff goes here" ?
Somewhere I have to give the script the user that it should run with.

Thanks,

TC

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 5 months ago #6

  • ddrury
  • ddrury's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 310
I'd suggest try putting them after the Zend_Session::writeClose(); line

You'll possibly also need to modify the line that sends a system message and as I said earlier add a site access rule that allows the php_sapi in - I missed this originally and spent ages trying to find out why things didn't work.
--
Dave

Local: Win 10 Pro/WSL Apache 2.4.41, PHP 7.4.16/8.0.3, MariaDB 10.4.10
Production: Apache 2.2, PHP 7.3.27, 10.3.18-MariaDB

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 4 months ago #7

  • tc_discoz
  • tc_discoz's Avatar Topic Author
  • Visitor
  • Visitor
Dave, I somehow seem to be stuck with the site-access topic.
PHP_SAPI seems to be treated as web spider.
How did you alter this behavior ?

Thanks !
TC

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 4 months ago #8

  • ddrury
  • ddrury's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 310
Good fun isn't it?

See php.net/manual/en/reserved.constants.php...erved.constants.core

In my case PHP_SAPI returns "cgi-fcgi" so after running the routine for the first time (when of course it will fail) go to "Control Panel - website - website access rule" and look for the rule where the user agent string is (in my case) "cgi-fcgi" then edit that rule and change the rule to allow
--
Dave

Local: Win 10 Pro/WSL Apache 2.4.41, PHP 7.4.16/8.0.3, MariaDB 10.4.10
Production: Apache 2.2, PHP 7.3.27, 10.3.18-MariaDB

Please Log in or Create an account to join the conversation.

Do you need a web hosting solution for your webtrees site?
If you prefer a host that specialises in webtrees, the following page lists some suppliers able to provide one for you: 

Accessing webtrees with cron job 5 years 4 months ago #9

  • ddrury
  • ddrury's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 310
I've just been updating my system and have noticed that at some point in the past Greg changed from Zend::Session to a homegrown class therefore the lines

use Zend_Session;

and

Zend_Session::writeClose();


should be removed from the example above
--
Dave

Local: Win 10 Pro/WSL Apache 2.4.41, PHP 7.4.16/8.0.3, MariaDB 10.4.10
Production: Apache 2.2, PHP 7.3.27, 10.3.18-MariaDB

Please Log in or Create an account to join the conversation.

Accessing webtrees with cron job 5 years 4 months ago #10

  • fisharebest
  • fisharebest's Avatar
  • Offline
  • Administrator
  • Administrator
  • Posts: 14515
> changed from Zend::Session to a homegrown class

I'm (slowly) removing the Zend library. There are better options available. The only remaining part of it is the mail library.

I've been looking for a replacement, but all the nice ones require PHP 5.5 or functions that are likely to be unavailable in many budget webhosting packages...
Greg Roach - This email address is being protected from spambots. You need JavaScript enabled to view it. - fisharebest.webtrees.net

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Powered by Kunena Forum