Completed
Push — master ( 6e487d...af4d7b )
by Angus
03:37
created

Site_Model::get_content()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 8
nop 4
dl 0
loc 33
ccs 19
cts 19
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
abstract class Site_Model extends CI_Model {
4 100
	public function __construct() {
5 100
		parent::__construct();
6
7 100
		$this->load->database();
8 100
	}
9
10
	abstract public function getFullTitleURL(string $title_url) : string;
11
12
	abstract public function getChapterData(string $title_url, string $chapter) : array;
13
14
	//TODO: When ci-phpunit-test supports PHP Parser 3.x, add " : ?array"
15
	abstract public function getTitleData(string $title_url);
16
17
	abstract public function isValidTitleURL(string $title_url) : bool;
18
	abstract public function isValidChapter(string $chapter) : bool;
19
20 12
	protected function get_content(string $url, string $cookie_string = "", string $cookiejar_path = "", bool $follow_redirect = FALSE) {
21 12
		$ch = curl_init();
22 12
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
23 12
		curl_setopt($ch, CURLOPT_ENCODING , "gzip");
24
		//curl_setopt($ch, CURLOPT_VERBOSE, 1);
25 12
		curl_setopt($ch, CURLOPT_HEADER, 1);
26
27 12
		if($follow_redirect)        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
28
29 12
		if(!empty($cookie_string))  curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
30 12
		if(!empty($cookiejar_path)) curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar_path);
31
32
		//Some sites check the useragent for stuff, use a pre-defined user-agent to avoid stuff.
33 12
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36');
34
35
		//TODO: Check in a while if this being enabled still causes issues
36
		//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //FIXME: This isn't safe, but it allows us to grab SSL URLs
37
38 12
		curl_setopt($ch, CURLOPT_URL, $url);
39 12
		$response = curl_exec($ch);
40
41 12
		$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
42 12
		$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
43 12
		$header      = http_parse_headers(substr($response, 0, $header_size));
44 12
		$body        = substr($response, $header_size);
45 12
		curl_close($ch);
46
47
		return [
48 12
			'headers'      => $header,
49 12
			'status_code' => $status_code,
50 12
			'body'        => $body
51
		];
52
	}
53
54
	/**
55
	 * @return DOMElement[]|false
56
	 */
57 4
	public function parseTitleDataDOM(array $content, string $site, string $title_url, string $node_title_string, string $node_row_string, string $node_latest_string, string $node_chapter_string, string $failure_string = "") {
58
		//list('headers' => $headers, 'status_code' => $status_code, 'body' => $data) = $content; //TODO: PHP 7.1
59 4
		$headers     = $content['headers'];
0 ignored issues
show
Unused Code introduced by
$headers is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60 4
		$status_code = $content['status_code'];
61 4
		$data        = $content['body'];
62
63 4
		$titleData = [];
0 ignored issues
show
Unused Code introduced by
$titleData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65 4
		if(!($status_code >= 200 && $status_code < 300)) {
66 1
			log_message('error', "{$site} : {$title_url} | Bad Status Code ({$status_code})");
67 3
		} else if(empty($data)) {
68
			log_message('error', "{$site} : {$title_url} | Data is empty? (Status code: {$status_code})");
69 3
		} else if($failure_string !== "" && strpos($data, $failure_string) !== FALSE) {
70 1
			log_message('error', "{$site} : {$title_url} | Failure string matched");
71
		} else {
72 2
			$dom = new DOMDocument();
73 2
			libxml_use_internal_errors(TRUE);
74 2
			$dom->loadHTML($data);
75 2
			libxml_use_internal_errors(FALSE);
76
77 2
			$xpath = new DOMXPath($dom);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
78 2
			$nodes_title = $xpath->query($node_title_string);
79 2
			$nodes_row   = $xpath->query($node_row_string);
80 2
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
81 2
				$firstRow      = $nodes_row->item(0);
82 2
				$nodes_latest  = $xpath->query($node_latest_string,  $firstRow);
83 2
				$nodes_chapter = $xpath->query($node_chapter_string, $firstRow);
84
85 2
				if($nodes_latest->length === 1 && $nodes_chapter->length === 1) {
86
					return [
87 2
						'nodes_title'   => $nodes_title->item(0),
88 2
						'nodes_latest'  => $nodes_latest->item(0),
89 2
						'nodes_chapter' => $nodes_chapter->item(0)
90
					];
91
				} else {
92
					log_message('error', "{$site} : {$title_url} | Invalid amount of nodes (LATEST: {$nodes_latest->length} | CHAPTER: {$nodes_chapter->length})");
93
				}
94
			} else {
95
				log_message('error', "{$site} : {$title_url} | Invalid amount of nodes (TITLE: {$nodes_title->length} | ROW: {$nodes_row->length})");
96
			}
97
		}
98 2
		return FALSE;
99
	}
