-
Notifications
You must be signed in to change notification settings - Fork 4
/
archive-usage-data.php
124 lines (114 loc) · 4.04 KB
/
archive-usage-data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* archive-usage-data.php - A PHP script to automate the downloading of
* usage data from Cloudtrax.com for archival purposes.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* @version 0.1, November 20, 2012
* @author Peter Rukavina <[email protected]>
* @copyright Copyright © 2012, Prince Street Home and School
* @license http://www.fsf.org/licensing/licenses/gpl.txt GNU Public License
*/
/**
* User-configurable settings
*/
// Username and password for cloudtrax.com
$username = "";
$password = "";
// The internal Cloudtrax.com name for your network.
$network = "";
// The name of the data file to archive into
$logfile = "./cloudtrax.log";
// URLs at CloudTrax.com -- you shouldn't really need to change these.
$cloudtrax_base = "https://cloudtrax.com/";
$loginurl = "dashboard.php";
$dataurl = "nodes_attnt2.php";
/**
* Login to Cloudtrax.com, saving cookies to a temporary file.
* This has the effect of saving a PHP session ID cookie, which is
* later sent *back* to authenticate future requests.
*/
exec("curl -s -L -e $cloudtrax_base$loginurl -c /tmp/cloudtrax-cookies.txt -X POST -d 'account=" . urlencode($username) . "&password=" . urlencode($password) ."&edit=Login' $cloudtrax_base$loginurl");
/**
* Download the data from Cloudtrax.com.
*/
$ch = curl_init("$cloudtrax_base$dataurl?id=" . urlencode($network) . "&showall=1&details=0");
curl_setopt($ch,CURLOPT_COOKIEFILE,"/tmp/cloudtrax-cookies.txt");
curl_setopt($ch,CURLOPT_COOKIEJAR,"/tmp/cloudtrax-cookies.txt");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$html = curl_exec($ch);
curl_close($ch);
/**
* Take the HTML we downloaded and parse the DOM, looking at the cells
* of the table with id of 'mytable'.
*/
$dom = new DOMDocument();
//$dom->validateOnParse = true;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$table = $xpath->query("//*[@id='mytable']")->item(0);
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $rowkey => $row) {
$cells = $row->getElementsByTagName('td');
foreach ($cells as $cellkey => $cell) {
$data[$rowkey][$cellkey] = innerHTML($cell);
}
}
/**
* Write the data out to a log file.
* Obviously you could output elsewhere too -- to MySQL or SQLite, or...
*/
$fp = fopen($logfile,"a");
foreach ($data as $key => $value) {
list($id,$label) = explode("<br></br>",$data[$key][1]);
$id = strip_tags($id);
$users = $data[$key][2];
list($down,$up) = explode("<br></br>",$data[$key][3]);
fwrite($fp,strftime("%Y-%m-%d %H:%M:%S") . "\t" . $id . "\t" . $label . "\t" . $users . "\t" . $down . "\t" . $up . "\n");
}
fclose($fp);
/**
* Helper function from http://kuttler.eu/post/php-innerhtml/
* Allows the innerHTML of a table cell to be extracted.
*/
function innerHTML( $contentdiv ) {
$r = '';
$elements = $contentdiv->childNodes;
foreach( $elements as $element ) {
if ( $element->nodeType == XML_TEXT_NODE ) {
$text = $element->nodeValue;
$r .= $text;
}
// FIXME we should return comments as well
elseif ( $element->nodeType == XML_COMMENT_NODE ) {
$r .= '';
}
else {
$r .= '<';
$r .= $element->nodeName;
if ( $element->hasAttributes() ) {
$attributes = $element->attributes;
foreach ( $attributes as $attribute )
$r .= " {$attribute->nodeName}='{$attribute->nodeValue}'" ;
}
$r .= '>';
$r .= innerHTML( $element );
$r .= "</{$element->nodeName}>";
}
}
return $r;
}