diff --git a/.env.dist b/.env.dist new file mode 100644 index 00000000..03df0e91 --- /dev/null +++ b/.env.dist @@ -0,0 +1,96 @@ +# This file is a "template" of what your .env file should look like. +# Set variables here that may be different on each deployment target of the app, +# e.g. development, staging, production. +# +# On `composer install`/`composer update`, this file is copied to `.env` (unless +# it already exists) and PHPLIST_SECRET is replaced with a freshly generated value. +# +# https://symfony.com/doc/current/configuration.html#configuring-environment-variables-in-env-files + +PHPLIST_DATABASE_DRIVER=pdo_mysql +PHPLIST_DATABASE_PATH= +PHPLIST_DATABASE_HOST=127.0.0.1 +PHPLIST_DATABASE_PORT=3306 +PHPLIST_DATABASE_NAME=phplistdb +PHPLIST_DATABASE_USER=phplist +PHPLIST_DATABASE_PASSWORD=phplist +DATABASE_PREFIX=phplist_ +LIST_TABLE_PREFIX=listattr_ +PHPLIST_ADMIN_PASSWORD=admin + +APP_DEV_VERSION=0 +APP_DEV_EMAIL=dev@dev.com +APP_POWERED_BY_PHPLIST=0 +PREFERENCEPAGE_SHOW_PRIVATE_LISTS=0 + +API_BASE_URL=http://api.phplist.local/ +FRONT_END_BASE_URL=http://frontend.phplist.local + +PARALLER_USE_WITH_PHPLIST3=0 + +# Email configuration +MAILER_FROM=noreply@phplist.com +MAILER_DSN=null://null +CONFIRMATION_URL=http://api.phplist.local/api/v2/subscriber/confirm/ +SUBSCRIPTION_CONFIRMATION_URL=http://api.phplist.local/api/v2/subscription/confirm/ +PASSWORD_RESET_URL=https://example.com/reset/ +SHOW_UNSUBSCRIBELINK=1 + +# Bounce email settings +BOUNCE_EMAIL=bounce@phplist.com +BOUNCE_IMAP_PASS=bounce@phplist.com +BOUNCE_IMAP_HOST=imap.phplist.com +BOUNCE_IMAP_PORT=993 +BOUNCE_IMAP_ENCRYPTION=ssl +BOUNCE_IMAP_MAILBOX=/var/spool/mail/bounces +BOUNCE_IMAP_MAILBOX_NAME=INBOX,ONE_MORE +BOUNCE_IMAP_PROTOCOL=imap +BOUNCE_IMAP_UNSUBSCRIBE_THRESHOLD=5 +BOUNCE_IMAP_BLACKLIST_THRESHOLD=3 +BOUNCE_IMAP_PURGE=0 +BOUNCE_IMAP_PURGE_UNPROCESSED=0 + +# Messenger configuration for asynchronous processing +MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=true + +# A secret key that's used to generate certain security-related tokens +PHPLIST_SECRET=%s +VERIFY_SSL=1 + +APP_PHPLIST_ISP_CONF_PATH=/etc/phplist.conf + +# Message sending +MAILQUEUE_BATCH_SIZE=5 +MAILQUEUE_BATCH_PERIOD=5 +MAILQUEUE_THROTTLE=5 +MESSAGING_MAX_PROCESS_TIME=600 +MAX_MAILSIZE=209715200 +DEFAULT_MESSAGEAGE=691200 +USE_MANUAL_TEXT_PART=0 +MESSAGING_BLACKLIST_GRACE_TIME=600 +GOOGLE_SENDERID= +USE_AMAZONSES=0 +USE_PRECEDENCE_HEADER=0 +EMBEDEXTERNALIMAGES=0 +EMBEDUPLOADIMAGES=0 +EXTERNALIMAGE_MAXAGE=0 +EXTERNALIMAGE_TIMEOUT=30 +EXTERNALIMAGE_MAXSIZE=204800 +FORWARD_ALTERNATIVE_CONTENT=0 +EMAILTEXTCREDITS=0 +ALWAYS_ADD_USERTRACK=1 +SEND_LISTADMIN_COPY=0 + +FORWARD_EMAIL_PERIOD="1 minute" +FORWARD_EMAIL_COUNT=1 +FORWARD_PERSONAL_NOTE_SIZE=0 +FORWARD_FRIEND_COUNT_ATTRIBUTE= +KEEPFORWARDERATTRIBUTES=0 + +UPLOADIMAGES_DIR=uploadimages +PHPLIST_UPLOADS_MAX_SIZE=5M + +PUBLIC_SCHEMA=https +PHPLIST_ATTACHMENT_DOWNLOAD_URL=https://example.com/download/ +PHPLIST_ATTACHMENT_REPOSITORY_PATH=/tmp +MAX_AVATAR_SIZE=100000 diff --git a/.env.test.local.dist b/.env.test.local.dist new file mode 100644 index 00000000..c9992c34 --- /dev/null +++ b/.env.test.local.dist @@ -0,0 +1,9 @@ +# Optional: copy this file to ".env.test.local" to run tests against an in-memory SQLite +# database instead of MySQL, so no database server is needed for `vendor/bin/phpunit`. +# +# Note: this file is not loaded automatically by PHPUnit CLI runs (this project's ApplicationKernel +# does not read .env files on its own); either export these as real environment variables before +# running phpunit, or wire them up via your own bootstrap/CI step. + +PHPLIST_DATABASE_DRIVER=pdo_sqlite +PHPLIST_DATABASE_PATH=:memory: \ No newline at end of file diff --git a/.gitignore b/.gitignore index 25db886b..072e5252 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,9 @@ /composer.lock /config/bundles.yml /config/config_modules.yml -/config/parameters.yml +/.env +/.env.local +/.env.*.local /config/routing_modules.yml /nbproject /var/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 0254484d..f6e2111f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,10 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Added - Graylog integration for centralized logging (#TBD) +- `symfony/dotenv` support: configuration values are now read from a `.env` file (generated from `.env.dist` on install/update), in addition to real environment variables (#TBD) ### Changed +- `config/parameters.yml.dist` no longer contains inline `env(VAR): default` fallbacks; defaults now live in `.env.dist` (#TBD) ### Deprecated diff --git a/PHPDOC.md b/PHPDOC.md index 00ec597f..2243e294 100644 --- a/PHPDOC.md +++ b/PHPDOC.md @@ -1,25 +1,27 @@ -# Class Documentation with PHPDoc +# Generating class documentation -We use [phpdoc](phpdoc.org) to automatically generate documentation for our annotated classes. +We use [phpDocumentor](https://phpdoc.org) to generate API docs from the docblocks on +our classes, properties, and methods. Output settings (title, output path) are defined +in [`phpdoc.xml`](phpdoc.xml); the generated docs are written to `docs/phpdocumentor/` +and are not committed to the repository. -So to be able to generate or update our class docs you would need to download and install `phpDocumentor` globally (for system wide use) as shown below: +## Install phpDocumentor -1. `cd ~` [*Optional : it's recommended to navigate to your home dir before downloading `phpDocumentor` as shown in step 2*] -2. `wget https://phpdoc.org/phpDocumentor.phar` -3. `chmod +x phpDocumentor.phar` -4. `mv phpDocumentor.phar /usr/local/bin/phpDocumentor` +phpDocumentor ships as a standalone `.phar`. Install it once, globally: -*Possibility : In case you don't want to install `phpDocumentor` globally you can skip step 4, however you would need to run `phpDocumentor` from whatever path it was installed in.* +```bash +wget https://phpdoc.org/phpDocumentor.phar -O /usr/local/bin/phpDocumentor +chmod +x /usr/local/bin/phpDocumentor +``` -*Tip : You might need to run step four as root on some systems. That is : `sudo mv phpDocumentor.phar /usr/local/bin/phpDocumentor`* +If you'd rather not install it globally, download the `.phar` anywhere and call it +by its full path in the steps below. -## Generate Docs +## Generate the docs -If you did install `phpDocumentor` globally as specified above then you can generate class docs as follows. -Run : `composer run-php-documentor` +```bash +composer run-php-documentor +``` - - -*Note : `composer generate docs` would only work if you installed `phpDocumentor` globally, if you did not run : `custom/path/phpDocumentor -d 'src,tests' -t docs/phpdoc` to generate docs* - -*Where `custom/path/` is the location where you downloaded `phpDocumentor`* +This runs `phpDocumentor -d 'src,tests'`, using the output path from `phpdoc.xml`. +Open `docs/phpdocumentor/index.html` in a browser to view the result. \ No newline at end of file diff --git a/README.md b/README.md index 2015718a..4b929df2 100755 --- a/README.md +++ b/README.md @@ -52,18 +52,19 @@ this code. ## Documentation -* [Class Docs](docs/phpdoc/) * [Class structure overview](docs/ClassStructure.md) -* [Graphic domain model](docs/DomainModel/DomainModel.svg) and [description of the domain entities](docs/DomainModel/Entities.md) -* [Mailer Transports](docs/mailer-transports.md) - How to use different email providers (Gmail, Amazon SES, Mailchimp, SendGrid) -* [Asynchronous Email Sending](docs/AsyncEmailSending.md) - How to use asynchronous email sending with Symfony Messenger +* [Domain model diagram](docs/DomainModel/DomainModel.svg) and [description of the domain entities](docs/DomainModel/Entities.md) +* [Mailer transports](docs/MailerTransports.md) - configuring Gmail, Amazon SES, Mailchimp, and SendGrid +* [Asynchronous email sending](docs/AsyncEmailSending.md) - queuing email delivery with Symfony Messenger +* [Graylog integration](docs/Graylog.md) - centralized log management +* [Generating class API docs](PHPDOC.md) - regenerating the phpDocumentor output ## Running the web server The phpList application is configured so that the built-in PHP web server can run in development and testing mode, while Apache can run in production mode. -Please first set the database credentials in `config/parameters.yml`. +Please first set the database credentials in `.env` (created from `.env.dist` on `composer install`/`composer update`). ### Development @@ -79,12 +80,6 @@ already in use, on the next free port after 8000). You can stop the server with CTRL + C. -#### Development and Documentation - -We use `phpDocumentor` to automatically generate documentation for classes. To make this process efficient and easier, you are required to properly "document" your `classes`,`properties`, `methods` ... by annotating them with [docblocks](https://docs.phpdoc.org/latest/guide/guides/docblocks.html). - -More about generating docs in [PHPDOC.md](PHPDOC.md) - ### Testing Create test db with name phplist in your mysql DB or uncomment sqlite part in config_test.yml file to use in memory DB for functional tests. @@ -200,31 +195,14 @@ To access the phpList data from a third-party application (i.e., not from a phpList module), please use the [REST API](https://github.com/phpList/rest-api). -## Email Configuration - -phpList supports multiple email transport providers through Symfony Mailer. The following transports are included: - -* Gmail -* Amazon SES -* Mailchimp Transactional (Mandrill) -* SendGrid - -For detailed configuration instructions, see the [Mailer Transports documentation](docs/mailer-transports.md). - -## Copyright - -phpList is copyright (C) 2000-2025 [phpList Ltd](https://www.phplist.com/). - +## Translations -### Translations -command to extract translation strings +To extract translation strings from the source into an XLIFF catalog: ```bash php bin/console translation:extract --force en --format=xlf ``` -```bash -vendor/bin/phpstan analyse -c phpstan.neon; -vendor/bin/phpmd src/ text config/PHPMD/rules.xml; -vendor/bin/phpcs --standard=config/PhpCodeSniffer/ --ignore=*/Migrations/* bin/ src/ tests/ public/; -``` +## Copyright + +phpList is copyright (C) 2000-2025 [phpList Ltd](https://www.phplist.com/). diff --git a/composer.json b/composer.json index 9c95fb23..4bab2a2c 100644 --- a/composer.json +++ b/composer.json @@ -87,7 +87,9 @@ "ext-fileinfo": "*", "setasign/fpdf": "^1.8", "phpdocumentor/reflection-docblock": "^5.2", - "guzzlehttp/guzzle": "^7.4.5" + "guzzlehttp/guzzle": "^7.4.5", + "symfony/dotenv": "^6.4", + "symfony/doctrine-messenger": "^6.4" }, "require-dev": { "phpunit/phpunit": "^9.5", @@ -127,7 +129,7 @@ "PhpList\\Core\\Composer\\ScriptHandler::createGeneralConfiguration", "PhpList\\Core\\Composer\\ScriptHandler::createBundleConfiguration", "PhpList\\Core\\Composer\\ScriptHandler::createRoutesConfiguration", - "PhpList\\Core\\Composer\\ScriptHandler::createParametersConfiguration", + "PhpList\\Core\\Composer\\ScriptHandler::createDotenvConfiguration", "php bin/console cache:clear", "php bin/console cache:warmup" ], diff --git a/config/config_test.yml b/config/config_test.yml index 36ce489c..fe97391a 100644 --- a/config/config_test.yml +++ b/config/config_test.yml @@ -11,9 +11,12 @@ framework: doctrine: dbal: -# driver: 'pdo_sqlite' -# memory: true - driver: 'pdo_mysql' + # Defaults to pdo_mysql via PHPLIST_DATABASE_DRIVER (see .env). To run tests against an + # in-memory SQLite database instead (no MySQL server needed), set in .env.test.local: + # PHPLIST_DATABASE_DRIVER=pdo_sqlite + # PHPLIST_DATABASE_PATH=:memory: + driver: '%database_driver%' + path: '%database_path%' host: '%database_host%' port: '%database_port%' dbname: 'phplist' diff --git a/config/doctrine_migrations.yml b/config/doctrine_migrations.yml index 97e3bd6f..7c5eda4a 100644 --- a/config/doctrine_migrations.yml +++ b/config/doctrine_migrations.yml @@ -2,7 +2,7 @@ doctrine_migrations: migrations_paths: 'PhpList\Core\Migrations': '%kernel.project_dir%/src/Migrations' # 'TatevikGr\RssBundle\RssFeedBundle\Migrations': '%kernel.project_dir%/vendor/tatevikgr/rss-bundle/src/RssFeedBundle/Migrations' - all_or_nothing: true + all_or_nothing: false organize_migrations: false custom_template: '%kernel.project_dir%/src/Migrations/_template_migration.php.tpl' storage: diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index 4193c501..93022618 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -1,8 +1,7 @@ # This file is the Symfony Messenger configuration for asynchronous processing framework: messenger: - # Uncomment this (and the failed transport below) to send failed messages to this transport for later handling. - # failure_transport: failed + failure_transport: failed transports: # https://symfony.com/doc/current/messenger.html#transport-configuration @@ -20,7 +19,7 @@ framework: multiplier: 2 max_delay: 0 - # failed: 'doctrine://default?queue_name=failed' + failed: 'doctrine://default?queue_name=failed' routing: # Route your messages to the transports @@ -28,7 +27,7 @@ framework: 'PhpList\Core\Domain\Messaging\Message\SubscriberConfirmationMessage': async_email 'PhpList\Core\Domain\Messaging\Message\SubscriptionConfirmationMessage': async_email 'PhpList\Core\Domain\Messaging\Message\PasswordResetMessage': async_email - 'PhpList\Core\Domain\Messaging\Message\CampaignProcessorMessage': async_email - 'PhpList\Core\Domain\Messaging\Message\SyncCampaignProcessorMessage': sync + 'PhpList\Core\Domain\Messaging\Message\CampaignProcessor\CampaignProcessorMessage': async_email + 'PhpList\Core\Domain\Messaging\Message\CampaignProcessor\SyncCampaignProcessorMessage': sync 'PhpList\Core\Domain\Subscription\Message\DynamicTableMessage': sync diff --git a/config/parameters.yml b/config/parameters.yml new file mode 100644 index 00000000..f2793be5 --- /dev/null +++ b/config/parameters.yml @@ -0,0 +1,100 @@ +# This file is a "template" of what your parameters.yml file should look like +# Set parameters here that may be different on each deployment target of the app, e.g. development, staging, production. +# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration +# +# These variables are read from environment variables using the "env" construct. +# The environment variables themselves are defined in the ".env" file (see ".env.dist" for the template) +# and/or in the actual environment (e.g. Apache host configuration, command line). +parameters: + database_driver: '%env(PHPLIST_DATABASE_DRIVER)%' + database_path: '%env(PHPLIST_DATABASE_PATH)%' + database_host: '%env(PHPLIST_DATABASE_HOST)%' + database_port: '%env(PHPLIST_DATABASE_PORT)%' + database_name: '%env(PHPLIST_DATABASE_NAME)%' + database_user: '%env(PHPLIST_DATABASE_USER)%' + database_password: '%env(PHPLIST_DATABASE_PASSWORD)%' + database_prefix: '%env(DATABASE_PREFIX)%' + app.default_admin_password: '%env(PHPLIST_DEFAULT_ADMIN_PASSWORD)%' + list_table_prefix: '%env(LIST_TABLE_PREFIX)%' + app.dev_version: '%env(APP_DEV_VERSION)%' + app.dev_email: '%env(APP_DEV_EMAIL)%' + app.powered_by_phplist: '%env(APP_POWERED_BY_PHPLIST)%' + app.preference_page_show_private_lists: '%env(PREFERENCEPAGE_SHOW_PRIVATE_LISTS)%' + + app.rest_api_base_url: '%env(API_BASE_URL)%/api/v2' + app.api_base_url: '%env(API_BASE_URL)%' + app.frontend_base_url: '%env(FRONT_END_BASE_URL)%' + + parallel_use_with_phplist3: '%env(PARALLER_USE_WITH_PHPLIST3)%' + + # Email configuration + app.mailer_from: '%env(MAILER_FROM)%' + app.mailer_dsn: '%env(MAILER_DSN)%' + app.confirmation_url: '%env(CONFIRMATION_URL)%' + app.subscription_confirmation_url: '%env(SUBSCRIPTION_CONFIRMATION_URL)%' + app.password_reset_url: '%env(PASSWORD_RESET_URL)%' + app.show_unsubscribe_link: '%env(SHOW_UNSUBSCRIBELINK)%' + + # bounce email settings + imap_bounce.email: '%env(BOUNCE_EMAIL)%' + imap_bounce.password: '%env(BOUNCE_IMAP_PASS)%' + imap_bounce.host: '%env(BOUNCE_IMAP_HOST)%' + imap_bounce.port: '%env(BOUNCE_IMAP_PORT)%' + imap_bounce.encryption: '%env(BOUNCE_IMAP_ENCRYPTION)%' + imap_bounce.mailbox: '%env(BOUNCE_IMAP_MAILBOX)%' + imap_bounce.mailbox_name: '%env(BOUNCE_IMAP_MAILBOX_NAME)%' + imap_bounce.protocol: '%env(BOUNCE_IMAP_PROTOCOL)%' + imap_bounce.unsubscribe_threshold: '%env(BOUNCE_IMAP_UNSUBSCRIBE_THRESHOLD)%' + imap_bounce.blacklist_threshold: '%env(BOUNCE_IMAP_BLACKLIST_THRESHOLD)%' + imap_bounce.purge: '%env(BOUNCE_IMAP_PURGE)%' + imap_bounce.purge_unprocessed: '%env(BOUNCE_IMAP_PURGE_UNPROCESSED)%' + + # Messenger configuration for asynchronous processing + app.messenger_transport_dsn: '%env(MESSENGER_TRANSPORT_DSN)%' + + # A secret key that's used to generate certain security-related tokens + secret: '%env(PHPLIST_SECRET)%' + phplist.verify_ssl: '%env(VERIFY_SSL)%' + + graylog_host: 'graylog.phplist.local' + graylog_port: 12201 + + app.phplist_isp_conf_path: '%env(APP_PHPLIST_ISP_CONF_PATH)%' + + # Message sending + messaging.mail_queue_batch_size: '%env(MAILQUEUE_BATCH_SIZE)%' + messaging.mail_queue_period: '%env(MAILQUEUE_BATCH_PERIOD)%' + messaging.mail_queue_throttle: '%env(MAILQUEUE_THROTTLE)%' + messaging.max_process_time: '%env(MESSAGING_MAX_PROCESS_TIME)%' + messaging.max_mail_size: '%env(MAX_MAILSIZE)%' + messaging.default_message_age: '%env(DEFAULT_MESSAGEAGE)%' + messaging.use_manual_text_part: '%env(USE_MANUAL_TEXT_PART)%' + messaging.blacklist_grace_time: '%env(MESSAGING_BLACKLIST_GRACE_TIME)%' + messaging.google_sender_id: '%env(GOOGLE_SENDERID)%' + messaging.use_amazon_ses: '%env(USE_AMAZONSES)%' + messaging.use_precedence_header: '%env(USE_PRECEDENCE_HEADER)%' + messaging.embed_external_images: '%env(EMBEDEXTERNALIMAGES)%' + messaging.embed_uploaded_images: '%env(EMBEDUPLOADIMAGES)%' + messaging.external_image_max_age: '%env(EXTERNALIMAGE_MAXAGE)%' + messaging.external_image_timeout: '%env(EXTERNALIMAGE_TIMEOUT)%' + messaging.external_image_max_size: '%env(EXTERNALIMAGE_MAXSIZE)%' + messaging.forward_alternative_content: '%env(FORWARD_ALTERNATIVE_CONTENT)%' + messaging.email_text_credits: '%env(EMAILTEXTCREDITS)%' + messaging.always_add_user_track: '%env(ALWAYS_ADD_USERTRACK)%' + messaging.send_list_admin_copy: '%env(SEND_LISTADMIN_COPY)%' + + phplist.forward_email_period: '%env(FORWARD_EMAIL_PERIOD)%' + phplist.forward_email_count: '%env(FORWARD_EMAIL_COUNT)%' + phplist.forward_personal_note_size: '%env(FORWARD_PERSONAL_NOTE_SIZE)%' + phplist.forward_friend_count_attribute: '%env(FORWARD_FRIEND_COUNT_ATTRIBUTE)%' + phplist.keep_forwarded_attributes: '%env(KEEPFORWARDERATTRIBUTES)%' + + phplist.upload_images_dir: '%env(UPLOADIMAGES_DIR)%' + phplist.uploads.allowed_mime_types: ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml'] + phplist.uploads.allowed_extensions: ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] + phplist.uploads.max_size: '%env(PHPLIST_UPLOADS_MAX_SIZE)%' + + phplist.public_schema: '%env(PUBLIC_SCHEMA)%' + phplist.attachment_download_url: '%env(PHPLIST_ATTACHMENT_DOWNLOAD_URL)%' + phplist.attachment_repository_path: '%env(PHPLIST_ATTACHMENT_REPOSITORY_PATH)%' + phplist.max_avatar_size: '%env(MAX_AVATAR_SIZE)%' diff --git a/config/parameters.yml.dist b/config/parameters.yml.dist deleted file mode 100644 index cf9a17e6..00000000 --- a/config/parameters.yml.dist +++ /dev/null @@ -1,168 +0,0 @@ -# This file is a "template" of what your parameters.yml file should look like -# Set parameters here that may be different on each deployment target of the app, e.g. development, staging, production. -# https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration -# -# These variables are read from environment variables using the "env" construct. -# You can set environment variables in the Apache host configuration and also on the command line. -# If you cannot provide any environment variables, you can also set the variables in this file -# in the lines with "env(VARIABLE_NAME)". -parameters: - database_driver: '%%env(PHPLIST_DATABASE_DRIVER)%%' - env(PHPLIST_DATABASE_DRIVER): 'pdo_mysql' - database_path: '%%env(PHPLIST_DATABASE_PATH)%%' - env(PHPLIST_DATABASE_PATH): null - database_host: '%%env(PHPLIST_DATABASE_HOST)%%' - env(PHPLIST_DATABASE_HOST): '127.0.0.1' - database_port: '%%env(PHPLIST_DATABASE_PORT)%%' - env(PHPLIST_DATABASE_PORT): '3306' - database_name: '%%env(PHPLIST_DATABASE_NAME)%%' - env(PHPLIST_DATABASE_NAME): 'phplistdb' - database_user: '%%env(PHPLIST_DATABASE_USER)%%' - env(PHPLIST_DATABASE_USER): 'phplist' - database_password: '%%env(PHPLIST_DATABASE_PASSWORD)%%' - env(PHPLIST_DATABASE_PASSWORD): 'phplist' - database_prefix: '%%env(DATABASE_PREFIX)%%' - env(DATABASE_PREFIX): 'phplist_' - list_table_prefix: '%%env(LIST_TABLE_PREFIX)%%' - env(LIST_TABLE_PREFIX): 'listattr_' - app.dev_version: '%%env(APP_DEV_VERSION)%%' - env(APP_DEV_VERSION): '0' - app.dev_email: '%%env(APP_DEV_EMAIL)%%' - env(APP_DEV_EMAIL): 'dev@dev.com' - app.powered_by_phplist: '%%env(APP_POWERED_BY_PHPLIST)%%' - env(APP_POWERED_BY_PHPLIST): '0' - app.preference_page_show_private_lists: '%%env(PREFERENCEPAGE_SHOW_PRIVATE_LISTS)%%' - env(PREFERENCEPAGE_SHOW_PRIVATE_LISTS): '0' - app.rest_api_base_url: '%%env(REST_API_BASE_URL)%%' - env(REST_API_BASE_URL): 'http://api.phplist.local/api/v2' - api_base_url: '%%env(API_BASE_URL)%%' - env(API_BASE_URL): 'http://api.phplist.local/' - app.frontend_base_url: '%%env(FRONT_END_BASE_URL)%%' - env(FRONT_END_BASE_URL): 'http://frontend.phplist.local' - parallel_use_with_phplist3: '%%env(parallel_use_with_phplist3)%%' - env(parallel_use_with_phplist3): '0' - - # Email configuration - app.mailer_from: '%%env(MAILER_FROM)%%' - env(MAILER_FROM): 'noreply@phplist.com' - app.mailer_dsn: '%%env(MAILER_DSN)%%' - env(MAILER_DSN): 'null://null' # set local_domain on transport - app.confirmation_url: '%%env(CONFIRMATION_URL)%%' - env(CONFIRMATION_URL): 'http://api.phplist.local/api/v2/subscriber/confirm/' - app.subscription_confirmation_url: '%%env(SUBSCRIPTION_CONFIRMATION_URL)%%' - env(SUBSCRIPTION_CONFIRMATION_URL): 'http://api.phplist.local/api/v2/subscription/confirm/' - app.password_reset_url: '%%env(PASSWORD_RESET_URL)%%' - env(PASSWORD_RESET_URL): 'https://example.com/reset/' - app.show_unsubscribe_link: '%%env(SHOW_UNSUBSCRIBELINK)%%' - env(SHOW_UNSUBSCRIBELINK): '1' - - # bounce email settings - imap_bounce.email: '%%env(BOUNCE_EMAIL)%%' - env(BOUNCE_EMAIL): 'bounce@phplist.com' - imap_bounce.password: '%%env(BOUNCE_IMAP_PASS)%%' - env(BOUNCE_IMAP_PASS): 'bounce@phplist.com' - imap_bounce.host: '%%env(BOUNCE_IMAP_HOST)%%' - env(BOUNCE_IMAP_HOST): 'imap.phplist.com' - imap_bounce.port: '%%env(BOUNCE_IMAP_PORT)%%' - env(BOUNCE_IMAP_PORT): '993' - imap_bounce.encryption: '%%env(BOUNCE_IMAP_ENCRYPTION)%%' - env(BOUNCE_IMAP_ENCRYPTION): 'ssl' - imap_bounce.mailbox: '%%env(BOUNCE_IMAP_MAILBOX)%%' - env(BOUNCE_IMAP_MAILBOX): '/var/spool/mail/bounces' - imap_bounce.mailbox_name: '%%env(BOUNCE_IMAP_MAILBOX_NAME)%%' - env(BOUNCE_IMAP_MAILBOX_NAME): 'INBOX,ONE_MORE' - imap_bounce.protocol: '%%env(BOUNCE_IMAP_PROTOCOL)%%' - env(BOUNCE_IMAP_PROTOCOL): 'imap' - imap_bounce.unsubscribe_threshold: '%%env(BOUNCE_IMAP_UNSUBSCRIBE_THRESHOLD)%%' - env(BOUNCE_IMAP_UNSUBSCRIBE_THRESHOLD): '5' - imap_bounce.blacklist_threshold: '%%env(BOUNCE_IMAP_BLACKLIST_THRESHOLD)%%' - env(BOUNCE_IMAP_BLACKLIST_THRESHOLD): '3' - imap_bounce.purge: '%%env(BOUNCE_IMAP_PURGE)%%' - env(BOUNCE_IMAP_PURGE): '0' - imap_bounce.purge_unprocessed: '%%env(BOUNCE_IMAP_PURGE_UNPROCESSED)%%' - env(BOUNCE_IMAP_PURGE_UNPROCESSED): '0' - - # Messenger configuration for asynchronous processing - app.messenger_transport_dsn: '%%env(MESSENGER_TRANSPORT_DSN)%%' - env(MESSENGER_TRANSPORT_DSN): 'doctrine://default?auto_setup=true' - - # A secret key that's used to generate certain security-related tokens - secret: '%%env(PHPLIST_SECRET)%%' - env(PHPLIST_SECRET): %1$s - phplist.verify_ssl: '%%env(VERIFY_SSL)%%' - env(VERIFY_SSL): '1' - - graylog_host: 'graylog.phplist.local' - graylog_port: 12201 - - app.phplist_isp_conf_path: '%%env(APP_PHPLIST_ISP_CONF_PATH)%%' - env(APP_PHPLIST_ISP_CONF_PATH): '/etc/phplist.conf' - - # Message sending - messaging.mail_queue_batch_size: '%%env(MAILQUEUE_BATCH_SIZE)%%' - env(MAILQUEUE_BATCH_SIZE): '5' - messaging.mail_queue_period: '%%env(MAILQUEUE_BATCH_PERIOD)%%' - env(MAILQUEUE_BATCH_PERIOD): '5' - messaging.mail_queue_throttle: '%%env(MAILQUEUE_THROTTLE)%%' - env(MAILQUEUE_THROTTLE): '5' - messaging.max_process_time: '%%env(MESSAGING_MAX_PROCESS_TIME)%%' - env(MESSAGING_MAX_PROCESS_TIME): '600' - messaging.max_mail_size: '%%env(MAX_MAILSIZE)%%' - env(MAX_MAILSIZE): '209715200' - messaging.default_message_age: '%%env(DEFAULT_MESSAGEAGE)%%' - env(DEFAULT_MESSAGEAGE): '691200' - messaging.use_manual_text_part: '%%env(USE_MANUAL_TEXT_PART)%%' - env(USE_MANUAL_TEXT_PART): '0' - messaging.blacklist_grace_time: '%%env(MESSAGING_BLACKLIST_GRACE_TIME)%%' - env(MESSAGING_BLACKLIST_GRACE_TIME): '600' - messaging.google_sender_id: '%%env(GOOGLE_SENDERID)%%' - env(GOOGLE_SENDERID): '' - messaging.use_amazon_ses: '%%env(USE_AMAZONSES)%%' - env(USE_AMAZONSES): '0' - messaging.use_precedence_header: '%%env(USE_PRECEDENCE_HEADER)%%' - env(USE_PRECEDENCE_HEADER): '0' - messaging.embed_external_images: '%%env(EMBEDEXTERNALIMAGES)%%' - env(EMBEDEXTERNALIMAGES): '0' - messaging.embed_uploaded_images: '%%env(EMBEDUPLOADIMAGES)%%' - env(EMBEDUPLOADIMAGES): '0' - messaging.external_image_max_age: '%%env(EXTERNALIMAGE_MAXAGE)%%' - env(EXTERNALIMAGE_MAXAGE): '0' - messaging.external_image_timeout: '%%env(EXTERNALIMAGE_TIMEOUT)%%' - env(EXTERNALIMAGE_TIMEOUT): '30' - messaging.external_image_max_size: '%%env(EXTERNALIMAGE_MAXSIZE)%%' - env(EXTERNALIMAGE_MAXSIZE): '204800' - messaging.forward_alternative_content: '%%env(FORWARD_ALTERNATIVE_CONTENT)%%' - env(FORWARD_ALTERNATIVE_CONTENT): '0' - messaging.email_text_credits: '%%env(EMAILTEXTCREDITS)%%' - env(EMAILTEXTCREDITS): '0' - messaging.always_add_user_track: '%%env(ALWAYS_ADD_USERTRACK)%%' - env(ALWAYS_ADD_USERTRACK): '1' - messaging.send_list_admin_copy: '%%env(SEND_LISTADMIN_COPY)%%' - env(SEND_LISTADMIN_COPY): '0' - - phplist.forward_email_period: '%%env(FORWARD_EMAIL_PERIOD)%%' - env(FORWARD_EMAIL_PERIOD): '1 minute' - phplist.forward_email_count: '%%env(FORWARD_EMAIL_COUNT)%%' - env(FORWARD_EMAIL_COUNT): '1' - phplist.forward_personal_note_size: '%%env(FORWARD_PERSONAL_NOTE_SIZE)%%' - env(FORWARD_PERSONAL_NOTE_SIZE): '0' - phplist.forward_friend_count_attribute: '%%env(FORWARD_FRIEND_COUNT_ATTRIBUTE)%%' - env(FORWARD_FRIEND_COUNT_ATTRIBUTE): '' - phplist.keep_forwarded_attributes: '%%env(KEEPFORWARDERATTRIBUTES)%%' - env(KEEPFORWARDERATTRIBUTES): '0' - - phplist.upload_images_dir: '%%env(UPLOADIMAGES_DIR)%%' - env(UPLOADIMAGES_DIR): 'uploadimages' - phplist.uploads.allowed_mime_types: ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml'] - phplist.uploads.allowed_extensions: ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] - phplist.uploads.max_size: '%%env(PHPLIST_UPLOADS_MAX_SIZE)%%' - env(PHPLIST_UPLOADS_MAX_SIZE): '5M' - - phplist.public_schema: '%%env(PUBLIC_SCHEMA)%%' - env(PUBLIC_SCHEMA): 'https' - phplist.attachment_download_url: '%%env(PHPLIST_ATTACHMENT_DOWNLOAD_URL)%%' - env(PHPLIST_ATTACHMENT_DOWNLOAD_URL): 'https://example.com/download/' - phplist.attachment_repository_path: '%%env(PHPLIST_ATTACHMENT_REPOSITORY_PATH)%%' - env(PHPLIST_ATTACHMENT_REPOSITORY_PATH): '/tmp' - phplist.max_avatar_size: '%%env(MAX_AVATAR_SIZE)%%' - env(MAX_AVATAR_SIZE): '100000' diff --git a/config/services.yml b/config/services.yml index 7c053ed9..1fcc3b35 100644 --- a/config/services.yml +++ b/config/services.yml @@ -51,6 +51,10 @@ services: tags: - { name: 'doctrine.dbal.schema_filter', connection: 'default' } + PhpList\Core\Core\Doctrine\TablePrefixListener: + arguments: + $tablePrefix: '%database_prefix%' + HTMLPurifier_Config: class: HTMLPurifier_Config factory: [ 'HTMLPurifier_Config', 'createDefault' ] diff --git a/docs/AsyncEmailSending.md b/docs/AsyncEmailSending.md index da4f247c..386eae49 100644 --- a/docs/AsyncEmailSending.md +++ b/docs/AsyncEmailSending.md @@ -64,10 +64,10 @@ You can test the email functionality using the built-in command: ```bash # Queue an email for asynchronous sending -bin/console app:send-test-email recipient@example.com +bin/console phplist:test-email recipient@example.com # Send an email synchronously (immediately) -bin/console app:send-test-email recipient@example.com --sync +bin/console phplist:test-email recipient@example.com --sync ``` ## Processing the Email Queue @@ -90,8 +90,14 @@ bin/console messenger:stats # View failed messages bin/console messenger:failed:show + +# Retry a failed message +bin/console messenger:failed:retry ``` +Failed messages are routed to the `failed` transport (a separate queue in the +same Doctrine table), configured in `config/packages/messenger.yaml`. + ## Troubleshooting If emails are not being sent: diff --git a/docs/ClassStructure.md b/docs/ClassStructure.md index 8b3d9516..1f586515 100644 --- a/docs/ClassStructure.md +++ b/docs/ClassStructure.md @@ -46,4 +46,3 @@ Security‑related concerns. Utilities to support tests. - Traits/: Reusable traits and helpers used in the test suite. - diff --git a/docs/DomainModel/Entities.md b/docs/DomainModel/Entities.md index 5b83323d..a434b9f7 100644 --- a/docs/DomainModel/Entities.md +++ b/docs/DomainModel/Entities.md @@ -1,5 +1,8 @@ # Domain Entities +Table names below use the default `DATABASE_PREFIX` (`phplist_`, set in `.env`). The +prefix is applied dynamically at runtime, so it can be changed per installation. + ## Identity Context ### Administrator @@ -13,11 +16,23 @@ Administrators are not subscribers. If administrators would like to subscribe to subscriber lists, they need to have a separate subscriber account. ### AdministratorAttribute -Table name: `phplist_adminattribute` or `phplist_admin_attribute` +Table name: `phplist_adminattribute` + +This is similar to a subscriber attribute: It defines a field for +administrators (name and ID only, not the value). These can then be used as +placeholders in campaigns. + +### AdministratorAttributeValue +Table name: `phplist_admin_attribute` + +The value of a particular **AdministratorAttribute** for a particular +**administrator**. -This is similar to a subscriber attribute: It allows you to have details of -administrators. These can then be used in campaigns. Basically, you can add -placeholders for administrator attributes in campaigns. +### AdministratorLogin +Table name: `phplist_admin_login` + +A record of a single login session for an **administrator**: source IP +address, session ID, and whether the session is still active. ### AdministratorPasswordRequest Table name: `phplist_admin_password_request` @@ -31,15 +46,14 @@ This table contains the API tokens for **administrators**. Those API tokens are used for access to the REST API. In the web frontend, they are also used for CSRF protection. - -## SubscriptionContext +## Subscription Context ### Attribute Table name: `phplist_user_attribute` An **attribute** is a field for subscribers. This entity does not -contain the values for this attribute for each individual subscribe, but -only the name of the attribute and an ID. +contain the values for this attribute for each individual subscriber, but +only the name of the attribute and an ID. ### AttributeValue Table name: `phplist_user_user_attribute` @@ -50,7 +64,7 @@ particular **subscriber**. ### SubscribePage Table name: `phplist_subscribepage` -*subscribePages** allow setting up a selection of subscriber lists, attributes +**SubscribePages** allow setting up a selection of subscriber lists, attributes and language, and some other settings to control the content for the page that can be used to subscribe to the system. As a result, you can e.g., have different pages per language, which allows you to translate all the content @@ -97,8 +111,6 @@ multiple subscriber lists, and a campaign can be sent to multiple subscriber lists, but this association ensures that a subscriber always only receives one copy of a campaign, regardless of other associations. -Should we use a named association for this? What should it be named? - ### SuppressionList Table name: `phplist_user_blacklist` @@ -113,21 +125,25 @@ Table name: `phplist_user_blacklist_data` This is some more additional info on a SuppressionList. - ## Messaging Context - ### Attachment Table name: `phplist_attachment` An attachment represents a file attached to exactly one **campaign**. ### Bounce -Table name: `phplist_boune` +Table name: `phplist_bounce` + +A recorded bounce message: the original bounce email's header and body, plus +a classification status and comment. ### BounceRegEx Table name: `phplist_bounceregex` +A regular expression used to classify **bounces** by matching their content, +with an associated action (e.g. unsubscribe the subscriber). + ### Campaign Table name: `phplist_message` @@ -137,7 +153,9 @@ potentially multiple subscriber lists). The campaign has been created by an **subscribers**. It is stored to which subscribers a campaign has been sent. ### CampaignBounce -Table name: `phplist_message_bounce` +Table name: `phplist_user_message_bounce` + +Links a **bounce** to the **subscriber** and **campaign** it resulted from. ### CampaignData Table name: `phplist_messagedata` @@ -147,7 +165,7 @@ Google tracking IDs, special relationships to **subscriber lists**, and alias titles. ### CampaignForward -Table name: `phplist_message_forward` +Table name: `phplist_user_message_forward` This tracks details of **campaigns** which were forwarded by a recipient **subscriber** to someone else via an email message. @@ -170,7 +188,6 @@ Table name: `phplist_templateimage` This contains images used in **templates**. The blob contains the image. - ## System Context ### Configuration @@ -208,7 +225,6 @@ time they were updated), [the MD5 for that](https://phplist.com/files/tlds-alpha-by-domain.txt.md5), etc. etc. - ## Tracking Context ### LinkTrackForward @@ -229,7 +245,6 @@ Table name: `phplist_linktrack_uml_click` When a **subscriber** clicks on a link in a message, this click will be recorded here. - ## Unused entities * LinkTrack, table name: `phplist_linktrack` diff --git a/docs/Graylog.md b/docs/Graylog.md index 0abbcb57..b5db0671 100644 --- a/docs/Graylog.md +++ b/docs/Graylog.md @@ -1,81 +1,50 @@ # Graylog Integration -This document explains how to use the Graylog integration in the phpList core application. +phpList can send logs to [Graylog](https://graylog.org/) over GELF (Graylog Extended +Log Format) using Monolog's `gelf` handler. The handler ships **disabled by default** +in both environments. -## Overview +## Enabling it -Graylog is a log management platform that collects, indexes, and analyzes log messages from various sources. The phpList core application is configured to send logs to Graylog using the GELF (Graylog Extended Log Format) protocol. - -## Configuration - -The Graylog integration is configured in the following files: - -- `config/config_prod.yml` - Production environment configuration -- `config/config_dev.yml` - Development environment configuration - -### Default Configuration - -By default, the application is configured to: - -- In production: Send logs of level "error" and above to Graylog -- In development: Send logs of all levels to Graylog - -The default configuration points to a placeholder Graylog server at `graylog.example.com:12201`. You need to update this to point to your actual Graylog server. - -### Updating the Graylog Server Details - -To update the Graylog server details, modify the following sections in the configuration files: - -In `config/config_prod.yml`: - -```yaml -graylog: - type: gelf - publisher: - hostname: graylog.example.com # Replace with your Graylog server hostname - port: 12201 # Default GELF UDP port - level: error # Only send errors and above to Graylog -``` - -In `config/config_dev.yml`: +1. In `config/config_prod.yml`, uncomment the `graylog` handler under `monolog.handlers`. + It sends `error`-level and above logs, using the `graylog_host` and `graylog_port` + parameters from `config/parameters.yml` (defaults: `graylog.phplist.local:12201`). +2. In `config/config_dev.yml`, uncomment the `graylog` handler to also log in + development. It sends every level except the `event` channel. +3. Update `graylog_host` and `graylog_port` in `config/parameters.yml` to point at + your Graylog server. ```yaml -graylog: - type: gelf - publisher: - hostname: graylog.example.com # Replace with your Graylog server hostname - port: 12201 # Default GELF UDP port - level: debug # Send all logs to Graylog in development - channels: ['!event'] +# config/parameters.yml +parameters: + graylog_host: 'graylog.example.com' + graylog_port: 12201 ``` -Replace `graylog.example.com` with the hostname or IP address of your Graylog server, and update the port if necessary. +## Graylog server setup -## Graylog Server Setup +Your Graylog server needs a GELF UDP input to receive these logs: -To receive logs from the application, your Graylog server needs to be configured with a GELF UDP input: - -1. In the Graylog web interface, go to System > Inputs -2. Select "GELF UDP" from the dropdown and click "Launch new input" -3. Configure the input with the following settings: +1. In the Graylog web interface, go to System > Inputs. +2. Select "GELF UDP" and click "Launch new input". +3. Configure it with: - Title: phpList Core - - Bind address: 0.0.0.0 (to listen on all interfaces) - - Port: 12201 (or the port you specified in the configuration) -4. Click "Save" - -## Testing the Integration + - Bind address: `0.0.0.0` (listen on all interfaces) + - Port: `12201` (or whatever you set as `graylog_port`) +4. Click "Save". -To test if logs are being sent to Graylog: +## Testing the integration -1. Generate some log messages in the application (e.g., by triggering an error) -2. Check the Graylog web interface to see if the logs are being received -3. If logs are not appearing, check the application logs for any errors related to the Graylog connection +1. Trigger a log message in the application (e.g. an error). +2. Check the Graylog web interface for the message. +3. If nothing shows up, see Troubleshooting below. ## Troubleshooting -If logs are not appearing in Graylog: +If logs aren't appearing in Graylog: -1. Verify that the Graylog server is running and accessible from the application server -2. Check that the GELF UDP input is properly configured and running in Graylog -3. Ensure that there are no firewall rules blocking UDP traffic on port 12201 (or your configured port) -4. Check the application logs for any errors related to the Graylog connection +1. Confirm the `graylog` handler is uncommented in the config for the environment + you're testing. +2. Verify the Graylog server is running and reachable from the application server. +3. Check that the GELF UDP input is running and bound to the port you configured. +4. Check for firewall rules blocking UDP traffic on that port. \ No newline at end of file diff --git a/docs/MailerTransports.md b/docs/MailerTransports.md index cde763da..9488923a 100644 --- a/docs/MailerTransports.md +++ b/docs/MailerTransports.md @@ -80,7 +80,7 @@ Notes: After setting up your preferred mailer transport, you can test it using the built-in test command: ```bash -bin/console app:send-test-email recipient@example.com +bin/console phplist:test-email recipient@example.com ``` ## Switching Between Transports @@ -91,7 +91,7 @@ You can easily switch between different mailer transports by changing the `MAILE 2. Set the environment variable in your server configuration 3. Set the environment variable before running a command: ```bash - MAILER_DSN=sendgrid://API_KEY@default bin/console app:send-test-email recipient@example.com + MAILER_DSN=sendgrid://API_KEY@default bin/console phplist:test-email recipient@example.com ``` ## Additional Configuration diff --git a/public/app.php b/public/app.php deleted file mode 100644 index 8e58c4f4..00000000 --- a/public/app.php +++ /dev/null @@ -1,11 +0,0 @@ -configure() - ->dispatch(); diff --git a/public/app_dev.php b/public/app_dev.php deleted file mode 100644 index 46c49194..00000000 --- a/public/app_dev.php +++ /dev/null @@ -1,14 +0,0 @@ -ensureDevelopmentOrTestingEnvironment() - ->setEnvironment(Environment::DEVELOPMENT) - ->configure() - ->dispatch(); diff --git a/public/app_test.php b/public/app_test.php deleted file mode 100644 index af816b87..00000000 --- a/public/app_test.php +++ /dev/null @@ -1,14 +0,0 @@ -ensureDevelopmentOrTestingEnvironment() - ->setEnvironment(Environment::TESTING) - ->configure() - ->dispatch(); diff --git a/src/Composer/ScriptHandler.php b/src/Composer/ScriptHandler.php index 55e23739..426ac71c 100644 --- a/src/Composer/ScriptHandler.php +++ b/src/Composer/ScriptHandler.php @@ -36,17 +36,22 @@ class ScriptHandler /** * @var string */ - const PARAMETERS_CONFIGURATION_FILE = '/config/parameters.yml'; + const GENERAL_CONFIGURATION_FILE = '/config/config_modules.yml'; /** * @var string */ - const GENERAL_CONFIGURATION_FILE = '/config/config_modules.yml'; + const DOTENV_FILE = '/.env'; + + /** + * @var string + */ + const DOTENV_TEMPLATE_FILE = '/.env.dist'; /** * @var string */ - const PARAMETERS_TEMPLATE_FILE = '/config/parameters.yml.dist'; + const PARAMETERS_CONFIGURATION_FILE = '/config/parameters.yml'; /** * @return string absolute application root directory without the trailing slash @@ -265,23 +270,40 @@ public static function clearAllCaches():void } /** - * Creates config/parameters.yml (the parameters configuration file). + * Creates the .env file (the environment variables consumed by the parameters configuration) + * by copying it from .env.dist, generating a fresh app secret in the process. * * @return void */ - public static function createParametersConfiguration(): void + public static function createDotenvConfiguration(): void { - $configurationFilePath = self::getApplicationRoot() . self::PARAMETERS_CONFIGURATION_FILE; - if (file_exists($configurationFilePath)) { + $appDotenvFilePath = self::getApplicationRoot() . self::DOTENV_FILE; + $templateFilePath = __DIR__ . '/../..' . static::DOTENV_TEMPLATE_FILE; + + if (file_exists($appDotenvFilePath)) { return; } - $templateFilePath = __DIR__ . '/../..' . static::PARAMETERS_TEMPLATE_FILE; $template = file_get_contents($templateFilePath); $secret = bin2hex(random_bytes(20)); $configuration = sprintf($template, $secret); + self::createAndWriteFile($appDotenvFilePath, $configuration); + } + + + /** + * Creates config/parameters.yml (the parameters configuration file). + * + * @return void + */ + public static function createParametersConfiguration(): void + { + $configurationFilePath = self::getApplicationRoot() . self::PARAMETERS_CONFIGURATION_FILE; + $templateFilePath = __DIR__ . '/../..' . static::PARAMETERS_CONFIGURATION_FILE; + $configuration = file_get_contents($templateFilePath); + self::createAndWriteFile($configurationFilePath, $configuration); } diff --git a/src/Core/ApplicationKernel.php b/src/Core/ApplicationKernel.php index 8f43e62b..8f67de65 100644 --- a/src/Core/ApplicationKernel.php +++ b/src/Core/ApplicationKernel.php @@ -128,6 +128,11 @@ public function registerContainerConfiguration(LoaderInterface $loader): void if (file_exists($twigConfigFile)) { $loader->load($twigConfigFile); } + + $messengerConfigFile = $this->getApplicationDir() . '/config/packages/messenger.yaml'; + if (file_exists($messengerConfigFile)) { + $loader->load($messengerConfigFile); + } } /** diff --git a/src/Core/Bootstrap.php b/src/Core/Bootstrap.php index 82ddb28f..4c7af464 100644 --- a/src/Core/Bootstrap.php +++ b/src/Core/Bootstrap.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManagerInterface; use Exception; use RuntimeException; +use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\ErrorHandler\ErrorHandler; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -147,10 +148,50 @@ public function configure(): Bootstrap { $this->isConfigured = true; - return $this->configureDebugging() + return $this->loadEnvironmentVariables() + ->configureDebugging() ->configureApplicationKernel(); } + /** + * Loads environment variables from the application's ".env" files (if present) using Symfony Dotenv, + * following the standard ".env" -> ".env.local" -> ".env.$environment" -> ".env.$environment.local" cascade. + * + * ".env.dist" is a template only and must never be used to source real configuration: Symfony Dotenv + * would otherwise silently load it (with its literal placeholder values) whenever ".env" is missing. + * + * @return Bootstrap fluent interface + * + * @throws RuntimeException if ".env" does not exist, or PHPLIST_SECRET was not resolved to a real value + * @SuppressWarnings("PHPMD.Superglobals") + */ + private function loadEnvironmentVariables(): Bootstrap + { + $applicationRoot = $this->applicationStructure->getApplicationRoot(); + $dotenvPath = $applicationRoot . '/.env'; + if (!file_exists($dotenvPath)) { + throw new RuntimeException( + 'No ".env" file was found at "' . $dotenvPath . '". Run "composer install"/"composer update" ' . + 'to generate it from ".env.dist" (which is a template only and must not be used directly), ' . + 'or create ".env" manually with a real PHPLIST_SECRET.', + 1754766600 + ); + } + + (new Dotenv())->loadEnv($dotenvPath, 'APP_ENV', $this->environment); + + $secret = $_SERVER['PHPLIST_SECRET'] ?? $_ENV['PHPLIST_SECRET'] ?? ''; + if ($secret === '' || $secret === '%s') { + throw new RuntimeException( + 'PHPLIST_SECRET in ".env" is missing or still set to the ".env.dist" template placeholder. ' . + 'Set it to a real, unique, freshly generated secret before starting the application.', + 1754766601 + ); + } + + return $this; + } + /** * Makes sure that configure has been called before. * diff --git a/src/Core/Doctrine/TablePrefixListener.php b/src/Core/Doctrine/TablePrefixListener.php new file mode 100644 index 00000000..eee9098f --- /dev/null +++ b/src/Core/Doctrine/TablePrefixListener.php @@ -0,0 +1,34 @@ +getClassMetadata(); + + if ($metadata->isMappedSuperclass || $metadata->isEmbeddedClass) { + return; + } + + if (!str_starts_with($metadata->getName(), 'PhpList\\Core\\Domain\\')) { + return; + } + + $metadata->setPrimaryTable([ + 'name' => $this->tablePrefix . $metadata->getTableName(), + ]); + } +} diff --git a/src/Domain/Analytics/Model/LinkTrack.php b/src/Domain/Analytics/Model/LinkTrack.php index 848dde5e..b0d8c7bf 100644 --- a/src/Domain/Analytics/Model/LinkTrack.php +++ b/src/Domain/Analytics/Model/LinkTrack.php @@ -12,12 +12,13 @@ use PhpList\Core\Domain\Common\Model\Interfaces\Identity; #[ORM\Entity(repositoryClass: LinkTrackRepository::class)] -#[ORM\Table(name: 'phplist_linktrack')] +#[ORM\Table(name: 'linktrack')] #[ORM\UniqueConstraint(name: 'phplist_linktrack_miduidurlindex', columns: ['messageid', 'userid', 'url'])] #[ORM\Index(name: 'phplist_linktrack_midindex', columns: ['messageid'])] #[ORM\Index(name: 'phplist_linktrack_miduidindex', columns: ['messageid', 'userid'])] #[ORM\Index(name: 'phplist_linktrack_uidindex', columns: ['userid'])] #[ORM\Index(name: 'phplist_linktrack_urlindex', columns: ['url'])] +#[ORM\Index(name: 'phplist_linktrack_latestclickindex', columns: ['latestclick'])] class LinkTrack implements DomainModel, Identity { #[ORM\Id] diff --git a/src/Domain/Analytics/Model/LinkTrackForward.php b/src/Domain/Analytics/Model/LinkTrackForward.php index 0e03c017..2bc059b0 100644 --- a/src/Domain/Analytics/Model/LinkTrackForward.php +++ b/src/Domain/Analytics/Model/LinkTrackForward.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Common\Model\Interfaces\Identity; #[ORM\Entity(repositoryClass: LinkTrackForwardRepository::class)] -#[ORM\Table(name: 'phplist_linktrack_forward')] +#[ORM\Table(name: 'linktrack_forward')] #[ORM\UniqueConstraint(name: 'phplist_linktrack_forward_urlunique', columns: ['urlhash'])] #[ORM\Index(name: 'phplist_linktrack_forward_urlindex', columns: ['url'])] #[ORM\Index(name: 'phplist_linktrack_forward_uuididx', columns: ['uuid'])] diff --git a/src/Domain/Analytics/Model/LinkTrackMl.php b/src/Domain/Analytics/Model/LinkTrackMl.php index 419c7911..ff6bab0a 100644 --- a/src/Domain/Analytics/Model/LinkTrackMl.php +++ b/src/Domain/Analytics/Model/LinkTrackMl.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Common\Model\Interfaces\DomainModel; #[ORM\Entity(repositoryClass: LinkTrackMlRepository::class)] -#[ORM\Table(name: 'phplist_linktrack_ml')] +#[ORM\Table(name: 'linktrack_ml')] #[ORM\Index(name: 'phplist_linktrack_ml_fwdindex', columns: ['forwardid'])] #[ORM\Index(name: 'phplist_linktrack_ml_midindex', columns: ['messageid'])] class LinkTrackMl implements DomainModel diff --git a/src/Domain/Analytics/Model/LinkTrackUmlClick.php b/src/Domain/Analytics/Model/LinkTrackUmlClick.php index 3faf811d..93a4b487 100644 --- a/src/Domain/Analytics/Model/LinkTrackUmlClick.php +++ b/src/Domain/Analytics/Model/LinkTrackUmlClick.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Common\Model\Interfaces\Identity; #[ORM\Entity(repositoryClass: LinkTrackUmlClickRepository::class)] -#[ORM\Table(name: 'phplist_linktrack_uml_click')] +#[ORM\Table(name: 'linktrack_uml_click')] #[ORM\UniqueConstraint(name: 'phplist_linktrack_uml_click_miduidfwdid', columns: ['messageid', 'userid', 'forwardid'])] #[ORM\Index(name: 'phplist_linktrack_uml_click_midindex', columns: ['messageid'])] #[ORM\Index(name: 'phplist_linktrack_uml_click_miduidindex', columns: ['messageid', 'userid'])] diff --git a/src/Domain/Analytics/Model/LinkTrackUserClick.php b/src/Domain/Analytics/Model/LinkTrackUserClick.php index 27205cbb..3725cf15 100644 --- a/src/Domain/Analytics/Model/LinkTrackUserClick.php +++ b/src/Domain/Analytics/Model/LinkTrackUserClick.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Common\Model\Interfaces\DomainModel; #[ORM\Entity(repositoryClass: LinkTrackUserClickRepository::class)] -#[ORM\Table(name: 'phplist_linktrack_userclick')] +#[ORM\Table(name: 'linktrack_userclick')] #[ORM\Index(name: 'phplist_linktrack_userclick_linkindex', columns: ['linkid'])] #[ORM\Index(name: 'phplist_linktrack_userclick_linkuserindex', columns: ['linkid', 'userid'])] #[ORM\Index(name: 'phplist_linktrack_userclick_linkusermessageindex', columns: ['linkid', 'userid', 'messageid'])] diff --git a/src/Domain/Analytics/Model/UserMessageView.php b/src/Domain/Analytics/Model/UserMessageView.php index b391d3f3..66240fee 100644 --- a/src/Domain/Analytics/Model/UserMessageView.php +++ b/src/Domain/Analytics/Model/UserMessageView.php @@ -11,10 +11,11 @@ use PhpList\Core\Domain\Common\Model\Interfaces\Identity; #[ORM\Entity(repositoryClass: UserMessageViewRepository::class)] -#[ORM\Table(name: 'phplist_user_message_view')] +#[ORM\Table(name: 'user_message_view')] #[ORM\Index(name: 'phplist_user_message_view_msgidx', columns: ['messageid'])] #[ORM\Index(name: 'phplist_user_message_view_useridx', columns: ['userid'])] #[ORM\Index(name: 'phplist_user_message_view_usermsgidx', columns: ['userid', 'messageid'])] +// todo: #[ORM\Index(name: 'phplist_user_message_view_viewedidx', columns: ['viewed'])] class UserMessageView implements DomainModel, Identity { #[ORM\Id] diff --git a/src/Domain/Analytics/Model/UserStats.php b/src/Domain/Analytics/Model/UserStats.php index c7b4b97e..57e671f7 100644 --- a/src/Domain/Analytics/Model/UserStats.php +++ b/src/Domain/Analytics/Model/UserStats.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Common\Model\Interfaces\Identity; #[ORM\Entity(repositoryClass: UserStatsRepository::class)] -#[ORM\Table(name: 'phplist_userstats')] +#[ORM\Table(name: 'userstats')] #[ORM\UniqueConstraint(name: 'phplist_userstats_entry', columns: ['unixdate', 'item', 'listid'])] #[ORM\Index(name: 'phplist_userstats_dateindex', columns: ['unixdate'])] #[ORM\Index(name: 'phplist_userstats_itemindex', columns: ['item'])] diff --git a/src/Domain/Analytics/Repository/LinkTrackRepository.php b/src/Domain/Analytics/Repository/LinkTrackRepository.php index 3d322099..4a66b17d 100644 --- a/src/Domain/Analytics/Repository/LinkTrackRepository.php +++ b/src/Domain/Analytics/Repository/LinkTrackRepository.php @@ -5,6 +5,7 @@ namespace PhpList\Core\Domain\Analytics\Repository; use DateTimeInterface; +use Doctrine\DBAL\Exception; use PhpList\Core\Domain\Analytics\Model\LinkTrack; use PhpList\Core\Domain\Common\Repository\AbstractRepository; use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait; @@ -53,4 +54,56 @@ public function countBetween(DateTimeInterface $start, DateTimeInterface $end): ->getQuery() ->getSingleScalarResult(); } + + /** + * @return array counts keyed by 'Y-m-d' + * @throws Exception + */ + public function countGroupedByDay(DateTimeInterface $start, DateTimeInterface $end): array + { + $connection = $this->getEntityManager()->getConnection(); + $table = $this->getClassMetadata()->getTableName(); + + $sql = sprintf( + 'SELECT DATE(latestclick) AS day, COUNT(*) AS cnt FROM %s WHERE latestclick >= :start' + . ' AND latestclick <= :end GROUP BY DATE(latestclick)', + $table + ); + + $rows = $connection->executeQuery($sql, [ + 'start' => $start->format('Y-m-d H:i:s'), + 'end' => $end->format('Y-m-d H:i:s'), + ])->fetchAllAssociative(); + + $result = []; + foreach ($rows as $row) { + $result[(string) $row['day']] = (int) $row['cnt']; + } + return $result; + } + + /** + * @param int[] $messageIds + * @return array unique-clicker counts keyed by message id + */ + public function countUniqueClickersByMessageIds(array $messageIds): array + { + if (empty($messageIds)) { + return []; + } + + $rows = $this->createQueryBuilder('lt') + ->select('lt.messageId AS messageId, COUNT(DISTINCT lt.userId) AS cnt') + ->where('lt.messageId IN (:ids)') + ->setParameter('ids', $messageIds) + ->groupBy('lt.messageId') + ->getQuery() + ->getResult(); + + $result = []; + foreach ($rows as $row) { + $result[(int) $row['messageId']] = (int) $row['cnt']; + } + return $result; + } } diff --git a/src/Domain/Analytics/Repository/UserMessageViewRepository.php b/src/Domain/Analytics/Repository/UserMessageViewRepository.php index 5a08b569..2c232aa0 100644 --- a/src/Domain/Analytics/Repository/UserMessageViewRepository.php +++ b/src/Domain/Analytics/Repository/UserMessageViewRepository.php @@ -5,6 +5,7 @@ namespace PhpList\Core\Domain\Analytics\Repository; use DateTimeInterface; +use Doctrine\DBAL\Exception; use PhpList\Core\Domain\Common\Repository\AbstractRepository; use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait; use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface; @@ -51,4 +52,56 @@ public function countBetween(DateTimeInterface $start, DateTimeInterface $end): ->getQuery() ->getSingleScalarResult(); } + + /** + * @return array counts keyed by 'Y-m-d' + * @throws Exception + */ + public function countGroupedByDay(DateTimeInterface $start, DateTimeInterface $end): array + { + $connection = $this->getEntityManager()->getConnection(); + $table = $this->getClassMetadata()->getTableName(); + + $sql = sprintf( + 'SELECT DATE(viewed) AS day, COUNT(*) AS cnt FROM %s WHERE viewed >= :start AND viewed <= :end' + . ' GROUP BY DATE(viewed)', + $table + ); + + $rows = $connection->executeQuery($sql, [ + 'start' => $start->format('Y-m-d H:i:s'), + 'end' => $end->format('Y-m-d H:i:s'), + ])->fetchAllAssociative(); + + $result = []; + foreach ($rows as $row) { + $result[(string) $row['day']] = (int) $row['cnt']; + } + return $result; + } + + /** + * @param int[] $messageIds + * @return array view counts keyed by message id + */ + public function countByMessageIds(array $messageIds): array + { + if (empty($messageIds)) { + return []; + } + + $rows = $this->createQueryBuilder('umv') + ->select('umv.messageId AS messageId, COUNT(umv.id) AS cnt') + ->where('umv.messageId IN (:ids)') + ->setParameter('ids', $messageIds) + ->groupBy('umv.messageId') + ->getQuery() + ->getResult(); + + $result = []; + foreach ($rows as $row) { + $result[(int) $row['messageId']] = (int) $row['cnt']; + } + return $result; + } } diff --git a/src/Domain/Analytics/Service/AnalyticsService.php b/src/Domain/Analytics/Service/AnalyticsService.php index 853c2f9e..f9f52721 100644 --- a/src/Domain/Analytics/Service/AnalyticsService.php +++ b/src/Domain/Analytics/Service/AnalyticsService.php @@ -430,18 +430,21 @@ public function getTopLocalParts(int $limit = 25): array public function getCampaignPerformance(): array { - $performance = []; $endDate = new DateTimeImmutable('today 23:59:59'); $startDate = $endDate->sub(new DateInterval('P29D'))->modify('00:00:00'); + $opensByDay = $this->userMessageViewManager->countViewsGroupedByDay($startDate, $endDate); + $clicksByDay = $this->linkTrackManager->countClicksGroupedByDay($startDate, $endDate); + + $performance = []; for ($index = 0; $index < 30; $index++) { - $dayStart = $startDate->add(new DateInterval('P' . $index . 'D')); - $dayEnd = $dayStart->modify('23:59:59'); + $day = $startDate->add(new DateInterval('P' . $index . 'D')); + $dateKey = $day->format('Y-m-d'); $performance[] = [ - 'date' => $dayStart->format('Y-m-d'), - 'opens' => $this->userMessageViewManager->countViewsBetween($dayStart, $dayEnd), - 'clicks' => $this->linkTrackManager->countClicksBetween($dayStart, $dayEnd), + 'date' => $dateKey, + 'opens' => $opensByDay[$dateKey] ?? 0, + 'clicks' => $clicksByDay[$dateKey] ?? 0, ]; } @@ -459,16 +462,16 @@ public function getRecentCampaigns(int $limit = 5): array $messages = $this->messageRepository ->getFilteredAfterId((new MessageFilter())->setLastId(0)->setLimit($limit)) ->getItems(); + + $messageIds = array_map(static fn ($message) => $message->getId(), $messages); + $viewCounts = $this->userMessageViewManager->countViewsByMessageIds($messageIds); + $uniqueClickCounts = $this->linkTrackManager->countUniqueClickersByMessageIds($messageIds); + $recentCampaigns = []; foreach ($messages as $message) { - $views = $this->userMessageViewManager->countViewsByMessageId($message->getId()); - $linkTracks = $this->linkTrackManager->getLinkTracksByMessageId($message->getId()); - - $uniqueClickers = []; - foreach ($linkTracks as $linkTrack) { - $uniqueClickers[$linkTrack->getUserId()] = true; - } - $uniqueClicks = count($uniqueClickers); + $id = $message->getId(); + $views = $viewCounts[$id] ?? 0; + $uniqueClicks = $uniqueClickCounts[$id] ?? 0; $sentCount = $message->getMetadata()->getViews() + $message->getMetadata()->getBounceCount(); diff --git a/src/Domain/Analytics/Service/Manager/LinkTrackManager.php b/src/Domain/Analytics/Service/Manager/LinkTrackManager.php index 9f657ebc..0775ec1d 100644 --- a/src/Domain/Analytics/Service/Manager/LinkTrackManager.php +++ b/src/Domain/Analytics/Service/Manager/LinkTrackManager.php @@ -34,4 +34,14 @@ public function countClicksBetween(DateTimeInterface $start, DateTimeInterface $ { return $this->linkTrackRepository->countBetween($start, $end); } + + public function countClicksGroupedByDay(DateTimeInterface $start, DateTimeInterface $end): array + { + return $this->linkTrackRepository->countGroupedByDay($start, $end); + } + + public function countUniqueClickersByMessageIds(array $messageIds): array + { + return $this->linkTrackRepository->countUniqueClickersByMessageIds($messageIds); + } } diff --git a/src/Domain/Analytics/Service/Manager/UserMessageViewManager.php b/src/Domain/Analytics/Service/Manager/UserMessageViewManager.php index 6dce3cf7..52192651 100644 --- a/src/Domain/Analytics/Service/Manager/UserMessageViewManager.php +++ b/src/Domain/Analytics/Service/Manager/UserMessageViewManager.php @@ -34,4 +34,14 @@ public function countViewsBetween(DateTimeInterface $start, DateTimeInterface $e { return $this->userMessageViewRepository->countBetween($start, $end); } + + public function countViewsGroupedByDay(DateTimeInterface $start, DateTimeInterface $end): array + { + return $this->userMessageViewRepository->countGroupedByDay($start, $end); + } + + public function countViewsByMessageIds(array $messageIds): array + { + return $this->userMessageViewRepository->countByMessageIds($messageIds); + } } diff --git a/src/Domain/Configuration/Model/Config.php b/src/Domain/Configuration/Model/Config.php index 00f0a6c5..80f60f19 100644 --- a/src/Domain/Configuration/Model/Config.php +++ b/src/Domain/Configuration/Model/Config.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Configuration\Repository\ConfigRepository; #[ORM\Entity(repositoryClass: ConfigRepository::class)] -#[ORM\Table(name: 'phplist_config')] +#[ORM\Table(name: 'config')] class Config implements DomainModel { #[ORM\Id] diff --git a/src/Domain/Configuration/Model/EventLog.php b/src/Domain/Configuration/Model/EventLog.php index c0cff22b..7e1ac3af 100644 --- a/src/Domain/Configuration/Model/EventLog.php +++ b/src/Domain/Configuration/Model/EventLog.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Configuration\Repository\EventLogRepository; #[ORM\Entity(repositoryClass: EventLogRepository::class)] -#[ORM\Table(name: 'phplist_eventlog')] +#[ORM\Table(name: 'eventlog')] #[ORM\Index(name: 'phplist_eventlog_enteredidx', columns: ['entered'])] #[ORM\Index(name: 'phplist_eventlog_pageidx', columns: ['page'])] #[ORM\HasLifecycleCallbacks] diff --git a/src/Domain/Configuration/Model/I18n.php b/src/Domain/Configuration/Model/I18n.php index 72397bb4..0f709259 100644 --- a/src/Domain/Configuration/Model/I18n.php +++ b/src/Domain/Configuration/Model/I18n.php @@ -14,7 +14,7 @@ * Symfony\Contracts\Translation will be used instead. */ #[ORM\Entity(repositoryClass: I18nRepository::class)] -#[ORM\Table(name: 'phplist_i18n')] +#[ORM\Table(name: 'i18n')] #[ORM\UniqueConstraint(name: 'phplist_i18n_lanorigunq', columns: ['lan', 'original'])] #[ORM\Index(name: 'phplist_i18n_lanorigidx', columns: ['lan', 'original'])] class I18n implements DomainModel diff --git a/src/Domain/Configuration/Model/UrlCache.php b/src/Domain/Configuration/Model/UrlCache.php index b6d032b9..a8394212 100644 --- a/src/Domain/Configuration/Model/UrlCache.php +++ b/src/Domain/Configuration/Model/UrlCache.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Configuration\Repository\UrlCacheRepository; #[ORM\Entity(repositoryClass: UrlCacheRepository::class)] -#[ORM\Table(name: 'phplist_urlcache')] +#[ORM\Table(name: 'urlcache')] #[ORM\Index(name: 'phplist_urlcache_urlindex', columns: ['url'])] #[ORM\HasLifecycleCallbacks] class UrlCache implements DomainModel, Identity diff --git a/src/Domain/Configuration/Service/Provider/ConfigProvider.php b/src/Domain/Configuration/Service/Provider/ConfigProvider.php index 3b22285f..2890a86d 100644 --- a/src/Domain/Configuration/Service/Provider/ConfigProvider.php +++ b/src/Domain/Configuration/Service/Provider/ConfigProvider.php @@ -33,7 +33,7 @@ public function isEnabled(ConfigOption $key): bool if (!in_array($key, $this->booleanValues, true)) { throw new InvalidArgumentException('Invalid boolean value key'); } - $config = $this->configRepository->findOneBy(['item' => $key->value]); + $config = $this->configRepository->findOneBy(['key' => $key->value]); if ($config !== null) { return filter_var($config->getValue(), FILTER_VALIDATE_BOOLEAN); diff --git a/src/Domain/Identity/Command/ImportDefaultsCommand.php b/src/Domain/Identity/Command/ImportDefaultsCommand.php index 47ac4295..c91457c3 100644 --- a/src/Domain/Identity/Command/ImportDefaultsCommand.php +++ b/src/Domain/Identity/Command/ImportDefaultsCommand.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; +use Symfony\Component\DependencyInjection\Attribute\Autowire; #[AsCommand( name: 'phplist:defaults:import', @@ -29,6 +30,8 @@ public function __construct( private readonly AdministratorRepository $administratorRepository, private readonly AdministratorManager $administratorManager, private readonly EntityManagerInterface $entityManager, + #[Autowire('%app.default_admin_password%')] + private readonly string $defaultAdminPassword = '' ) { parent::__construct(); } @@ -37,15 +40,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $login = self::DEFAULT_LOGIN; $email = self::DEFAULT_EMAIL; - $envPassword = getenv('PHPLIST_ADMIN_PASSWORD'); - $envPassword = is_string($envPassword) && trim($envPassword) !== '' ? $envPassword : null; + $password = $this->defaultAdminPassword !== '' ? $this->defaultAdminPassword : null; $allPrivileges = $this->allPrivilegesGranted(); $existing = $this->administratorRepository->findOneBy(['loginName' => $login]); if ($existing === null) { // If creating the default admin, require a password. Prefer env var, else prompt for input. - $password = $envPassword; if ($password === null) { /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); diff --git a/src/Domain/Identity/Model/AdminAttributeDefinition.php b/src/Domain/Identity/Model/AdminAttributeDefinition.php index 3fe45e76..c2b20d0b 100644 --- a/src/Domain/Identity/Model/AdminAttributeDefinition.php +++ b/src/Domain/Identity/Model/AdminAttributeDefinition.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Identity\Repository\AdminAttributeDefinitionRepository; #[ORM\Entity(repositoryClass: AdminAttributeDefinitionRepository::class)] -#[ORM\Table(name: 'phplist_adminattribute')] +#[ORM\Table(name: 'adminattribute')] #[ORM\HasLifecycleCallbacks] class AdminAttributeDefinition implements DomainModel, Identity { diff --git a/src/Domain/Identity/Model/AdminAttributeValue.php b/src/Domain/Identity/Model/AdminAttributeValue.php index 3d99ba73..35188ec6 100644 --- a/src/Domain/Identity/Model/AdminAttributeValue.php +++ b/src/Domain/Identity/Model/AdminAttributeValue.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Identity\Repository\AdminAttributeValueRepository; #[ORM\Entity(repositoryClass: AdminAttributeValueRepository::class)] -#[ORM\Table(name: 'phplist_admin_attribute')] +#[ORM\Table(name: 'admin_attribute')] #[ORM\HasLifecycleCallbacks] class AdminAttributeValue implements DomainModel { diff --git a/src/Domain/Identity/Model/AdminLogin.php b/src/Domain/Identity/Model/AdminLogin.php index 91be3331..74d9abee 100644 --- a/src/Domain/Identity/Model/AdminLogin.php +++ b/src/Domain/Identity/Model/AdminLogin.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Identity\Repository\AdminLoginRepository; #[ORM\Entity(repositoryClass: AdminLoginRepository::class)] -#[ORM\Table(name: 'phplist_admin_login')] +#[ORM\Table(name: 'admin_login')] #[ORM\HasLifecycleCallbacks] class AdminLogin implements DomainModel, Identity { diff --git a/src/Domain/Identity/Model/AdminPasswordRequest.php b/src/Domain/Identity/Model/AdminPasswordRequest.php index 0d761adf..230e675a 100644 --- a/src/Domain/Identity/Model/AdminPasswordRequest.php +++ b/src/Domain/Identity/Model/AdminPasswordRequest.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Identity\Repository\AdminPasswordRequestRepository; #[ORM\Entity(repositoryClass: AdminPasswordRequestRepository::class)] -#[ORM\Table(name: 'phplist_admin_password_request')] +#[ORM\Table(name: 'admin_password_request')] class AdminPasswordRequest implements DomainModel, Identity { #[ORM\Id] diff --git a/src/Domain/Identity/Model/Administrator.php b/src/Domain/Identity/Model/Administrator.php index 2f3de5eb..f6c9ba05 100644 --- a/src/Domain/Identity/Model/Administrator.php +++ b/src/Domain/Identity/Model/Administrator.php @@ -25,7 +25,7 @@ * @author Tatevik Grigoryan */ #[ORM\Entity(repositoryClass: AdministratorRepository::class)] -#[ORM\Table(name: 'phplist_admin')] +#[ORM\Table(name: 'admin')] #[ORM\UniqueConstraint(name: 'phplist_admin_loginnameidx', columns: ['loginname'])] #[ORM\HasLifecycleCallbacks] class Administrator implements DomainModel, Identity, CreationDate, ModificationDate diff --git a/src/Domain/Identity/Model/AdministratorToken.php b/src/Domain/Identity/Model/AdministratorToken.php index 4e37b2b5..3d9da22d 100644 --- a/src/Domain/Identity/Model/AdministratorToken.php +++ b/src/Domain/Identity/Model/AdministratorToken.php @@ -19,7 +19,7 @@ * @author Tateik Grigoryan */ #[ORM\Entity(repositoryClass: AdministratorTokenRepository::class)] -#[ORM\Table(name: 'phplist_admintoken')] +#[ORM\Table(name: 'admintoken')] #[ORM\HasLifecycleCallbacks] class AdministratorToken implements DomainModel, Identity, CreationDate { diff --git a/src/Domain/Identity/Repository/AdministratorRepository.php b/src/Domain/Identity/Repository/AdministratorRepository.php index 640a0a55..0bdae5b6 100644 --- a/src/Domain/Identity/Repository/AdministratorRepository.php +++ b/src/Domain/Identity/Repository/AdministratorRepository.php @@ -45,15 +45,27 @@ public function __construct( */ public function findOneByLoginCredentials(string $loginName, string $plainTextPassword): ?Administrator { - $passwordHash = $this->hashGenerator->createPasswordHash($plainTextPassword); - - return $this->findOneBy( + /** @var Administrator|null $administrator */ + $administrator = $this->findOneBy( [ 'loginName' => $loginName, - 'passwordHash' => $passwordHash, 'superUser' => true, ] ); + + $passwordHash = $administrator?->getPasswordHash(); + if ($administrator === null || $passwordHash === null + || !$this->hashGenerator->verifyPassword($plainTextPassword, $passwordHash) + ) { + return null; + } + + if ($this->hashGenerator->isLegacyHash($passwordHash)) { + $administrator->setPasswordHash($this->hashGenerator->createPasswordHash($plainTextPassword)); + $this->save($administrator); + } + + return $administrator; } /** @return Administrator[] */ diff --git a/src/Domain/Messaging/Command/ProcessQueueCommand.php b/src/Domain/Messaging/Command/ProcessQueueCommand.php index 080c24cb..69bf967b 100644 --- a/src/Domain/Messaging/Command/ProcessQueueCommand.php +++ b/src/Domain/Messaging/Command/ProcessQueueCommand.php @@ -27,31 +27,16 @@ )] class ProcessQueueCommand extends Command { - private MessageRepository $messageRepository; - private LockFactory $lockFactory; - private MessageProcessingPreparator $messagePreparator; - private MessageBusInterface $messageBus; - private ConfigProvider $configProvider; - private TranslatorInterface $translator; - private EntityManagerInterface $entityManager; - public function __construct( - MessageRepository $messageRepository, - LockFactory $lockFactory, - MessageProcessingPreparator $messagePreparator, - MessageBusInterface $messageBus, - ConfigProvider $configProvider, - TranslatorInterface $translator, - EntityManagerInterface $entityManager, + private readonly MessageRepository $messageRepository, + private readonly LockFactory $lockFactory, + private readonly MessageProcessingPreparator $messagePreparator, + private readonly MessageBusInterface $messageBus, + private readonly ConfigProvider $configProvider, + private readonly TranslatorInterface $translator, + private readonly EntityManagerInterface $entityManager, ) { parent::__construct(); - $this->messageRepository = $messageRepository; - $this->lockFactory = $lockFactory; - $this->messagePreparator = $messagePreparator; - $this->messageBus = $messageBus; - $this->configProvider = $configProvider; - $this->translator = $translator; - $this->entityManager = $entityManager; } protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Domain/Messaging/Command/SendTestEmailCommand.php b/src/Domain/Messaging/Command/SendTestEmailCommand.php index e9670239..2766af9d 100644 --- a/src/Domain/Messaging/Command/SendTestEmailCommand.php +++ b/src/Domain/Messaging/Command/SendTestEmailCommand.php @@ -21,14 +21,11 @@ )] class SendTestEmailCommand extends Command { - private EmailService $emailService; - private TranslatorInterface $translator; - - public function __construct(EmailService $emailService, TranslatorInterface $translator) - { + public function __construct( + private readonly EmailService $emailService, + private readonly TranslatorInterface $translator + ) { parent::__construct(); - $this->emailService = $emailService; - $this->translator = $translator; } protected function configure(): void diff --git a/src/Domain/Messaging/Model/Attachment.php b/src/Domain/Messaging/Model/Attachment.php index d49cd386..a8b38b4b 100644 --- a/src/Domain/Messaging/Model/Attachment.php +++ b/src/Domain/Messaging/Model/Attachment.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Messaging\Repository\AttachmentRepository; #[ORM\Entity(repositoryClass: AttachmentRepository::class)] -#[ORM\Table(name: 'phplist_attachment')] +#[ORM\Table(name: 'attachment')] class Attachment implements DomainModel, Identity { public const FORWARD = 'forwarded'; diff --git a/src/Domain/Messaging/Model/Bounce.php b/src/Domain/Messaging/Model/Bounce.php index 54e5895d..071b869f 100644 --- a/src/Domain/Messaging/Model/Bounce.php +++ b/src/Domain/Messaging/Model/Bounce.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Messaging\Repository\BounceRepository; #[ORM\Entity(repositoryClass: BounceRepository::class)] -#[ORM\Table(name: 'phplist_bounce')] +#[ORM\Table(name: 'bounce')] #[ORM\Index(name: 'phplist_bounce_dateindex', columns: ['date'])] #[ORM\Index(name: 'phplist_bounce_statusidx', columns: ['status'])] class Bounce implements DomainModel, Identity diff --git a/src/Domain/Messaging/Model/BounceRegex.php b/src/Domain/Messaging/Model/BounceRegex.php index c54ca7c0..5d0d0521 100644 --- a/src/Domain/Messaging/Model/BounceRegex.php +++ b/src/Domain/Messaging/Model/BounceRegex.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Messaging\Repository\BounceRegexRepository; #[ORM\Entity(repositoryClass: BounceRegexRepository::class)] -#[ORM\Table(name: 'phplist_bounceregex')] +#[ORM\Table(name: 'bounceregex')] #[ORM\UniqueConstraint(name: 'phplist_bounceregex_regex', columns: ['regexhash'])] class BounceRegex implements DomainModel, Identity { diff --git a/src/Domain/Messaging/Model/BounceRegexBounce.php b/src/Domain/Messaging/Model/BounceRegexBounce.php index e815cd1f..c50d20d5 100644 --- a/src/Domain/Messaging/Model/BounceRegexBounce.php +++ b/src/Domain/Messaging/Model/BounceRegexBounce.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Messaging\Repository\BounceRegexBounceRepository; #[ORM\Entity(repositoryClass: BounceRegexBounceRepository::class)] -#[ORM\Table(name: 'phplist_bounceregex_bounce')] +#[ORM\Table(name: 'bounceregex_bounce')] class BounceRegexBounce implements DomainModel { #[ORM\Id] diff --git a/src/Domain/Messaging/Model/ListMessage.php b/src/Domain/Messaging/Model/ListMessage.php index 3a5d655a..d624b699 100644 --- a/src/Domain/Messaging/Model/ListMessage.php +++ b/src/Domain/Messaging/Model/ListMessage.php @@ -14,7 +14,7 @@ use PhpList\Core\Domain\Subscription\Model\SubscriberList; #[ORM\Entity(repositoryClass: ListMessageRepository::class)] -#[ORM\Table(name: 'phplist_listmessage')] +#[ORM\Table(name: 'listmessage')] #[ORM\UniqueConstraint(name: 'phplist_listmessage_messageid', columns: ['messageid', 'listid'])] #[ORM\Index(name: 'phplist_listmessage_listmessageidx', columns: ['listid', 'messageid'])] #[ORM\HasLifecycleCallbacks] diff --git a/src/Domain/Messaging/Model/Message.php b/src/Domain/Messaging/Model/Message.php index 4d5f4e8f..94faee06 100644 --- a/src/Domain/Messaging/Model/Message.php +++ b/src/Domain/Messaging/Model/Message.php @@ -22,8 +22,9 @@ use PhpList\Core\Domain\Messaging\Repository\MessageRepository; #[ORM\Entity(repositoryClass: MessageRepository::class)] -#[ORM\Table(name: 'phplist_message')] +#[ORM\Table(name: 'message')] #[ORM\Index(name: 'phplist_message_uuididx', columns: ['uuid'])] +#[ORM\Index(name: 'phplist_message_sentidx', columns: ['sent'])] #[ORM\HasLifecycleCallbacks] class Message implements DomainModel, Identity, ModificationDate, OwnableInterface { diff --git a/src/Domain/Messaging/Model/Message/MessageStatus.php b/src/Domain/Messaging/Model/Message/MessageStatus.php index 789f07c2..7f6e0daa 100644 --- a/src/Domain/Messaging/Model/Message/MessageStatus.php +++ b/src/Domain/Messaging/Model/Message/MessageStatus.php @@ -12,7 +12,6 @@ enum MessageStatus: string case InProcess = 'inprocess'; case Sent = 'sent'; case Suspended = 'suspended'; - case Requeued = 'requeued'; /** * Allowed transitions for each state @@ -23,12 +22,10 @@ public function allowedTransitions(): array { return match ($this) { self::Draft => [self::Prepared, self::Submitted], - self::Suspended => [self::Submitted, self::Requeued], + self::Suspended, self::Sent => [self::Submitted], self::Submitted => [self::Prepared, self::InProcess, self::Suspended], self::Prepared => [self::InProcess, self::Suspended], self::InProcess => [self::Sent, self::Suspended, self::Submitted], - self::Requeued => [self::InProcess, self::Suspended], - self::Sent => [self::Requeued], }; } diff --git a/src/Domain/Messaging/Model/MessageAttachment.php b/src/Domain/Messaging/Model/MessageAttachment.php index e26d0d87..2007ad5c 100644 --- a/src/Domain/Messaging/Model/MessageAttachment.php +++ b/src/Domain/Messaging/Model/MessageAttachment.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Messaging\Repository\MessageAttachmentRepository; #[ORM\Entity(repositoryClass: MessageAttachmentRepository::class)] -#[ORM\Table(name: 'phplist_message_attachment')] +#[ORM\Table(name: 'message_attachment')] #[ORM\Index(name: 'phplist_message_attachment_messageattidx', columns: ['messageid', 'attachmentid'])] #[ORM\Index(name: 'phplist_message_attachment_messageidx', columns: ['messageid'])] class MessageAttachment implements Identity diff --git a/src/Domain/Messaging/Model/MessageData.php b/src/Domain/Messaging/Model/MessageData.php index 56744251..d364889c 100644 --- a/src/Domain/Messaging/Model/MessageData.php +++ b/src/Domain/Messaging/Model/MessageData.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Messaging\Repository\MessageDataRepository; #[ORM\Entity(repositoryClass: MessageDataRepository::class)] -#[ORM\Table(name: 'phplist_messagedata')] +#[ORM\Table(name: 'messagedata')] class MessageData implements DomainModel { #[ORM\Id] diff --git a/src/Domain/Messaging/Model/SendProcess.php b/src/Domain/Messaging/Model/SendProcess.php index 5faeaf35..14abe737 100644 --- a/src/Domain/Messaging/Model/SendProcess.php +++ b/src/Domain/Messaging/Model/SendProcess.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Messaging\Repository\SendProcessRepository; #[ORM\Entity(repositoryClass: SendProcessRepository::class)] -#[ORM\Table(name: 'phplist_sendprocess')] +#[ORM\Table(name: 'sendprocess')] #[ORM\HasLifecycleCallbacks] class SendProcess implements DomainModel, Identity, ModificationDate { diff --git a/src/Domain/Messaging/Model/Template.php b/src/Domain/Messaging/Model/Template.php index dc1b67a0..3bbd8c8c 100644 --- a/src/Domain/Messaging/Model/Template.php +++ b/src/Domain/Messaging/Model/Template.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Messaging\Repository\TemplateRepository; #[ORM\Entity(repositoryClass: TemplateRepository::class)] -#[ORM\Table(name: 'phplist_template')] +#[ORM\Table(name: 'template')] #[ORM\UniqueConstraint(name: 'phplist_template_title', columns: ['title'])] class Template implements DomainModel, Identity { diff --git a/src/Domain/Messaging/Model/TemplateImage.php b/src/Domain/Messaging/Model/TemplateImage.php index c1c5c8c4..a0da4692 100644 --- a/src/Domain/Messaging/Model/TemplateImage.php +++ b/src/Domain/Messaging/Model/TemplateImage.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Messaging\Repository\TemplateImageRepository; #[ORM\Entity(repositoryClass: TemplateImageRepository::class)] -#[ORM\Table(name: 'phplist_templateimage')] +#[ORM\Table(name: 'templateimage')] #[ORM\Index(name: 'phplist_templateimage_templateidx', columns: ['template'])] class TemplateImage implements DomainModel, Identity { diff --git a/src/Domain/Messaging/Model/UserMessage.php b/src/Domain/Messaging/Model/UserMessage.php index d5fe202c..93b457f3 100644 --- a/src/Domain/Messaging/Model/UserMessage.php +++ b/src/Domain/Messaging/Model/UserMessage.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Subscription\Model\Subscriber; #[ORM\Entity(repositoryClass: UserMessageRepository::class)] -#[ORM\Table(name: 'phplist_usermessage')] +#[ORM\Table(name: 'usermessage')] #[ORM\Index(name: 'phplist_usermessage_enteredindex', columns: ['entered'])] #[ORM\Index(name: 'phplist_usermessage_messageidindex', columns: ['messageid'])] #[ORM\Index(name: 'phplist_usermessage_statusidx', columns: ['status'])] diff --git a/src/Domain/Messaging/Model/UserMessageBounce.php b/src/Domain/Messaging/Model/UserMessageBounce.php index 3b58bf47..2a7ef519 100644 --- a/src/Domain/Messaging/Model/UserMessageBounce.php +++ b/src/Domain/Messaging/Model/UserMessageBounce.php @@ -11,11 +11,12 @@ use PhpList\Core\Domain\Messaging\Repository\UserMessageBounceRepository; #[ORM\Entity(repositoryClass: UserMessageBounceRepository::class)] -#[ORM\Table(name: 'phplist_user_message_bounce')] +#[ORM\Table(name: 'user_message_bounce')] #[ORM\Index(name: 'phplist_user_message_bounce_bounceidx', columns: ['bounce'])] #[ORM\Index(name: 'phplist_user_message_bounce_msgidx', columns: ['message'])] #[ORM\Index(name: 'phplist_user_message_bounce_umbindex', columns: ['user', 'message', 'bounce'])] #[ORM\Index(name: 'phplist_user_message_bounce_useridx', columns: ['user'])] +// todo: #[ORM\Index(name: 'phplist_user_message_bounce_timeidx', columns: ['time'])] class UserMessageBounce implements DomainModel, Identity { #[ORM\Id] diff --git a/src/Domain/Messaging/Model/UserMessageForward.php b/src/Domain/Messaging/Model/UserMessageForward.php index 3b920189..1dd32806 100644 --- a/src/Domain/Messaging/Model/UserMessageForward.php +++ b/src/Domain/Messaging/Model/UserMessageForward.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Messaging\Repository\UserMessageForwardRepository; #[ORM\Entity(repositoryClass: UserMessageForwardRepository::class)] -#[ORM\Table(name: 'phplist_user_message_forward')] +#[ORM\Table(name: 'user_message_forward')] #[ORM\Index(name: 'phplist_user_message_forward_messageidx', columns: ['message'])] #[ORM\Index(name: 'phplist_user_message_forward_useridx', columns: ['user'])] #[ORM\Index(name: 'phplist_user_message_forward_usermessageidx', columns: ['user', 'message'])] diff --git a/src/Domain/Messaging/Repository/MessageRepository.php b/src/Domain/Messaging/Repository/MessageRepository.php index d18ce68b..cc22602c 100644 --- a/src/Domain/Messaging/Repository/MessageRepository.php +++ b/src/Domain/Messaging/Repository/MessageRepository.php @@ -116,7 +116,7 @@ public function getByStatusAndEmbargo(Message\MessageStatus $status, DateTimeImm { return $this->createQueryBuilder('m') ->where('m.metadata.status = :status') - ->andWhere('m.schedule.embargo IS NULL OR m.embargo <= :embargo') + ->andWhere('m.schedule.embargo IS NULL OR m.schedule.embargo <= :embargo') ->setParameter('status', $status->value) ->setParameter('embargo', $embargo) ->getQuery() diff --git a/src/Domain/Subscription/Model/SubscribePage.php b/src/Domain/Subscription/Model/SubscribePage.php index 3b484920..bc4ea54f 100644 --- a/src/Domain/Subscription/Model/SubscribePage.php +++ b/src/Domain/Subscription/Model/SubscribePage.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository; #[ORM\Entity(repositoryClass: SubscriberPageRepository::class)] -#[ORM\Table(name: 'phplist_subscribepage')] +#[ORM\Table(name: 'subscribepage')] class SubscribePage implements DomainModel, Identity, OwnableInterface { #[ORM\Id] diff --git a/src/Domain/Subscription/Model/SubscribePageData.php b/src/Domain/Subscription/Model/SubscribePageData.php index 7d8dcd4e..8b94e729 100644 --- a/src/Domain/Subscription/Model/SubscribePageData.php +++ b/src/Domain/Subscription/Model/SubscribePageData.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository; #[ORM\Entity(repositoryClass: SubscriberPageDataRepository::class)] -#[ORM\Table(name: 'phplist_subscribepage_data')] +#[ORM\Table(name: 'subscribepage_data')] class SubscribePageData implements DomainModel { #[ORM\Id] diff --git a/src/Domain/Subscription/Model/Subscriber.php b/src/Domain/Subscription/Model/Subscriber.php index 97d45b83..8eda5ed6 100644 --- a/src/Domain/Subscription/Model/Subscriber.php +++ b/src/Domain/Subscription/Model/Subscriber.php @@ -24,7 +24,7 @@ * @SuppressWarnings(PHPMD.ExcessivePublicCount) */ #[ORM\Entity(repositoryClass: SubscriberRepository::class)] -#[ORM\Table(name: 'phplist_user_user')] +#[ORM\Table(name: 'user_user')] #[ORM\Index(name: 'phplist_user_user_idxuniqid', columns: ['uniqid'])] #[ORM\Index(name: 'phplist_user_user_enteredindex', columns: ['entered'])] #[ORM\Index(name: 'phplist_user_user_confidx', columns: ['confirmed'])] diff --git a/src/Domain/Subscription/Model/SubscriberAttributeDefinition.php b/src/Domain/Subscription/Model/SubscriberAttributeDefinition.php index 26b7a786..dbe397d2 100644 --- a/src/Domain/Subscription/Model/SubscriberAttributeDefinition.php +++ b/src/Domain/Subscription/Model/SubscriberAttributeDefinition.php @@ -12,7 +12,7 @@ use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository; #[ORM\Entity(repositoryClass: SubscriberAttributeDefinitionRepository::class)] -#[ORM\Table(name: 'phplist_user_attribute')] +#[ORM\Table(name: 'user_attribute')] #[ORM\Index(name: 'phplist_user_attribute_idnameindex', columns: ['id', 'name'])] #[ORM\Index(name: 'phplist_user_attribute_nameindex', columns: ['name'])] class SubscriberAttributeDefinition implements DomainModel, Identity diff --git a/src/Domain/Subscription/Model/SubscriberAttributeValue.php b/src/Domain/Subscription/Model/SubscriberAttributeValue.php index 3af333ff..6678b489 100644 --- a/src/Domain/Subscription/Model/SubscriberAttributeValue.php +++ b/src/Domain/Subscription/Model/SubscriberAttributeValue.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeValueRepository; #[ORM\Entity(repositoryClass: SubscriberAttributeValueRepository::class)] -#[ORM\Table(name: 'phplist_user_user_attribute')] +#[ORM\Table(name: 'user_user_attribute')] #[ORM\Index(name: 'phplist_user_user_attribute_attindex', columns: ['attributeid'])] #[ORM\Index(name: 'phplist_user_user_attribute_attuserid', columns: ['userid', 'attributeid'])] #[ORM\Index(name: 'phplist_user_user_attribute_userindex', columns: ['userid'])] diff --git a/src/Domain/Subscription/Model/SubscriberHistory.php b/src/Domain/Subscription/Model/SubscriberHistory.php index 1799c01b..08f4f974 100644 --- a/src/Domain/Subscription/Model/SubscriberHistory.php +++ b/src/Domain/Subscription/Model/SubscriberHistory.php @@ -11,7 +11,7 @@ use PhpList\Core\Domain\Subscription\Repository\SubscriberHistoryRepository; #[ORM\Entity(repositoryClass: SubscriberHistoryRepository::class)] -#[ORM\Table(name: 'phplist_user_user_history')] +#[ORM\Table(name: 'user_user_history')] #[ORM\Index(name: 'phplist_user_user_history_dateidx', columns: ['date'])] #[ORM\Index(name: 'phplist_user_user_history_userididx', columns: ['userid'])] class SubscriberHistory implements DomainModel, Identity diff --git a/src/Domain/Subscription/Model/SubscriberList.php b/src/Domain/Subscription/Model/SubscriberList.php index 621f855e..d1d2a071 100644 --- a/src/Domain/Subscription/Model/SubscriberList.php +++ b/src/Domain/Subscription/Model/SubscriberList.php @@ -25,7 +25,7 @@ * @author Tatevik Grigoryan */ #[ORM\Entity(repositoryClass: SubscriberListRepository::class)] -#[ORM\Table(name: 'phplist_list')] +#[ORM\Table(name: 'list')] #[ORM\Index(name: 'phplist_list_nameidx', columns: ['name'])] #[ORM\Index(name: 'phplist_list_listorderidx', columns: ['listorder'])] #[ORM\HasLifecycleCallbacks] diff --git a/src/Domain/Subscription/Model/Subscription.php b/src/Domain/Subscription/Model/Subscription.php index fe4b5e2a..98df4703 100644 --- a/src/Domain/Subscription/Model/Subscription.php +++ b/src/Domain/Subscription/Model/Subscription.php @@ -22,7 +22,7 @@ * @author Tatevik Grigoryan */ #[ORM\Entity(repositoryClass: SubscriptionRepository::class)] -#[ORM\Table(name: 'phplist_listuser')] +#[ORM\Table(name: 'listuser')] #[ORM\Index(name: 'phplist_listuser_userenteredidx', columns: ['userid', 'entered'])] #[ORM\Index(name: 'phplist_listuser_userlistenteredidx', columns: ['userid', 'entered', 'listid'])] #[ORM\Index(name: 'phplist_listuser_useridx', columns: ['userid'])] diff --git a/src/Domain/Subscription/Model/UserBlacklist.php b/src/Domain/Subscription/Model/UserBlacklist.php index 9b150686..f940f79b 100644 --- a/src/Domain/Subscription/Model/UserBlacklist.php +++ b/src/Domain/Subscription/Model/UserBlacklist.php @@ -10,7 +10,7 @@ use PhpList\Core\Domain\Subscription\Repository\UserBlacklistRepository; #[ORM\Entity(repositoryClass: UserBlacklistRepository::class)] -#[ORM\Table(name: 'phplist_user_blacklist')] +#[ORM\Table(name: 'user_blacklist')] #[ORM\Index(name: 'phplist_user_blacklist_emailidx', columns: ['email'])] class UserBlacklist implements DomainModel { diff --git a/src/Domain/Subscription/Model/UserBlacklistData.php b/src/Domain/Subscription/Model/UserBlacklistData.php index ff133161..52725e1b 100644 --- a/src/Domain/Subscription/Model/UserBlacklistData.php +++ b/src/Domain/Subscription/Model/UserBlacklistData.php @@ -9,7 +9,7 @@ use PhpList\Core\Domain\Subscription\Repository\UserBlacklistDataRepository; #[ORM\Entity(repositoryClass: UserBlacklistDataRepository::class)] -#[ORM\Table(name: 'phplist_user_blacklist_data')] +#[ORM\Table(name: 'user_blacklist_data')] #[ORM\Index(name: 'phplist_user_blacklist_data_emailidx', columns: ['email'])] #[ORM\Index(name: 'phplist_user_blacklist_data_emailnameidx', columns: ['email', 'name'])] class UserBlacklistData implements DomainModel diff --git a/src/Migrations/AbstractPrefixedMigration.php b/src/Migrations/AbstractPrefixedMigration.php new file mode 100644 index 00000000..96959f5f --- /dev/null +++ b/src/Migrations/AbstractPrefixedMigration.php @@ -0,0 +1,70 @@ +getTablePrefix(), + $sql + ), + $params, + $types + ); + } + + /** + * Legacy phpList dumps don't all carry the same set of index names (older exports predate + * some indexes entirely), so a hardcoded RENAME INDEX can fail against a given dump. This + * renames whichever of the candidate legacy names is actually present, or creates the target + * index fresh if none of them are. + */ + protected function renameOrCreateIndex( + Schema $schema, + string $tableName, + array $possibleOldIndexNames, + string $newIndexName, + array $columns + ): void { + $table = $schema->getTable($this->getPrefixedTableName($tableName)); + + foreach ($possibleOldIndexNames as $oldIndexName) { + if ($table->hasIndex($oldIndexName)) { + $this->addSql(sprintf('ALTER TABLE %s RENAME INDEX %s TO %s', $tableName, $oldIndexName, $newIndexName)); + + return; + } + } + + if (!$table->hasIndex($newIndexName)) { + $this->addSql(sprintf('CREATE INDEX %s ON %s (%s)', $newIndexName, $tableName, implode(', ', $columns))); + } + } + + private function getPrefixedTableName(string $tableName): string + { + return str_replace(self::DEFAULT_PREFIX, $this->getTablePrefix(), $tableName); + } + + private function getTablePrefix(): string + { + $prefix = $_ENV['DATABASE_PREFIX'] ?? getenv('DATABASE_PREFIX'); + + return is_string($prefix) && $prefix !== '' ? $prefix : self::DEFAULT_PREFIX; + } +} diff --git a/src/Migrations/Version20251028092901MySqlInit.php b/src/Migrations/Version20251028092901MySqlInit.php index 5589fadf..7de730c0 100644 --- a/src/Migrations/Version20251028092901MySqlInit.php +++ b/src/Migrations/Version20251028092901MySqlInit.php @@ -6,12 +6,11 @@ use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Schema\Schema; -use Doctrine\Migrations\AbstractMigration; /** * Manual Migration */ -final class Version20251028092901MySqlInit extends AbstractMigration +final class Version20251028092901MySqlInit extends AbstractPrefixedMigration { public function getDescription(): string { diff --git a/src/Migrations/Version20251028092902MySqlEngineUpdate.php b/src/Migrations/Version20251028092902MySqlEngineUpdate.php new file mode 100644 index 00000000..4175c741 --- /dev/null +++ b/src/Migrations/Version20251028092902MySqlEngineUpdate.php @@ -0,0 +1,60 @@ +connection->getDatabasePlatform(); + $this->skipIf(!$platform instanceof MySQLPlatform, sprintf( + 'Unsupported platform for this migration: %s', + get_class($platform) + )); + + $engine = $this->connection->fetchOne(" + SELECT ENGINE + FROM information_schema.TABLES + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'phplist_user_user' + "); + + if ($engine !== 'InnoDB') { + // legacy phpList installs created these tables as MyISAM, which cannot be referenced by + // the InnoDB foreign keys added below (MySQL error 1824: Failed to open the referenced table) + $this->addSql('ALTER TABLE phplist_admin ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_admin_attribute ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_adminattribute ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_admintoken ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_list ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_listmessage ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_listuser ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_message ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_subscribepage ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_template ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_templateimage ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_attribute ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_blacklist ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_blacklist_data ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_user ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_user_attribute ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_user_user_history ENGINE=InnoDB'); + $this->addSql('ALTER TABLE phplist_usermessage ENGINE=InnoDB'); + } + } + + public function down(Schema $schema): void + { + + } +} diff --git a/src/Migrations/Version20251028092902MySqlUpdate.php b/src/Migrations/Version20251028092902MySqlUpdate.php index 2c0e872e..869915aa 100644 --- a/src/Migrations/Version20251028092902MySqlUpdate.php +++ b/src/Migrations/Version20251028092902MySqlUpdate.php @@ -6,10 +6,9 @@ use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; -use Doctrine\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; -final class Version20251028092902MySqlUpdate extends AbstractMigration +final class Version20251028092902MySqlUpdate extends AbstractPrefixedMigration { public function getDescription(): string { @@ -24,19 +23,25 @@ public function up(Schema $schema): void get_class($platform) )); + $this->addSql('UPDATE phplist_admin SET created = COALESCE(created, modified, NOW()) WHERE created IS NULL'); $this->addSql('ALTER TABLE phplist_admin CHANGE created created DATETIME NOT NULL, CHANGE modified modified DATETIME NOT NULL, CHANGE superuser superuser TINYINT(1) NOT NULL, CHANGE disabled disabled TINYINT(1) NOT NULL, CHANGE privileges privileges LONGTEXT DEFAULT NULL'); $this->addSql('ALTER TABLE phplist_admin RENAME INDEX loginnameidx TO phplist_admin_loginnameidx'); + $this->addSql('DELETE t FROM phplist_admin_attribute t LEFT JOIN phplist_adminattribute p ON t.adminattributeid = p.id WHERE p.id IS NULL'); + $this->addSql('DELETE t FROM phplist_admin_attribute t LEFT JOIN phplist_admin p ON t.adminid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_admin_attribute ADD CONSTRAINT FK_58E07690D3B10C48 FOREIGN KEY (adminattributeid) REFERENCES phplist_adminattribute (id)'); $this->addSql('ALTER TABLE phplist_admin_attribute ADD CONSTRAINT FK_58E07690B8ED4D93 FOREIGN KEY (adminid) REFERENCES phplist_admin (id)'); $this->addSql('CREATE INDEX IDX_58E07690D3B10C48 ON phplist_admin_attribute (adminattributeid)'); $this->addSql('CREATE INDEX IDX_58E07690B8ED4D93 ON phplist_admin_attribute (adminid)'); $this->addSql('ALTER TABLE phplist_admin_login CHANGE active active TINYINT(1) NOT NULL'); + $this->addSql('DELETE t FROM phplist_admin_login t LEFT JOIN phplist_admin p ON t.adminid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_admin_login ADD CONSTRAINT FK_5FCE0842B8ED4D93 FOREIGN KEY (adminid) REFERENCES phplist_admin (id)'); $this->addSql('CREATE INDEX IDX_5FCE0842B8ED4D93 ON phplist_admin_login (adminid)'); $this->addSql('ALTER TABLE phplist_admin_password_request CHANGE id_key id_key INT UNSIGNED AUTO_INCREMENT NOT NULL'); + $this->addSql('UPDATE phplist_admin_password_request t LEFT JOIN phplist_admin p ON t.admin = p.id SET t.admin = NULL WHERE t.admin IS NOT NULL AND p.id IS NULL'); $this->addSql('ALTER TABLE phplist_admin_password_request ADD CONSTRAINT FK_DC146F3B880E0D76 FOREIGN KEY (`admin`) REFERENCES phplist_admin (id)'); $this->addSql('CREATE INDEX IDX_DC146F3B880E0D76 ON phplist_admin_password_request (`admin`)'); $this->addSql('ALTER TABLE phplist_admintoken CHANGE adminid adminid INT DEFAULT NULL, CHANGE value value VARCHAR(255) NOT NULL'); + $this->addSql('UPDATE phplist_admintoken t LEFT JOIN phplist_admin p ON t.adminid = p.id SET t.adminid = NULL WHERE t.adminid IS NOT NULL AND p.id IS NULL'); $this->addSql('ALTER TABLE phplist_admintoken ADD CONSTRAINT FK_CB15D477B8ED4D93 FOREIGN KEY (adminid) REFERENCES phplist_admin (id) ON DELETE CASCADE'); $this->addSql('CREATE INDEX IDX_CB15D477B8ED4D93 ON phplist_admintoken (adminid)'); $this->addSql('ALTER TABLE phplist_attachment CHANGE description description LONGTEXT DEFAULT NULL'); @@ -72,11 +77,14 @@ public function up(Schema $schema): void $this->addSql('ALTER TABLE phplist_linktrack_userclick RENAME INDEX midindex TO phplist_linktrack_userclick_midindex'); $this->addSql('ALTER TABLE phplist_linktrack_userclick RENAME INDEX uidindex TO phplist_linktrack_userclick_uidindex'); $this->addSql('ALTER TABLE phplist_list CHANGE description description VARCHAR(255) NOT NULL, CHANGE modified modified DATETIME NOT NULL, CHANGE active active TINYINT(1) NOT NULL, CHANGE category category VARCHAR(255) NOT NULL'); + $this->addSql('UPDATE phplist_list t LEFT JOIN phplist_admin p ON t.owner = p.id SET t.owner = NULL WHERE t.owner IS NOT NULL AND p.id IS NULL'); $this->addSql('ALTER TABLE phplist_list ADD CONSTRAINT FK_A4CE8621CF60E67C FOREIGN KEY (owner) REFERENCES phplist_admin (id)'); $this->addSql('CREATE INDEX IDX_A4CE8621CF60E67C ON phplist_list (owner)'); $this->addSql('ALTER TABLE phplist_list RENAME INDEX nameidx TO phplist_list_nameidx'); $this->addSql('ALTER TABLE phplist_list RENAME INDEX listorderidx TO phplist_list_listorderidx'); $this->addSql('ALTER TABLE phplist_listmessage CHANGE modified modified DATETIME NOT NULL'); + $this->addSql('DELETE t FROM phplist_listmessage t LEFT JOIN phplist_message p ON t.messageid = p.id WHERE p.id IS NULL'); + $this->addSql('DELETE t FROM phplist_listmessage t LEFT JOIN phplist_list p ON t.listid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_listmessage ADD CONSTRAINT FK_83B22D7A31478478 FOREIGN KEY (messageid) REFERENCES phplist_message (id)'); $this->addSql('ALTER TABLE phplist_listmessage ADD CONSTRAINT FK_83B22D7A8E44C1EF FOREIGN KEY (listid) REFERENCES phplist_list (id)'); $this->addSql('CREATE INDEX IDX_83B22D7A31478478 ON phplist_listmessage (messageid)'); @@ -85,13 +93,17 @@ public function up(Schema $schema): void $this->addSql('ALTER TABLE phplist_listmessage RENAME INDEX messageid TO phplist_listmessage_messageid'); $this->addSql('DROP INDEX userlistenteredidx ON phplist_listuser'); $this->addSql('ALTER TABLE phplist_listuser CHANGE modified modified DATETIME NOT NULL'); + $this->addSql('DELETE t FROM phplist_listuser t LEFT JOIN phplist_user_user p ON t.userid = p.id WHERE p.id IS NULL'); + $this->addSql('DELETE t FROM phplist_listuser t LEFT JOIN phplist_list p ON t.listid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_listuser ADD CONSTRAINT FK_F467E411F132696E FOREIGN KEY (userid) REFERENCES phplist_user_user (id)'); $this->addSql('ALTER TABLE phplist_listuser ADD CONSTRAINT FK_F467E4118E44C1EF FOREIGN KEY (listid) REFERENCES phplist_list (id) ON DELETE CASCADE'); $this->addSql('CREATE INDEX phplist_listuser_userlistenteredidx ON phplist_listuser (userid, entered, listid)'); $this->addSql('ALTER TABLE phplist_listuser RENAME INDEX userenteredidx TO phplist_listuser_userenteredidx'); - $this->addSql('ALTER TABLE phplist_listuser RENAME INDEX useridx TO phplist_listuser_useridx'); - $this->addSql('ALTER TABLE phplist_listuser RENAME INDEX listidx TO phplist_listuser_listidx'); - $this->addSql('ALTER TABLE phplist_message CHANGE footer footer LONGTEXT DEFAULT NULL, CHANGE modified modified DATETIME NOT NULL, CHANGE userselection userselection LONGTEXT DEFAULT NULL, CHANGE htmlformatted htmlformatted TINYINT(1) NOT NULL, CHANGE astext astext TINYINT(1) NOT NULL, CHANGE ashtml ashtml TINYINT(1) NOT NULL, CHANGE astextandhtml astextandhtml TINYINT(1) NOT NULL, CHANGE aspdf aspdf TINYINT(1) NOT NULL, CHANGE astextandpdf astextandpdf TINYINT(1) NOT NULL, CHANGE viewed viewed INT DEFAULT 0 NOT NULL, CHANGE bouncecount bouncecount INT DEFAULT 0 NOT NULL'); + $this->renameOrCreateIndex($schema, 'phplist_listuser', ['useridx'], 'phplist_listuser_useridx', ['userid']); + $this->renameOrCreateIndex($schema, 'phplist_listuser', ['listidx'], 'phplist_listuser_listidx', ['listid']); + $this->addSql('ALTER TABLE phplist_message CHANGE footer footer LONGTEXT DEFAULT NULL, CHANGE modified modified DATETIME NOT NULL, CHANGE userselection userselection LONGTEXT DEFAULT NULL, CHANGE htmlformatted htmlformatted TINYINT(1) NOT NULL, CHANGE astext astext INT DEFAULT 0 NOT NULL, CHANGE ashtml ashtml INT DEFAULT 0 NOT NULL, CHANGE astextandhtml astextandhtml INT DEFAULT 0 NOT NULL, CHANGE aspdf aspdf INT DEFAULT 0 NOT NULL, CHANGE astextandpdf astextandpdf INT DEFAULT 0 NOT NULL, CHANGE viewed viewed INT DEFAULT 0 NOT NULL, CHANGE bouncecount bouncecount INT DEFAULT 0 NOT NULL'); + $this->addSql('UPDATE phplist_message t LEFT JOIN phplist_admin p ON t.owner = p.id SET t.owner = NULL WHERE t.owner IS NOT NULL AND p.id IS NULL'); + $this->addSql('UPDATE phplist_message t LEFT JOIN phplist_template p ON t.template = p.id SET t.template = NULL WHERE t.template IS NOT NULL AND p.id IS NULL'); $this->addSql('ALTER TABLE phplist_message ADD CONSTRAINT FK_C5D81FCDCF60E67C FOREIGN KEY (owner) REFERENCES phplist_admin (id)'); $this->addSql('ALTER TABLE phplist_message ADD CONSTRAINT FK_C5D81FCD97601F83 FOREIGN KEY (template) REFERENCES phplist_template (id) ON DELETE SET NULL'); $this->addSql('CREATE INDEX IDX_C5D81FCDCF60E67C ON phplist_message (owner)'); @@ -102,11 +114,13 @@ public function up(Schema $schema): void $this->addSql('ALTER TABLE phplist_messagedata CHANGE data data LONGTEXT CHARACTER SET utf8mb4 DEFAULT NULL'); $this->addSql('ALTER TABLE phplist_sendprocess CHANGE modified modified DATETIME NOT NULL'); $this->addSql('ALTER TABLE phplist_subscribepage CHANGE active active TINYINT(1) DEFAULT 0 NOT NULL'); + $this->addSql('UPDATE phplist_subscribepage t LEFT JOIN phplist_admin p ON t.owner = p.id SET t.owner = NULL WHERE t.owner IS NOT NULL AND p.id IS NULL'); $this->addSql('ALTER TABLE phplist_subscribepage ADD CONSTRAINT FK_5BAC7737CF60E67C FOREIGN KEY (owner) REFERENCES phplist_admin (id)'); $this->addSql('CREATE INDEX IDX_5BAC7737CF60E67C ON phplist_subscribepage (owner)'); $this->addSql('ALTER TABLE phplist_subscribepage_data CHANGE data data LONGTEXT DEFAULT NULL'); $this->addSql('ALTER TABLE phplist_template RENAME INDEX title TO phplist_template_title'); $this->addSql('ALTER TABLE phplist_templateimage CHANGE template template INT NOT NULL'); + $this->addSql('DELETE t FROM phplist_templateimage t LEFT JOIN phplist_template p ON t.template = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_templateimage ADD CONSTRAINT FK_30A85BA97601F83 FOREIGN KEY (template) REFERENCES phplist_template (id)'); $this->addSql('ALTER TABLE phplist_templateimage RENAME INDEX templateidx TO phplist_templateimage_templateidx'); $this->addSql('ALTER TABLE phplist_urlcache RENAME INDEX urlindex TO phplist_urlcache_urlindex'); @@ -117,6 +131,7 @@ public function up(Schema $schema): void $this->addSql('ALTER TABLE phplist_user_blacklist RENAME INDEX emailidx TO phplist_user_blacklist_emailidx'); $this->addSql('DROP INDEX email ON phplist_user_blacklist_data'); $this->addSql('ALTER TABLE phplist_user_blacklist_data CHANGE email email VARCHAR(255) NOT NULL, CHANGE data data LONGTEXT DEFAULT NULL, ADD PRIMARY KEY (email)'); + $this->addSql('DELETE t FROM phplist_user_blacklist_data t LEFT JOIN phplist_user_blacklist p ON t.email = p.email WHERE p.email IS NULL'); $this->addSql('ALTER TABLE phplist_user_blacklist_data ADD CONSTRAINT FK_6D67150CE7927C74 FOREIGN KEY (email) REFERENCES phplist_user_blacklist (email) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_user_blacklist_data RENAME INDEX emailidx TO phplist_user_blacklist_data_emailidx'); $this->addSql('ALTER TABLE phplist_user_blacklist_data RENAME INDEX emailnameidx TO phplist_user_blacklist_data_emailnameidx'); @@ -131,31 +146,42 @@ public function up(Schema $schema): void $this->addSql('ALTER TABLE phplist_user_message_view RENAME INDEX useridx TO phplist_user_message_view_useridx'); $this->addSql('ALTER TABLE phplist_user_message_view RENAME INDEX usermsgidx TO phplist_user_message_view_usermsgidx'); $this->addSql('ALTER TABLE phplist_user_user CHANGE confirmed confirmed TINYINT(1) NOT NULL, CHANGE blacklisted blacklisted TINYINT(1) NOT NULL, CHANGE optedin optedin TINYINT(1) NOT NULL, CHANGE bouncecount bouncecount INT NOT NULL, CHANGE modified modified DATETIME NOT NULL, CHANGE uuid uuid VARCHAR(36) NOT NULL, CHANGE htmlemail htmlemail TINYINT(1) NOT NULL, CHANGE passwordchanged passwordchanged DATETIME DEFAULT NULL, CHANGE disabled disabled TINYINT(1) NOT NULL, CHANGE extradata extradata LONGTEXT DEFAULT NULL'); - $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX idxuniqid TO phplist_user_user_idxuniqid'); + $this->renameOrCreateIndex( + $schema, + 'phplist_user_user', + ['idxuniqid', 'idx_phplist_user_user_uniqid'], + 'phplist_user_user_idxuniqid', + ['uniqid'] + ); $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX enteredindex TO phplist_user_user_enteredindex'); - $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX confidx TO phplist_user_user_confidx'); - $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX blidx TO phplist_user_user_blidx'); - $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX optidx TO phplist_user_user_optidx'); + $this->renameOrCreateIndex($schema, 'phplist_user_user', ['confidx'], 'phplist_user_user_confidx', ['confirmed']); + $this->renameOrCreateIndex($schema, 'phplist_user_user', ['blidx'], 'phplist_user_user_blidx', ['blacklisted']); + $this->renameOrCreateIndex($schema, 'phplist_user_user', ['optidx'], 'phplist_user_user_optidx', ['optedin']); $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX uuididx TO phplist_user_user_uuididx'); $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX foreignkey TO phplist_user_user_foreignkey'); $this->addSql('ALTER TABLE phplist_user_user RENAME INDEX email TO phplist_user_user_email'); $this->addSql('ALTER TABLE phplist_user_user_attribute CHANGE value value LONGTEXT DEFAULT NULL'); + $this->addSql('DELETE t FROM phplist_user_user_attribute t LEFT JOIN phplist_user_attribute p ON t.attributeid = p.id WHERE p.id IS NULL'); + $this->addSql('DELETE t FROM phplist_user_user_attribute t LEFT JOIN phplist_user_user p ON t.userid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_user_user_attribute ADD CONSTRAINT FK_E24E310878C45AB5 FOREIGN KEY (attributeid) REFERENCES phplist_user_attribute (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_user_user_attribute ADD CONSTRAINT FK_E24E3108F132696E FOREIGN KEY (userid) REFERENCES phplist_user_user (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_user_user_attribute RENAME INDEX attindex TO phplist_user_user_attribute_attindex'); $this->addSql('ALTER TABLE phplist_user_user_attribute RENAME INDEX attuserid TO phplist_user_user_attribute_attuserid'); $this->addSql('ALTER TABLE phplist_user_user_attribute RENAME INDEX userindex TO phplist_user_user_attribute_userindex'); $this->addSql('ALTER TABLE phplist_user_user_history CHANGE detail detail LONGTEXT DEFAULT NULL, CHANGE systeminfo systeminfo LONGTEXT DEFAULT NULL'); + $this->addSql('DELETE t FROM phplist_user_user_history t LEFT JOIN phplist_user_user p ON t.userid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_user_user_history ADD CONSTRAINT FK_6DBB605CF132696E FOREIGN KEY (userid) REFERENCES phplist_user_user (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_user_user_history RENAME INDEX dateidx TO phplist_user_user_history_dateidx'); $this->addSql('ALTER TABLE phplist_user_user_history RENAME INDEX userididx TO phplist_user_user_history_userididx'); + $this->addSql('DELETE t FROM phplist_usermessage t LEFT JOIN phplist_user_user p ON t.userid = p.id WHERE p.id IS NULL'); + $this->addSql('DELETE t FROM phplist_usermessage t LEFT JOIN phplist_message p ON t.messageid = p.id WHERE p.id IS NULL'); $this->addSql('ALTER TABLE phplist_usermessage ADD CONSTRAINT FK_7F30F469F132696E FOREIGN KEY (userid) REFERENCES phplist_user_user (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_usermessage ADD CONSTRAINT FK_7F30F46931478478 FOREIGN KEY (messageid) REFERENCES phplist_message (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE phplist_usermessage RENAME INDEX enteredindex TO phplist_usermessage_enteredindex'); $this->addSql('ALTER TABLE phplist_usermessage RENAME INDEX messageidindex TO phplist_usermessage_messageidindex'); - $this->addSql('ALTER TABLE phplist_usermessage RENAME INDEX statusidx TO phplist_usermessage_statusidx'); + $this->renameOrCreateIndex($schema, 'phplist_usermessage', ['statusidx'], 'phplist_usermessage_statusidx', ['status']); $this->addSql('ALTER TABLE phplist_usermessage RENAME INDEX useridindex TO phplist_usermessage_useridindex'); - $this->addSql('ALTER TABLE phplist_usermessage RENAME INDEX viewedidx TO phplist_usermessage_viewedidx'); + $this->renameOrCreateIndex($schema, 'phplist_usermessage', ['viewedidx'], 'phplist_usermessage_viewedidx', ['viewed']); $this->addSql('ALTER TABLE phplist_userstats RENAME INDEX dateindex TO phplist_userstats_dateindex'); $this->addSql('ALTER TABLE phplist_userstats RENAME INDEX itemindex TO phplist_userstats_itemindex'); $this->addSql('ALTER TABLE phplist_userstats RENAME INDEX listdateindex TO phplist_userstats_listdateindex'); diff --git a/src/Migrations/Version20251031072945PostGreInit.php b/src/Migrations/Version20251031072945PostGreInit.php index 80c27956..08076e8d 100644 --- a/src/Migrations/Version20251031072945PostGreInit.php +++ b/src/Migrations/Version20251031072945PostGreInit.php @@ -5,7 +5,6 @@ namespace PhpList\Core\Migrations; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; -use Doctrine\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; /** @@ -15,7 +14,7 @@ * * Ex: phplist_linktrack_forward phplist_linktrack_forward_urlindex (but there are more) */ -final class Version20251031072945PostGreInit extends AbstractMigration +final class Version20251031072945PostGreInit extends AbstractPrefixedMigration { public function getDescription(): string { @@ -121,7 +120,7 @@ public function up(Schema $schema): void $this->addSql('CREATE INDEX phplist_listuser_userlistenteredidx ON phplist_listuser (userid, entered, listid)'); $this->addSql('CREATE INDEX phplist_listuser_useridx ON phplist_listuser (userid)'); $this->addSql('CREATE INDEX phplist_listuser_listidx ON phplist_listuser (listid)'); - $this->addSql('CREATE TABLE phplist_message (id INT NOT NULL, owner INT DEFAULT NULL, template INT DEFAULT NULL, modified TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, uuid VARCHAR(36) DEFAULT \'\', htmlformatted BOOLEAN NOT NULL, sendformat VARCHAR(20) DEFAULT NULL, astext BOOLEAN NOT NULL, ashtml BOOLEAN NOT NULL, aspdf BOOLEAN NOT NULL, astextandhtml BOOLEAN NOT NULL, astextandpdf BOOLEAN NOT NULL, repeatinterval INT DEFAULT 0, repeatuntil TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, requeueinterval INT DEFAULT 0, requeueuntil TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, embargo TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, status VARCHAR(255) DEFAULT NULL, viewed INT DEFAULT 0 NOT NULL, bouncecount INT DEFAULT 0 NOT NULL, entered TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, sent TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, sendstart TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, subject VARCHAR(255) DEFAULT \'(no subject)\' NOT NULL, message TEXT DEFAULT NULL, textmessage TEXT DEFAULT NULL, footer TEXT DEFAULT NULL, fromfield VARCHAR(255) DEFAULT \'\' NOT NULL, tofield VARCHAR(255) DEFAULT \'\' NOT NULL, replyto VARCHAR(255) DEFAULT \'\' NOT NULL, userselection TEXT DEFAULT NULL, rsstemplate VARCHAR(100) DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE phplist_message (id INT NOT NULL, owner INT DEFAULT NULL, template INT DEFAULT NULL, modified TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, uuid VARCHAR(36) DEFAULT \'\', htmlformatted BOOLEAN NOT NULL, sendformat VARCHAR(20) DEFAULT NULL, astext INT DEFAULT 0 NOT NULL, ashtml INT DEFAULT 0 NOT NULL, aspdf INT DEFAULT 0 NOT NULL, astextandhtml INT DEFAULT 0 NOT NULL, astextandpdf INT DEFAULT 0 NOT NULL, repeatinterval INT DEFAULT 0, repeatuntil TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, requeueinterval INT DEFAULT 0, requeueuntil TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, embargo TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, status VARCHAR(255) DEFAULT NULL, viewed INT DEFAULT 0 NOT NULL, bouncecount INT DEFAULT 0 NOT NULL, entered TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, sent TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, sendstart TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, subject VARCHAR(255) DEFAULT \'(no subject)\' NOT NULL, message TEXT DEFAULT NULL, textmessage TEXT DEFAULT NULL, footer TEXT DEFAULT NULL, fromfield VARCHAR(255) DEFAULT \'\' NOT NULL, tofield VARCHAR(255) DEFAULT \'\' NOT NULL, replyto VARCHAR(255) DEFAULT \'\' NOT NULL, userselection TEXT DEFAULT NULL, rsstemplate VARCHAR(100) DEFAULT NULL, PRIMARY KEY(id))'); $this->addSql('CREATE INDEX IDX_C5D81FCDCF60E67C ON phplist_message (owner)'); $this->addSql('CREATE INDEX IDX_C5D81FCD97601F83 ON phplist_message (template)'); $this->addSql('CREATE INDEX phplist_message_uuididx ON phplist_message (uuid)'); diff --git a/src/Migrations/Version20260204094237.php b/src/Migrations/Version20260204094237.php deleted file mode 100644 index 00e7fd91..00000000 --- a/src/Migrations/Version20260204094237.php +++ /dev/null @@ -1,55 +0,0 @@ -connection->getDatabasePlatform(); - $this->skipIf(!$platform instanceof PostgreSQLPlatform, sprintf( - 'Unsupported platform for this migration: %s', - get_class($platform) - )); - - $this->addSql('ALTER TABLE phplist_message ALTER astext TYPE INT USING astext::integer'); - $this->addSql('ALTER TABLE phplist_message ALTER ashtml TYPE INT USING ashtml::integer'); - $this->addSql('ALTER TABLE phplist_message ALTER aspdf TYPE INT USING aspdf::integer'); - $this->addSql('ALTER TABLE phplist_message ALTER astextandhtml TYPE INT USING astextandhtml::integer'); - $this->addSql('ALTER TABLE phplist_message ALTER astextandpdf TYPE INT USING astextandpdf::integer'); - } - - public function down(Schema $schema): void - { - $platform = $this->connection->getDatabasePlatform(); - $this->skipIf(!$platform instanceof PostgreSQLPlatform, sprintf( - 'Unsupported platform for this migration: %s', - get_class($platform) - )); - - $this->addSql('ALTER TABLE phplist_message ALTER astext TYPE BOOLEAN USING (astext::integer <> 0)'); - $this->addSql('ALTER TABLE phplist_message ALTER ashtml TYPE BOOLEAN USING (ashtml::integer <> 0)'); - $this->addSql('ALTER TABLE phplist_message ALTER aspdf TYPE BOOLEAN USING (aspdf::integer <> 0)'); - $this->addSql('ALTER TABLE phplist_message ALTER astextandhtml TYPE BOOLEAN USING (astextandhtml::integer <> 0)'); - $this->addSql('ALTER TABLE phplist_message ALTER astextandpdf TYPE BOOLEAN USING (astextandpdf::integer <> 0)'); - } -} diff --git a/src/Migrations/_template_migration.php.tpl b/src/Migrations/_template_migration.php.tpl index 72561549..cd2cde8f 100644 --- a/src/Migrations/_template_migration.php.tpl +++ b/src/Migrations/_template_migration.php.tpl @@ -6,7 +6,6 @@ namespace ; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; -use Doctrine\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; /** @@ -16,7 +15,7 @@ use Doctrine\DBAL\Schema\Schema; * * Ex: phplist_linktrack_forward phplist_linktrack_forward_urlindex (but there are more) */ -final class extends AbstractMigration +final class extends AbstractPrefixedMigration { public function getDescription(): string { diff --git a/src/Security/HashGenerator.php b/src/Security/HashGenerator.php index a70acaa3..67ab3054 100644 --- a/src/Security/HashGenerator.php +++ b/src/Security/HashGenerator.php @@ -12,17 +12,40 @@ class HashGenerator { /** + * Legacy algorithm that older password hashes in the database may still use. + * * @var string */ - const PASSWORD_HASH_ALGORITHM = 'sha256'; + const LEGACY_PASSWORD_HASH_ALGORITHM = 'sha256'; + + public function createPasswordHash(string $plainTextPassword): string + { + return password_hash($plainTextPassword, PASSWORD_DEFAULT); + } /** - * @param string $plainTextPassword + * Checks a plaintext password against a stored hash. * - * @return string + * Hashes created by {@see createPasswordHash()} are verified with `password_verify()`. + * As a fallback, this also accepts hashes created by the old, unsalted + * sha256-based scheme, so administrators with pre-existing hashes can still log in. */ - public function createPasswordHash(string $plainTextPassword): string + public function verifyPassword(string $plainTextPassword, string $hash): bool + { + if (password_verify($plainTextPassword, $hash)) { + return true; + } + + return $this->isLegacyHash($hash) + && hash_equals(hash(static::LEGACY_PASSWORD_HASH_ALGORITHM, $plainTextPassword), $hash); + } + + /** + * Checks whether $hash was created by the old, unsalted sha256-based scheme + * rather than by {@see createPasswordHash()}. + */ + public function isLegacyHash(string $hash): bool { - return hash(static::PASSWORD_HASH_ALGORITHM, $plainTextPassword); + return preg_match('/^[0-9a-f]{64}$/', $hash) === 1; } } diff --git a/tests/Unit/Domain/Analytics/Service/AnalyticsServiceTest.php b/tests/Unit/Domain/Analytics/Service/AnalyticsServiceTest.php index a56dd5d8..2470f470 100644 --- a/tests/Unit/Domain/Analytics/Service/AnalyticsServiceTest.php +++ b/tests/Unit/Domain/Analytics/Service/AnalyticsServiceTest.php @@ -4,7 +4,9 @@ namespace PhpList\Core\Tests\Unit\Domain\Analytics\Service; +use DateInterval; use DateTime; +use DateTimeImmutable; use PhpList\Core\Domain\Analytics\Model\LinkTrack; use PhpList\Core\Domain\Analytics\Repository\UserMessageViewRepository; use PhpList\Core\Domain\Analytics\Service\AnalyticsService; @@ -376,4 +378,83 @@ public function testGetSummaryStatistics(): void self::assertEquals(2.0, $result['bounce_rate']['value']); self::assertEquals(0.0, $result['bounce_rate']['change_vs_last_month']); } + + public function testGetCampaignPerformance(): void + { + $endDate = new DateTimeImmutable('today 23:59:59'); + $startDate = $endDate->sub(new DateInterval('P29D'))->modify('00:00:00'); + + $someDay = $startDate->add(new DateInterval('P5D'))->format('Y-m-d'); + + $this->userMessageViewManager->expects(self::once()) + ->method('countViewsGroupedByDay') + ->with($startDate, $endDate) + ->willReturn([$someDay => 7]); + + $this->linkTrackManager->expects(self::once()) + ->method('countClicksGroupedByDay') + ->with($startDate, $endDate) + ->willReturn([$someDay => 3]); + + $result = $this->subject->getCampaignPerformance(); + + self::assertCount(30, $result); + + $matching = array_values(array_filter($result, static fn ($row) => $row['date'] === $someDay)); + self::assertCount(1, $matching); + self::assertSame(7, $matching[0]['opens']); + self::assertSame(3, $matching[0]['clicks']); + + $other = array_values(array_filter($result, static fn ($row) => $row['date'] !== $someDay)); + self::assertSame(0, $other[0]['opens']); + self::assertSame(0, $other[0]['clicks']); + } + + public function testGetRecentCampaigns(): void + { + $limit = 5; + $messageId = 42; + + $messageMetadata = $this->createMock(MessageMetadata::class); + $messageMetadata->method('getViews')->willReturn(80); + $messageMetadata->method('getBounceCount')->willReturn(20); + $messageMetadata->method('getSent')->willReturn(new DateTime('2023-02-01 10:00:00')); + $messageMetadata->method('getStatus')->willReturn(null); + + $messageContent = $this->createMock(MessageContent::class); + $messageContent->method('getSubject')->willReturn('Recent Campaign'); + + $message = $this->createMock(Message::class); + $message->method('getId')->willReturn($messageId); + $message->method('getMetadata')->willReturn($messageMetadata); + $message->method('getContent')->willReturn($messageContent); + + $messageResult = new PaginatedResult([$message], 1, 1, $messageId); + + $this->messageRepository->expects(self::once()) + ->method('getFilteredAfterId') + ->with($this->callback(function (MessageFilter $filter) use ($limit): bool { + return $filter->getLastId() === 0 && $filter->getLimit() === $limit; + })) + ->willReturn($messageResult); + + $this->userMessageViewManager->expects(self::once()) + ->method('countViewsByMessageIds') + ->with([$messageId]) + ->willReturn([$messageId => 40]); + + $this->linkTrackManager->expects(self::once()) + ->method('countUniqueClickersByMessageIds') + ->with([$messageId]) + ->willReturn([$messageId => 10]); + + $result = $this->subject->getRecentCampaigns($limit); + + self::assertCount(1, $result); + self::assertSame('Recent Campaign', $result[0]['name']); + self::assertNull($result[0]['status']); + self::assertSame('2023-02-01', $result[0]['date']); + self::assertSame('40%', $result[0]['open_rate']); + self::assertSame('10%', $result[0]['click_rate']); + } } diff --git a/tests/Unit/Domain/Configuration/Service/Provider/ConfigProviderTest.php b/tests/Unit/Domain/Configuration/Service/Provider/ConfigProviderTest.php index ab6e90c5..bd7eee08 100644 --- a/tests/Unit/Domain/Configuration/Service/Provider/ConfigProviderTest.php +++ b/tests/Unit/Domain/Configuration/Service/Provider/ConfigProviderTest.php @@ -71,7 +71,7 @@ public function testIsEnabledUsesRepositoryValueWhenPresent(): void $this->repo ->expects($this->once()) ->method('findOneBy') - ->with(['item' => $key->value]) + ->with(['key' => $key->value]) ->willReturn($configEntity); // Defaults should not be consulted if repo has value @@ -90,7 +90,7 @@ public function testIsEnabledFallsBackToDefaultsWhenRepoMissing(): void $this->repo ->expects($this->once()) ->method('findOneBy') - ->with(['item' => $key->value]) + ->with(['key' => $key->value]) ->willReturn(null); $this->defaults diff --git a/tests/Unit/Security/HashGeneratorTest.php b/tests/Unit/Security/HashGeneratorTest.php index b8bd956b..86aac803 100644 --- a/tests/Unit/Security/HashGeneratorTest.php +++ b/tests/Unit/Security/HashGeneratorTest.php @@ -21,27 +21,64 @@ protected function setUp(): void $this->subject = new HashGenerator(); } - public function testCreatePasswordHashCreates64CharacterHash(): void + public function testCreatePasswordHashCreatesPasswordHashCompatibleHash(): void { $hash = $this->subject->createPasswordHash('Portal'); - self::assertMatchesRegularExpression('/^[a-z0-9]{64}$/', $hash); + + self::assertNotFalse(password_get_info($hash)['algo']); } - public function testCreatePasswordHashCalledTwoTimesWithSamePasswordCreatesSameHash(): void + public function testCreatePasswordHashCalledTwoTimesWithSamePasswordCreatesDifferentHashes(): void { $password = 'Aperture Science'; $hash1 = $this->subject->createPasswordHash($password); $hash2 = $this->subject->createPasswordHash($password); - self::assertSame($hash1, $hash2); + self::assertNotSame($hash1, $hash2); } - public function testCreatePasswordHashCalledTwoTimesWithDifferentPasswordsCreatesDifferentHashes(): void + public function testVerifyPasswordForMatchingPasswordAndHashReturnsTrue(): void { - $hash1 = $this->subject->createPasswordHash('Mel'); - $hash2 = $this->subject->createPasswordHash('Cave Johnson'); + $password = 'Cave Johnson'; + $hash = $this->subject->createPasswordHash($password); - self::assertNotSame($hash1, $hash2); + self::assertTrue($this->subject->verifyPassword($password, $hash)); + } + + public function testVerifyPasswordForNonMatchingPasswordAndHashReturnsFalse(): void + { + $hash = $this->subject->createPasswordHash('Mel'); + + self::assertFalse($this->subject->verifyPassword('Cave Johnson', $hash)); + } + + public function testVerifyPasswordForMatchingPasswordAndLegacyHashReturnsTrue(): void + { + $password = 'Bazinga!'; + $legacyHash = hash(HashGenerator::LEGACY_PASSWORD_HASH_ALGORITHM, $password); + + self::assertTrue($this->subject->verifyPassword($password, $legacyHash)); + } + + public function testVerifyPasswordForNonMatchingPasswordAndLegacyHashReturnsFalse(): void + { + $legacyHash = hash(HashGenerator::LEGACY_PASSWORD_HASH_ALGORITHM, 'Bazinga!'); + + self::assertFalse($this->subject->verifyPassword('wrong-password', $legacyHash)); + } + + public function testIsLegacyHashForSha256HashReturnsTrue(): void + { + $legacyHash = hash(HashGenerator::LEGACY_PASSWORD_HASH_ALGORITHM, 'Bazinga!'); + + self::assertTrue($this->subject->isLegacyHash($legacyHash)); + } + + public function testIsLegacyHashForPasswordHashHashReturnsFalse(): void + { + $hash = $this->subject->createPasswordHash('Bazinga!'); + + self::assertFalse($this->subject->isLegacyHash($hash)); } }