Completed
Push — master ( 4c0e90...a03a76 )
by Angus
02:49
created

Base_Site_Model   C

Complexity

Total Complexity 63

Size/Duplication

Total Lines 459
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 24.18%

Importance

Changes 0
Metric Value
dl 0
loc 459
ccs 37
cts 153
cp 0.2418
rs 5.8893
c 0
b 0
f 0
wmc 63
lcom 2
cbo 5

15 Methods

Rating   Name   Duplication   Size   Complexity  
getFullTitleURL() 0 1 ?
getChapterData() 0 1 ?
getTitleData() 0 1 ?
A doCustomUpdate() 0 1 1
A __construct() 0 7 1
A isValidTitleURL() 0 5 2
A isValidChapter() 0 5 2
C get_content() 0 60 10
B handleCloudFlare() 0 27 4
C parseTitleDataDOM() 0 63 15
A cleanTitleDataDOM() 0 3 1
C doCustomFollow() 0 27 7
A handleCustomFollow() 0 6 2
A doCustomCheck() 0 12 2
C doCustomCheckCompare() 0 50 16

How to fix   Complexity   

Complex Class

Complex classes like Base_Site_Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Base_Site_Model, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
/**
4
 * Class Tracker_Sites_Model
5
 */
6
class Tracker_Sites_Model extends CI_Model {
7 127
	public function __construct() {
8 127
		parent::__construct();
9 127
	}
10
11
	public function __get($name) {
12
		//TODO: Is this a good idea? There wasn't a good consensus on if this is good practice or not..
13
		//      It's probably a minor speed reduction, but that isn't much of an issue.
14
		//      An alternate solution would simply have a function which generates a PHP file with code to load each model. Similar to: https://github.com/shish/shimmie2/blob/834bc740a4eeef751f546979e6400fd089db64f8/core/util.inc.php#L1422
15
		$validClasses = [
16
			'Base_Site_Model',
17
			'Base_FoolSlide_Site_Model',
18
			'Base_myMangaReaderCMS_Site_Model',
19
			'Base_GlossyBright_Site_Model',
20
			'Base_Roku_Site_Model'
21
		];
22
		if(!class_exists($name) || !(in_array(get_parent_class($name), $validClasses))) {
23
			return get_instance()->{$name};
24
		} else {
25
			$this->loadSite($name);
26
			return $this->{$name};
27
		}
28
	}
29
30
	private function loadSite(string $siteName) : void {
31
		$this->{$siteName} = new $siteName();
32
	}
33
}
34
35
abstract class Base_Site_Model extends CI_Model {
36
	public $site          = '';
37
	public $titleFormat   = '//';
38
	public $chapterFormat = '//';
39
	public $hasCloudFlare = FALSE;
40
	public $userAgent     = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36';
41
42
	public $baseURL = '';
43
44
	/**
45
	 * 0: No custom updater.
46
	 * 1: Uses following page.
47
	 * 2: Uses latest releases page.
48
	 */
49
	public $customType = 0;
50
51
	public $canHaveNoChapters = FALSE;
52
53 16
	public function __construct() {
54 16
		parent::__construct();
55
56 16
		$this->load->database();
57
58 16
		$this->site = get_class($this);
59 16
	}
60
61
	/**
62
	 * Generates URL to the title page of the requested series.
63
	 *
64
	 * NOTE: In some cases, we are required to store more data in the title_string than is needed to generate the URL. (Namely as the title_string is our unique identifier for that series)
65
	 *       When storing additional data, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed.
66
	 *
67
	 * Example:
68
	 *    return "http://mangafox.me/manga/{$title_url}/";
69
	 *
70
	 * Example (with extra data):
71
	 *    $title_parts = explode(':--:', title_url);
72
	 *    return "https://bato.to/comic/_/comics/-r".$title_parts[0];
73
	 *
74
	 * @param string $title_url
75
	 * @return string
76
	 */
77
	abstract public function getFullTitleURL(string $title_url) : string;
78
79
	/**
80
	 * Generates chapter data from given $title_url and $chapter.
81
	 *
82
	 * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format.
83
	 *
84
	 * NOTE: In some cases, we are required to store the chapter number, and the segment required to generate the chapter URL separately.
85
	 *       Much like when generating the title URL, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed.
86
	 *
87
	 * Example:
88
	 *     return [
89
	 *        'url'    => $this->getFullTitleURL($title_url).'/'.$chapter,
90
	 *        'number' => "c{$chapter}"
91
	 *    ];
92
	 *
93
	 * @param string $title_url
94
	 * @param string $chapter
95
	 * @return array [url, number]
96
	 */
97
	abstract public function getChapterData(string $title_url, string $chapter) : array;
98
99
	/**
100
	 * Used to get the latest chapter of given $title_url.
101
	 *
102
	 * This <should> utilize both get_content and parseTitleDataDOM functions when possible, as these can both reduce a lot of the code required to set this up.
103
	 *
104
	 * $titleData params must be set accordingly:
105
	 * * `title` should always be used with html_entity_decode.
106
	 * * `latest_chapter` must match $this->chapterFormat.
107
	 * * `last_updated` should always be in date("Y-m-d H:i:s") format.
108
	 * * `followed` should never be set within via getTitleData, with the exception of via a array_merge with doCustomFollow.
109
	 *
110
	 * $firstGet is set to true when the series is first added to the DB, and is used to follow the series on given site (if possible).
111
	 *
112
	 * @param string $title_url
113
	 * @param bool   $firstGet
114
	 * @return array|null [title,latest_chapter,last_updated,followed?]
115
	 */
116
	abstract public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array;
117
118
	/**
119
	 * Validates given $title_url against titleFormat.
120
	 *
121
	 * Failure to match against titleFormat will stop the series from being added to the DB.
122
	 *
123
	 * @param string $title_url
124
	 * @return bool
125
	 */
126 2
	final public function isValidTitleURL(string $title_url) : bool {
127 2
		$success = (bool) preg_match($this->titleFormat, $title_url);
128 2
		if(!$success) log_message('error', "Invalid Title URL ({$this->site}): {$title_url}");
129 2
		return $success;
130
	}
131
132
	/**
133
	 * Validates given $chapter against chapterFormat.
134
	 *
135
	 * Failure to match against chapterFormat will stop the chapter being updated.
136
	 *
137
	 * @param string $chapter
138
	 * @return bool
139
	 */
140 2
	final public function isValidChapter(string $chapter) : bool {
141 2
		$success = (bool) preg_match($this->chapterFormat, $chapter);
142 2
		if(!$success) log_message('error', "Invalid Chapter ({$this->site}): {$chapter}");
143 2
		return $success;
144
	}
145
146
	/**
147
	 * Used by getTitleData (& similar functions) to get the requested page data.
148
	 *
149
	 * @param string $url
150
	 * @param string $cookie_string
151
	 * @param string $cookiejar_path
152
	 * @param bool   $follow_redirect
153
	 * @param bool   $isPost
154
	 * @param array  $postFields
155
	 *
156
	 * @return array|bool
157
	 */
158
	final protected function get_content(string $url, string $cookie_string = "", string $cookiejar_path = "", bool $follow_redirect = FALSE, bool $isPost = FALSE, array $postFields = []) {
159
		$refresh = TRUE; //For sites that have CloudFlare, we want to loop get_content again.
160
		$loops   = 0;
161
		while($refresh && $loops < 2) {
162
			$refresh = FALSE;
163
			$loops++;
164
165
			$ch = curl_init();
166
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
167
			curl_setopt($ch, CURLOPT_ENCODING , "gzip");
168
			//curl_setopt($ch, CURLOPT_VERBOSE, 1);
169
			curl_setopt($ch, CURLOPT_HEADER, 1);
170
171
			if($follow_redirect)        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
172
173
			if($cookies = $this->cache->get("cloudflare_{$this->site}")) {
174
				$cookie_string .= "; {$cookies}";
175
			}
176
177
			if(!empty($cookie_string))  curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
178
			if(!empty($cookiejar_path)) curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar_path);
179
180
			//Some sites check the useragent for stuff, use a pre-defined user-agent to avoid stuff.
181
			curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
182
183
			//NOTE: This is required for SSL URLs for now. Without it we tend to get error code 60.
184
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //FIXME: This isn't safe, but it allows us to grab SSL URLs
185
186
			curl_setopt($ch, CURLOPT_URL, $url);
187
188
			if($isPost) {
189
				curl_setopt($ch,CURLOPT_POST, count($postFields));
190
				curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($postFields));
191
			}
192
193
			$response = curl_exec($ch);
194
195
			$this->Tracker->admin->incrementRequests();
196
197
			if($response === FALSE) {
198
				log_message('error', "curl failed with error: ".curl_errno($ch)." | ".curl_error($ch));
199
				//FIXME: We don't always account for FALSE return
200
				return FALSE;
201
			}
202
203
			$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
204
			$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
205
			$headers     = http_parse_headers(substr($response, 0, $header_size));
206
			$body        = substr($response, $header_size);
207
			curl_close($ch);
208
209
			if($status_code === 503) $refresh = $this->handleCloudFlare($url, $body);
210
		}
