2
2
<?php
3
3
4
4
// Use http_proxy environment variable
5
+
5
6
if ($ proxy = str_replace ("http:// " , "tcp:// " , getenv ('http_proxy ' )))
6
- stream_context_set_default (array ('http ' => array ('proxy ' => $ proxy ,'request_fulluri ' => true )));
7
+ stream_context_set_default (array ('http ' => array ('proxy ' => $ proxy ,'request_fulluri ' => true )));
8
+
9
+ $ opts = getopt ('p:hf ' ,array ('help ' ));
10
+ $ provider = (isset ($ opts ['p ' ]) ? $ opts ['p ' ] : 'bash ' );
11
+
12
+ if (isset ($ opts ['h ' ]) || isset ($ opts ['help ' ])) {
13
+ echo "-h / --help This helptext \n" ;
14
+ echo "-p provider Choose provider (bash, qdb or ingdal) \n" ;
15
+ return ;
16
+ }
7
17
8
- $ quotes = new BashOrgQuotes ();
18
+ if (isset ($ opts ['f ' ]) || isset ($ opts ['flush ' ])) {
19
+ unlink (sys_get_temp_dir () . PATH_SEPARATOR . "bashquote_cache " );
20
+ }
21
+
22
+ $ quotes = new Quotes ($ provider );
9
23
$ quote = $ quotes ->getQuote ();
10
24
echo $ quote ."\n" ;
11
25
@@ -14,20 +28,24 @@ echo $quote."\n";
14
28
*
15
29
* A generic class for fetching quotes
16
30
*/
17
- abstract class Quotes {
31
+ class Quotes {
18
32
19
33
private $ quotefile = '' ; // Quote cache location
20
34
private $ quotes = array (); // Quotes
35
+ private $ provider = '' ;
21
36
22
37
/**
23
38
* Class constructur
24
39
*
40
+ * @param string $provider
41
+ * @throws Exception
25
42
* Opens the quote cache.
26
43
*/
27
- function __construct () {
44
+ function __construct ($ provider ) {
28
45
if (file_exists (getenv ('HOME ' )."/.bashqdb " )) // Clean up old style cache
29
46
unlink (getenv ('HOME ' )."/.bashqdb " );
30
47
48
+ $ this ->provider = $ provider ;
31
49
$ this ->quotefile = sys_get_temp_dir () . PATH_SEPARATOR . "bashquote_cache " ;
32
50
$ this ->quotefp = fopen ($ this ->quotefile ,"c+ " );
33
51
// Only one instance accessing the cache at a time
@@ -74,49 +92,77 @@ abstract class Quotes {
74
92
* Checks if the cache is empty, and if so, fills it.
75
93
*/
76
94
function fillIfEmpty () {
77
- if (!$ this ->quotes )
78
- $ this ->quotes = $ this ->fetchQuotes ();
95
+ if (!$ this ->quotes [ $ this -> provider ] )
96
+ $ this ->quotes [ $ this -> provider ] = $ this ->fetchQuotes ();
79
97
}
80
98
81
99
/**
82
100
* Get a single quote
83
101
*/
84
102
function getQuote () {
85
- $ quote = array_pop ($ this ->quotes );
103
+ $ quote = NULL ;
104
+ if (isset ($ this ->quotes [$ this ->provider ])) {
105
+ $ quote = array_pop ($ this ->quotes [$ this ->provider ]);
106
+ }
86
107
if (!$ quote ) { // This is not good enough error handling
87
108
$ this ->fillIfEmpty ();
88
- $ quote = array_pop ($ this ->quotes );
109
+ $ quote = array_pop ($ this ->quotes [ $ this -> provider ] );
89
110
}
90
111
return $ quote ;
91
112
}
92
113
93
- }
114
+ /**
115
+ * Fetch and parse a batch of quotes
116
+ */
117
+ function fetchQuotes () {
118
+ $ conf = self ::getProviderConfig ($ this ->provider );
119
+ libxml_use_internal_errors (TRUE );
120
+ $ doc = new DOMDocument ;
121
+ $ doc ->loadHTMLFile ($ conf ['url ' ]);
122
+ $ xpath = new DOMXPath ($ doc );
123
+ $ elements = $ xpath ->query ($ conf ['xpath ' ]);
124
+ $ quotes = array ();
125
+ foreach ($ elements as $ element ) {
126
+ print_r ($ element );
127
+ $ quote = trim ($ element ->textContent );
128
+ switch ($ conf ['type ' ]) {
129
+ case 'pre ' :
130
+ $ quote = html_entity_decode ($ quote );
131
+ break ;
132
+ case 'html ' :
133
+ $ quote = str_replace (array ("\n" , "\r" ), "" , $ quote );
134
+ $ quote = html_entity_decode ($ quote );
135
+ $ quote = str_replace (array ("<br /> " , "<br> " ), "\n" , $ quote );
136
+ }
137
+ $ quotes [] = $ quote ;
138
+ }
139
+ return $ quotes ;
140
+ }
94
141
95
- /**
96
- * Class for handling quotes from bash.org
97
- *
98
- * Implementation of class Quotes
99
- */
100
- class BashOrgQuotes extends Quotes {
101
-
102
- /**
103
- * Fetch and parse a batch of random quotes from bash.org
104
- */
105
- function fetchQuotes () {
106
- libxml_use_internal_errors ( TRUE );
107
- $ doc = new DOMDocument ;
108
- $ doc -> loadHTMLFile ( " http://bash.org/?random1 " );
109
- $ xpath = new DOMXPath ( $ doc );
110
- $ elements = $ xpath -> query ( ' //p[@class="qt"] ' );
111
- $ quotes = array ();
112
- foreach ( $ elements as $ element ) {
113
- $ quote = $ element -> textContent ;
114
- $ quote = html_entity_decode ( $ quote );
115
- $ quote = str_replace ( " <br /> " , "\n" , $ quote );
116
- $ quotes [] = $ quote ;
117
- }
118
- return $ quotes ;
119
- }
142
+ /**
143
+ * Get provider definitions
144
+ */
145
+ static function getProviderConfig ( $ provider ) {
146
+ $ configs = array (
147
+ ' bash ' => array (
148
+ ' url ' => ' http://bash.org/?random1 ' ,
149
+ ' xpath ' => ' //p[@class="qt"] ' ,
150
+ ' type ' => ' pre '
151
+ ),
152
+ ' ingdal ' => array (
153
+ ' url ' => ' http://bash.ingdal.net/?random2 ' ,
154
+ ' xpath ' => ' //div[@class="quote_quote"] ' ,
155
+ ' type ' => ' pre '
156
+ ),
157
+ ' qdb ' => array (
158
+ ' url ' => ' http://qdb.us/qdb.xml?action=random&fixed=0&client=bashquote ' ,
159
+ ' xpath ' => ' //description ' ,
160
+ ' type ' => ' html '
161
+ ),
162
+ );
163
+ if ( isset ( $ configs [ $ provider ]))
164
+ return $ configs [ $ provider ];
165
+ return NULL ;
166
+ }
120
167
}
121
168
122
- ?>
0 commit comments