Completed
Push — master ( 0f9479...65c76e )
by Angus
02:49
created

Base_Site_Model::doCustomCheckCompare()   C

Complexity

Conditions 16
Paths 104

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 16.351

Importance

Changes 0
Metric Value
cc 16
eloc 28
nc 104
nop 2
dl 0
loc 50
ccs 24
cts 27
cp 0.8889
crap 16.351
rs 5.2742
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 119
	public function __construct() {
8 119
		parent::__construct();
9 119
	}
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
		if(!class_exists($name) || !(in_array(get_parent_class($name), ['Base_Site_Model', 'Base_FoolSlide_Site_Model', 'Base_myMangaReaderCMS_Site_Model']))) {
16
			return get_instance()->{$name};
17
		} else {
18
			$this->loadSite($name);
19
			return $this->{$name};
20
		}
21
	}
22
23
	private function loadSite(string $siteName) : void {
24
		$this->{$siteName} = new $siteName();
25
	}
26
}
27
28
abstract class Base_Site_Model extends CI_Model {
29
	public $site          = '';
30
	public $titleFormat   = '//';
31
	public $chapterFormat = '//';
32
	public $hasCloudFlare = FALSE;
33
	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';
34
35
	/**
36
	 * 0: No custom updater.
37
	 * 1: Uses following page.
38
	 * 2: Uses latest releases page.
39
	 */
40
	public $customType = 0;
41
42 16
	public function __construct() {
43 16
		parent::__construct();
44
45 16
		$this->load->database();
46
47 16
		$this->site = get_class($this);
48 16
	}
49
50
	/**
51
	 * Generates URL to the title page of the requested series.
52
	 *
53
	 * 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)
54
	 *       When storing additional data, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed.
55
	 *
56
	 * Example:
57
	 *    return "http://mangafox.me/manga/{$title_url}/";
58
	 *
59
	 * Example (with extra data):
60
	 *    $title_parts = explode(':--:', title_url);
61
	 *    return "https://bato.to/comic/_/comics/-r".$title_parts[0];
62
	 *
63
	 * @param string $title_url
64
	 * @return string
65
	 */
66
	abstract public function getFullTitleURL(string $title_url) : string;
67
68
	/**
69
	 * Generates chapter data from given $title_url and $chapter.
70
	 *
71
	 * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format.
72
	 *
73
	 * NOTE: In some cases, we are required to store the chapter number, and the segment required to generate the chapter URL separately.
74
	 *       Much like when generating the title URL, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed.
75
	 *
76
	 * Example:
77
	 *     return [
78
	 *        'url'    => $this->getFullTitleURL($title_url).'/'.$chapter,
79
	 *        'number' => "c{$chapter}"
80
	 *    ];
81
	 *
82
	 * @param string $title_url
83
	 * @param string $chapter
84
	 * @return array [url, number]
85
	 */
86
	abstract public function getChapterData(string $title_url, string $chapter) : array;
87
88
	/**
89
	 * Used to get the latest chapter of given $title_url.
90
	 *
91
	 * 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.
92
	 *
93
	 * $titleData params must be set accordingly:
94
	 * * `title` should always be used with html_entity_decode.
95
	 * * `latest_chapter` must match $this->chapterFormat.
96
	 * * `last_updated` should always be in date("Y-m-d H:i:s") format.
97
	 * * `followed` should never be set within via getTitleData, with the exception of via a array_merge with doCustomFollow.
98
	 *
99
	 * $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).
100
	 *
101
	 * @param string $title_url
102
	 * @param bool   $firstGet
103
	 * @return array|null [title,latest_chapter,last_updated,followed?]
104
	 */
105
	abstract public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array;
106
107
	/**
108
	 * Validates given $title_url against titleFormat.
109
	 *
110
	 * Failure to match against titleFormat will stop the series from being added to the DB.
111
	 *
112
	 * @param string $title_url
113
	 * @return bool
114
	 */
115 2
	final public function isValidTitleURL(string $title_url) : bool {
116 2
		$success = (bool) preg_match($this->titleFormat, $title_url);
117 2
		if(!$success) log_message('error', "Invalid Title URL ({$this->site}): {$title_url}");
118 2
		return $success;
119
	}
120
121
	/**
122
	 * Validates given $chapter against chapterFormat.
123
	 *
124
	 * Failure to match against chapterFormat will stop the chapter being updated.
125
	 *
126
	 * @param string $chapter
127
	 * @return bool
128
	 */
129 2
	final public function isValidChapter(string $chapter) : bool {
130 2
		$success = (bool) preg_match($this->chapterFormat, $chapter);
131 2
		if(!$success) log_message('error', "Invalid Chapter ({$this->site}): {$chapter}");
132 2
		return $success;
133
	}
134
135
	/**
136
	 * Used by getTitleData (& similar functions) to get the requested page data.
137
	 *
138
	 * @param string $url
139
	 * @param string $cookie_string
140
	 * @param string $cookiejar_path
141
	 * @param bool   $follow_redirect
142
	 * @param bool   $isPost
143
	 * @param array  $postFields
144
	 *
145
	 * @return array|bool
146
	 */
147
	final protected function get_content(string $url, string $cookie_string = "", string $cookiejar_path = "", bool $follow_redirect = FALSE, bool $isPost = FALSE, array $postFields = []) {
148
		$refresh = TRUE; //For sites that have CloudFlare, we want to loop get_content again.
149
		while($refresh) {
150
			$refresh = FALSE;
151
152
			$ch = curl_init();
153
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
154
			curl_setopt($ch, CURLOPT_ENCODING , "gzip");
155
			//curl_setopt($ch, CURLOPT_VERBOSE, 1);
156
			curl_setopt($ch, CURLOPT_HEADER, 1);
157
158
			if($follow_redirect)        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
159
160
			if(($cookies = $this->cache->get("cloudflare_{$this->site}"))) {
161
				$cookie_string .= "; {$cookies}";
162
			}
163
164
			if(!empty($cookie_string))  curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
165
			if(!empty($cookiejar_path)) curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar_path);
166
167
			//Some sites check the useragent for stuff, use a pre-defined user-agent to avoid stuff.
168
			curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
169
170
			//NOTE: This is required for SSL URLs for now. Without it we tend to get error code 60.
171
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //FIXME: This isn't safe, but it allows us to grab SSL URLs
172
173
			curl_setopt($ch, CURLOPT_URL, $url);
174
175
			if($isPost) {
176
				curl_setopt($ch,CURLOPT_POST, count($postFields));
177
				curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($postFields));
178
			}
179
180
			$response = curl_exec($ch);
181
			if($response === FALSE) {
182
				log_message('error', "curl failed with error: ".curl_errno($ch)." | ".curl_error($ch));
183
				//FIXME: We don't always account for FALSE return
184
				return FALSE;
185
			}
186
187
			$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
188
			$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
189
			$headers     = http_parse_headers(substr($response, 0, $header_size));
190
			$body        = substr($response, $header_size);
191
			curl_close($ch);
192
193
			if($status_code === 503) $refresh = $this->handleCloudFlare($url, $body);
194
		}
