b802c396f360bb58f5a0918ad7dcc3e9a100f60f
[infodrom.org/www.zeitungsliste.de] / lib / core.inc
1 <?php
2
3 function db_connect()
4 {
5   global $cfg;
6
7   $dsn = "dbname=".$cfg['dbname'];
8   if (isset($cfg['dbhost'])) $dsn .= " host=".$cfg['dbhost'];
9   if (isset($cfg['dbport'])) $dsn .= " port=".$cfg['dbport'];
10   if (isset($cfg['dbuser'])) $dsn .= " user=".$cfg['dbuser'];
11   if (isset($cfg['dbpass'])) $dsn .= " password=".$cfg['dbpass'];
12
13   $dbh = pg_pconnect ($dsn)
14     or carp("Unable to connect to SQL server");
15   pg_exec ($dbh, "SET DateStyle='ISO'");
16
17   return $dbh;
18 }
19
20 function db_query($query)
21 {
22   global $cfg;
23
24   // error_log($query);
25   $sth = pg_exec ($cfg['dbh'], $query);
26
27   if ($sth === false) {
28     error_log ($query);
29     error_log (pg_last_error($cfg['dbh']));
30     return false;
31   }
32
33   return $sth;
34 }
35
36 function db_last_id($table, $column)
37 {
38   $query = sprintf ("SELECT currval('%s_%s_seq')", $table, $column);
39
40   $sth =  db_query ($query);
41
42   if (!$sth)
43     return false;
44
45   if (pg_num_rows($sth) == 0) {
46     error_log ("$query resulted in an empty set");
47     return false;
48   }
49
50   $row = pg_fetch_row($sth, 0);
51
52   return $row[0];
53 }
54
55 // preserve <p></p>, <b></b>, <em></em>, <br>, <a href></a>
56 function string_sanitise($text)
57 {
58   $ntext = preg_replace (array('/<a\s+href=["\']?([^>"\']*)["\']?>([^<]*)<\/a>/i'),
59                          array('[[$1][$2]]'),
60                          $text);
61
62   $ntext = htmlspecialchars($ntext, ENT_QUOTES);
63
64   $ntext = preg_replace(array('/&lt;(\/?(p|br|b|em))&gt;/i',
65                               '/\[\[([^\]]+)\]\[([^\]]+)\]\]/'),
66                         array('<$1>','<a href="$1">$2</a>'),
67                         $ntext);
68
69   return $ntext;
70 }
71
72 function basepath()
73 {
74   global $cfg;
75   global $_SERVER;
76
77   $pos = strpos(strtolower($cfg['home']), strtolower($_SERVER['SERVER_NAME']));
78   $base = substr($cfg['home'], $pos+strlen($_SERVER['SERVER_NAME']));
79
80   $pos = strpos($_SERVER['REQUEST_URI'], "?");
81   if ($pos === false)
82     $uri = $_SERVER['REQUEST_URI'];
83   else 
84     $uri = substr($_SERVER['REQUEST_URI'], 0, $pos);
85
86   if (strpos($uri, $base) === 0) {
87     $cfg['path'] = substr($uri, strlen($base));
88     $pos = strrpos($cfg['path'], '/');
89     if ($pos !== false) {
90       $cfg['dir'] = substr($cfg['path'], 0, $pos);
91     }
92     return str_repeat("../", substr_count($cfg['path'], "/"));
93   }
94 }
95
96 function logged_in()
97 {
98   global $_SESSION;
99
100   return isset($_SESSION['uid']);
101 }
102
103 function javascript_ok()
104 {
105   global $_SESSION;
106
107   if (!logged_in())
108     return true;
109
110   if ($_SESSION['javascript'] == true)
111     return true;
112
113   return false;
114 }
115
116 function is_spider()
117 {
118   global $_SERVER;
119   global $_SESSION;
120
121   if (isset($_SESSION['uid']))
122     if (isset($_SESSION['robot']))
123       return $_SESSION['robot'];
124
125   $known_spiders = array('Yahoo! Slurp',
126                          'Googlebot',
127                          'Mediapartners-Google',
128                          'VoilaBot',
129                          'Gigabot/3.0',
130                          'Speedy Spider',
131                          'LinkWalker/2.0',
132                          'proximic',
133                          'Yeti/1.0',
134                          'Eurobot/1.0',
135                          'MnoGoSearch/',
136                          'ia_archiver',
137                          'Seekbot/1.0',
138                          'MyEngines-Bot',
139                          'larbin_',
140                          'findlinks/1',
141                          'holmes/3.12',
142                          'NoteworthyBot/0.1',
143                          'Eurosoft-Bot',
144                          'Eurobot/1',
145                          'Yandex',
146                          'msnbot',
147                          'Twiceler-0.9',
148                          'ScoutJet',
149                          'penthesilea/0.3',
150                          'VisBot/2.0',
151                          'Netluchs/Nutch-1.0',
152                          'Yanga WorldSearch Bot',
153                          'Semager',
154                          'DotBot',
155                          'Baiduspider',
156                          'MJ12bot/',
157                          'Yahoo-MMCrawler',
158                          'Charlotte/1.1',
159                          'Gaisbot/3.0',
160                          'psbot/0.1',
161                          'Ask Jeeves/Teoma',
162                          'SapphireWebCrawler/Nutch',
163                          'kalooga/KaloogaBot',
164                          'SimilarPages/Nutch-1.0-dev',
165                          'Cogentbot/1.',
166                          'CatchBot/1.0',
167                          'GingerCrawler/1.0',
168                          'yacybot',
169                          'www.yacy.net',
170                          'Nutch-1.0',
171                          'Tagoobot/3.0',
172                          'SapphireWebCrawler/1.0',
173                          'BotOnParade',
174                          'SurveyBot/2.3',
175                          'XmarksFetch/1.0',
176                          'spbot/',
177                          'TinEye/1.1',
178                          'TurnitinBot/2.1',
179                          'CligooRobot/1.0',
180                          'libwww-perl',
181                          'Wget',
182                          'Python-urllib',
183                          'CamontSpider/1.0',
184                          'TwengaBot-Discover',
185                          'Hailoobot/1.2',
186                          'bingbot/2.0',
187                          'Exabot/3.0',
188                          'spbot/2.1',
189                          'Search17Bot/1',
190                          'Linguee Bot',
191                          'findlinks/2',
192                          'SiteBot/0.1',
193                          'Purebot/1.1',
194                          'Ezooms/1.0',
195                          'discobot/1.1',
196                          'AhrefsBot/1.0',
197                          'suggybot v0.01a',
198                          'DomainCrawler/2.0',
199                          'Plukkie/1',
200                          '/Nutch-',
201                          'aiHitBot/1.0',
202                          'AhrefsBot/',
203                          'Crawler',
204                          'Acoon',
205                          'aiHitBot',
206                          'SeznamBot',
207                          'CareerBot/1',
208                          'SiteExplorer/1',
209                          'discoverybot/2.0',
210                          'SemrushBot/',
211                          'archive.org_bot',
212                          'waybackarchive.org/1.0',
213                          'spbot/4.0',
214                          'Mail.RU_Bot/2.0',
215                          'linkdexbot/2.0',
216                          'webmeup-crawler.com',
217                          'meanpathbot/',
218                          'SearchmetricsBot',
219                          'publiclibraryarchive.org/',
220                          'memoryBot/1',
221                          'Lipperhey SEO Service',
222                          'Sitedomain-Bot',
223                          'seoscanners.net/',
224                          'Qwantify/',
225                          'WBSearchBot/',
226                          'Seobility',
227                          'MetaJobBot',
228                          'BellPagesCA/1.0',
229                          'Cityreview Robot');
230
231   foreach ($known_spiders as $spider_id) {
232     if (strpos($_SERVER['HTTP_USER_AGENT'], $spider_id) !== false) {
233       if (isset($_SESSION['uid']))
234         $_SESSION['robot'] = true;
235       return true;
236     }
237   }
238
239   if (isset($_SESSION['uid']))
240     $_SESSION['robot'] = false;
241   return false;
242 }
243
244 function is_admin()
245 {
246   global $_SESSION;
247
248   if (!array_key_exists('nickname', $_SESSION))
249     return false;
250
251   error_log( $_SESSION['nickname'] === 'Joey');
252   return $_SESSION['nickname'] === 'Joey';
253 }
254
255 function format_info_bookmarks()
256 {
257   global $_SESSION;
258   global $cfg;
259
260   if (!isset($_SESSION['uid']))
261     return false;
262
263   $query = sprintf("SELECT zeitung,name FROM bookmarks " .
264                    "JOIN zeitungen ON zeitungen.id = zeitung " .
265                    "WHERE uid = %d " .
266                    "ORDER BY priority,name LIMIT 20",
267                    $_SESSION['uid']);
268
269   $sth = db_query($query);
270
271   if ($sth === false || pg_num_rows ($sth) == 0)
272     return false;
273
274   $ret = '<p><ul>';
275   for ($n=0; $n < pg_num_rows ($sth); $n++) {
276     $row = pg_fetch_array ($sth, $n);
277     $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a></li>',
278                     $cfg['basepath'], $row['zeitung'], $row['name']);
279   }
280   $ret .= '</ul></p>';
281
282   return $ret;
283 }
284
285 ?>