2009-08-03 16:42:04 +04:00
< ? php
/*
* This file is part of FEED ON FEEDS - http :// feedonfeeds . com /
*
* fof - db . php - ( nearly ) all of the DB specific code
*
2014-11-01 15:21:34 +03:00
* Copyright ( C ) 2004 - 2007 Stephen Minutillo steve @ minutillo . com http :// minutillo . com / steve /
* ( C ) 2009 - 2014 Vitaliy Filippov vitalif @ mail . ru http :// yourcmc . ru / wiki /
2009-08-03 16:42:04 +04:00
*
* Distributed under the GPL - see LICENSE
*/
$FOF_FEED_TABLE = FOF_FEED_TABLE ;
$FOF_ITEM_TABLE = FOF_ITEM_TABLE ;
$FOF_ITEM_TAG_TABLE = FOF_ITEM_TAG_TABLE ;
$FOF_SUBSCRIPTION_TABLE = FOF_SUBSCRIPTION_TABLE ;
$FOF_TAG_TABLE = FOF_TAG_TABLE ;
$FOF_USER_TABLE = FOF_USER_TABLE ;
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
function fof_db_connect ()
{
global $fof_connection ;
2015-09-16 13:47:49 +03:00
$fof_connection = mysqli_connect ( 'p:' . FOF_DB_HOST , FOF_DB_USER , FOF_DB_PASS , FOF_DB_DBNAME )
or die ( " <br><br>Cannot connect to database. Please update configuration in <b>fof-config.php</b>. MySQL says: <i> " . mysqli_connect_error () . " </i> " );
2014-12-19 15:29:57 +03:00
fof_db_query ( " SET NAMES utf8 " );
2009-08-03 16:42:04 +04:00
}
function fof_db_optimize ()
{
2010-01-25 20:30:38 +03:00
/* Может быть, проблемы из-за этого? */
/*global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE;*/
/*fof_db_query("optimize table $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_ITEM_TAG_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_TAG_TABLE, $FOF_USER_TABLE");*/
2009-08-03 16:42:04 +04:00
}
function fof_safe_query ( /* $query, [$args...]*/ )
{
2015-09-16 13:47:49 +03:00
global $fof_connection ;
$args = func_get_args ();
2009-08-03 16:42:04 +04:00
$query = array_shift ( $args );
2015-09-16 13:47:49 +03:00
if ( isset ( $args [ 0 ]) && is_array ( $args [ 0 ]))
$args = $args [ 0 ];
foreach ( $args as & $a )
$a = $fof_connection -> real_escape_string ( $a );
2009-08-03 16:42:04 +04:00
$query = vsprintf ( $query , $args );
return fof_db_query ( $query );
}
function fof_db_query ( $sql , $live = 0 )
2009-10-26 16:16:37 +03:00
{
2009-08-03 16:42:04 +04:00
global $fof_connection ;
2009-10-26 16:16:37 +03:00
list ( $usec , $sec ) = explode ( " " , microtime ());
2009-08-03 16:42:04 +04:00
$t1 = ( float ) $sec + ( float ) $usec ;
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$result = $fof_connection -> query ( $sql );
2009-10-26 16:16:37 +03:00
2014-12-10 00:28:17 +03:00
$num = $affected = 0 ;
2015-09-16 13:47:49 +03:00
if ( is_object ( $result ))
$num = $result -> num_rows ;
if ( $result )
$affected = $fof_connection -> affected_rows ;
2009-10-26 16:16:37 +03:00
list ( $usec , $sec ) = explode ( " " , microtime ());
2009-08-03 16:42:04 +04:00
$t2 = ( float ) $sec + ( float ) $usec ;
$elapsed = $t2 - $t1 ;
$logmessage = sprintf ( " %.3f: [%s] (%d / %d) " , $elapsed , $sql , $num , $affected );
fof_log ( $logmessage , " query " );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
if ( $live )
2009-08-03 16:42:04 +04:00
{
return $result ;
}
else
{
2015-09-16 13:47:49 +03:00
if ( $fof_connection -> errno )
fof_die_mysql_error ( " Cannot run query ' $sql ': " . $fof_connection -> errno . " : " . $fof_connection -> error );
2009-08-03 16:42:04 +04:00
return $result ;
}
}
2015-09-16 13:47:49 +03:00
function fof_num_rows ( $result )
{
return $result ? $result -> num_rows : 0 ;
}
function fof_insert_id ()
{
global $fof_connection ;
return $fof_connection -> insert_id ;
}
2009-10-26 16:16:37 +03:00
function fof_die_mysql_error ( $error )
{
ob_start ();
print_r ( debug_backtrace ());
$trace = ob_get_contents ();
ob_end_clean ();
$error .= " \n " . $trace ;
fof_log ( $error , 'error' );
2011-07-21 15:25:55 +04:00
$error = " SQL error. Have you run <a href= \" install.php \" ><code>install.php</code></a> to create or upgrade your installation? \n <pre> $error </pre> " ;
die ( $error );
2009-10-26 16:16:37 +03:00
}
2009-08-03 16:42:04 +04:00
function fof_db_get_row ( $result )
{
2015-09-16 13:47:49 +03:00
return $result -> fetch_assoc ();
2009-08-03 16:42:04 +04:00
}
2010-01-25 21:08:56 +03:00
function fof_db_get_value ( $sql )
{
if ( ! ( $result = fof_db_query ( $sql )) ||
! ( $row = fof_db_get_row ( $result )))
return NULL ;
2015-09-16 13:47:49 +03:00
return reset ( $row );
2010-01-25 21:08:56 +03:00
}
2009-08-03 16:42:04 +04:00
////////////////////////////////////////////////////////////////////////////////
// Feed level stuff
////////////////////////////////////////////////////////////////////////////////
function fof_db_feed_mark_cached ( $feed_id )
{
global $FOF_FEED_TABLE ;
2009-10-26 16:16:37 +03:00
$result = fof_safe_query ( " update $FOF_FEED_TABLE set feed_cache_date = %d where feed_id = %d " , time (), $feed_id );
2009-08-03 16:42:04 +04:00
}
function fof_db_feed_mark_attempted_cache ( $feed_id )
{
global $FOF_FEED_TABLE ;
2009-10-26 16:16:37 +03:00
$result = fof_safe_query ( " update $FOF_FEED_TABLE set feed_cache_attempt_date = %d where feed_id = %d " , time (), $feed_id );
2009-08-03 16:42:04 +04:00
}
function fof_db_feed_update_metadata ( $feed_id , $url , $title , $link , $description , $image , $image_cache_date )
{
global $FOF_FEED_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$sql = " update $FOF_FEED_TABLE set feed_url = '%s', feed_title = '%s', feed_link = '%s', feed_description = '%s' " ;
$args = array ( $url , $title , $link , $description );
2009-10-26 16:16:37 +03:00
if ( $image )
{
$sql .= " , feed_image = '%s' " ;
2009-08-03 16:42:04 +04:00
$args [] = $image ;
2009-10-26 16:16:37 +03:00
}
else
{
$sql .= " , feed_image = NULL " ;
}
2009-08-03 16:42:04 +04:00
$sql .= " , feed_image_cache_date = %d " ;
$args [] = $image_cache_date ;
2009-10-26 16:16:37 +03:00
$sql .= " where feed_id = %d " ;
2009-08-03 16:42:04 +04:00
$args [] = $feed_id ;
2009-10-26 16:16:37 +03:00
$result = fof_safe_query ( $sql , $args );
2009-08-03 16:42:04 +04:00
}
function fof_db_get_latest_item_age ( $user_id )
{
global $FOF_SUBSCRIPTION_TABLE , $FOF_ITEM_TABLE ;
2014-12-09 01:29:47 +03:00
$result = fof_safe_query (
" select max(i.item_cached) max_date, i.feed_id id " .
" from $FOF_ITEM_TABLE i, $FOF_SUBSCRIPTION_TABLE s " .
" where s.user_id=%d and s.feed_id=i.feed_id group by i.feed_id " , $user_id
);
2009-10-26 16:16:37 +03:00
return $result ;
2009-08-03 16:42:04 +04:00
}
function fof_db_get_subscriptions ( $user_id )
{
2014-11-24 02:34:08 +03:00
global $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE ;
2009-10-26 16:16:37 +03:00
2010-01-25 20:30:38 +03:00
return fof_safe_query ( " select * from $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE where $FOF_SUBSCRIPTION_TABLE .user_id = %d and $FOF_FEED_TABLE .feed_id = $FOF_SUBSCRIPTION_TABLE .feed_id order by feed_title " , $user_id );
2009-08-03 16:42:04 +04:00
}
function fof_db_get_feeds ()
{
2014-11-24 02:34:08 +03:00
global $FOF_FEED_TABLE ;
2009-10-26 16:16:37 +03:00
2010-01-25 20:30:38 +03:00
return fof_db_query ( " select * from $FOF_FEED_TABLE order by feed_title " );
2009-08-03 16:42:04 +04:00
}
2009-12-07 01:12:15 +03:00
function fof_db_get_item_by_id ( $item_id )
{
global $FOF_ITEM_TABLE ;
if (( $result = fof_db_query ( " select * from $FOF_ITEM_TABLE where item_id= " . intval ( $item_id ))) &&
( $row = fof_db_get_row ( $result )))
return $row ;
return NULL ;
}
2009-08-03 16:42:04 +04:00
function fof_db_get_item_count ( $user_id )
{
2014-11-24 02:34:08 +03:00
global $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE ;
2014-11-24 02:55:40 +03:00
return fof_safe_query (
" select s.feed_id as id, (select count(*) from $FOF_ITEM_TABLE i where i.feed_id=s.feed_id) " .
" as count from $FOF_SUBSCRIPTION_TABLE s where s.user_id = %d " , $user_id
);
2009-08-03 16:42:04 +04:00
}
2014-11-01 15:21:34 +03:00
function fof_db_get_tagged_item_count ( $user_id , $tag_id )
2009-08-03 16:42:04 +04:00
{
2014-11-01 16:12:13 +03:00
global $FOF_ITEM_TAG_TABLE ;
2014-11-01 15:21:34 +03:00
return fof_safe_query (
2014-11-01 16:12:13 +03:00
" select count(*) as count, feed_id as id " .
" from $FOF_ITEM_TAG_TABLE where tag_id = %d and user_id = %d group by id " ,
2014-11-01 15:21:34 +03:00
$tag_id , $user_id
);
2009-08-03 16:42:04 +04:00
}
2014-11-24 02:40:04 +03:00
function fof_db_get_subscribed_users ( $feed_id )
{
global $FOF_SUBSCRIPTION_TABLE ;
return fof_safe_query ( " select user_id from $FOF_SUBSCRIPTION_TABLE where $FOF_SUBSCRIPTION_TABLE .feed_id = %d " , $feed_id );
}
2009-08-03 16:42:04 +04:00
function fof_db_is_subscribed ( $user_id , $feed_url )
{
2014-11-24 02:34:08 +03:00
global $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select $FOF_SUBSCRIPTION_TABLE .feed_id from $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE where feed_url='%s' and $FOF_SUBSCRIPTION_TABLE .feed_id = $FOF_FEED_TABLE .feed_id and $FOF_SUBSCRIPTION_TABLE .user_id = %d " , $feed_url , $user_id );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
if ( fof_num_rows ( $result ) == 0 )
2009-08-03 16:42:04 +04:00
return false ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
return true ;
}
function fof_db_get_feed_by_url ( $feed_url )
{
2014-11-24 02:34:08 +03:00
global $FOF_FEED_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select * from $FOF_FEED_TABLE where feed_url='%s' " , $feed_url );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return fof_db_get_row ( $result );
2009-08-03 16:42:04 +04:00
}
2010-01-25 20:30:38 +03:00
function fof_db_get_feed_by_id ( $feed_id , $user_id = false )
2009-08-03 16:42:04 +04:00
{
2010-01-25 20:30:38 +03:00
global $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE ;
$j = " " ;
if ( $user_id )
$j = " JOIN $FOF_SUBSCRIPTION_TABLE s ON s.user_id= " . intval ( $user_id ) . " AND s.feed_id=s.feed_id " ;
$result = fof_safe_query ( " select * from $FOF_FEED_TABLE f $j where f.feed_id=%d " , $feed_id );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return fof_db_get_row ( $result );
2009-08-03 16:42:04 +04:00
}
function fof_db_add_feed ( $url , $title , $link , $description )
{
global $FOF_FEED_TABLE , $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE , $fof_connection ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " insert into $FOF_FEED_TABLE (feed_url,feed_title,feed_link,feed_description) values ('%s', '%s', '%s', '%s') " , $url , $title , $link , $description );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return fof_insert_id ();
2009-08-03 16:42:04 +04:00
}
function fof_db_add_subscription ( $user_id , $feed_id )
{
global $FOF_SUBSCRIPTION_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " insert into $FOF_SUBSCRIPTION_TABLE (feed_id, user_id) values (%d, %d) " , $feed_id , $user_id );
}
function fof_db_delete_subscription ( $user_id , $feed_id )
{
2013-11-28 16:41:30 +04:00
global $FOF_SUBSCRIPTION_TABLE , $FOF_ITEM_TAG_TABLE , $FOF_ITEM_TABLE ;
2009-10-26 16:16:37 +03:00
2013-11-28 16:41:30 +04:00
fof_safe_query (
" delete from s, it using $FOF_ITEM_TAG_TABLE it, $FOF_SUBSCRIPTION_TABLE s, $FOF_ITEM_TABLE i " .
" where i.item_id=it.item_id and s.feed_id=%d and i.feed_id=s.feed_id and it.user_id=%d and s.user_id=it.user_id " ,
$feed_id , $user_id
);
2009-08-03 16:42:04 +04:00
}
function fof_db_delete_feed ( $feed_id )
{
global $FOF_FEED_TABLE , $FOF_ITEM_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " delete from $FOF_ITEM_TABLE where feed_id = %d " , $feed_id );
2009-08-22 01:36:26 +04:00
fof_safe_query ( " delete from $FOF_FEED_TABLE where feed_id = %d " , $feed_id );
2009-08-03 16:42:04 +04:00
}
////////////////////////////////////////////////////////////////////////////////
// Item level stuff
////////////////////////////////////////////////////////////////////////////////
function fof_db_find_item ( $feed_id , $item_guid )
{
global $FOF_FEED_TABLE , $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE , $fof_connection ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select item_id from $FOF_ITEM_TABLE where feed_id=%d and item_guid='%s' " , $feed_id , $item_guid );
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return $row ? $row [ 'item_id' ] : NULL ;
2009-08-03 16:42:04 +04:00
}
2010-02-15 22:06:58 +03:00
$fof_db_item_fields = array ( 'item_guid' , 'item_link' , 'item_title' , 'item_author' , 'item_content' , 'item_cached' , 'item_published' , 'item_updated' );
function fof_db_add_item ( $feed_id , $item )
2009-08-03 16:42:04 +04:00
{
2010-02-15 22:06:58 +03:00
global $FOF_FEED_TABLE , $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE , $fof_connection , $fof_db_item_fields ;
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$args = array ();
2010-02-15 22:06:58 +03:00
$sql = " insert into $FOF_ITEM_TABLE (feed_id, " . implode ( " , " , $fof_db_item_fields ) . " ) values ( " . intval ( $feed_id );
foreach ( $fof_db_item_fields as $k )
2015-09-16 13:47:49 +03:00
{
$sql .= " , '%s' " ;
$args [] = $item [ $k ];
}
2010-02-15 22:06:58 +03:00
$sql .= " ) " ;
2015-09-16 13:47:49 +03:00
fof_safe_query ( $sql , $args );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return fof_insert_id ();
2009-08-03 16:42:04 +04:00
}
2011-07-18 01:45:18 +04:00
function fof_db_get_items ( $user_id = 1 , $feed = NULL , $what = " unread " ,
$when = NULL , $start = NULL , $limit = NULL , $order = " desc " , $search = NULL )
2009-08-03 16:42:04 +04:00
{
global $FOF_SUBSCRIPTION_TABLE , $FOF_FEED_TABLE , $FOF_ITEM_TABLE , $FOF_ITEM_TAG_TABLE , $FOF_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2011-02-16 00:13:04 +03:00
$user_id = intval ( $user_id );
2009-08-03 16:42:04 +04:00
$prefs = fof_prefs ();
$offset = $prefs [ 'tzoffset' ];
2014-12-09 01:29:47 +03:00
if ( ! empty ( $prefs [ 'dst' ]))
2011-07-21 15:25:55 +04:00
$offset += date ( 'I' );
2009-10-26 16:16:37 +03:00
2011-07-18 01:45:18 +04:00
if ( ! is_null ( $when ) && $when != " " )
2009-08-03 16:42:04 +04:00
{
if ( $when == " today " )
$whendate = fof_todays_date ();
else
$whendate = $when ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$whendate = explode ( " / " , $whendate );
$begin = gmmktime ( 0 , 0 , 0 , $whendate [ 1 ], $whendate [ 2 ], $whendate [ 0 ]) - ( $offset * 60 * 60 );
$end = $begin + ( 24 * 60 * 60 );
}
2009-10-26 16:16:37 +03:00
2014-12-09 18:33:36 +03:00
$limit_clause = '' ;
2011-07-18 01:45:18 +04:00
if ( is_numeric ( $start ))
2009-08-03 16:42:04 +04:00
{
2011-07-18 01:45:18 +04:00
if ( ! is_numeric ( $limit ))
2009-08-03 16:42:04 +04:00
$limit = $prefs [ " howmany " ];
$limit_clause = " limit $start , $limit " ;
}
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$args = array ();
2012-11-29 02:16:43 +04:00
$select = " SELECT STRAIGHT_JOIN i.* , f.*, s.subscription_prefs " ;
2011-07-18 01:45:18 +04:00
$from = " $FOF_ITEM_TABLE i, $FOF_FEED_TABLE f, $FOF_SUBSCRIPTION_TABLE s " ;
2014-11-01 15:21:34 +03:00
$where = " WHERE s.user_id= $user_id AND s.feed_id=f.feed_id AND f.feed_id=i.feed_id " ;
2009-10-26 16:16:37 +03:00
2011-07-18 01:45:18 +04:00
if ( ! is_null ( $feed ) && $feed != " " )
2014-11-01 16:12:13 +03:00
$where .= sprintf ( " AND f.feed_id = %d " , $feed );
2009-10-26 16:16:37 +03:00
2014-11-01 15:21:34 +03:00
$pubdate = 'i.item_published' ;
2009-10-26 16:16:37 +03:00
2011-07-18 01:45:18 +04:00
if ( $what != " all " )
2009-08-03 16:42:04 +04:00
{
2014-11-01 16:12:13 +03:00
$alltags = array ();
foreach ( fof_db_get_tags ( $user_id ) as $t ) // returned from cache
{
$alltags [ mb_strtolower ( $t [ 'tag_name' ])] = $t ;
}
$min_tag = false ;
$min_i = false ;
2009-10-26 16:16:15 +03:00
$tags = preg_split ( " /[ \ s,]*,[ \ s,]*/ " , $what );
2011-02-16 00:13:04 +03:00
foreach ( $tags as $i => $tag )
{
2014-11-01 16:12:13 +03:00
$tag = mb_strtolower ( trim ( $tag ));
if ( isset ( $alltags [ $tag ]))
{
$from = " $FOF_ITEM_TAG_TABLE it $i , $from " ;
// feed_id condition is redundant, used to speedup match by feed+tag(s)
$where .= " AND it $i .feed_id=i.feed_id AND it $i .user_id= $user_id " .
" AND it $i .item_id=i.item_id AND it $i .tag_id=%d " ;
$args [] = $alltags [ $tag ][ 'tag_id' ];
if ( ! $min_tag || $min_tag [ 'count' ] > $alltags [ $tag ][ 'count' ])
{
// For several tags it's better to sort using the most rare one
$min_tag = $alltags [ $tag ];
$min_i = $i ;
}
}
2011-02-16 00:13:04 +03:00
}
2014-11-01 16:12:13 +03:00
if ( $min_i !== false )
2014-11-01 15:21:34 +03:00
{
2014-11-01 16:12:13 +03:00
$pubdate = " it $min_i .item_published " ;
2014-11-01 15:21:34 +03:00
}
2009-08-03 16:42:04 +04:00
}
2009-10-26 16:16:37 +03:00
2014-11-01 15:21:34 +03:00
if ( ! is_null ( $when ) && $when != " " )
$where .= sprintf ( " AND $pubdate > %d AND $pubdate < %d " , $begin , $end );
2011-07-18 01:45:18 +04:00
if ( ! is_null ( $search ) && $search != " " )
2009-08-03 16:42:04 +04:00
{
2014-11-01 15:21:34 +03:00
$where .= " AND (i.item_title like '%%%s%%' OR i.item_content like '%%%s%%') " ;
2009-08-03 16:42:04 +04:00
$args [] = $search ;
$args [] = $search ;
}
2009-10-26 16:16:37 +03:00
2014-11-01 15:21:34 +03:00
$order_by = " order by $pubdate desc $limit_clause " ;
2009-10-26 16:16:37 +03:00
2014-12-09 01:29:47 +03:00
$query = " $select FROM $from $where $order_by " ;
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( $query , $args );
2015-09-16 13:47:49 +03:00
if ( fof_num_rows ( $result ) == 0 )
2009-08-03 16:42:04 +04:00
return array ();
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
2009-08-03 16:42:04 +04:00
{
2010-01-25 20:30:38 +03:00
$row [ 'prefs' ] = unserialize ( $row [ 'subscription_prefs' ]);
2009-08-03 16:42:04 +04:00
$array [] = $row ;
}
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$array = fof_multi_sort ( $array , 'item_published' , $order != " asc " );
2009-10-26 16:16:37 +03:00
2011-07-18 01:45:18 +04:00
foreach ( $array as $i => $item )
2009-08-03 16:42:04 +04:00
{
$ids [] = $item [ 'item_id' ];
$lookup [ $item [ 'item_id' ]] = $i ;
$array [ $i ][ 'tags' ] = array ();
}
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$items = join ( $ids , " , " );
2009-10-26 16:16:37 +03:00
2014-12-09 18:33:36 +03:00
$result = fof_safe_query (
" select t.tag_name, it.item_id from $FOF_TAG_TABLE t, $FOF_ITEM_TAG_TABLE it " .
" where t.tag_id = it.tag_id and it.item_id in (%s) and it.user_id = %d " , $items , $user_id
);
2011-07-18 01:45:18 +04:00
while ( $row = fof_db_get_row ( $result ))
2009-08-03 16:42:04 +04:00
{
$item_id = $row [ 'item_id' ];
$tag = $row [ 'tag_name' ];
$array [ $lookup [ $item_id ]][ 'tags' ][] = $tag ;
}
return $array ;
}
function fof_db_get_item ( $user_id , $item_id )
{
2014-12-09 16:07:00 +03:00
global $FOF_FEED_TABLE , $FOF_ITEM_TABLE , $FOF_ITEM_TAG_TABLE , $FOF_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2014-12-09 16:07:00 +03:00
$query = " select f.feed_image, f.feed_title, f.feed_link, f.feed_description, i.* " .
" from $FOF_FEED_TABLE f, $FOF_ITEM_TABLE i where i.feed_id=f.feed_id and i.item_id = %d " ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( $query , $item_id );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$item = fof_db_get_row ( $result );
2009-08-03 16:42:04 +04:00
$item [ 'tags' ] = array ();
2014-12-09 16:07:00 +03:00
if ( $user_id )
2009-10-26 16:16:37 +03:00
{
2014-12-09 16:07:00 +03:00
$result = fof_safe_query (
" select t.tag_name from $FOF_TAG_TABLE t, $FOF_ITEM_TAG_TABLE it " .
" where t.tag_id = it.tag_id and it.item_id = %d and it.user_id = %d " , $item_id , $user_id
);
while ( $row = fof_db_get_row ( $result ))
{
2009-10-26 16:16:37 +03:00
$item [ 'tags' ][] = $row [ 'tag_name' ];
2014-12-09 16:07:00 +03:00
}
2009-10-26 16:16:37 +03:00
}
2009-08-03 16:42:04 +04:00
return $item ;
}
////////////////////////////////////////////////////////////////////////////////
// Tag stuff
////////////////////////////////////////////////////////////////////////////////
function fof_db_get_subscription_to_tags ()
{
2009-10-26 16:16:15 +03:00
$r = array ( 'filter' => array ());
2009-08-03 16:42:04 +04:00
global $FOF_SUBSCRIPTION_TABLE ;
$result = fof_safe_query ( " select * from $FOF_SUBSCRIPTION_TABLE " );
while ( $row = fof_db_get_row ( $result ))
{
$prefs = unserialize ( $row [ 'subscription_prefs' ]);
$tags = $prefs [ 'tags' ];
2014-12-10 13:28:29 +03:00
if ( ! isset ( $r [ $row [ 'feed_id' ]]))
2009-10-26 16:16:15 +03:00
$r [ $row [ 'feed_id' ]] = array ();
2009-08-03 16:42:04 +04:00
$r [ $row [ 'feed_id' ]][ $row [ 'user_id' ]] = $tags ;
2014-12-10 13:28:29 +03:00
if ( ! empty ( $prefs [ 'filter' ]))
2009-10-26 16:16:15 +03:00
{
2014-12-10 13:28:29 +03:00
if ( ! isset ( $r [ 'filter' ][ $row [ 'feed_id' ]]))
2009-10-26 16:16:15 +03:00
$r [ 'filter' ][ $row [ 'feed_id' ]] = array ();
$r [ 'filter' ][ $row [ 'feed_id' ]][ $row [ 'user_id' ]] = $prefs [ 'filter' ];
}
2009-08-03 16:42:04 +04:00
}
2009-10-26 16:16:37 +03:00
return $r ;
2009-08-03 16:42:04 +04:00
}
2009-10-26 16:16:15 +03:00
function fof_db_get_subscription_prefs ( $user_id , $feed_id )
2009-08-03 16:42:04 +04:00
{
global $FOF_SUBSCRIPTION_TABLE ;
$result = fof_safe_query ( " select subscription_prefs from $FOF_SUBSCRIPTION_TABLE where feed_id = %d and user_id = %d " , $feed_id , $user_id );
$row = fof_db_get_row ( $result );
$prefs = unserialize ( $row [ 'subscription_prefs' ]);
2009-10-26 16:16:15 +03:00
return $prefs ;
}
2009-10-26 16:16:37 +03:00
2009-10-26 16:16:15 +03:00
function fof_db_set_subscription_prefs ( $user_id , $feed_id , $prefs )
{
global $FOF_SUBSCRIPTION_TABLE ;
return fof_safe_query ( " update $FOF_SUBSCRIPTION_TABLE set subscription_prefs = '%s' where feed_id = %d and user_id = %d " , serialize ( $prefs ), $feed_id , $user_id );
}
2014-11-01 15:21:34 +03:00
// tag feed and all its items
2009-10-26 16:16:15 +03:00
function fof_db_tag_feed ( $user_id , $feed_id , $tag_id )
{
$prefs = fof_db_get_subscription_prefs ( $user_id , $feed_id );
2014-11-01 15:21:34 +03:00
if ( ! is_array ( $prefs [ 'tags' ]) || ! in_array ( $tag_id , $prefs [ 'tags' ]))
$prefs [ 'tags' ][] = $tag_id ;
2009-10-26 16:16:15 +03:00
fof_db_set_subscription_prefs ( $user_id , $feed_id , $prefs );
2015-02-27 22:17:16 +03:00
fof_db_tag_feed_items ( $user_id , $feed_id , $tag_id );
}
function fof_db_tag_feed_items ( $user_id , $feed_id , $tag_id , $since = 0 )
{
global $FOF_ITEM_TAG_TABLE , $FOF_ITEM_TABLE ;
2014-11-01 15:21:34 +03:00
fof_safe_query (
2014-11-01 16:12:13 +03:00
" insert into $FOF_ITEM_TAG_TABLE (tag_id, user_id, item_id, item_published, feed_id) " .
" select %d, %d, item_id, item_published, feed_id from $FOF_ITEM_TABLE where feed_id=%d " .
2015-02-27 22:17:16 +03:00
( $since ? " and item_published >= " . $since : '' ) .
2014-11-01 15:21:34 +03:00
" on duplicate key update item_published=values(item_published) " ,
$tag_id , $user_id , $feed_id
);
2009-10-26 16:16:15 +03:00
}
2009-10-26 16:16:37 +03:00
2010-01-25 20:30:38 +03:00
function fof_db_set_feedprop ( $user_id , $feed_id , $prop , $value )
2009-10-26 16:16:15 +03:00
{
$chg = false ;
$prefs = fof_db_get_subscription_prefs ( $user_id , $feed_id );
2014-12-16 00:36:41 +03:00
if ( empty ( $prefs [ $prop ]) ? $value : ( $prefs [ $prop ] != $value ))
2009-10-26 16:16:15 +03:00
{
2010-01-25 20:30:38 +03:00
$prefs [ $prop ] = $value ;
2009-10-26 16:16:15 +03:00
$chg = true ;
}
fof_db_set_subscription_prefs ( $user_id , $feed_id , $prefs );
return $chg ;
2009-08-03 16:42:04 +04:00
}
2014-11-01 15:21:34 +03:00
// untag feed and all its items
2009-08-03 16:42:04 +04:00
function fof_db_untag_feed ( $user_id , $feed_id , $tag_id )
{
2014-11-01 15:21:34 +03:00
global $FOF_ITEM_TAG_TABLE , $FOF_ITEM_TABLE ;
2009-10-26 16:16:15 +03:00
$prefs = fof_db_get_subscription_prefs ( $user_id , $feed_id );
2009-08-03 16:42:04 +04:00
if ( is_array ( $prefs [ 'tags' ]))
$prefs [ 'tags' ] = array_diff ( $prefs [ 'tags' ], array ( $tag_id ));
2009-10-26 16:16:15 +03:00
fof_db_set_subscription_prefs ( $user_id , $feed_id , $prefs );
2014-11-01 15:21:34 +03:00
fof_safe_query (
" delete from it using $FOF_ITEM_TAG_TABLE it, $FOF_ITEM_TABLE i " .
2014-12-16 00:36:41 +03:00
" where it.item_id=i.item_id and it.user_id=%d and it.tag_id=%d and i.feed_id=%d " ,
2014-11-01 15:21:34 +03:00
$user_id , $tag_id , $feed_id
);
2009-08-03 16:42:04 +04:00
}
function fof_db_get_item_tags ( $user_id , $item_id )
{
2014-11-24 02:34:08 +03:00
global $FOF_TAG_TABLE , $FOF_ITEM_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select $FOF_TAG_TABLE .tag_name from $FOF_TAG_TABLE , $FOF_ITEM_TAG_TABLE where $FOF_TAG_TABLE .tag_id = $FOF_ITEM_TAG_TABLE .tag_id and $FOF_ITEM_TAG_TABLE .item_id = %d and $FOF_ITEM_TAG_TABLE .user_id = %d " , $item_id , $user_id );
2009-10-26 16:16:37 +03:00
return $result ;
2009-08-03 16:42:04 +04:00
}
function fof_db_item_has_tags ( $item_id )
{
2014-11-24 02:34:08 +03:00
global $FOF_ITEM_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2014-11-24 02:34:08 +03:00
$result = fof_safe_query ( " select count(*) as \" count \" from $FOF_ITEM_TAG_TABLE where item_id=%d and tag_id > 2 " , $item_id );
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
return $row [ " count " ];
}
function fof_db_get_unread_count ( $user_id )
{
global $FOF_ITEM_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
$result = fof_safe_query ( " select count(*) as \" count \" from $FOF_ITEM_TAG_TABLE where tag_id = 1 and user_id = %d " , $user_id );
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
return $row [ " count " ];
}
function fof_db_get_tag_unread ( $user_id )
{
2014-11-01 15:21:34 +03:00
global $FOF_TAG_TABLE , $FOF_ITEM_TAG_TABLE ;
2009-08-03 16:42:04 +04:00
2014-11-01 16:12:13 +03:00
$result = fof_safe_query (
" select straight_join count(*) as count, it2.tag_id from $FOF_ITEM_TAG_TABLE it, $FOF_ITEM_TAG_TABLE it2 use index (item_id_user_id_tag_id) " .
" where it.item_id = it2.item_id and it.tag_id = 1 and it.user_id = %d and it2.user_id = %d group by it2.tag_id " , $user_id , $user_id
);
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$counts = array ();
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
2009-08-03 16:42:04 +04:00
$counts [ $row [ 'tag_id' ]] = $row [ 'count' ];
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
return $counts ;
}
function fof_db_get_tags ( $user_id )
{
global $FOF_TAG_TABLE , $FOF_ITEM_TABLE , $FOF_ITEM_TAG_TABLE , $fof_connection ;
2009-10-26 16:16:37 +03:00
2014-11-01 16:12:13 +03:00
static $cache = array ();
if ( isset ( $cache [ $user_id ]))
return $cache [ $user_id ];
$sql = " SELECT $FOF_TAG_TABLE .tag_id, $FOF_TAG_TABLE .tag_name, count( $FOF_ITEM_TAG_TABLE .item_id) as count
2009-08-03 16:42:04 +04:00
FROM $FOF_TAG_TABLE
LEFT JOIN $FOF_ITEM_TAG_TABLE ON $FOF_TAG_TABLE . tag_id = $FOF_ITEM_TAG_TABLE . tag_id
WHERE $FOF_ITEM_TAG_TABLE . user_id = % d
GROUP BY $FOF_TAG_TABLE . tag_id order by $FOF_TAG_TABLE . tag_name " ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( $sql , $user_id );
2014-11-01 16:12:13 +03:00
$tags = array ();
while ( $row = fof_db_get_row ( $result ))
$tags [ $row [ 'tag_id' ]] = $row ;
2009-10-26 16:16:37 +03:00
2014-11-01 16:12:13 +03:00
$cache [ $user_id ] = $tags ;
return $tags ;
2009-08-03 16:42:04 +04:00
}
function fof_db_get_tag_id_map ()
{
global $FOF_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$result = fof_safe_query ( " select * from $FOF_TAG_TABLE " );
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$tags = array ();
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
2009-08-03 16:42:04 +04:00
$tags [ $row [ 'tag_id' ]] = $row [ 'tag_name' ];
2009-10-26 16:16:37 +03:00
return $tags ;
2009-08-03 16:42:04 +04:00
}
function fof_db_create_tag ( $user_id , $tag )
{
global $FOF_TAG_TABLE , $fof_connection ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " insert into $FOF_TAG_TABLE (tag_name) values ('%s') " , $tag );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return fof_insert_id ();
2009-08-03 16:42:04 +04:00
}
function fof_db_get_tag_by_name ( $user_id , $tag )
{
2014-11-24 02:34:08 +03:00
global $FOF_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select $FOF_TAG_TABLE .tag_id from $FOF_TAG_TABLE where $FOF_TAG_TABLE .tag_name = '%s' " , $tag );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
return $row ? $row [ 'tag_id' ] : NULL ;
2009-08-03 16:42:04 +04:00
}
function fof_db_mark_unread ( $user_id , $items )
{
fof_db_tag_items ( $user_id , 1 , $items );
}
function fof_db_mark_read ( $user_id , $items )
{
fof_db_untag_items ( $user_id , 1 , $items );
}
2011-07-21 11:56:59 +04:00
function fof_db_delete_items ( $user_id , $items , $check_private )
{
global $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE ;
if ( ! $items )
return ;
if ( $check_private )
{
// Check if items correspond to $user_id's private feed
$result = fof_db_query (
" SELECT i.item_id FROM $FOF_ITEM_TABLE i " .
" LEFT JOIN $FOF_SUBSCRIPTION_TABLE s ON s.feed_id=i.feed_id AND s.user_id != $user_id " .
" WHERE s.feed_id IS NULL AND i.item_id IN ( " . implode ( ',' , array_map ( 'intval' , $items )) . " ) "
);
$items = array ();
foreach ( $result as $r )
$items [] = $r [ 'item_id' ];
}
if ( ! $items )
return ;
$items = implode ( ',' , array_map ( 'intval' , $items ));
fof_db_query ( " DELETE FROM $FOF_ITEM_TABLE WHERE item_id IN ( $items ) " );
}
2009-08-03 16:42:04 +04:00
function fof_db_mark_feed_read ( $user_id , $feed_id )
{
2014-11-24 02:34:08 +03:00
fof_db_untag_feed ( $user_id , $feed_id , 1 );
2009-08-03 16:42:04 +04:00
}
2012-02-21 15:11:35 +04:00
function fof_db_mark_feed_unread ( $user_id , $feed_id , $what )
2009-08-03 16:42:04 +04:00
{
2015-02-27 22:17:16 +03:00
fof_db_tag_feed_items ( $user_id , $feed_id , 1 , $what == 'today' ? mktime ( 0 , 0 , 0 , date ( 'n' ), date ( 'j' ), date ( 'Y' )) : 0 );
2009-08-03 16:42:04 +04:00
}
2014-11-24 02:34:08 +03:00
function fof_db_mark_item_unread ( $id , $except_users = array ())
2009-08-03 16:42:04 +04:00
{
2014-11-24 02:34:08 +03:00
global $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE , $FOF_ITEM_TAG_TABLE ;
fof_safe_query (
" insert into $FOF_ITEM_TAG_TABLE (user_id, tag_id, item_id, feed_id, item_published) " .
" select s.user_id, 1, i.item_id, i.feed_id, i.item_published from $FOF_ITEM_TABLE i, $FOF_SUBSCRIPTION_TABLE s " .
" where i.item_id=%d and i.feed_id=s.feed_id " .
( $except_users ? " and s.user_id not in ( " . implode ( ',' , array_map ( 'intval' , $except_users )) . " ) " : '' ) .
" on duplicate key update feed_id=values(feed_id), item_published=values(item_published) " , $id
);
2009-08-03 16:42:04 +04:00
}
function fof_db_tag_items ( $user_id , $tag_id , $items )
{
2014-11-01 15:21:34 +03:00
global $FOF_ITEM_TAG_TABLE , $FOF_ITEM_TABLE ;
2011-07-21 11:56:59 +04:00
if ( ! $items )
return ;
2009-08-03 16:42:04 +04:00
2014-11-01 15:21:34 +03:00
$items = implode ( ',' , ( array ) $items );
2014-11-01 16:12:13 +03:00
$sql = " insert into $FOF_ITEM_TAG_TABLE (user_id, tag_id, item_id, item_published, feed_id) " .
2014-11-01 16:47:58 +03:00
" select " . intval ( $user_id ) . " , " . intval ( $tag_id ) . " , item_id, item_published, feed_id " .
" from $FOF_ITEM_TABLE where item_id in ( $items ) " .
2014-11-24 02:34:08 +03:00
" on duplicate key update feed_id=values(feed_id), item_published=values(item_published) " ;
2009-10-26 16:16:37 +03:00
2014-11-24 02:34:08 +03:00
fof_db_query ( $sql , 1 );
2009-08-03 16:42:04 +04:00
}
function fof_db_untag_items ( $user_id , $tag_id , $items )
{
global $FOF_ITEM_TAG_TABLE ;
2009-10-26 16:16:37 +03:00
2011-07-21 11:56:59 +04:00
if ( ! $items )
return ;
if ( ! is_array ( $items ))
$items = array ( $items );
2009-10-26 16:16:37 +03:00
2011-07-21 11:56:59 +04:00
$sql = " item_id IN ( " . implode ( " , " , array_map ( 'intval' , $items )) . " ) " ;
$sql = " delete from $FOF_ITEM_TAG_TABLE where user_id = %d and tag_id = %d and $sql " ;
2009-10-26 16:16:37 +03:00
2011-07-21 15:29:15 +04:00
fof_safe_query ( $sql , array ( $user_id , $tag_id ));
2009-08-03 16:42:04 +04:00
}
////////////////////////////////////////////////////////////////////////////////
// User stuff
////////////////////////////////////////////////////////////////////////////////
function fof_db_get_users ()
{
global $FOF_USER_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select user_name, user_id, user_prefs from $FOF_USER_TABLE " );
2009-10-26 16:16:37 +03:00
2014-12-10 13:28:29 +03:00
$users = array ();
while ( $row = fof_db_get_row ( $result ))
2009-08-03 16:42:04 +04:00
{
2014-12-10 13:28:29 +03:00
$users [ $row [ 'user_id' ]][ 'user_name' ] = $row [ 'user_name' ];
$users [ $row [ 'user_id' ]][ 'user_prefs' ] = unserialize ( $row [ 'user_prefs' ]);
2009-08-03 16:42:04 +04:00
}
}
function fof_db_add_user ( $username , $password )
{
global $FOF_USER_TABLE ;
2009-10-26 16:16:37 +03:00
$password_hash = md5 ( $password . $username );
fof_safe_query ( " insert into $FOF_USER_TABLE (user_name, user_password_hash) values ('%s', '%s') " , $username , $password_hash );
2009-08-03 16:42:04 +04:00
}
function fof_db_change_password ( $username , $password )
{
global $FOF_USER_TABLE ;
2009-10-26 16:16:37 +03:00
$password_hash = md5 ( $password . $username );
fof_safe_query ( " update $FOF_USER_TABLE set user_password_hash = '%s' where user_name = '%s' " , $password_hash , $username );
2009-08-03 16:42:04 +04:00
}
2011-07-21 15:25:55 +04:00
function fof_db_get_user ( $username , $userid = NULL )
2009-08-03 16:42:04 +04:00
{
global $FOF_USER_TABLE ;
2011-07-21 15:25:55 +04:00
if ( $username === NULL )
$result = fof_safe_query ( " select * from $FOF_USER_TABLE where user_id = %d " , $userid );
else
$result = fof_safe_query ( " select * from $FOF_USER_TABLE where user_name = '%s' " , $username );
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2011-07-21 15:25:55 +04:00
return $row ;
}
2009-10-26 16:16:37 +03:00
2011-07-21 15:25:55 +04:00
function fof_update_user ( $user )
{
global $FOF_USER_TABLE ;
if ( ! $user || ! $user [ 'user_id' ])
return ;
$values = array ();
$query = " " ;
foreach ( array ( 'user_name' , 'user_password_hash' , 'user_level' , 'user_prefs' ) as $k )
{
$query .= ( $query ? " , " : " " ) . " ` $k `='%s' " ;
$values [] = $user [ $k ];
}
$query = " UPDATE $FOF_USER_TABLE SET $query WHERE user_id='%s' " ;
$values [] = $user [ 'user_id' ];
array_unshift ( $values , $query );
call_user_func_array ( 'fof_safe_query' , $values );
2009-08-03 16:42:04 +04:00
}
function fof_db_delete_user ( $username )
{
global $FOF_USER_TABLE , $FOF_ITEM_TAG_TABLE , $FOF_SUBSCRIPTION_TABLE ;
2012-03-03 00:31:25 +04:00
$user = fof_db_get_user ( $username );
$user_id = $user [ 'user_id' ];
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " delete from $FOF_SUBSCRIPTION_TABLE where user_id = %d " , $user_id );
fof_safe_query ( " delete from $FOF_ITEM_TAG_TABLE where user_id = %d " , $user_id );
fof_safe_query ( " delete from $FOF_USER_TABLE where user_id = %d " , $user_id );
}
function fof_db_save_prefs ( $user_id , $prefs )
{
global $FOF_USER_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$prefs = serialize ( $prefs );
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
fof_safe_query ( " update $FOF_USER_TABLE set user_prefs = '%s' where user_id = %d " , $prefs , $user_id );
}
2011-07-21 15:25:55 +04:00
function fof_set_current_user ( $row )
{
global $fof_user_id , $fof_user_name , $fof_user_level ;
$fof_user_name = $row [ 'user_name' ];
$fof_user_id = $row [ 'user_id' ];
$fof_user_level = $row [ 'user_level' ];
return true ;
}
2009-08-03 16:42:04 +04:00
function fof_db_authenticate ( $user_name , $user_password_hash )
{
2014-11-24 02:34:08 +03:00
global $FOF_USER_TABLE ;
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
$result = fof_safe_query ( " select * from $FOF_USER_TABLE where user_name = '%s' and user_password_hash = '%s' " , $user_name , $user_password_hash );
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
if ( fof_num_rows ( $result ) == 0 )
2009-08-03 16:42:04 +04:00
return false ;
2009-10-26 16:16:37 +03:00
2015-09-16 13:47:49 +03:00
$row = fof_db_get_row ( $result );
2009-10-26 16:16:37 +03:00
2011-07-21 15:25:55 +04:00
fof_set_current_user ( $row );
2009-10-26 16:16:37 +03:00
2009-08-03 16:42:04 +04:00
return true ;
}
2009-12-07 01:12:15 +03:00
function fof_db_get_is_duplicate_item ( $item_id , $item_guid , $content_md5 )
{
global $FOF_ITEM_TABLE , $FOF_USER_TABLE , $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE ;
$result = fof_safe_query ( " select * from $FOF_ITEM_TABLE where item_guid='%s' AND item_id!='%d' " , $item_guid , $item_id );
$dups = array ();
while ( $row = fof_db_get_row ( $result ))
if ( md5 ( $row [ 'item_content' ]) == $content_md5 )
$dups [] = intval ( $row [ 'item_id' ]);
if ( ! count ( $dups ))
return array ();
$result = fof_db_query (
" SELECT DISTINCT $FOF_SUBSCRIPTION_TABLE .user_id FROM $FOF_ITEM_TABLE , $FOF_SUBSCRIPTION_TABLE " .
" WHERE item_id IN ( " . join ( " , " , $dups ) . " ) AND $FOF_SUBSCRIPTION_TABLE .feed_id= $FOF_ITEM_TABLE .feed_id "
);
$users = array ();
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
$users [] = reset ( $row );
2009-12-07 01:12:15 +03:00
return $users ;
}
2010-01-25 20:30:38 +03:00
function fof_db_set_feed_custom_title ( $user_id , $feed_id , $feed_title , $custom_title )
{
global $FOF_FEED_TABLE , $FOF_SUBSCRIPTION_TABLE ;
$prefs = fof_db_get_subscription_prefs ( $user_id , $feed_id );
if ( ! $prefs )
return false ;
if ( ! $custom_title || $feed_title == $custom_title )
$r = $prefs [ 'feed_title' ] = '' ;
else
$r = $prefs [ 'feed_title' ] = $custom_title ;
fof_db_set_subscription_prefs ( $user_id , $feed_id , $prefs );
return $r ;
}
2010-01-28 15:03:50 +03:00
function fof_db_get_top_readers ( $days , $count = NULL )
{
global $FOF_ITEM_TABLE , $FOF_USER_TABLE , $FOF_ITEM_TAG_TABLE , $FOF_SUBSCRIPTION_TABLE , $FOF_TAG_TABLE ;
$result = fof_db_query (
" select u.*, count(i.item_id) posts from $FOF_USER_TABLE u
join $FOF_SUBSCRIPTION_TABLE s on s . user_id = u . user_id
join $FOF_ITEM_TABLE i on i . feed_id = s . feed_id
join $FOF_TAG_TABLE t on t . tag_name = 'unread'
2011-07-21 15:25:55 +04:00
left join $FOF_ITEM_TAG_TABLE ti on ti . tag_id = t . tag_id and ti . item_id = i . item_id and ti . user_id = u . user_id
2010-01-28 15:03:50 +03:00
where i . item_published > " .(time()-intval( $days *86400)). " and ti . tag_id is null
group by u . user_id order by posts desc
" .(!is_null( $count ) ? " limit $count " : " " ));
$readers = array ();
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
2010-01-28 15:03:50 +03:00
$readers [] = $row ;
return $readers ;
}
2010-01-28 16:49:32 +03:00
function fof_db_get_most_popular_feeds ( $count = NULL )
{
global $FOF_SUBSCRIPTION_TABLE , $FOF_FEED_TABLE ;
$result = fof_db_query (
" select *, count(u.user_id) readers from $FOF_FEED_TABLE f join $FOF_SUBSCRIPTION_TABLE u on u.feed_id=f.feed_id
left join $FOF_SUBSCRIPTION_TABLE s on s . feed_id = f . feed_id and s . user_id = " .fof_current_user(). "
where s . feed_id is null and f . feed_url not regexp '^[a-z]+://[^/]+:[^/]*@'
group by f . feed_id having readers > 1 order by readers desc
" .(!is_null( $count ) ? " limit $count " : " " ));
$feeds = array ();
2015-09-16 13:47:49 +03:00
while ( $row = fof_db_get_row ( $result ))
2010-01-28 16:49:32 +03:00
$feeds [] = $row ;
return $feeds ;
}
2011-07-21 15:25:55 +04:00
function fof_db_get_feed_single_user ( $id )
{
global $FOF_SUBSCRIPTION_TABLE ;
2015-09-16 13:47:49 +03:00
$result = fof_safe_query ( " SELECT user_id, COUNT(user_id) n FROM $FOF_SUBSCRIPTION_TABLE WHERE feed_id=%d GROUP BY feed_id " , $id );
$row = fof_db_get_row ( $result );
if ( ! $row || $row [ 'n' ] > 1 )
2011-07-21 15:25:55 +04:00
return NULL ;
2015-09-16 13:47:49 +03:00
return $row [ 'user_id' ];
2011-07-21 15:25:55 +04:00
}
function fof_cache_fn ( $key )
{
if ( preg_match ( '/[^a-z0-9_\-]/' , $key ))
$key = 'md5_' . md5 ( $key );
return dirname ( __FILE__ ) . '/cache/' . $key ;
}
function fof_cache_get ( $key )
{
$file = fof_cache_fn ( $key );
$fp = @ fopen ( $file , " rb " );
if ( ! $fp )
return NULL ;
$expire = intval ( fgets ( $fp ));
$value = fread ( $fp , 1048576 );
fclose ( $fp );
if ( time () > $expire )
{
fof_cache_unset ( $key );
return NULL ;
}
return $value ;
}
function fof_cache_set ( $key , $value , $ttl = 86400 )
{
$file = fof_cache_fn ( $key );
if ( $fp = fopen ( $file , " wb " ))
{
fwrite ( $fp , ( time () + $ttl ) . " \n " . $value );
fclose ( $fp );
return true ;
}
return false ;
}
function fof_cache_unset ( $key )
{
$file = fof_cache_fn ( $key );
@ unlink ( $file );
}