Completed
Push — master ( 757aad...e77452 )
by Angus
02:47
created

Userscript   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 30.3%

Importance

Changes 0
Metric Value
dl 0
loc 126
ccs 10
cts 33
cp 0.303
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 5
B update() 0 33 5
A report_bug() 0 20 4
B favourite() 0 23 5
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', 500)) {
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() {
37 1
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
38
		if($this->limiter->limit('tracker_userscript_bug', 250)) {
39
			$this->output->set_status_header('429', 'Rate limit reached'); //rate limited reached
40
		} else {
41
			$this->form_validation->set_rules('manga[site]', 'Manga [Site]', 'required');
42
			$this->form_validation->set_rules('manga[title]', 'Manga [Title]', 'required');
43
			$this->form_validation->set_rules('manga[chapter]', 'Manga [Chapter]', 'required');
44
45
			if($this->form_validation->run() === TRUE) {
46
				$manga = $this->input->post('manga');
47
48
				$titleData = $this->Tracker->list->update($this->userID, $manga['site'], $manga['title'], $manga['chapter'], FALSE, TRUE);
49
				if($titleData) {
50
					$json = [
51
						'mal_sync' => $this->User_Options->get('mal_sync', $this->userID),
52
						'mal_id'   => $this->Tracker->tag->getMalID($this->userID, $titleData['id']),
53
						'chapter'  => $titleData['chapter']
54
					];
55
56
					$this->output
57
					     ->set_status_header('200')
58
					     ->set_content_type('application/json', 'utf-8')
59
					     ->set_output(json_encode($json));
60
				} else {
61
					//TODO: We should probably try and have more verbose errors here. Return via JSON or something.
62
					$this->output->set_status_header('400', 'Unable to update?');
63
				}
64
			} else {
65
				$this->output->set_status_header('400', 'Missing/invalid parameters.');
66
			}
67
		}
68
	}
69
70
	/**
71
	 * Report a bug via userscript.
72
	 *
73
	 * REQ_PARAMS: api-key, bug[url], bug[text]
74
	 * METHOD:     POST
75
	 * URL:        /ajax/userscript/report_bug
76
	 */
77
	public function report_bug() {
78
		$this->load->library('user_agent');
79
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
80
		$this->form_validation->set_rules('bug[url]',  'Bug [URL]',  'required');
81
		$this->form_validation->set_rules('bug[text]', 'Bug [Text]', 'required');
82
83
		if($this->form_validation->run() === TRUE) {
84
			$bug = $this->input->post('bug');
85
86
			//Preferably, I'd like to validate this in some way, but it's a bit too easy to bypass
87
			$success = $this->Tracker->bug->report($bug['text'], NULL, $bug['url']);
88
			if($success) {
89
				$this->output->set_status_header('200'); //Success!
90
			} else {
91
				$this->output->set_status_header('400', 'Unable to report bug?');
92
			}
93
		} else {
94
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
95
		}
96
	}
97
98
	/**
99
	 * Favourite a chapter via the userscript.
100
	 *
101
	 * REQ_PARAMS: api-key, manga[site], manga[title], manga[chapter]
102
	 * METHOD:     POST
103
	 * URL:        /ajax/userscript/favourite
104
	 */
105
	public function favourite() {
106
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
107
		if($this->limiter->limit('tracker_userscript_favourite', 250)) {
108
			$this->output->set_status_header('429', 'Rate limit reached'); //rate limited reached
109
		} else {
110
			$this->form_validation->set_rules('manga[site]', 'Manga [Site]', 'required');
111
			$this->form_validation->set_rules('manga[title]', 'Manga [Title]', 'required');
112
			$this->form_validation->set_rules('manga[chapter]', 'Manga [Chapter]', 'required');
113
114
			if($this->form_validation->run() === TRUE) {
115
				$manga = $this->input->post('manga');
116
117
				$success = $this->Tracker->favourites->set($manga['site'], $manga['title'], $manga['chapter'], $this->userID);
118
				if($success['bool']) {
119
					$this->output->set_status_header('200', $success['status']); //Success!
120
				} else {
121
					$this->output->set_status_header('400', $success['status']);
122
				}
123
			} else {
124
				$this->output->set_status_header('400', 'Missing/invalid parameters.');
125
			}
126
		}
127
	}
128
}
129