1 |
aramv |
1277 |
<?php |
2 |
|
|
class Trunk { |
3 |
|
|
public $trunk_page_url, // the url of the cricket page for this trunk |
4 |
|
|
$metatrunk_page_url, // the url of a page where the constituent links of this trunk can be found |
5 |
|
|
$name, // friendly name of the trunk |
6 |
|
|
$constituents = array(), // an array of links that compose the current link |
7 |
|
|
$daily_image_url, // the url to the daily graph image |
8 |
|
|
$weekly_image_url, // the url to the weekly graph image |
9 |
aramv |
1288 |
$random_token, // a random token, to ensure images are fresh |
10 |
|
|
$is_constituent; |
11 |
aramv |
1277 |
|
12 |
|
|
public function __construct($trunk_page_url, $metatrunk_page_url, $is_constituent = false){ |
13 |
|
|
if($this->is_relative_url($trunk_page_url)){ |
14 |
|
|
$this->trunk_page_url = $this->get_absolute_url() . $trunk_page_url; |
15 |
|
|
} else { |
16 |
|
|
$this->trunk_page_url = $trunk_page_url; |
17 |
|
|
$this->get_cricket_images_from_url($trunk_page_url); |
18 |
|
|
} |
19 |
aramv |
1288 |
|
20 |
aramv |
1277 |
$this->metatrunk_page_url = $metatrunk_page_url; |
21 |
|
|
$this->random_token = rand(); |
22 |
aramv |
1288 |
$this->is_constituent = $is_constituent; |
23 |
|
|
if(!$this->is_constituent){ |
24 |
aramv |
1277 |
$this->find_constituents(); |
25 |
|
|
} |
26 |
aramv |
1288 |
|
27 |
|
|
$this->name = $this->name_from_url($trunk_page_url); |
28 |
aramv |
1277 |
} |
29 |
|
|
|
30 |
|
|
public function to_html(){ |
31 |
aramv |
1657 |
$html_string = '<h2><a href=' . $this->trunk_page_url . '">' . $this->name . '</a></h2>'; |
32 |
aramv |
1277 |
if(!empty($this->constituents)){ |
33 |
aramv |
1657 |
$html_string .= '<div id="constituents_div"><ul>'; |
34 |
aramv |
1277 |
foreach($this->constituents as $index => $constituent){ |
35 |
aramv |
1656 |
$html_string .= '<li><a href="' . $constituent->trunk_page_url . '" alt="' . $constituent->name . '" onmouseover="switch_graphs(this.href)"><b>' . $constituent->name . '</b></li>'; |
36 |
aramv |
1277 |
} |
37 |
aramv |
1657 |
$html_string .= '</ul></div>'; |
38 |
aramv |
1277 |
} |
39 |
aramv |
1657 |
$html_string .= '<div id="graph_div"><p>'; |
40 |
aramv |
1279 |
if($this->daily_image_url){ |
41 |
aramv |
1656 |
$html_string .= '<a id="daily_graph_a" href="' . $this->trunk_page_url . '" title="' . $this->name . '"><img id="daily_graph_i" alt="' . $this->name . '" src="' . $this->daily_image_url() . '"/></a><br/>'; |
42 |
aramv |
1279 |
} |
43 |
|
|
if($this->weekly_image_url){ |
44 |
aramv |
1656 |
$html_string .= '<a id="weekly_graph_a" href="' . $this->trunk_page_url . '" title="' . $this->name. '"><img id="weekly_graph_i" alt="' . $this->name . '" src="' . $this->weekly_image_url() . '"/></a>'; |
45 |
aramv |
1279 |
} |
46 |
aramv |
1657 |
$html_string .= '</p></div>'; |
47 |
aramv |
1277 |
return $html_string; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
public function daily_image_url(){ |
51 |
|
|
return $this->daily_image_url . $this->random_token; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public function weekly_image_url(){ |
55 |
|
|
return $this->weekly_image_url . $this->random_token; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/* returns the two key components from the url, separated by <-> delimiter */ |
59 |
|
|
private function name_from_url($url){ |
60 |
aramv |
1288 |
$name = ""; |
61 |
aramv |
1277 |
$name_parts = split("%2F", CricketSpider::code_from_url($url)); |
62 |
|
|
if(@$name_parts[1]){ |
63 |
aramv |
1288 |
if($this->is_constituent){ |
64 |
|
|
$name = $name_parts[1]; |
65 |
|
|
} else { |
66 |
|
|
$name = $name_parts[0] . " ↔ " . $name_parts[1]; |
67 |
|
|
} |
68 |
aramv |
1277 |
} else { |
69 |
aramv |
1289 |
$url_parts = array(); |
70 |
|
|
preg_match("/\/([^\/]+).html/", $url, $url_parts); |
71 |
aramv |
1288 |
$name = $url_parts[1]; |
72 |
aramv |
1277 |
} |
73 |
aramv |
1288 |
return $name; |
74 |
aramv |
1277 |
} |
75 |
|
|
|
76 |
|
|
private function find_constituents(){ |
77 |
aramv |
1288 |
$constituent_urls = CricketSpider::get_urls_from_html_file($this->metatrunk_page_url); |
78 |
|
|
foreach($constituent_urls[3] as $index => $url){ |
79 |
|
|
$this->constituents[] = new Trunk($constituent_urls[3][$index], $constituent_urls[2][$index], true); |
80 |
aramv |
1277 |
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
private function is_relative_url($url){ |
84 |
|
|
return !stristr($url, "http://"); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/* will set the daily_image_url and weekly_image_url if the provided url points to a remote page */ |
88 |
|
|
private function get_cricket_images_from_url($url){ |
89 |
|
|
$cricket_images_url_cache = CricketSpider::$cache_dir . '/' . md5($url); |
90 |
|
|
if(CricketSpider::probe_cache($cricket_images_url_cache)){ |
91 |
|
|
$data = unserialize(file_get_contents($cricket_images_url_cache)); |
92 |
|
|
} else { |
93 |
|
|
$local_urls = array(); |
94 |
|
|
$page = ""; |
95 |
|
|
/* apparently cricket checks the UA string and tries to display GIFs to wget, which don't work for some reason */ |
96 |
|
|
/* image links */ |
97 |
|
|
$page = shell_exec("wget -O- --user-agent=Firefox " . escapeshellarg($url)); |
98 |
aramv |
1656 |
preg_match_all('/<img src="(.*);rand=\d*".+/', $page, $local_urls); |
99 |
aramv |
1277 |
/* $local_urls holds matched items, $local_urls[1] holds the matched part between ()s. |
100 |
|
|
$local_urls[1][0] is the daily graph, $local_urls[1][1] is the weekly graph */ |
101 |
|
|
/* new_url is an array of: |
102 |
|
|
url to page |
103 |
|
|
url to graph image |
104 |
|
|
name of the graph |
105 |
|
|
*/ |
106 |
|
|
$data = array("http://www.dutchgrid.nl/ndpf/cricket/" . $local_urls[1][0] . ";rand=", "http://www.dutchgrid.nl/ndpf/cricket/" . $local_urls[1][1] . ";rand="); |
107 |
|
|
file_put_contents($cricket_images_url_cache, serialize($data)); |
108 |
|
|
} |
109 |
|
|
$this->daily_image_url = $data[0]; |
110 |
|
|
$this->weekly_image_url = $data[1]; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
private function get_absolute_url(){ |
114 |
|
|
$full_url = 'http'; |
115 |
|
|
$script_name = ''; |
116 |
|
|
if(isset($_SERVER['REQUEST_URI'])) { |
117 |
|
|
$script_name = $_SERVER['REQUEST_URI']; |
118 |
|
|
} else { |
119 |
|
|
$script_name = $_SERVER['PHP_SELF']; |
120 |
|
|
if($_SERVER['QUERY_STRING']>' ') { |
121 |
|
|
$script_name .= '?'.$_SERVER['QUERY_STRING']; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
/* strip index.php from script name */ |
126 |
|
|
|
127 |
|
|
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') { |
128 |
|
|
$full_url .= 's'; |
129 |
|
|
} |
130 |
|
|
$full_url .= '://'; |
131 |
|
|
if($_SERVER['SERVER_PORT']!='80') { |
132 |
|
|
$full_url .= |
133 |
|
|
$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$script_name; |
134 |
|
|
} else { |
135 |
|
|
$full_url .= $_SERVER['HTTP_HOST'].$script_name; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/* strip trailing slash */ |
139 |
|
|
return $full_url; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
|
|
//function make_html_snippets($data, $token){ |
144 |
|
|
//$html_string = '<h2>Combined trunk</h2><p><ul id="dock-container">'; |
145 |
|
|
//foreach($data as $index => $trunk_data){ |
146 |
|
|
//$html_string .= '<li><a href="' . $trunk_data[0] . '" title ="' . $trunk_data[3] . '"><img alt="' . $trunk_data[3] . '" src="' . $trunk_data[1] . '" class="thumbnail"/></a></li>'; |
147 |
|
|
//} |
148 |
|
|
//$html_string .= '</p></ul>'; |
149 |
|
|
//return $html_string; |
150 |
|
|
//} |
151 |
|
|
|
152 |
|
|
} |
153 |
|
|
?> |
154 |
|
|
|