100
}
101
class Sites_Model extends CI_Model {
102
	public $MangaFox;
103
	public $MangaHere;
104
	public $Batoto;
105
	public $DynastyScans;
106
	public $MangaPanda;
107
	public $MangaStream;
108
	public $WebToons;
109
	public $KissManga;
110
	public $KireiCake;
111
	public $GameOfScanlation;
112
	public $MangaCow;
113
	public $SeaOtterScans;
114
	public $HelveticaScans;
115
	public $SenseScans;
116
117 100
	public function __construct() {
118 100
		parent::__construct();
119
120 100
		$this->MangaFox         = new MangaFox();
121 100
		$this->MangaHere        = new MangaHere();
122 100
		$this->Batoto           = new Batoto();
123 100
		$this->DynastyScans     = new DynastyScans();
124 100
		$this->MangaPanda       = new MangaPanda();
125 100
		$this->MangaStream      = new MangaStream();
126 100
		$this->WebToons         = new WebToons();
127 100
		$this->KissManga        = new KissManga();
128 100
		$this->KireiCake        = new KireiCake();
129 100
		$this->GameOfScanlation = new GameOfScanlation();
130 100
		$this->MangaCow         = new MangaCow();
131 100
		$this->SeaOtterScans    = new SeaOtterScans();
132 100
		$this->HelveticaScans   = new HelveticaScans();
133 100
		$this->SenseScans       = new SenseScans();
134 100
	}
135
}
136
137
class MangaFox extends Site_Model {
138 2
	public function getFullTitleURL(string $title_url) : string {
139 2
		return "http://mangafox.me/manga/{$title_url}/";
140
	}
141
142
	public function isValidTitleURL(string $title_url) : bool {
143
		$success = (bool) preg_match('/^[a-z0-9_]+$/', $title_url);
144
		if(!$success) log_message('error', "Invalid Title URL (MangaFox): {$title_url}");
145
		return $success;
146
	}
147
	public function isValidChapter(string $chapter) : bool {
148
		$success = (bool) preg_match('/^(?:v[0-9a-zA-Z]+\/)?c[0-9\.]+$/', $chapter);
149
		if(!$success) log_message('error', 'Invalid Chapter (MangaFox): '.$chapter);
150
		return $success;
151
	}
152
153
	public function getChapterData(string $title_url, string $chapter) : array {
154
		return [
155
			'url'    => "http://mangafox.me/manga/{$title_url}/{$chapter}/",
156
			'number' => $chapter
157
		];
158
	}
159
160 2
	public function getTitleData(string $title_url) {
161 2
		$titleData = [];
162
163 2
		$fullURL = $this->getFullTitleURL($title_url);
164 2
		$content = $this->get_content($fullURL);
165
166 2
		$data = $this->parseTitleDataDOM(
167
			$content,
168 2
			'MangaFox',
169
			$title_url,
170 2
			"//meta[@property='og:title']/@content",
171 2
			"//body/div[@id='page']/div[@class='left']/div[@id='chapters']/ul[1]/li[1]",
172 2
			"div/span[@class='date']",
173 2
			"div/h3/a"
174
		);
175 2
		if($data) {
176
			//This seems to be be the only viable way to grab the title...
177 1
			$titleData['title'] = html_entity_decode(substr($data['nodes_title']->textContent, 0, -6));
178
179 1
			$link = preg_replace('/^(.*\/)(?:[0-9]+\.html)?$/', '$1', (string) $data['nodes_chapter']->getAttribute('href'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180 1
			$chapterURLSegments = explode('/', $link);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
181 1
			$titleData['latest_chapter'] = $chapterURLSegments[5] . (isset($chapterURLSegments[6]) && !empty($chapterURLSegments[6]) ? "/{$chapterURLSegments[6]}" : "");
182 1
			$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $data['nodes_latest']->nodeValue));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
183
		}
184
185 2
		return (!empty($titleData) ? $titleData : NULL);
186
	}
187
}
188
189
class MangaHere extends Site_Model {
190 2
	public function getFullTitleURL(string $title_url) : string {
191 2
		return "http://www.mangahere.co/manga/{$title_url}/";
192
	}
193
194
	public function isValidTitleURL(string $title_url) : bool {
195
		$success = (bool) preg_match('/^[a-z0-9_]+$/', $title_url);
196
		if(!$success) log_message('error', "Invalid Title URL (MangaFox): {$title_url}");
197
		return $success;
198
	}
199
	public function isValidChapter(string $chapter) : bool {
200
		$success = (bool) preg_match('/^(?:v[0-9]+\/)?c[0-9]+(?:\.[0-9]+)?$/', $chapter);
201
		if(!$success) log_message('error', 'Invalid Chapter (MangaFox): '.$chapter);
202
		return $success;
203
	}
204
205
	public function getChapterData(string $title, string $chapter) : array {
206
		return [
207
			'url'    => "http://www.mangahere.co/manga/{$title}/{$chapter}/",
208
			'number' => $chapter
209
		];
210
	}
211
212 2
	public function getTitleData(string $title_url) {
213 2
		$titleData = [];
214
215 2
		$fullURL = $this->getFullTitleURL($title_url);
216 2
		$content = $this->get_content($fullURL);
217
218 2
		$data = $this->parseTitleDataDOM(
219
			$content,
220 2
			'MangaHere',
221
			$title_url,
222 2
			"//meta[@property='og:title']/@content",
223 2
			"//body/section/article/div/div[@class='manga_detail']/div[@class='detail_list']/ul[1]/li[1]",
224 2
			"span[@class='right']",
225 2
			"span[@class='left']/a",
226 2
			"<div class=\"error_text\">Sorry, the page you have requested can’t be found."
227
		);
228 2
		if($data) {
229
			//This seems to be be the only viable way to grab the title...
230 1
			$titleData['title'] = $data['nodes_title']->textContent;
231
232 1
			$link = preg_replace('/^(.*\/)(?:[0-9]+\.html)?$/', '$1', (string) $data['nodes_chapter']->getAttribute('href'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
233 1
			$chapterURLSegments = explode('/', $link);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
234 1
			$titleData['latest_chapter'] = $chapterURLSegments[5] . (isset($chapterURLSegments[6]) && !empty($chapterURLSegments[6]) ? "/{$chapterURLSegments[6]}" : "");
235 1
			$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $data['nodes_latest']->nodeValue));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
236
		}
237
238 2
		return (!empty($titleData) ? $titleData : NULL);
239
	}
240
}
241
242
class Batoto extends Site_Model {
243
	//Batoto is a bit tricky to track. Unlike MangaFox and MangaHere, it doesn't store anything in the title_url, which means we have to get the data via other methods.
244
	//One problem we have though, is the tracker must support multiple sites, so this means we need to do some weird things to track Batoto.
245
	//title_url is stored like: "TITLE_URL:--:LANGUAGE"
246
	//chapter_urls are stored like "CHAPTER_ID:--:CHAPTER_NUMBER"
247
248
	public function getFullTitleURL(string $title_string) : string {
249
		//FIXME: This does not point to the language specific title page. Should ask if it is possible to set LANG as arg?
250
		//FIXME: This points to a generic URL which will redirect according to the ID. Preferably we'd try and get the exact URL from the title, but we can't pass it here.
251
		$title_parts = explode(':--:', $title_string);
252
		return "http://bato.to/comic/_/comics/-r".$title_parts[0];
253
	}
254
255
	public function isValidTitleURL(string $title_url) : bool {
256
		$success = (bool) preg_match('/^[0-9]+:--:(?:English|Spanish|French|German|Portuguese|Turkish|Indonesian|Greek|Filipino|Italian|Polish|Thai|Malay|Hungarian|Romanian|Arabic|Hebrew|Russian|Vietnamese|Dutch)$/', $title_url);
257
		if(!$success) log_message('error', "Invalid Title URL (Batoto): {$title_url}");
258
		return $success;
259
	}
260
	public function isValidChapter(string $chapter) : bool {
261
		//FIXME: We're not validating the chapter name since we don't know what all the possible valid characters can be
262
		//       Preferably we'd just use /^[0-9a-z]+:--:(v[0-9]+\/)?c[0-9]+(\.[0-9]+)?$/
263
264
		$success = (bool) preg_match('/^[0-9a-z]+:--:.+$/', $chapter);
265
		if(!$success) log_message('error', 'Invalid Chapter (Batoto): '.$chapter);
266
		return $success;
267
	}
268
269 View Code Duplication
	public function getChapterData(string $title_string, string $chapter) : array {
270
		//$title_string isn't used here.
271
272
		$chapter_parts = explode(':--:', $chapter);
273
		return [
274
			'url'    => "http://bato.to/reader#" . $chapter_parts[0],
275
			'number' => $chapter_parts[1]
276
		];
277
	}
278
279
	public function getTitleData(string $title_string) {
280
		$title_parts = explode(':--:', $title_string);
281
		$title_url   = $this->getFullTitleURL($title_string);
282
		$title_lang  = $title_parts[1];
283
		//TODO: Validate title_lang from array?
284
285
		$titleData = [];
286
287
		//Bato.to is annoying and locks stuff behind auth. See: https://github.com/DakuTree/manga-tracker/issues/14#issuecomment-233830855
288
		$cookies = [
289
			"lang_option={$title_lang}",
290
			"member_id=" . $this->config->item('batoto_cookie_member_id'),
291
			"pass_hash=" . $this->config->item('batoto_cookie_pass_hash')
292
		];
293
		$content = $this->get_content($title_url, implode("; ", $cookies), "", TRUE);
294
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
295
		if(!$data) {
296
			log_message('error', "Batoto: Couldn't successfully grab URL ({$title_url})");
297
			return NULL;
298
		}
299
300
		$data = preg_replace('/^[\s\S]+<!-- ::: CONTENT ::: -->/', '<!-- ::: CONTENT ::: -->', $data);
301
		$data = preg_replace('/<!-- end mainContent -->[\s\S]+$/', '<!-- end mainContent -->', $data);
302
		$data = preg_replace('/<div id=\'commentsStart\' class=\'ipsBox\'>[\s\S]+$/', '</div></div><!-- end mainContent -->', $data);
303
		if(strpos($data, '>Register now<') === FALSE) {
304
			//Auth was successful
305
			$dom = new DOMDocument();
306
			libxml_use_internal_errors(true);
307
			$dom->loadHTML($data);
308
			libxml_use_internal_errors(false);
309
310
			$xpath = new DOMXPath($dom);
311
			$nodes = $xpath->query("(//div/div)[last()]/table/tbody/tr[2]");
312
			if($nodes->length === 1) {
313
				$node = $nodes[0];
314
315
				$nodes_chapter = $xpath->query('td/a[contains(@href,\'reader\')]', $node);
316
				$nodes_updated = $xpath->query('td[last()]', $node);
317
				if($nodes_chapter->length === 1 && $nodes_updated->length === 1) {
318
					$chapter_element = $nodes_chapter->item(0);
319
					$updated_element = $nodes_updated->item(0);
320
321
					///^(?:Vol\.(?<volume>\S+) )?(?:Ch.(?<chapter>[^\s:]+)(?:\s?-\s?(?<extra>[0-9]+))?):?.*/
322
					preg_match('/^(?:Vol\.(?<volume>\S+) )?(?:Ch.(?<chapter>[^\s:]+)(?:\s?-\s?(?<extra>[0-9]+))?):?.*/', trim($chapter_element->nodeValue), $text);
323
324
					$titleData['title']          = html_entity_decode(trim($xpath->query('//h1[@class="ipsType_pagetitle"]')->item(0)->nodeValue));
325
					$titleData['latest_chapter'] = substr($chapter_element->getAttribute('href'), 22) . ':--:' . ((!empty($text['volume']) ? 'v'.$text['volume'].'/' : '') . 'c'.$text['chapter'] . (!empty($text['extra']) ? '-'.$text['extra'] : ''));
326
327
					$dateString = $updated_element->nodeValue;
328
					if($dateString == 'An hour ago') {
329
						$dateString = '1 hour ago';
330
					}
331
					$titleData['last_updated']   = date("Y-m-d H:i:s", strtotime(preg_replace('/ (-|\[A\]).*$/', '', $dateString)));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
332
				} else {
333
					log_message('error', "Batoto: Regex missing <td> ({$title_url})");
334
					return NULL;
335
				}
336
			} else {
337
				log_message('error', "Batoto: Regex missing <tr> ({$title_url})");
338
				return NULL;
339
			}
340
		} else {
341
			//Auth was not successful. Alert the admin / Try to login.
342
			log_message('error', "Batoto: Auth not successful ({$title_url})");
343
			return NULL;
344
		}
345
		return (!empty($titleData) ? $titleData : NULL);
346
	}
347
}
348
349
class DynastyScans extends Site_Model {
350
	//FIXME: This has some major issues. SEE: https://github.com/DakuTree/manga-tracker/issues/58
351
	public function getFullTitleURL(string $title_string) : string {
352
		$title_parts = explode(':--:', $title_string);
353
		$url_type = ($title_parts[1] == '0' ? 'series' : 'chapters');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
354
355
		return 'http://dynasty-scans.com/'.$url_type.'/'.$title_parts[0];
356
	}
357
358
	public function isValidTitleURL(string $title_url) : bool {
359
		$success = (bool) preg_match('/^[a-z0-9_]+:--:(?:0|1)$/', $title_url);
360
		if(!$success) log_message('error', "Invalid Title URL (DynastyScans): {$title_url}");
361
		return $success;
362
	}
363
	public function isValidChapter(string $chapter) : bool {
364
		$success = (bool) preg_match('/^[0-9a-z_]+$/', $chapter);
365
		if(!$success) log_message('error', 'Invalid Chapter (DynastyScans): '.$chapter);
366
		return $success;
367
	}
368
369
	public function getChapterData(string $title_string, string $chapter) : array {
370
		$title_parts = explode(':--:', $title_string);
371
		/* Known chapter url formats (# is numbers):
372
		       chapters_#A_#B - Ch#A-#B
373
		       ch_#A          - Ch#A
374
		       ch_#A_#B       - Ch#A.#B
375
		       <NOTHING>      - Oneshot (This is passed as "oneshot")
376
		*/
377
378
		$chapterData = [
379
			'url'    => 'http://dynasty-scans.com/chapters/' . $title_parts[0].'_'.$chapter,
380
			'number' => ''
381
		];
382
383
		if($chapter == 'oneshot') {
384
			$chapterData['number'] = 'oneshot';
385
		} else {
386
			$chapter = preg_replace("/^([a-zA-Z]+)/", '$1_', $chapter);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
387
			$chapterSegments = explode('_', $chapter);
388
			switch($chapterSegments[0]) {
389
				case 'ch':
390
					$chapterData['number'] = 'c'.$chapterSegments[1].(isset($chapterSegments[2]) && !empty($chapterSegments[2]) ? '.'.$chapterSegments[2] : '');
391
					break;
392
393
				case 'chapters':
394
					//This is barely ever used, but I have seen it.
395
					$chapterData['number'] = 'c'.$chapterSegments[1].'-'.$chapterSegments[2];
396
					break;
397
398
				default:
399
					//TODO: FALLBACK, ALERT ADMIN?
400
					$chapterData['number'] = $chapter;
401
					break;
402
			}
403
		}
404
		return $chapterData;
405
	}
406
407 1
	public function getTitleData(string $title_string) {
408 1
		$title_parts = explode(':--:', $title_string);
409 1
		$title_url   = $title_parts[0];
410
411 1
		$titleData = [];
412
		//FIXME: Using regex here is probably a terrible idea, but we're doing it anyway....
413
		//FIXME (ASAP): All the regex here should be checked to see if it even returns something, and we should probably error if possible.
414 1
		if($title_parts[1] == '0') {
415 1
			$content = $this->get_content('http://dynasty-scans.com/series/'.$title_url);
416 1
			$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
417
418 1
			preg_match('/<b>.*<\/b>/', $data, $matchesT);
419 1
			preg_match('/\/doujins\/[^"]+">(.+)?(?=<\/a>)<\/a>/', $data, $matchesD);
420 1
			$titleData['title'] = (!empty($matchesD) ? (substr($matchesD[1], 0, -7) !== 'Original' ? substr($matchesD[1], 0, -7).' - ' : '') : '') . substr($matchesT[0], 3, -4);
421
422 1
			$data = preg_replace('/^[\S\s]*(<dl class=\'chapter-list\'>[\S\s]*<\/dl>)[\S\s]*$/', '$1', $data);
423 1
			preg_match_all('/<dd>[\s\S]+?(?=<\/dd>)<\/dd>/', $data, $matches);
424 1
			$latest_chapter_html = array_pop($matches[0]);
425
426 1
			preg_match('/\/chapters\/([^"]+)/', $latest_chapter_html, $matches);
427 1
			$titleData['latest_chapter'] = substr($matches[1], strlen($title_url)+1);
428
			//FIXME: THIS IS A TEMP FIX, SEE https://github.com/DakuTree/manga-tracker/issues/58
429 1
			if(!$titleData['latest_chapter']) {
430
				log_message('error', 'DynastyScans::getTitleData cannot parse title properly as it contains oneshot. || URL: '.$title_url);
431
				return NULL;
432
			}
433
434 1
			preg_match('/<small>released (.*)<\/small>/', $latest_chapter_html, $matches);
435 1
			$titleData['last_updated']   = date("Y-m-d H:i:s", strtotime(str_replace('\'', '', $matches[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
436
		} elseif($title_parts[1] == '1') {
437
			$content = $this->get_content('http://dynasty-scans.com/chapters/'.$title_url);
438
			$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
439
440
			preg_match('/<b>.*<\/b>/', $data, $matchesT);
441
			preg_match('/\/doujins\/[^"]+">(.+)?(?=<\/a>)<\/a>/', $data, $matchesD);
442
			$titleData['title'] = (!empty($matchesD) ? ($matchesD[1] !== 'Original' ? $matchesD[1].' - ' : '') : '') . substr($matchesT[0], 3, -4);
443
444
			$titleData['latest_chapter'] = 'oneshot'; //This will never change
445
446
			preg_match('/<i class="icon-calendar"><\/i> (.*)<\/span>/', $data, $matches);
447
			$titleData['last_updated']   = date("Y-m-d H:i:s", strtotime($matches[1]));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
448
449
			//Oneshots are special, and really shouldn't need to be re-tracked
450
			//FIXME: We need to have a specific "no-track" complete param.
451
			$titleData['complete'] = 'Y';
452
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
453
			//FIXME: WTF?
454
		}
455 1
		return (!empty($titleData) ? $titleData : NULL);
456
	}
457
}
458
459 View Code Duplication
class MangaPanda extends Site_Model {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
460
	public function getFullTitleURL(string $title_url) : string {
461
		return "http://www.mangapanda.com/{$title_url}";
462
	}
463
464
	public function getChapterData(string $title_url, string $chapter) : array {
465
		return [
466
			'url'    => "http://www.mangapanda.com/{$title_url}/{$chapter}/",
467
			'number' => 'c'.$chapter
468
		];
469
	}
470
471
	public function isValidTitleURL(string $title_url) : bool {
472
		$success = (bool) preg_match('/^[a-z0-9-]+$/', $title_url);
473
		if(!$success) log_message('error', "Invalid Title URL (MangaPanda): {$title_url}");
474
		return $success;
475
	}
476
	public function isValidChapter(string $chapter) : bool {
477
		$success = (bool) preg_match('/^[0-9]+$/', $chapter);
478
		if(!$success) log_message('error', 'Invalid Chapter (MangaPanda): '.$chapter);
479
		return $success;
480
	}
481
482 1
	public function getTitleData(string $title_url) {
483 1
		$titleData = [];
484
485 1
		$fullURL = "http://www.mangapanda.com/{$title_url}";
486
487 1
		$content = $this->get_content($fullURL);
488 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
489 1
		if($data !== '<h1>404 Not Found</h1>') {
490
			//$data = preg_replace('/^[\S\s]*(<body id="body">[\S\s]*<\/body>)[\S\s]*$/', '$1', $data);
491
492 1
			$dom = new DOMDocument();
493 1
			libxml_use_internal_errors(true);
494 1
			$dom->loadHTML($data);
495 1
			libxml_use_internal_errors(false);
496
497 1
			$xpath = new DOMXPath($dom);
498
499 1
			$nodes_title = $xpath->query("//h2[@class='aname']");
500 1
			$nodes_row   = $xpath->query("(//table[@id='listing']/tr)[last()]");
501 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
502
				//This seems to be be the only viable way to grab the title...
503 1
				$titleData['title'] = $nodes_title->item(0)->nodeValue;
504
505 1
				$firstRow      = $nodes_row->item(0);
506 1
				$nodes_latest  = $xpath->query("td[2]",   $firstRow);
507 1
				$nodes_chapter = $xpath->query("td[1]/a", $firstRow);
508
509 1
				$titleData['latest_chapter'] = preg_replace('/^.*\/([0-9]+)$/', '$1', (string) $nodes_chapter->item(0)->getAttribute('href'));
510 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest->item(0)->nodeValue));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
511
			}
512
		} else {
513
			log_message('error', "Series missing? (MangaPanda): {$title_url}");
514
			return NULL;
515
		}
516
517 1
		return (!empty($titleData) ? $titleData : NULL);
518
	}
519
}
520
521 View Code Duplication
class MangaStream extends Site_Model {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
522
	public function getFullTitleURL(string $title_url) : string {
523
		return "https://mangastream.com/manga/{$title_url}/";
524
	}
525
526
	public function isValidTitleURL(string $title_url) : bool {
527
		$success = (bool) preg_match('/^[a-z0-9_]+$/', $title_url);
528
		if(!$success) log_message('error', "Invalid Title URL (MangaStream): {$title_url}");
529
		return $success;
530
	}
531
	public function isValidChapter(string $chapter) : bool {
532
		$success = (bool) preg_match('/^(.*?)\/[0-9]+$/', $chapter);
533
		if(!$success) log_message('error', 'Invalid Chapter (MangaStream): '.$chapter);
534
		return $success;
535
	}
536
537
	public function getChapterData(string $title_url, string $chapter) : array {
538
		return [
539
			'url'    => "https://mangastream.com/r/{$title_url}/{$chapter}",
540
			'number' => 'c'.explode('/', $chapter)[0]
541
		];
542
	}
543
544
	public function getTitleData(string $title_url) {
545
		$titleData = [];
546
547
		$fullURL = $this->getFullTitleURL($title_url);
548
549
		$content = $this->get_content($fullURL);
550
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
551
		if($data !== 'Can\'t find the manga series.') { //FIXME: We should check for he proper error here.
552
			//$data = preg_replace('/^[\S\s]*(<body id="body">[\S\s]*<\/body>)[\S\s]*$/', '$1', $data);
553
554
			$dom = new DOMDocument();
555
			libxml_use_internal_errors(true);
556
			$dom->loadHTML($data);
557
			libxml_use_internal_errors(false);
558
559
			$xpath = new DOMXPath($dom);
560
561
			$nodes_title = $xpath->query("//div[contains(@class, 'content')]/div[1]/h1");
562
			$nodes_row   = $xpath->query("//div[contains(@class, 'content')]/div[1]/table/tr[2]"); //Missing tbody here..
563
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
564
				$titleData['title'] = $nodes_title->item(0)->nodeValue;
565
566
				$firstRow      = $nodes_row->item(0);
567
				$nodes_latest  = $xpath->query("td[2]",   $firstRow);
568
				$nodes_chapter = $xpath->query("td[1]/a", $firstRow);
569
570
				$titleData['latest_chapter'] = preg_replace('/^.*\/(.*?\/[0-9]+)\/[0-9]+$/', '$1', (string) $nodes_chapter->item(0)->getAttribute('href'));
571
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest->item(0)->nodeValue));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
572
			}
573
		} else {
574
			log_message('error', "Series missing? (MangaStream): {$title_url}");
575
			return NULL;
576
		}
