If you browse to /image and and in a group that does not have "administer_tac_lite" access in the access control page you are presented with an error message:

  • warning: pg_query() [function.pg-query]: Query failed: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list in /usr/share/drupal/includes/database.pgsql.inc on line 84.
  • user warning:
    query: SELECT DISTINCT(n.nid) FROM node n INNER JOIN term_node tn ON n.nid = tn.nid INNER JOIN node_access na ON na.nid = n.nid WHERE (na.grant_view >= 1 AND ((na.gid = -1 AND na.realm = 'all') OR (na.gid = 15 AND na.realm = 'tac_lite') OR (na.gid = 15 AND na.realm = 'tac_lite') OR (na.gid = 17 AND na.realm = 'tac_lite') OR (na.gid = 18 AND na.realm = 'tac_lite') OR (na.gid = 0 AND na.realm = 'tac_lite'))) AND tn.tid IN (19) AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC LIMIT 1 OFFSET 0 in /usr/share/drupal/includes/database.pgsql.inc on line 103.
  • warning: pg_query() [function.pg-query]: Query failed: ERROR: invalid input syntax for integer: "" in /usr/share/drupal/includes/database.pgsql.inc on line 84.
  • user warning:
    query: SELECT DISTINCT(n.nid), n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.moderate, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid INNER JOIN node_access na ON na.nid = n.nid WHERE (na.grant_view >= 1 AND ((na.gid = -1 AND na.realm = 'all') OR (na.gid = 15 AND na.realm = 'tac_lite') OR (na.gid = 15 AND na.realm = 'tac_lite') OR (na.gid = 17 AND na.realm = 'tac_lite') OR (na.gid = 18 AND na.realm = 'tac_lite') OR (na.gid = 0 AND na.realm = 'tac_lite'))) AND n.nid = '' in /usr/share/drupal/includes/database.pgsql.inc on line 103.

If I enable access to "administer_tac_lite" for the group that the user I am is in it works.
So now everyone has "administer_tac_lite" access... I haven't found a place where this adds any functionality to people who shouldn't have it - as the user access control seems to be taking care of that, but it does seem like a bug.

Comments

JamieR’s picture

Found the security risk:
On the "my account" page the user can access tac_lite settings for themselves and change thier level of access.

JamieR’s picture

Status: Active » Closed (fixed)

I closed this as I looks like this has more to do with the image.module than tac_lite. Thanks.

JamieR’s picture

Project: Taxonomy Access Control Lite » Image
Component: Code » image_gallery
Status: Closed (fixed) » Active

Moving to image.module

Dave Cohen’s picture

Just FYI, the tests with na.realm='tac_lite' are added by tac_lite in hook_db_rewrite_sql. They are designed to alter the query to return only nodes the current user is allowed to see. If they are in fact causing a problem, please let me know.

If you give users administer_tac_lite privelege, it should bypass adding that stuff to the query. So if doing that solves the problem, the problem may be somehow caused by db_rewrite_sql. You should not give those users that permission, however. It will allow them to see all content as if tac_lite were not installed. Perhaps in your case you do not need it???

I wrote tac_lite. I don't have a postgres environment to test. I've only tried it with mysql.

JamieR’s picture

Project: Image » Taxonomy Access Control Lite
Component: image_gallery » Code

Excellent - just the guy I'd want to talk to then! Yes the module is behaving exactly as you describe. All users can view all the image galleries. Which is actually bad. I just hadn't found that until after I brought up the other issue. Please let me know how I can help you. How I can help you help me! ;)

Thanks a lot - I guess I should move this back to tac_lite...

Dave Cohen’s picture

Project: Taxonomy Access Control Lite » Image
Component: Code » image.module

I'm pretty sure the issue is not with tac_lite so I'm moving it back to image. Not sure it's there, either. It's hard to know which module is generating those queries.

You have two problems, each query is failing for a different reason.

The first is related to http://drupal.org/node/69560. Change the query that begins "SELECT DISTINCT(n.nid) FROM node n" to begin "SELECT DISTINCT(n.nid), n.sticky, n.created FROM node n". In other words, put all the fields used in the order by clause into the select clause.

