]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Priority for native networks when searching by contact url
[friendica.git] / mod / dirfind.php
1 <?php
2 require_once('include/contact_widgets.php');
3 require_once('include/socgraph.php');
4 require_once('include/Contact.php');
5 require_once('include/contact_selectors.php');
6 require_once('mod/contacts.php');
7
8 function dirfind_init(&$a) {
9
10         if(! local_user()) {
11                 notice( t('Permission denied.') . EOL );
12                 return;
13         }
14
15         if(! x($a->page,'aside'))
16                 $a->page['aside'] = '';
17
18         $a->page['aside'] .= findpeople_widget();
19
20         $a->page['aside'] .= follow_widget();
21 }
22
23
24
25 function dirfind_content(&$a, $prefix = "") {
26
27         $community = false;
28         $discover_user = false;
29
30         $local = get_config('system','poco_local_search');
31
32         $search = $prefix.notags(trim($_REQUEST['search']));
33
34         if(strpos($search,'@') === 0) {
35                 $search = substr($search,1);
36                 if ((valid_email($search) AND validate_email($search)) OR
37                         (substr(normalise_link($search), 0, 7) == "http://")) {
38                         $user_data = probe_url($search);
39                         $discover_user = (in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)));
40                 }
41         }
42
43         if(strpos($search,'!') === 0) {
44                 $search = substr($search,1);
45                 $community = true;
46         }
47
48         $o = '';
49
50         if($search) {
51
52                 if ($discover_user) {
53                         $j = new stdClass();
54                         $j->total = 1;
55                         $j->items_page = 1;
56                         $j->page = $a->pager['page'];
57
58                         $objresult = new stdClass();
59                         $objresult->cid = 0;
60                         $objresult->name = $user_data["name"];
61                         $objresult->addr = $user_data["addr"];
62                         $objresult->url = $user_data["url"];
63                         $objresult->photo = $user_data["photo"];
64                         $objresult->tags = "";
65                         $objresult->network = $user_data["network"];
66
67                         $contact = get_contact_details_by_url($user_data["url"], local_user());
68                         $objresult->cid = $contact["cid"];
69
70                         $j->results[] = $objresult;
71
72                         // Add the contact to the global contacts if it isn't already in our system
73                         if (($contact["cid"] == 0) AND ($contact["zid"] == 0) AND ($contact["gid"] == 0))
74                                 poco_check($user_data["url"], $user_data["name"], $user_data["network"], $user_data["photo"],
75                                         "", "", "", "", "", datetime_convert(), 0);
76                 } elseif ($local) {
77
78                         if ($community)
79                                 $extra_sql = " AND `community`";
80                         else
81                                 $extra_sql = "";
82
83                         $perpage = 80;
84                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
85
86                         if (get_config('system','diaspora_enabled'))
87                                 $diaspora = NETWORK_DIASPORA;
88                         else
89                                 $diaspora = NETWORK_DFRN;
90
91                         if (!get_config('system','ostatus_disabled'))
92                                 $ostatus = NETWORK_OSTATUS;
93                         else
94                                 $ostatus = NETWORK_DFRN;
95
96                         $search2 = "%".$search."%";
97
98                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
99                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
100                                                 AND `contact`.`network` = `gcontact`.`network`
101                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
102                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
103                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
104                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
105                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
106                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
107                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
108                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
109                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
110                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
111
112                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
113                                         FROM `gcontact`
114                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
115                                                 AND `contact`.`network` = `gcontact`.`network`
116                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
117                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
118                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
119                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
120                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
121                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
122                                                 GROUP BY `gcontact`.`nurl`
123                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
124                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
125                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
126                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
127                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
128                                         intval($startrec), intval($perpage));
129                         $j = new stdClass();
130                         $j->total = $count[0]["total"];
131                         $j->items_page = $perpage;
132                         $j->page = $a->pager['page'];
133                         foreach ($results AS $result) {
134                                 if (poco_alternate_ostatus_url($result["url"]))
135                                          continue;
136
137                                 $result = get_contact_details_by_url($result["url"], local_user(), $result);
138
139                                 if ($result["name"] == "") {
140                                         $urlparts = parse_url($result["url"]);
141                                         $result["name"] = end(explode("/", $urlparts["path"]));
142                                 }
143
144                                 $objresult = new stdClass();
145                                 $objresult->cid = $result["cid"];
146                                 $objresult->name = $result["name"];
147                                 $objresult->addr = $result["addr"];
148                                 $objresult->url = $result["url"];
149                                 $objresult->photo = $result["photo"];
150                                 $objresult->tags = $result["keywords"];
151                                 $objresult->network = $result["network"];
152
153                                 $j->results[] = $objresult;
154                         }
155
156                         // Add found profiles from the global directory to the local directory
157                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
158                 } else {
159
160                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
161
162                         if(strlen(get_config('system','directory')))
163                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
164
165                         $j = json_decode($x);
166                 }
167
168                 if($j->total) {
169                         $a->set_pager_total($j->total);
170                         $a->set_pager_itemspage($j->items_page);
171                 }
172
173                 if(count($j->results)) {
174
175                         $id = 0;
176
177                         foreach($j->results as $jj) {
178
179                                 $alt_text = "";
180
181                                 $contact_details = get_contact_details_by_url($jj->url, local_user());
182
183                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
184
185                                 // If We already know this contact then don't show the "connect" button
186                                 if ($jj->cid > 0) {
187                                         $connlnk = "";
188                                         $conntxt = "";
189                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
190                                                         intval($jj->cid));
191                                         if ($contact) {
192                                                 $photo_menu = contact_photo_menu($contact[0]);
193                                                 $details = _contact_detail_for_template($contact[0]);
194                                                 $alt_text = $details['alt_text'];
195                                         } else
196                                                 $photo_menu = array();
197                                 } else {
198                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
199                                         $conntxt = t('Connect');
200                                         $photo_menu = array(
201                                                 'profile' => array(t("View Profile"), zrl($jj->url)),
202                                                 'follow' => array(t("Connect/Follow"), $connlnk)
203                                         );
204                                 }
205
206                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
207
208                                 $entry = array(
209                                         'alt_text' => $alt_text,
210                                         'url' => zrl($jj->url),
211                                         'itemurl' => $itemurl,
212                                         'name' => htmlentities($jj->name),
213                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
214                                         'img_hover' => $jj->tags,
215                                         'conntxt' => $conntxt,
216                                         'connlnk' => $connlnk,
217                                         'photo_menu' => $photo_menu,
218                                         'details'       => $contact_details['location'],
219                                         'tags'          => $contact_details['keywords'],
220                                         'about'         => $contact_details['about'],
221                                         'account_type'  => (($contact_details['community']) ? t('Forum') : ''),
222                                         'network' => network_to_name($jj->network, $jj->url),
223                                         'id' => ++$id,
224                                 );
225                                 $entries[] = $entry;
226                         }
227
228                 $tpl = get_markup_template('viewcontact_template.tpl');
229
230                 $o .= replace_macros($tpl,array(
231                         'title' => sprintf( t('People Search - %s'), $search),
232                         '$contacts' => $entries,
233                         '$paginate' => paginate($a),
234                 ));
235
236                 }
237                 else {
238                         info( t('No matches') . EOL);
239                 }
240
241         }
242
243         return $o;
244 }