577
578
		return (!empty($titleData) ? $titleData : NULL);
579
	}
580
}
581
582
class WebToons extends Site_Model {
583
	/* Webtoons.com has a very weird and pointless URL format.
584
	   TITLE URL:   /#LANG#/#GENRE#/#TITLE#/list?title_no=#TITLEID#
585
	   RSS URL:     /#LANG#/#GENRE#/#TITLE#/rss?title_no=#TITLEID#
586
	   CHAPTER URL: /#LANG#/#GENRE#/#TITLE#/#CHAPTER#/viewer?title_no=#TITLEID#&episode_no=#CHAPTERID#
587
588
	   For both the title and chapter URLs, only the TITLEID and CHAPTERID are needed. Everything else can be anything at all (Well, alphanumeric at least).
589
	   The RSS URL however, requires everything to be exactly correct. I have no idea why this is, but it does mean we need to store all that info too.
590
	   We <could> not use the RSS url, and just parse via the title url, but rss is much better in the long run as it shouldn't change much.
591
592
	   FORMATS:
593
	   TITLE_URL: ID:--:LANG:--:TITLE:--:GENRE
594
	   CHAPTER:   ID:--:CHAPTER_N
595
	*/
596
	//private $validLang = ['en', 'zh-hant', 'zh-hans', 'th', 'id'];
597
598
	public function getFullTitleURL(string $title_url) : string {
599
		$title_parts = explode(':--:', $title_url);
600
		return "http://www.webtoons.com/{$title_parts[1]}/{$title_parts[3]}/{$title_parts[2]}/list?title_no={$title_parts[0]}/";
601
	}
602
603
	public function isValidTitleURL(string $title_url) : bool {
604
		$success = (bool) preg_match('/^[0-9]+:--:(?:en|zh-hant|zh-hans|th|id):--:[a-z0-9-]+:--:(?:drama|fantasy|comedy|action|slice-of-life|romance|superhero|thriller|sports|sci-fi)$/', $title_url);
605
		if(!$success) log_message('error', "Invalid Title URL (WebToons): {$title_url}");
606
		return $success;
607
	}
608
	public function isValidChapter(string $chapter) : bool {
609
		$success = (bool) preg_match('/^[0-9]+:--:.*$/', $chapter);
610
		if(!$success) log_message('error', 'Invalid Chapter (WebToons): '.$chapter);
611
		return $success;
612
	}
613
614
	public function getChapterData(string $title_url, string $chapter) : array {
615
		$title_parts   = explode(':--:', $title_url);
616
		$chapter_parts = explode(':--:', $chapter);
617
618
		return [
619
			'url'    => "http://www.webtoons.com/{$title_parts[1]}/{$title_parts[3]}/{$title_parts[2]}/{$chapter_parts[1]}/viewer?title_no={$title_parts[0]}&episode_no={$chapter_parts[0]}",
620
			'number' => $chapter_parts[1] //TODO: Possibly replace certain formats in here? Since webtoons doesn't have a standard chapter format
621
		];
622
	}
623
624 1
	public function getTitleData(string $title_url) {
625 1
		$titleData = [];
626
627 1
		$title_parts = explode(':--:', $title_url);
628 1
		$fullURL = "http://www.webtoons.com/{$title_parts[1]}/{$title_parts[3]}/{$title_parts[2]}/rss?title_no={$title_parts[0]}";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
629
630 1
		$content = $this->get_content($fullURL);
631 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
632 1
		if($data !== 'Can\'t find the manga series.') { //FIXME: We should check for he proper error here.
633 1
			$xml = simplexml_load_string($data) or die("Error: Cannot create object");
634 1
			if(isset($xml->{'channel'}->item[0])) {
635 1
				$titleData['title'] = trim((string) $xml->{'channel'}->title);
636
637 1
				$chapterURLSegments = explode('/', ((string) $xml->{'channel'}->item[0]->link));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
638 1
				$titleData['latest_chapter'] = preg_replace('/^.*?([0-9]+)$/', '$1', $chapterURLSegments[7]) . ':--:' . $chapterURLSegments[6];
639 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $xml->{'channel'}->item[0]->pubDate));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
640
			}
641
		} else {
642
			log_message('error', "Series missing? (WebToons): {$title_url}");
643
			return NULL;
644
		}
