2013-02-26 01:09:40 +00:00
< ? php /** @file */
2012-12-20 03:51:52 +00:00
2012-12-27 03:39:37 +00:00
require_once ( 'include/permissions.php' );
2012-12-20 03:51:52 +00:00
function find_upstream_directory ( $dirmode ) {
2014-04-26 01:10:23 +00:00
global $DIRECTORY_FALLBACK_SERVERS ;
2013-10-14 02:29:54 +00:00
$preferred = get_config ( 'system' , 'directory_server' );
2014-04-26 01:10:23 +00:00
if ( ! $preferred ) {
/**
* No directory has yet been set . For most sites , pick one at random
* from our list of directory servers . However , if we ' re a directory
* server ourself , point at the local instance
* We will then set this value so this should only ever happen once .
* Ideally there will be an admin setting to change to a different
* directory server if you don ' t like our choice or if circumstances change .
*/
$dirmode = intval ( get_config ( 'system' , 'directory_mode' ));
if ( $dirmode == DIRECTORY_MODE_NORMAL ) {
$toss = mt_rand ( 0 , count ( $DIRECTORY_FALLBACK_SERVERS ));
$preferred = $DIRECTORY_FALLBACK_SERVERS [ $toss ];
set_config ( 'system' , 'directory_server' , $preferred );
}
else {
set_config ( 'system' , 'directory_server' , z_root ());
}
}
return array ( 'url' => $preferred );
2012-12-20 03:51:52 +00:00
}
2014-08-31 15:58:17 +00:00
function check_upstream_directory () {
/**
* Directories may come and go over time . We will need to check that our
* directory server is still valid occasionally , and reset to something that
* is if our directory has gone offline for any reason
*/
$directory = get_config ( 'system' , 'directory_server' );
if ( $directory ) {
$r = q ( " select * from site where site_url = '%s' and (site_flags & %d) " ,
dbesc ( $directory ),
intval ( DIRECTORY_MODE_PRIMARY | DIRECTORY_MODE_SECONDARY | DIRECTORY_MODE_STANDALONE )
);
}
// If we've got something, it's still a directory. If we haven't, we need to reset and let find_upstream_directory() fix it
if ( ! $r ) {
set_config ( 'system' , 'directory_server' , '' );
}
return ;
}
2013-10-14 02:49:40 +00:00
function dir_sort_links () {
$o = replace_macros ( get_markup_template ( 'dir_sort_links.tpl' ), array (
'$header' => t ( 'Sort Options' ),
'$normal' => t ( 'Alphabetic' ),
'$reverse' => t ( 'Reverse Alphabetic' ),
'$date' => t ( 'Newest to Oldest' )
));
return $o ;
}
2013-11-12 21:26:47 +00:00
function dir_safe_mode () {
2013-11-09 18:43:40 +00:00
$observer = get_observer_hash ();
2013-12-22 07:47:44 +00:00
if ( ! $observer )
return ;
2013-11-09 18:45:52 +00:00
if ( $observer )
2013-11-09 18:43:40 +00:00
$safe_mode = get_xconfig ( $observer , 'directory' , 'safe_mode' );
2013-11-11 06:13:17 +00:00
if ( $safe_mode === '0' )
2013-11-09 18:43:40 +00:00
$toggle = t ( 'Enable Safe Search' );
else
$toggle = t ( 'Disable Safe Search' );
$o = replace_macros ( get_markup_template ( 'safesearch.tpl' ), array (
2013-11-11 09:18:09 +00:00
'$safemode' => t ( 'Safe Mode' ),
2013-11-09 18:43:40 +00:00
'$toggle' => $toggle ,
));
return $o ;
}
2013-04-19 03:47:35 +00:00
function sync_directories ( $dirmode ) {
if ( $dirmode == DIRECTORY_MODE_STANDALONE || $dirmode == DIRECTORY_MODE_NORMAL )
return ;
2014-08-18 02:06:56 +00:00
$realm = get_directory_realm ();
if ( $realm == DIRECTORY_REALM ) {
$r = q ( " select * from site where (site_flags & %d) and site_url != '%s' and ( site_realm = '%s' or site_realm = '') " ,
intval ( DIRECTORY_MODE_PRIMARY | DIRECTORY_MODE_SECONDARY ),
dbesc ( z_root ()),
dbesc ( $realm )
);
}
else {
$r = q ( " select * from site where (site_flags & %d) and site_url != '%s' and site_realm like '%s' " ,
intval ( DIRECTORY_MODE_PRIMARY | DIRECTORY_MODE_SECONDARY ),
dbesc ( z_root ()),
dbesc ( protect_sprintf ( '%' . $realm . '%' ))
);
}
2013-04-19 03:47:35 +00:00
// If there are no directory servers, setup the fallback master
2014-08-18 02:06:56 +00:00
// FIXME - what to do if we're in a different realm?
2013-04-19 03:47:35 +00:00
if (( ! $r ) && ( z_root () != DIRECTORY_FALLBACK_MASTER )) {
$r = array (
'site_url' => DIRECTORY_FALLBACK_MASTER ,
'site_flags' => DIRECTORY_MODE_PRIMARY ,
2014-09-09 03:35:15 +00:00
'site_update' => NULL_DATE ,
2014-08-18 02:06:56 +00:00
'site_directory' => DIRECTORY_FALLBACK_MASTER . '/dirsearch' ,
'site_realm' => DIRECTORY_REALM
2013-04-19 03:47:35 +00:00
);
2014-08-18 02:06:56 +00:00
$x = q ( " insert into site ( site_url, site_flags, site_update, site_directory, site_realm )
values ( '%s' , % d ', ' % s ', ' % s ', ' % s ' ) " ,
2013-04-19 03:47:35 +00:00
dbesc ( $r [ 0 ][ 'site_url' ]),
intval ( $r [ 0 ][ 'site_flags' ]),
dbesc ( $r [ 0 ][ 'site_update' ]),
2014-08-18 02:06:56 +00:00
dbesc ( $r [ 0 ][ 'site_directory' ]),
dbesc ( $r [ 0 ][ 'site_realm' ])
2013-04-19 03:47:35 +00:00
);
2013-10-01 01:33:27 +00:00
$r = q ( " select * from site where (site_flags & %d) and site_url != '%s' " ,
intval ( DIRECTORY_MODE_PRIMARY | DIRECTORY_MODE_SECONDARY ),
dbesc ( z_root ())
);
2013-04-19 03:47:35 +00:00
}
2013-10-01 01:33:27 +00:00
if ( ! $r )
return ;
2013-04-19 03:47:35 +00:00
2013-10-01 01:33:27 +00:00
foreach ( $r as $rr ) {
if ( ! $rr [ 'site_directory' ])
continue ;
2014-06-30 01:18:29 +00:00
2014-06-30 01:24:34 +00:00
logger ( 'sync directories: ' . $rr [ 'site_directory' ]);
2014-06-30 01:18:29 +00:00
// for brand new directory servers, only load the last couple of days. Everything before that will be repeats.
2014-09-09 03:35:15 +00:00
$syncdate = (( $rr [ 'site_sync' ] === NULL_DATE ) ? datetime_convert ( 'UTC' , 'UTC' , 'now - 2 days' ) : $rr [ 'site_sync' ]);
2014-06-30 01:18:29 +00:00
$x = z_fetch_url ( $rr [ 'site_directory' ] . '?f=&sync=' . urlencode ( $syncdate ));
2013-10-01 01:33:27 +00:00
if ( ! $x [ 'success' ])
continue ;
$j = json_decode ( $x [ 'body' ], true );
if (( ! $j [ 'transactions' ]) || ( ! is_array ( $j [ 'transactions' ])))
continue ;
q ( " update site set site_sync = '%s' where site_url = '%s' limit 1 " ,
dbesc ( datetime_convert ()),
dbesc ( $rr [ 'site_url' ])
);
2013-04-19 03:47:35 +00:00
2013-10-01 01:33:27 +00:00
logger ( 'sync_directories: ' . $rr [ 'site_url' ] . ': ' . print_r ( $j , true ), LOGGER_DATA );
if ( count ( $j [ 'transactions' ])) {
foreach ( $j [ 'transactions' ] as $t ) {
$r = q ( " select * from updates where ud_guid = '%s' limit 1 " ,
dbesc ( $t [ 'transaction_id' ])
);
if ( $r )
continue ;
$ud_flags = 0 ;
if ( is_array ( $t [ 'flags' ]) && in_array ( 'deleted' , $t [ 'flags' ]))
$ud_flags |= UPDATE_FLAGS_DELETED ;
2014-03-04 05:00:42 +00:00
if ( is_array ( $t [ 'flags' ]) && in_array ( 'forced' , $t [ 'flags' ]))
$ud_flags |= UPDATE_FLAGS_FORCED ;
2013-10-01 01:33:27 +00:00
$z = q ( " insert into updates ( ud_hash, ud_guid, ud_date, ud_flags, ud_addr )
2013-10-15 03:51:26 +00:00
values ( '%s' , '%s' , '%s' , % d , '%s' ) " ,
2013-10-01 01:33:27 +00:00
dbesc ( $t [ 'hash' ]),
dbesc ( $t [ 'transaction_id' ]),
dbesc ( $t [ 'timestamp' ]),
intval ( $ud_flags ),
dbesc ( $t [ 'address' ])
);
}
}
}
}
2013-04-19 03:47:35 +00:00
2013-10-01 01:33:27 +00:00
function update_directory_entry ( $ud ) {
2013-04-19 03:47:35 +00:00
2013-10-01 01:33:27 +00:00
logger ( 'update_directory_entry: ' . print_r ( $ud , true ), LOGGER_DATA );
2013-04-19 03:47:35 +00:00
2013-10-01 01:33:27 +00:00
if ( $ud [ 'ud_addr' ] && ( ! ( $ud [ 'ud_flags' ] & UPDATE_FLAGS_DELETED ))) {
2013-10-15 04:56:56 +00:00
$success = false ;
2013-10-01 01:33:27 +00:00
$x = zot_finger ( $ud [ 'ud_addr' ], '' );
if ( $x [ 'success' ]) {
$j = json_decode ( $x [ 'body' ], true );
2013-10-15 04:56:56 +00:00
if ( $j )
$success = true ;
2014-07-01 04:17:19 +00:00
$y = import_xchan ( $j , 0 , $ud );
2013-10-01 01:33:27 +00:00
}
2013-10-15 04:56:56 +00:00
if ( ! $success ) {
2013-10-01 01:33:27 +00:00
$r = q ( " update updates set ud_last = '%s' where ud_addr = '%s' " ,
dbesc ( datetime_convert ()),
dbesc ( $ud [ 'ud_addr' ])
);
}
}
2013-10-25 02:38:50 +00:00
2013-04-19 03:47:35 +00:00
}
2014-02-11 22:19:20 +00:00
/**
* @ function local_dir_update ( $uid , $force )
* push local channel updates to a local directory server
*
*/
2013-04-19 03:47:35 +00:00
2014-02-11 22:19:20 +00:00
function local_dir_update ( $uid , $force ) {
2013-04-19 03:47:35 +00:00
2014-02-11 22:19:20 +00:00
logger ( 'local_dir_update' , LOGGER_DEBUG );
2013-02-20 07:21:23 +00:00
2013-10-25 02:38:50 +00:00
$p = q ( " select channel.channel_hash, channel_address, channel_timezone, profile.* from profile left join channel on channel_id = uid where uid = %d and is_default = 1 " ,
2012-12-27 03:39:37 +00:00
intval ( $uid )
);
$profile = array ();
2014-07-24 22:55:01 +00:00
$profile [ 'encoding' ] = 'zot' ;
2012-12-27 03:39:37 +00:00
if ( $p ) {
$hash = $p [ 0 ][ 'channel_hash' ];
$profile [ 'description' ] = $p [ 0 ][ 'pdesc' ];
$profile [ 'birthday' ] = $p [ 0 ][ 'dob' ];
2013-07-03 05:41:17 +00:00
if ( $age = age ( $p [ 0 ][ 'dob' ], $p [ 0 ][ 'channel_timezone' ], '' ))
$profile [ 'age' ] = $age ;
2012-12-27 03:39:37 +00:00
$profile [ 'gender' ] = $p [ 0 ][ 'gender' ];
$profile [ 'marital' ] = $p [ 0 ][ 'marital' ];
$profile [ 'sexual' ] = $p [ 0 ][ 'sexual' ];
$profile [ 'locale' ] = $p [ 0 ][ 'locality' ];
$profile [ 'region' ] = $p [ 0 ][ 'region' ];
$profile [ 'postcode' ] = $p [ 0 ][ 'postal_code' ];
$profile [ 'country' ] = $p [ 0 ][ 'country_name' ];
2013-12-25 09:57:04 +00:00
$profile [ 'about' ] = $p [ 0 ][ 'about' ];
$profile [ 'homepage' ] = $p [ 0 ][ 'homepage' ];
$profile [ 'hometown' ] = $p [ 0 ][ 'hometown' ];
2012-12-27 03:39:37 +00:00
if ( $p [ 0 ][ 'keywords' ]) {
$tags = array ();
$k = explode ( ' ' , $p [ 0 ][ 'keywords' ]);
if ( $k )
foreach ( $k as $kk )
if ( trim ( $kk ))
$tags [] = trim ( $kk );
if ( $tags )
$profile [ 'keywords' ] = $tags ;
}
2013-02-20 07:31:26 +00:00
$hidden = ( 1 - intval ( $p [ 0 ][ 'publish' ]));
2013-02-20 07:41:42 +00:00
logger ( 'hidden: ' . $hidden );
2013-02-20 07:31:26 +00:00
$r = q ( " select xchan_flags from xchan where xchan_hash = '%s' limit 1 " ,
dbesc ( $p [ 0 ][ 'channel_hash' ])
);
// Be careful - XCHAN_FLAGS_HIDDEN should evaluate to 1
if (( $r [ 0 ][ 'xchan_flags' ] & XCHAN_FLAGS_HIDDEN ) != $hidden )
$new_flags = $r [ 0 ][ 'xchan_flags' ] ^ XCHAN_FLAGS_HIDDEN ;
else
$new_flags = $r [ 0 ][ 'xchan_flags' ];
if ( $new_flags != $r [ 0 ][ 'xchan_flags' ]) {
2013-02-20 07:41:42 +00:00
2013-02-20 07:31:26 +00:00
$r = q ( " update xchan set xchan_flags = %d where xchan_hash = '%s' limit 1 " ,
intval ( $new_flags ),
2013-02-20 07:41:42 +00:00
dbesc ( $p [ 0 ][ 'channel_hash' ])
2013-02-20 07:31:26 +00:00
);
2013-02-20 07:41:42 +00:00
2013-02-20 07:31:26 +00:00
}
2013-12-24 03:44:23 +00:00
$address = $p [ 0 ][ 'channel_address' ] . '@' . get_app () -> get_hostname ();
2013-02-20 07:31:26 +00:00
2012-12-27 03:39:37 +00:00
if ( perm_is_allowed ( $uid , '' , 'view_profile' )) {
2013-12-24 03:44:23 +00:00
import_directory_profile ( $hash , $profile , $address , 0 );
2012-12-27 03:39:37 +00:00
}
else {
// they may have made it private
$r = q ( " delete from xprof where xprof_hash = '%s' limit 1 " ,
dbesc ( $hash )
);
2012-12-28 07:33:37 +00:00
$r = q ( " delete from xtag where xtag_hash = '%s' limit 1 " ,
dbesc ( $hash )
);
2012-12-27 03:39:37 +00:00
}
}
2014-02-11 04:38:26 +00:00
$ud_hash = random_string () . '@' . get_app () -> get_hostname ();
2014-06-01 23:34:04 +00:00
update_modtime ( $hash , $ud_hash , $p [ 0 ][ 'channel_address' ] . '@' . get_app () -> get_hostname (),(( $force ) ? UPDATE_FLAGS_FORCED : UPDATE_FLAGS_UPDATED ));
2013-10-25 02:38:50 +00:00
2012-12-27 03:39:37 +00:00
}