За последние 24 часа нас посетили 28097 программистов и 1815 роботов. Сейчас ищут 2005 программистов ...

Графический поиск

Тема в разделе "PHP для новичков", создана пользователем vaneeeek, 19 июн 2011.

  1. vaneeeek

    vaneeeek Активный пользователь

    С нами с:
    13 июн 2011
    Сообщения:
    68
    Симпатии:
    0
    Адрес:
    москва
    Привет всем.Так как я не нашёл ответа на свой прошлый вопрос,про поиск,то я нашел одну галерею изображений типо скрипт,и вытащил оттуда сам скрипт поиска.


    Вот он
    HTML:
    1. <table border="0" cellspacing="0" cellpadding="1">
    2.  
    3.                 <tr>
    4.                   <td>
    5.                     <input type="text" name="search_keywords" size="15" class="searchinput" />
    6.                   </td>
    7.                   <td>
    8.                     <input type="submit" value="Suchen" class="button" name="submit" />
    9.                   </td>
    10.                 </tr>
    11.                 <tr valign="top">
    12.                   <td colspan="2"><a href="./search.php" class="smalltext">Erweiterte Suche</a></td>
    13.  
    14.                 </tr>
    15.               </table>
    16.  





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


    Вот 3 файл search_utils.php

    PHP:
    1.  
    2. <?php
    3. /**************************************************************************
    4.  *                                                                        *
    5.  *    4images - A Web Based Image Gallery Management System               *
    6.  *    ----------------------------------------------------------------    *
    7.  *                                                                        *
    8.  *             File: search_utils.php                                     *
    9.  *        Copyright: (C) 2002-2011 Jan Sorgalla                           *
    10.  *            Email: [email=jan@4homepages.de]jan@4homepages.de[/email]                                    *
    11.  *              Web: [url=http://www.4homepages.de]http://www.4homepages.de[/url]                             *
    12.  *    Scriptversion: 1.7.10                                               *
    13.  *                                                                        *
    14.  *    Never released without support from: Nicky ([url=http://www.nicky.net]http://www.nicky.net[/url])   *
    15.  *                                                                        *
    16.  **************************************************************************
    17.  *                                                                        *
    18.  *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
    19.  *    bedingungen (Lizenz.txt) fьr weitere Informationen.                 *
    20.  *    ---------------------------------------------------------------     *
    21.  *    This script is NOT freeware! Please read the Copyright Notice       *
    22.  *    (Licence.txt) for further information.                              *
    23.  *                                                                        *
    24.  *************************************************************************/
    25. if (!defined('ROOT_PATH')) {
    26.   die("Security violation");
    27. }
    28.  
    29. if (!$search_match_fields) {
    30.   $search_match_fields = array(
    31.     "image_name"        => "name_match",
    32.     "image_description" => "desc_match",
    33.     "image_keywords"    => "keys_match",
    34.   );
    35. }
    36.  
    37. if (!$search_index_types) {
    38.   /*
    39.    * Types are:
    40.    *
    41.    *   fulltext: Content will be split up by whitespaces. Words will be normalized and cleaned up.
    42.    *   keywords: Content will be split up by comma. Words will NOT be normalized and cleaned up.
    43.    *   phrase:   Content will NOT be split up. Words will NOT be normalized and cleaned up.
    44.    *
    45.    * Note that max. length of the words is 50 chars. This means that MAX_SEARCH_KEYWORD_LENGTH cannot exceed 50 chars (default is 25).
    46.    */
    47.   $search_index_types = array(
    48.     "image_name"        => "fulltext",
    49.     "image_description" => "fulltext",
    50.     "image_keywords"    => "keywords",
    51.   );
    52. }
    53.  
    54. function convert_special($text) {
    55.   return strtr(
    56.     $text,
    57.     array(
    58.       "Д" => "AE",
    59.       "Ц" => "OE",
    60.       "Ь" => "UE",
    61.       "д" => "ae",
    62.       "ц" => "oe",
    63.       "ь" => "ue",
    64.       "Я" => "ss"
    65.     )
    66.   );
    67. }
    68.  
    69. function clean_search_word($val) {
    70.   $val = strip_tags(trim(stripslashes($val)));
    71.   $val = convert_special($val);
    72.   $val = strtolower($val);
    73.   $val = preg_replace('/[\n\t\r]+/', ' ', $val);
    74.  
    75.   return $val;
    76. }
    77.  
    78. function normalize_search_word($val) {
    79.   $search_array = array(
    80.     "/&(?!(#[0-9]+|[a-z]+);)/si",
    81.     "#([^]_a-z0-9-=\"'\/])([a-z]+?)://([^, \(\)<>\n\r]+)#si",
    82.     "#([^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^, \(\)<>\n\r]*)?)#si",
    83.     "#[-_'`ґ\^\$\(\)<>\"\|,@\?%~\+\.\[\]{}:\/=!§\\\\]+#s"
    84.   );
    85.  
    86.   $replace_array = array(
    87.     " ",
    88.     " ",
    89.     " ",
    90.     ""
    91.   );
    92.  
    93.   $val = preg_replace($search_array, $replace_array, $val);
    94.  
    95.   return $val;
    96. }
    97.  
    98. function prepare_searchwords_for_search($val)
    99. {
    100.     $val = clean_search_word($val);
    101.     $val = preg_replace('/\s+/', ' ', $val);
    102.  
    103.     $stopword_list = get_stopwords();
    104.  
    105.     $tokens = array();
    106.     $modifier = null;
    107.     for ($nextToken = strtok($val, ' '); $nextToken !== false; $nextToken = strtok(' ')) {
    108.       if ($nextToken[0] == '"') {
    109.         $nextToken = $nextToken[strlen($nextToken)-1] == '"' ? substr($nextToken, 1, -1) : substr($nextToken, 1) . ' ' . strtok('"');
    110.       } elseif ($nextToken[0] == '+') {
    111.         $modifier = 'and';
    112.         $nextToken = substr($nextToken, 1);
    113.       } elseif ($nextToken[0] == '-') {
    114.         $modifier = 'not';
    115.         $nextToken = substr($nextToken, 1);
    116.       } elseif ($nextToken == 'or' || $nextToken == 'oder') {
    117.         $modifier = null;
    118.         continue;
    119.       } elseif ($nextToken == 'and' || $nextToken == 'und') {
    120.         $modifier = 'and';
    121.         continue;
    122.       } elseif ($nextToken == 'not') {
    123.         $modifier = 'not';
    124.         continue;
    125.       }
    126.  
    127.       $nextToken = trim($nextToken);
    128.  
    129.       if ($nextToken != '') {
    130.         $len = strlen(preg_replace("/&(#[0-9]+|[a-z]+);/siU", "_", $nextToken));
    131.         if ($len >= MIN_SEARCH_KEYWORD_LENGTH && $len <= MAX_SEARCH_KEYWORD_LENGTH && !in_array($nextToken, $stopword_list)) {
    132.             if ($modifier) {
    133.               $tokens[] = $modifier;
    134.             }
    135.  
    136.             $normalized = normalize_search_word($nextToken);
    137.  
    138.             if (trim($normalized) != '' && $normalized != $nextToken) {
    139.               $nextToken = array($nextToken, $normalized);
    140.             }
    141.  
    142.             $tokens[] = $nextToken;
    143.         }
    144.       }
    145.  
    146.       $modifier = null;
    147.     }
    148.  
    149.     return $tokens;
    150. }
    151.  
    152. function prepare_searchwords($val, $for_search = false)
    153. {
    154.   // Backwards compatibility
    155.   if ($for_search) {
    156.     return prepare_searchwords_for_search($val);
    157.   }
    158.  
    159.   if (!is_array($val)) {
    160.     $val = clean_search_word($val);
    161.     $val = normalize_search_word($val);
    162.     $val = str_replace("*", "", $val);
    163.  
    164.     if (empty($val)) {
    165.       return array();
    166.     }
    167.  
    168.     $split_words = preg_split("/\s+/", $val);
    169.   } else {
    170.     $split_words = $val;
    171.     $split_words = array_map('clean_search_word', $split_words);
    172.     $split_words = array_unique(array_filter($split_words));
    173.   }
    174.  
    175.   $stopword_list = get_stopwords();
    176.   $clean_words = array();
    177.  
    178.   foreach ($split_words as $word) {
    179.     $word = trim($word);
    180.     if ($word == "") {
    181.       continue;
    182.     }
    183.  
    184.     if ($word == "and" || $word == "und" || $word == "or" || $word == "oder" || $word == "not") {
    185.       continue;
    186.     }
    187.  
    188.     $len = strlen(preg_replace("/&(#[0-9]+|[a-z]+);/siU", "_", $word));
    189.     if ($len >= MIN_SEARCH_KEYWORD_LENGTH && $len <= MAX_SEARCH_KEYWORD_LENGTH && !in_array($word, $stopword_list)) {
    190.       $clean_words[] = $word;
    191.     }
    192.   }
    193.  
    194.   return $clean_words;
    195. }
    196.  
    197. function add_searchwords($image_id = 0, $raw_words = array()) {
    198.   global $site_db, $search_match_fields, $search_index_types;
    199.  
    200.   if (!$image_id || empty($raw_words)) {
    201.     return false;
    202.   }
    203.  
    204.   $match_table_fields = $site_db->get_table_fields(WORDMATCH_TABLE);
    205.  
    206.   $clean_words = array();
    207.   $allwords_sql = "";
    208.  
    209.   foreach ($raw_words as $key => $val) {
    210.     if (isset($search_index_types[$key])) {
    211.       $type = $search_index_types[$key];
    212.     } else {
    213.       $type = 'fulltext';
    214.     }
    215.  
    216.     switch ($type) {
    217.       case 'phrase':
    218.         if (is_array($val)) {
    219.           $val = implode(' ', $val);
    220.         }
    221.         $split_words = prepare_searchwords(array($val));
    222.         break;
    223.       case 'keywords':
    224.         if (!is_array($val)) {
    225.           $val = explode(',', $val);
    226.         }
    227.         $split_words = prepare_searchwords($val);
    228.         break;
    229.       case 'fulltext':
    230.       default:
    231.         if (is_array($val)) {
    232.           $val = implode(' ', $val);
    233.         }
    234.         $split_words = prepare_searchwords($val);
    235.         break;
    236.     }
    237.  
    238.     if (empty($split_words)) {
    239.       continue;
    240.     }
    241.  
    242.     $word_cache = array();
    243.     foreach ($split_words as $word) {
    244.       $word_cache[$word] = 1;
    245.       $allwords_sql .= ($allwords_sql != "") ? ", '".$word."'" : "'".$word."'";
    246.     }
    247.     if (!empty($word_cache)) {
    248.       $clean_words[$key] = $word_cache;
    249.     }
    250.   }
    251.  
    252.   $word_exists = array();
    253.   if ($allwords_sql != "") {
    254.     $sql = "SELECT word_text, word_id
    255.            FROM ".WORDLIST_TABLE."
    256.            WHERE word_text IN ($allwords_sql)";
    257.     $result = $site_db->query($sql);
    258.  
    259.     while ($row = $site_db->fetch_array($result)) {
    260.       $word_exists[$row['word_text']] = $row['word_id'];
    261.     }
    262.     $site_db->free_result();
    263.   }
    264.  
    265.   $word_done = array();
    266.   $new_words = array();
    267.   $word_insert_sql = "";
    268.   foreach ($clean_words as $key => $val) {
    269.     foreach ($val as $key2 => $val2) {
    270.       if (!isset($word_done[$key2])) {
    271.         $word_done[$key2] = 1;
    272.         if (isset($word_exists[$key2])) {
    273.           $word_insert_sql .= (($word_insert_sql != "" ) ? ", " : "")."(".$image_id.", ".$word_exists[$key2];
    274.           foreach ($search_match_fields as $key3 => $val3) {
    275.             if (isset($match_table_fields[$val3])) {
    276.               $match = (isset($clean_words[$key3][$key2])) ? 1 : 0;
    277.               $word_insert_sql .= ", ".$match;
    278.             }
    279.           }
    280.           $word_insert_sql .= ")";
    281.         }
    282.         else {
    283.           $new_words[$key2] = array();
    284.           foreach ($search_match_fields as $key3 => $val3) {
    285.             $match = (isset($clean_words[$key3][$key2])) ? 1 : 0;
    286.             $new_words[$key2][$val3] = $match;
    287.           }
    288.         }
    289.       }
    290.     }
    291.   }
    292.  
    293.   if ($word_insert_sql != "") {
    294.     $match_image_fields_sql = "";
    295.     foreach ($search_match_fields as $field) {
    296.       $match_image_fields_sql .= ", ".$field;
    297.     }
    298.     $sql = "REPLACE INTO ".WORDMATCH_TABLE."
    299.            (image_id, word_id".$match_image_fields_sql.")
    300.            VALUES
    301.            $word_insert_sql";
    302.     $site_db->query($sql);
    303.   }
    304.  
    305.   if (!empty($new_words)) {
    306.     $value_sql = "";
    307.     foreach ($new_words as $key => $val) {
    308.       $value_sql .= (($value_sql != "") ? ", " : "")."('".addslashes($key)."', NULL)";
    309.     }
    310.     if ($value_sql != "") {
    311.       $sql = "INSERT IGNORE INTO ".WORDLIST_TABLE." (word_text, word_id)
    312.              VALUES $value_sql";
    313.       $site_db->query($sql);
    314.     }
    315.  
    316.     foreach ($new_words as $key => $val) {
    317.       $match_insert_key_sql = "";
    318.       $match_insert_val_sql = "";
    319.       foreach ($search_match_fields as $field) {
    320.         if (isset($match_table_fields[$field])) {
    321.           $match_insert_key_sql .= ", ".$field;
    322.           $match_insert_val_sql .= ", ".$val[$field];
    323.         }
    324.       }
    325.       $sql = "INSERT INTO ".WORDMATCH_TABLE." (image_id, word_id".$match_insert_key_sql.")
    326.              SELECT DISTINCT $image_id, word_id".$match_insert_val_sql."
    327.                FROM ".WORDLIST_TABLE."
    328.                WHERE word_text = '$key'";
    329.       $site_db->query($sql);
    330.     }
    331.   }
    332.   return true;
    333. }
    334.  
    335. function remove_searchwords($image_ids_sql = "") {
    336.   global $site_db;
    337.  
    338.   if (empty($image_ids_sql)) {
    339.     return false;
    340.   }
    341.  
    342.   foreach (explode(',', $image_ids_sql) as $image_id) {
    343.     $image_id = intval($image_id);
    344.     $sql = "SELECT word_id
    345.            FROM ".WORDMATCH_TABLE."
    346.            WHERE image_id = $image_id";
    347.     $result = $site_db->query($sql);
    348.     $all_word_id_sql = "";
    349.     while ($row = $site_db->fetch_array($result)) {
    350.       $all_word_id_sql .= (($all_word_id_sql != "") ? ", " : "").$row['word_id'];
    351.     }
    352.  
    353.     if ($all_word_id_sql != "") {
    354.       $sql = "SELECT word_id, COUNT(word_id) as word_id_count
    355.              FROM ".WORDMATCH_TABLE."
    356.              WHERE word_id IN ($all_word_id_sql)
    357.              GROUP BY word_id";
    358.       $result = $site_db->query($sql);
    359.  
    360.       $word_id_delete_sql = "";
    361.       while ($row = $site_db->fetch_array($result)) {
    362.         if ($row['word_id_count'] == 1) {
    363.           $word_id_delete_sql .= (($word_id_delete_sql != "") ? ", " : "").$row['word_id'];
    364.         }
    365.       }
    366.  
    367.       if ($word_id_delete_sql != "") {
    368.         $sql = "DELETE FROM ".WORDLIST_TABLE."
    369.                WHERE word_id IN ($word_id_delete_sql)";
    370.         $site_db->query($sql);
    371.       }
    372.  
    373.       $sql = "DELETE FROM ".WORDMATCH_TABLE."
    374.              WHERE image_id = $image_id";
    375.       $site_db->query($sql);
    376.     }
    377.   }
    378.  
    379.   return true;
    380. }
    381.  
    382. function get_stopwords() {
    383.   global $config, $stopwords;
    384.   if (empty($stopwords)) {
    385.     $stopword_list = @file(ROOT_PATH."lang/".$config['language_dir']."/search_stopterms.txt");
    386.     $stopwords = array();
    387.     if (!empty($stopword_list)) {
    388.       foreach ($stopword_list as $word) {
    389.         $stopwords[] = trim($word);
    390.       }
    391.     }
    392.   }
    393.   return $stopwords;
    394. }
    395. ?>
    396.  
    Вот 4 файл global.php
    PHP:
    1.  
    2. <?php
    3. /**************************************************************************
    4.  *                                                                        *
    5.  *    4images - A Web Based Image Gallery Management System               *
    6.  *    ----------------------------------------------------------------    *
    7.  *                                                                        *
    8.  *             File: global.php                                           *
    9.  *        Copyright: (C) 2002-2011 Jan Sorgalla                           *
    10.  *            Email: [email=jan@4homepages.de]jan@4homepages.de[/email]                                    *
    11.  *              Web: [url=http://www.4homepages.de]http://www.4homepages.de[/url]                             *
    12.  *    Scriptversion: 1.7.10                                               *
    13.  *                                                                        *
    14.  *    Never released without support from: Nicky ([url=http://www.nicky.net]http://www.nicky.net[/url])   *
    15.  *                                                                        *
    16.  **************************************************************************
    17.  *                                                                        *
    18.  *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
    19.  *    bedingungen (Lizenz.txt) fьr weitere Informationen.                 *
    20.  *    ---------------------------------------------------------------     *
    21.  *    This script is NOT freeware! Please read the Copyright Notice       *
    22.  *    (Licence.txt) for further information.                              *
    23.  *                                                                        *
    24.  *************************************************************************/
    25. if (!defined('ROOT_PATH')) {
    26.   die("Security violation");
    27. }
    28. $start_time = microtime();
    29.  
    30. error_reporting(E_ERROR | E_WARNING | E_PARSE);
    31. if (function_exists("set_magic_quotes_runtime"))
    32. {
    33. }
    34.  
    35. if (!function_exists("date_default_timezone_set")) {
    36.   function date_default_timezone_set($timezone) {
    37.     return true;
    38.   }
    39. }
    40.  
    41. function addslashes_array($array) {
    42.   foreach ($array as $key => $val) {
    43.     $array[$key] = (is_array($val)) ? addslashes_array($val) : addslashes($val);
    44.   }
    45.   return $array;
    46. }
    47.  
    48. if (!isset($HTTP_GET_VARS)) {
    49.   $HTTP_GET_VARS    = &$_GET;
    50.   $HTTP_POST_VARS   = &$_POST;
    51.   $HTTP_COOKIE_VARS = &$_COOKIE;
    52.   $HTTP_POST_FILES  = &$_FILES;
    53.   $HTTP_SERVER_VARS = &$_SERVER;
    54.   $HTTP_ENV_VARS    = &$_ENV;
    55. }
    56.  
    57. if (isset($HTTP_GET_VARS['GLOBALS']) || isset($HTTP_POST_VARS['GLOBALS']) || isset($HTTP_COOKIE_VARS['GLOBALS']) || isset($HTTP_POST_FILES['GLOBALS'])) {
    58.     // Try to exploit PHP bug
    59.     die("Security violation");
    60. }
    61.  
    62. if (get_magic_quotes_gpc() == 0) {
    63.   $HTTP_GET_VARS    = addslashes_array($HTTP_GET_VARS);
    64.   $HTTP_POST_VARS   = addslashes_array($HTTP_POST_VARS);
    65.   $HTTP_COOKIE_VARS = addslashes_array($HTTP_COOKIE_VARS);
    66. }
    67.  
    68. $search_match_fields = null;
    69. $search_index_types = null;
    70.  
    71. $cat_cache = array();
    72. $cat_parent_cache = array();
    73. $new_image_cache = array();
    74. $session_info = array();
    75. $user_info = array();
    76. $user_access = array();
    77. $config = array();
    78. $lang = array();
    79. $mime_type_match = array();
    80. $additional_image_fields = array();
    81. $additional_user_fields = array();
    82. $additional_urls = array();
    83. $global_info = array();
    84. $auth_cat_sql = array();
    85. unset($self_url);
    86. unset($url);
    87. unset($script_url);
    88.  
    89. $db_servertype = "mysql";
    90. $db_host = "localhost";
    91. $db_name = "";
    92. $db_user = "";
    93. $db_password = "";
    94.  
    95. $table_prefix = "4images_";
    96.  
    97. // Initialize cache configuration
    98. $cache_enable          = 0;
    99. $cache_lifetime        = 3600; // 1 hour
    100. $cache_path            = ROOT_PATH.'cache';
    101. $cache_page_index      = 1;
    102. $cache_page_categories = 1;
    103. $cache_page_top        = 1;
    104. $cache_page_rss        = 1;
    105.  
    106. // Initialize CAPTCHA configuration
    107. $captcha_enable              = 1;
    108. $captcha_enable_comments     = 1;
    109. $captcha_enable_upload       = 1;
    110. $captcha_enable_registration = 1;
    111. $captcha_enable_postcards    = 1;
    112. $captcha_ttf                 = 1;
    113. $captcha_path                = ROOT_PATH.'captcha';
    114. $captcha_chars               = "abcdefghijklmnopqrstuvwxyz123456789";
    115. $captcha_length              = 6;
    116. $captcha_wordfile            = 0;
    117. $captcha_width               = 200;
    118. $captcha_height              = 70;
    119. $captcha_text_color          = '#000000';
    120. $captcha_text_size           = 20;
    121. $captcha_text_transparency   = 50;
    122. $captcha_filter_text         = 1;
    123. $captcha_filter_bg           = 1;
    124.  
    125. // Initialize CSRF protection configuration
    126. $csrf_protection_enable      = 1;
    127. $csrf_protection_frontend    = 1;
    128. $csrf_protection_backend     = 1;
    129. $csrf_protection_expires     = 7200;
    130. $csrf_protection_name        = '__csrf';
    131. $csrf_protection_xhtml       = 1;
    132.  
    133. @include(ROOT_PATH.'config.php');
    134.  
    135. if (!$cache_enable) {
    136.   $cache_page_index      = 0;
    137.   $cache_page_categories = 0;
    138.   $cache_page_top        = 0;
    139.   $cache_page_rss        = 0;
    140. }
    141.  
    142. if (!$captcha_enable) {
    143.   $captcha_enable_comments     = 0;
    144.   $captcha_enable_upload       = 0;
    145.   $captcha_enable_registration = 0;
    146.   $captcha_enable_postcards    = 0;
    147. }
    148.  
    149. // Include default languages
    150. @include_once(ROOT_PATH.'lang/english/main.php');
    151. include_once(ROOT_PATH.'includes/constants.php');
    152. include_once(ROOT_PATH.'includes/functions.php');
    153.  
    154. function clean_string($string) {
    155.   $canCheckUTF8Error = defined('PREG_BAD_UTF8_ERROR') && function_exists('preg_last_error');
    156.  
    157.   // Remove any attribute starting with "on" or xmlns
    158.   $tmp = preg_replace('#(<[^>]+[\x00-\x20\"\'])(on|xmlns)[^>]*>#iUu',"$1>",$string);
    159.   if ($canCheckUTF8Error && (PREG_BAD_UTF8_ERROR == preg_last_error())) {
    160.       $tmp = preg_replace('#(<[^>]+[\x00-\x20\"\'])(on|xmlns)[^>]*>#iU',"$1>",$string);
    161.   }
    162.   $string = $tmp;
    163.  
    164.   // Remove javascript: and vbscript: protocol
    165.   $tmp = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu','$1=$2nojavascript...',$string);
    166.   if ($canCheckUTF8Error && (PREG_BAD_UTF8_ERROR == preg_last_error())) {
    167.       $tmp = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU','$1=$2nojavascript...',$string);
    168.   }
    169.   $string = $tmp;
    170.   $tmp = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu','$1=$2novbscript...',$string);
    171.   if ($canCheckUTF8Error && (PREG_BAD_UTF8_ERROR == preg_last_error())) {
    172.       $tmp = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU','$1=$2novbscript...',$string);
    173.   }
    174.   $string = $tmp;
    175.  
    176.   // <span style="width: expression(alert('Ping!'));"></span>
    177.   // only works in ie...
    178.   $string = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU',"$1>",$string);
    179.   $string = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU',"$1>",$string);
    180.   $tmp = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*>#iUu',"$1>",$string);
    181.   if ($canCheckUTF8Error && (PREG_BAD_UTF8_ERROR == preg_last_error())) {
    182.       $tmp = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*>#iU',"$1>",$string);
    183.   }
    184.   $string = $tmp;
    185.  
    186.   // Remove namespaced elements (we do not need them...)
    187.   $string = preg_replace('#</*\w+:\w[^>]*>#i',"",$string);
    188.  
    189.   // Remove all control (i.e. with ASCII value lower than 0x20 (space),
    190.   // except of 0x0A (line feed) and 0x09 (tabulator)
    191.   $search =
    192.     "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
    193.   $replace = //str_repeat("\r", strlen($search2));
    194.     "\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D\x0D";
    195.  
    196.   $string = str_replace("\r\n", "\n", $string);
    197.   $string = str_replace("\r",   "\n", $string);
    198.   $string = strtr($string, $search, $replace);
    199.   $string = str_replace("\r", '', $string);  // \r === \x0D
    200.  
    201.   // Remove really unwanted tags
    202.   do {
    203.     $oldstring = $string;
    204.     $string = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i',"",$string);
    205.   } while ($oldstring != $string);
    206.  
    207.   return $string;
    208. }
    209.  
    210. function clean_array($array) {
    211.   foreach ($array as $key => $val) {
    212.     $key = clean_string($key);
    213.  
    214.     if (is_array($val)) {
    215.       $val = clean_array($val);
    216.     } else {
    217.       $val = clean_string($val);
    218.     }
    219.  
    220.     $array[$key] = $val;
    221.   }
    222.  
    223.   return $array;
    224. }
    225.  
    226. if (!defined('IN_CP')) {
    227.   $HTTP_GET_VARS    = clean_array($HTTP_GET_VARS);
    228.   $HTTP_POST_VARS   = clean_array($HTTP_POST_VARS);
    229.   $HTTP_COOKIE_VARS = clean_array($HTTP_COOKIE_VARS);
    230.   $HTTP_POST_FILES  = clean_array($HTTP_POST_FILES);
    231. }
    232.  
    233. //-----------------------------------------------------
    234. //--- Useful Stuff ------------------------------------
    235. //-----------------------------------------------------
    236. if (isset($HTTP_GET_VARS['action']) || isset($HTTP_POST_VARS['action'])) {
    237.   $action = (isset($HTTP_POST_VARS['action'])) ? stripslashes(trim((string)$HTTP_POST_VARS['action'])) : stripslashes(trim((string)$HTTP_GET_VARS['action']));
    238.   $action = preg_replace("/[^a-z0-9_-]+/i", "", $action);
    239. }
    240. else {
    241.   $action = "";
    242. }
    243.  
    244. if (isset($HTTP_GET_VARS['mode']) || isset($HTTP_POST_VARS['mode'])) {
    245.   $mode = (isset($HTTP_POST_VARS['mode'])) ? stripslashes(trim((string)$HTTP_POST_VARS['mode'])) : stripslashes(trim((string)$HTTP_GET_VARS['mode']));
    246.   $mode = preg_replace("/[^a-z0-9_-]+/i", "", $mode);
    247. }
    248. else {
    249.   $mode = "";
    250. }
    251.  
    252. if (isset($HTTP_GET_VARS[URL_CAT_ID]) || isset($HTTP_POST_VARS[URL_CAT_ID])) {
    253.   $cat_id = (isset($HTTP_POST_VARS[URL_CAT_ID])) ? intval($HTTP_POST_VARS[URL_CAT_ID]) : intval($HTTP_GET_VARS[URL_CAT_ID]);
    254. }
    255. else {
    256.   $cat_id = 0;
    257. }
    258.  
    259. if (isset($HTTP_GET_VARS[URL_IMAGE_ID]) || isset($HTTP_POST_VARS[URL_IMAGE_ID])) {
    260.   $image_id = (isset($HTTP_POST_VARS[URL_IMAGE_ID])) ? intval($HTTP_POST_VARS[URL_IMAGE_ID]) : intval($HTTP_GET_VARS[URL_IMAGE_ID]);
    261. }
    262. else {
    263.   $image_id = 0;
    264. }
    265.  
    266. if (isset($HTTP_GET_VARS[URL_ID]) || isset($HTTP_POST_VARS[URL_ID])) {
    267.   $id = (isset($HTTP_POST_VARS[URL_ID])) ? intval($HTTP_POST_VARS[URL_ID]) : intval($HTTP_GET_VARS[URL_ID]);
    268. }
    269. else {
    270.   $id = 0;
    271. }
    272.  
    273. if (isset($HTTP_GET_VARS[URL_PAGE]) || isset($HTTP_POST_VARS[URL_PAGE])) {
    274.   $page = (isset($HTTP_POST_VARS[URL_PAGE])) ? intval($HTTP_POST_VARS[URL_PAGE]) : intval($HTTP_GET_VARS[URL_PAGE]);
    275.     if (!$page) {
    276.     $page = 1;
    277.   }
    278. }
    279. else {
    280.   $page = 1;
    281. }
    282.  
    283. if (isset($HTTP_POST_VARS['show_result']) || isset($HTTP_GET_VARS['show_result'])) {
    284.   $show_result = 1;
    285. }
    286. else {
    287.   $show_result = 0;
    288. }
    289.  
    290. if (isset($HTTP_POST_VARS['search_keywords']) || isset($HTTP_GET_VARS['search_keywords'])) {
    291.   $search_keywords = (isset($HTTP_POST_VARS['search_keywords'])) ? trim((string)$HTTP_POST_VARS['search_keywords']) : trim((string)$HTTP_GET_VARS['search_keywords']);
    292.   if ($search_keywords != "") {
    293.     $show_result = 1;
    294.   }
    295. }
    296. else {
    297.   $search_keywords = "";
    298. }
    299.  
    300. if (isset($HTTP_POST_VARS['search_user']) || isset($HTTP_GET_VARS['search_user'])) {
    301.   $search_user = (isset($HTTP_POST_VARS['search_user'])) ? trim((string)$HTTP_POST_VARS['search_user']) : trim((string)$HTTP_GET_VARS['search_user']);
    302.   if ($search_user != "") {
    303.     $show_result = 1;
    304.   }
    305. }
    306. else {
    307.   $search_user = "";
    308. }
    309.  
    310. if (isset($HTTP_POST_VARS['search_new_images']) || isset($HTTP_GET_VARS['search_new_images'])) {
    311.   $search_new_images = 1;
    312.   $show_result = 1;
    313. }
    314. else {
    315.   $search_new_images = 0;
    316. }
    317.  
    318. if (empty($PHP_SELF)) {
    319.   if (!empty($HTTP_SERVER_VARS['PHP_SELF'])) {
    320.     $PHP_SELF = $HTTP_SERVER_VARS["PHP_SELF"];
    321.   }
    322.   elseif (!empty($HTTP_ENV_VARS['PHP_SELF'])) {
    323.     $PHP_SELF = $HTTP_ENV_VARS["PHP_SELF"];
    324.   }
    325.     elseif (!empty($HTTP_SERVER_VARS['PATH_INFO'])) {
    326.     $PHP_SELF = $HTTP_SERVER_VARS['PATH_INFO'];
    327.   }
    328.   else {
    329.     $PHP_SELF = getenv("SCRIPT_NAME");
    330.   }
    331. }
    332.  
    333. $self_url = basename($PHP_SELF);
    334. if (empty($self_url) || !preg_match("/\.php$/", $self_url)) {
    335.   $self_url = "index.php";
    336. }
    337.  
    338. //if (getenv("QUERY_STRING")) {
    339. //  $self_url .= "?".getenv("QUERY_STRING");
    340. //  $self_url = preg_replace(array("/([?|&])action=[^?|&]*/", "/([?|&])mode=[^?|&]*/", "/([?|&])phpinfo=[^?|&]*/", "/([?|&])printstats=[^?|&]*/", "/[?|&]".URL_ID."=[^?|&]*/", "/[?|&]l=[^?|&]*/", "/[&?]+$/"), array("", "", "", "", "", "", ""), $self_url);
    341. //}
    342. //else {
    343.   if (preg_match("/details.php/", $self_url) && !preg_match("/[?|&]".URL_IMAGE_ID."=[^?|&]*/", $self_url) && $image_id) {
    344.     $self_url .= "?".URL_IMAGE_ID."=".$image_id;
    345.   }
    346.   elseif (preg_match("/categories.php/", $self_url) && !preg_match("/[?|&]".URL_CAT_ID."=[^?|&]*/", $self_url)) {
    347.     $self_url .= "?".URL_CAT_ID."=".$cat_id;
    348.   }
    349.   if (isset($show_result) && $show_result) {
    350.     $self_url .= preg_match("/\?/", $self_url) ? "&amp;" : "?";
    351.     $self_url .= "show_result=1";
    352.   }
    353.   if ($page && $page != 1) {
    354.     $self_url .= preg_match("/\?/", $self_url) ? "&amp;" : "?";
    355.     $self_url .= URL_PAGE."=".$page;
    356.   }
    357. //}
    358.  
    359. if (isset($HTTP_GET_VARS['url']) || isset($HTTP_POST_VARS['url'])) {
    360.   $url = (isset($HTTP_GET_VARS['url'])) ? trim($HTTP_GET_VARS['url']) : trim($HTTP_POST_VARS['url']);
    361. }
    362. else {
    363.   $url = "";
    364. }
    365. if (empty($url)) {
    366.   $url = get_basename(getenv("HTTP_REFERER"));
    367. }
    368. else {
    369.   if ($url == getenv("HTTP_REFERER")) {
    370.     $url = "index.php";
    371.   }
    372. }
    373. $url = preg_replace(array("/[?|&]action=[^?|&]*/", "/[?|&]mode=[^?|&]*/", "/[?|&]".URL_ID."=[^?|&]*/", "/[?|&]l=[^?|&]*/", "/[&?]+$/"), array("", "", "", "", ""), $url);
    374. if ($url == $self_url || $url == "" || !preg_match("/\.php/", $url)) {
    375.   $url = "index.php";
    376. }
    377.  
    378. if (defined("SCRIPT_URL") && SCRIPT_URL != "") {
    379.   $script_url = SCRIPT_URL;
    380. }
    381. else {
    382.   $port = (!preg_match("/^(80|443)$/", getenv("SERVER_PORT"), $port_match)) ? ":".getenv("SERVER_PORT") : "";
    383.   $script_url  = (isset($port_match[1]) && $port_match[1] == 443) ? "https://" : "http://";
    384.   $script_url .= (!empty($HTTP_SERVER_VARS['HTTP_HOST'])) ? $HTTP_SERVER_VARS['HTTP_HOST'] : getenv("SERVER_NAME");
    385.   if ($port) $script_url = str_replace(":".$port, "", $script_url);
    386.   $script_url .= $port;
    387.  
    388.   $dirname = str_replace("\\", "/", dirname($PHP_SELF));
    389.   $script_url .= ($dirname != "/") ? $dirname : "";
    390. }
    391.  
    392. // Check if we should redirect to the installation routine
    393. if (!defined("4IMAGES_ACTIVE")) {
    394.   redirect("install.php");
    395. }
    396.  
    397. //-----------------------------------------------------
    398. //--- Start DB ----------------------------------------
    399. //-----------------------------------------------------
    400. include_once(ROOT_PATH.'includes/db_'.strtolower($db_servertype).'.php');
    401. $site_db = new Db($db_host, $db_user, $db_password, $db_name);
    402.  
    403. //-----------------------------------------------------
    404. //--- Generate Setting --------------------------------
    405. //-----------------------------------------------------
    406. $sql = "SELECT setting_name, setting_value
    407.        FROM ".SETTINGS_TABLE;
    408. $result = $site_db->query($sql);
    409. if (!$result) {
    410.   echo $lang['no_settings'];
    411.   exit;
    412. }
    413. while ($row = $site_db->fetch_array($result)) {
    414.   $config[$row['setting_name']] = $row['setting_value'];
    415. }
    416. $site_db->free_result();
    417.  
    418. $config['allowed_mediatypes'] = str_replace(" ", "", $config['allowed_mediatypes']);
    419. $config['allowed_mediatypes_array'] = explode(",", $config['allowed_mediatypes']);
    420. $config['allowed_mediatypes_match'] = str_replace(",", "|", $config['allowed_mediatypes']);
    421.  
    422. $msg = "";
    423. $clickstream = "";
    424. define('MEDIA_PATH', ROOT_PATH.MEDIA_DIR);
    425. define('THUMB_PATH', ROOT_PATH.THUMB_DIR);
    426. define('MEDIA_TEMP_PATH', ROOT_PATH.MEDIA_TEMP_DIR);
    427. define('THUMB_TEMP_PATH', ROOT_PATH.THUMB_TEMP_DIR);
    428. define('TEMPLATE_PATH', ROOT_PATH.TEMPLATE_DIR."/".$config['template_dir']);
    429. define('ICON_PATH', ROOT_PATH.TEMPLATE_DIR."/".$config['template_dir']."/icons");
    430.  
    431. //-----------------------------------------------------
    432. //--- Templates ---------------------------------------
    433. //-----------------------------------------------------
    434. include_once(ROOT_PATH.'includes/template.php');
    435. $site_template = new Template(TEMPLATE_PATH);
    436.  
    437. $config['language_dir_default'] = $config['language_dir'];
    438. $l = null;
    439. if (isset($HTTP_GET_VARS['l']) || isset($HTTP_POST_VARS['l'])) {
    440.   $requested_l = (isset($HTTP_GET_VARS['l'])) ? trim($HTTP_GET_VARS['l']) : trim($HTTP_POST_VARS['l']);
    441.   if (!preg_match('#\.\.[\\\/]#', $requested_l) && $requested_l != $config['language_dir'] && file_exists(ROOT_PATH.'lang/'.$requested_l.'/main.php')) {
    442.     $l = $requested_l;
    443.     $config['language_dir'] = $l;
    444.   }
    445. }
    446.  
    447. include_once(ROOT_PATH.'lang/'.$config['language_dir'].'/main.php');
    448. include_once(ROOT_PATH."includes/db_field_definitions.php");
    449. include_once(ROOT_PATH.'includes/auth.php');
    450.  
    451. //-----------------------------------------------------
    452. //--- Security ----------------------------------------
    453. //-----------------------------------------------------
    454. include_once(ROOT_PATH.'includes/security_utils.php');
    455.  
    456. //-----------------------------------------------------
    457. //--- Cache -------------------------------------------
    458. //-----------------------------------------------------
    459. include_once(ROOT_PATH.'includes/cache_utils.php');
    460.  
    461. //-----------------------------------------------------
    462. //--- CAPTCHA -----------------------------------------
    463. //-----------------------------------------------------
    464. include_once(ROOT_PATH.'includes/captcha_utils.php');
    465.  
    466. //-----------------------------------------------------
    467. //--- CSRF protection ---------------------------------
    468. //-----------------------------------------------------
    469. include_once(ROOT_PATH.'includes/csrf_utils.php');
    470.  
    471. //-----------------------------------------------------
    472. //--- GZip Compression --------------------------------
    473. //-----------------------------------------------------
    474. $do_gzip_compress = 0;
    475. if ($config['gz_compress'] == 1 && !isset($nozip)) {
    476.   if (get_php_version() >= 40004) {
    477.     if (extension_loaded("zlib")) {
    478.       ob_start("ob_gzhandler");
    479.     }
    480.   }
    481.   elseif (get_php_version() > 40000) {
    482.     if (preg_match("/gzip/i", $HTTP_SERVER_VARS["HTTP_ACCEPT_ENCODING"]) || preg_match("/x-gzip/i", $HTTP_SERVER_VARS["HTTP_ACCEPT_ENCODING"])) {
    483.       if (extension_loaded("zlib")) {
    484.         $do_gzip_compress = 1;
    485.         ob_start();
    486.         ob_implicit_flush(0);
    487.       }
    488.     }
    489.   }
    490. }
    491.  
    492. if (defined("GET_CACHES")) {
    493.   $config['cat_order'] = empty($config['cat_order']) ? 'cat_order, cat_name' : $config['cat_order'];
    494.   $config['cat_sort']  = empty($config['cat_sort']) ? 'ASC' : $config['cat_sort'];
    495.   $sql = "SELECT cat_id, cat_name, cat_description, cat_parent_id, cat_hits, cat_order, auth_viewcat, auth_viewimage, auth_download, auth_upload, auth_directupload, auth_vote, auth_sendpostcard, auth_readcomment, auth_postcomment
    496.          FROM ".CATEGORIES_TABLE."
    497.          ORDER BY ".$config['cat_order']." " .$config['cat_sort'];
    498.   $result = $site_db->query($sql);
    499.  
    500.   while ($row = $site_db->fetch_array($result)) {
    501.     $cat_cache[$row['cat_id']] = $row;
    502.     $cat_parent_cache[$row['cat_parent_id']][] = $row['cat_id'];
    503.   }
    504.   $site_db->free_result();
    505.  
    506.   // --------------------------------------
    507.  
    508.   $new_cutoff = time() - (60 * 60 * 24 * $config['new_cutoff']);
    509.  
    510.   $sql = "SELECT cat_id, COUNT(image_id) AS new_images
    511.          FROM ".IMAGES_TABLE."
    512.          WHERE image_active = 1 AND image_date >= $new_cutoff
    513.          GROUP BY cat_id";
    514.   $result = $site_db->query($sql);
    515.  
    516.   while ($row = $site_db->fetch_array($result)) {
    517.     $new_image_cache[$row['cat_id']] = $row['new_images'];
    518.   }
    519.   $site_db->free_result();
    520.  
    521.   // --------------------------------------
    522.  
    523.   $sql = "SELECT cat_id, COUNT(*) AS num_images
    524.          FROM ".IMAGES_TABLE."
    525.          WHERE image_active = 1
    526.          GROUP BY cat_id";
    527.   $result = $site_db->query($sql);
    528.  
    529.   while ($row = $site_db->fetch_array($result)) {
    530.     $cat_cache[$row['cat_id']]['num_images'] = $row['num_images'];
    531.   }
    532.   $site_db->free_result();
    533. } //end if GET_CACHES
    534.  
    535. ?>
    536.  
    Вот 5 файл sessions.php
    PHP:
    1.  
    2. <?php
    3. /**************************************************************************
    4.  *                                                                        *
    5.  *    4images - A Web Based Image Gallery Management System               *
    6.  *    ----------------------------------------------------------------    *
    7.  *                                                                        *
    8.  *             File: sessions.php                                         *
    9.  *        Copyright: (C) 2002-2011 Jan Sorgalla                           *
    10.  *            Email: [email=jan@4homepages.de]jan@4homepages.de[/email]                                    *
    11.  *              Web: [url=http://www.4homepages.de]http://www.4homepages.de[/url]                             *
    12.  *    Scriptversion: 1.7.10                                               *
    13.  *                                                                        *
    14.  *    Never released without support from: Nicky ([url=http://www.nicky.net]http://www.nicky.net[/url])   *
    15.  *                                                                        *
    16.  **************************************************************************
    17.  *                                                                        *
    18.  *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
    19.  *    bedingungen (Lizenz.txt) fьr weitere Informationen.                 *
    20.  *    ---------------------------------------------------------------     *
    21.  *    This script is NOT freeware! Please read the Copyright Notice       *
    22.  *    (Licence.txt) for further information.                              *
    23.  *                                                                        *
    24.  *************************************************************************/
    25. if (!defined('ROOT_PATH')) {
    26.   die("Security violation");
    27. }
    28.  
    29. //-----------------------------------------------------
    30. //--- Start Configuration -----------------------------
    31. //-----------------------------------------------------
    32.  
    33. define('SESSION_NAME', 'sessionid');
    34.  
    35. $user_table_fields = array(
    36.   "user_id" => "user_id",
    37.   "user_level" => "user_level",
    38.   "user_name" => "user_name",
    39.   "user_password" => "user_password",
    40.   "user_email" => "user_email",
    41.   "user_showemail" => "user_showemail",
    42.   "user_allowemails" => "user_allowemails",
    43.   "user_invisible" => "user_invisible",
    44.   "user_joindate" => "user_joindate",
    45.   "user_activationkey" => "user_activationkey",
    46.   "user_lastaction" => "user_lastaction",
    47.   "user_location" => "user_location",
    48.   "user_lastvisit" => "user_lastvisit",
    49.   "user_comments" => "user_comments",
    50.   "user_homepage" => "user_homepage",
    51.   "user_icq" => "user_icq"
    52. );
    53.  
    54. //-----------------------------------------------------
    55. //--- End Configuration -------------------------------
    56. //-----------------------------------------------------
    57.  
    58. function get_user_table_field($add, $user_field) {
    59.   global $user_table_fields;
    60.   return (!empty($user_table_fields[$user_field])) ? $add.$user_table_fields[$user_field] : "";
    61. }
    62.  
    63. class Session {
    64.  
    65.   var $session_id;
    66.   var $session_key;
    67.   var $user_ip;
    68.   var $user_location;
    69.   var $current_time;
    70.   var $session_timeout;
    71.   var $mode = "get";
    72.   var $session_info = array();
    73.   var $user_info = array();
    74.  
    75.   function Session() {
    76.     global $config;
    77.     $this->session_timeout = $config['session_timeout'] * 60;
    78.     $this->user_ip = $this->get_user_ip();
    79.     $this->user_location = $this->get_user_location();
    80.     $this->current_time = time();
    81.  
    82.     if (defined('SESSION_KEY') && SESSION_KEY != '') {
    83.         $this->session_key = SESSION_KEY;
    84.     } else {
    85.         $this->session_key = md5('4images' . realpath(ROOT_PATH));
    86.     }
    87.  
    88.     // Stop adding SID to URLs
    89.     @ini_set('session.use_trans_sid', 0);
    90.  
    91.     //@ini_set('session.cookie_lifetime', $this->session_timeout);
    92.  
    93.     @session_start();
    94.  
    95.     $this->demand_session();
    96.   }
    97.  
    98.   function set_cookie_data($name, $value, $permanent = 1) {
    99.     $cookie_expire = ($permanent) ? $this->current_time + 60 * 60 * 24 * 365 : 0;
    100.     $cookie_name = COOKIE_NAME.$name;
    101.     setcookie($cookie_name, $value, $cookie_expire, COOKIE_PATH, COOKIE_DOMAIN, COOKIE_SECURE);
    102.     $HTTP_COOKIE_VARS[$cookie_name] = $value;
    103.   }
    104.  
    105.   function read_cookie_data($name) {
    106.     global $HTTP_COOKIE_VARS;
    107.     $cookie_name = COOKIE_NAME.$name;
    108.     return (isset($HTTP_COOKIE_VARS[$cookie_name])) ? $HTTP_COOKIE_VARS[$cookie_name] : false;
    109.   }
    110.  
    111.   function get_session_id() {
    112.     if (SID == '') {
    113.       $this->mode = "cookie";
    114.     }
    115.  
    116.     if (preg_match('/[^a-z0-9]+/i', session_id())) {
    117.     }
    118.  
    119.     $this->session_id = session_id();
    120.   }
    121.  
    122.   function demand_session() {
    123.     $this->get_session_id();
    124.     if (!$this->load_session_info()) {
    125.       $this->delete_old_sessions();
    126.       $user_id = ($this->read_cookie_data("userid")) ? intval($this->read_cookie_data("userid")) : GUEST;
    127.       $this->start_session($user_id);
    128.     }
    129.     else {
    130.       $this->user_info = $this->load_user_info($this->session_info['session_user_id']);
    131.       $update_cutoff = ($this->user_info['user_id'] != GUEST) ? $this->current_time - $this->user_info['user_lastaction'] : $this->current_time - $this->session_info['session_lastaction'];
    132.       if ($update_cutoff > 60) {
    133.         $this->update_session();
    134.         $this->delete_old_sessions();
    135.       }
    136.     }
    137.   }
    138.  
    139.   function start_session($user_id = GUEST, $login_process = 0) {
    140.     global $site_db;
    141.  
    142.     $this->user_info = $this->load_user_info($user_id);
    143.     if ($this->user_info['user_id'] != GUEST && !$login_process) {
    144.       if (secure_compare($this->read_cookie_data("userpass"), md5($this->user_info['user_password'])) && $this->user_info['user_level'] > USER_AWAITING) {
    145.         $this->set_cookie_data("userpass", $this->user_info['user_password']);
    146.       }
    147.       else {
    148.         $this->set_cookie_data("userpass", "", 0);
    149.         $this->user_info = $this->load_user_info(GUEST);
    150.       }
    151.     }
    152.  
    153.     //if (!$login_process) {
    154.       $sql = "REPLACE INTO ".SESSIONS_TABLE."
    155.              (session_id, session_user_id, session_lastaction, session_location, session_ip)
    156.              VALUES
    157.              ('".addslashes($this->session_id)."', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
    158.       $site_db->query($sql);
    159.     //}
    160.  
    161.     $this->session_info['session_user_id'] = $this->user_info['user_id'];
    162.     $this->session_info['session_lastaction'] = $this->current_time;
    163.     $this->session_info['session_location'] = $this->user_location;
    164.     $this->session_info['session_ip'] = $this->user_ip;
    165.  
    166.     if ($this->user_info['user_id'] != GUEST) {
    167.       $this->user_info['user_lastvisit'] = (!empty($this->user_info['user_lastaction'])) ? $this->user_info['user_lastaction'] : $this->current_time;
    168.       $sql = "UPDATE ".USERS_TABLE."
    169.              SET ".get_user_table_field("", "user_lastaction")." = $this->current_time, ".get_user_table_field("", "user_location")." = '$this->user_location', ".get_user_table_field("", "user_lastvisit")." = ".$this->user_info['user_lastvisit']."
    170.              WHERE ".get_user_table_field("", "user_id")." = ".$this->user_info['user_id'];
    171.       $site_db->query($sql);
    172.     }
    173.     $this->set_cookie_data("lastvisit", $this->user_info['user_lastvisit']);
    174.     $this->set_cookie_data("userid", $this->user_info['user_id']);
    175.     return true;
    176.   }
    177.  
    178.   function login($user_name = "", $user_password = "", $auto_login = 0, $set_auto_login = 1) {
    179.     global $site_db, $user_table_fields;
    180.  
    181.     if (empty($user_name) || empty($user_password)) {
    182.       return false;
    183.     }
    184.     $sql = "SELECT ".get_user_table_field("", "user_id").get_user_table_field(", ", "user_password")."
    185.            FROM ".USERS_TABLE."
    186.            WHERE ".get_user_table_field("", "user_name")." = '$user_name' AND ".get_user_table_field("", "user_level")." <> ".USER_AWAITING;
    187.     $row = $site_db->query_firstrow($sql);
    188.  
    189.     $user_id = (isset($row[$user_table_fields['user_id']])) ? $row[$user_table_fields['user_id']] : GUEST;
    190.     if ($user_id != GUEST) {
    191.       if (compare_passwords($user_password, $row[$user_table_fields['user_password']])) {
    192.         $sql = "UPDATE ".SESSIONS_TABLE."
    193.                SET session_user_id = $user_id
    194.                WHERE session_id = '".addslashes($this->session_id)."'";
    195.         $site_db->query($sql);
    196.         if ($set_auto_login) {
    197.           $this->set_cookie_data("userpass", ($auto_login) ? md5($row[$user_table_fields['user_password']]) : "");
    198.         }
    199.         $this->start_session($user_id, 1);
    200.         return true;
    201.       }
    202.     }
    203.     return false;
    204.   }
    205.  
    206.   function logout($user_id) {
    207.     global $site_db;
    208.     $sql = "DELETE FROM ".SESSIONS_TABLE."
    209.            WHERE session_id = '".addslashes($this->session_id)."' OR session_user_id = $user_id";
    210.     $site_db->query($sql);
    211.     $this->set_cookie_data("userpass", "", 0);
    212.     $this->set_cookie_data("userid", GUEST);
    213.  
    214.     $this->session_info = array();
    215.  
    216.     return true;
    217.   }
    218.  
    219.   function delete_old_sessions() {
    220.     global $site_db;
    221.     $expiry_time = $this->current_time - $this->session_timeout;
    222.     $sql = "DELETE FROM ".SESSIONS_TABLE."
    223.            WHERE session_lastaction < $expiry_time";
    224.     $site_db->query($sql);
    225.  
    226.     return true;
    227.   }
    228.  
    229.   function update_session() {
    230.     global $site_db;
    231.  
    232.     $sql = "REPLACE INTO ".SESSIONS_TABLE."
    233.           (session_id, session_user_id, session_lastaction, session_location, session_ip)
    234.           VALUES
    235.           ('".addslashes($this->session_id)."', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
    236.     $site_db->query($sql);
    237.  
    238.     $this->session_info['session_lastaction'] = $this->current_time;
    239.     $this->session_info['session_location'] = $this->user_location;
    240.     $this->session_info['session_ip'] = $this->user_ip;
    241.  
    242.     if ($this->user_info['user_id'] != GUEST) {
    243.       $sql = "UPDATE ".USERS_TABLE."
    244.              SET ".get_user_table_field("", "user_lastaction")." = $this->current_time, ".get_user_table_field("", "user_location")." = '$this->user_location'
    245.              WHERE ".get_user_table_field("", "user_id")." = ".$this->user_info['user_id'];
    246.       $site_db->query($sql);
    247.     }
    248.     return;
    249.   }
    250.  
    251.   function return_session_info() {
    252.     return $this->session_info;
    253.   }
    254.  
    255.   function return_user_info() {
    256.     return $this->user_info;
    257.   }
    258.  
    259.   function freeze() {
    260.     return;
    261.   }
    262.  
    263.   function load_session_info() {
    264.     $register_globals = strtolower(@ini_get('register_globals'));
    265.     if ($register_globals && $register_globals != "off" && $register_globals != "false") {
    266.       session_register($this->session_key);
    267.  
    268.       if (!isset($GLOBALS[$this->session_key])) {
    269.         $GLOBALS[$this->session_key] = array();
    270.       }
    271.  
    272.       $this->session_info = &$GLOBALS[$this->session_key];
    273.  
    274.     } else {
    275.       if (isset($_SESSION)) {
    276.         if (!isset($_SESSION[$this->session_key])) {
    277.           $_SESSION[$this->session_key] = a
     
  2. vaneeeek

    vaneeeek Активный пользователь

    С нами с:
    13 июн 2011
    Сообщения:
    68
    Симпатии:
    0
    Адрес:
    москва
    Вот 5 файл полностью

    PHP:
    1.  
    2. <?php
    3. /**************************************************************************
    4.  *                                                                        *
    5.  *    4images - A Web Based Image Gallery Management System               *
    6.  *    ----------------------------------------------------------------    *
    7.  *                                                                        *
    8.  *             File: sessions.php                                         *
    9.  *        Copyright: (C) 2002-2011 Jan Sorgalla                           *
    10.  *            Email: [email=jan@4homepages.de]jan@4homepages.de[/email]                                    *
    11.  *              Web: [url=http://www.4homepages.de]http://www.4homepages.de[/url]                             *
    12.  *    Scriptversion: 1.7.10                                               *
    13.  *                                                                        *
    14.  *    Never released without support from: Nicky ([url=http://www.nicky.net]http://www.nicky.net[/url])   *
    15.  *                                                                        *
    16.  **************************************************************************
    17.  *                                                                        *
    18.  *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
    19.  *    bedingungen (Lizenz.txt) fьr weitere Informationen.                 *
    20.  *    ---------------------------------------------------------------     *
    21.  *    This script is NOT freeware! Please read the Copyright Notice       *
    22.  *    (Licence.txt) for further information.                              *
    23.  *                                                                        *
    24.  *************************************************************************/
    25. if (!defined('ROOT_PATH')) {
    26.   die("Security violation");
    27. }
    28.  
    29. //-----------------------------------------------------
    30. //--- Start Configuration -----------------------------
    31. //-----------------------------------------------------
    32.  
    33. define('SESSION_NAME', 'sessionid');
    34.  
    35. $user_table_fields = array(
    36.   "user_id" => "user_id",
    37.   "user_level" => "user_level",
    38.   "user_name" => "user_name",
    39.   "user_password" => "user_password",
    40.   "user_email" => "user_email",
    41.   "user_showemail" => "user_showemail",
    42.   "user_allowemails" => "user_allowemails",
    43.   "user_invisible" => "user_invisible",
    44.   "user_joindate" => "user_joindate",
    45.   "user_activationkey" => "user_activationkey",
    46.   "user_lastaction" => "user_lastaction",
    47.   "user_location" => "user_location",
    48.   "user_lastvisit" => "user_lastvisit",
    49.   "user_comments" => "user_comments",
    50.   "user_homepage" => "user_homepage",
    51.   "user_icq" => "user_icq"
    52. );
    53.  
    54. //-----------------------------------------------------
    55. //--- End Configuration -------------------------------
    56. //-----------------------------------------------------
    57.  
    58. function get_user_table_field($add, $user_field) {
    59.   global $user_table_fields;
    60.   return (!empty($user_table_fields[$user_field])) ? $add.$user_table_fields[$user_field] : "";
    61. }
    62.  
    63. class Session {
    64.  
    65.   var $session_id;
    66.   var $session_key;
    67.   var $user_ip;
    68.   var $user_location;
    69.   var $current_time;
    70.   var $session_timeout;
    71.   var $mode = "get";
    72.   var $session_info = array();
    73.   var $user_info = array();
    74.  
    75.   function Session() {
    76.     global $config;
    77.     $this->session_timeout = $config['session_timeout'] * 60;
    78.     $this->user_ip = $this->get_user_ip();
    79.     $this->user_location = $this->get_user_location();
    80.     $this->current_time = time();
    81.  
    82.     if (defined('SESSION_KEY') && SESSION_KEY != '') {
    83.         $this->session_key = SESSION_KEY;
    84.     } else {
    85.         $this->session_key = md5('4images' . realpath(ROOT_PATH));
    86.     }
    87.  
    88.     // Stop adding SID to URLs
    89.     @ini_set('session.use_trans_sid', 0);
    90.  
    91.     //@ini_set('session.cookie_lifetime', $this->session_timeout);
    92.  
    93.     @session_start();
    94.  
    95.     $this->demand_session();
    96.   }
    97.  
    98.   function set_cookie_data($name, $value, $permanent = 1) {
    99.     $cookie_expire = ($permanent) ? $this->current_time + 60 * 60 * 24 * 365 : 0;
    100.     $cookie_name = COOKIE_NAME.$name;
    101.     setcookie($cookie_name, $value, $cookie_expire, COOKIE_PATH, COOKIE_DOMAIN, COOKIE_SECURE);
    102.     $HTTP_COOKIE_VARS[$cookie_name] = $value;
    103.   }
    104.  
    105.   function read_cookie_data($name) {
    106.     global $HTTP_COOKIE_VARS;
    107.     $cookie_name = COOKIE_NAME.$name;
    108.     return (isset($HTTP_COOKIE_VARS[$cookie_name])) ? $HTTP_COOKIE_VARS[$cookie_name] : false;
    109.   }
    110.  
    111.   function get_session_id() {
    112.     if (SID == '') {
    113.       $this->mode = "cookie";
    114.     }
    115.  
    116.     if (preg_match('/[^a-z0-9]+/i', session_id())) {
    117.     }
    118.  
    119.     $this->session_id = session_id();
    120.   }
    121.  
    122.   function demand_session() {
    123.     $this->get_session_id();
    124.     if (!$this->load_session_info()) {
    125.       $this->delete_old_sessions();
    126.       $user_id = ($this->read_cookie_data("userid")) ? intval($this->read_cookie_data("userid")) : GUEST;
    127.       $this->start_session($user_id);
    128.     }
    129.     else {
    130.       $this->user_info = $this->load_user_info($this->session_info['session_user_id']);
    131.       $update_cutoff = ($this->user_info['user_id'] != GUEST) ? $this->current_time - $this->user_info['user_lastaction'] : $this->current_time - $this->session_info['session_lastaction'];
    132.       if ($update_cutoff > 60) {
    133.         $this->update_session();
    134.         $this->delete_old_sessions();
    135.       }
    136.     }
    137.   }
    138.  
    139.   function start_session($user_id = GUEST, $login_process = 0) {
    140.     global $site_db;
    141.  
    142.     $this->user_info = $this->load_user_info($user_id);
    143.     if ($this->user_info['user_id'] != GUEST && !$login_process) {
    144.       if (secure_compare($this->read_cookie_data("userpass"), md5($this->user_info['user_password'])) && $this->user_info['user_level'] > USER_AWAITING) {
    145.         $this->set_cookie_data("userpass", $this->user_info['user_password']);
    146.       }
    147.       else {
    148.         $this->set_cookie_data("userpass", "", 0);
    149.         $this->user_info = $this->load_user_info(GUEST);
    150.       }
    151.     }
    152.  
    153.     //if (!$login_process) {
    154.       $sql = "REPLACE INTO ".SESSIONS_TABLE."
    155.              (session_id, session_user_id, session_lastaction, session_location, session_ip)
    156.              VALUES
    157.              ('".addslashes($this->session_id)."', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
    158.       $site_db->query($sql);
    159.     //}
    160.  
    161.     $this->session_info['session_user_id'] = $this->user_info['user_id'];
    162.     $this->session_info['session_lastaction'] = $this->current_time;
    163.     $this->session_info['session_location'] = $this->user_location;
    164.     $this->session_info['session_ip'] = $this->user_ip;
    165.  
    166.     if ($this->user_info['user_id'] != GUEST) {
    167.       $this->user_info['user_lastvisit'] = (!empty($this->user_info['user_lastaction'])) ? $this->user_info['user_lastaction'] : $this->current_time;
    168.       $sql = "UPDATE ".USERS_TABLE."
    169.              SET ".get_user_table_field("", "user_lastaction")." = $this->current_time, ".get_user_table_field("", "user_location")." = '$this->user_location', ".get_user_table_field("", "user_lastvisit")." = ".$this->user_info['user_lastvisit']."
    170.              WHERE ".get_user_table_field("", "user_id")." = ".$this->user_info['user_id'];
    171.       $site_db->query($sql);
    172.     }
    173.     $this->set_cookie_data("lastvisit", $this->user_info['user_lastvisit']);
    174.     $this->set_cookie_data("userid", $this->user_info['user_id']);
    175.     return true;
    176.   }
    177.  
    178.   function login($user_name = "", $user_password = "", $auto_login = 0, $set_auto_login = 1) {
    179.     global $site_db, $user_table_fields;
    180.  
    181.     if (empty($user_name) || empty($user_password)) {
    182.       return false;
    183.     }
    184.     $sql = "SELECT ".get_user_table_field("", "user_id").get_user_table_field(", ", "user_password")."
    185.            FROM ".USERS_TABLE."
    186.            WHERE ".get_user_table_field("", "user_name")." = '$user_name' AND ".get_user_table_field("", "user_level")." <> ".USER_AWAITING;
    187.     $row = $site_db->query_firstrow($sql);
    188.  
    189.     $user_id = (isset($row[$user_table_fields['user_id']])) ? $row[$user_table_fields['user_id']] : GUEST;
    190.     if ($user_id != GUEST) {
    191.       if (compare_passwords($user_password, $row[$user_table_fields['user_password']])) {
    192.         $sql = "UPDATE ".SESSIONS_TABLE."
    193.                SET session_user_id = $user_id
    194.                WHERE session_id = '".addslashes($this->session_id)."'";
    195.         $site_db->query($sql);
    196.         if ($set_auto_login) {
    197.           $this->set_cookie_data("userpass", ($auto_login) ? md5($row[$user_table_fields['user_password']]) : "");
    198.         }
    199.         $this->start_session($user_id, 1);
    200.         return true;
    201.       }
    202.     }
    203.     return false;
    204.   }
    205.  
    206.   function logout($user_id) {
    207.     global $site_db;
    208.     $sql = "DELETE FROM ".SESSIONS_TABLE."
    209.            WHERE session_id = '".addslashes($this->session_id)."' OR session_user_id = $user_id";
    210.     $site_db->query($sql);
    211.     $this->set_cookie_data("userpass", "", 0);
    212.     $this->set_cookie_data("userid", GUEST);
    213.  
    214.     $this->session_info = array();
    215.  
    216.     return true;
    217.   }
    218.  
    219.   function delete_old_sessions() {
    220.     global $site_db;
    221.     $expiry_time = $this->current_time - $this->session_timeout;
    222.     $sql = "DELETE FROM ".SESSIONS_TABLE."
    223.            WHERE session_lastaction < $expiry_time";
    224.     $site_db->query($sql);
    225.  
    226.     return true;
    227.   }
    228.  
    229.   function update_session() {
    230.     global $site_db;
    231.  
    232.     $sql = "REPLACE INTO ".SESSIONS_TABLE."
    233.           (session_id, session_user_id, session_lastaction, session_location, session_ip)
    234.           VALUES
    235.           ('".addslashes($this->session_id)."', ".$this->user_info['user_id'].", $this->current_time, '$this->user_location', '$this->user_ip')";
    236.     $site_db->query($sql);
    237.  
    238.     $this->session_info['session_lastaction'] = $this->current_time;
    239.     $this->session_info['session_location'] = $this->user_location;
    240.     $this->session_info['session_ip'] = $this->user_ip;
    241.  
    242.     if ($this->user_info['user_id'] != GUEST) {
    243.       $sql = "UPDATE ".USERS_TABLE."
    244.              SET ".get_user_table_field("", "user_lastaction")." = $this->current_time, ".get_user_table_field("", "user_location")." = '$this->user_location'
    245.              WHERE ".get_user_table_field("", "user_id")." = ".$this->user_info['user_id'];
    246.       $site_db->query($sql);
    247.     }
    248.     return;
    249.   }
    250.  
    251.   function return_session_info() {
    252.     return $this->session_info;
    253.   }
    254.  
    255.   function return_user_info() {
    256.     return $this->user_info;
    257.   }
    258.  
    259.   function freeze() {
    260.     return;
    261.   }
    262.  
    263.   function load_session_info() {
    264.     $register_globals = strtolower(@ini_get('register_globals'));
    265.     if ($register_globals && $register_globals != "off" && $register_globals != "false") {
    266.       session_register($this->session_key);
    267.  
    268.       if (!isset($GLOBALS[$this->session_key])) {
    269.         $GLOBALS[$this->session_key] = array();
    270.       }
    271.  
    272.       $this->session_info = &$GLOBALS[$this->session_key];
    273.  
    274.     } else {
    275.       if (isset($_SESSION)) {
    276.         if (!isset($_SESSION[$this->session_key])) {
    277.           $_SESSION[$this->session_key] = array();
    278.         }
    279.  
    280.         $this->session_info = &$_SESSION[$this->session_key];
    281.  
    282.       } else {
    283.         if (!isset($GLOBALS['HTTP_SESSION_VARS'][$this->session_key])) {
    284.           $GLOBALS['HTTP_SESSION_VARS'][$this->session_key] = array();
    285.         }
    286.  
    287.         $this->session_info = &$GLOBALS['HTTP_SESSION_VARS'][$this->session_key];
    288.       }
    289.     }
    290.  
    291.     if (!isset($this->session_info['session_ip'])) {
    292.       $this->session_info = array();
    293.       return false;
    294.     }
    295.  
    296.     if ($this->mode == "get" && $this->session_info['session_ip'] != $this->user_ip) {
    297.       if (function_exists('session_regenerate_id')) {
    298.         @session_regenerate_id();
    299.       }
    300.       $this->get_session_id();
    301.       $this->session_info = array();
    302.       return false;
    303.     }
    304.  
    305.     return $this->session_info;
    306.   }
    307.  
    308.   function load_user_info($user_id = GUEST) {
    309.     global $site_db, $user_table_fields, $additional_user_fields;
    310.  
    311.     if ($user_id != GUEST) {
    312.       $sql = "SELECT u.*, l.*
    313.              FROM ".USERS_TABLE." u, ".LIGHTBOXES_TABLE." l
    314.              WHERE ".get_user_table_field("u.", "user_id")." = $user_id AND l.user_id = ".get_user_table_field("u.", "user_id");
    315.       $user_info = $site_db->query_firstrow($sql);
    316.       if (!$user_info) {
    317.         $sql = "SELECT *
    318.                FROM ".USERS_TABLE."
    319.                WHERE ".get_user_table_field("", "user_id")." = $user_id";
    320.         $user_info = $site_db->query_firstrow($sql);
    321.         if ($user_info) {
    322.           $lightbox_id = get_random_key(LIGHTBOXES_TABLE, "lightbox_id");
    323.           $sql = "INSERT INTO ".LIGHTBOXES_TABLE."
    324.                  (lightbox_id, user_id, lightbox_lastaction, lightbox_image_ids)
    325.                  VALUES
    326.                  ('$lightbox_id', ".$user_info[$user_table_fields['user_id']].", $this->current_time, '')";
    327.           $site_db->query($sql);
    328.           $user_info['lightbox_lastaction'] = $this->current_time;
    329.           $user_info['lightbox_image_ids'] = "";
    330.         }
    331.       }
    332.     }
    333.     if (empty($user_info[$user_table_fields['user_id']])) {
    334.       $user_info = array();
    335.       $user_info['user_id'] = GUEST;
    336.       $user_info['user_level'] = GUEST;
    337.       $user_info['user_lastaction'] = $this->current_time;
    338.       $user_info['user_lastvisit'] = ($this->read_cookie_data("lastvisit")) ? $this->read_cookie_data("lastvisit") : $this->current_time;
    339.     }
    340.     foreach ($user_table_fields as $key => $val) {
    341.       if (isset($user_info[$val])) {
    342.         $user_info[$key] = $user_info[$val];
    343.       }
    344.       elseif (!isset($user_info[$key])) {
    345.         $user_info[$key] = "";
    346.       }
    347.     }
    348.     foreach ($additional_user_fields as $key => $val)
    349.     {
    350.       if (!isset($user_info[$key]))
    351.       {
    352.         $user_info[$key] = "";
    353.       }
    354.     }
    355.     return $user_info;
    356.   }
    357.  
    358.   function set_session_var($var_name, $value) {
    359.     $this->session_info[$var_name] = $value;
    360.     return true;
    361.   }
    362.  
    363.   function get_session_var($var_name) {
    364.     if (isset($this->session_info[$var_name])) {
    365.       return $this->session_info[$var_name];
    366.     }
    367.  
    368.     return '';
    369.   }
    370.  
    371.   function drop_session_var($var_name) {
    372.     unset($this->session_info[$var_name]);
    373.   }
    374.  
    375.   function get_user_ip() {
    376.     global $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
    377.     $ip = (!empty($HTTP_SERVER_VARS['REMOTE_ADDR'])) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ((!empty($HTTP_ENV_VARS['REMOTE_ADDR'])) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv("REMOTE_ADDR"));
    378.     $ip = preg_replace("/[^\.0-9]+/", "", $ip);
    379.     return substr($ip, 0, 50);
    380.   }
    381.  
    382.   function get_user_location() {
    383.     global $self_url;
    384.     return (defined("IN_CP")) ? "Control Panel" : preg_replace(array("/([?|&])action=[^?|&]*/", "/([?|&])mode=[^?|&]*/", "/([?|&])phpinfo=[^?|&]*/", "/([?|&])printstats=[^?|&]*/", "/[?|&]".URL_ID."=[^?|&]*/", "/[?|&]l=[^?|&]*/", "/[&?]+$/"), array("", "", "", "", "", "", ""), addslashes($self_url));
    385.   }
    386.  
    387.   function url($url, $amp = "&amp;") {
    388.     global $l;
    389.     $dummy_array = explode("#", $url);
    390.     $url = $dummy_array[0];
    391.  
    392.     if ($this->mode == "get" && strpos($url, $this->session_id) === false) {
    393.       $url .= strpos($url, '?') !== false ? $amp : "?";
    394.       $url .= SESSION_NAME."=".$this->session_id;
    395.     }
    396.  
    397.     if (!empty($l)) {
    398.       $url .= strpos($url, '?') !== false ? $amp : "?";
    399.       $url .= "l=".$l;
    400.     }
    401.  
    402.     $url .= (isset($dummy_array[1])) ? "#".$dummy_array[1] : "";
    403.     return $url;
    404.   }
    405. } //end of class
    406.  
    407. //-----------------------------------------------------
    408. //--- Start Session -----------------------------------
    409. //-----------------------------------------------------
    410. define('COOKIE_NAME', '4images_');
    411. define('COOKIE_PATH', '');
    412. define('COOKIE_DOMAIN', '');
    413. define('COOKIE_SECURE', '0');
    414.  
    415. $site_sess = new Session();
    416.  
    417. // Get Userinfo
    418. $session_info = $site_sess->return_session_info();
    419. $user_info = $site_sess->return_user_info();
    420.  
    421. //-----------------------------------------------------
    422. //--- Get User Caches ---------------------------------
    423. //-----------------------------------------------------
    424. $num_total_online = 0;
    425. $num_visible_online = 0;
    426. $num_invisible_online = 0;
    427. $num_registered_online = 0;
    428. $num_guests_online = 0;
    429. $user_online_list = "";
    430. $prev_user_ids = array();
    431. $prev_session_ips = array();
    432.  
    433. if (defined("GET_USER_ONLINE") && ($config['display_whosonline'] == 1 || $user_info['user_level'] == ADMIN)) {
    434.   $time_out = time() - 300;
    435.   $sql = "SELECT s.session_user_id, s.session_lastaction, s.session_ip".get_user_table_field(", u.", "user_id").get_user_table_field(", u.", "user_level").get_user_table_field(", u.", "user_name").get_user_table_field(", u.", "user_invisible")."
    436.      FROM ".SESSIONS_TABLE." s
    437.      LEFT JOIN ".USERS_TABLE." u ON (".get_user_table_field("u.", "user_id")." = s.session_user_id)
    438.      WHERE s.session_lastaction >= $time_out
    439.      ORDER BY ".get_user_table_field("u.", "user_id")." ASC, s.session_ip ASC";
    440.   $result = $site_db->query($sql);
    441.   while ($row = $site_db->fetch_array($result)) {
    442.     if ($row['session_user_id'] != GUEST && (isset($row[$user_table_fields['user_id']]) && $row[$user_table_fields['user_id']] != GUEST)) {
    443.       if (!isset($prev_user_ids[$row['session_user_id']])) {
    444.         $is_invisible = (isset($row[$user_table_fields['user_invisible']]) && $row[$user_table_fields['user_invisible']] == 1) ? 1 : 0;
    445.         $invisibleuser = ($is_invisible) ? "*" : "";
    446.         $username = (isset($row[$user_table_fields['user_level']]) && $row[$user_table_fields['user_level']] == ADMIN && $config['highlight_admin'] == 1) ? sprintf("<b>%s</b>", $row[$user_table_fields['user_name']]) : $row[$user_table_fields['user_name']];
    447.         if (!$is_invisible || $user_info['user_level'] == ADMIN) {
    448.           $user_online_list .= ($user_online_list != "") ? ", " : "";
    449.           $user_profile_link = (!empty($url_show_profile)) ? preg_replace("/{user_id}/", $row['session_user_id'], $url_show_profile) : ROOT_PATH."member.php?action=showprofile&amp;".URL_USER_ID."=".$row['session_user_id'];
    450.           $user_online_list .= "<a href=\"".$site_sess->url($user_profile_link)."\">".str_replace(array("{", "}"), array("{", "}"), $username)."</a>".$invisibleuser;
    451.         }
    452.         (!$is_invisible) ? $num_visible_online++ : $num_invisible_online++;
    453.         $num_registered_online++;
    454.       }
    455.       $prev_user_ids[$row['session_user_id']] = 1;
    456.     }
    457.     else {
    458.       if (!isset($prev_session_ips[$row['session_ip']])) {
    459.         $num_guests_online++;
    460.       }
    461.     }
    462.     $prev_session_ips[$row['session_ip']] = 1;
    463.   }
    464.   $num_total_online = $num_registered_online + $num_guests_online;
    465.   //$num_invisible_online = $num_registered_online - $num_visible_online;
    466.  
    467.   $site_template->register_vars(array(
    468.     "num_total_online" => $num_total_online,
    469.     "num_invisible_online" => $num_invisible_online,
    470.     "num_registered_online" => $num_registered_online,
    471.     "num_guests_online" => $num_guests_online,
    472.     "user_online_list" => $user_online_list,
    473.     "lang_user_online" => str_replace('{num_total_online}', $num_total_online, $lang['user_online']),
    474.     "lang_user_online_detail" => str_replace(array('{num_registered_online}','{num_invisible_online}','{num_guests_online}'), array($num_registered_online,$num_invisible_online,$num_guests_online), $lang['user_online_detail']),
    475.   ));
    476.   $whos_online = $site_template->parse_template("whos_online");
    477.   $site_template->register_vars("whos_online", $whos_online);
    478.   unset($whos_online);
    479.   unset($prev_user_ids);
    480.   unset($prev_session_ips);
    481. }
    482. ?>
    483.  



    Кто может на основе всего этого написать мне поиск по картинкам,и чтобы он искал только по папке:"NEWPICTURE"
     
  3. vaneeeek

    vaneeeek Активный пользователь

    С нами с:
    13 июн 2011
    Сообщения:
    68
    Симпатии:
    0
    Адрес:
    москва
    А то очень надо плиз.
     
  4. Апельсин

    Апельсин Активный пользователь

    С нами с:
    20 мар 2010
    Сообщения:
    3.645
    Симпатии:
    2
    ок, щас все сделаю. [​IMG]
     
  5. vaneeeek

    vaneeeek Активный пользователь

    С нами с:
    13 июн 2011
    Сообщения:
    68
    Симпатии:
    0
    Адрес:
    москва
    Да уж спасибо.Я воще то в натуре прошу.
     
  6. tommyangelo

    tommyangelo Старожил

    С нами с:
    6 дек 2009
    Сообщения:
    2.549
    Симпатии:
    0
    Адрес:
    Мариуполь
    чувак, мы тебе премию дадим за самый длинный код)

    А по теме - если очень надо - предложи бабла, может кто заинтересуется)
     
  7. vaneeeek

    vaneeeek Активный пользователь

    С нами с:
    13 июн 2011
    Сообщения:
    68
    Симпатии:
    0
    Адрес:
    москва
    Скока примерно???