645
646 1
		return (!empty($titleData) ? $titleData : NULL);
647
	}
648
}
649
650
class KissManga extends Site_Model {
651
	/* This site is a massive pain in the ass. The only reason I'm supporting it is it's one of the few aggregator sites which actually support more risqué manga.
652
	   The main problem with this site is it has some form of bot protection. To view any part of the site normally, you need a cookie set by the bot protection.
653
654
	   To generate this cookie, we need three variables. Two are static, but the other is generated by randomly generated JS on the page.
655
	   The randomly generated JS is the troublesome part. We can't easily parse this with PHP. Both V8JS & SpiderMonkey refuse to build properly for me, so that rules that out.
656
	   The other option is using regex, but that is a rabbit hole I don't want to touch with a ten-foot pole.
657
658
	   To make the entire site work, I've built a python script to handle grabbing this cookie. This is grabbed & updated at the same time the manga are updated. The script saves the cookiejar which the PHP later reads.
659
	   The cookie has a length of 1 year, but I don't think it actually lasts that long, so we update every 6hours instead.
660
	   I should probably also mention that the cookie generated also uses your user-agent, so if it changes the cookie will break.
661
	*/
662
663
	public function getFullTitleURL(string $title_url) : string {
664
		return "http://kissmanga.com/Manga/{$title_url}";
665
	}
666
667
	public function isValidTitleURL(string $title_url) : bool {
668
		$success = (bool) preg_match('/^[A-Za-z0-9-]+/', $title_url);
669
		if(!$success) log_message('error', "Invalid Title URL (KissManga): {$title_url}");
670
		return $success;
671
	}
672
	public function isValidChapter(string $chapter) : bool {
673
		$success = (bool) preg_match('/^.*?:--:[0-9]+$/', $chapter);
674
		if(!$success) log_message('error', 'Invalid Chapter (KissManga): '.$chapter);
675
		return $success;
676
	}
677
678 View Code Duplication
	public function getChapterData(string $title_url, string $chapter) : array {
679
		$chapter_parts = explode(':--:', $chapter);
680
681
		return [
682
			'url'    => "http://kissmanga.com/Manga/{$title_url}/{$chapter_parts[0]}?id={$chapter_parts[1]}",
683
			//FIXME: KM has an extremely inconsistant chapter format which makes it difficult to parse.
684
			'number' => /*preg_replace('/--.*?$/', '', */$chapter_parts[0]/*)*/
685
		];
686
	}
687
688
	public function getTitleData(string $title_url) {
689
		$titleData = [];
690
691
		//Check if cookiejar is a day old (so we can know if something went wrong)
692
		$cookiejar_path = str_replace("public/", "_scripts/cookiejar", FCPATH);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
693
		$cookie_last_updated = filemtime($cookiejar_path);
694
		if($cookie_last_updated && ((time() - 86400) < $cookie_last_updated)) {
695
696
			$fullURL = $this->getFullTitleURL($title_url);
697
698
			$content = $this->get_content($fullURL, '', $cookiejar_path);
699
			$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
700
			if(strpos($data, 'containerRoot') !== FALSE) {
701
				//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
702
				$data = preg_replace('/^[\S\s]*(<div id="leftside">[\S\s]*)<div id="rightside">[\S\s]*$/', '$1', $data);
703
704
				$dom = new DOMDocument();
705
				libxml_use_internal_errors(true);
706
				$dom->loadHTML($data);
707
				libxml_use_internal_errors(false);
708
709
				$xpath = new DOMXPath($dom);
710
711
				$nodes_title = $xpath->query("//a[@class='bigChar']");
712
				$nodes_row   = $xpath->query("//table[@class='listing']/tr[3]");
713
				if($nodes_title->length === 1 && $nodes_row->length === 1) {
714
					$titleData['title'] = $nodes_title->item(0)->textContent;
715
716
					$firstRow      = $nodes_row->item(0);
717
					$nodes_latest  = $xpath->query("td[2]",   $firstRow);
718
					$nodes_chapter = $xpath->query("td[1]/a", $firstRow);
719
720
					$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
721
					$chapterURLSegments = explode('/', preg_replace('/\?.*$/', '', $link));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
722
					$titleData['latest_chapter'] = $chapterURLSegments[3] . ':--:' . preg_replace('/.*?([0-9]+)$/', '$1', $link);
723
					$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest->item(0)->textContent));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
724
				}
725
			} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
726
				//TODO: Throw ERRORS;
727
			}
