pods-framework / pods

The Pods Framework is a Content Development Framework for WordPress - It lets you create and extend content types that can be used for any project. Add fields of various types we've built in, or add your own with custom inputs, you have total control.
https://pods.io/
GNU General Public License v2.0
1.07k stars 265 forks source link

Error with empty WP table prefix in PodsData.php #5803

Closed galloppinggryphon closed 4 years ago

galloppinggryphon commented 4 years ago

PHP Warning: strpos(): Empty needle in /home/***/wp/wp-content/plugins/pods/classes/PodsData.php on line 3058

On this line, traverse_recurse() checks if the selected table name is a WP table or not by testing for the presence of the WP table prefix at the beginning of the string. In my setup, the table prefix is empty, leading to the error above.

To Reproduce

The problem occurs when running a custom WHERE clause pulling a custom field:

pods( 'directory_entry', [
    //'where' => "`t.post_title` = 'foobar'" // <<< This works
    'where' => "`entry_type.meta_value` = 'organization'" // <<< This produces the error
] );

The code is located in a theme template file. As far as I can tell the WHERE clause works; it produces the right output.

Expected behavior Pods should be flexible enough to allow an empty prefix or state clearly in the setup documentation that this does not work, and then produce a clear error message in the Dashboard.

Possible Workaround Changing line 3058 to check if the prefix is empty bypasses the problem, but I don't know that it's enough to fix it completely or if Pods relies on the presence of a prefix throughout the code.

