/
generalClass.php
executable file
·497 lines (450 loc) · 16.2 KB
/
generalClass.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
defined('SITEROOT') or define('SITEROOT', './');
//used in $_SESSION['key'];
defined('SEARCH') or define('SEARCH', 'search');
defined('ROWS_PER_PAGE') or define('ROWS_PER_PAGE', 25);
require_once(SITEROOT."configs/base.inc.php");
//global $config;
//echo "<pre>"; print_r($config); echo "</pre>";
class GeneralClass extends BaseClass
{
var $sid, $url, $self;
public function __construct($site_id) {
global $config;
parent::__construct();
$this -> sid = $site_id;
$this -> url = $_SERVER['PHP_SELF'];
$this -> self = basename($this -> url, '.php');
$this->template_dir = SITEROOT.'templates/default/';
$this->dbh = $this->mysql_connect_dixi();
$this->general = $config['d'];
$this->lang = $_SESSION[PACKAGE]['language'];
}
// 每次用户点击,breadcrumb 都应该重�?
function set_1_breadcrumb() {
unset($_SESSION[PACKAGE]['breadcrumb']);
$home = $this->lang=='English' ? 'Home' : '首页';
$_SESSION[PACKAGE]['breadcrumb'][] = array(
'link' => 'index.php',
'name' => $home
);
}
public function set_breadcrumb($breadcrumb)
{
$this->set_1_breadcrumb();
if(count($breadcrumb)>1) {
foreach($breadcrumb as $b)
$_SESSION[PACKAGE]['breadcrumb'][] = $b;
}
else {
array_push($_SESSION[PACKAGE]['breadcrumb'], array_pop($breadcrumb));
}
$this->__p($_SESSION[PACKAGE]['breadcrumb']);
}
//////////////// Category ////////////////
function get_menu_info($cate_id) {
$query = "select name, curl, description, frequency, tag from categories where cid=" . $cate_id;
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
$b = array();
// 逻辑:当面包屑为一时,表示只有一层,数组个数为一;当面包�?1时,表示有多层,数组个数>1
// 这里,要用array(array(..))来控制count()=1, 否则count()>1.
$b[] = array('name'=>$row['name'], 'active'=>1);
$this->set_breadcrumb($b);
return $row;
}
function get_category_list()
{
list($cate_id, $category) = array(0, '');
$ary = array();
$query = "select name, cid, description, frequency, tag from categories order by weight";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
if(!$cate_id && !$category) {
$cate_id = $row['cid'];
$category = $row['name'];
}
array_push($ary, $row);
}
mysql_free_result($res);
$b = array();
$b[] = array('name'=>$category, 'active'=>1);
$this->set_breadcrumb($b);
return $ary;
}
//////////////// Items ////////////////
function get_item_list($cid)
{
list($cate_id, $category, $item) = array(0, '', '');
$ary = array();
$query = "select name, iid, description, category, cid from items where cid=$cid order by weight;";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
if(!$cate_id && !$category && !$item) {
$cate_id = $row['cid'];
$category = $row['category'];
$item = $row['name'];
}
array_push($ary, $row);
}
mysql_free_result($res);
$b = array();
$b[] = array('name'=>$category, 'active'=>1);
//$b[] = array('name'=>$category, 'link'=>$this->general.'?cmenu='.$cate_id);
//$b[] = array('name'=>$item, 'active'=>1);
$this->set_breadcrumb($b);
return $ary;
}
function get_item_count() {
$ary = array();
$sql = "select iid, count(*) total from contents group by iid";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
return $ary;
}
function get_category_count() {
$ary = array();
$sql = "select cid, count(*) total from items group by cid";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
return $ary;
}
//////////////// Contents ////////////////
//上下文应该是同一个category或item下的所有内�?而不是所有的,连续的cid.
function get_content($cid) {
#$sql = "select cid, content, title, category, cate_id, item, iid from contents where cid=".$cid;
$sql = "select * from contents where cid=".$cid;
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
//添加面包屑功�?
$b = array();
$b[] = array('name'=>$row['category'], 'link'=>$this->general.'?cmenu='.$row['cate_id']);
$b[] = array('name'=>$row['item'], 'link'=>$this->general.'?iid='.$row['iid']);
$b[] = array('name'=>$row['title'], 'active'=>1);
$this->set_breadcrumb($b);
$this->update_total($cid);
return $row;
}
function get_content_previous($cid) {
$sql = "select cid, title from contents where cid < " . $cid . " order by cid desc limit 1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
return $row;
}
function get_content_next($cid) {
$sql = "select cid, title from contents where cid >".$cid. " order by cid limit 1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
return $row;
}
// 输出内容,并构建面包�?
function get_content_1($cid)
{
#$sql = "select content, title, cid, category, cate_id, item, iid from contents where cid=".$cid;
$sql = "select * from contents where cid=".$cid;
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
//添加面包屑功�?
$b = array();
$b[] = array('name'=>$row['category'], 'link'=>$this->general.'?cmenu='.$row['cate_id']);
$b[] = array('name'=>$row['item'], 'link'=>$this->general.'?iid='.$row['iid']);
$b[] = array('name'=>$row['title'], 'active'=>1);
$this->set_breadcrumb($b);
$this->update_total($cid);
return $row;
}
// and language=''
function get_contents_list($iid) {
$ary = array();
$sql = "select title, cid, category, cate_id, item, iid from contents where iid=".$iid . " order by cid desc";
$res = mysql_query($sql);
list($cate_id, $category, $item) = array(0, '', '');
$t = '<ul class="nav nav-pills nav-stacked">';
// $t .= '<li><a href="'.$config['general'].'?cid='.$row['cid'].'">'.$row['title']."</a></li>\n";
while($row = mysql_fetch_assoc($res)) {
if(!$cate_id && !$category && !$item) {
$cate_id = $row['cate_id'];
$category = $row['category'];
$item = $row['item'];
}
$t .= '<li><a href="'.$this->general.'?cid='.$row['cid'].'">'.$row['title']."</a></li>\n";
}
$t .= '</ul>';
mysql_free_result($res);
//添加面包屑功�?
$b = array();
$b[] = array('name'=>$category, 'link'=>$this->general.'?cmenu='.$cate_id);
$b[] = array('name'=>$item, 'active'=>1);
$this->set_breadcrumb($b);
return $t;
}
function set_keywords($key)
{
//将关键词写入keywords表�?
if($key!='') {
$user = isset($_SESSION[PACKAGE]['username']) ? $_SESSION[PACKAGE]['username'] : '';
if(empty($user)) $user = basename(__FILE__).', search';
$query = "INSERT INTO keywords (keyword,createdby, created) VALUES ".
"('".$key."', '".$user."', now()) ON DUPLICATE KEY UPDATE total=total+1";
mysql_query($query);
$query = "insert into tags (name, createdby, created) values " .
"('".$key."', '".$user."', now()) ON DUPLICATE KEY UPDATE total=total+1";
mysql_query($query);
}
return true;
}
function update_total($cid) {
$sql = "update contents set total=toal+1 where cid=".$cid;
mysql_query($sql);
}
function insert_comments()
{
$sql ="insert into comments(content, create_time, author, cid) values('" .
$_POST['fayan'] . "', now(), '" .
$_POST['username'] . "', " .
$_POST['cid'] . ")";
mysql_query($sql);
}
function get_comments($cid)
{
$ary = array();
//$sql = "select id, content, author, create_time, cid, area from comments where cid=".$cid." order by id desc";
$sql = "select id, content, author, create_time, cid, area from comments order by rand()";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
return $ary;
}
function select_contents_by_keyword($key)
{
$this->set_keywords($key);
$t=''; $name='';
if($this->lang=='English') {
$t = 'All Records';
$name = 'Search - ';
}
else {
$t = '所有记录';
$name = '搜索 - ';
}
$_SESSION[PACKAGE][SEARCH]['key'] = $key ? mysql_real_escape_string($key) : $t;
//添加面包屑功�?
$b = array();
$b[] = array('name'=>$name.$_SESSION[PACKAGE][SEARCH]['key'], 'active'=>1);
$this->set_breadcrumb($b);
//计算对于此关键词,总共多少记录�?
$total = $this->get_contents_count($key);
$total_pages = ceil($total/ROWS_PER_PAGE);
$_SESSION[PACKAGE][SEARCH]['total'] = $total;
$_SESSION[PACKAGE][SEARCH]['total_pages'] = $total_pages;
//第一页:
$page = 1;
$_SESSION[PACKAGE][SEARCH]['page'] = $page;
//当前从第几条记录开始显示�?
$row_no = 0;
//生成新的查询语句�?
$lang_case = " and language = '" . $this->lang . "' ";
/* select cid, title, date(created) as date, match(title, content) against('不理性行为' in boolean mode) as relevance
* from contents where match(title, content) against ('不理性行为' in boolean mode)
* and language = '中文' order by relevance desc limit 0,25
*
$sql = "select cid, title, date(created) as date,
MATCH(title, content) AGAINST('$key' in boolean mode) as relevancy
from contents
where MATCH(title, content) AGAINST('$key' in boolean mode) "
.$lang_case." order by relevancy desc";
*/
$sql = "select cid, title, date(created) as date from contents
where content like '%".$key ."%' "
. " or title like '%".$key ."%' "
.$lang_case." order by cid desc";
$_SESSION[PACKAGE][SEARCH]['sql'] = $sql;
$sql .= " limit " . $row_no . "," . ROWS_PER_PAGE;
$ary = array();
$res = mysql_query($sql);
// echo $sql;
while($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
//返回生成的结果�?
return $ary;
}
function get_contents_count($key)
{
$sql = "select count(*) from contents
where content like '%".$key ."%' " . " or title like '%".$key ."%' and language='" . $this->lang . "'";
$result = mysql_query($sql);
$num = mysql_fetch_row($result);
mysql_free_result($result);
return $num[0];
}
function select_contents_by_page()
{
//计算共有多少页?
$total_pages = isset($_SESSION[PACKAGE][SEARCH]['total_pages']) ? $_SESSION[PACKAGE][SEARCH]['total_pages'] : 1;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
if ($page > $total_pages) $page = $total_pages;
if ($page < 1) $page = 1;
$_SESSION[PACKAGE][SEARCH]['page'] = $page;
//当前从第几条记录开始显示�?
$row_no = ((int)$page-1)*ROWS_PER_PAGE;
//生成新的查询语句�?
if(preg_match("/limit/i", $_SESSION[PACKAGE][SEARCH]['sql']))
$_SESSION[PACKAGE][SEARCH]['sql'] = preg_replace("/limit.*$/i", '', $_SESSION[PACKAGE][SEARCH]['sql']);
$sql = $_SESSION[PACKAGE][SEARCH]['sql'];
$sql .= " limit " . $row_no . "," . ROWS_PER_PAGE;
$_SESSION[PACKAGE][SEARCH]['sql'] = $sql;
$ary = array();
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
//返回生成的结果�?
return $ary;
}
function draw()
{
$current_page = $_SESSION[PACKAGE][SEARCH]['page'] ? $_SESSION[PACKAGE][SEARCH]['page'] : 1;
$total_pages = $_SESSION[PACKAGE][SEARCH]['total_pages'] ? $_SESSION[PACKAGE][SEARCH]['total_pages'] : 0;
$links = array(); $queryURL = '';
if (count($_GET)) {
foreach ($_GET as $key => $value) {
if ($key != 'page') $queryURL .= '&'.$key.'='.$value;
}
}
if (($total_pages) > 1) {
if ($current_page != 1) {
$links[] = '<a href="?page=1'.$queryURL.'">«« 首页 </a>';
$links[] = '<a href="?page='.($current_page - 1).$queryURL.'">« 前页</a>';
}
for ($j = ($current_page-4); $j < ($current_page+4); $j++) {
if($j<1) continue;
if($j>$total_pages) break;
if ($current_page == $j) {
$links[] = '<a href="javascript:;">'.$j.'</a>';
} else {
$links[] = '<a href="?page='.$j.$queryURL.'">'.$j.'</a>';
}
}
if ($current_page < $total_pages) {
$links[] = '<a href="?page='.($current_page + 1).$queryURL.'"> 下页 » </a>';
$links[] = '<a href="?page='.($total_pages).$queryURL.'"> 末页 »» </a>';
}
return $links;
}
}
// deprecated.
function draw_old()
{
$current_page = $_SESSION[PACKAGE][SEARCH]['page'] ? $_SESSION[PACKAGE][SEARCH]['page'] : 1;
$total_pages = $_SESSION[PACKAGE][SEARCH]['total_pages'] ? $_SESSION[PACKAGE][SEARCH]['total_pages'] : 0;
$plinks = array(); $links = array(); $slinks = array(); $queryURL = '';
if (count($_GET)) {
foreach ($_GET as $key => $value) {
if ($key != 'page') $queryURL .= '&'.$key.'='.$value;
}
}
// $plinks[] = ' <a href="?page='.($current_page - 1).$queryURL.'">« 前页</a> ';
if (($total_pages) > 1) {
if ($current_page != 1) {
$plinks[] = ' <a href="?page=1'.$queryURL.'">«« 首页 </a> ';
}
for ($j = ($current_page-3); $j < ($current_page+3); $j++) {
if($j<1) continue;
if($j>$total_pages) break;
if ($current_page == $j) {
$links[] = ' <a class="selected">'.$j.'</a> ';
} else {
$links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> ';
}
}
// $slinks[] = ' <a href="?page='.($current_page + 1).$queryURL.'"> 下页 » </a> ';
if ($current_page < $total_pages) {
$slinks[] = ' <a href="?page='.($total_pages).$queryURL.'"> 最�?»» </a> ';
}
return implode(' ', $plinks).implode(' ', $links).implode(' ', $slinks);
}
}
# 随机从数据库中抽�?�?随即生成1-6个记�?
function get_rand_keywords() {
$ary = array();
$sql = "select keyword from keywords order by rand() limit 0, 4";
$res = mysql_query($sql);
while($row = mysql_fetch_row($res)) {
array_push($ary, $row[0]);
}
mysql_free_result($res);
return $ary;
}
function assemble_menu($menu)
{
$info = array();
if (preg_match("/English/i", $this->lang)) {
$info['title'] = $menu['curl'];
$t = 'Category'. $menu['curl']."<br>\n";
$t .= "Currently this model is still under developing, will be ready shortly. Thanks for the visiting.<br>\n";
$info['content'] = $t;
}
else {
$info['title'] = $menu['name'];
$t = '分类为:'. $menu['name']."<br>\n";
$t .= '详细信息为:'. $menu['description']."<br>\n";
$t .= '标签为:' . $menu['tag']?$menu['tag']:$menu['name']."<br>\n";
$t .= "目前该分类还处在开发阶段,很快就会有内容呈现。谢谢关注�?br>\n";
$info['content'] = $t;
}
return $info;
}
function assemble_sitemap($sm)
{
$info = array();
if (preg_match("/English/i", $this->lang)) {
$info['title'] = $sm[1];
$info['content'] = "Currently this model is under developing, will be ready shortly.<br>\n";
}
else {
$info['title'] = $sm[0];
$info['content'] = "目前该分类还处在开发阶段,很快就会有内容呈现。谢谢关注�?br>\n";
}
return $info;
}
function get_relative_articles($cid,$iid,$cate_id) {
$ary = array();
$sql = "select cid, title, (FLOOR( 1 + RAND( ) *1000 )) AS guanzhu from contents where cid!=$cid and iid=$iid order by pubdate desc limit 0,6";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
return $ary;
}
function get_relative_references($cid,$iid,$cate_id)
{
$sql = "select cid, title, (FLOOR( 1 + RAND( ) *1000 )) AS guanzhu from contents where cid!=$cid and iid=$iid order by rand() limit 0,6";
$res = mysql_query($sql);
$html = "<ul>\n";
while($row = mysql_fetch_array($res)) {
$html .= '<li class="tab_list"><i class="icon-circle-arrow-right"></i> <a href="?cid=';
$html .= $row[0] . '">' . $row[1] . '</a><span class="renshu">';
$html .= $row[2] . '</span></li>' . "\n";
}
$html .= "</ul>\n";
mysql_free_result($res);
return $html;
}
}
?>