Completed
Push — master ( 9553e2...91bcf6 )
by Angus
09:37
created

Userscript::favourite()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 0
dl 0
loc 23
ccs 0
cts 0
cp 0
crap 30
rs 8.5906
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', 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 View Code Duplication
			if($this->form_validation->run() === TRUE) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
				$manga = $this->input->post('manga');
47
48
				$success = $this->Tracker->updateTracker($this->userID, $manga['site'], $manga['title'], $manga['chapter']);
49
				if($success) {
50
					$this->output->set_status_header('200'); //Success!
51
				} else {
52
					//TODO: We should probably try and have more verbose errors here. Return via JSON or something.
53
					$this->output->set_status_header('400', 'Unable to update?');
54
				}
55
			} else {
56
				$this->output->set_status_header('400', 'Missing/invalid parameters.');
57
			}
58
		}
59
	}
60
61
	/**
62
	 * Report a bug via userscript.
63
	 *
64
	 * REQ_PARAMS: api-key, bug[url], bug[text]
65
	 * METHOD:     POST
66
	 * URL:        /ajax/userscript/report_bug
67
	 */
68
	public function report_bug() {
69
		$this->load->library('user_agent');
70
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
71
		$this->form_validation->set_rules('bug[url]',  'Bug [URL]',  'required');
72
		$this->form_validation->set_rules('bug[text]', 'Bug [Text]', 'required');
73
74 View Code Duplication
		if($this->form_validation->run() === TRUE) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
			$bug = $this->input->post('bug');
76
77
			//Preferably, I'd like to validate this in some way, but it's a bit too easy to bypass
78
			$success = $this->Tracker->reportBug($bug['text'], NULL, $bug['url']);
79
			if($success) {
80
				$this->output->set_status_header('200'); //Success!
81
			} else {
82
				$this->output->set_status_header('400', 'Unable to report bug?');
83
			}
84
		} else {
85
			$this->output->set_status_header('400', 'Missing/invalid parameters.');
86
		}
87
	}
88
89
	/**
90
	 * Favourite a chapter via the userscript.
91
	 *
92
	 * REQ_PARAMS: api-key, manga[site], manga[title], manga[chapter]
93
	 * METHOD:     POST
94
	 * URL:        /ajax/userscript/favourite
95
	 */
96
	public function favourite() {
97
		if($this->output->is_custom_header_set()) { $this->output->reset_status_header(); return; }
98
		if($this->limiter->limit('tracker_userscript_favourite', 250)) {
99
			$this->output->set_status_header('429', 'Rate limit reached'); //rate limited reached
100
		} else {
101
			$this->form_validation->set_rules('manga[site]', 'Manga [Site]', 'required');
102
			$this->form_validation->set_rules('manga[title]', 'Manga [Title]', 'required');
103
			$this->form_validation->set_rules('manga[chapter]', 'Manga [Chapter]', 'required');
104
105
			if($this->form_validation->run() === TRUE) {
106
				$manga = $this->input->post('manga');
107
108
				$success = $this->Tracker->favouriteChapter($this->userID, $manga['site'], $manga['title'], $manga['chapter']);
109
				if($success['bool']) {
110
					$this->output->set_status_header('200', $success['status']); //Success!
111
				} else {
112
					$this->output->set_status_header('400', $success['status']);
113
				}
114
			} else {
115
				$this->output->set_status_header('400', 'Missing/invalid parameters.');
116
			}
117
		}
118
	}
119
}
120