]> git.mxchange.org Git - friendica-addons.git/blob - convpath/convpath.php
0aaeb5f92e7dddd92a8f32c6c0240e5ef6edd17f
[friendica-addons.git] / convpath / convpath.php
1 <?php
2 /**
3  * Name: Convert Paths
4  * Description: Converts all internal paths according to the current scheme (http or https)
5  * Version: 1.0
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  * Status: Unsupported
8  */
9
10 function convpath_install() {
11         register_hook('page_end', 'addon/convpath/convpath.php', 'convpath_page_end');
12         register_hook('page_header', 'addon/convpath/convpath.php', 'convpath_page_header');
13         register_hook('ping_xmlize',  'addon/convpath/convpath.php', 'convpath_ping_xmlize_hook');
14         register_hook('prepare_body', 'addon/convpath/convpath.php', 'convpath_prepare_body_hook');
15         register_hook('display_item', 'addon/convpath/convpath.php', 'convpath_display_item_hook');
16 }
17
18
19 function convpath_uninstall() {
20         unregister_hook('page_end', 'addon/convpath/convpath.php', 'convpath_page_end');
21         unregister_hook('page_header', 'addon/convpath/convpath.php', 'convpath_page_header');
22         unregister_hook('ping_xmlize',  'addon/convpath/convpath.php', 'convpath_ping_xmlize_hook');
23         unregister_hook('prepare_body', 'addon/convpath/convpath.php', 'convpath_prepare_body_hook');
24         unregister_hook('display_item', 'addon/convpath/convpath.php', 'convpath_display_item_hook');
25 }
26
27 function convpath_ping_xmlize_hook(&$a, &$o) {
28         $o["photo"] = convpath_url($a, $o["photo"]);
29 }
30
31 function convpath_page_header(&$a, &$o){
32         $o = convpath_convert($o);
33 }
34
35 function convpath_page_end(&$a, &$o){
36         $o = convpath_convert($o);
37         if (isset($a->page['aside']))
38                 $a->page['aside'] = convpath_convert($a->page['aside']);
39 }
40
41 function convpath_prepare_body_hook(&$a, &$o) {
42         $o["html"] = convpath_convert($o["html"]);
43 }
44
45 function convpath_display_item_hook(&$a, &$o) {
46         if (isset($o["output"])) {
47                 if (isset($o["output"]["thumb"]))
48                         $o["output"]["thumb"] = convpath_url($a, $o["output"]["thumb"]);
49                 if (isset($o["output"]["author-avatar"]))
50                         $o["output"]["author-avatar"] = convpath_url($a, $o["output"]["author-avatar"]);
51                 if (isset($o["output"]["owner-avatar"]))
52                         $o["output"]["owner-avatar"] = convpath_url($a, $o["output"]["owner-avatar"]);
53                 if (isset($o["output"]["owner_photo"]))
54                         $o["output"]["owner_photo"] = convpath_url($a, $o["output"]["owner_photo"]);
55         }
56 }
57
58 function convpath_url($a, $path) {
59         if ($path == "")
60                 return("");
61
62         $ssl = (substr($a->get_baseurl(), 0, 8) == "https://");
63
64         if ($ssl) {
65                 $search = "http://".$a->get_hostname();
66                 $replace = "https://".$a->get_hostname();
67         } else {
68                 $search = "https://".$a->get_hostname();
69                 $replace = "http://".$a->get_hostname();
70         }
71
72         $path = str_replace($search, $replace, $path);
73
74         return($path);
75 }
76
77 /*
78 Converts a given path according to the current scheme
79 */
80 function convpath_convert($path) {
81         global $a;
82
83         if ($path == "")
84                 return("");
85
86         $ssl = (substr($a->get_baseurl(), 0, 8) == "https://");
87
88         if ($ssl) {
89                 $search = "http://".$a->get_hostname();
90                 $replace = "https://".$a->get_hostname();
91         } else {
92                 $search = "https://".$a->get_hostname();
93                 $replace = "http://".$a->get_hostname();
94         }
95         $searcharr = array("src='".$search, 'src="'.$search);
96         $replacearr = array("src='".$replace, 'src="'.$replace);
97         $path = str_replace($searcharr, $replacearr, $path);
98
99         //$path = str_replace($search, $replace, $path);
100
101         return($path);
102 }