211
212
		return [
213
			'headers'     => $headers,
0 ignored issues
show
Bug introduced by
The variable $headers does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
214
			'status_code' => $status_code,
0 ignored issues
show
Bug introduced by
The variable $status_code does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
215
			'body'        => $body
0 ignored issues
show
Bug introduced by
The variable $body does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
216
		];
217
	}
218
219
	final private function handleCloudFlare(string $url, string $body) : bool {
220
		$refresh = FALSE;
221
222
		if((strpos($body, 'DDoS protection by Cloudflare') !== FALSE) || (strpos($body, '<input type="hidden" id="jschl-answer" name="jschl_answer"/>') !== FALSE)) {
223
			//print "Cloudflare detected? Grabbing Cookies.\n";
224
			if(!$this->hasCloudFlare) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
225
				//TODO: Site appears to have enabled CloudFlare, disable it and contact admin.
226
				//      We'll continue to bypass CloudFlare as this may occur in a loop.
227
			}
228
229
			$urlData = [
230
				'url'        => $url,
231
				'user_agent' => $this->userAgent
232
			];
233
			//TODO: shell_exec seems bad since the URLs "could" be user inputted? Better way of doing this?
234
			$result = shell_exec('python '.APPPATH.'../_scripts/get_cloudflare_cookie.py '.escapeshellarg(json_encode($urlData)));
235
			$cookieData = json_decode($result, TRUE);
236
237
			$this->cache->save("cloudflare_{$this->site}", $cookieData['cookies'],  31536000 /* 1 year, or until we renew it */);
238
			log_message('debug', "Saving CloudFlare Cookies for {$this->site}");
239
240
			$refresh = TRUE;
241
		} 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...
