Completed
Push — master ( 9d6ede...76f36d )
by Angus
02:44
created

Base_Site_Model::get_content()   C

Complexity

Conditions 10
Paths 97

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 97
nop 6
dl 0
loc 57
ccs 0
cts 34
cp 0
crap 110
rs 6.7123
c 0
b 0
f 0

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