4images PHP Cross Reference Image Galleries

Source: /search.php - 335 lines - 13195 bytes - Summary - Text - Print

   1  <?php
   2  /**************************************************************************
   3   *                                                                        *
   4   *    4images - A Web Based Image Gallery Management System               *
   5   *    ----------------------------------------------------------------    *
   6   *                                                                        *
   7   *             File: search.php                                           *
   8   *        Copyright: (C) 2002-2012 Jan Sorgalla                           *
   9   *            Email: jan@4homepages.de                                    *
  10   *              Web: http://www.4homepages.de                             *
  11   *    Scriptversion: 1.7.11                                               *
  12   *                                                                        *
  13   *    Never released without support from: Nicky (http://www.nicky.net)   *
  14   *                                                                        *
  15   **************************************************************************
  16   *                                                                        *
  17   *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
  18   *    bedingungen (Lizenz.txt) für weitere Informationen.                 *
  19   *    ---------------------------------------------------------------     *
  20   *    This script is NOT freeware! Please read the Copyright Notice       *
  21   *    (Licence.txt) for further information.                              *
  22   *                                                                        *
  23   *************************************************************************/
  24  
  25  $main_template = 'search';
  26  
  27  define('GET_CACHES', 1);
  28  define('ROOT_PATH', './');
  29  define('MAIN_SCRIPT', __FILE__);
  30  include (ROOT_PATH.'global.php');
  31  require (ROOT_PATH.'includes/sessions.php');
  32  $user_access = get_permission();
  33  include (ROOT_PATH.'includes/search_utils.php');
  34  
  35  $org_search_keywords = $search_keywords;
  36  $org_search_user = $search_user;
  37  
  38  if (isset($HTTP_GET_VARS['search_terms']) || isset($HTTP_POST_VARS['search_terms'])) {
  39    $search_terms = isset($HTTP_POST_VARS['search_terms']) ? $HTTP_POST_VARS['search_terms'] : $HTTP_GET_VARS['search_terms'];
  40    $search_terms = $search_terms == "all" ? 1 : 0;
  41  }
  42  else {
  43    $search_terms = 0;
  44  }
  45  
  46  if (isset($HTTP_GET_VARS['search_fields']) || isset($HTTP_POST_VARS['search_fields'])) {
  47    $search_fields = isset($HTTP_POST_VARS['search_fields']) ? trim($HTTP_POST_VARS['search_fields']) : trim($HTTP_GET_VARS['search_fields']);
  48  }
  49  else {
  50    $search_fields = "all";
  51  }
  52  
  53  $search_cat = $cat_id;
  54  
  55  $search_id = array();
  56  
  57  if ($search_user != "" && $show_result == 1) {
  58    $search_user = str_replace('*', '%', trim($search_user));
  59    $sql = "SELECT ".get_user_table_field("", "user_id")."
  60            FROM ".USERS_TABLE."
  61            WHERE ".get_user_table_field("", "user_name")." LIKE '$search_user'";
  62    $result = $site_db->query($sql);
  63    $search_id['user_ids'] = "";
  64    if ($result) {
  65      while ($row = $site_db->fetch_array($result)) {
  66        $search_id['user_ids'] .= (($search_id['user_ids'] != "") ? ", " : "").$row[$user_table_fields['user_id']];
  67      }
  68      $site_db->free_result($result);
  69    }
  70  }
  71  
  72  if ($search_keywords != "" && $show_result == 1) {
  73    $split_words = prepare_searchwords_for_search($search_keywords);
  74  
  75    $match_field_sql = ($search_fields != "all" && isset($search_match_fields[$search_fields])) ? "AND m.".$search_match_fields[$search_fields]." = 1" : "";
  76    $search_word_cache = array();
  77    for ($i = 0; $i < sizeof($split_words); $i++) {
  78      if ($split_words[$i] == "and" || $split_words[$i] == "und" || $split_words[$i] == "or" || $split_words[$i] == "oder" || $split_words[$i] == "not") {
  79        $search_word_cache[$i] = ($search_terms) ? "and" : $split_words[$i];
  80      }
  81      else {
  82        $curr_words = $split_words[$i];
  83        if (!is_array($curr_words)) {
  84            $curr_words = array($curr_words);
  85        }
  86  
  87        $where = array();
  88        foreach ($curr_words as $curr_word) {
  89            $where[] = "w.word_text LIKE '".addslashes(str_replace("*", "%", $curr_word))."'";
  90        }
  91   
  92        $sql = "SELECT m.image_id
  93                FROM (".WORDLIST_TABLE." w, ".WORDMATCH_TABLE." m)
  94                WHERE (" . implode(' OR ', $where) . ")
  95                AND m.word_id = w.word_id
  96                $match_field_sql";
  97        $result = $site_db->query($sql);
  98        $search_word_cache[$i] = array();
  99        while ($row = $site_db->fetch_array($result)) {
 100          $search_word_cache[$i][$row['image_id']] = 1;
 101        }
 102        $site_db->free_result();
 103      }
 104    }
 105  
 106    $is_first_word = 1;
 107    $operator = "or";
 108    $image_id_list = array();
 109    for ($i = 0; $i < sizeof($search_word_cache); $i++) {
 110      if ($search_word_cache[$i] == "and" || $search_word_cache[$i] == "und" || $search_word_cache[$i] == "or" || $search_word_cache[$i] == "oder" || $search_word_cache[$i] == "not") {
 111        if (!$is_first_word) {
 112          $operator = $search_word_cache[$i];
 113        }
 114      }
 115      elseif (is_array($search_word_cache[$i])) {
 116        if ($search_terms) {
 117          $operator = "and";
 118        }
 119        foreach ($search_word_cache[$i] as $key => $val) {
 120          if ($is_first_word || $operator == "or" || $operator == "oder") {
 121            $image_id_list[$key] = 1;
 122          }
 123          elseif ($operator == "not") {
 124            unset($image_id_list[$key]);
 125          }
 126        }
 127        if (($operator == "and" || $operator == "und") && !$is_first_word) {
 128          foreach ($image_id_list as $key => $val) {
 129            if (!isset($search_word_cache[$i][$key])) {
 130              unset($image_id_list[$key]);
 131            }
 132          }
 133        }
 134      }
 135      $is_first_word = 0;
 136    }
 137  
 138    $search_id['image_ids'] = "";
 139    foreach ($image_id_list as $key => $val) {
 140      $search_id['image_ids'] .= (($search_id['image_ids'] != "") ? ", " : "").$key;
 141    }
 142    unset($image_id_list);
 143  }
 144  
 145  if ($search_new_images && $show_result == 1) {
 146    $search_id['search_new_images'] = 1;
 147  }
 148  
 149  if ($search_cat && $show_result == 1) {
 150    $search_id['search_cat'] = $search_cat;
 151  }
 152  
 153  if (!empty($search_id)) {
 154    $site_sess->set_session_var("search_id", serialize($search_id));
 155  }
 156  
 157  include (ROOT_PATH.'includes/page_header.php');
 158  
 159  $num_rows_all = 0;
 160  if ($show_result == 1) {
 161    if (empty($search_id)) {
 162      if (!empty($session_info['search_id'])) {
 163        $search_id = unserialize($session_info['search_id']);
 164      } else {
 165        $search_id = unserialize($site_sess->get_session_var("search_id"));
 166      }
 167    }
 168  
 169    $sql_where_query = "";
 170  
 171    if (!empty($search_id['image_ids'])) {
 172      $sql_where_query .= "AND i.image_id IN (".$search_id['image_ids'].") ";
 173    }
 174  
 175    if (!empty($search_id['user_ids'])) {
 176      $sql_where_query .= "AND i.user_id IN (".$search_id['user_ids'].") ";
 177    }
 178  
 179    if (!empty($search_id['search_new_images']) && $search_id['search_new_images'] == 1) {
 180      $new_cutoff = time() - 60 * 60 * 24 * $config['new_cutoff'];
 181      $sql_where_query .= "AND i.image_date >= $new_cutoff ";
 182    }
 183  
 184    if (!empty($search_id['search_cat']) && $search_id['search_cat'] != 0) {
 185      $cat_id_sql = 0;
 186      if (check_permission("auth_viewcat", $search_id['search_cat'])) {
 187        $sub_cat_ids = get_subcat_ids($search_id['search_cat'], $search_id['search_cat'], $cat_parent_cache);
 188        $cat_id_sql .= ", ".$search_id['search_cat'];
 189        if (!empty($sub_cat_ids[$search_id['search_cat']])) {
 190          foreach ($sub_cat_ids[$search_id['search_cat']] as $val) {
 191            if (check_permission("auth_viewcat", $val)) {
 192              $cat_id_sql .= ", ".$val;
 193            }
 194          }
 195        }
 196      }
 197      $cat_id_sql = $cat_id_sql !== 0 ? "AND i.cat_id IN ($cat_id_sql)" : "";
 198    }
 199    else {
 200      $cat_id_sql = get_auth_cat_sql("auth_viewcat", "NOTIN");
 201      $cat_id_sql = $cat_id_sql !== 0 ? "AND i.cat_id NOT IN (".$cat_id_sql.")" : "";
 202    }
 203  
 204    if (!empty($sql_where_query)) {
 205      $sql = "SELECT COUNT(*) AS num_rows_all
 206              FROM ".IMAGES_TABLE." i
 207              WHERE i.image_active = 1 $sql_where_query
 208              $cat_id_sql";
 209      $row = $site_db->query_firstrow($sql);
 210      $num_rows_all = $row['num_rows_all'];
 211    }
 212  }
 213  
 214  if (!$num_rows_all && $show_result == 1)  {
 215    $msg = preg_replace("/".$site_template->start."search_keywords".$site_template->end."/", $search_keywords, $lang['search_no_results']);
 216  }
 217  
 218  //-----------------------------------------------------
 219  //--- Show Search Results -----------------------------
 220  //-----------------------------------------------------
 221  if ($num_rows_all && $show_result == 1)  {
 222    $link_arg = $site_sess->url(ROOT_PATH."search.php?show_result=1");
 223  
 224    include (ROOT_PATH.'includes/paging.php');
 225    $getpaging = new Paging($page, $perpage, $num_rows_all, $link_arg);
 226    $offset = $getpaging->get_offset();
 227    $site_template->register_vars(array(
 228      "paging" => $getpaging->get_paging(),
 229      "paging_stats" => $getpaging->get_paging_stats()
 230    ));
 231  
 232    $imgtable_width = ceil((intval($config['image_table_width'])) / $config['image_cells']);
 233    if ((substr($config['image_table_width'], -1)) == "%") {
 234      $imgtable_width .= "%";
 235    }
 236  
 237    $additional_sql = "";
 238    if (!empty($additional_image_fields)) {
 239      foreach ($additional_image_fields as $key => $val) {
 240        $additional_sql .= ", i.".$key;
 241      }
 242    }
 243  
 244    $sql = "SELECT i.image_id, i.cat_id, i.user_id, i.image_name, i.image_description, i.image_keywords, i.image_date, i.image_active, i.image_media_file, i.image_thumb_file, i.image_download_url, i.image_allow_comments, i.image_comments, i.image_downloads, i.image_votes, i.image_rating, i.image_hits".$additional_sql.", c.cat_name".get_user_table_field(", u.", "user_name")."
 245            FROM (".IMAGES_TABLE." i,  ".CATEGORIES_TABLE." c)
 246            LEFT JOIN ".USERS_TABLE." u ON (".get_user_table_field("u.", "user_id")." = i.user_id)
 247            WHERE i.image_active = 1
 248            $sql_where_query
 249            AND c.cat_id = i.cat_id $cat_id_sql
 250            ORDER BY ".$config['image_order']." ".$config['image_sort'].", image_id ".$config['image_sort']."
 251            LIMIT $offset, $perpage";
 252    $result = $site_db->query($sql);
 253  
 254    $thumbnails = "<table width=\"".$config['image_table_width']."\" border=\"0\" cellpadding=\"".$config['image_table_cellpadding']."\" cellspacing=\"".$config['image_table_cellspacing']."\">\n";
 255  
 256    $count = 0;
 257    $bgcounter = 0;
 258    while ($image_row = $site_db->fetch_array($result)) {
 259      if ($count == 0) {
 260        $row_bg_number = ($bgcounter++ % 2 == 0) ? 1 : 2;
 261        $thumbnails .= "<tr class=\"imagerow".$row_bg_number."\">\n";
 262      }
 263      $thumbnails .= "<td width=\"".$imgtable_width."\" valign=\"top\">\n";
 264      show_image($image_row, "search");
 265      $thumbnails .= $site_template->parse_template("thumbnail_bit");
 266      $thumbnails .= "\n</td>\n";
 267      $count++;
 268      if ($count == $config['image_cells']) {
 269        $thumbnails .= "</tr>\n";
 270        $count = 0;
 271      }
 272    } // end while
 273    if ($count > 0)  {
 274      $leftover = ($config['image_cells'] - $count);
 275      if ($leftover >= 1) {
 276        for ($i = 0; $i < $leftover; $i++) {
 277          $thumbnails .= "<td width=\"".$imgtable_width."\">\n&nbsp;\n</td>\n";
 278        }
 279        $thumbnails .= "</tr>\n";
 280      }
 281    }
 282    $thumbnails .= "</table>\n";
 283    $content = $thumbnails;
 284    unset($thumbnails);
 285  } // end if
 286  else {
 287    $site_template->register_vars(array(
 288      "search_keywords" => format_text(stripslashes($org_search_keywords), 2),
 289      "search_user" => format_text(stripslashes($org_search_user), 2),
 290      "lang_search_by_keyword" => $lang['search_by_keyword'],
 291      "lang_search_by_username" => $lang['search_by_username'],
 292      "lang_new_images_only" => $lang['new_images_only'],
 293      "lang_search_terms" => $lang['search_terms'],
 294      "lang_or" => $lang['or'],
 295      "lang_and" => $lang['and'],
 296      "lang_category" => $lang['category'],
 297      "lang_search_fields" => $lang['search_fields'],
 298      "lang_all_fields" => $lang['all_fields'],
 299      "lang_name_only" => $lang['name_only'],
 300      "lang_description_only" => $lang['description_only'],
 301      "lang_keywords_only" => $lang['keywords_only'],
 302      "category_dropdown" => get_category_dropdown($cat_id)
 303    ));
 304  
 305    if (!empty($additional_image_fields)) {
 306      $additional_field_array = array();
 307      foreach ($additional_image_fields as $key => $val) {
 308        if (isset($lang[$key.'_only'])) {
 309          $additional_field_array['lang_'.$key.'_only'] = $lang[$key.'_only'];
 310        }
 311      }
 312      if (!empty($additional_field_array)) {
 313        $site_template->register_vars($additional_field_array);
 314      }
 315    }
 316    $content = $site_template->parse_template("search_form");
 317  }
 318  
 319  //-----------------------------------------------------
 320  //--- Clickstream -------------------------------------
 321  //-----------------------------------------------------
 322  $clickstream = "<span class=\"clickstream\"><a href=\"".$site_sess->url(ROOT_PATH."index.php")."\" class=\"clickstream\">".$lang['home']."</a>".$config['category_separator'].$lang['search']."</span>";
 323  
 324  //-----------------------------------------------------
 325  //--- Print Out ---------------------------------------
 326  //-----------------------------------------------------
 327  $site_template->register_vars(array(
 328    "content" => $content,
 329    "msg" => $msg,
 330    "clickstream" => $clickstream,
 331    "lang_search" => $lang['search']
 332  ));
 333  $site_template->print_template($site_template->parse_template($main_template));
 334  include (ROOT_PATH.'includes/page_footer.php');
 335  ?>

title

Description

title

Description

title

Description

title

title

Body