1<?php
2/**
3 * Admin panel for news feed plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca>
7 */
8 if(!defined('DOKU_INC')) die();
9require_once DOKU_INC . "lib/plugins/news/scripts/rss.php";
10
11/**
12 * All DokuWiki plugins to extend the admin function
13 * need to inherit from this class
14 */
15class admin_plugin_news extends DokuWiki_Admin_Plugin {
16
17    var $output = '';
18	var $helper;
19	var $pagedata;
20	var $prev_deleted = "";
21    var $is_prev_deleted = array();
22	var $subfeed_selected = 0;
23	var $subfeed_name = "";
24
25  	function admin_plugin_news() {
26		   $this->helper =& plugin_load('helper', 'news');
27           if($_REQUEST['subfeeds'] != 'NotSet') {
28               $this->helper->setSubFeed($_REQUEST['subfeeds']);
29           }
30           $this->pagedata = $this->helper->_readFile($this->helper->getMetaFN('pagedata','.ser'),true);
31
32	}
33    /**
34     * handle user request
35     */
36    function handle() {
37
38      if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do
39
40      $this->output = '';
41      if (!checkSecurityToken()) return;
42      if (!is_array($_REQUEST['cmd'])) return;
43
44      // verify valid values
45      switch (key($_REQUEST['cmd'])) {
46        case 'prune' :
47		   $this->prune();
48		   break;
49		case 'confirm' :
50		   $this->confirm();
51		   break;
52        case 'restore' :
53		   $this->is_prev_deleted =array();
54		   $this->prev_deleted = "";
55           if(isset($_REQUEST[subfeed_dir])) {
56                  $this->helper->setSubFeed($_REQUEST['subfeed_dir']);
57                  $this->pagedata = $this->helper->_readFile($this->helper->getMetaFN('pagedata','.ser'),true);
58           }
59           // $this->output=$_REQUEST;
60		   return;
61		 case 'generate':
62           $this->output=$this->generate($_REQUEST['subfeeds']);
63		   return;
64         case 'subfeed':
65           $this->subfeed_selected = $_REQUEST['subfeed_inx'];
66           if($this->subfeed_selected  > 0) {
67              $this->subfeed_name= $_REQUEST['subfeeds'];
68           }
69           break;
70
71      }
72
73          $deleted = array();
74		  if(isset($_REQUEST['delete']) && $_REQUEST['delete']) {
75			$deletes = $_REQUEST['delete'];
76			$deleted = array_keys($deletes);
77		  }
78
79
80		  if($_REQUEST['prev_del']) {
81		     $prev_deleted = $_REQUEST['prev_del'];
82			 $prev_deleted = explode(',',$prev_deleted);
83			 $prev_deleted = array_merge($prev_deleted, $deleted);
84			 $prev_deleted = array_unique($prev_deleted);
85			 $this->prev_deleted = implode(",", $prev_deleted);
86			 $this->is_prev_deleted = $prev_deleted;
87		  }
88        // $this->output=$_REQUEST;
89    }
90
91    /**
92     * output appropriate html
93     */
94    function html() {
95
96      ptln('<div style="width:90%;margin:auto;display:none;"  class="news_info"><p>' . $this->getLang('instructions') . '</p></div>');
97      ptln('<form action="'.wl($ID).'" method="post" name="news_data" onsubmit="newshandler(this);">');
98      ptln('  <input type="hidden" name="do"   value="admin" />');
99      ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
100	  ptln('  <input type="hidden" name="prev_del" id ="prev_del" value="' .$this->prev_deleted. '" />');
101
102      ptln('  <input type="hidden" name="subfeed_inx" id ="subfeed_inx" value="0" />');
103      ptln('  <input type="hidden" name="subfeed_dir" id ="subfeed_dir" value="" />');
104      formSecurityToken();
105
106      ptln('  <input type="submit" name="cmd[prune]"  value="'.$this->getLang('btn_prune').'" />');
107      ptln('  <input type="submit" name="cmd[restore]"  value="'.$this->getLang('btn_restore').'" />');
108	  ptln('  <input type="submit" name="cmd[confirm]" onclick="return confirm_del();" value="'.$this->getLang('btn_confirm').'" />');
109	  ptln('  <input type="submit" name="cmd[generate]" value="'.$this->getLang('btn_generate').'" />');
110      ptln('  <select  id="subfeeds" name="subfeeds" onchange="subfeedshandler()" ><option value="NotSet">' . $this->getLang('select_feed') .'</option>');
111      $this->subfeed_options();
112      ptln('</select>');
113      ptln('  <input type="submit" id = "subfeedbtn" name="cmd[subfeed]" value="'.$this->getLang('btn_confirmfeed').'" />');
114      ptln('  <input type="button" id = "news_infobtn" value="'.$this->getLang('btn_info').'" />');
115	  ptln('<div id="pagedata_news"><br />');
116	  $this->table_header();
117			foreach($this->pagedata as $md5=>$pageinfo) {
118			   $this->pagedata_row($md5,$pageinfo);
119			}
120
121	  $this->table_footer();
122	  ptln('</div>');
123      ptln('</form>');
124
125	  if($this->output) {
126		  ptln('<p><pre>');
127		  echo print_r($this->output,true);
128          echo  $this->subfeed_name;
129		  ptln('</pre></p>');
130     }
131	 }
132
133	 function table_header() {
134	    ptln('<table cellspacing="8">');
135		$theader = $this->theader("Delete") . $this->theader("Page") . $this->theader("GM Time") .
136		            $this->theader("Local Time");
137        ptln("<tr>$theader</tr>");
138	 }
139	 function table_footer() {
140	    ptln('</table>');
141	 }
142
143	 function pagedata_row($md5,$info) {
144	   static $inx = 0;
145
146	   if(in_array($md5,$this->is_prev_deleted)) return;
147
148	   $type = 'delete';
149	   $cb_id =$type . '_' . $inx;
150		ptln('<tr>');
151
152		$row = '<td align="center">' . "<input type = 'checkbox'  id='$cb_id'  name ='" . $type . "[$md5]' value = '$md5'>" .'</td>';
153		$row .= $this->cell($info['id'] ) .  $this->cell($info['gmtime']) .
154		       $this->cell(date('r',$info['time']));
155	    ptln($row);
156		ptln('</td></tr>');
157
158		$index++;
159	 }
160	 function cell($data="") {
161	     return "<td class='right'>$data</td>";
162	 }
163	 function theader($name="") {
164
165	    return "<th align='center'>$name</th>";
166	 }
167
168	 function subfeed_options() {
169         $dir = DOKU_INC . 'data/meta/newsfeed/';
170
171          if($this->subfeed_selected == 0 && isset($_REQUEST['subfeed_dir'])) {
172              $sbname_check = $_REQUEST['subfeed_dir'];
173          }
174         else  $sbname_check = 'NotSet';
175
176         $index = 1;
177         if ($dh = opendir($dir)) {
178            while (($file = readdir($dh)) !== false) {
179              if($file == '.' || $file == '..') continue;
180              if(is_dir($dir . $file)) {
181                 if($this->subfeed_selected == $index || $file == $sbname_check) {
182                     ptln("<option value='$file' selected>$file</option>");
183                 }
184                 else {
185                     ptln("<option value='$file' >$file</option>");
186                    }
187                $index++;
188             }
189         }
190
191        }
192          closedir($dh);
193
194     }
195
196	 function prune() {
197
198
199		  $deletes = $_REQUEST['delete'];
200		  $deleted = array_keys($deletes);
201
202		  foreach($deleted as $d) {
203			  unset($this->pagedata[$d]);
204		  }
205
206	 }
207
208	 function confirm() {
209
210		 $deleted = explode(',',$_REQUEST['prev_del']);
211         if(isset($_REQUEST['subfeed_dir']) && $_REQUEST['subfeed_dir'] != 'NotSet') {
212               $this->helper->setSubFeed($_REQUEST['subfeed_dir']);
213               $meta_dir =$this->helper->getMetaDirectory();
214           }
215           else $meta_dir = "newsfeed:";
216
217
218
219		  foreach($deleted as $d) {
220			  unset($this->pagedata[$d]);
221		  }
222          $pfile =$this->helper->getMetaFN('pagedata', '.ser');
223          $this->helper->_writeFile($pfile,$this->pagedata,true);
224
225  		  foreach($deleted as $d) {
226		     $file = metaFN($meta_dir . $d, '.gz');
227			 if(file_exists($file)) {
228			   @unlink($file);
229			 }
230		  }
231
232	 }
233
234	 function generate($subfeed) {
235		global $newsChannelTitle;
236        global $newsChannelDescription;
237        global $newsChannelTtl;
238        $newsfeed_ini = DOKU_INC . 'lib/plugins/news/scripts/newsfeed.ini';
239
240         if(file_exists($newsfeed_ini)) {
241            $ini_array = parse_ini_file($newsfeed_ini, true);
242            $which = isset($ini_array[$subfeed]) ? $subfeed : 'default';
243            $newsChannelTitle = $ini_array[$which]['title'];
244            $newsChannelDescription = $ini_array[$which]['description'] ;
245            $newsChannelTtl = isset($ini_array[$which]['ttl']) ? $ini_array[$which]['ttl'] : $this->getConf('ttl');
246        }
247        else {
248            $subfeed = "";
249            $newsChannelDescription = $this->getConf('desc');
250            $newsChannelTitle=$this->getConf('title');
251            $newsChannelTtl = $this->getConf('ttl');
252        }
253
254		    $create_time = 0;
255			$ttl = $this->getConf('ttl');
256            if($subfeed && $subfeed !='NotSet') {
257                $xml_file = DOKU_INC . $subfeed . '_news.xml';
258            }
259	        else $xml_file = DOKU_INC . 'news_feed.xml';
260
261            $current_time = time();
262
263	 		new externalNewsFeed($xml_file,$ttl,$subfeed);
264
265			if(@file_exists($xml_file)) {
266			     $create_time= filectime($xml_file);
267	         }
268			if($create_time >= $current_time) {
269			   return 'Feed generated: ' . date('r',$create_time) ;
270			}
271			else {
272			   return "A new feed may not have been generated.  Check $xml_file.";
273			}
274	 }
275}
276