The second query simply ends with "AND n.nid = ". There needs to be a numerical nid there. Probably a case of code not checking that an nid exists before executing the query.

I don't use the image module or postgres, so I may have reached my limit on how to help with this.

JamieR’s picture

Status: Active » Reviewed & tested by the community

I added the ORDER BY fields to the SELECT statements in the two query strings in function image_gallery_page. They end up looking like this:

/**
 * Image gallery callback, displays an image gallery
 */
function image_gallery_page($type = NULL, $tid = 0) {
  $galleries = taxonomy_get_tree(_image_gallery_get_vid(), $tid, -1, 1);
  for ($i=0; $i < count($galleries); $i++) {
    $galleries[$i]->count = taxonomy_term_count_nodes($galleries[$i]->tid, 'image');
    $tree = taxonomy_get_tree(_image_gallery_get_vid(), $galleries[$i]->tid, -1);
    $descendant_tids = array_merge(array($galleries[$i]->tid), array_map('_taxonomy_get_tid_from_term', $tree));
    $last = db_fetch_object(
      db_query_range(
        db_rewrite_sql(
          'SELECT n.nid, n.sticky, n.created
          FROM {node} n 
          INNER JOIN {term_node} tn 
          ON n.nid = tn.nid 
          WHERE tn.tid IN (%s) 
          AND n.status = 1 
          ORDER BY n.sticky DESC, n.created DESC'
        ), 
        implode(',', $descendant_tids), 0, 1
      )
    );
    $galleries[$i]->latest = node_load(array('nid' => $last->nid));
  }

  $images = array();
  if ($tid) {
    $result = pager_query(
      db_rewrite_sql(
        "SELECT n.nid, n.sticky, n.created
         FROM {term_node} t 
         INNER JOIN {node} n 
         ON t.nid=n.nid 
         WHERE n.status=1 
         AND n.type='image' 
         AND t.tid=%d 
         ORDER BY n.sticky DESC, n.created DESC"
      ), variable_get('image_images_per_page', 6), 0, NULL, $tid
    );

This fixed the first error I was getting but the other persisted:

warning: pg_query() [function.pg-query]: Query failed: ERROR: invalid input syntax for integer: "" in /usr/share/drupal/includes/database.pgsql.inc on line 84.
user warning: query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.moderate, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE n.nid = '' in /usr/share/drupal/includes/database.pgsql.inc on line 103.

I was unable to track down anything in the image_gallery.module or the tac_lite.module having to do with this error. So, I went and created a new Drupal installation and started over with these two modules, thinking that perhaps the error was having to do with the other error saving a wrong setting in the database.

The good news is that the new installation worked! No errors. Tested all options and configurations that I could think of. the bad news is that I have put A LOT of work into my installation that is still getting the errors, and I could really use some help finding that bug and getting it out of my database!

I've tried:
- enabling/unenabling the gallery and tac_lite modules
- removing some image gallery related row in the variable and vocabulary database tables
- dropping all the image nodes and recreating them as well as the gallery pages

Any other suggestions? I should probably post this on the forums... more traffic.

This revision should be implemented into the repository

Thank you! Jamie.

JamieR’s picture

Tracked down the other error. It had nothing to do with a clean installation. Here is are the patches. I've done two more but can't commit into this project. Someone please help.

First one fixes the error messages that result from querying when there are no nodes showing up in a gallery:

/**
 * Image gallery callback, displays an image gallery
 */
function image_gallery_page($type = NULL, $tid = 0) {
  $galleries = taxonomy_get_tree(_image_gallery_get_vid(), $tid, -1, 1);
  for ($i=0; $i < count($galleries); $i++) {
    $galleries[$i]->count = taxonomy_term_count_nodes($galleries[$i]->tid, 'image');
    $tree = taxonomy_get_tree(_image_gallery_get_vid(), $galleries[$i]->tid, -1);
    $descendant_tids = array_merge(
          array($galleries[$i]->tid), array_map('_taxonomy_get_tid_from_term', $tree));
    $db_rewrite_var = db_rewrite_sql(
      'SELECT n.nid, n.sticky, n.created
       FROM {node} n 
       INNER JOIN {term_node} tn 
       ON n.nid = tn.nid 
       WHERE tn.tid IN (%s) 
       AND n.status = 1 
       ORDER BY n.sticky DESC, n.created DESC'
    );
    $db_query_range_var = db_query_range($db_rewrite_var, implode(',', $descendant_tids), 0, 1);
    $last = db_fetch_object($db_query_range_var);
    if($last->nid > 0) { $galleries[$i]->latest = node_load(array('nid' => $last->nid)); }
  }

The important line is:
if($last->nid > 0) { $galleries[$i]->latest = node_load(array('nid' => $last->nid)); }
The rest of the changes were just so that I could debug.

The second patch just hides the display of the gallery if there are no images in it (or if no images show up because of tac_lite - the gallery page itself can't be controled by tac_lite because it is not a node)

/**
 * Theme a gallery page
 */
function theme_image_gallery($galleries, $images) {
  drupal_set_html_head(theme('stylesheet_import', base_path() . drupal_get_path('module', 'image_gallery') .'/image_gallery.css'));
  //
  // We'll add height to keep thumbnails lined up.
  $size = _image_get_dimensions('thumbnail');
  $width = $size['width'];
  $height = $size['height'];
  //
  $content = '';
  if (count($galleries)) {
    $content.= '<ul class="galleries">';
    foreach ($galleries as $gallery) {
      if ($gallery->count > 0) {
        $content .= '<li style="height : '.$height .'px">';
        if ($gallery->count)
          $content.= l(image_display($gallery->latest, 'thumbnail'), 'image/tid/'.$gallery->tid, array(), NULL, NULL, FALSE, TRUE);
        $content.= "<h3>".l($gallery->name, 'image/tid/'.$gallery->tid) . "</h3>\n";
        $content.= '<div class="description">'. check_markup($gallery->description) ."</div>\n";
        $content.= '<p class="count">' . format_plural($gallery->count, 'There is 1 image in this gallery', 'There are %count images in this gallery') . "</p>\n";
        if ($gallery->latest->changed) {
          $content.= '<p class="last">'. t('Last updated: %date', array('%date' => format_date($gallery->latest->changed))) . "</p>\n";
        }
        $content.= "</li>\n";
      }
    }
    $content.= "</ul>\n";
  }

Here the important lines are:
if ($gallery->count > 0) {
and the '}' that closes the statement.

I really hope this helps someone else, as I've spent more time on it than I'd have liked. ;)

JamieR’s picture

Component: image.module » image_gallery
Assigned: Unassigned » JamieR
StatusFileSize
new12.78 KB

I'm attaching the revised image_gallery.module for someone to commit. Thanks!

walkah’s picture

Status: Reviewed & tested by the community » Needs work

can you please provide a proper patch file? Thanks.

For more information check : http://drupal.org/diffandpatch

JamieR’s picture

Status: Needs work » Needs review
StatusFileSize
new5.49 KB

Thanks for bearing with me - my first patch file... There are a few style changes that show up in the patch file that aren't really necessary, but that helped me troubleshoot. If you don't like them, can you just edit the patch file? Let me know if I should do something. Thank you!

drewish’s picture

Status: Needs review » Needs work

the patch is correct but you've added newlines to break up several of the queries. this makes it very hard to visually inspect the changes and is discouraged.

sun’s picture

Version: 4.7.x-1.x-dev » 6.x-1.x-dev
Assigned: JamieR » Unassigned
xurizaemon’s picture

removing line breaks in SQL queries

sun’s picture

Needs to be re-rolled, because some other patches landed. Also, the coding-style lacks in some places. Please read http://drupal.org/coding-standards

sun’s picture

Status: Needs work » Closed (cannot reproduce)

Sorry, but it doesn't look like this is still an issue.

Feel free to re-open this issue if you want to provide further information. Thanks.