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