728
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
729
			//Do nothing, wait until next update.
730
			//TODO: NAG ADMIN??
731
		}
732
733
		return (!empty($titleData) ? $titleData : NULL);
734
	}
735
}
736
737
class KireiCake extends Site_Model {
738 1
	public function getFullTitleURL(string $title_url) : string {
739 1
		return "https://reader.kireicake.com/series/{$title_url}";
740
	}
741
742
	public function isValidTitleURL(string $title_url) : bool {
743
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
744
		if(!$success) log_message('error', "Invalid Title URL (KireiCake): {$title_url}");
745
		return $success;
746
	}
747
	public function isValidChapter(string $chapter) : bool {
748
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
749
		if(!$success) log_message('error', 'Invalid Chapter (KireiCake): '.$chapter);
750
		return $success;
751
	}
752
753
	public function getChapterData(string $title_url, string $chapter) : array {
754
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
755
		$chapter_parts = explode('/', $chapter);
756
		return [
757
			'url'    => "https://reader.kireicake.com/read/{$title_url}/{$chapter}/",
758
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
759
		];
760
	}
761
762 1
	public function getTitleData(string $title_url) {
763 1
		$titleData = [];
764
765 1
		$fullURL = $this->getFullTitleURL($title_url);
766
767 1
		$content = $this->get_content($fullURL);
768 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
769 1
		if(strpos($data, '404 Page Not Found') === FALSE) {
770
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
771 1
			$data = preg_replace('/^[\S\s]*(<article>[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
772
773 1
			$dom = new DOMDocument();
774 1
			libxml_use_internal_errors(true);
775 1
			$dom->loadHTML($data);
776 1
			libxml_use_internal_errors(false);
777
778 1
			$xpath = new DOMXPath($dom);
779
780 1
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
781 1
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='element'][1]");
782 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
783 1
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
784
785 1
				$firstRow      = $nodes_row->item(0);
786 1
				$nodes_latest  = $xpath->query("div[@class='meta_r']",  $firstRow);
787 1
				$nodes_chapter = $xpath->query("div[@class='title']/a", $firstRow);
788
789 1
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
790 1
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
791 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest->item(0)->textContent)[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
792
			}
793
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
794
			//TODO: Throw ERRORS;
795
		}
796
797 1
		return (!empty($titleData) ? $titleData : NULL);
798
	}
