summaryrefslogtreecommitdiff
path: root/utils/modules/rss.php
blob: 02501b3e5495b6935c37bdbf55bca49dcde599c7 (plain)
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
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/convert.php");

class RSSConfig {
  private $file;
  private $configs = array();

  public $title;
  public $url;
  public $description;
  public $editoremail;
  public $webmasteremail;
  public $target;

  // Admin config
  public $admin_title = "RSS Config";
  public $admin_submodules = array("Config" => "config");

  public function admin_config($action, $vars)
  {
    switch($action) {
    case "update":
      $this->title = $vars["title"];
      $this->url = $vars["url"];
      $this->description = $vars["description"];
      $this->editoremail = $vars["editoremail"];
      $this->webmasteremail = $vars["webmasteremail"];
      $this->target = $vars["target"];
			$this->write();
			break;
			
    default:
      $form = new Form("update");
      $form->addWidget(new LineEdit("Title:", "title", $this->title));
      $form->addWidget(new LineEdit("Base URL:", "url", $this->url));
      $form->addWidget(new LineEdit("Description:", "description", $this->description));
      $form->addWidget(new LineEdit("Editor Email:", "editoremail", $this->editoremail));
      $form->addWidget(new LineEdit("Webmaster Email:", "webmasteremail", $this->webmasteremail));
      $form->addWidget(new LineEdit("Target file:", "target", $this->target));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "config":
      $this->admin_config($action, $vars);
      break;
    }
  }

  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<rss title=\"".xmlenc($this->title)."\"\n");
    fwrite($fp, "     url=\"".xmlenc($this->url)."\"\n");
    fwrite($fp, "     description=\"".xmlenc($this->description)."\"\n");
    fwrite($fp, "     editoremail=\"".xmlenc($this->editoremail)."\"\n");
    fwrite($fp, "     webmasteremail=\"".xmlenc($this->webmasteremail)."\"\n");
    fwrite($fp, "     target=\"".xmlenc($this->target)."\">\n");
    fwrite($fp, "</rss>\n");

    fclose($fp);
  }

  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = TRUE;
    $dom->load($this->file);

		$rss = $dom->documentElement;
    $this->title = $rss->getAttribute('title');
    $this->url = $rss->getAttribute('url');
    $this->description = $rss->getAttribute('description');
    $this->editoremail = $rss->getAttribute('editoremail');
    $this->webmasteremail = $rss->getAttribute('webmasteremail');
    $this->target = $rss->getAttribute('target');
  }

  public function value($name, $default = "")
  {
    if(isset($this->configs[$name])) return $this->configs[$name];
    return $default;
  }

  public function setValue($name, $value)
  {
    $this->configs[$name] = $value;
  }

  public function RSSConfig($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }

}

function rss_init()
{
  global $DATA_DIR;
  return new RSSConfig($DATA_DIR . "/rss.xml");
}

?>