242
			//Either site doesn't have CloudFlare or we have bypassed it. Either is good!
243
		}
244
		return $refresh;
245
	}
246
247
	/**
248
	 * Used by getTitleData to get the title, latest_chapter & last_updated data from the data returned by get_content.
249
	 *
250
	 * parseTitleDataDOM checks if the data returned by get_content is valid via a few simple checks.
251
	 * * If the request was actually successful, had a valid status code & data wasn't empty. We also do an additional check on an optional $failure_string param, which will throw a failure if it's matched.
252
	 *
253
	 * Data is cleaned by cleanTitleDataDOM prior to being passed to DOMDocument.
254
	 *
255
	 * All $node_* params must be XPath to the requested node, and must only return 1 result. Anything else will throw a failure.
256
	 *
257
	 * @param array  $content
258
	 * @param string $title_url
259
	 * @param string $node_title_string
260
	 * @param string $node_row_string
261
	 * @param string $node_latest_string
262
	 * @param string $node_chapter_string
263
	 * @param string $failure_string
264
	 * @param string $no_chapters_string
265
	 * @return DOMElement[]|false [nodes_title,nodes_chapter,nodes_latest]
266
	 */
267
	final protected function parseTitleDataDOM(
268
		$content, string $title_url,
269
		string $node_title_string, string $node_row_string,
270
		string $node_latest_string, string $node_chapter_string,
271
		string $failure_string = "", string $no_chapters_string = "") {
272
273
		if(!is_array($content)) {
274
			log_message('error', "{$this->site} : {$title_url} | Failed to grab URL (See above curl error)");
275
		} else {
276
			list('headers' => $headers, 'status_code' => $status_code, 'body' => $data) = $content;
0 ignored issues
show
Unused Code introduced by
The assignment to $headers is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
277
278
			if(!($status_code >= 200 && $status_code < 300)) {
279
				log_message('error', "{$this->site} : {$title_url} | Bad Status Code ({$status_code})");
280
			} else if(empty($data)) {
281
				log_message('error', "{$this->site} : {$title_url} | Data is empty? (Status code: {$status_code})");
282
			} else if($failure_string !== "" && strpos($data, $failure_string) !== FALSE) {
283
				log_message('error', "{$this->site} : {$title_url} | Failure string matched");
284
			} else {
285
				$data = $this->cleanTitleDataDOM($data); //This allows us to clean the DOM prior to parsing. It's faster to grab the only part we need THEN parse it.
286
287
				$dom = new DOMDocument();
288
				libxml_use_internal_errors(TRUE);
289
				$dom->loadHTML('<?xml encoding="utf-8" ?>' . $data);
290
				libxml_use_internal_errors(FALSE);
291
292
				$xpath = new DOMXPath($dom);
293
				$nodes_title = $xpath->query($node_title_string);
294
				$nodes_row   = $xpath->query($node_row_string);
295
				if($nodes_title->length === 1) {
296
					if($nodes_row->length === 1) {
297
						$firstRow      = $nodes_row->item(0);
298
						$nodes_latest  = $xpath->query($node_latest_string,  $firstRow);
299
300
						if($node_chapter_string !== '') {
301
							$nodes_chapter = $xpath->query($node_chapter_string, $firstRow);
302
						} else {
303
							$nodes_chapter = $nodes_row;
304
						}
305
306
						if($nodes_latest->length === 1 && $nodes_chapter->length === 1) {
307
							return [
308
								'nodes_title'   => $nodes_title->item(0),
309
								'nodes_latest'  => $nodes_latest->item(0),
310
								'nodes_chapter' => $nodes_chapter->item(0)
311
							];
312
						} else {
313
							log_message('error', "{$this->site} : {$title_url} | Invalid amount of nodes (LATEST: {$nodes_latest->length} | CHAPTER: {$nodes_chapter->length})");
314
						}
315
					} elseif($this->canHaveNoChapters && !empty($no_chapters_string) && strpos($data, $no_chapters_string) !== FALSE) {
316
						return [
317
							'nodes_title' => $nodes_title->item(0)
318
						];
319
					} else {
320
						log_message('error', "{$this->site} : {$title_url} | Invalid amount of nodes (ROW: {$nodes_row->length})");
321
					}
322
				} else {
323
					log_message('error', "{$this->site} : {$title_url} | Invalid amount of nodes (TITLE: {$nodes_title->length})");
324
				}
325
			}
326
		}
327
328
		return FALSE;
329
	}
330
331
	/**
332
	 * Used by parseTitleDataDOM to clean the data prior to passing it to DOMDocument & DOMXPath.
333
	 * This is mostly done as an (assumed) speed improvement due to the reduced amount of DOM to parse, or simply just making it easier to parse with XPath.
334
	 *
335
	 * @param string $data
336
	 * @return string
337
	 */
338
	public function cleanTitleDataDOM(string $data) : string {
339
		return $data;
340
	}