195
196
		return [
197
			'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...
198
			'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...
199
			'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...
200
		];
201
	}
202
203
	final private function handleCloudFlare(string $url, string $body) : bool {
204
		$refresh = FALSE;
205
206
		if(strpos($body, 'DDoS protection by Cloudflare') !== false) {
207
			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...
208
				//TODO: Site appears to have enabled CloudFlare, disable it and contact admin.
209
				//      We'll continue to bypass CloudFlare as this may occur in a loop.
210
			}
211
212
			$urlData = [
213
				'url'        => $url,
214
				'user_agent' => $this->userAgent
215
			];
216
			//TODO: shell_exec seems bad since the URLs "could" be user inputted? Better way of doing this?
217
			$result = shell_exec('python '.APPPATH.'../_scripts/get_cloudflare_cookie.py '.escapeshellarg(json_encode($urlData)));
218
			$cookieData = json_decode($result, TRUE);
219
220
			$this->cache->save("cloudflare_{$this->site}", $cookieData['cookies'],  31536000 /* 1 year, or until we renew it */);
221
			$refresh = TRUE;
222
		} 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...
223
			//Either site doesn't have CloudFlare or we have bypassed it. Either is good!
224
		}
225
		return $refresh;
226
	}
227
228
	/**
229
	 * Used by getTitleData to get the title, latest_chapter & last_updated data from the data returned by get_content.
230
	 *
231
	 * parseTitleDataDOM checks if the data returned by get_content is valid via a few simple checks.
232
	 * * 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.
233
	 *
234
	 * Data is cleaned by cleanTitleDataDOM prior to being passed to DOMDocument.
235
	 *
236
	 * All $node_* params must be XPath to the requested node, and must only return 1 result. Anything else will throw a failure.
237
	 *
238
	 * @param array  $content
239
	 * @param string $title_url
240
	 * @param string $node_title_string
241
	 * @param string $node_row_string
242
	 * @param string $node_latest_string
243
	 * @param string $node_chapter_string
244
	 * @param string $failure_string
245
	 * @return DOMElement[]|false [nodes_title,nodes_chapter,nodes_latest]
246
	 */