799
}
800
801
class GameOfScanlation extends Site_Model {
802
	public function getFullTitleURL(string $title_url) : string {
803
		return "https://gameofscanlation.moe/forums/{$title_url}/";
804
	}
805
806
	public function isValidTitleURL(string $title_url) : bool {
807
		$success = (bool) preg_match('/^[a-z0-9-]+/', $title_url);
808
		if(!$success) log_message('error', "Invalid Title URL (GameOfScanlation): {$title_url}");
809
		return $success;
810
	}
811
	public function isValidChapter(string $chapter) : bool {
812
		$success = (bool) preg_match('/^[a-z0-9\.-]+$/', $chapter);
813
		if(!$success) log_message('error', 'Invalid Chapter (GameOfScanlation): '.$chapter);
814
		return $success;
815
	}
816
817
	public function getChapterData(string $title_url, string $chapter) : array {
818
		return [
819
			'url'    => "https://gameofscanlation.moe/projects/".preg_replace("/\\.[0-9]+$/", "", $title_url).'/'.$chapter.'/',
820
			'number' => preg_replace("/chapter-/", "c", preg_replace("/\\.[0-9]+$/", "", $chapter))
821
		];
822
	}
823
824
	public function getTitleData(string $title_url) {
825
		$titleData = [];
826
827
		$fullURL = $this->getFullTitleURL($title_url);
828
829
		$content = $this->get_content($fullURL);
830
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
831
		if(strpos($data, '404 Page Not Found') === FALSE) {
832
			//$data = preg_replace('/^[\S\s]*(<ol[\S\s]*)<\/ol>[\S\s]*$/', '$1', $data);
833
834
			$dom = new DOMDocument();
835
			libxml_use_internal_errors(true);
836
			$dom->loadHTML($data);
837
			libxml_use_internal_errors(false);
838
839
			$xpath = new DOMXPath($dom);
840
841
			$nodes_title = $xpath->query("//meta[@property='og:title']");
842
			$nodes_row   = $xpath->query("//ol[@class='discussionListItems']/li[1]/div[@class='home_list']/ul/li/div[@class='list_press_text']");
843
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
844
				$titleData['title'] = html_entity_decode($nodes_title->item(0)->getAttribute('content'));
845
846
				$firstRow      = $nodes_row->item(0);
847
				$nodes_latest  = $xpath->query("p[@class='author']/span|p[@class='author']/abbr", $firstRow);
848
				$nodes_chapter = $xpath->query("p[@class='text_work']/a",                         $firstRow);
849
850
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
851
				$titleData['latest_chapter'] = preg_replace('/^projects\/.*?\/(.*?)\/$/', '$1', $link);
852
				$titleData['last_updated'] =  date("Y-m-d H:i:s", (int) $nodes_latest->item(0)->getAttribute('data-time'));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
853
			} else {
854
				log_message('error', "GameOfScanlation: Unable to find nodes.");
855
				return NULL;
856
			}
857
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
858
			//TODO: Throw ERRORS;
859
		}
860
861
		return (!empty($titleData) ? $titleData : NULL);
862
	}
