streams/mod/display.php

269 lines
6.5 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
function display_content(&$a, $update = 0, $load = false) {
2010-09-09 03:14:17 +00:00
2013-09-11 04:17:33 +00:00
// logger("mod-display: update = $update load = $load");
2013-03-25 02:51:23 +00:00
2012-10-24 05:01:18 +00:00
if(intval(get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
notice( t('Public access denied.') . EOL);
return;
}
require_once("include/bbcode.php");
require_once('include/security.php');
require_once('include/conversation.php');
2012-02-13 08:33:20 +00:00
require_once('include/acl_selectors.php');
require_once('include/items.php');
2013-01-06 21:42:51 +00:00
$a->page['htmlhead'] .= replace_macros(get_markup_template('display-head.tpl'), array());
if(argc() > 1 && argv(1) !== 'load')
$item_hash = argv(1);
2013-03-25 02:51:23 +00:00
2013-03-25 19:20:12 +00:00
if($_REQUEST['mid'])
$item_hash = $_REQUEST['mid'];
2010-09-09 03:14:17 +00:00
2012-10-24 05:01:18 +00:00
if(! $item_hash) {
2010-09-09 03:14:17 +00:00
$a->error = 404;
notice( t('Item not found.') . EOL);
return;
}
2012-10-24 05:01:18 +00:00
$observer_is_owner = false;
2012-03-07 00:28:52 +00:00
if(local_user() && (! $update)) {
$channel = $a->get_channel();
$channel_acl = array(
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid']
);
$x = array(
'is_owner' => true,
'allow_location' => ((intval(get_pconfig($channel['channel_id'],'system','use_browser_location'))) ? '1' : ''),
'default_location' => $channel['channel_location'],
'nickname' => $channel['channel_address'],
'lockstate' => (($group || $cid || $channel['channel_allow_cid'] || $channel['channel_allow_gid'] || $channel['channel_deny_cid'] || $channel['channel_deny_gid']) ? 'lock' : 'unlock'),
'acl' => populate_acl($channel_acl),
'bang' => '',
'visitor' => true,
'profile_uid' => local_user(),
'return_path' => 'channel/' . $channel['channel_address']
);
$o .= status_editor($a,$x);
}
// This page can be viewed by anybody so the query could be complicated
// First we'll see if there is a copy of the item which is owned by us - if we're logged in locally.
// If that fails (or we aren't logged in locally),
// query an item in which the observer (if logged in remotely) has cid or gid rights
// and if that fails, look for a copy of the post that has no privacy restrictions.
// If we find the post, but we don't find a copy that we're allowed to look at, this fact needs to be reported.
// find a copy of the item somewhere
$target_item = null;
2013-02-11 01:03:33 +00:00
$r = q("select id, uid, mid, parent_mid, item_restrict from item where mid like '%s' limit 1",
dbesc($item_hash . '%')
);
2013-02-11 01:03:33 +00:00
if($r) {
$target_item = $r[0];
2010-09-09 03:14:17 +00:00
}
$r = null;
if($target_item['item_restrict'] & ITEM_WEBPAGE) {
$x = q("select * from channel where channel_id = %d limit 1",
intval($target_item['uid'])
);
$y = q("select * from item_id where uid = %d and service = 'WEBPAGE' and iid = %d limit 1",
intval($target_item['uid']),
intval($target_item['id'])
);
if($x && $y) {
goaway(z_root() . '/page/' . $x[0]['channel_address'] . '/' . $y[0]['sid']);
}
else {
notice( t('Page not found.') . EOL);
return '';
}
}
if((! $update) && (! $load)) {
$o .= '<div id="live-display"></div>' . "\r\n";
2013-06-18 09:06:16 +00:00
$o .= "<script> var profile_uid = " . ((intval(local_user())) ? local_user() : (-1))
. "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
$a->page['htmlhead'] .= replace_macros(get_markup_template("build_query.tpl"),array(
'$baseurl' => z_root(),
'$pgtype' => 'display',
'$uid' => '0',
'$gid' => '0',
'$cid' => '0',
'$cmin' => '0',
'$cmax' => '99',
'$star' => '0',
'$liked' => '0',
'$conv' => '0',
'$spam' => '0',
2014-03-27 07:35:46 +00:00
'$fh' => '0',
'$nouveau' => '0',
'$wall' => '0',
'$page' => (($a->pager['page'] != 1) ? $a->pager['page'] : 1),
'$list' => ((x($_REQUEST,'list')) ? intval($_REQUEST['list']) : 0),
'$search' => '',
'$order' => '',
'$file' => '',
'$cats' => '',
2014-12-15 22:37:31 +00:00
'$tags' => '',
'$dend' => '',
'$dbegin' => '',
'$verb' => '',
2013-03-25 19:20:12 +00:00
'$mid' => $item_hash
));
2010-09-09 03:14:17 +00:00
2012-10-24 05:01:18 +00:00
}
2012-10-24 05:01:18 +00:00
$observer_hash = get_observer_hash();
$sql_extra = public_permissions_sql($observer_hash);
2012-10-24 05:01:18 +00:00
2013-12-17 11:23:39 +00:00
if(($update && $load) || ($_COOKIE['jsAvailable'] != 1)) {
2012-10-24 05:01:18 +00:00
$updateable = false;
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$pager_sql = sprintf(" LIMIT %d OFFSET %d ", intval($a->pager['itemspage']),intval($a->pager['start']));
2012-10-24 05:01:18 +00:00
2013-12-17 11:23:39 +00:00
if($load || ($_COOKIE['jsAvailable'] != 1)) {
$r = null;
require_once('include/identity.php');
$sys = get_sys_channel();
2014-12-19 08:28:36 +00:00
$sysid = $sys['channel_id'];
if(local_user()) {
$r = q("SELECT * from item
WHERE item_restrict = 0
and uid = %d
and mid = '%s'
limit 1",
intval(local_user()),
dbesc($target_item['parent_mid'])
);
if($r) {
$updateable = true;
}
2014-06-11 05:35:15 +00:00
}
if($r === null) {
// in case somebody turned off public access to sys channel content using permissions
// make that content unsearchable by ensuring the owner_xchan can't match
2014-12-19 08:28:36 +00:00
if(! perm_is_allowed($sysid,$observer_hash,'view_stream'))
$sysid = 0;
$r = q("SELECT * from item
WHERE item_restrict = 0
and mid = '%s'
AND (((( `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = ''
AND `item`.`deny_gid` = '' AND item_private = 0 )
and owner_xchan in ( " . stream_perms_xchans(($observer_hash) ? (PERMS_NETWORK|PERMS_PUBLIC) : PERMS_PUBLIC) . " ))
2014-12-19 08:28:36 +00:00
OR uid = %d )
$sql_extra )
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
limit 1",
dbesc($target_item['parent_mid']),
2014-12-19 08:28:36 +00:00
intval($sysid)
);
}
}
else {
$r = array();
}
}
2012-10-24 05:01:18 +00:00
if($r) {
2012-10-24 05:01:18 +00:00
$parents_str = ids_to_querystr($r,'id');
if($parents_str) {
2012-10-24 05:01:18 +00:00
$items = q("SELECT `item`.*, `item`.`id` AS `item_id`
FROM `item`
WHERE item_restrict = 0 and parent in ( %s ) ",
dbesc($parents_str)
);
2012-10-24 05:01:18 +00:00
xchan_query($items);
2013-02-11 08:20:14 +00:00
$items = fetch_post_tags($items,true);
$items = conv_sort($items,'created');
}
} else {
$items = array();
}
2012-10-24 05:01:18 +00:00
2013-12-17 11:23:39 +00:00
if ($_COOKIE['jsAvailable'] == 1) {
$o .= conversation($a, $items, 'display', $update, 'client');
} else {
$o .= conversation($a, $items, 'display', $update, 'traditional');
}
if($updateable) {
$x = q("UPDATE item SET item_unseen = 0 where item_unseen = 1 AND uid = %d and parent = %d ",
intval(local_user()),
intval($r[0]['parent'])
);
}
$o .= '<div id="content-complete"></div>';
return $o;
2010-09-09 03:14:17 +00:00
/*
elseif((! $update) && (! {
$r = q("SELECT `id`, item_flags FROM `item` WHERE `id` = '%s' OR `mid` = '%s' LIMIT 1",
dbesc($item_hash),
dbesc($item_hash)
2010-09-17 10:10:19 +00:00
);
if($r) {
2015-01-23 05:04:54 +00:00
if(intval($r[0]['item_deleted'])) {
2010-09-17 10:10:19 +00:00
notice( t('Item has been removed.') . EOL );
}
else {
notice( t('Permission denied.') . EOL );
}
}
else {
notice( t('Item not found.') . EOL );
}
}
*/
2010-09-09 03:14:17 +00:00
return $o;
2010-09-17 10:10:19 +00:00
}