Completed
Push — master ( 2233b4...404f7e )
by Angus
02:02
created

Userscript::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 0
dl 0
loc 22
ccs 8
cts 12
cp 0.6667
crap 5.9256
rs 8.6737
c 0
b 0
f 0
1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class Userscript extends AJAX_Controller {
4
	private $userID;
5
6 1
	public function __construct() {
7 1
		parent::__construct();
8
9 1
		$this->load->library('vendor/Limiter');
10 1
		$this->load->library('form_validation');
11
12
		//500 requests per hour to either AJAX request.
13 1
		if($this->limiter->limit('tracker_userscript', 1000)) {
14
			$this->output->set_status_header('429', 'Rate limit reached'); //rate limited reached
15
		}
16
17
		//API Key is required for all AJAX requests
18
		//We're not using set_rules here since we can't run form_validation twice.
19 1
		if($this->form_validation->required($this->input->post('api-key')) && ctype_alnum($this->input->post('api-key'))) {
20
			$this->userID = $this->User->get_id_from_api_key($this->input->post('api-key'));
21
			if(!$this->userID) {
22
				$this->output->set_status_header('400', 'Invalid API Key');
23
			}
24
		} else {
25 1
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
26
		}
27 1
	}
28
29
	/**
30
	 * This is the main update URL for the userscript.
31
	 *
32
	 * REQ_PARAMS: api-key, manga[site], manga[title], manga[chapter]
33
	 * METHOD:     POST
34
	 * URL:        /ajax/userscript/update
35
	 */
36 1
	public function update() : void {
37 1
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
38
39
		$this->form_validation->set_rules('manga[site]', 'Manga [Site]', 'required');
40
		$this->form_validation->set_rules('manga[title]', 'Manga [Title]', 'required');
41
		$this->form_validation->set_rules('manga[chapter]', 'Manga [Chapter]', 'required');
42
43
		if($this->form_validation->run() === TRUE) {
44
			$manga = $this->input->post('manga');
45
46
			$titleData = $this->Tracker->list->update($this->userID, $manga['site'], $manga['title'], $manga['chapter'], TRUE, TRUE);
47
			if($titleData) {
48
				$malID = $this->Tracker->list->getMalID($this->userID, $titleData['id']);
49
				$json = [
50
					'mal_sync' => $this->User_Options->get('mal_sync', $this->userID),
51
					'mal_id'   => $malID['id'] ?? NULL,
52
					'chapter'  => $titleData['chapter']
53
				];
54
55
				$this->output
56
				     ->set_status_header('200')
57
				     ->set_content_type('application/json', 'utf-8')
58
				     ->set_output(json_encode($json));
59
			} else {
60
				//TODO: We should probably try and have more verbose errors here. Return via JSON or something.
61
				$this->output->set_status_header('400', 'Unable to update?');
62
			}
63
		} else {
64
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
65
		}
66
	}
67
68
	/**
69
	 * Report a bug via userscript.
70
	 *
71
	 * REQ_PARAMS: api-key, bug[url], bug[text]
72
	 * METHOD:     POST
73
	 * URL:        /ajax/userscript/report_bug
74
	 */
75
	public function report_bug() : void {
76
		$this->load->library('user_agent');
77
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
78
		$this->form_validation->set_rules('bug[url]',  'Bug [URL]',  'required');
79
		$this->form_validation->set_rules('bug[text]', 'Bug [Text]', 'required');
80
81
		if($this->form_validation->run() === TRUE) {
82
			$bug = $this->input->post('bug');
83
84
			//Preferably, I'd like to validate this in some way, but it's a bit too easy to bypass
85
			$success = $this->Tracker->bug->report($bug['text'], NULL, $bug['url']);
86
			if($success) {
87
				$this->output->set_status_header('200'); //Success!
88
			} else {
89
				$this->output->set_status_header('400', 'Unable to report bug?');
90
			}
91
		} else {
92
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
93
		}
94
	}
95
96
	/**
97
	 * Favourite a chapter via the userscript.
98
	 *
99
	 * REQ_PARAMS: api-key, manga[site], manga[title], manga[chapter]
100
	 * METHOD:     POST
101
	 * URL:        /ajax/userscript/favourite
102
	 */
103
	public function favourite() : void {
104
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
105
106
		if($this->limiter->limit('tracker_userscript_bug', 250)) {
107
			$this->output->set_status_header('429', 'Rate limit reached'); //rate limited reached
108
		} else {
109
			$this->form_validation->set_rules('manga[site]', 'Manga [Site]', 'required');
110
			$this->form_validation->set_rules('manga[title]', 'Manga [Title]', 'required');
111
			$this->form_validation->set_rules('manga[chapter]', 'Manga [Chapter]', 'required');
112
113
			if($this->form_validation->run() === TRUE) {
114
				$manga = $this->input->post('manga');
115
116
				$success = $this->Tracker->favourites->set($manga['site'], $manga['title'], $manga['chapter'], $this->userID);
117
				if($success['bool']) {
118
					$this->output->set_status_header('200', $success['status']); //Success!
119
				} else {
120
					$this->output->set_status_header('400', $success['status']);
121
				}
122
			} else {
123
				$this->output->set_status_header('400', 'Missing/invalid parameters.');
124
			}
125
		}
126
	}
127
}
128