OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@846 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_GoogleHistory / trunk / NP_GoogleHistory.php
1 <?php
2 class NP_GoogleHistory extends NucleusPlugin {
3         function getName() { return 'My GoogleHistory'; }
4         function getAuthor()  { return 'nakahara21'; }
5         function getURL() { return 'http://xx.nakahara21.net/'; }
6         function getVersion() { return '0.3'; }
7         function getDescription() { return 'Show history og google keywords'; }
8         function supportsFeature($what) {
9                 switch($what){
10                         case 'SqlTablePrefix':
11                                 return 1;
12                         default:
13                                 return 0;
14                 }
15         }
16
17         function install() {
18                 $this->createOption("ex", "extension of logfile (ex: log):", "text", "log");
19         }
20
21         function init(){
22                 global $CONF;
23                 $this->logdir = $this->getDirectory();
24                 $this->month = date("Y-m");
25                 $this->logfile = $this->logdir.$this->month.'.'.$this->getOption("ex");
26         }
27
28         function doSkinVar($skinType, $show="gform", $maxtoshow = 5) {
29                 global $CONF;
30
31                 if($show=="list"){
32                         $this->showGwordList($maxtoshow);
33                 }
34                 if($show=="gform"){
35 ?>
36 <div>
37 <form method="post" action="<?php echo $CONF['ActionURL'] ?>" target="_blank">
38 <input type="hidden" name="action" value="plugin" />
39 <input type="hidden" name="name" value="GoogleHistory" />
40 <input type="hidden" name="type" value="gsearch" />
41 <input type="text" value="" name="gword" size="15" />
42 <input type="submit" value="!" />
43 </form>
44 </div>
45 <?php
46                 }
47                 
48         }
49
50         function doAction($type) {
51                 $gword = requestVar('gword');
52                 switch ($type) {
53                         case 'gsearch':
54                                 $this->saveGword($gword);
55                                 $gurl = $this->makeGurl($gword);
56                                 header('Location: ' . $gurl);
57                                 break;
58                         default:
59                                 return 'Unexisting action: ' . $type;
60                 }
61                 exit;
62         }
63
64         function makeGurl($gword) {
65                 $en_gword = mb_convert_encoding($gword, "UTF-8", _CHARSET);
66                 $gurl = 'http://www.google.co.jp/search?ie=UTF-8&oe=UTF-8&q='.urlencode($en_gword);
67                 return $gurl;
68         }
69
70         function saveGword($gword) {
71                 $time = time();
72                 $gword = mb_convert_encoding($gword, "sjis", "UTF-8");
73                 $arr_data = Array($gword,$time);
74
75                 $fp = @fopen($this->logfile,"a+");
76                 if (!$fp) {
77                         if(!is_dir($this->logdir)) die("No such directory : ".$this->logdir."\n");
78                         if(!is_writable($this->logdir)) die("Cannot write to this directory : ".$this->logdir."\n");
79                         die("ERROR\n");
80                 }
81                 $tmp = fread ($fp, filesize ($this->logfile));
82                 ftruncate($fp,0);
83                 rewind($fp);
84                 fputs($fp,@join("\t",$arr_data)."\n");
85                 fputs($fp,$tmp);
86                 fclose($fp);
87         }
88
89         function readlog($maxtoshow){
90                 if( $handle = opendir($this->logdir)){
91                         while( false !== $file = readdir($handle)){
92                                 sscanf($file,"%4s-%2s.%s", $y, $m, $ex);
93                                 if(checkdate($m,1,$y) && $ex == $this->getOption("ex")){
94                                         $filelist[] = $file;
95                                 }
96                         }
97                         closedir($handle);
98                 }
99                 $log = array();
100                 for($i=0;$this->num<$maxtoshow;$i++){
101                         if($filelist[$i]){
102                                 $data = @file($this->logdir.$filelist[$i]);
103                                 $this->num += count($data);
104                                 $log = array_merge($log, $data);
105                         }else{
106                                 break;
107                         }
108                 }
109                         return $log;
110         }
111
112         function showGwordList($maxtoshow){
113                 $log = $this->readlog($maxtoshow);
114                 if(($amount = min($maxtoshow, $this->num)) >0){
115                         for($i=0;$i<$amount;$i++){
116                                 list($word,$timestamp) = explode("\t",$log[$i]);
117                                 $word = mb_convert_encoding($word, _CHARSET, "sjis");
118                                 $gtime = date("Y-m-d H:i",$timestamp);
119                                 $gurl = $this->makeGurl($word);
120                                 echo '<li><a href="'.$gurl.'" target="_blank">'.$word.'</a> '.$gtime.'</li>';
121                         }
122                 }
123         }
124 }
125 ?>