source: trunk/home/mylinks.php @ 1874

Revision 1874, 3.7 KB checked in by Xiping.Wang, 17 months ago (diff)

[trunk]merge wordpress branch, upgrade to wordpress 3.0.4

Line 
1<?php
2require_once( dirname(__file__) . '/../common/omconfig.php' );
3//require_once( FS_COMMON_SKIN_DIRECTORY . '/common.php' );
4require_once( FS_COMMON_DIRECTORY.'/utils.php');
5//require_once('includes/config.php');
6require_once ('includes/classes/DB.php' );
7
8class myLinks {
9
10        private $bIsLoggedOn;
11        private $oDb;   //Wiki DB Connection
12
13        public function __construct(){
14
15                $this->bIsLoggedOn = GetLoggedInStateFromWikiCookies();
16
17                $this->oDb          = &newDb( 'wiki',true );
18                if( $this->bIsLoggedOn ){
19                        $this->iUserId      = $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserID' ];
20                        $this->sUserName    = $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserName' ];
21                }
22        }
23       
24        public function isLoggedOn(){
25                return $this->bIsLoggedOn;
26        }
27
28        public function loadlinks(){
29                echo $this->loaddata($this->iUserId); // Get limited links of user_id
30        }
31
32        public function savelinks($url, $title){
33                //$title = $this->getUrlTitle($url); // Get title by using url
34                // If it is already registered url, remove original and enter again. (to put it top)
35                $sSql = "DELETE FROM " . OMWIKI_DB_TABLE_PREFIX . "user_mylinks WHERE user_id='" . $this->iUserId . "' AND url='" . $url . "' AND title='" . $title . "'";
36                $rows = $this->oDb->ExecuteSql($sSql);
37               
38                // Register url data to database
39                $sSql = "INSERT INTO ". OMWIKI_DB_TABLE_PREFIX . "user_mylinks(user_id,url,title,registered_date) VALUES('" . $this->iUserId . "', '" . $url . "', '" . $title . "', '" . date("YmdHis") . "')";
40                $rows = $this->oDb->ExecuteSql($sSql);
41
42                echo $this->loaddata($this->iUserId);
43        }
44
45        public function deletelinks($articleurl) {
46                $sSql = "DELETE FROM " . OMWIKI_DB_TABLE_PREFIX . "user_mylinks WHERE user_id='" . $this->iUserId . "' AND url='" . $articleurl . "'";
47                $rows = $this->oDb->ExecuteSql($sSql);
48               
49                echo $this->loaddata($this->iUserId);
50        }
51       
52        public function loaddata() {
53                $sSql = "SELECT * FROM ". OMWIKI_DB_TABLE_PREFIX . "user_mylinks WHERE user_id='" . $this->iUserId . "' ORDER BY registered_date DESC LIMIT 5";
54                $rows = $this->oDb->GetRows( $sSql );
55                if (count($rows) > 0) {
56                        $html = '';
57                        for ($i=0; $i<count($rows); $i++) {
58                                $html .= '<div>&bull;&nbsp;<a href="' . $rows[$i]['url'] . '">' . $rows[$i]['title'] . '</a><br /></div>';
59                        }
60                        return $html;
61                } else {
62                        return "false";
63                }
64        }
65
66        public function getUrlTitle($url) {
67                $fd = @fopen($url, 'r');
68                if ($fd) {
69                        $html = fread($fd, 1750);
70                        fclose($fd);
71
72                        // Get title from title tag
73                        preg_match_all('/<title>(.*)<\/title>/si', $html, $matches);
74                        $title = $matches[1][0];
75
76                        // Get encoding from charset attribute
77                        preg_match_all('/<meta.*charset=([^;"]*)">/i', $html, $matches);
78                        $encoding = strtoupper($matches[1][0]);
79
80                        // Convert to UTF-8 from the original encoding
81                        if (function_exists('mb_convert_encoding') ) {
82                                $title = @mb_convert_encoding($title, 'UTF-8', $encoding);
83                        }
84
85                        if (mb_strlen($title, 'utf-8') > 0) {
86                                return $title;
87                        } else {
88                                // No title, so return filename
89                                $uriparts = explode('/', $url);
90                                $filename = end($uriparts);
91                                unset($uriparts);
92
93                                return $filename;
94                        }
95                } else {
96                        return false;
97                }
98        }
99}
100
101$mylinks = new myLinks();
102
103if( $mylinks->isLoggedOn()&&isset($_GET['action']) ){
104
105        $action = $_GET['action'];
106        if ($action == 'savelinks'){
107                $url = mysql_escape_string($_GET['url']);
108                $title = mysql_escape_string($_GET['title']);
109                $mylinks->savelinks($url, $title);
110        }
111        if ($action == 'loadlinks'){
112                $mylinks->loadlinks();
113        }
114        if ($action == 'deletelinks') {
115                $articleurl = mysql_escape_string($_GET['url']);
116                $mylinks->deletelinks($articleurl);
117        }
118}
119?>
Note: See TracBrowser for help on using the repository browser.