if ( ! empty( $traverse_recurse['params'] ) && ! empty( $traverse_recurse['params']->table ) && ( $wpdb->prefix == '' || 0 === strpos( $traverse_recurse['params']->table, $wpdb->prefix ) ) ) {

Pods Version

2.7.21

WordPress Environment

` ### wp-core ### version: 5.4.2 site_language: en_US user_language: en_US timezone: +00:00 permalink: /blogs/%category%/%year%/%postname%/ https_status: true user_registration: 0 default_comment_status: closed multisite: false user_count: 1 dotorg_communication: true ### wp-paths-sizes ### wordpress_path: /home/example/dev.example.ca/web/wp wordpress_size: 45.65 MB (47870665 bytes) uploads_path: /home/example/dev.example.ca/web/wp/wp-content/uploads uploads_size: 158.20 MB (165880778 bytes) themes_path: /home/example/dev.example.ca/web/wp/wp-content/themes themes_size: 17.49 MB (18339471 bytes) plugins_path: /home/example/dev.example.ca/web/wp/wp-content/plugins plugins_size: 122.06 MB (127991952 bytes) database_size: 11.54 MB (12100430 bytes) total_size: 354.94 MB (372183296 bytes) ### wp-dropins (1) ### db-error.php: true ### wp-active-theme ### name: example (example-theme) version: author: author_website: parent_theme: none theme_features: align-wide, editor-font-sizes, editor-color-palette, automatic-feed-links, title-tag, responsive-embeds, menus, html5, post-thumbnails theme_path: /home/example/dev.example.ca/web/wp/wp-content/themes/example-theme ### wp-themes-inactive (1) ### Twenty Twenty: version: 1.0, author: the WordPress team (latest version: 1.4) ### wp-plugins-active (18) ### Crop Thumbnails: version: 1.2.6, author: Volkmar Kantor Disable Gutenberg Autosave: version: 1.0.7, author: websevendev (latest version: 1.0.8) Duplicate Post: version: 3.2.4, author: Enrico Battocchi (latest version: 3.2.5) Events Manager: version: 5.9.7.1, author: Marcus Sykes (latest version: 5.9.8.1) Forminator: version: 1.11.4, author: WPMU DEV (latest version: 1.13.4) Gutenberg: version: 7.8.0, author: Gutenberg Team (latest version: 8.7.1) Gutenberg Ramp: version: 1.1.0, author: Automattic, Inc. Inline Google Spreadsheet Viewer: version: 0.13.2, author: Meitar Moscovitz Kadence Blocks - Gutenberg Page Builder Toolkit: version: 1.7.10, author: Kadence WP (latest version: 1.8.11) Loginizer: version: 1.4.8, author: Softaculous (latest version: 1.5.3) Media Library Assistant: version: 2.81, author: David Lingren (latest version: 2.83) Pods - Custom Content Types and Fields: version: 2.7.21, author: Pods Framework Team Redirection: version: 4.7.1, author: John Godley (latest version: 4.8) Regenerate Thumbnails: version: 3.1.3, author: Alex Mills (Viper007Bond) Rewrite Rules Inspector: version: 1.2.1, author: Daniel Bachhuber, Automattic Safe SVG: version: 1.9.8, author: Daryll Doyle (latest version: 1.9.9) TinyMCE Advanced: version: 5.4.0, author: Andrew Ozz WP Mail SMTP: version: 1.8.1, author: WPForms (latest version: 2.2.1) ### wp-plugins-inactive (13) ### Edit Flow: version: 0.9.3, author: Daniel Bachhuber, Scott Bressler, Mohammad Jangda, Automattic, and others (latest version: 0.9.6) Gutenberg: version: 7.6.1, author: Gutenberg Team (latest version: 8.7.1) Gutenberg Custom Fields: version: 1.5.6, author: Riad Benguella Gutenberg Manager: version: 1.5, author: unCommons Team Hello Dolly: version: 1.7.2, author: Matt Mullenweg JSON Basic Authentication: version: 0.1, author: WordPress API Team PressPermit: version: 2.8.8, author: PublishPress (latest version: 3.2.4) PublishPress Revisions: version: 2.1.6, author: PublishPress (latest version: 2.3.10) Simple Custom CSS: version: 4.0.3, author: John Regan, Danny Van Kooten (latest version: 4.0.4) Stylist: version: 0.2.6, author: StylistWP TablePress: version: 1.10, author: Tobias Bäthge (latest version: 1.11) WebP Express: version: 0.17.2, author: Bjørn Rosell (latest version: 0.17.4) WordPress Beta Tester: version: 2.2.7, author: Peter Westwood, Andy Fragen (latest version: 2.2.12) ### wp-media ### image_editor: WP_Image_Editor_Imagick imagick_module_version: 1687 imagemagick_version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org imagick_limits: imagick::RESOURCETYPE_AREA: 62 GB imagick::RESOURCETYPE_DISK: -1 imagick::RESOURCETYPE_FILE: 6144 imagick::RESOURCETYPE_MAP: 62 GB imagick::RESOURCETYPE_MEMORY: 31 GB imagick::RESOURCETYPE_THREAD: 1 gd_version: bundled (2.1.0 compatible) ghostscript_version: 9.26 ### wp-server ### server_architecture: Linux 5.4.32-grsec-grsec-plus+ x86_64 httpd_software: Apache php_version: 7.2.30 64bit php_sapi: cgi-fcgi max_input_variables: 1000 time_limit: 120 memory_limit: 1024M max_input_time: -1 upload_max_size: 50M php_post_max_size: 55M curl_version: 7.58.0 OpenSSL/1.1.1 suhosin: false imagick_availability: true ### wp-database ### extension: mysqli server_version: 5.7.28-log client_version: mysqlnd 5.0.12-dev - 20150407 - $Id: 3591daad22de08524295e1bd073aceeff11e6579 $ ### wp-constants ### WP_HOME: undefined WP_SITEURL: undefined WP_CONTENT_DIR: /home/example/dev.example.ca/web/wp/wp-content WP_PLUGIN_DIR: /home/example/dev.example.ca/web/wp/wp-content/plugins WP_MAX_MEMORY_LIMIT: 1024M WP_DEBUG: true WP_DEBUG_DISPLAY: true WP_DEBUG_LOG: true SCRIPT_DEBUG: false WP_CACHE: false CONCATENATE_SCRIPTS: undefined COMPRESS_SCRIPTS: undefined COMPRESS_CSS: undefined WP_LOCAL_DEV: undefined DB_CHARSET: utf8mb4 DB_COLLATE: undefined ### wp-filesystem ### wordpress: writable wp-content: writable uploads: writable plugins: writable themes: writable ### pods ### pods-server-software: Apache pods-user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0 pods-session-save-path: /tmp pods-session-save-path-exists: Yes pods-session-save-path-writable: Yes pods-session-max-lifetime: 1440 pods-opcode-cache-apc: No pods-opcode-cache-memcached: No pods-opcode-cache-opcache: Yes pods-opcode-cache-redis: No pods-object-cache-apc: No pods-object-cache-apcu: No pods-object-cache-memcache: No pods-object-cache-memcached: Yes pods-object-cache-redis: No pods-memory-current-usage: 60.245M pods-memory-current-usage-real: 8.000M pods-network-wide: No pods-install-location: /home/example/dev.example.ca/web/wp/wp-content/plugins/pods/ pods-developer: No pods-tableless-mode: No pods-light-mode: No pods-strict: Yes pods-allow-deprecated: No pods-api-cache: Yes pods-shortcode-allow-evaluate-tags: No `
issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label Type: Bug to this issue, with a confidence of 0.95. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

JoryHogeveen commented 4 years ago

'where' => "entry_type.meta_value = 'organization'" // <<< This produces the error

This would work if entry_type is an existing field. Is that te case? Also, that part should not be wrapped in quotes. But if you do you'll need to wrap each traversal name individually, not as a whole.

galloppinggryphon commented 4 years ago

entry_type is indeed an existing field and the clause filters data the way I would expect. With the workaround, I remove the error too.

The code is a PHP array `[ 'where' => "entry_type.meta_value = 'organization'" ] so the quotes are legit. I'm following instructions from https://pods.io/docs/code/pods/find/

Anyway, this is the code that triggers the error, 'Empty needle' (in classes/PodsData.php on line 3058): strpos( $traverse_recurse['params']->table, $wpdb->prefix )

The value of the needle, $wpdb->prefix, is empty or zero-length string (''), causing PHP to lose its head. The simple fix of checking if $wpdb->prefix is empty appears to have solved the issue.

WP is running on PHP 7.2. I wrote a strpos test on PHP 7.3 and 7.4, but it was just as unhappy about an empty needle. Funnily enough, it had no problems with accepting null or an undeclared variable. Aaah, PHP.

JoryHogeveen commented 4 years ago

Hi @galloppinggryphon

That is strange.. $wpdb->prefix should never be empty, or did you omit the prefix in general for this installation?

JoryHogeveen commented 4 years ago

Hi @galloppinggryphon

I've created a patch that should fix your issue. See https://github.com/pods-framework/pods/pull/5816 If you could confirm this fix that would be great!

Cheers, Jory