247
	final protected function parseTitleDataDOM(
248
		$content, string $title_url,
249
		string $node_title_string, string $node_row_string,
250
		string $node_latest_string, string $node_chapter_string,
251
		string $failure_string = "") {
252
253
		if(!is_array($content)) {
254
			log_message('error', "{$this->site} : {$title_url} | Failed to grab URL (See above curl error)");
255
		} else {
256
			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...
257
258
			if(!($status_code >= 200 && $status_code < 300)) {
259
				log_message('error', "{$this->site} : {$title_url} | Bad Status Code ({$status_code})");
260
			} else if(empty($data)) {
261
				log_message('error', "{$this->site} : {$title_url} | Data is empty? (Status code: {$status_code})");
262
			} else if($failure_string !== "" && strpos($data, $failure_string) !== FALSE) {
263
				log_message('error', "{$this->site} : {$title_url} | Failure string matched");
264
			} else {
265
				$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.
266
267
				$dom = new DOMDocument();
268
				libxml_use_internal_errors(TRUE);
269
				$dom->loadHTML('<?xml encoding="utf-8" ?>' . $data);
270
				libxml_use_internal_errors(FALSE);
271
272
				$xpath = new DOMXPath($dom);
273
				$nodes_title = $xpath->query($node_title_string);
274
				$nodes_row   = $xpath->query($node_row_string);
275
				if($nodes_title->length === 1 && $nodes_row->length === 1) {
276
					$firstRow      = $nodes_row->item(0);
277
					$nodes_latest  = $xpath->query($node_latest_string,  $firstRow);
278
279
					if($node_chapter_string !== '') {
280
						$nodes_chapter = $xpath->query($node_chapter_string, $firstRow);
281
					} else {
282
						$nodes_chapter = $nodes_row;
283
					}
284
285
					if($nodes_latest->length === 1 && $nodes_chapter->length === 1) {
286
						return [
287
							'nodes_title'   => $nodes_title->item(0),
288
							'nodes_latest'  => $nodes_latest->item(0),
289
							'nodes_chapter' => $nodes_chapter->item(0)
290
						];
291
					} else {
292
						log_message('error', "{$this->site} : {$title_url} | Invalid amount of nodes (LATEST: {$nodes_latest->length} | CHAPTER: {$nodes_chapter->length})");
293
					}
294
				} else {
295
					log_message('error', "{$this->site} : {$title_url} | Invalid amount of nodes (TITLE: {$nodes_title->length} | ROW: {$nodes_row->length})");
296
				}
297
			}
298
		}
299
300
		return FALSE;
301
	}
302
303
	/**
304
	 * Used by parseTitleDataDOM to clean the data prior to passing it to DOMDocument & DOMXPath.
305
	 * 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.
306
	 *
307
	 * @param string $data
308
	 * @return string
309
	 */
310
	public function cleanTitleDataDOM(string $data) : string {
311
		return $data;
312
	}
313
314
	/**
315
	 * Used to follow a series on given site if supported.
316
	 *
317
	 * This is called by getTitleData if $firstGet is true (which occurs when the series is first being added to the DB).
318
	 *
319
	 * Most of the actual following is done by handleCustomFollow.
320
	 *
321
	 * @param string $data
322
	 * @param array  $extra
323
	 * @return array
324
	 */