863
}
864
865
class MangaCow extends Site_Model {
866 1
	public function getFullTitleURL(string $title_url) : string {
867 1
		return "http://mngcow.co/{$title_url}/";
868
	}
869
870
	public function isValidTitleURL(string $title_url) : bool {
871
		$success = (bool) preg_match('/^[a-zA-Z0-9_]+/', $title_url);
872
		if(!$success) log_message('error', "Invalid Title URL (MangaCow): {$title_url}");
873
		return $success;
874
	}
875
	public function isValidChapter(string $chapter) : bool {
876
		$success = (bool) preg_match('/^[0-9]+$/', $chapter);
877
		if(!$success) log_message('error', 'Invalid Chapter (MangaCow): '.$chapter);
878
		return $success;
879
	}
880
881
	public function getChapterData(string $title_url, string $chapter) : array {
882
		return [
883
			'url'    => $this->getFullTitleURL($title_url).$chapter.'/',
884
			'number' => "c{$chapter}"
885
		];
886
	}
887
888 1
	public function getTitleData(string $title_url) {
889 1
		$titleData = [];
890
891 1
		$fullURL = $this->getFullTitleURL($title_url);
892
893 1
		$content = $this->get_content($fullURL);
894 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
895 1
		if(strpos($data, '404 Page Not Found') === FALSE) {
896 1
			$dom = new DOMDocument();
897 1
			libxml_use_internal_errors(true);
898 1
			$dom->loadHTML($data);
899 1
			libxml_use_internal_errors(false);
900
901 1
			$xpath = new DOMXPath($dom);
902
903 1
			$nodes_title = $xpath->query("//h4");
904 1
			$nodes_row   = $xpath->query("//ul[contains(@class, 'mng_chp')]/li[1]/a[1]");
905 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
906 1
				$titleData['title'] = trim($nodes_title->item(0)->nodeValue);
907
908 1
				$nodes_chapter = $nodes_row;
909 1
				$nodes_latest  = $xpath->query("b[@class='dte']", $nodes_row->item(0));
910
911 1
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
912 1
				$titleData['latest_chapter'] = preg_replace('/^.*\/([0-9]+)\/$/', '$1', $link);
913 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) substr($nodes_latest->item(0)->getAttribute('title'), 13)));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
914
			} else {
915
				log_message('error', "MangaCow: Unable to find nodes.");
916 1
				return NULL;
917
			}
918
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
919
			//TODO: Throw ERRORS;
920
		}
921
922 1
		return (!empty($titleData) ? $titleData : NULL);
923
	}
