forked from npatel2012/myfaq-php-isa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
138 lines (117 loc) · 4.04 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* The rest/json application interface
*
* PHP Version 5.3
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* @category phpMyFAQ
* @package PMF_Service
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2009-2015 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link http://www.phpmyfaq.de
* @since 2009-09-03
*/
define('IS_VALID_PHPMYFAQ', null);
//
// Bootstrapping
//
require 'inc/Bootstrap.php';
//
// Send headers
//
$http = new PMF_Helper_Http();
$http->setContentType('application/json');
$http->addHeader();
//
// Set user permissions
//
$currentUser = -1;
$currentGroups = array(-1);
$action = PMF_Filter::filterInput(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
$language = PMF_Filter::filterInput(INPUT_GET, 'lang', FILTER_SANITIZE_STRING, 'en');
$categoryId = PMF_Filter::filterInput(INPUT_GET, 'categoryId', FILTER_VALIDATE_INT);
$recordId = PMF_Filter::filterInput(INPUT_GET, 'recordId', FILTER_VALIDATE_INT);
//
// Get language (default: english)
//
$Language = new PMF_Language($faqConfig);
$language = $Language->setLanguage($faqConfig->get('main.languageDetection'), $faqConfig->get('main.language'));
//
// Set language
//
if (PMF_Language::isASupportedLanguage($language)) {
require PMF_LANGUAGE_DIR . '/language_' . $language . '.php';
} else {
require PMF_LANGUAGE_DIR . '/language_en.php';
}
$faqConfig->setLanguage($Language);
$plr = new PMF_Language_Plurals($PMF_LANG);
PMF_String::init($language);
//
// Set empty result
//
$result = array();
//
// Check if FAQ should be secured
//
if ($faqConfig->get('security.enableLoginOnly')) {
echo json_encode(array('You are not allowed to view this content.'));
$http->sendStatus(403);
}
//
// Handle actions
//
switch ($action) {
case 'getVersion':
$result = array('version' => $faqConfig->get('main.currentVersion'));
break;
case 'getApiVersion':
$result = array('apiVersion' => (int)$faqConfig->get('main.currentApiVersion'));
break;
case 'search':
$faq = new PMF_Faq($faqConfig);
$user = new PMF_User($faqConfig);
$search = new PMF_Search($faqConfig);
$faqSearchResult = new PMF_Search_Resultset($user, $faq, $faqConfig);
$searchString = PMF_Filter::filterInput(INPUT_GET, 'q', FILTER_SANITIZE_STRIPPED);
$searchResults = $search->search($searchString, false);
$url = $faqConfig->get('main.referenceURL') . '/index.php?action=artikel&cat=%d&id=%d&artlang=%s';
$faqSearchResult->reviewResultset($searchResults);
$result = array();
foreach ($faqSearchResult->getResultset() as $data) {
$data->answer = html_entity_decode(strip_tags($data->answer), ENT_COMPAT, 'utf-8');
$data->answer = PMF_Utils::makeShorterText($data->answer, 12);
$data->link = sprintf($url, $data->category_id, $data->id, $data->lang);
$result[] = $data;
}
break;
case 'getCategories':
$category = new PMF_Category($faqConfig, $currentGroups, true);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$result = $category->categories;
break;
case 'getFaqs':
$faq = new PMF_Faq($faqConfig);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);
$result = $faq->getAllRecordPerCategory($categoryId);
break;
case 'getFaq':
$faq = new PMF_Faq($faqConfig);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);
$faq->getRecord($recordId);
$result = $faq->faqRecord;
break;
default:
$result = 'I am completely operational, and all my circuits are functioning perfectly.';
break;
}
// print result as JSON
echo json_encode($result);