Commit graph

9 commits

Author SHA1 Message Date
MDW
f551727df9
Qual/Sec: Use db->sanitize instead of db->escape when not quoted, fix some quoting (#39286)
* FIX phpstan: Societe::$client (int<0,3>) does not accept -1 (#39244)

Societe::$client is annotated @var int<0,3> (domain 0=no customer,
1=customer, 2=prospect, 3=both) in societe.class.php, but the create
form in societe/card.php pre-set it to -1 as a transient "nothing
pre-selected" placeholder, tripping PHPStan level 10 on develop.

The value is never persisted (save paths compute 0..3) and the only
reader, the switch() building the form, treats -1 and 0 identically via
its default case. Use 0 (the property's own default) so the assignment
matches the declared type. No behavior change.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>

* Qual: Fix entity handling in bonprelevement.class.php

The variable name 'entities' was changed to 'entity' to better reflect the singular nature of the value being retrieved.
It was also casted to an int instead of using an unquoted db->escape.

* Qual: Improve SQL query construction in company.lib.php

By escaping the concatenated string, injection verification does not identify a false positive.

* Qual: Avoid void positive, sanitize instead of escape

* Qual: Avoid false positive, sanitize instead of escape

* Sec: Fix potential injection with cast to int (timespent_duration)

* Qual: Avoid false positive, sanitize instead of escape

* Qual: Avoid false positive, sanitize instead of escape

* fix: Replace escape() with sanitize() in SQL queries

# FIX: Replace escape() with sanitize() in SQL queries

- Replace escape() with sanitize() in SQL queries to ensure proper SQL injection protection
- Update SQL queries in multiple files to use sanitize() instead of escape()
- Ensure consistent use of sanitize() across the codebase for better security

* fix(sql): Replace escape with sanitize in SQL queries

# FIX: Replace escape with sanitize in SQL queries

- Replace `escape` with `sanitize` in SQL queries to ensure proper SQL injection prevention

* Qual: Improve field escaping/quoting

# Qual: Qual: Improve field escaping/quoting

---------

Co-authored-by: Jam Balaya <jambalaya.pyoncafe@outlook.jp>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-27 13:00:33 +02:00
Pichinov-Jose
1aeec05d80
Fix: MCP server propagates authenticated service user to tools (#38294)
The MCP server (htdocs/ai/server/mcp_server.php) loads a service user
from AI_MCP_USER_ID but does not expose it as the PHP global $user.
Several tool classes call 'global $user' to identify the caller --
they therefore see no authenticated user in HTTP MCP context (no PHP
web session exists) and return 'User not authenticated' even though
authentication via X-API-Key/Bearer has already succeeded.

This breaks roughly 40% of the MCP tools shipped by the module when
called from external clients (Claude Desktop Custom Connectors,
Claude Code CLI, MCP Inspector, custom scripts).

Two complementary fixes applied:

1. mcp_server.php: after successfully loading the service user,
   assign it to $GLOBALS['user'] and the local $user variable. This
   acts as a safety net for any tool (current or third-party) that
   relies on the legacy 'global $user' pattern.

2. Three tool classes harmonize their constructor signatures with
   the McpHandler contract -- new $className($db, $user, $conf):
   - ToolCrudObjects (htdocs/ai/tools/crud_objects.class.php)
   - ToolInvoices (htdocs/ai/tools/invoices.class.php)
   - ToolReports (htdocs/ai/tools/reports.class.php)

   These now accept $user via constructor (matching the pattern
   already used in ToolThirdParty, ToolCategories, ToolProducts)
   and store it as $this->user. Methods that previously called
   'global $user' (execute, validateInvoice, payInvoice) now use
   the injected $this->user, with a fallback to the global for
   web-session contexts.

Tested with:
- create_other_document (proposal, supplier_invoice): works
- search_invoice, validate_invoice, pay_invoice: works
- get_sales_report, get_purchase_report: works
- AI Assistant web UI: unchanged, no regression
2026-07-13 06:57:42 +02:00
Laurent Destailleur
85c4766b80 Debug v24 2026-06-10 23:31:55 +02:00
MDW
f55e7998cc
Qual: Fix Phan notices, set PHP min to 7.2 to avoid some (#38400)
* Qual: Fix Phan notices, set PHP min to 7.2 to avoid some

* Qual: Fix Phan and PHPStan notices

* Update apstats.php

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
2026-05-23 04:09:56 +02:00
Laurent Destailleur
3e3ca74a67 Debug AI assistant and MCP server 2026-05-20 01:27:19 +02:00
Pichinov-Jose
cdaff51147
Fix: FactureFournisseur::addline parameter order in ToolCrudObjects (#38295)
Unlike Facture::addline() which takes ($desc, $pu, $qty, $txtva, ...),
FactureFournisseur::addline() uses a different parameter order:

  ($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty,
   $fk_product, $remise_percent, ...)

i.e. $qty is in position 6, not 3.

ToolCrudObjects::processAddLine() was calling
FactureFournisseur::addline() with the Facture order, which produced:

- our $qty (1) passed as $txtva  -> effective VAT rate 1%
- our $vat (20) passed as $txlocaltax1 -> wrong local tax
- a hardcoded 0 passed as $qty -> the line was inserted with
  qty=0 and silently dropped from the visible totals

The result: every supplier invoice created via create_other_document
ended up with the header correctly created but a 0.00 EUR total,
with no error returned (lines_added was reported as success).

This patch reorders the parameters to match the actual
FactureFournisseur::addline() signature and adds an inline comment
to warn future maintainers about this Dolibarr API quirk.

Other addline calls in the same switch (invoice/order/proposal,
supplier_order/supplier_proposal) use their respective correct
signatures already and are unaffected.

Tested with: create_other_document object_type=supplier_invoice
with one line (qty=1, unit_price=10, vat_rate=20) -> line now
appears with Total HT 10.00 EUR.
2026-05-18 01:16:32 +02:00
Pichinov-Jose
0e591afe34
Fix: allow free-text invoice/order/proposal lines when no product matches (#38298)
ToolCrudObjects::processAddLine() uses the line's 'description' as a
fallback product identifier when 'product' is not explicitly provided.
It then calls findProduct() to look up an existing Dolibarr product
by ref/barcode/label.

When no product matches (the common case for AI-generated lines that
describe a one-off service or a custom item), findProduct() returns
an error array and the function aborts WITHOUT ever calling addline().
The resulting document gets its header created but ends up with zero
lines and a 0.00 total.

This is contrary to standard Dolibarr behavior: free-text line items
(no product link, just a description and a price) are a first-class
feature of invoices, orders, and proposals.

This patch distinguishes between the two cases:

- Caller explicitly passed 'product': they wanted a specific product
  -> still abort with the original error if not found (unchanged).

- Caller only passed 'description': it's a free-text line ->
  silently fall through with $prod = null. Dolibarr then creates a
  free-text line with the user-provided description, qty, unit_price
  and vat_rate.

Tested with: create_other_document object_type=proposal containing
11 line items, none of which match existing product references ->
all 11 lines are now created with their correct totals (previously
the document was created with 0 lines and a 0.00 total).
2026-05-18 01:16:01 +02:00
Pichinov-Jose
dbae20fb5e
Fix: ToolCrudObjects header inputSchema JSON Schema compliance (#38299)
create_customer_invoice and create_other_document declare a 'header'
object whose schema has 'required: [socid]' but no 'properties'
entry. This is technically allowed by JSON Schema but stricter
validators flag it as suspicious: the 'required' array references a
property that is not declared in 'properties'.

Beyond compliance, the omission means LLMs (and human reviewers) have
no machine-readable description of which fields are actually accepted
in the 'header' object. The Dolibarr backend already accepts socid,
date, duree_validite, note_public, note_private and others -- this
patch documents the most useful ones explicitly so consumer tools
can produce richer payloads.

Tested with: create_other_document object_type=proposal accepting
a 'header' that contains all of socid + date + duree_validite +
note_public -> all four fields are correctly mapped to the Propal
object (Dolibarr already accepts unknown properties via dynamic
property assignment, so this is purely a schema-documentation change).
2026-05-18 01:14:03 +02:00
sonikf
faa8005eb4
NEW: MCP server and AI Assistant (#37888)
* fix list query

* Update other.lang

* remove duplicate

* initial commit

* AI Assistant initial commit

* Rename htdocs/ai/index.php to htdocs/ai/assistant/index.php

* AI Assistant initial commit

* Initial commit

* Rename htdocs/ai/ai_assistant.css to htdocs/ai/css/ai_assistant.css

* Initial commit

* Rename htdocs/ai/ai_assistant.js to htdocs/ai/js/ai_assistant.js

* Initial commit

* Rename htdocs/ai/mcp_server.php to htdocs/ai/server/mcp_server.php

* Initial commit

* category tools

* Rename htdocs/ai/categories.php to htdocs/ai/tools/categories.php

* Global tools to restrict AI

* Tools for crud actions on Dolibarr objects

* Tools for products and services.

* Tools to generate reports

* Missing PHPDoc

* Tools for thirdparties and contacts

* Tool for generating navigation URLs in Dolibarr

* Tools related to invoices and payments

* Update ai_assistant.js

* Initial commit

* Rename htdocs/ai/purge_logs.php to htdocs/ai/scripts/purge_logs.php

* fix pre commit

* re fix pre-commit

* fix pre-commit

* Rename categories.php to categories.class.php

* Rename conversation.php to conversation.class.php

* Rename crud_objects.php to crud_objects.class.php

* Rename invoices.php to invoices.class.php

* Rename navigation.php to navigation.class.php

* Rename products.php to products.class.php

* Rename reports.php to reports.class.php

* Rename thirdparty.php to thirdparty.class.php

* fix pre-commit

* Update parse_intent.php

* fix pre commit

* fix phpdoc

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* fix pre-commit

* add missing phpdoc

* fix pre-commit

* fix phpdoc

* fix phpdoc

* fix pre-commit

* fix pre-commit

* fix phpdoc

* fix pre-commit

* fix pre-commit

* fix phpdoc

* fix phpdoc

* fix phpdoc

* fix phpdoc

* fix php-stan

* fix php-stan

* fix phan

* fix phan

* fix phan

* security check

* security check

* security check

* security check

* enhance security check

* enhance security check

* fix pre-commit

* Update log_viewer.php

* Improve access control logic in log_viewer.php

Refactor access control checks for admin and module activation.

* fix phan

* fix phan

* fix phan

* fix phan

* fix phan

* try fix phan

* fix pre-commit

* fix phan

* try fix phan

* fix phan

* fix php-stan

* fix phan php-stan

* fix pre-commit

* fix phan php-stan

* fix pre-commit

* fix phan php-stan

* fix phan php-stan

* fix phn php-stan

* fix phan php-stan

* fix phan php-stan

* fix phan php-stan

* fix phan php-stan

* fix pre-commit

* fix phan php-stan

* fix php-stan

* Fix MCP tool registration for .class.php file naming

* fix typo

* fix ci

* fix pre-commit

* fix ci

* fix pre-commit

* Update categories.class.php

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* add missing user in constructor

* add missing user in constructor

* improve invoice validation error handling

* add missing user in constructor

* Rename mcp_protocol.php to mcp_protocol.class.php

* Update mcp_server.php

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
2026-05-11 01:59:55 +02:00