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