325
	final public function doCustomFollow(string $data = "", array $extra = []) : array {
326
		$titleData = [];
327
		$this->handleCustomFollow(function($content, $id, closure $successCallback = NULL) use(&$titleData) {
328
			if(is_array($content)) {
329
				if(array_key_exists('status_code', $content)) {
330
					$statusCode = $content['status_code'];
331
					if($statusCode === 200) {
332
						$isCallable = is_callable($successCallback);
333
						if(($isCallable && $successCallback($content['body'])) || !$isCallable) {
334
							$titleData['followed'] = 'Y';
335
336
							log_message('info', "doCustomFollow succeeded for {$id}");
337
						} else {
338
							log_message('error', "doCustomFollow failed (Invalid response?) for {$id}");
339
						}
340
					} else {
341
						log_message('error', "doCustomFollow failed (Invalid status code ({$statusCode})) for {$id}");
342
					}
343
				} else {
344
					log_message('error', "doCustomFollow failed (Missing status code?) for {$id}");
345
				}
346
			} else {
347
				log_message('error', "doCustomFollow failed (Failed request) for {$id}");
348
			}
349
		}, $data, $extra);
350
		return $titleData;
351
	}
352
353
	/**
354
	 * Used by doCustomFollow to handle following series on sites.
355
	 *
356
	 * Uses get_content to get data.
357
	 *
358
	 * $callback must return ($content, $id, closure $successCallback = NULL).
359
	 * * $content is simply just the get_content data.
360
	 * * $id is the dbID. This should be passed by the $extra arr.
361
	 * * $successCallback is an optional success check to make sure the series was properly followed.
362
	 *
363
	 * @param callable $callback
364
	 * @param string   $data
365
	 * @param array    $extra
366
	 */
367
	public function handleCustomFollow(callable $callback, string $data = "", array $extra = []) {}
368
369
	/**
370
	 * Used to check the sites following page for new updates (if supported).
371
	 * This should work much like getTitleData, but instead checks the following page.
372
	 *
373
	 * This must return an array containing arrays of each of the chapters data.
374
	 */
375
	public function doCustomUpdate() {}
376
377
	/**
378
	 * Used by the custom updater to check if a chapter looks newer than the current one.
379
	 *
380
	 * This calls doCustomCheckCompare which handles the majority of the checking.
381
	 * NOTE: Depending on the site, you may need to call getChapterData to get the chapter number to be used with this.
382
	 *
383
	 * @param string $oldChapter
384
	 * @param string $newChapter
385
	 */
386
	public function doCustomCheck(string $oldChapter, string $newChapter) {}
387
388
	/**
389
	 * Used by doCustomCheck to check if a chapter looks newer than the current one.
390
	 * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format.
391
	 *
392
	 * 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.
393
	 *
394
	 * @param array $oldChapterSegments
395
	 * @param array $newChapterSegments
396
	 * @return bool
397
	 */
398 12
	final public function doCustomCheckCompare(array $oldChapterSegments, array $newChapterSegments) : bool {
399
		//NOTE: We only need to check against the new chapter here, as that is what is used for confirming update.
400 12
		$status = FALSE;
401
402
		//Make sure we have a volume element
403 12
		if(count($oldChapterSegments) === 1) array_unshift($oldChapterSegments, 'v0');
404 12
		if(count($newChapterSegments) === 1) array_unshift($newChapterSegments, 'v0');
405
406 12
		$oldCount = count($oldChapterSegments);
407 12
		$newCount = count($newChapterSegments);
408 12
		if($newCount === $oldCount) {
409
			//Make sure chapter format looks correct.
410
			//NOTE: We only need to check newCount as we know oldCount is the same count.
411 12
			if($newCount === 2) {
412
				//FIXME: Can we loop this?
413 12
				$oldVolume = substr(array_shift($oldChapterSegments), 1);
414 12
				$newVolume = substr(array_shift($newChapterSegments), 1);
415
416
				//Forcing volume to 0 as TBD might not be the latest (although it can be, but that is covered by other checks)
417 12
				if(in_array($oldVolume, ['TBD', 'TBA', 'NA', 'LMT'])) $oldVolume = 0;
418 12
				if(in_array($newVolume, ['TBD', 'TBA', 'NA', 'LMT'])) $newVolume = 0;
419
420 12
				$oldVolume = floatval($oldVolume);
421 12
				$newVolume = floatval($newVolume);
422
			} else {
423
				$oldVolume = 0;
424
				$newVolume = 0;
425
			}
426 12
			$oldChapter = floatval(substr(array_shift($oldChapterSegments), 1));
427 12
			$newChapter = floatval(substr(array_shift($newChapterSegments), 1));
428
429 12
			if($newChapter > $oldChapter && ($oldChapter >= 10 && $newChapter >= 10)) {
430
				//$newChapter is higher than $oldChapter AND $oldChapter and $newChapter are both more than 10
431
				//This is intended to cover the /majority/ of valid updates, as we technically shouldn't have to check volumes.
432
433 4
				$status = TRUE;
434 8
			} elseif($newVolume > $oldVolume && ($oldChapter < 10 && $newChapter < 10)) {
435
				//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).
436 1
				$status = TRUE;
437 7
			} elseif($newVolume > $oldVolume && $newChapter >= $oldChapter) {
438
				//$newVolume is higher, and chapter is higher so no need to check chapter.
439 2
				$status = TRUE;
440 5
			} elseif($newChapter > $oldChapter) {
441
				//$newVolume isn't higher, but chapter is.
442
				$status = TRUE;
443
			}
444
		}
445
446 12
		return $status;
447
	}
