G&G BRANCH ATTENDANCE RELAY v1.0.4
======================================

TARGET
------
Relay domain: https://emailsservices.in
Relay folder: public_html/attendance-relay
Relay URL:    https://emailsservices.in/attendance-relay
Main CRM:     https://ggdigitalsolution.com

WHAT THIS DOES
--------------
Branch connector -> emailsservices.in relay -> controlled queue -> main CRM

The existing Windows Connector EXE does NOT need to be rebuilt.
Only change its Server URL to:
https://emailsservices.in/attendance-relay

Keep the existing connector token unchanged.

INSTALL
-------
1. In cPanel for emailsservices.in, open File Manager.
2. Go to public_html.
3. Upload the folder "attendance-relay" from this package.
   Final path should look like:
   public_html/attendance-relay/index.php
4. Confirm PHP 7.4+ and OpenSSL are enabled. PHP 8.1+ is still recommended.
5. Open:
   https://emailsservices.in/attendance-relay
   You should see JSON with "G&G Branch Attendance Relay".
6. The "storage" folder must be writable by PHP. Normal cPanel permissions 0755
   usually work. If status reports unwritable, use 0770 or 0775.
7. Replace your Perfex module with the supplied branch_attendance v1.0.27 package.
8. In each branch Windows connector:
   Server URL = https://emailsservices.in/attendance-relay
   Token      = keep the current token
9. Test one branch first, then change the rest.

CPANEL CRON JOB
---------------
Run every minute. Recommended command:

curl -fsS "https://emailsservices.in/attendance-relay/relay/flush?key=4abc39d116d8d4634938ffab54a0a7d60b5b63f966111011&limit=50" >/dev/null 2>&1

This drains queued events/employees/completion messages to the main CRM in a
controlled order. If one branch has a temporary error, other branches continue.

STATUS PAGE
-----------
Private status URL:

https://emailsservices.in/attendance-relay/relay/status?key=175139520c9854aff98180a7dc325dbe94b5815715f600ee

Keep this URL/key private.

SECURITY KEYS
-------------
Relay shared secret (already matched in the supplied Perfex module):
83126918e9c18e27ef7b41f9bf1ba317066320355ca130b1be7316a6a2f97cd2

Cron key:
4abc39d116d8d4634938ffab54a0a7d60b5b63f966111011

Status key:
175139520c9854aff98180a7dc325dbe94b5815715f600ee

Encryption key:
Stored only in config.php. Do not move it to public documentation.

FALLBACK
--------
The relay normally receives Fetch jobs pushed directly by Perfex. If that push
fails, it checks the main CRM at most once every 120 seconds per connector.
The existing connector may still poll the relay every ~5 seconds, but those idle
requests no longer reach your main CRM.

ROLLBACK
--------
If you ever need to bypass the relay, change a branch connector Server URL back to:
https://ggdigitalsolution.com

No connector token change is required.

IMPORTANT
---------
Keep config.php private. The included .htaccess blocks direct web access to
config.php, lib.php, cron.php and the storage folder. Do not remove those rules.


V1.0.2 CPANEL DIAGNOSTIC / PERMISSION FIX
-------------------------------
- Removed the PHP 8-only str_starts_with() dependency so PHP 7.4 can run the relay.
- Removed Options -Indexes from .htaccess because some cPanel/LiteSpeed setups reject
  that directive and return HTTP 500 before PHP starts.
- Added a JSON fatal-error wrapper so runtime failures are easier to diagnose.

If the old v1.0.0 gave HTTP 500 immediately after changing the Windows connector URL,
replace the relay folder with this v1.0.2 package. No Windows connector or Perfex token
change is required.


V1.0.2 NOTES
------------
- Performs a real storage write test during startup.
- Tries chmod 0775 on existing relay storage directories when needed.
- Returns a specific storage/OpenSSL error instead of the generic runtime message.
- Adds diagnostic.php. Open:
  https://emailsservices.in/attendance-relay/diagnostic.php
  It exposes no relay secrets. Remove diagnostic.php after setup if desired.
- Uses CURLINFO_HTTP_CODE for wider PHP/cURL compatibility.


V1.0.4 AUTO-DRAIN FIX
----------------------
- Fixes the case where the Windows connector says the job completed to the relay
  but Perfex still shows the sync job as Pending because cPanel cron was not running.
- Any normal connector poll now checks the local relay queue first.
- If queue files exist, up to 25 are forwarded to the main CRM under a global lock.
- If the queue is empty, connector polling does NOT make an extra CRM request.
- cPanel cron remains recommended as a backup, especially if all branch PCs are off.
- Existing queued events/employees/complete messages are preserved and will be
  delivered after installing this version.