341
342
	/**
343
	 * Used to follow a series on given site if supported.
344
	 *
345
	 * This is called by getTitleData if $firstGet is true (which occurs when the series is first being added to the DB).
346
	 *
347
	 * Most of the actual following is done by handleCustomFollow.
348
	 *
349
	 * @param string $data
350
	 * @param array  $extra
351
	 * @return array
352
	 */
353
	final public function doCustomFollow(string $data = "", array $extra = []) : array {
354
		$titleData = [];
355
		$this->handleCustomFollow(function($content, $id, closure $successCallback = NULL) use(&$titleData) {
356
			if(is_array($content)) {
357
				if(array_key_exists('status_code', $content)) {
358
					$statusCode = $content['status_code'];
359
					if($statusCode === 200) {
360
						$isCallable = is_callable($successCallback);
361
						if(($isCallable && $successCallback($content['body'])) || !$isCallable) {
362
							$titleData['followed'] = 'Y';
363
364
							log_message('info', "doCustomFollow succeeded for {$id}");
365
						} else {
366
							log_message('error', "doCustomFollow failed (Invalid response?) for {$id}");
367
						}
368
					} else {
369
						log_message('error', "doCustomFollow failed (Invalid status code ({$statusCode})) for {$id}");
370
					}
371
				} else {
372
					log_message('error', "doCustomFollow failed (Missing status code?) for {$id}");
373
				}
374
			} else {
375
				log_message('error', "doCustomFollow failed (Failed request) for {$id}");
376
			}
377
		}, $data, $extra);
378
		return $titleData;
379
	}
380
381
	/**
382
	 * Used by doCustomFollow to handle following series on sites.
383
	 *
384
	 * Uses get_content to get data.
385
	 *
386
	 * $callback must return ($content, $id, closure $successCallback = NULL).
387
	 * * $content is simply just the get_content data.
388
	 * * $id is the dbID. This should be passed by the $extra arr.
389
	 * * $successCallback is an optional success check to make sure the series was properly followed.
390
	 *
391
	 * @param callable $callback
392
	 * @param string   $data
393
	 * @param array    $extra
394
	 */
395
	public function handleCustomFollow(callable $callback, string $data = "", array $extra = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
396
		if($this->customType === 2) {
397
			$content = ['status_code' => 200];
398
			$callback($content, $extra['id']);
399
		}
400
	}
401
402
	/**
403
	 * Used to check the sites following page for new updates (if supported).
404
	 * This should work much like getTitleData, but instead checks the following page.
405
	 *
406
	 * This must return an array containing arrays of each of the chapters data.
407
	 */
408
	public function doCustomUpdate() {}
409
410
	/**
411
	 * Used by the custom updater to check if a chapter looks newer than the current one.
412
	 *
413
	 * This calls doCustomCheckCompare which handles the majority of the checking.
414
	 * NOTE: Depending on the site, you may need to call getChapterData to get the chapter number to be used with this.
415
	 *
416
	 * @param string $oldChapterString
417
	 * @param string $newChapterString
418
	 * @return bool
419
	 */
420
	public function doCustomCheck(?string $oldChapterString, string $newChapterString) : bool {
421
		if(!is_null($oldChapterString)) {
422
			$oldChapterSegments = explode('/', $this->getChapterData('', $oldChapterString)['number']);
423
			$newChapterSegments = explode('/', $this->getChapterData('', $newChapterString)['number']);
424
425
			$status = $this->doCustomCheckCompare($oldChapterSegments, $newChapterSegments);
426
		} else {
427
			$status = TRUE;
428
		}
429
430
		return $status;
431
	}
432
433
	/**
434
	 * Used by doCustomCheck to check if a chapter looks newer than the current one.
435
	 * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format.
436
	 *
437
	 * To avoid issues with the occasional off case, this will only ever return true if we are 100% sure that the new chapter is newer than the old one.
438
	 *
439
	 * @param array $oldChapterSegments
440
	 * @param array $newChapterSegments
441
	 * @return bool
442
	 */
443 12
	final public function doCustomCheckCompare(array $oldChapterSegments, array $newChapterSegments) : bool {
444
		//NOTE: We only need to check against the new chapter here, as that is what is used for confirming update.
445 12
		$status = FALSE;
446
447
		//Make sure we have a volume element
448 12
		if(count($oldChapterSegments) === 1) array_unshift($oldChapterSegments, 'v0');
449 12
		if(count($newChapterSegments) === 1) array_unshift($newChapterSegments, 'v0');
450
451 12
		$oldCount = count($oldChapterSegments);
452 12
		$newCount = count($newChapterSegments);
453 12
		if($newCount === $oldCount) {
454
			//Make sure chapter format looks correct.
455
			//NOTE: We only need to check newCount as we know oldCount is the same count.
456 12
			if($newCount === 2) {
457
				//FIXME: Can we loop this?
458 12
				$oldVolume = substr(array_shift($oldChapterSegments), 1);
459 12
				$newVolume = substr(array_shift($newChapterSegments), 1);
460
461
				//Forcing volume to 0 as TBD might not be the latest (although it can be, but that is covered by other checks)
462 12
				if(in_array($oldVolume, ['TBD', 'TBA', 'NA', 'LMT'])) $oldVolume = 0;
463 12
				if(in_array($newVolume, ['TBD', 'TBA', 'NA', 'LMT'])) $newVolume = 0;
464
465 12
				$oldVolume = floatval($oldVolume);
466 12
				$newVolume = floatval($newVolume);
467
			} else {
468
				$oldVolume = 0;
469
				$newVolume = 0;
470
			}
471 12
			$oldChapter = floatval(substr(array_shift($oldChapterSegments), 1));
472 12
			$newChapter = floatval(substr(array_shift($newChapterSegments), 1));
473
474 12
			if($newChapter > $oldChapter && ($oldChapter >= 10 && $newChapter >= 10)) {
475
				//$newChapter is higher than $oldChapter AND $oldChapter and $newChapter are both more than 10
476
				//This is intended to cover the /majority/ of valid updates, as we technically shouldn't have to check volumes.
477
478 4
				$status = TRUE;
479 8
			} elseif($newVolume > $oldVolume && ($oldChapter < 10 && $newChapter < 10)) {
480
				//This is pretty much just to match a one-off case where the site doesn't properly increment chapter numbers across volumes, and instead does something like: v1/c1..v1/c5, v2/c1..v1/c5 (and so on).
481 1
				$status = TRUE;
482 7
			} elseif($newVolume > $oldVolume && $newChapter >= $oldChapter) {
483
				//$newVolume is higher, and chapter is higher so no need to check chapter.
484 2
				$status = TRUE;
485 5
			} elseif($newChapter > $oldChapter) {
486
				//$newVolume isn't higher, but chapter is.
487
				$status = TRUE;
488
			}
489
		}
490
491 12
		return $status;
492
	}