448
}
449
450
abstract class Base_FoolSlide_Site_Model extends Base_Site_Model {
451
	public $titleFormat   = '/^[a-z0-9_-]+$/';
452
	public $chapterFormat = '/^en(?:-us)?\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/';
453
	public $customType    = 2;
454
455
	public $baseURL = '';
456
457
	public function getFullTitleURL(string $title_url) : string {
458
		return "{$this->baseURL}/series/{$title_url}";
459
	}
460
461
	public function getChapterData(string $title_url, string $chapter) : array {
462
		$chapter_parts = explode('/', $chapter); //returns #LANG#/#VOLUME#/#CHAPTER#/#CHAPTER_EXTRA#(/#PAGE#/)
463
		return [
464
			'url'    => $this->getChapterURL($title_url, $chapter),
465
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
466
		];
467
	}
468
	public function getChapterURL(string $title_url, string $chapter) : string {
469
		return "{$this->baseURL}/read/{$title_url}/{$chapter}/";
470
	}
471
472
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
473
		$titleData = [];
474
475
		$jsonURL = $this->getJSONTitleURL($title_url);
476
		if($content = $this->get_content($jsonURL)) {
477
			$json = json_decode($content['body'], TRUE);
478
			if($json && isset($json['chapters']) && count($json['chapters']) > 0) {
479
				$titleData['title'] = trim($json['comic']['name']);
480
481
				//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..
482
				usort($json['chapters'], function($a, $b) {
483
					return floatval("{$b['chapter']['chapter']}.{$b['chapter']['subchapter']}") <=> floatval("{$a['chapter']['chapter']}.{$a['chapter']['subchapter']}");
484
				});
485
				$latestChapter = reset($json['chapters'])['chapter'];
486
487
				$latestChapterString = "{$latestChapter['language']}/{$latestChapter['volume']}/{$latestChapter['chapter']}";
488
				if($latestChapter['subchapter'] !== '0') {
489
					$latestChapterString .= "/{$latestChapter['subchapter']}";
490
				}
491
				$titleData['latest_chapter'] = $latestChapterString;
492
493
				//No need to use date() here since this is already formatted as such.
494
				$titleData['last_updated'] = ($latestChapter['updated'] !== '0000-00-00 00:00:00' ? $latestChapter['updated'] : $latestChapter['created']);
495
			}
496
		}
497
498
		return (!empty($titleData) ? $titleData : NULL);
499
	}
500
501
	//Since we're just checking the latest updates page and not a following page, we just need to simulate a follow.
502
	//TODO: It would probably be better to have some kind of var which says that the custom update uses a following page..
503
	public function handleCustomFollow(callable $callback, string $data = "", array $extra = []) {
504
		$content = ['status_code' => 200];
505
		$callback($content, $extra['id']);
506
	}
