2
2
<?php
3
3
4
4
// Use http_proxy environment variable
5
- stream_context_set_default (array ('http ' => array (
6
- 'proxy ' => str_replace ("http:// " , "tcp:// " , getenv ('http_proxy ' )),
7
- 'request_fulluri ' => true
8
- )));
5
+ if ($ proxy = str_replace ("http:// " , "tcp:// " , getenv ('http_proxy ' )))
6
+ stream_context_set_default (array ('http ' => array ('proxy ' => $ proxy ,'request_fulluri ' => true )));
9
7
10
8
$ quotes = new BashOrgQuotes ();
11
9
$ quote = $ quotes ->getQuote ();
12
10
$ quote = $ quotes ::cleanupQuote ($ quote );
13
11
echo $ quote ."\n" ;
14
12
15
- class Quotes {
13
+ /**
14
+ * Quotes class
15
+ *
16
+ * A generic class for fetching quotes
17
+ */
18
+ abstract class Quotes {
16
19
17
20
private $ quotefile = '' ; // Quote cache location
18
21
private $ quotes = array (); // Quotes
19
22
23
+ /**
24
+ * Class constructur
25
+ *
26
+ * Opens the quote cache.
27
+ */
20
28
function __construct () {
21
29
$ this ->quotefile = getenv ('HOME ' )."/.bashqdb " ;
22
30
$ this ->quotefp = fopen ($ this ->quotefile ,"c+ " );
@@ -26,13 +34,21 @@ class Quotes {
26
34
$ this ->cacheRead ();
27
35
}
28
36
37
+ /**
38
+ * Class destructor
39
+ *
40
+ * Fills the cache (if needed), and writes it back to disk.
41
+ */
29
42
function __destruct () {
30
43
$ this ->fillIfEmpty ();
31
44
$ this ->cacheWrite ();
32
45
flock ($ this ->quotefp , LOCK_UN );
33
46
fclose ($ this ->quotefp );
34
47
}
35
48
49
+ /**
50
+ * Read the cache contents
51
+ */
36
52
function cacheRead () {
37
53
$ cache = file ($ this ->quotefile , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT );
38
54
if ($ cache ) {
@@ -43,29 +59,43 @@ class Quotes {
43
59
}
44
60
}
45
61
62
+ /**
63
+ * Write the cache contents to disk
64
+ */
46
65
function cacheWrite () {
47
66
if (FALSE === file_put_contents ($ this ->quotefile , implode ("\n" , $ this ->quotes ))) {
48
67
throw new Exception ('Could not write quote cache ' );
49
68
}
50
69
}
51
70
71
+ /**
72
+ * Checks if the cache is empty, and if so, fills it.
73
+ */
52
74
function fillIfEmpty () {
53
75
if (!$ this ->quotes )
54
76
$ this ->quotes = $ this ->fetchQuotes ();
55
77
}
56
78
79
+ /**
80
+ * Get a single quote
81
+ */
57
82
function getQuote () {
58
83
$ quote = array_pop ($ this ->quotes );
59
- $ this ->fillIfEmpty ();
60
84
if (!$ quote ) { // This is not good enough error handling
61
- $ quote = array_pop ($ this ->quotes );
62
85
$ this ->fillIfEmpty ();
86
+ $ quote = array_pop ($ this ->quotes );
63
87
}
64
88
return $ quote ;
65
89
}
66
90
}
67
91
92
+ /**
93
+ * Class for handling quotes from bash.org
94
+ *
95
+ * Implementation of class Quotes
96
+ */
68
97
class BashOrgQuotes extends Quotes {
98
+
69
99
function fetchQuotes () { // */p[@class='qt']
70
100
$ matches = array ();
71
101
$ source = file_get_contents ("http://bash.org/?random1 " );
@@ -84,5 +114,5 @@ class BashOrgQuotes extends Quotes {
84
114
return $ quote ;
85
115
}
86
116
}
87
- // http://www.qdb.us/api
117
+
88
118
?>
0 commit comments