493
}
494
495
abstract class Base_FoolSlide_Site_Model extends Base_Site_Model {
496
	public $titleFormat   = '/^[a-z0-9_-]+$/';
497
	public $chapterFormat = '/^(?:en(?:-us)?|pt|es)\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/';
498
	public $customType    = 2;
499
500
	public function getFullTitleURL(string $title_url) : string {
501
		return "{$this->baseURL}/series/{$title_url}";
502
	}
503
504
	public function getChapterData(string $title_url, string $chapter) : array {
505
		$chapter_parts = explode('/', $chapter); //returns #LANG#/#VOLUME#/#CHAPTER#/#CHAPTER_EXTRA#(/#PAGE#/)
506
		return [
507
			'url'    => $this->getChapterURL($title_url, $chapter),
508
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
509
		];
510
	}
511
	public function getChapterURL(string $title_url, string $chapter) : string {
512
		return "{$this->baseURL}/read/{$title_url}/{$chapter}/";
513
	}
514
515
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
516
		$titleData = [];
517
518
		$jsonURL = $this->getJSONTitleURL($title_url);
519
		if($content = $this->get_content($jsonURL)) {
520
			$json = json_decode($content['body'], TRUE);
521
			if($json && isset($json['chapters']) && count($json['chapters']) > 0) {
522
				$titleData['title'] = trim($json['comic']['name']);
523
524
				//FoolSlide title API doesn't appear to let you sort (yet every other API method which has chapters does, so we need to sort ourselves..
525
				usort($json['chapters'], function($a, $b) {
526
					return floatval("{$b['chapter']['chapter']}.{$b['chapter']['subchapter']}") <=> floatval("{$a['chapter']['chapter']}.{$a['chapter']['subchapter']}");
527
				});
528
				$latestChapter = reset($json['chapters'])['chapter'];
529
530
				$latestChapterString = "{$latestChapter['language']}/{$latestChapter['volume']}/{$latestChapter['chapter']}";
531
				if($latestChapter['subchapter'] !== '0') {
532
					$latestChapterString .= "/{$latestChapter['subchapter']}";
533
				}
534
				$titleData['latest_chapter'] = $latestChapterString;
535
536
				//No need to use date() here since this is already formatted as such.
537
				$titleData['last_updated'] = ($latestChapter['updated'] !== '0000-00-00 00:00:00' ? $latestChapter['updated'] : $latestChapter['created']);
538
			}
539
		}
540
541
		return (!empty($titleData) ? $titleData : NULL);
542
	}
543
544
	public function doCustomUpdate() {
545
		$titleDataList = [];
546
547
		$jsonURL = $this->getJSONUpdateURL();
548
		if(($content = $this->get_content($jsonURL)) && $content['status_code'] == 200) {
549
			if(($json = json_decode($content['body'], TRUE)) && isset($json['chapters'])) {
550
				//This should fix edge cases where chapters are uploaded in bulk in the wrong order (HelveticaScans does this with Mousou Telepathy).
551
				usort($json['chapters'], function($a, $b) {
552
					$a_date = new DateTime($a['chapter']['updated'] !== '0000-00-00 00:00:00' ? $a['chapter']['updated'] : $a['chapter']['created']);
553
					$b_date = new DateTime($b['chapter']['updated'] !== '0000-00-00 00:00:00' ? $b['chapter']['updated'] : $b['chapter']['created']);
554
					return $b_date <=> $a_date;
555
				});
556
557
				$parsedTitles = [];
558
				foreach($json['chapters'] as $chapterData) {
559
					if(!in_array($chapterData['comic']['stub'], $parsedTitles)) {
560
						$parsedTitles[] = $chapterData['comic']['stub'];
561
562
						$titleData = [];
563
						$titleData['title'] = trim($chapterData['comic']['name']);
564
565
						$latestChapter = $chapterData['chapter'];
566
567
						$latestChapterString = "en/{$latestChapter['volume']}/{$latestChapter['chapter']}";
568
						if($latestChapter['subchapter'] !== '0') {
569
							$latestChapterString .= "/{$latestChapter['subchapter']}";
570
						}
571
						$titleData['latest_chapter'] = $latestChapterString;
572
573
						//No need to use date() here since this is already formatted as such.
574
						$titleData['last_updated'] = ($latestChapter['updated'] !== '0000-00-00 00:00:00' ? $latestChapter['updated'] : $latestChapter['created']);
575
576
						$titleDataList[$chapterData['comic']['stub']] = $titleData;
577
					} else {
578
						//We already have title data for this title.
579
						continue;
580
					}
581
				}
582
			} else {
583
				log_message('error', "{$this->site} - Custom updating failed (no chapters arg?) for {$this->baseURL}.");
584
			}
585
		} else {
586
			log_message('error', "{$this->site} - Custom updating failed for {$this->baseURL}.");
587
		}
588
589
		return $titleDataList;
590
	}
