From b02e30f1cfb3e92c65a5b8783256260a2e72ecc2 Mon Sep 17 00:00:00 2001
From: Mike Macgirvin <mike@macgirvin.com>
Date: Sun, 22 Aug 2010 20:57:20 -0700
Subject: [PATCH] sort out notify x-domain

---
 database.sql         |  1 +
 include/items.php    |  4 +++-
 include/notifier.php |  4 +++-
 include/poller.php   | 22 ++++++++++++++++------
 mod/dfrn_notify.php  | 20 +++++++++++++-------
 mod/item.php         | 21 +++++++++++++--------
 mod/photos.php       | 29 ++++++++++++++++++-----------
 7 files changed, 67 insertions(+), 34 deletions(-)

diff --git a/database.sql b/database.sql
index af1ec630f2..2541de76a3 100644
--- a/database.sql
+++ b/database.sql
@@ -149,6 +149,7 @@ CREATE TABLE IF NOT EXISTS `item` (
   `parent-uri` char(255) CHARACTER SET ascii NOT NULL,
   `created` datetime NOT NULL,
   `edited` datetime NOT NULL,
+  `changed` datetime NOT NULL,
   `owner-name` char(255) NOT NULL,
   `owner-link` char(255) NOT NULL,
   `owner-avatar` char(255) NOT NULL,
diff --git a/include/items.php b/include/items.php
index aeb89892ea..f57f3a833b 100644
--- a/include/items.php
+++ b/include/items.php
@@ -67,10 +67,11 @@ function get_feed_for(&$a,$dfrn_id,$owner_id,$last_update) {
 		FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
 		WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
 		AND NOT `item`.`type` IN ( 'remote', 'net-comment' ) AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-		AND `item`.`edited` > '%s'
+		AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
 		$sql_extra
 		ORDER BY `parent` ASC, `created` ASC LIMIT 0, 300",
 		intval($owner_id),
+		dbesc($check_date),
 		dbesc($check_date)
 	);
 	if(! count($r))
@@ -209,6 +210,7 @@ function post_remote($a,$arr) {
 	$arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
 	$arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
 	$arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
+	$arr['changed'] = datetime_convert();
 	$arr['title'] = notags(trim($arr['title']));
 	$arr['location'] = notags(trim($arr['location']));
 	$arr['body'] = escape_tags(trim($arr['body']));
diff --git a/include/notifier.php b/include/notifier.php
index d00ef54f43..fe1925c337 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -17,7 +17,7 @@
 	if($argc < 3)
 		exit;
 
-	$a->set_baseurl(get_config('system',url'));
+	$a->set_baseurl(get_config('system','url'));
 
 	$baseurl = $argv[1];
 	$a->set_baseurl($argv[1]);
@@ -34,6 +34,8 @@
 			break;
 	}
 
+	if($debugging)
+		dbg(3);
 
 	$recipients = array();
 
diff --git a/include/poller.php b/include/poller.php
index fcd74fd69b..80b6f35129 100644
--- a/include/poller.php
+++ b/include/poller.php
@@ -191,18 +191,20 @@ echo "Length:" . strlen($xml) . "\r\n";
 				);
 				if(count($r)) {
 					if($r[0]['uri'] == $r[0]['parent-uri']) {
-						$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
+						$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
 							`body` = '', `title` = ''
 							WHERE `parent-uri` = '%s'",
 							dbesc($when),
+							dbesc(datetime_convert()),
 							dbesc($r[0]['uri'])
 						);
 					}
 					else {
-						$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
+						$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
 							`body` = '', `title` = '' 
 							WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 							dbesc($when),
+							dbesc(datetime_convert()),
 							dbesc($uri),
 							intval($importer['uid'])
 						);
@@ -233,13 +235,20 @@ echo "Length:" . strlen($xml) . "\r\n";
 				);
 				// FIXME update content if 'updated' changes
 				if(count($r)) {
-					$allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
+					$allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
 					if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-						$r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+						$r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s'",
+							dbesc(datetime_convert()),
+							dbesc($parent_uri)
+						);
+						$r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 							intval($allow[0]['data']),
+							dbesc(datetime_convert()),
 							dbesc($item_id),
 							intval($importer['uid'])
 						);
