From dde8d8976d2164a7c75b6c659a626d210bfc5dcd Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <hypolite@mrpetovan.com>
Date: Wed, 24 Oct 2018 12:03:14 -0400
Subject: [PATCH] Fix usages of Pager with JSON calls in mod/match and
 mod/dirfind

---
 mod/dirfind.php | 26 ++++++++++++--------------
 mod/match.php   |  4 +++-
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/mod/dirfind.php b/mod/dirfind.php
index 9d3c852f49..5d7815bd80 100644
--- a/mod/dirfind.php
+++ b/mod/dirfind.php
@@ -67,6 +67,7 @@ function dirfind_content(App $a, $prefix = "") {
 	$o = '';
 
 	if ($search) {
+		$pager = new Pager($a->query_string);
 
 		if ($discover_user) {
 			$j = new stdClass();
@@ -94,14 +95,13 @@ function dirfind_content(App $a, $prefix = "") {
 				Model\GContact::update($user_data);
 			}
 		} elseif ($local) {
-
-			if ($community)
+			if ($community) {
 				$extra_sql = " AND `community`";
-			else
+			} else {
 				$extra_sql = "";
+			}
 
-			$perpage = 80;
-			$startrec = (($pager->getPage()) * $perpage) - $perpage;
+			$pager->setItemsPerPage(80);
 
 			if (Config::get('system','diaspora_enabled')) {
 				$diaspora = Protocol::DIASPORA;
@@ -138,10 +138,10 @@ function dirfind_content(App $a, $prefix = "") {
 					DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
 					DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
 					DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
-					intval($startrec), intval($perpage));
+					$pager->getStart(), $pager->getItemsPerPage());
 			$j = new stdClass();
 			$j->total = $count[0]["total"];
-			$j->items_page = $perpage;
+			$j->items_page = $pager->getItemsPerPage();
 			$j->page = $pager->getPage();
 			foreach ($results AS $result) {
 				if (PortableContact::alternateOStatusUrl($result["nurl"])) {
@@ -178,18 +178,18 @@ function dirfind_content(App $a, $prefix = "") {
 			// Add found profiles from the global directory to the local directory
 			Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
 		} else {
-
 			$p = (($pager->getPage() != 1) ? '&p=' . $pager->getPage() : '');
 
-			if(strlen(Config::get('system','directory')))
-				$x = Network::fetchUrl(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
+			if (strlen(Config::get('system','directory'))) {
+				$x = Network::fetchUrl(get_server() . '/lsearch?f=' . $p .  '&search=' . urlencode($search));
+			}
 
 			$j = json_decode($x);
+
+			$pager->setItemsPerPage($j->items_page);
 		}
 
 		if (!empty($j->results)) {
-			$pager = new Pager($a->query_string, $j->items_page);
-
 			$id = 0;
 
 			foreach ($j->results as $jj) {
@@ -250,13 +250,11 @@ function dirfind_content(App $a, $prefix = "") {
 			}
 
 			$tpl = get_markup_template('viewcontact_template.tpl');
-
 			$o .= replace_macros($tpl,[
 				'title' => $header,
 				'$contacts' => $entries,
 				'$paginate' => $pager->renderFull($j->total),
 			]);
-
 		} else {
 			info(L10n::t('No matches') . EOL);
 		}
diff --git a/mod/match.php b/mod/match.php
index a514439db6..43a3916506 100644
--- a/mod/match.php
+++ b/mod/match.php
@@ -54,7 +54,7 @@ function match_content(App $a)
 	$tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']);
 
 	if ($tags) {
-		$pager = new Pager($a->query_string, $j->items_page);
+		$pager = new Pager($a->query_string);
 
 		$params['s'] = $tags;
 		if ($pager->getPage() != 1) {
@@ -70,6 +70,8 @@ function match_content(App $a)
 		$j = json_decode($x);
 
 		if (count($j->results)) {
+			$pager->setItemsPerPage($j->items_page);
+
 			$id = 0;
 
 			foreach ($j->results as $jj) {
-- 
2.39.5