591
592
	public function getJSONTitleURL(string $title_url) : string {
593
		return "{$this->baseURL}/api/reader/comic/stub/{$title_url}/format/json";
594
	}
595
	public function getJSONUpdateURL() : string {
596
		return "{$this->baseURL}/api/reader/chapters/orderby/desc_created/format/json";
597
	}
598
}
599
600
abstract class Base_myMangaReaderCMS_Site_Model extends Base_Site_Model {
601
	public $titleFormat   = '/^[a-zA-Z0-9_-]+$/';
602
	public $chapterFormat = '/^(?:oneshot|(?:chapter-)?[0-9\.]+)$/';
603
	public $customType    = 2;
604
605
	public function getFullTitleURL(string $title_url) : string {
606
		return "{$this->baseURL}/manga/{$title_url}";
607
	}
608
609
	public function getChapterData(string $title_url, string $chapter) : array {
610
		$chapterN = (ctype_digit($chapter) ? "c${chapter}" : $chapter);
611
		return [
612
			'url'    => $this->getChapterURL($title_url, $chapter),
613
			'number' => $chapterN
614
		];
615
	}
616
	public function getChapterURL(string $title_url, string $chapter) : string {
617
		return $this->getFullTitleURL($title_url).'/'.$chapter;
618
	}
619
620
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
621
		$titleData = [];
622
623
		$fullURL = $this->getFullTitleURL($title_url);
624
625
		$content = $this->get_content($fullURL);
626
627
		$data = $this->parseTitleDataDOM(
628
			$content,
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by $this->get_content($fullURL) on line 625 can also be of type false; however, Base_Site_Model::parseTitleDataDOM() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
629
			$title_url,
630
			"(//h2[@class='widget-title'])[1]",
631
			"//ul[contains(@class, 'chapters')]/li[not(contains(@class, 'btn'))][1]",
632
			"div[contains(@class, 'action')]/div[@class='date-chapter-title-rtl']",
633
			"h5/a[1] | h3/a[1]",
634
			"Whoops, looks like something went wrong."
635
		);
636
		if($data) {
637
			$titleData['title'] = trim($data['nodes_title']->textContent);
638
639
			$segments = explode('/', (string) $data['nodes_chapter']->getAttribute('href'));
640
			$needle = array_search('manga', array_reverse($segments, TRUE)) + 2;
641
			$titleData['latest_chapter'] = $segments[$needle];
642
643
			$dateString = $data['nodes_latest']->nodeValue;
644
			$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime(preg_replace('/ (-|\[A\]).*$/', '', $dateString)));
645
		}
646
647
		return (!empty($titleData) ? $titleData : NULL);
648
	}
649
650
	public function doCustomUpdate() {
651
		$titleDataList = [];
652
653
		$updateURL = "{$this->baseURL}/latest-release";
654
		if(($content = $this->get_content($updateURL)) && $content['status_code'] == 200) {
655
			$data = $content['body'];
656
657
			$data = preg_replace('/^[\s\S]+<dl>/', '<dl>', $data);
658
			$data = preg_replace('/<\/dl>[\s\S]+$/', '</dl>', $data);
659
660
			$dom = new DOMDocument();
661
			libxml_use_internal_errors(TRUE);
662
			$dom->loadHTML($data);
663
			libxml_use_internal_errors(FALSE);
664
665
			$xpath      = new DOMXPath($dom);
666
			$nodes_rows = $xpath->query("//dl/dd | //div[@class='mangalist']/div[@class='manga-item']");
667
			if($nodes_rows->length > 0) {
668
				foreach($nodes_rows as $row) {
669
					$titleData = [];
670
671
					$nodes_title   = $xpath->query("div[@class='events ']/div[@class='events-body']/h3[@class='events-heading']/a | h3/a", $row);
672
					$nodes_chapter = $xpath->query("(div[@class='events '][1]/div[@class='events-body'][1] | div[@class='manga-chapter'][1])/h6[@class='events-subtitle'][1]/a[1]", $row);
673
					$nodes_latest  = $xpath->query("div[@class='time'] | small", $row);
674
675
					if($nodes_title->length === 1 && $nodes_chapter->length === 1 && $nodes_latest->length === 1) {
676
						$title = $nodes_title->item(0);
677
678
						preg_match('/(?<url>[^\/]+(?=\/$|$))/', $title->getAttribute('href'), $title_url_arr);
679
						$title_url = $title_url_arr['url'];
680
681
						if(!array_key_exists($title_url, $titleDataList)) {
682
							$titleData['title'] = trim($title->textContent);
683
684
							$chapter = $nodes_chapter->item(0);
685
							preg_match('/(?<chapter>[^\/]+(?=\/$|$))/', $chapter->getAttribute('href'), $chapter_arr);
686
							$titleData['latest_chapter'] = $chapter_arr['chapter'];
687
688
							$dateString = str_replace('/', '-', trim($nodes_latest->item(0)->nodeValue)); //NOTE: We replace slashes here as it stops strtotime interpreting the date as US date format.
689
							if($dateString == 'T') {
690
								$dateString = date("Y-m-d",now());
691
							}
692
							$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime($dateString . ' 00:00'));
693
694
							$titleDataList[$title_url] = $titleData;
695
						}
696
					} else {
697
						log_message('error', "{$this->site}/Custom | Invalid amount of nodes (TITLE: {$nodes_title->length} | CHAPTER: {$nodes_chapter->length}) | LATEST: {$nodes_latest->length})");
698
					}
699
				}
700
			} else {
701
				log_message('error', "{$this->site} | Following list is empty?");
702
			}
703
		} else {
704
			log_message('error', "{$this->site} - Custom updating failed for {$this->baseURL}.");
705
		}
