Commit graph

5 commits

Author SHA1 Message Date
MDW
b51b38ef64
Qual: Fix 'SqlInjection' notices (#39223)
# Qual: Fix 'SqlInjection' notices

Batch of 25 files with changes to fix 'SqlInjection' Notices
2026-07-20 02:51:42 +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
0fc4a4abe4 Move error string 2026-05-19 14:08:05 +02:00
Pichinov-Jose
325000c398
Fix: get_sales_report respects the group_by parameter (#38303)
The get_sales_report tool declares a group_by parameter with enum
values [thirdparty, product, month] in its inputSchema, but the
implementation ignored $args['group_by'] entirely and always
returned a flat list of invoices.

Meanwhile getPurchaseReport() in the same class already implemented
the correct grouped pattern (s.nom or DATE_FORMAT date) -- this
patch ports that pattern to getSalesReport(), with three modes:

- With a socid: detailed invoice list for that customer (unchanged
  behavior, kept for backward compatibility).

- Without socid, group_by=thirdparty (default): aggregate by
  customer with COUNT and SUM(total_ttc) -> Top-customers report.

- Without socid, group_by=month: aggregate by DATE_FORMAT(datef,
  '%Y-%m') -> monthly revenue report.

- Without socid, group_by=product: aggregate by product reference
  via INNER JOIN on facturedet, with COALESCE(p.ref, fd.description)
  so free-text lines without a product link still appear under
  their description. SUM(fd.total_ttc) gives per-product revenue,
  COUNT(DISTINCT f.rowid) gives the number of invoices that
  contained the product.

Tested:
- group_by=thirdparty: top customers correctly ranked by total.
- group_by=month: 2026-04 and 2026-05 buckets correctly populated.
- group_by=product: top product (SAM-BIK3-CAS-1430-24-N) correctly
  ranked at 1 276 EUR over 5 invoices.
- With socid: unchanged detailed listing.

Note: the 'product' grouping is also missing from getPurchaseReport()
in the current code. That can be addressed in a follow-up patch on
the same pattern.
2026-05-18 00:04:14 +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