Completed
Push — master ( 6d39b2...c981e2 )
by Angus
56:07
created

Base_myMangaReaderCMS_Site_Model::doCustomUpdate()   C

Complexity

Conditions 10
Paths 3

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

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