706
707
		return $titleDataList;
708
	}
709
}
710
711
abstract class Base_GlossyBright_Site_Model extends Base_Site_Model {
712
	public $titleFormat   = '/^[a-zA-Z0-9_-]+$/';
713
	public $chapterFormat = '/^[0-9\.]+$/';
714
715
	public $customType    = 2;
716
717
	public function getFullTitleURL(string $title_url) : string {
718
		return "{$this->baseURL}/{$title_url}";
719
	}
720
721
	public function getChapterData(string $title_url, string $chapter) : array {
722
		return [
723
			'url'    => $this->getFullTitleURL($title_url).'/'.$chapter.'/',
724
			'number' => "c{$chapter}"
725
		];
726
	}
727
728
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
729
		$titleData = [];
730
731
		$fullURL = "{$this->baseURL}/manga-rss/{$title_url}";
732
		$content = $this->get_content($fullURL);
733
		$data    = $this->parseTitleDataDOM(
734
			$content,
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by $this->get_content($fullURL) on line 732 can also be of type false; however, Base_Site_Model::parseTitleDataDOM() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
735
			$title_url,
736
			"//rss/channel/image/title",
737
			"//rss/channel/item[1]",
738
			"pubdate",
739
			"title"
740
		);
741
		if($data) {
742
			$titleData['title'] = preg_replace('/^Recent chapters of (.*?) manga$/', '$1', trim($data['nodes_title']->textContent));
743
744
			//For whatever reason, DOMDocument breaks the <link> element we need to grab the chapter, so we have to grab it elsewhere.
745
			$titleData['latest_chapter'] = preg_replace('/^.*? - ([0-9\.]+) - .*?$/', '$1', trim($data['nodes_chapter']->textContent));
746
747
			$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $data['nodes_latest']->textContent));
748
		}
749
750
		return (!empty($titleData) ? $titleData : NULL);
751
	}
752
753
	public function doCustomUpdate() {
754
		$titleDataList = [];
755
756
		$baseURLRegex = str_replace('.', '\\.', parse_url($this->baseURL, PHP_URL_HOST));
757
		if(($content = $this->get_content($this->baseURL)) && $content['status_code'] == 200) {
758
			$data = $content['body'];
759
760
			$dom = new DOMDocument();
761
			libxml_use_internal_errors(TRUE);
762
			$dom->loadHTML($data);
763
			libxml_use_internal_errors(FALSE);
764
765
			$xpath      = new DOMXPath($dom);
766
			$nodes_rows = $xpath->query("//table[@id='wpm_mng_lst']/tr/td | //*[@id='wpm_mng_lst']/li/div");
767
			if($nodes_rows->length > 0) {
768
				foreach($nodes_rows as $row) {
769
					$titleData = [];
770
771
					$nodes_title   = $xpath->query("a[2]", $row);
772
					$nodes_chapter = $xpath->query("a[2]", $row);
773
					$nodes_latest  = $xpath->query("b", $row);
774
775
					if($nodes_latest->length === 0) {
776
						$nodes_latest = $xpath->query('text()[last()]', $row);
777
					}
778
779
					if($nodes_title->length === 1 && $nodes_chapter->length === 1 && $nodes_latest->length === 1) {
780
						$title   = $nodes_title->item(0);
781
						$chapter = $nodes_chapter->item(0);
782
783
						preg_match('/'.$baseURLRegex.'\/(?<url>.*?)\//', $title->getAttribute('href'), $title_url_arr);
784
						$title_url = $title_url_arr['url'];
785
786
						if(!array_key_exists($title_url, $titleDataList)) {
787
							$titleData['title'] = trim($title->getAttribute('title'));
788
789
							preg_match('/(?<chapter>[^\/]+(?=\/$|$))/', $chapter->getAttribute('href'), $chapter_arr);
790
							$titleData['latest_chapter'] = $chapter_arr['chapter'];
791
792
							$dateString = trim($nodes_latest->item(0)->textContent);
793
							switch($dateString) {
794
								case 'Today':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
795
									$dateString = date("Y-m-d", now());
796
									break;
797
798
								case 'Yesterday':
799
									$dateString = date("Y-m-d", strtotime("-1 days"));
800
									break;
801
802
								default:
803
									//Do nothing
804
									break;
805
							}
806
							$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime($dateString));
807
808
							$titleDataList[$title_url] = $titleData;
809
						}
810
					} else {
811
						log_message('error', "{$this->site}/Custom | Invalid amount of nodes (TITLE: {$nodes_title->length} | CHAPTER: {$nodes_chapter->length}) | LATEST: {$nodes_latest->length})");
812
					}
813
				}
814
			} else {
815
				log_message('error', "{$this->site} | Following list is empty?");
816
			}
817
		} else {
818
			log_message('error', "{$this->site} - Custom updating failed.");
819
		}
