]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Only offer the choice to activate Diaspora and OStatus if the system is ready for it
[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
29         $local = get_config('system','poco_local_search');
30
31         $search = $prefix.notags(trim($_REQUEST['search']));
32
33         if(strpos($search,'@') === 0)
34                 $search = substr($search,1);
35
36         if(strpos($search,'!') === 0) {
37                 $search = substr($search,1);
38                 $community = true;
39         }
40
41         $o = '';
42
43         if($search) {
44
45                 if ($local) {
46
47                         if ($community)
48                                 $extra_sql = " AND `community`";
49                         else
50                                 $extra_sql = "";
51
52                         $perpage = 80;
53                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
54
55                         if (get_config('system','diaspora_enabled'))
56                                 $diaspora = NETWORK_DIASPORA;
57                         else
58                                 $diaspora = NETWORK_DFRN;
59
60                         if (!get_config('system','ostatus_disabled'))
61                                 $ostatus = NETWORK_OSTATUS;
62                         else
63                                 $ostatus = NETWORK_DFRN;
64
65                         $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND
66                                         (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR
67                                                 `about` REGEXP '%s' OR `keywords` REGEXP '%s')".$extra_sql,
68                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
69                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
70                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)));
71
72                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network` , `gcontact`.`keywords`
73                                         FROM `gcontact`
74                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
75                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
76                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
77                                         WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND
78                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
79                                         (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR
80                                                 `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') $extra_sql
81                                                 GROUP BY `gcontact`.`nurl`
82                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
83                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
84                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
85                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
86                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)),
87                                         intval($startrec), intval($perpage));
88                         $j = new stdClass();
89                         $j->total = $count[0]["total"];
90                         $j->items_page = $perpage;
91                         $j->page = $a->pager['page'];
92                         foreach ($results AS $result) {
93                                 if (poco_alternate_ostatus_url($result["url"]))
94                                          continue;
95
96                                 if ($result["name"] == "") {
97                                         $urlparts = parse_url($result["url"]);
98                                         $result["name"] = end(explode("/", $urlparts["path"]));
99                                 }
100
101                                 $objresult = new stdClass();
102                                 $objresult->cid = $result["cid"];
103                                 $objresult->name = $result["name"];
104                                 $objresult->url = $result["url"];
105                                 $objresult->photo = $result["photo"];
106                                 $objresult->tags = $result["keywords"];
107                                 $objresult->network = $result["network"];
108
109                                 $j->results[] = $objresult;
110                         }
111
112                         // Add found profiles from the global directory to the local directory
113                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
114                 } else {
115
116                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
117
118                         if(strlen(get_config('system','directory')))
119                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
120
121                         $j = json_decode($x);
122                 }
123
124                 if($j->total) {
125                         $a->set_pager_total($j->total);
126                         $a->set_pager_itemspage($j->items_page);
127                 }
128
129                 if(count($j->results)) {
130
131                         $id = 0;
132
133                         foreach($j->results as $jj) {
134
135                                 $alt_text = "";
136
137                                 $itemurl = $jj->url;
138
139                                 // If We already know this contact then don't show the "connect" button
140                                 if ($jj->cid > 0) {
141                                         $connlnk = "";
142                                         $conntxt = "";
143                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
144                                                         intval($jj->cid));
145                                         if ($contact) {
146                                                 $photo_menu = contact_photo_menu($contact[0]);
147                                                 $details = _contact_detail_for_template($contact[0]);
148                                                 $alt_text = $details['alt_text'];
149                                         } else
150                                                 $photo_menu = array();
151                                 } else {
152                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
153                                         $conntxt = t('Connect');
154                                         $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
155                                         $photo_menu[] = array(t("Connect/Follow"), $connlnk);
156                                 }
157
158                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
159
160                                 $entry = array(
161                                         'alt_text' => $alt_text,
162                                         'url' => zrl($jj->url),
163                                         'itemurl' => $itemurl,
164                                         'name' => htmlentities($jj->name),
165                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
166                                         'img_hover' => $jj->tags,
167                                         'conntxt' => $conntxt,
168                                         'connlnk' => $connlnk,
169                                         'photo_menu' => $photo_menu,
170                                         'network' => network_to_name($jj->network, $jj->url),
171                                         'id' => ++$id,
172                                 );
173                                 $entries[] = $entry;
174                         }
175
176                 $tpl = get_markup_template('viewcontact_template.tpl');
177
178                 $o .= replace_macros($tpl,array(
179                         'title' => sprintf( t('People Search - %s'), $search),
180                         '$contacts' => $entries,
181                         '$paginate' => paginate($a),
182                 ));
183
184                 }
185                 else {
186                         info( t('No matches') . EOL);
187                 }
188
189         }
190
191         return $o;
192 }