]> git.mxchange.org Git - friendica-addons.git/blob - pages/pages.php
Pages: Moved under Networks
[friendica-addons.git] / pages / pages.php
1 <?php
2 /**
3  * Name: Pages
4  * Description: Shows lists of community pages
5  * Version: 1.0
6  * Author: Michael Vogel <ike@piratenpartei.de>
7  *
8  */
9
10 function pages_install() {
11         register_hook('page_end', 'addon/pages/pages.php', 'pages_page_end');
12 }
13
14 function pages_uninstall() {
15         unregister_hook('page_end', 'addon/pages/pages.php', 'pages_page_end');
16 }
17
18 function pages_iscommunity($url, &$pagelist) {
19         // check every week for the status - should be enough
20         if ($pagelist[$url]["checked"]<time()-86400*7) {
21                 // When too old or not found fetch the status from the profile
22                 $ch = curl_init();
23
24                 $url = str_replace("/profile/","/hcard/", $url);
25
26                 curl_setopt($ch, CURLOPT_URL, $url);
27                 curl_setopt($ch, CURLOPT_HEADER, 0);
28                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
29                 curl_setopt($ch, CURLOPT_TIMEOUT, 2);
30  
31                 $page = curl_exec($ch);
32  
33                 curl_close($ch);
34
35                 $iscommunity = (strpos($page, '<meta name="friendika.community" content="true" />') != 0);
36
37                 $pagelist[$url] = array("community" => $iscommunity, "checked" => time());
38         } else // Fetch from cache
39                 $iscommunity = $pagelist[$url]["community"];
40         return($iscommunity);
41 }
42
43 function pages_getpages($uid) {
44
45         // Fetch cached pagelist from configuration
46         $pagelist = get_pconfig($uid,'pages','pagelist');
47
48         if (sizeof($pagelist) == 0)
49                 $pagelist = array();
50
51         $contacts = q("SELECT `id`, `url`, `Name` FROM `contact`
52                         WHERE `network`= 'dfrn' AND `uid` = %d",
53                         intval($uid));
54
55         $pages = array();
56
57         // Look if the profile is a community page
58         foreach($contacts as $contact) {
59                 if (pages_iscommunity($contact["url"], $pagelist))
60                         $pages[] = array("url"=>$contact["url"], "Name"=>$contact["Name"], "id"=>$contact["id"]);
61         }
62
63         // Write back cached pagelist
64         set_pconfig($uid,'pages','pagelist', $pagelist);
65         return($pages);
66 }
67
68 function pages_page_end($a,&$b) {
69         // Only move on if if it's the "network" module and there is a logged on user
70         if (($a->module != "network") OR ($a->user['uid'] == 0))
71                 return;
72
73         $pages = '<div id="pages-sidebar" class="widget">
74                         <div class="title tool">
75                         <h3>'.t("Community").'</h3></div>
76                         <div id="sidebar-pages-list"><ul>';
77
78         $contacts = pages_getpages($a->user['uid']);
79
80         foreach($contacts as $contact) {
81                 $pages .= '<li class="tool"><a href="'.$a->get_baseurl().'/redir/'.$contact["id"].'" class="label" target="external-link">'.
82                                 $contact["Name"]."</a></li>";
83         }
84         $pages .= "</ul></div></div>";
85         if (sizeof($contacts) > 0) {
86                 $pos = strpos($a->page['aside'], '<div id="saved-search-list"');
87                 if ($pos > 0) {
88                         $a->page['aside'] = substr($a->page['aside'], 0, $pos).$pages.substr($a->page['aside'], $pos);
89                 } else
90                         $a->page['aside'] = $pages.$a->page['aside'];
91         }
92 }
93 ?>