]> git.mxchange.org Git - friendica-addons.git/blob - pages/pages.php
Pages: New plugin that displays a list of community pages on the sidebar.
[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                 curl_setopt($ch, CURLOPT_URL, $url);
25                 curl_setopt($ch, CURLOPT_HEADER, 0);
26                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
27                 curl_setopt($ch, CURLOPT_TIMEOUT, 2);
28  
29                 $page = curl_exec($ch);
30  
31                 curl_close($ch);
32
33                 $iscommunity = (strpos($page, '<meta name="friendika.community" content="true" />') != 0);
34
35                 $pagelist[$url] = array("community" => $iscommunity, "checked" => time());
36         } else // Fetch from cache
37                 $iscommunity = $pagelist[$url]["community"];
38         return($iscommunity);
39 }
40
41 function pages_getpages($uid) {
42
43         // Fetch cached pagelist from configuration
44         $pagelist = get_pconfig($uid,'pages','pagelist');
45
46         if (sizeof($pagelist) == 0)
47                 $pagelist = array();
48
49         $contacts = q("SELECT `id`, `url`, `Name` FROM `contact`
50                         WHERE `network`= 'dfrn' AND `uid` = %d",
51                         intval($uid));
52
53         $pages = array();
54
55         // Look if the profile is a community page
56         foreach($contacts as $contact) {
57                 if (pages_iscommunity($contact["url"], $pagelist))
58                         $pages[] = array("url"=>$contact["url"], "Name"=>$contact["Name"], "id"=>$contact["id"]);
59         }
60
61         // Write back cached pagelist
62         set_pconfig($uid,'pages','pagelist', $pagelist);
63         return($pages);
64 }
65
66 function pages_page_end($a,&$b) {
67         // Only move on if if it's the "network" module and there is a logged on user
68         if (($a->module != "network") OR ($a->user['uid'] == 0))
69                 return;
70
71         $pages = '<div id="pages-sidebar" class="widget">
72                         <div class="title tool">
73                         <h3>'.t("Community").'</h3></div>
74                         <div id="sidebar-pages-list"><ul>';
75
76         $contacts = pages_getpages($a->user['uid']);
77
78         foreach($contacts as $contact) {
79                 $pages .= '<li class="tool"><a href="'.$a->get_baseurl().'/redir/'.$contact["id"].'" class="label" target="external-link">'.
80                                 $contact["Name"]."</a></li>";
81         }
82         $pages .= "</ul></div></div>";
83         if (sizeof($contacts) > 0)
84                 $a->page['aside'] = $pages.$a->page['aside'];
85 }
86 ?>