1 | <?php |
||
5 | class Crss { |
||
6 | |||
7 | private $table = 'RSSFeed'; |
||
8 | private $rssFile; |
||
9 | private $valid = false; |
||
10 | private $dbOptions = []; |
||
11 | private $dbStructure; |
||
12 | private $db; |
||
13 | private $newsCount; |
||
14 | private $feedDescription; |
||
15 | private $sendHeader; |
||
16 | /** |
||
17 | * Initiates the class, takes parameters as input but should work with standard settings. |
||
18 | * Connects to the DB and makes sure the table exists. |
||
19 | * @param $params is the input to adjust standard settings. I recommend to change only the 'feedDescription' part. |
||
20 | */ |
||
21 | 2 | public function __construct($params = []) |
|
22 | { |
||
23 | 2 | date_default_timezone_set('Europe/Stockholm'); |
|
24 | $options = [ |
||
25 | 2 | 'rssFile' => REALPATH(__DIR__) . '/rsscache/rss.xml', |
|
26 | 2 | 'table' => $this->table, |
|
27 | 2 | 'newsCount' => 5, |
|
28 | 2 | 'sendHeader' => true, |
|
29 | 'db' => [ |
||
30 | 2 | 'dsn' => 'sqlite:' . REALPATH(__DIR__) . '/src.sqlite', |
|
31 | 2 | 'username' => null, |
|
32 | 2 | 'password' => null, |
|
33 | 2 | 'driver_options' => null, |
|
34 | 'debug' => false |
||
35 | 2 | ], |
|
36 | 'feedDescription' => [ |
||
37 | 2 | 'title' => 'CRSS easy feed', |
|
38 | 2 | 'link' => 'http://www.github.com', |
|
39 | 'description' => 'Description of the amazing feed.' |
||
40 | 2 | ] |
|
41 | 2 | ]; |
|
42 | |||
43 | 2 | foreach ($params as $key => $value) { |
|
44 | 2 | if (is_array($value)) { |
|
45 | 1 | foreach ($value as $subKey => $subValue) { |
|
46 | 1 | $options[$key][$subKey] = $subValue; |
|
47 | 1 | } |
|
48 | 1 | } else { |
|
49 | 1 | $options[$key] = $value; |
|
50 | } |
||
51 | 2 | } |
|
52 | |||
53 | 2 | $this->rssFile = $options['rssFile']; |
|
54 | 2 | $this->table = $options['table']; |
|
55 | 2 | $this->dbOptions = $options['db']; |
|
56 | 2 | $this->newsCount = $options['newsCount']; |
|
57 | 2 | $this->feedDescription = $options['feedDescription']; |
|
58 | 2 | $this->sendHeader = $options['sendHeader']; |
|
59 | |||
60 | 2 | $this->dbStructure = 'CREATE TABLE IF NOT EXISTS ' . $this->table . ' ( |
|
61 | ID INTEGER PRIMARY KEY NOT NULL, |
||
62 | TITLE CHAR(50) NOT NULL, |
||
63 | LINK CHAR(50) NOT NULL, |
||
64 | DESCRIPTION CHAR(255) NOT NULL, |
||
65 | CREATED DATETIME |
||
66 | 2 | )'; |
|
67 | |||
68 | 2 | $this->connect(); |
|
69 | 1 | $this->createDB(); |
|
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * Function to clear the whole database from rss input |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 1 | public function clearRSS() |
|
82 | |||
83 | /** |
||
84 | * Internal function for connecting to the database |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | 2 | private function connect() |
|
89 | { |
||
90 | try { |
||
91 | 2 | $this->db = new \PDO( |
|
92 | 2 | $this->dbOptions['dsn'], |
|
93 | 2 | $this->dbOptions['username'], |
|
94 | 2 | $this->dbOptions['password'], |
|
95 | 2 | $this->dbOptions['driver_options'] |
|
96 | 2 | ); |
|
97 | |||
98 | 2 | } catch (\Exception $e) { |
|
99 | //Change to true to debug database connection |
||
100 | 1 | if ($this->dbOptions['debug']) { |
|
101 | // For debug purpose, shows all connection details |
||
102 | 1 | throw $e; |
|
103 | } else { |
||
104 | // Hide connection details. |
||
105 | throw new \PDOException("Could not connect to database, hiding connection details."); |
||
106 | } |
||
107 | } |
||
108 | 1 | $this->db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ); |
|
109 | 1 | } |
|
110 | |||
111 | 1 | public function createDB() { |
|
115 | |||
116 | /** |
||
117 | * Compares RSS file created timestamp and database timestamp to judge if new RSS file is needed |
||
118 | * |
||
119 | * Sets internal variable $this->valid to true/false depending if new file needs to be generate |
||
120 | * @return void |
||
121 | */ |
||
122 | 1 | private function checkValidity() |
|
149 | |||
150 | /** |
||
151 | * Creates a new RSS File with information from the database. Max amount of news is configured in the initiation. |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | private function createRSS() |
||
193 | |||
194 | /** |
||
195 | * Function that should be called to receive the RSS feed. |
||
196 | * Checks if the latest RSS File is up to date, if not, generates a new one. |
||
197 | * @return void |
||
198 | */ |
||
199 | 1 | public function getRSS() |
|
213 | |||
214 | /** |
||
215 | * Inserts information to RSS database |
||
216 | * @param $input should be an associative array with three pars TITLE, LINK, DESCRIPTION |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | 1 | public function insertRSS($input = []) |
|
225 | } |
||
226 |