507
	public function doCustomUpdate() {
508
		$titleDataList = [];
509
510
		$jsonURL = $this->getJSONUpdateURL();
511
		if(($content = $this->get_content($jsonURL)) && $content['status_code'] == 200) {
512
			if(($json = json_decode($content['body'], TRUE)) && isset($json['chapters'])) {
513
				//This should fix edge cases where chapters are uploaded in bulk in the wrong order (HelveticaScans does this with Mousou Telepathy).
514
				usort($json['chapters'], function($a, $b) {
515
					$a_date = new DateTime($a['chapter']['updated'] !== '0000-00-00 00:00:00' ? $a['chapter']['updated'] : $a['chapter']['created']);
516
					$b_date = new DateTime($b['chapter']['updated'] !== '0000-00-00 00:00:00' ? $b['chapter']['updated'] : $b['chapter']['created']);
517
					return $b_date <=> $a_date;
518
				});
519
520
				$parsedTitles = [];
521
				foreach($json['chapters'] as $chapterData) {
522
					if(!in_array($chapterData['comic']['stub'], $parsedTitles)) {
523
						$parsedTitles[] = $chapterData['comic']['stub'];
524
525
						$titleData = [];
526
						$titleData['title'] = trim($chapterData['comic']['name']);
527
528
						$latestChapter = $chapterData['chapter'];
529
530
						$latestChapterString = "en/{$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
						$titleDataList[$chapterData['comic']['stub']] = $titleData;
540
					} else {
541
						//We already have title data for this title.
542
						continue;
543
					}
544
				}
545
			} else {
546
				log_message('error', "{$this->site} - Custom updating failed (no chapters arg?) for {$this->baseURL}.");
547
			}
548
		} else {
549
			log_message('error', "{$this->site} - Custom updating failed for {$this->baseURL}.");
550
		}
551
552
		return $titleDataList;
553
	}
554
	public function doCustomCheck(string $oldChapterString, string $newChapterString) {
555
		$oldChapterSegments = explode('/', $this->getChapterData('', $oldChapterString)['number']);
556
		$newChapterSegments = explode('/', $this->getChapterData('', $newChapterString)['number']);
557
558
		$status = $this->doCustomCheckCompare($oldChapterSegments, $newChapterSegments);
559
560
		return $status;
561
	}
562
563
	public function getJSONTitleURL(string $title_url) : string {
564
		return "{$this->baseURL}/api/reader/comic/stub/{$title_url}/format/json";
565
	}
566
	public function getJSONUpdateURL() : string {
567
		return "{$this->baseURL}/api/reader/chapters/orderby/desc_created/format/json";
568
	}
569
}
570
571
abstract class Base_myMangaReaderCMS_Site_Model extends Base_Site_Model {
572
	public $titleFormat   = '/^[a-zA-Z0-9_-]+$/';
573
	public $chapterFormat = '/^[0-9\.]+$/';
574
	public $customType    = 0; //FIXME
575
576
	public $baseURL = '';
577
578
	public function getFullTitleURL(string $title_url) : string {
579
		return "{$this->baseURL}/manga/{$title_url}";
580
	}
581
582
	public function getChapterData(string $title_url, string $chapter) : array {
583
		return [
584
			'url'    => $this->getChapterURL($title_url, $chapter),
585
			'number' => "c{$chapter}"
586
		];
587
	}
588
	public function getChapterURL(string $title_url, string $chapter) : string {
589
		return $this->getFullTitleURL($title_url).'/'.$chapter;
590
	}
591
592
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
593
		$titleData = [];
594
595
		$fullURL = $this->getFullTitleURL($title_url);
596
597
		$content = $this->get_content($fullURL);
598
599
		$data = $this->parseTitleDataDOM(
600
			$content,
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by $this->get_content($fullURL) on line 597 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...
601
			$title_url,
602
			"(//h2[@class='widget-title'])[1]",
603
			"//ul[contains(@class, 'chapters')]/li[not(contains(@class, 'btn'))][1]",
604
			"div[contains(@class, 'action')]/div[@class='date-chapter-title-rtl']",
605
			"h5/a[1] | h3/a[1]",
606
			"Whoops, looks like something went wrong."
607
		);
608
		if($data) {
609
			$titleData['title'] = trim($data['nodes_title']->textContent);
610
611
			$titleData['latest_chapter'] = preg_replace('/^.*\/([0-9\.]+)$/', '$1', (string) $data['nodes_chapter']->getAttribute('href'));
612
613
			$dateString = $data['nodes_latest']->nodeValue;
614
			$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime(preg_replace('/ (-|\[A\]).*$/', '', $dateString)));
615
		}
616
617
		return (!empty($titleData) ? $titleData : NULL);
618
	}
619
}
620