820
821
		return $titleDataList;
822
	}
823
}
824
825
abstract class Base_Roku_Site_Model extends Base_Site_Model {
826
	public $titleFormat   = '/^[a-zA-Z0-9-]+$/';
827
	public $chapterFormat = '/^[0-9\.]+$/';
828
829
	public $customType    = 2;
830
831
	public function getFullTitleURL(string $title_url) : string {
832
		return "{$this->baseURL}/series/{$title_url}";
833
	}
834
	public function getChapterData(string $title_url, string $chapter) : array {
835
		return [
836
			'url'    => "{$this->baseURL}/read/{$title_url}/{$chapter}",
837
			'number' => "c{$chapter}"
838
		];
839
	}
840
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
841
		$titleData = [];
842
		$fullURL = $this->getFullTitleURL($title_url);
843
		$content = $this->get_content($fullURL);
844
		$data = $this->parseTitleDataDOM(
845
			$content,
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by $this->get_content($fullURL) on line 843 can also be of type false; however, Base_Site_Model::parseTitleDataDOM() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
846
			$title_url,
847
			"//div[@id='activity']/descendant::div[@class='media'][1]/descendant::div[@class='media-body']/h2/text()",
848
			"//ul[contains(@class, 'media-list')]/li[@class='media'][1]/a",
849
			"div[@class='media-body']/span[@class='text-muted']",
850
			""
851
		);
852
		if($data) {
853
			$titleData['title'] = trim(preg_replace('/ Added on .*$/','', $data['nodes_title']->textContent));
854
			$titleData['latest_chapter'] = preg_replace('/^.*\/([0-9\.]+)$/', '$1', (string) $data['nodes_chapter']->getAttribute('href'));
855
856
			$dateString = preg_replace('/^Added (?:on )?/', '',$data['nodes_latest']->textContent);
857
			$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime($dateString));
858
		}
859
		return (!empty($titleData) ? $titleData : NULL);
860
	}
861
862
863
	public function doCustomUpdate() {
864
		$titleDataList = [];
865
866
		$updateURL = "{$this->baseURL}/latest";
867
		if(($content = $this->get_content($updateURL)) && $content['status_code'] == 200) {
868
			$data = $content['body'];
869
870
			$dom = new DOMDocument();
871
			libxml_use_internal_errors(TRUE);
872
			$dom->loadHTML($data);
873
			libxml_use_internal_errors(FALSE);
874
875
			$xpath      = new DOMXPath($dom);
876
			$nodes_rows = $xpath->query("//div[@class='content-wrapper']/div[@class='row']/div/div");
877
			if($nodes_rows->length > 0) {
878
				foreach($nodes_rows as $row) {
879
					$titleData = [];
880
881
					$nodes_title   = $xpath->query("div[@class='caption']/h6/a", $row);
882
					$nodes_chapter = $xpath->query("div[@class='panel-footer no-padding']/a", $row);
883
					$nodes_latest  = $xpath->query("div[@class='caption']/text()", $row);
884
885
					if($nodes_title->length === 1 && $nodes_chapter->length === 1 && $nodes_latest->length === 1) {
886
						$title = $nodes_title->item(0);
887
888
						preg_match('/(?<url>[^\/]+(?=\/$|$))/', $title->getAttribute('href'), $title_url_arr);
889
						$title_url = $title_url_arr['url'];
890
891
						if(!array_key_exists($title_url, $titleDataList)) {
892
							$titleData['title'] = trim($title->textContent);
893
894
							$chapter = $nodes_chapter->item(0);
895
							preg_match('/(?<chapter>[^\/]+(?=\/$|$))/', $chapter->getAttribute('href'), $chapter_arr);
896
							$titleData['latest_chapter'] = $chapter_arr['chapter'];
897
898
							$dateString = trim(str_replace('Added ', '', $nodes_latest->item(0)->textContent));
899
							$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime($dateString));
900
901
							$titleDataList[$title_url] = $titleData;
902
						}
903
					} else {
904
						log_message('error', "{$this->site}/Custom | Invalid amount of nodes (TITLE: {$nodes_title->length} | CHAPTER: {$nodes_chapter->length}) | LATEST: {$nodes_latest->length})");
905
					}
906
				}
907
			} else {
908
				log_message('error', "{$this->site} | Following list is empty?");
909
			}
910
		} else {
911
			log_message('error', "{$this->site} - Custom updating failed.");
912
		}
913
914
		return $titleDataList;
915
	}
916
}
917