source: trunk/Modules/News/NewsSource.php

Last change on this file was 953, checked in by chronos, 18 months ago
  • Added: News can be set as intranet only.
File size: 4.4 KB
Line 
1<?php
2
3function GetTextBetween(string &$Text, string $Start, string $End): string
4{
5 $Result = '';
6 if (($Start == '') && ($End == ''))
7 {
8 $Result = $Text;
9 $Text = '';
10 } else
11 if (($Start == '') && ($End != ''))
12 {
13 if (strpos($Text, $End) !== false)
14 {
15 $Result = substr($Text, 0, strpos($Text, $End));
16 $Text = substr($Text, strpos($Text, $End) + strlen($End));
17 }
18 } else
19 if (($Start != '') && ($End == ''))
20 {
21 if (strpos($Text, $Start) !== false)
22 {
23 $Result = substr($Text, strpos($Text, $Start) + strlen($Start));
24 $Text = '';
25 }
26 } else
27 if ((strpos($Text, $Start) !== false) and (strpos($Text, $End) !== false))
28 {
29 $Text = substr($Text, strpos($Text, $Start) + strlen($Start));
30 $Result = substr($Text, 0, strpos($Text, $End));
31 $Text = substr($Text, strpos($Text, $End) + strlen($End));
32 }
33 return $Result;
34}
35
36function HumanDateTimeToTime(string $DateTime): int
37{
38 if ($DateTime == '') return NULL;
39 $DateTime = str_replace('. ', '.', $DateTime);
40 $Parts = explode(' ', $DateTime);
41 $DateParts = explode('.', $Parts[0]);
42 if (count($Parts) > 1) {
43 $TimeParts = explode(':', $Parts[1]);
44 if (count($TimeParts) == 1) $TimeParts[1] = '0';
45 if (count($TimeParts) == 2) $TimeParts[2] = '0';
46 } else $TimeParts = array(0, 0, 0);
47 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[0], $DateParts[2]);
48 return $Result;
49}
50
51function HumanDateToTime(string $Date): int
52{
53 if ($Date == '') return NULL;
54 return HumanDateTimeToTime($Date.' 0:0:0');
55}
56
57function GetUrlBase(string $Url): string
58{
59 $Result = parse_url($Url);
60 return $Result['scheme']."://".$Result['host'];
61}
62
63class NewsSources
64{
65 public Database $Database;
66 public array $Items;
67
68 function Parse($Id = null)
69 {
70 $Output = '';
71 $Where = '(Enabled=1)';
72 if (($Id != null) and is_numeric($Id)) $Where .= ' AND (Id='.$Id.')';
73 $DbResult = $this->Database->select('NewsImport', '*', $Where);
74 while ($DbRow = $DbResult->fetch_assoc())
75 {
76 $Method = $DbRow['Method'];
77 if ($Method == 'vismo') $Source = new NewsSourceVismo();
78 else if ($Method == 'zdechovnet') $Source = new NewsSourceZdechovNET();
79 else {
80 $Output .= 'Unsupported parse method: '.$Method.'<br/>';
81 continue;
82 }
83 $Source->Database = $this->Database;
84 $Source->Id = $DbRow['Id'];
85 $Source->URL = $DbRow['Source'];
86 $Source->Method = $Method;
87 $Source->Category = $DbRow['Category'];
88 $Source->Name = $DbRow['Name'];
89 $this->Items[] = $Source;
90 $Output .= $Source->DoImport();
91 }
92 return $Output;
93 }
94}
95
96class NewsSource
97{
98 public string $Name;
99 public string $URL;
100 public $Method;
101 public int $Id;
102 public Database $Database;
103 public array $NewsItems;
104 public int $AddedCount;
105 public $Category;
106
107 function __construct()
108 {
109 $this->NewsItems = array();
110 $this->AddedCount = 0;
111 }
112
113 function Import(): string
114 {
115 return '';
116 }
117
118 function DoImport(): string
119 {
120 $this->NewsItems = array();
121 $this->AddedCount = 0;
122 $Output = 'Parsing '.$this->Name.' (#'.$this->Id.')...';
123 $Output .= $this->Import();
124 $Output .= ' parsed: '.count($this->NewsItems);
125 foreach ($this->NewsItems as $NewsItem)
126 {
127 $this->AddedCount += $NewsItem->AddIfNotExist();
128 }
129 $Output .= ', new added: '.$this->AddedCount;
130 $Output .= '</br>'."\n";
131 return $Output;
132 }
133}
134
135class NewsItem
136{
137 public Database $Database;
138 public string $Title = '';
139 public string $Content = '';
140 public int $Date = 0;
141 public string $Link = '';
142 public string $Category = '';
143 public string $Author = '';
144 public string $IP = '';
145 public string $Enclosure = '';
146
147 function AddIfNotExist(): int
148 {
149 $Where = '(`Title` = "'.$this->Database->real_escape_string($this->Title).'") AND '.
150 '(`Category` = "'.$this->Category.'") AND '.
151 '(`Date` = "'.TimeToMysqlDateTime($this->Date).'")';
152 $DbResult = $this->Database->select('News', '*', $Where);
153 if ($DbResult->num_rows == 0)
154 {
155 $this->Database->insert('News', array(
156 'Content' => $this->Content,
157 'Date' => TimeToMysqlDateTime($this->Date),
158 'Title' => $this->Title,
159 'Link' => $this->Link,
160 'Category' => $this->Category,
161 'Author' => $this->Author,
162 'IP' => $this->IP,
163 'Enclosure' => $this->Enclosure,
164 ));
165 $Result = 1;
166 } else $Result = 0;
167 return $Result;
168 }
169}
Note: See TracBrowser for help on using the repository browser.