* Copyright (c) 2001-2008 Platon Group, http://platon.sk/ * All rights reserved. * * See README file for more information about this software. * See COPYING file for license information. * * Download the latest version from * http://platon.sk/projects/Metafox/ */ /* $Platon: Metafox/db.inc.php,v 1.91 2024/11/20 15:48:03 igor Exp $ */ /* * updates: 23/5/2001, 29/7/2001, 11/10/2001, 2/11/2001, 11/1/2002, 23/7/2002 * whole august/2002, 30/11/2002, 15/1/2003 */ require_once 'cfgldr.inc.php'; require_once 'functions.inc.php'; require_once 'db-core/db-core.inc.php'; function ezin_get_remote_addr($include_remote_host = false) /* {{{ */ { $ret = array(); foreach (array(($include_remote_host ? 'REMOTE_HOST' : 'REMOTE_ADDR'), 'REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP') as $key) { if (isset($_SERVER[$key]) && strlen($_SERVER[$key]) > 0) { $ret[] = trim(stripslashes($_SERVER[$key])); } } $ret = array_unique($ret); $ret = join(' / ', $ret); return $ret; } /* }}} */ /* * VALIDATION FUNCTIONS */ /* * ezin_validate_IDs() */ function ezin_validate_IDs($ar) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $ezin_authors = $ezin_sys['db']['tables']['authors']; $ezin_sections = $ezin_sys['db']['tables']['sections']; $ezin_articles = $ezin_sys['db']['tables']['articles']; $ezin_messages = $ezin_sys['db']['tables']['messages']; $row = false; $ret_row = false; $id_included = 0; $c_author = ! empty($ar['author_id']); $c_section = ! empty($ar['section_id']); $c_article = ! empty($ar['article_id']); $c_message = ! empty($ar['message_id']) && ! is_array($ar['message_id']) && intval($ar['message_id']) > 0; $conditions = array(); $tables = array(); $select_cols = array(); if ($c_author || $c_section || $c_article || $c_message) { if ($c_author) { if ($c_article) { $tables[] = $ezin_articles; $select_cols[] = $ezin_articles.'.author_id AS author_id'; $select_cols[] = $ezin_articles.'.id AS article_id'; $select_cols[] = $ezin_articles.'.access_key AS article_access_key'; // $conditions[] = $ezin_articles.'.author_id = '.$ezin_authors.'.id'; } else { $tables[] = $ezin_authors; $select_cols[] = $ezin_authors.'.id AS author_id'; $select_cols[] = $ezin_authors.'.access_key AS author_access_key'; if (intval($ar['author_id']) > 0) { $conditions[] = $ezin_authors.'.id = '.intval($ar['author_id']); } else { $conditions[] = $ezin_authors.'.access_key = "'.ezin_db_escape($ar['author_id']).'"'; } } } else { $tables = array($ezin_sections); $select_cols[] = $ezin_sections.'.id AS section_id'; $select_cols[] = $ezin_sections.'.access_key AS section_access_key'; $select_cols[] = $ezin_sections.'.bind_lang AS section_bind_lang'; $select_cols[] = $ezin_sections.'.lang AS lang'; if ($c_article || $c_message) { $tables[] = $ezin_articles; $select_cols[] = $ezin_articles.'.id AS article_id'; $select_cols[] = $ezin_articles.'.access_key AS article_access_key'; $select_cols[] = $ezin_articles.'.bind_lang AS article_bind_lang'; $conditions = array_merge($conditions, array( $ezin_sections.'.id = '.$ezin_articles.'.section_id', //$ezin_articles.'.datetime <= SYSDATE() ', '(FIND_IN_SET("enabled", '.$ezin_articles.'.display) > 0)', '(FIND_IN_SET("articles_enabled", '.$ezin_sections.'.display) > 0)', )); } if ($c_message) { $tables[] = $ezin_messages; $select_cols[] = $ezin_messages.'.id AS message_id'; $conditions[] = $ezin_articles.'.id = '.$ezin_messages.'.article_id'; $conditions[] = $ezin_messages.'.id = '.intval($ar['message_id']); $id_included = 1; } } if (! $id_included && $c_article) { if (intval($ar['article_id']) > 0) { $conditions[] = $ezin_articles.'.id = '.intval($ar['article_id']); } else { $conditions[] = $ezin_articles.'.access_key = "'.ezin_db_escape($ar['article_id']).'"'; } $id_included = 1; } if (! $id_included && $c_section) { $conditions[] = '(FIND_IN_SET("articles_enabled", '.$ezin_sections.'.display) > 0)'; if (intval($ar['section_id']) > 0) { $conditions[] = $ezin_sections.'.id = '.intval($ar['section_id']); } else { $conditions[] = $ezin_sections.'.access_key = "'.ezin_db_escape($ar['section_id']).'"'; } $id_included = 1; } $query = sprintf('SELECT %s FROM %s WHERE %s',// LIMIT 1', join(', ', $select_cols), join(', ', $tables), join(' AND ', $conditions)); //echo $query;exit; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_validate_IDs()'); $first = true; $ret_row = false; while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { if ($first) { $first = false; $ret_row = $row; if (@$ar['lang'] == false || @$ar['lang'] == '') break; } if (isset($row['lang']) && ! @strcasecmp($row['lang'], $ar['lang'])) { $ret_row = $row; break; } } mysqli_free_result($result); } if ($ret_row !== false) { $ar['lang'] = @$ret_row['lang']; $ar['author_id'] = @$ret_row['author_id']; $ar['section_id'] = @$ret_row['section_id']; $ar['article_id'] = isset($ret_row['article_id']) ? $ret_row['article_id'] : 0; $ar['message_id'] = isset($ret_row['message_id']) ? $ret_row['message_id'] : (is_array($ar['message_id']) ? $ar['message_id'] : 0); $ar['section_bind_lang'] = intval(@$ret_row['section_bind_lang']); $ar['article_bind_lang'] = intval(@$ret_row['article_bind_lang']); $ar['section_access_key'] = @$ret_row['section_access_key']; $ar['article_access_key'] = @$ret_row['article_access_key']; $ar['author_access_key'] = @$ret_row['author_access_key']; } else { // lookup for old access-keys $ar_redirect = null; if (strlen($ar['author_id']) > 0) { $ret = ezin_get_access_key_redirect($ar['author_id'], 'author'); if (is_array($ret) && count($ret) > 0) { $ar_redirect = ezin_validate_IDs(array('author_id' => $ret['id'])); $ar_redirect['redirect'] = true; } } else if (strlen($ar['article_id']) > 0) { $ret = ezin_get_access_key_redirect($ar['article_id'], 'article'); if (is_array($ret) && count($ret) > 0) { $ar_redirect = ezin_validate_IDs(array('article_id' => $ret['id'])); $ar_redirect['redirect'] = true; } if (is_null($ar_redirect)) { $parts = ezin_source_article_id_decode($ar['article_id']); $section = ezin_get_section($parts['section_id']); $ar_redirect['section_id'] = $parts['section_id']; $ar_redirect['article_id'] = $parts['article_id']; $ar_redirect['lang'] = $section['section_lang']; } } else if (strlen($ar['section_id']) > 0) { $ret = ezin_get_access_key_redirect($ar['section_id'], 'section'); if (is_array($ret) && count($ret) > 0) { $ar_redirect = ezin_validate_IDs(array('section_id' => $ret['id'])); $ar_redirect['redirect'] = true; } else { // lookup for articles with actual access-key if (ezin_exists_article($ar['section_id'])) { $ar_redirect = ezin_validate_IDs(array('article_id' => $ar['section_id'])); $ar_redirect['redirect'] = true; } } } if (!is_null($ar_redirect)) { $ar = $ar_redirect; } else { $ar['lang'] = ''; $ar['author_id'] = 0; $ar['section_id'] = 0; $ar['article_id'] = 0; $ar['message_id'] = 0; $ar['section_bind_lang'] = 0; $ar['article_bind_lang'] = 0; $ar['section_access_key'] = ''; $ar['article_access_key'] = ''; } } return $ar; } /* }}} */ function ezin_get_bind_lang_table_wrapper(&$lang_tbl_type, $section_bind, $article_bind = 0) /* {{{ */ { $lang_tbl_type = 'A'; $bind_lang = intval($article_bind); if ($article_bind == 0) { $lang_tbl_type = 'S'; $bind_lang = intval($section_bind); } return ezin_get_bind_lang_table($bind_lang, $lang_tbl_type); } /* }}} */ function ezin_get_bind_lang_table($bind_lang, $type = 'S' /* S or A */) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $bind_lang = intval($bind_lang); if ($bind_lang == 0) { return array(); } if ($type == 'S') { $query = 'SELECT id, access_key, lang, link' .' FROM '.$ezin_sys['db']['tables']['sections'] .' WHERE bind_lang = '.$bind_lang .' ORDER BY id ASC'; } else { $query = 'SELECT articles.id, articles.access_key, sections.lang FROM ' .$ezin_sys['db']['tables']['articles'].' AS articles, ' .$ezin_sys['db']['tables']['sections'].' AS sections ' .' WHERE articles.section_id = sections.id' .' AND articles.bind_lang = '.$bind_lang .' ORDER BY articles.datetime ASC'; } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_bind_lang_table()'); $array = array(); while (($row = mysqli_fetch_array($result, MYSQLI_NUM)) != null) { $array[$row[2]] = $row[0]; $array[$row[2].'2'] = empty($row[1]) ? $row[0] : $row[1]; $array[$row[2].'3'] = empty($row[3]) ? null : $row[3]; } mysqli_free_result($result); return $array; } /* }}} */ /* * DATA RETRIEVAL FUNCTIONS */ /* * ezin_get_section_list() */ function ezin_get_section_list($lang = '', $current_section_id = null) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $current_section_id = intval($current_section_id); $query = 'SELECT' .' id section_id,' .' type2 section_type,' .' ord section_ord,' .' a_limit section_limit,' .' name section_name,' .' access_key section_access_key,' .' template_key section_template_key,' .' link section_link,' .' created_dt section_created_dt,' .' changed_dt section_changed_dt' .' FROM '.$ezin_sys['db']['tables']['sections'] .' WHERE (FIND_IN_SET("in_list", display) > 0)'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } $query .= ' ORDER BY ord ASC, type ASC, id ASC'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_section_list()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } /* }}} */ function ezin_get_section_list_counts($lang = '', $current_section_id = null) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $current_section_id = intval($current_section_id); $query = 'SELECT id, type, accesses_count, sessions_count, raw_accesses_count, raw_sessions_count' .' FROM '.$ezin_sys['db']['tables']['sections'] .' WHERE (FIND_IN_SET("in_list", display) > 0)'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } $query .= ' ORDER BY ord ASC, type ASC, id ASC'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_section_list()'); $array = array(); for ($k = 0; ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) { $array['section_accesses_count_'.$row['id']] = $row['accesses_count']; $array['section_sessions_count_'.$row['id']] = $row['sessions_count']; $array['section_raw_accesses_count_'.$row['id']] = $row['raw_accesses_count']; $array['section_raw_sessions_count_'.$row['id']] = $row['raw_sessions_count']; if ($row['id'] == $current_section_id) { $array['section_selected_'.$row['id']] = 1; } else if (!$current_section_id && $row['type'] == 'global') { $array['section_selected_'.$row['id']] = 1; } else { $array['section_selected_'.$row['id']] = 0; } } mysqli_free_result($result); return $array; } /* }}} */ function ezin_get_section_id($section_access_key) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; if (strlen($section_access_key) <= 0) { return false; } $query = 'SELECT' .' sections.id section_id' .' FROM '.$ezin_sys['db']['tables']['sections'].' AS sections' .' WHERE sections.access_key = "'.ezin_db_escape($section_access_key).'"'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_section_id() [1]'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) return false; mysqli_free_result($result); return $row['section_id']; } /* }}} */ /* * ezin_get_section() */ function ezin_get_section($section_id, $article_id = null) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $section_id = intval($section_id); $article_id = intval($article_id); $query = 'SELECT' .' sections.id section_id,' .' sections.type section_type,' .' sections.type2 section_type2,' .' sections.display section_display,' .' sections.select section_select,' .' sections.ord section_ord,' .' sections.a_limit section_limit,' .' sections.accesses_count as section_accesses_count,' .' sections.sessions_count as section_sessions_count,' .' sections.raw_accesses_count as section_raw_accesses_count,' .' sections.raw_sessions_count as section_raw_sessions_count,' .' sections.name section_name,' .' sections.access_key section_access_key,' .' sections.template_key section_template_key,' .' sections.link section_link,' .' sections.lang section_lang' .' FROM '.$ezin_sys['db']['tables']['sections'].' AS sections' /* XXX let this here, although global sections now use direct ID retrieving */ .' WHERE '.($section_id > 0 ? 'sections.id = '.$section_id : '(FIND_IN_SET("global", sections.type2) > 0)') .' ORDER BY sections.id ASC LIMIT 1'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_section() [1]'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) $row = array(); mysqli_free_result($result); if (count($row) > 0) { $editorial = ezin_get_editorial($section_id, $article_id); if (is_array($editorial) && count($editorial) > 0) { foreach ($editorial as $key => $val) { $row["section_$key"] = $val; } } } return $row; } /* }}} */ /* * ezin_exists_section() */ function ezin_exists_section($section_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; if (is_numeric($section_id)) { $where = sprintf('id = %d', intval($section_id)); } else { $where = sprintf('access_key = "%s"', ezin_db_escape(trim($section_id))); } $query = sprintf('SELECT COUNT(*)' .' FROM %s' .' WHERE %s', $ezin_sys['db']['tables']['sections'], $where); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_exists_section()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); return (intval($row[0]) > 0) ? TRUE : FALSE; } /* }}} */ function ezin_get_editorial($section_id = null, $article_id = null) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $section_id = intval($section_id); $article_id = intval($article_id); for ($step = 1; $step <= 3; $step++) { $query = null; if ($step == 1 && $article_id > 0) { $query = 'SELECT' .' UNIX_TIMESTAMP(editorials.datetime) editorial_timestamp,' .' editorials.text_wordcount editorial_text_wordcount,' .' editorials.text_flags editorial_text_flags,' .' editorials.text editorial_text,' .' editorials.title editorial_title' .' FROM '.$ezin_sys['db']['tables']['editorials'].' AS editorials' .' WHERE article_id = '.$article_id .' AND (editorials.datetime IS NULL OR editorials.datetime <= SYSDATE())' .' ORDER BY editorials.datetime DESC LIMIT 1'; } if ($step == 2 && $section_id > 0) { $query = 'SELECT' .' UNIX_TIMESTAMP(editorials.datetime) editorial_timestamp,' .' editorials.text_wordcount editorial_text_wordcount,' .' editorials.text_flags editorial_text_flags,' .' editorials.text editorial_text,' .' editorials.title editorial_title' .' FROM '.$ezin_sys['db']['tables']['editorials'].' AS editorials,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections' .' WHERE editorials.section_id = '.$section_id .' AND (editorials.datetime IS NULL OR editorials.datetime <= SYSDATE())' .' AND (editorials.section_id = sections.id)' .' AND (FIND_IN_SET("editorials", sections.select) > 0)' .' ORDER BY editorials.datetime DESC LIMIT 1'; } if ($step == 3) { $query = 'SELECT' .' UNIX_TIMESTAMP(editorials.datetime) editorial_timestamp,' .' editorials.text_wordcount editorial_text_wordcount,' .' editorials.text_flags editorial_text_flags,' .' editorials.text editorial_text,' .' editorials.title editorial_title,' .' 1 editorial_inherited' .' FROM '.$ezin_sys['db']['tables']['editorials'].' AS editorials,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections' .' WHERE sections.id = editorials.section_id AND (FIND_IN_SET("global", sections.type2) > 0)' .' AND (editorials.datetime IS NULL OR editorials.datetime <= SYSDATE())' .' ORDER BY editorials.datetime DESC LIMIT 1'; } if (strlen($query) > 0) { $row = null; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_editorials [1]'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $row = array_merge($row, $row); } mysqli_free_result($result); if (is_array($row)) { return $row; } } } return null; } /* }}} */ /* * ezin_get_articles_todo() */ function ezin_get_articles_todo() /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $query = sprintf('SELECT COUNT(*) FROM %s WHERE datetime > SYSDATE()', $ezin_sys['db']['tables']['articles']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_articles_todo()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); return intval($row[0]); } /* }}} */ /* * ezin_get_article_list() * TODO: this should probably re-think or re-implement (with database enums too) */ function ezin_get_article_list($section_id = 0, $author_id = 0, $limit = 10, $lang = '', $from = null) /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $section_id = intval($section_id); //$author_id = intval($author_id); $limit = intval($limit); $query = 'SELECT' .' articles.id article_id,' .' articles.atitle article_atitle,' .' articles.title article_title,' .' articles.status article_status,' .' articles.access_key article_access_key,' .' articles.template_key article_template_key,' .' UNIX_TIMESTAMP(articles.datetime) article_timestamp,' .' articles.bodytext_wordcount article_bodytext_wordcount,' .' articles.bodytext_flags article_bodytext_flags,' .' articles.ghometext_wordcount article_ghometext_wordcount,' .' articles.ghometext_flags article_ghometext_flags,' .' articles.ghometext article_ghometext,' .' articles.hometext_wordcount article_hometext_wordcount,' .' articles.hometext_flags article_hometext_flags,' .' articles.hometext article_hometext,' .' articles.calendar_flag article_calendar_flag,' .' articles.created_dt article_created_dt,' .' articles.changed_dt article_changed_dt,' .' authors.id article_author_id,' .' authors.name article_author_name,' .' authors.email article_author_email,' .' authors.homepage article_author_homepage,' .' authors.position article_author_position,' .' authors.location article_author_location,' .' authors.access_key article_author_access_key,' .' authors.template_key article_author_template_key,' //.' authors.description article_author_description,' .' sections.id article_section_id,' .' sections.name article_section_name,' .' sections.type article_section_type,' .' sections.access_key article_section_access_key,' .' sections.template_key article_section_template_key,' .' sections.link article_section_link' //.' sections.ord article_section_ord,' //.' sections.a_limit article_section_limit' .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles' .' LEFT JOIN '.$ezin_sys['db']['tables']['authors'].' AS authors' .' ON articles.author_id = authors.id,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections'; $query .= ' WHERE articles.section_id = sections.id' .(($ezin_cfg['article_datetime_filter'] == 1) ? ' AND articles.datetime <= SYSDATE()' : '') // .' AND UNIX_TIMESTAMP(articles.datetime) > 0' .' AND ( (FIND_IN_SET("articles_enabled", sections.display) > 0)' .' OR (FIND_IN_SET("articles_in_normal", sections.display) > 0)' .' OR (FIND_IN_SET("articles_in_global", sections.display) > 0)' .' )'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } if (strlen($author_id) > 0 && !(is_int($author_id) && $author_id == 0)) { $limit = 0; // unlimited articles on author pages if (is_numeric($author_id)) { if (intval($author_id) > 0) { $query_nobind = ' AND authors.id = '.intval($author_id); $query2 = 'SELECT id' .' FROM '.$ezin_sys['db']['tables']['authors'] .' WHERE bind = (' .' SELECT bind' .' FROM '.$ezin_sys['db']['tables']['authors'] .' WHERE id = '.intval($author_id) .' LIMIT 1' .' )' .' AND bind != 0'; } } else { $query_nobind = ' AND authors.name LIKE "%'.ezin_db_escape($author_id).'%"'; $query2 = 'SELECT id' .' FROM '.$ezin_sys['db']['tables']['authors'] .' WHERE bind = (' .' SELECT bind' .' FROM '.$ezin_sys['db']['tables']['authors'] .' WHERE name LIKE "%'.ezin_db_escape($author_id).'%"' .' LIMIT 1' .' )' .' AND bind != 0'; } $result2 = mysqli_query($ezin_db_handle, $query2) or ezin_db_error('ezin_get_article_list()'); $array2 = array(); for ($k = 0; ($array2[$k] = mysqli_fetch_array($result2, MYSQLI_ASSOC)) != null; $k++) ; unset($array2[$k]); mysqli_free_result($result2); if (count($array2) > 0) { $query .= ' AND ('; for ($a = 0; $a < count($array2); $a++) { $query .= ($a > 0) ? ' OR ' : ''; $query .= ' authors.id = '.intval($array2[$a]['id']); } $query .= ')'; } else { $query .= $query_nobind; } } else { $query .= ' AND ((FIND_IN_SET("in_normal", articles.display) > 0)' .' OR (FIND_IN_SET("in_global", articles.display) > 0)' .' )'; if ($section_id > 0) { $query .= ' AND sections.id = '.$section_id; $query .= ' AND (FIND_IN_SET("articles_in_normal", sections.display) > 0)'; $query .= ' AND (' .' ((FIND_IN_SET("articles", sections.select) > 0)' .' AND (UNIX_TIMESTAMP(articles.datetime) > 0))' .' OR ((FIND_IN_SET("pages", sections.select) > 0)' .' AND (UNIX_TIMESTAMP(articles.datetime) = 0))' .' )'; } else { $query .= ' AND (FIND_IN_SET("articles_in_global", sections.display) > 0)' .' AND (FIND_IN_SET("in_global", articles.display) > 0)'; } } $query .= ' ORDER BY articles.datetime ' .($ezin_cfg['order_articles_desc'] ? 'DESC' : 'ASC') .', articles.id DESC'; if ($limit > 0) { if (is_null($from)) { $query .= ' LIMIT '.$limit; } else { $from = intval($from); $query .= ' LIMIT '.$from.','.$limit; } } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_article_list()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); // add articles from sources $sources_articles = ezin_get_article_list_sources($section_id); if (is_array($sources_articles) && (count($sources_articles) > 1)) { $array = array_merge($array, $sources_articles); // order all articles by time $sortArray = array(); foreach($array as $article){ foreach($article as $key=>$value){ if(!isset($sortArray[$key])){ $sortArray[$key] = array(); } $sortArray[$key][] = $value; } } $orderby = 'article_timestamp'; array_multisort($sortArray[$orderby], ($ezin_cfg['order_articles_desc']) ? SORT_DESC : SORT_ASC, $array); } if ($limit > 0) { $array = array_slice($array, 0, $limit); } return $array; } /* }}} */ function ezin_get_available_articles($lang = '') /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $section_id = intval($section_id); $limit = intval($limit); $query = 'SELECT' .' articles.id article_id,' .' articles.atitle article_atitle,' .' articles.title article_title,' .' articles.status article_status,' .' articles.access_key article_access_key,' .' articles.template_key article_template_key,' .' UNIX_TIMESTAMP(articles.datetime) article_timestamp,' .' articles.bodytext_wordcount article_bodytext_wordcount,' .' articles.bodytext_flags article_bodytext_flags,' .' articles.ghometext_wordcount article_ghometext_wordcount,' .' articles.ghometext_flags article_ghometext_flags,' .' articles.ghometext article_ghometext,' .' articles.hometext_wordcount article_hometext_wordcount,' .' articles.hometext_flags article_hometext_flags,' .' articles.hometext article_hometext,' .' articles.calendar_flag article_calendar_flag,' .' articles.created_dt article_created_dt,' .' articles.changed_dt article_changed_dt,' .' sections.id article_section_id,' .' sections.name article_section_name,' .' sections.type article_section_type,' .' sections.access_key article_section_access_key,' .' sections.template_key article_section_template_key,' .' sections.link article_section_link' .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles' .' LEFT JOIN '.$ezin_sys['db']['tables']['sections'].' AS sections' .' ON articles.section_id = sections.id'; $query .= ' WHERE articles.datetime <= SYSDATE()' .' AND ( (FIND_IN_SET("articles_enabled", sections.display) > 0)' .' OR (FIND_IN_SET("articles_in_normal", sections.display) > 0)' .' OR (FIND_IN_SET("articles_in_global", sections.display) > 0)' .' )' .' AND (FIND_IN_SET("enabled", articles.display) > 0)'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } $query .= ' ORDER BY articles.datetime ' .($ezin_cfg['order_articles_desc'] ? 'DESC' : 'ASC') .', articles.id DESC'; if ($limit > 0) { if (is_null($from)) { $query .= ' LIMIT '.$limit; } else { $from = intval($from); $query .= ' LIMIT '.$from.','.$limit; } } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_available_articles()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } /* }}} */ /* * ezin_get_featured_articles_list() */ function ezin_get_featured_articles_list($limit = 10, $lang = '') /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $limit = intval($limit); $query = 'SELECT' .' articles.id featured_article_id,' .' articles.atitle featured_article_atitle,' .' articles.title featured_article_title,' .' articles.status featured_article_status,' .' articles.access_key featured_article_access_key,' .' articles.template_key featured_article_template_key,' .' UNIX_TIMESTAMP(articles.datetime) featured_article_timestamp,' .' articles.bodytext_wordcount featured_article_bodytext_wordcount,' .' articles.bodytext_flags featured_article_bodytext_flags,' .' articles.ghometext_wordcount featured_article_ghometext_wordcount,' .' articles.ghometext_flags featured_article_ghometext_flags,' .' articles.ghometext featured_article_ghometext,' .' articles.hometext_wordcount featured_article_hometext_wordcount,' .' articles.hometext_flags featured_article_hometext_flags,' .' articles.hometext featured_article_hometext,' .' articles.calendar_flag featured_article_calendar_flag,' .' authors.id featured_article_author_id,' .' authors.name featured_article_author_name,' .' authors.email featured_article_author_email,' .' authors.homepage featured_article_author_homepage,' .' authors.position featured_article_author_position,' .' authors.location featured_article_author_location,' .' authors.access_key featured_article_author_access_key,' .' authors.template_key featured_article_author_template_key,' .' sections.id featured_article_section_id,' .' sections.name featured_article_section_name,' .' sections.type featured_article_section_type,' .' sections.access_key featured_article_section_access_key,' .' sections.template_key featured_article_section_template_key,' .' sections.link featured_article_section_link' .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles' .' LEFT JOIN '.$ezin_sys['db']['tables']['authors'].' AS authors' .' ON articles.author_id = authors.id,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections'; $query .= ' WHERE articles.section_id = sections.id' .' AND articles.featured_articles_flag IS NOT NULL'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } $query .= ' ORDER BY articles.featured_articles_flag DESC'; if ($limit > 0) { $query .= ' LIMIT '.$limit; } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_featured_articles_list()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } /* }}} */ /* * ezin_get_article_list_counts() */ function ezin_get_article_list_counts($section_id = 0, $author_id = 0, $limit = 10, $lang = '') /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $section_id = intval($section_id); $limit = intval($limit); $query = 'SELECT articles.id,' .' articles.accesses_count, articles.sessions_count, articles.message_count' .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles' .' LEFT JOIN '.$ezin_sys['db']['tables']['authors'].' AS authors' .' ON articles.author_id = authors.id,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections'; $query .= ' WHERE articles.section_id = sections.id' .' AND articles.datetime <= SYSDATE()' .' AND UNIX_TIMESTAMP(articles.datetime) > 0' .' AND articles.status NOT IN ("", "disabled")' .' AND sections.type NOT IN ("", "full_hidden", "disabled")'; if ($lang != '') { $query .= ' AND (lang IS NULL OR lang = ""' .' OR lang LIKE "'.ezin_db_escape($lang).'")'; } if (strlen($author_id) > 0) { $limit = 0; // unlimited articles on author pages if (is_numeric($author_id)) { $query .= ' AND authors.id = '.intval($author_id); } else { $query .= ' AND authors.name LIKE "%'.ezin_db_escape($author_id).'%"'; } } else { $query .= ' AND articles.status NOT IN ("hidden")'; if ($section_id > 0) { $query .= ' AND sections.id = '.$section_id; $query .= ' AND (sections.type != "global" OR articles.status != "partly_showed")'; } else { $query .= ' AND sections.type != "hidden2" AND articles.status != "partly_showed"'; } } $query .= ' ORDER BY articles.datetime ' .($ezin_cfg['order_articles_desc'] ? 'DESC' : 'ASC') .', articles.id DESC'; if ($limit > 0) { $query .= ' LIMIT '.$limit; } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_article_list_counts()'); $array = array(); for ($k = 0; ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) { $array['article_accesses_count_'.$row['id']] = $row['accesses_count']; $array['article_sessions_count_'.$row['id']] = $row['sessions_count']; $array['article_message_count_'.$row['id']] = $row['message_count']; } mysqli_free_result($result); return $array; } /* }}} */ /* * ezin_get_article_list_source() */ function ezin_get_article_list_sources($section_id) /* {{{ */ { $src_articles = ezin_get_sources($section_id); $array = array(); foreach($src_articles as $src) { switch ($src['source_type']) { case 'rss': // download articles from RSS url $rss_articles = ezin_get_rss_articles($src); if (count($rss_articles) > 0) { $array = array_merge($array, $rss_articles); } break; } } return $array; } /* }}} */ /* * ezin_get_article() */ function ezin_get_article($article_id) /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; /* Article from source */ if (strlen($article_id) >= 6 && substr($article_id, 0, 6) == 'source') { $parts = ezin_source_article_id_decode($article_id); $source = ezin_get_source($parts['source_id']); $articles = ezin_get_rss_articles($source); foreach ($articles as $article) { if ($article['article_id'] == $article_id) { return $article; } } return false; } /* Article from DB */ $article_id = intval($article_id); $query = 'SELECT' .' articles.id article_id,' .' articles.author_id article_author_id,' .' articles.section_id article_section_id,' .' articles.atitle article_atitle,' .' articles.title article_title,' .' articles.status article_status,' .' articles.accesses_count article_accesses_count,' .' articles.sessions_count article_sessions_count,' .' articles.message_count article_message_count,' .' articles.access_key article_access_key,' .' articles.template_key article_template_key,' .' UNIX_TIMESTAMP(articles.datetime) article_timestamp,' .' articles.hometext_wordcount article_hometext_wordcount,' .' articles.bodytext_wordcount article_bodytext_wordcount,' .' articles.hometext_flags article_hometext_flags,' .' articles.bodytext_flags article_bodytext_flags,' .' articles.hometext article_hometext,' .' articles.bodytext article_bodytext,' .' articles.calendar_flag article_calendar_flag,' .' authors.id article_author_id,' .' authors.access_key article_author_access_key,' .' authors.name article_author_name,' .' authors.email article_author_email,' .' authors.homepage article_author_homepage,' .' authors.position article_author_position,' .' authors.location article_author_location,' //.' authors.description article_author_description,' .' sections.id article_section_id,' .' sections.name article_section_name,' .' sections.type article_section_type,' .' sections.access_key article_section_access_key,' .' sections.template_key article_section_template_key,' .' sections.link article_section_link' //.' sections.ord article_section_ord,' //.' sections.a_limit article_section_limit' .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles' .' LEFT JOIN '.$ezin_sys['db']['tables']['authors'].' AS authors' .' ON articles.author_id = authors.id,' .' '.$ezin_sys['db']['tables']['sections'].' AS sections' .' WHERE articles.section_id = sections.id ' .' AND articles.id = '.$article_id .' ORDER BY articles.datetime ' .($ezin_cfg['order_articles_desc'] ? 'DESC' : 'ASC') .', articles.id ASC'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_article()'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) $row = array(); mysqli_free_result($result); return $row; } /* }}} */ /* * ezin_exists_article() */ function ezin_exists_article($article_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; if (is_numeric($article_id)) { $where = sprintf('id = %d', intval($article_id)); } else { $where = sprintf('access_key = "%s"', ezin_db_escape(trim($article_id))); } $query = sprintf('SELECT COUNT(*)' .' FROM %s' .' WHERE %s', $ezin_sys['db']['tables']['articles'], $where); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_exists_article()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); return (intval($row[0]) > 0) ? TRUE : FALSE; } /* }}} */ /* * ezin_get_oldest_dt() * - date of oldest article */ function ezin_get_oldest_dt() { global $ezin_sys; global $ezin_db_handle; $query = sprintf('SELECT created_dt' .' FROM %s' .' WHERE created_dt > 0' .' ORDER BY created_dt ASC' .' LIMIT 1', $ezin_sys['db']['tables']['articles']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_exists_article()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); return $row[0]; } /* * ezin_get_author() */ function ezin_get_author($author_id) /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $author_id = intval($author_id); $ret_row = array(); $query = sprintf('SELECT * FROM %s WHERE id = %d', $ezin_sys['db']['tables']['authors'], $author_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_author() [1]'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) $row = array(); mysqli_free_result($result); foreach (array_keys($row) as $key) { $ret_row['author_'.$key] = $row[$key]; } $query = sprintf('SELECT * FROM %s WHERE author_id = %d LIMIT 1', $ezin_sys['db']['tables']['users'], $author_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_author() [2]'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) $row = array(); mysqli_free_result($result); foreach (array_keys($row) as $key) { if ($key == 'id') { $ret_row['author_user_id'] = $row[$key]; } else { $ret_row['author_'.$key] = $row[$key]; } } $query = sprintf('SELECT COUNT(*) FROM %s WHERE author_id = %d AND status != "disabled"', $ezin_sys['db']['tables']['articles'], $author_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_author() [3]'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); $ret_row['author_article_count'] = intval($row[0]); return $ret_row; } /* }}} */ /* * ezin_get_authors() */ function ezin_get_authors($count = 0) { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $count = intval($count); $ret_row = array(); if ($count == 0) { $query = sprintf('SELECT * FROM %s', $ezin_sys['db']['tables']['authors']); } else { $query = sprintf('SELECT * FROM %s LIMIT %d', $ezin_sys['db']['tables']['authors'], $count); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_authors() [1]'); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $ret_row[] = $row; } mysqli_free_result($result); return $ret_row; } function ezin_exists_author($username) /* {{{ */ { global $ezin_sys, $ezin_cfg; global $ezin_db_handle; $query = sprintf('SELECT COUNT(*) FROM %s WHERE username = "%s"', $ezin_sys['db']['tables']['users'], ezin_db_escape($username)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_exists_author() [1]'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); return intval($row[0]); } /* }}} */ function ezin_save_author($data) /* {{{ */ { $exists = ezin_exists_author($data['username']); if ($exists) { return ezin_update_author($data); } else if ($ezin_cfg['user_create_role_id'] > 0) { return ezin_insert_author($data); } return false; } /* }}} */ function ezin_update_author($data) /* {{{ */ { global $ezin_sys, $ezin_cfg; global $ezin_db_handle; $password = $data['password']; $data = ezin_db_escape($data); // author_id $query = sprintf('SELECT id,author_id FROM %s WHERE username = "%s"', $ezin_sys['db']['tables']['users'], $data['username']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_author() [2]'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); $user_id = intval($row[0]); $author_id = intval($row[1]); // users update (password only) if (strlen($password) > 0) { $query = sprintf('UPDATE %s SET password="%s", changed_by="%s", changed_id=%d, changed_dt=SYSDATE()' .' WHERE username="%s"', $ezin_sys['db']['tables']['users'], md5($data['password']), $data['username'], $user_id, $data['username']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_author() [1]'); } // users update $query = sprintf('UPDATE %s SET name="%s", email="%s", homepage="%s",' .'position="%s", location="%s", description="%s",' .'changed_by="%s", changed_id=%d, changed_dt=SYSDATE() WHERE id=%d', $ezin_sys['db']['tables']['authors'], $data['name'], $data['email'], $data['homepage'], $data['position'], $data['location'], $data['description'], $data['username'], $user_id, $author_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_author() [3]'); return true; } /* }}} */ function ezin_insert_author($data) /* {{{ */ { global $ezin_sys, $ezin_cfg; global $ezin_db_handle; $data = ezin_db_escape($data); // authors INSERT $query = sprintf('INSERT INTO %s' .' (type,name,email,homepage,position,location,description,created_by,created_dt)' .' VALUES ("external","%s","%s","%s","%s","%s","%s","%s",SYSDATE())', $ezin_sys['db']['tables']['authors'], $data['name'], $data['email'], $data['homepage'], $data['position'], $data['location'], $data['description'], $data['username']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_insert_author() [1]'); // author_id $query = 'SELECT LAST_INSERT_ID()'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_insert_author() [2]'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row = array(); mysqli_free_result($result); $author_id = intval($row[0]); // users INSERT $query = sprintf('INSERT INTO %s' .' (username,password,author_id,role_id,created_by,created_dt)' .' VALUES ("%s","%s",%d,%d,"%s",SYSDATE())', $ezin_sys['db']['tables']['users'], $data['username'], md5($data['password']), $author_id, $ezin_cfg['user_create_role_id'],$data['username']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_insert_author() [3]'); return true; } /* }}} */ /* * ezin_get_users() */ function ezin_get_users($count = 0) { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $count = intval($count); $ret_row = array(); if ($count == 0) { $query = sprintf('SELECT * FROM %s ORDER BY username', $ezin_sys['db']['tables']['users']); } else { $query = sprintf('SELECT * FROM %s ORDER BY username LIMIT %d', $ezin_sys['db']['tables']['users'], $count); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_users() [1]'); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $ret_row[] = $row; } mysqli_free_result($result); return $ret_row; } /* * ezin_get_roles() */ function ezin_get_roles($count = 0) { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $count = intval($count); $ret_row = array(); if ($count == 0) { $query = sprintf('SELECT * FROM %s ORDER BY name', $ezin_sys['db']['tables']['roles']); } else { $query = sprintf('SELECT * FROM %s ORDER BY name LIMIT %d', $ezin_sys['db']['tables']['roles'], $count); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_roles() [1]'); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $ret_row[] = $row; } mysqli_free_result($result); return $ret_row; } /* * COUNTS MANIPULATION FUNCTIONS */ /* * ezin_set_author_counts() */ function ezin_set_author_counts($author_id, $count_ar) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $author_id = intval($author_id); $key_array = array('accesses_count', 'sessions_count'); $set_str = ''; $delim = ''; foreach ($key_array as $key) { if (isset($count_ar[$key])) { $set_str .= $delim.$key.' = '.intval($count_ar[$key]); if ($delim == '') $delim = ', '; } elseif (isset($count_ar['author_'.$key])) { $set_str .= $delim.$key.' = '.intval($count_ar['author_'.$key]); if ($delim == '') $delim = ', '; } } if ($set_str == '') return true; $query = 'UPDATE '.$ezin_sys['db']['tables']['authors'] .' SET '.$set_str .' WHERE id = '.$author_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_set_author_counts()'); return true; } /* }}} */ /* * ezin_set_section_counts() */ function ezin_set_section_counts($section_id, $count_ar) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $section_id = intval($section_id); $key_array = array('accesses_count', 'sessions_count', 'raw_accesses_count', 'raw_sessions_count'); $set_str = ''; $delim = ''; foreach ($key_array as $key) { if (isset($count_ar[$key])) { $set_str .= $delim.$key.' = '.intval($count_ar[$key]); if ($delim == '') $delim = ', '; } elseif (isset($count_ar['section_'.$key])) { $set_str .= $delim.$key.' = '.intval($count_ar['section_'.$key]); if ($delim == '') $delim = ', '; } } if ($set_str == '') return true; $query = 'UPDATE '.$ezin_sys['db']['tables']['sections'] .' SET '.$set_str .' WHERE id = '.$section_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_set_section_counts()'); return true; } /* }}} */ /* * ezin_set_article_counts() */ function ezin_set_article_counts($article_id, $count_ar) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $article_id = intval($article_id); $key_array = array('accesses_count', 'sessions_count'); $set_str = ''; $delim = ''; foreach ($key_array as $key) { if (isset($count_ar[$key])) { $set_str .= $delim.$key.' = '.intval($count_ar[$key]); if ($delim == '') $delim = ', '; } elseif (isset($count_ar['article_'.$key])) { $set_str .= $delim.$key.' = '.intval($count_ar['article_'.$key]); if ($delim == '') $delim = ', '; } } if ($set_str == '') return true; $query = 'UPDATE '.$ezin_sys['db']['tables']['articles'] .' SET '.$set_str .' WHERE id = '.$article_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_set_article_counts()'); return true; } /* }}} */ /* * ezin_update_message_count() */ function ezin_update_message_count($article_id, $direction = 0, $count = -1) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $article_id = intval($article_id); $direction = intval($direction); $count = intval($count); if ($article_id <= 0) { return false; } if ($direction == 0 && $count < 0) { $count = ezin_get_message_count($article_id); } $query = 'UPDATE '.$ezin_sys['db']['tables']['articles'] .' SET message_count = ' . ($direction == 0 ? $count : 'message_count '.($direction < 0 ? '-' : '+').' '.$count) .' WHERE id = '.$article_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_message_count()'); return true; } /* }}} */ /* * ezin_get_message_count() * * If article message_count is NULL or negative value, this function is used * for setting up this field to proper value. */ function ezin_get_message_count($article_id = 0) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $query = 'SELECT count(*)' .' FROM '.$ezin_sys['db']['tables']['messages']; $article_id = intval($article_id); if ($article_id > 0) $query .= ' WHERE article_id = '.$article_id; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_message_count()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row[0] = 0; mysqli_free_result($result); return $row[0]; } /* }}} */ /* * ezin_update_counts_temp() */ function ezin_update_counts_temp() /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $query = 'SELECT ip FROM '.$ezin_sys['db']['tables']['counts_temp'] .' GROUP BY ip' .' HAVING UNIX_TIMESTAMP(SYSDATE()) - MAX(timestamp) > ' .intval($ezin_cfg['session_timeout']); $result = mysqli_query($ezin_db_handle, $query); if ($result == false) { if (mysqli_errno($ezin_db_handle) == '145') { $res2 = mysqli_query($ezin_db_handle, 'REPAIR TABLE '.$ezin_sys['db']['tables']['counts_temp']) or ezin_db_error('ezin_update_counts_temp() [1:145:R]'); // repeat last query $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts_temp() [1:145:N]'); } else { ezin_db_error('ezin_update_counts_temp() [1]'); } } $delim = ''; $ips = ''; while (($row = mysqli_fetch_array($result, MYSQLI_NUM)) != null) { $ips .= $delim.'"'.ezin_db_escape($row[0]).'"'; if ($delim == '') $delim = ','; } mysqli_free_result($result); if ($ips != '') { $query = 'DELETE FROM '.$ezin_sys['db']['tables']['counts_temp'] .' WHERE ip IN ('.$ips.')'; // ) mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts_temp() [2]'); } return true; } /* }}} */ /* * ezin_update_counts() */ function ezin_update_counts($author_id, $section_id, $article_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $author_id = intval($author_id); $section_id = intval($section_id); $article_id = intval($article_id); $remote_addr = ezin_get_remote_addr(); $remote_addr = ezin_db_escape($remote_addr); $type_ids = array(); $all_type_ids = array(); if ($author_id > 0) { $all_type_ids['U'] = '"U'.$author_id.'"'; } else { $all_type_ids['s'] = '"s'.$section_id.'"'; if ($article_id > 0) { $all_type_ids['A'] = '"A'.$article_id.'"'; } else { $all_type_ids['S'] = '"S'.$section_id.'"'; } } $query = 'SELECT type_id FROM '.$ezin_sys['db']['tables']['counts_temp'] .' WHERE ip = "'.$remote_addr.'"' .' AND type_id IN ('.join(',',$all_type_ids).')'; // ) $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [1]'); while ($row = mysqli_fetch_row($result)) { $type_ids[$row[0][0]] = '"'.$row[0].'"'; } mysqli_free_result($result); if ($article_id > 0) { $query = 'UPDATE '.$ezin_sys['db']['tables']['articles'] .' SET accesses_count = accesses_count + 1' .(! isset($type_ids['A']) ? ', sessions_count = sessions_count + 1' : '') .' WHERE id = '.$article_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [2Aa]'); $query = 'UPDATE '.$ezin_sys['db']['tables']['sections'] .' SET raw_accesses_count = raw_accesses_count + 1' .(! isset($type_ids['s']) ? ', raw_sessions_count = raw_sessions_count + 1' : '') .' WHERE id = '.$section_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [2Ab]'); } elseif ($section_id > 0) { $query = 'UPDATE '.$ezin_sys['db']['tables']['sections'] .' SET accesses_count = accesses_count + 1,' .' raw_accesses_count = raw_accesses_count + 1' .(! isset($type_ids['S']) ? ', sessions_count = sessions_count + 1' : '') .(! isset($type_ids['s']) ? ', raw_sessions_count = raw_sessions_count + 1' : '') .' WHERE id = '.$section_id; //.' WHERE '.($section_id > 0 ? 'id = "'.$section_id.'"' : 'type = "global"'); mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [2B]'); } elseif ($author_id > 0) { $query = 'UPDATE '.$ezin_sys['db']['tables']['authors'] .' SET accesses_count = accesses_count + 1' .(! isset($type_ids['U']) ? ', sessions_count = sessions_count + 1' : '') .' WHERE id = '.$author_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [2C]'); } $type_ids = array_diff($all_type_ids, $type_ids); if (count($type_ids)) { $delim = ''; $query = 'INSERT INTO '.$ezin_sys['db']['tables']['counts_temp'] .' (type_id, timestamp, ip) VALUES '; foreach ($type_ids as $val) { $query .= $delim.'('.$val.',UNIX_TIMESTAMP(SYSDATE()),"'.$remote_addr.'")'; if ($delim == '') $delim = ', '; } mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [3A]'); } else { /* If no INSERT action is taken, we will update 's'. Raw session ('s') is already in table and it is present in both cases we must do this job, to avoid unwanted session expiration (imagine situation, when someone will click still on the same article) */ $type_id = null; if (isset($all_type_ids['s'])) { $type_id = $all_type_ids['s']; } else if (isset($all_type_ids['U'])) { $type_id = $all_type_ids['U']; } if ($type_id !== null) { $query = 'UPDATE '.$ezin_sys['db']['tables']['counts_temp'] .' SET timestamp = UNIX_TIMESTAMP(SYSDATE())' .' WHERE ip = "'.$remote_addr.'"' .' AND type_id = '.$type_id; mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_update_counts() [3B]'); } } } /* }}} */ /* * Encode parts of source article */ function ezin_source_article_id_encode($arr) { return implode(':', array('source', intval($arr['section_id']), intval($arr['source_id']), $arr['hash'])); } /* * Decode source article into parts */ function ezin_source_article_id_decode($str) { $parts = explode(':', $str); return array( 'type' => $parts[0], // always 'source' 'section_id' => $parts[1], 'source_id' => $parts[2], 'hash' => $parts[3], ); } /* * Return articles from RSS url */ function ezin_get_rss_articles($source) { require_once 'soap/toArray.php'; if (!is_array($source)) { return array(); } $articles = array(); $cache_rss_xml = ezin_cache_load('Source'.intval($source['id'])); $refresh_time = ($cache_rss_xml['var_var'] == 1) ? $source['fail_refresh_time'] : $source['normal_refresh_time']; if (($cache_rss_xml['timestamp'] + ($refresh_time*60)) < Time()) { $rss_xml = ezin_get_curl($source['url'], $source['connection_timeout']); ezin_cache_clear('Source'.intval($source['id'])); if (is_bool($rss_xml) && !$rss_xml) { ezin_cache_save('Source'.intval($source['id']), $cache_rss_xml['content'], 1); } else { ezin_cache_save('Source'.intval($source['id']), $rss_xml, 0); } } else { $rss_xml = $cache_rss_xml['content']; } $rss_arr = xml2ary($rss_xml); $items = $rss_arr['rss']['_c']['channel']['_c']['item']; if (!is_array($items)) { return array(); } if (count($items) == 1) { $items = array('_c' => $items); } $section = ezin_get_section($source['section_id']); foreach($items as $item) { $article = array( 'article_id' => ezin_source_article_id_encode(array('section_id' => $source['section_id'], 'source_id' => $source['id'], 'hash' => md5($item['_c']['link']['_v']))), 'article_access_key' => $item['_c']['link']['_v'], 'article_title' => $item['_c']['title']['_v'], 'article_timestamp' => strtotime($item['_c']['pubDate']['_v']), 'article_hometext' => $item['_c']['description']['_v'], 'article_section_link' => $rss_arr['rss']['_c']['channel']['_c']['link']['_v'], 'article_section_name' => $rss_arr['rss']['_c']['channel']['_c']['title']['_v'], 'article_lang' => $section['section_lang'], 'article_section_id' => $section['section_id'], ); $articles[] = $article; } return $articles; } /* * Return source */ function ezin_get_source($source_id) { global $ezin_sys; global $ezin_db_handle; $source_id = intval($source_id); $query = sprintf('SELECT *' .' FROM %s AS sources' .' WHERE sources.id = %d', $ezin_sys['db']['tables']['sources'], $source_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_source()'); $ret = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); return $ret; } /* * Return sources for section */ function ezin_get_sources($section_id = 0) { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $section_id = intval($section_id); $query = sprintf('SELECT sources.*, sections.display' .' FROM %s AS sources' .' LEFT JOIN %s AS sections' .' ON sources.section_id = sections.id' .' WHERE sources.section_id = %d' .' AND FIND_IN_SET("articles_in_normal",sections.display)', $ezin_sys['db']['tables']['sources'], $ezin_sys['db']['tables']['sections'], $section_id); if ($section_id == 0) { // globalna sekcia $query = sprintf('SELECT sources.*, sections.display' .' FROM %s AS sources' .' LEFT JOIN %s AS sections' .' ON sources.section_id = sections.id' .' WHERE FIND_IN_SET("articles_in_global",sections.display)', $ezin_sys['db']['tables']['sources'], $ezin_sys['db']['tables']['sections'], $section_id); } else { // normalna sekcia $query = sprintf('SELECT sources.*, sections.display' .' FROM %s AS sources' .' LEFT JOIN %s AS sections' .' ON sources.section_id = sections.id' .' WHERE sources.section_id = %d' .' AND FIND_IN_SET("articles_in_normal",sections.display)', $ezin_sys['db']['tables']['sources'], $ezin_sys['db']['tables']['sections'], $section_id); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_article_list()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } /* * Boxes for actual theme */ function ezin_get_boxes($box_names = array()) { if (!is_array($box_names) && strlen($box_names) > 0) { $box_names[0] = $box_names; } if (count($box_names) <= 0) { return array(); } global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $cond = ''; foreach($box_names as $name) { if (strlen($cond) > 0) { $cond .= 'OR '; } $cond .= 'name = "'.$name.'" '; } $query = sprintf('SELECT *' .' FROM %s' .' WHERE theme = "%s"' .' AND (%s)', $ezin_sys['db']['tables']['boxes'], $ezin_cfg['theme']['name'], $cond); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_boxes()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } function ezin_get_box($box_id) { if (is_null($box_id) || strlen($box_id) <= 0) { return null; } global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $query = sprintf('SELECT *' .' FROM %s' .' WHERE id = %d' .' LIMIT 1', $ezin_sys['db']['tables']['boxes'], intval($box_id)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_box_name()'); $ret = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); return $ret; } function ezin_insert_boxes() { global $ezin_sys; global $ezin_cfg; global $ezin_user; global $ezin_db_handle; $box_prefix = 'box_'; $files = ezin_get_template_files(); $boxes = array(); foreach ($files as $file) { echo $file.'
'; $tmpl = new HTML_Template_Platon($file); $vars = $tmpl->getVars(); foreach($vars as $var) { if (substr($var, 0, strlen($box_prefix)) == $box_prefix) { $box_name = substr($var, strlen($box_prefix)); if (!in_array($box_name, $boxes)) { $boxes[] = $box_name; } } } } foreach($boxes as $box) { $query = sprintf('INSERT IGNORE INTO %s' .' (theme, name, created_id, created_by, created_dt)' .' VALUES' .' ("%s", "%s", %d, "%s", "%s")', $ezin_sys['db']['tables']['boxes'], $ezin_cfg['theme']['name'], $box, $ezin_user['id'], $ezin_user['username'], date('Y-m-d H:i:s') ); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_insert_boxes()'); } if (!is_null($result)) { mysqli_free_result($result); } return true; } /* * MESSAGES RELATED FUNCTION */ /* * ezin_get_message_list() */ function ezin_get_message_list($article_id, $parent) /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $article_id = intval($article_id); $parent = intval($parent); $query = 'SELECT' .' messages.id message_id,' .' messages.parent message_parent,' .' messages.article_id message_article_id,' .' messages.author message_author,' .' messages.email message_email,' .' messages.homepage message_homepage,' .' messages.subject message_subject,' .' messages.message message_message,' .' UNIX_TIMESTAMP(messages.datetime) message_timestamp,' .' messages.ip message_ip' .' FROM '.$ezin_sys['db']['tables']['messages'].' AS messages ' .' WHERE messages.parent = '.$parent .(isset($article_id) ? ' AND messages.article_id = '.$article_id : '') .' ORDER BY messages.datetime ' . ($ezin_cfg['order_messages_desc'] ? 'DESC' : 'ASC'); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_message_list()'); $array = array(); for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) ; unset($array[$k]); mysqli_free_result($result); return $array; } /* }}} */ /* * ezin_get_message() */ function ezin_get_message($message_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $message_id = intval($message_id); $query = 'SELECT' .' messages.id message_id,' .' messages.parent message_parent,' .' messages.article_id message_article_id,' .' messages.author message_author,' .' messages.email message_email,' .' messages.homepage message_homepage,' .' messages.subject message_subject,' .' messages.message message_message,' .' UNIX_TIMESTAMP(messages.datetime) message_timestamp,' .' messages.ip message_ip,' .' articles.id message_article_id,' .' articles.author_id message_article_author_id,' .' articles.section_id message_article_section_id,' .' articles.atitle message_article_atitle,' .' articles.title message_article_title,' .' articles.status message_article_status,' .' UNIX_TIMESTAMP(articles.datetime) message_article_timestamp,' .' articles.hometext_wordcount message_article_hometext_wordcount,' .' articles.bodytext_wordcount message_article_bodytext_wordcount,' .' articles.hometext_flags message_article_hometext_flags,' .' articles.bodytext_flags message_article_bodytext_flags,' .' articles.hometext message_article_hometext,' .' articles.bodytext message_article_bodytext,' .' authors.id article_author_id,' .' authors.name article_author_name,' .' authors.email article_author_email,' .' authors.homepage article_author_homepage,' .' authors.position article_author_position,' .' authors.location article_author_location,' //.' authors.description article_author_description,' .' sections.id article_section_id,' .' sections.name article_section_name,' .' sections.type article_section_type' //.' sections.ord article_section_ord,' //.' sections.a_limit article_section_limit' // XXX: article/section access/template keys are missing .' FROM '.$ezin_sys['db']['tables']['articles'].' AS articles, ' .$ezin_sys['db']['tables']['authors'].' AS authors, ' .$ezin_sys['db']['tables']['sections'].' AS sections, ' .$ezin_sys['db']['tables']['messages'].' AS messages' .' WHERE articles.author_id = authors.id ' .' AND articles.section_id = sections.id ' .' AND articles.id = messages.article_id ' .' AND messages.id = '.$message_id; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_message()'); if (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) == null) $row = array(); mysqli_free_result($result); return $row; } /* }}} */ /* * ezin_save_message() */ function ezin_save_message($data) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $ar = ezin_validate_IDs(array( 'section_id' => 0, 'article_id' => 0, 'message_id' => $data['parent'])); $article_id = $ar['article_id']; if (empty($article_id)) { $ar = ezin_validate_IDs(array( 'section_id' => 0, 'article_id' => $data['article_id'], 'message_id' => 0)); $article_id = $ar['article_id']; if (empty($article_id)) { return false; } $data['article_id'] = $article_id; $data['parent'] = 0; } $data['ip'] = ezin_get_remote_addr(true); $data = ezin_db_escape($data); $query = sprintf('INSERT INTO %s' .' (id,parent,article_id,author,email,homepage,subject,message,datetime,ip)' .' VALUES (0,"%s","%s","%s","%s","%s","%s","%s",SYSDATE(),"%s")', $ezin_sys['db']['tables']['messages'], $data['parent'], $data['article_id'], $data['author'], $data['email'], $data['homepage'], $data['subject'], $data['message'], $data['ip']); // $result = must be here, see return below $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_save_message()'); return $result; } /* }}} */ /* * ezin_check_message_flood() */ function ezin_check_message_flood() /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $timeout = intval($ezin_cfg['message_flood_timeout']); if ($timeout <= 0) return 0; // no flood $ip = ezin_get_remote_addr(); $query = 'SELECT count(*) ' .' FROM ' . $ezin_sys['db']['tables']['messages'] .' WHERE ip LIKE "'.ezin_db_escape($ip).'%"' .' AND UNIX_TIMESTAMP(SYSDATE()) - UNIX_TIMESTAMP(datetime)' .' <= '.$timeout; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_check_message_flood()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row[0] = 0; mysqli_free_result($result); return $row[0]; // 0 - no flooder detected, > 0 - flooders count } /* }}} */ /* * POLL RELATED FUNCTIONS */ function ezin_get_poll($section_id, $lang = '') /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $section_id = intval($section_id); if ($section_id > 0) { $query = 'SELECT' .' polls.id poll_id,' .' polls.votes poll_votes,' .' polls.answers poll_answers,' .' polls.question poll_question,' .' polls.description poll_description' .' FROM '.$ezin_sys['db']['tables']['polls'].' AS polls' .' WHERE polls.section_id = '.$section_id .' AND polls.dt_valid_from <= SYSDATE()' .' AND polls.dt_valid_to >= SYSDATE()' .' ORDER BY polls.dt_valid_from DESC LIMIT 1'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_poll() [1]'); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); if (is_array($row)) { return $row; } } $query = sprintf('SELECT id FROM %s WHERE (FIND_IN_SET("global", type2) > 0)', $ezin_sys['db']['tables']['sections']); if (strlen($lang) > 0) { $query .= sprintf(' AND lang = "%s"', ezin_db_escape($lang)); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_poll() [2]'); $ids_str = ''; while (($row = mysqli_fetch_array($result, MYSQLI_NUM)) != null) { $ids_str != '' && $ids_str .= ','; $ids_str .= $row[0]; } mysqli_free_result($result); if (strlen($ids_str)) { $query = 'SELECT' .' polls.id poll_id,' .' polls.votes poll_votes,' .' polls.answers poll_answers,' .' polls.question poll_question,' .' polls.description poll_description' .' FROM '.$ezin_sys['db']['tables']['polls'].' AS polls' .' WHERE polls.section_id IN ('.$ids_str.')' .' AND polls.dt_valid_from <= SYSDATE()' .' AND polls.dt_valid_to >= SYSDATE()' .' ORDER BY polls.dt_valid_from DESC LIMIT 1'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_poll() [3]'); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); if (is_array($row)) { return $row; } } return array( 'poll_id' => 0, 'poll_votes' => 0, 'poll_answers' => 0, 'poll_question' => '' ); } /* }}} */ function ezin_get_poll_answer_list($poll_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $poll_id = intval($poll_id); $query = 'SELECT' .' id poll_answer_id,' .' answer poll_answer,' .' answer poll_answer_answer,' .' votes poll_answer_votes' .' FROM '.$ezin_sys['db']['tables']['poll_answers'] .' WHERE poll_id = '.$poll_id; $query .= ' ORDER BY votes DESC, id ASC'; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_poll_answer_list()'); $array = array(); $total_votes = 0; for ($k = 0; ($array[$k] = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null; $k++) { $total_votes += $array[$k]['poll_answer_votes']; } unset($array[$k]); for ($k = 0; $k < count($array); $k++) { $array[$k]['poll_answer_ord'] = $k + 1; $array[$k]['poll_answer_pct'] = round( ($array[$k]['poll_answer_votes'] / max(1, $total_votes)) * 100); } mysqli_free_result($result); return $array; } /* }}} */ function ezin_get_poll_answer_list_counts($poll_id) /* {{{ */ { // TODO implement return array(); } /* }}} */ function ezin_is_valid_poll($poll_answer_id) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $poll_answer_id = intval($poll_answer_id); $query = 'SELECT answers.poll_id' .' FROM '.$ezin_sys['db']['tables']['polls'].' AS polls,' .$ezin_sys['db']['tables']['poll_answers'].' AS answers' .' WHERE polls.id = answers.poll_id' .' AND polls.dt_valid_from <= SYSDATE()' .' AND polls.dt_valid_to >= SYSDATE()' .' AND answers.id = '.$poll_answer_id; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_is_valid_poll()'); $row = mysqli_fetch_array($result, MYSQLI_NUM); mysqli_free_result($result); return is_array($row) ? $row[0] : 0; } /* }}} */ function ezin_poll_answer_vote($poll_answer_id, $poll_id = 0) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $poll_answer_id = intval($poll_answer_id); $poll_id = intval($poll_id); $query = sprintf('UPDATE %s SET votes = votes + 1 WHERE id = %d', $ezin_sys['db']['tables']['poll_answers'], $poll_answer_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_poll_answer_vote() [1]'); if ($poll_id > 0) { $query = sprintf('UPDATE %s SET votes = votes + 1 WHERE id = %d', $ezin_sys['db']['tables']['polls'], $poll_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_poll_answer_vote() [2]'); } return true; } /* }}} */ /* * FEEDBACK/NEWSLETTER RELATED FUNCTIONS */ /* * ezin_save_feedback() */ function ezin_feedback_required_items() /* {{{ */ { $config = ezin_get_config(); $items = preg_split('/\s+/', isset($config['feedback_required_items']) ? $config['feedback_required_items'] : ''); $ret = array(); foreach ($items as $item) { $item = trim($item); if (strlen($item) > 0) { $ret[] = $item; } } return $ret; } /* }}} */ function ezin_save_feedback($section_id, $data) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $ip = ezin_get_remote_addr(true); $ua = stripslashes(@$_SERVER['HTTP_USER_AGENT']); $query = sprintf('INSERT INTO %s (id,datetime,section_id,ip,user_agent)' .' VALUES (0,SYSDATE(),%d,"%s","%s")', $ezin_sys['db']['tables']['feedbacks'], intval($section_id), ezin_db_escape($ip), ezin_db_escape($ua)); mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_save_feedback() [1]'); $query = 'SELECT LAST_INSERT_ID()'; $result = mysqli_query($ezin_db_handle, $query); $feed_id = 0; if ($result != false) { $feed_id = mysqli_fetch_array($result, MYSQLI_NUM); $feed_id = intval(@$feed_id[0]); mysqli_free_result($result); } if ($feed_id <= 0) { ezin_db_error('ezin_save_feedback() [2]'); } foreach ($data as $key => $val) { $query = sprintf('INSERT INTO %s (feedback_id, field, data)' .' VALUES (%d, "%s", "%s")', $ezin_sys['db']['tables']['feedback_data'], $feed_id, ezin_db_escape($key), ezin_db_escape($val)); mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_save_feedback() [3]'."[$key]"); } return true; } /* }}} */ function ezin_save_email($section_id, $email = null, $phone = null) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $ip = ezin_get_remote_addr(true); $ua = stripslashes(@$_SERVER['HTTP_USER_AGENT']); $query = sprintf('INSERT INTO %s (email,phone,id,dt_added,section_id,ip,user_agent)' .' VALUES ("%s","%s",0,SYSDATE(),%d,"%s","%s")', $ezin_sys['db']['tables']['nl_emails'], ezin_db_escape($email), ezin_db_escape($phone), intval($section_id), ezin_db_escape($ip), ezin_db_escape($ua)); if (mysqli_query($ezin_db_handle, $query)) { return true; } return false; } /* }}} */ function ezin_get_newsletter_emails() /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $query = sprintf('SELECT *' .' FROM %s' .' WHERE dt_removed IS NULL' .' OR dt_removed > NOW()', $ezin_sys['db']['tables']['nl_emails']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_newsletter_emails() [1]'); $ret = array(); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $ret[] = $row['email']; } mysqli_free_result($result); return $ret; } /* }}} */ /* * ezin_check_feedback_flood() */ function ezin_check_feedback_flood() /* {{{ */ { global $ezin_sys; global $ezin_cfg; global $ezin_db_handle; $timeout = intval($ezin_cfg['feedback_flood_timeout']); if ($timeout <= 0) return 0; // no flood $ip = ezin_get_remote_addr(); $query = 'SELECT count(*) ' .' FROM ' . $ezin_sys['db']['tables']['feedbacks'] .' WHERE ip LIKE "'.ezin_db_escape($ip).'%"' .' AND UNIX_TIMESTAMP(SYSDATE()) - UNIX_TIMESTAMP(datetime)' .' <= '.$timeout; $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_check_feedback_flood()'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) == null) $row[0] = 0; mysqli_free_result($result); return $row[0]; // 0 - no flooder detected, > 0 - flooders count } /* }}} */ /* * CACHE RELATED FUNCTIONS */ function ezin_get_config() /* {{{ */ { global $ezin_sys; global $ezin_db_handle; // We want to retrieve config in proper encoding $encoding = ezin_cache_load_content('encoding'); if (strlen($encoding) > 0) { ezin_set_db_encoding($encoding); } // Config retrieval from cache $content = ezin_cache_load_content('config'); if (isset($content)) { $array = @unserialize($content); if (is_array($array) && count($array) > 0) { // reload dynamic config $query = sprintf('SELECT a_key, type, value FROM %s WHERE type = "1"', $ezin_sys['db']['tables']['config_data']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_config() [v1:1]'); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { $array[$row['a_key']] = ezin_eval($row['value']); } mysqli_free_result($result); return $array; } else { // for case that stored content was corrupted ezin_cache_clear('config'); } } // Config retrieval and parsing $query = sprintf('SELECT a_key, type, value FROM %s', $ezin_sys['db']['tables']['config_data']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_config() [v2:1]'); $array = array(); while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != null) { if (isset($row['type']) && $row['type'] == 1) { $array[$row['a_key']] = ezin_eval($row['value']); } else { $array[$row['a_key']] = $row['value']; } } mysqli_free_result($result); if (! is_array($array) || count($array) <= 0) { ezin_db_error('configuration not initialized'); } /* Move this to the more proper (better) place and remove it from "config". -- Nepto [2006-07-09] */ $query = sprintf('SELECT COUNT(*), SUM(id) FROM %s', $ezin_sys['db']['tables']['authors']); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_config() [v2:2]'); if (($row = mysqli_fetch_array($result, MYSQLI_NUM)) != null && $row[0] <= 1) { $array['single_author_id'] = intval($row[1]); // single author ID } else { $array['single_author_id'] = -1; } // Config and encoding storage in cache ezin_cache_save('config', serialize($array)); ezin_cache_save('encoding', $array['encoding']); return $array; } /* }}} */ function ezin_cache_save($cache_id = 0, $content = '', $var_var = 0) /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $cache_id = ezin_db_escape(trim($cache_id)); $content = ezin_db_escape($content); $var_var = intval($var_var); $query = sprintf('INSERT INTO %s (id, var_var, timestamp, content)' .' VALUES ("%s", %d, UNIX_TIMESTAMP(SYSDATE()), \'%s\')' .' ON DUPLICATE KEY UPDATE' .' var_var = VALUES(var_var),' .' timestamp = VALUES(timestamp),' .' content = VALUES(content)', $ezin_sys['db']['tables']['cache'], $cache_id, $var_var, $content); // possible error is ignored here mysqli_query($ezin_db_handle, $query); return true; } /* }}} */ function ezin_cache_load($cache_id = 'X') /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $cache_id = ezin_db_escape(trim($cache_id)); $var_var = 0; $query = sprintf('SELECT * FROM %s WHERE id = "%s"', $ezin_sys['db']['tables']['cache'], $cache_id); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_cache_load()'); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); if ($row == null) { return false; } return $row; } /* }}} */ function ezin_cache_load_content($cache_id = 'X') /* {{{ */ { $ar = ezin_cache_load($cache_id); if (isset($ar['content'])) { return $ar['content']; } return false; } /* }}} */ function ezin_cache_clear($cache_id = 'X') /* {{{ */ { global $ezin_sys; global $ezin_db_handle; $delete_all = false; $delete_gs = false; if (is_array($cache_id)) { if (count($cache_id) <= 0) { $delete_all = true; } else { $cache_id_list = array(); foreach ($cache_id as $id) { if ($id == 'Sglobal') { $delete_gs = true; continue; } $cache_id_list[] = "id LIKE '".ezin_db_escape(trim($id))."%'"; } $query = sprintf('DELETE FROM %s WHERE %s', $ezin_sys['db']['tables']['cache'], join(' OR ', $cache_id_list)); } } else { if (strlen($cache_id) <= 0) { $delete_all = true; } else if ($cache_id == 'Sglobal') { $delete_gs = true; } else { $cache_id = ezin_db_escape(trim($cache_id)); $query = sprintf('DELETE FROM %s WHERE id LIKE "%s%%"', $ezin_sys['db']['tables']['cache'], $cache_id); } } if ($delete_all) { $query = 'DELETE FROM '.$ezin_sys['db']['tables']['cache']; } else if ($delete_gs) { $query2 = sprintf('SELECT id FROM %s WHERE (FIND_IN_SET("global", type2) > 0)', $ezin_sys['db']['tables']['sections']); $result = mysqli_query($ezin_db_handle, $query2) or ezin_db_error('ezin_cache_clear() [1]'); $global_sections = array(); while (($row = mysqli_fetch_array($result, MYSQLI_NUM)) != null) { $global_sections[] = 'S'.$row[0]; } mysqli_free_result($result); $ret = count($global_sections) > 0 ? ezin_cache_clear($global_sections) : true; if ($ret === true && strlen($query) > 0) { } else { return $ret; } } mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_cache_clear() [2]'); return true; } /* }}} */ /* * ACCESS KEY REDIRECTS */ function ezin_exist_access_key_redirect($name, $type) { global $ezin_sys; global $ezin_db_handle; $query = sprintf('SELECT COUNT(*) AS count' .' FROM %s' .' WHERE old_key = "%s"' .' AND `type` = "%s"', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($name), ezin_db_escape($type)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_exist_access_key_redirect()'); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); return ($row['count'] == 1); } function ezin_get_access_key_redirect($name, $type) { global $ezin_sys; global $ezin_db_handle; $query = sprintf('SELECT *' .' FROM %s' .' WHERE `old_key` = "%s"' .' AND `type` = "%s" LIMIT 1', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($name), ezin_db_escape($type)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_get_access_key_redirect()'); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); mysqli_free_result($result); return $row; } function ezin_add_access_key_redirect($name, $type, $id) { global $ezin_sys; global $ezin_db_handle; if (strlen($name) <= 0) { return false; } if (ezin_exist_access_key_redirect($name, $type)) { $query = sprintf('UPDATE %s' .' SET id = %d' .' WHERE `old_key` = "%s"' .' AND `type` = "%s"', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($type), intval($id), ezin_db_escape($name), ezin_db_escape($type)); } else { $query = sprintf('INSERT INTO %s' .' (old_key, type, id)' .' VALUES' .' ("%s", "%s", %d)', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($name), ezin_db_escape($type), intval($id)); } $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_add_access_key_redirect()'); $affected = mysqli_affected_rows($ezin_db_handle); mysqli_free_result($result); return ($affected == 1); } function ezin_delete_access_key_redirect($name, $type) { global $ezin_sys; global $ezin_db_handle; $query = sprintf('DELETE FROM %s' .' WHERE `old_key` = "%s"' .' AND `type` = "%s"', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($name), ezin_db_escape($type)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_delete_access_key_redirect()'); $affected = mysqli_affected_rows($ezin_db_handle); return ($affected == 1); } function ezin_clear_access_key_redirect($type, $id) { global $ezin_sys; global $ezin_db_handle; $query = sprintf('DELETE FROM %s' .' WHERE `type` = "%s"' .' AND `id` = %d', $ezin_sys['db']['tables']['redirects'], ezin_db_escape($type), intval($id)); $result = mysqli_query($ezin_db_handle, $query) or ezin_db_error('ezin_clear_access_key_redirect()'); $affected = mysqli_affected_rows($ezin_db_handle); } /* Modeline for ViM {{{ * vim: set ts=4: * vim600: fdm=marker fdl=0 fdc=0: * }}} */ ?>