+
+
 					}
 					continue;
 				}
@@ -260,10 +269,11 @@ echo "Length:" . strlen($xml) . "\r\n";
 					intval($importer['uid'])
 				);
 				if(count($r)) {
-					$allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
+					$allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
 					if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-						$r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+						$r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 							intval($allow[0]['data']),
+							dbesc(datetime_convert()),
 							dbesc($item_id),
 							intval($importer['uid'])
 						);
diff --git a/mod/dfrn_notify.php b/mod/dfrn_notify.php
index 2ac98af596..089e508ad3 100644
--- a/mod/dfrn_notify.php
+++ b/mod/dfrn_notify.php
@@ -68,7 +68,7 @@ function dfrn_notify_post(&$a) {
 		$msg['uri'] = notags(unxmlify($base['id'][0]['data']));
 		$msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
 		$msg['created'] = datetime_convert(notags(unxmlify('UTC','UTC',$base['sentdate'][0]['data'])));
-
+		
 		dbesc_array($msg);
 
 		$r = q("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) 
@@ -124,16 +124,18 @@ function dfrn_notify_post(&$a) {
 			);
 			if(count($r)) {
 				if($r[0]['uri'] == $r[0]['parent-uri']) {
-					$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
+					$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'
 						WHERE `parent-uri` = '%s'",
 						dbesc($when),
+						dbesc(datetime_convert()),
 						dbesc($r[0]['uri'])
 					);
 				}
 				else {
-					$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
+					$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' 
 						WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 						dbesc($when),
+						dbesc(datetime_convert()),
 						dbesc($uri),
 						intval($importer['importer_uid'])
 					);
@@ -166,12 +168,14 @@ function dfrn_notify_post(&$a) {
 					intval($importer['importer_uid'])
 				);
 				if(count($r)) {
-					$r1 = q("UPDATE `item` SET `last-child` = 0 WHERE `uid` = %d AND `parent` = %d",
+					$r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d",
+						dbesc(datetime_convert()),
 						intval($importer['importer_uid']),
 						intval($r[0]['parent'])
 					);
 				}
-				$r2 = q("UPDATE `item` SET `last-child` = 1 WHERE `uid` = %d AND `id` = %d LIMIT 1",
+				$r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+						dbesc(datetime_convert()),
 						intval($importer['importer_uid']),
 						intval($posted_id)
 				);
@@ -214,8 +218,9 @@ function dfrn_notify_post(&$a) {
 				if(count($r)) {
 					$allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
 					if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-						$r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+						$r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 							intval($allow[0]['data']),
+							dbesc(datetime_convert()),
 							dbesc($item_id),
 							intval($importer['importer_uid'])
 						);
@@ -272,8 +277,9 @@ function dfrn_notify_post(&$a) {
 			if(count($r)) {
 				$allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
 				if($allow && $allow[0]['data'] != $r[0]['last-child']) {
-					$r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+					$r = q("UPDATE `item` SET `last-child` = %d, `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 						intval($allow[0]['data']),
+						dbesc(datetime_convert()),
 						dbesc($item_id),
 						intval($importer['importer_uid'])
 					);
diff --git a/mod/item.php b/mod/item.php
index 5b81f13eeb..812216426b 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -143,8 +143,8 @@ function item_post(&$a) {
 
 	$r = q("INSERT INTO `item` (`uid`,`type`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
 		`author-name`, `author-link`, `author-avatar`, `created`,
-		`edited`, `uri`, `title`, `body`, `location`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
-		VALUES( %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
+		`edited`, `changed`, `uri`, `title`, `body`, `location`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
+		VALUES( %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
 		intval($profile_uid),
 		dbesc($post_type),
 		intval($contact_id),
@@ -154,8 +154,9 @@ function item_post(&$a) {
 		dbesc($author['name']),
 		dbesc($author['url']),
 		dbesc($author['thumb']),
-		datetime_convert(),
-		datetime_convert(),
+		dbesc(datetime_convert()),
+		dbesc(datetime_convert()),
+		dbesc(datetime_convert()),
 		dbesc($uri),
 		dbesc($title),
 		dbesc($body),
@@ -173,7 +174,8 @@ function item_post(&$a) {
 		if($parent) {
 
 			// This item is the last leaf and gets the comment box, clear any ancestors
-			$r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent` = %d ",
+			$r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
+				dbesc(datetime_convert()),
 				intval($parent)
 			);
 
@@ -227,10 +229,11 @@ function item_post(&$a) {
 			}
 		}
 
-		$r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `last-child` = 1, `visible` = 1
+		$r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
 			WHERE `id` = %d LIMIT 1",
 			intval($parent),
 			dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
+			dbesc(datetime_convert()),
 			intval($post_id)
 		);
 		// photo comments turn the corresponding item visible to the profile wall
@@ -279,7 +282,8 @@ function item_content(&$a) {
 
 			// delete the item
 
-			$r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s' WHERE `id` = %d LIMIT 1",
+			$r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
+				dbesc(datetime_convert()),
 				dbesc(datetime_convert()),
 				intval($item['id'])
 			);
@@ -300,9 +304,10 @@ function item_content(&$a) {
 			// If it's the parent of a comment thread, kill all the kids
 
 			if($item['uri'] == $item['parent-uri']) {
-				$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `body` = '' 
+				$r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
 					WHERE `parent-uri` = '%s' AND `uid` = %d ",
 					dbesc(datetime_convert()),
+					dbesc(datetime_convert()),
 					dbesc($item['parent-uri']),
 					intval($item['uid'])
 				);
diff --git a/mod/photos.php b/mod/photos.php
index bbc9c2df5c..0558f746a9 100644
--- a/mod/photos.php
+++ b/mod/photos.php
@@ -117,7 +117,8 @@ function photos_post(&$a) {
 			);
 			if(count($r)) {
 				foreach($r as $rr) {
-					q("UPDATE `item` SET `deleted` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
+					q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
+						dbesc(datetime_convert()),
 						dbesc($rr['parent-uri']),
 						intval($_SESSION['uid'])
 					);
@@ -153,7 +154,9 @@ function photos_post(&$a) {
 				intval($_SESSION['uid'])
 			);
 			if(count($i)) {
-				q("UPDATE `item` SET `deleted` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
+				q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
+					dbesc(datetime_convert()),
+					dbesc(datetime_convert()),
 					dbesc($i[0]['uri']),
 					intval($_SESSION['uid'])
 				);
@@ -219,8 +222,8 @@ function photos_post(&$a) {
 
 			$r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,
 				`owner-name`,`owner-link`,`owner-avatar`, `created`,
-				`edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
-				VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
+				`edited`, `changed`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
+				VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
 				intval($_SESSION['uid']),
 				dbesc('photo'),
 				dbesc($p[0]['resource-id']),			
@@ -228,8 +231,9 @@ function photos_post(&$a) {
 				dbesc($contact_record['name']),
 				dbesc($contact_record['url']),
 				dbesc($contact_record['thumb']),
-				datetime_convert(),
-				datetime_convert(),
+				dbesc(datetime_convert()),
+				dbesc(datetime_convert()),
+				dbesc(datetime_convert()),
 				dbesc($uri),
 				dbesc($uri),
 				dbesc($title),
@@ -254,8 +258,10 @@ function photos_post(&$a) {
 			}
 		}
 
-		$r = q("UPDATE `item` SET `tag` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
+		$r = q("UPDATE `item` SET `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
 			dbesc($tags),
+			dbesc(datetime_convert()),
+			dbesc(datetime_convert()),
 			intval($item_id),
 			intval($_SESSION['uid'])
 		);
@@ -386,8 +392,8 @@ function photos_post(&$a) {
 
 
 	$r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
-		`edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `visible`)
-		VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
+		`edited`, `changed`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `visible`)
+		VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
 		intval($_SESSION['uid']),
 		dbesc('photo'),
 		dbesc($photo_hash),			
@@ -395,8 +401,9 @@ function photos_post(&$a) {
 		dbesc($contact_record['name']),
 		dbesc($contact_record['url']),
 		dbesc($contact_record['thumb']),
-		datetime_convert(),
-		datetime_convert(),
+		dbesc(datetime_convert()),
+		dbesc(datetime_convert()),
+		dbesc(datetime_convert()),
 		dbesc($uri),
 		dbesc($uri),
 		dbesc($title),