924
}
925
926 View Code Duplication
class SeaOtterScans extends Site_Model {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
927 1
	public function getFullTitleURL(string $title_url) : string {
928 1
		return "http://reader.seaotterscans.com/series/{$title_url}";
929
	}
930
931
	public function isValidTitleURL(string $title_url) : bool {
932
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
933
		if(!$success) log_message('error', "Invalid Title URL (SeaOtterScans): {$title_url}");
934
		return $success;
935
	}
936
	public function isValidChapter(string $chapter) : bool {
937
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
938
		if(!$success) log_message('error', 'Invalid Chapter (SeaOtterScans): '.$chapter);
939
		return $success;
940
	}
941
942
	public function getChapterData(string $title_url, string $chapter) : array {
943
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
944
		$chapter_parts = explode('/', $chapter);
945
		return [
946
			'url'    => "https://reader.seaotterscans.com/read/{$title_url}/{$chapter}/",
947
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
948
		];
949
	}
950
951 1
	public function getTitleData(string $title_url) {
952 1
		$titleData = [];
953
954 1
		$fullURL = $this->getFullTitleURL($title_url);
955
956 1
		$content = $this->get_content($fullURL);
957 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
958 1
		if(strpos($data, '404 Page Not Found') === FALSE) {
959
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
960 1
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
961
962 1
			$dom = new DOMDocument();
963 1
			libxml_use_internal_errors(true);
964 1
			$dom->loadHTML($data);
965 1
			libxml_use_internal_errors(false);
966
967 1
			$xpath = new DOMXPath($dom);
968
969 1
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
970
971
			//SOO sometimes uses volume groups which are above recent chapters (if they haven't been grouped yet), so make sure we check for both.
972 1
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
973 1
			if($nodes_row->length !== 1) {
974 1
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
975
			}
976 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
977 1
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
978
979 1
				$firstRow = $nodes_row->item(0);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
980 1
				$nodes_latest  = $xpath->query("div[@class='meta_r']",  $firstRow);
981 1
				$nodes_chapter = $xpath->query("div[@class='title']/a", $firstRow);
982
983 1
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
984 1
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
985 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest[0]->textContent)[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
986
			}
987
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
988
			//TODO: Throw ERRORS;
989
		}
990
991 1
		return (!empty($titleData) ? $titleData : NULL);
992
	}
993
}
994
995 View Code Duplication
class HelveticaScans extends Site_Model {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
996 1
	public function getFullTitleURL(string $title_url) : string {
997 1
		return "http://helveticascans.com/reader/series/{$title_url}";
998
	}
999
1000
	public function isValidTitleURL(string $title_url) : bool {
1001
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
1002
		if(!$success) log_message('error', "Invalid Title URL (HelveticaScans): {$title_url}");
1003
		return $success;
1004
	}
1005
	public function isValidChapter(string $chapter) : bool {
1006
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
1007
		if(!$success) log_message('error', 'Invalid Chapter (HelveticaScans): '.$chapter);
1008
		return $success;
1009
	}
1010
1011
	public function getChapterData(string $title_url, string $chapter) : array {
1012
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
1013
		$chapter_parts = explode('/', $chapter);
1014
		return [
1015
			'url'    => "http://helveticascans.com/reader/read/{$title_url}/{$chapter}/",
1016
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
1017
		];
1018
	}
1019
1020 1
	public function getTitleData(string $title_url) {
1021 1
		$titleData = [];
1022
1023 1
		$fullURL = $this->getFullTitleURL($title_url);
1024
1025 1
		$content = $this->get_content($fullURL);
1026 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1027 1
		if(strpos($data, '404 Page Not Found') === FALSE) {
1028
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
1029 1
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
1030
1031 1
			$dom = new DOMDocument();
1032 1
			libxml_use_internal_errors(true);
1033 1
			$dom->loadHTML($data);
1034 1
			libxml_use_internal_errors(false);
1035
1036 1
			$xpath = new DOMXPath($dom);
1037
1038 1
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
1039 1
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
1040 1
			if($nodes_row->length !== 1) {
1041
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
1042
			}
1043 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
1044 1
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
1045
1046
1047 1
				$nodes_latest  = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
1048 1
				$nodes_chapter = $xpath->query("div[@class='title']/a", $nodes_row[0]);
1049
1050 1
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1051 1
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
1052 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest->item(0)->textContent)[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1053
			}
1054
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
1055
			//TODO: Throw ERRORS;
1056
		}
1057
1058 1
		return (!empty($titleData) ? $titleData : NULL);
1059
	}
1060
}
1061
1062 View Code Duplication
class SenseScans extends Site_Model {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1063 1
	public function getFullTitleURL(string $title_url) : string {
1064 1
		return "http://reader.sensescans.com/series/{$title_url}";
1065
	}
1066
1067
	public function isValidTitleURL(string $title_url) : bool {
1068
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
1069
		if(!$success) log_message('error', "Invalid Title URL (SenseScans): {$title_url}");
1070
		return $success;
1071
	}
1072
	public function isValidChapter(string $chapter) : bool {
1073
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
1074
		if(!$success) log_message('error', 'Invalid Chapter (SenseScans): '.$chapter);
1075
		return $success;
1076
	}
1077
1078
	public function getChapterData(string $title_url, string $chapter) : array {
1079
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
1080
		$chapter_parts = explode('/', $chapter);
1081
		return [
1082
			'url'    => "http://reader.sensescans.com/read/{$title_url}/{$chapter}/",
1083
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
1084
		];
1085
	}
1086
1087 1
	public function getTitleData(string $title_url) {
1088 1
		$titleData = [];
1089
1090 1
		$fullURL = $this->getFullTitleURL($title_url);
1091
1092 1
		$content = $this->get_content($fullURL);
1093 1
		$data = $content['body'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1094 1
		if(strpos($data, '404 Page Not Found') === FALSE) {
1095
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
1096 1
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
1097
1098 1
			$dom = new DOMDocument();
1099 1
			libxml_use_internal_errors(true);
1100 1
			$dom->loadHTML($data);
1101 1
			libxml_use_internal_errors(false);
1102
1103 1
			$xpath = new DOMXPath($dom);
1104
1105 1
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
1106 1
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
1107 1
			if($nodes_row->length !== 1) {
1108
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
1109
			}
1110 1
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
1111 1
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
1112
1113 1
				$nodes_latest    = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1114 1
				$nodes_chapter   = $xpath->query("div[@class='title']/a", $nodes_row[0]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 3 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1115
1116 1
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1117 1
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
1118 1
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest->item(0)->textContent)[1])));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
1119
			}
1120
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
1121
			//TODO: Throw ERRORS;
1122
		}
1123
1124 1
		return (!empty($titleData) ? $titleData : NULL);
1125
	}
1126
}
1127