@@ -2,21 +2,21 @@ |
||
| 2 | 2 | |
| 3 | 3 | interface SpellProvider |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Spellchecks an array of words. |
|
| 7 | - * |
|
| 8 | - * @param string $locale Locale code to check |
|
| 9 | - * @param array $words List of words to spellcheck. |
|
| 10 | - * @return array List of misspelled words. |
|
| 11 | - */ |
|
| 12 | - public function checkWords($locale, $words); |
|
| 5 | + /** |
|
| 6 | + * Spellchecks an array of words. |
|
| 7 | + * |
|
| 8 | + * @param string $locale Locale code to check |
|
| 9 | + * @param array $words List of words to spellcheck. |
|
| 10 | + * @return array List of misspelled words. |
|
| 11 | + */ |
|
| 12 | + public function checkWords($locale, $words); |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Returns suggestions of for a specific word. |
|
| 16 | - * |
|
| 17 | - * @param string $locale Locale code to check |
|
| 18 | - * @param string $word Specific word to get suggestions for. |
|
| 19 | - * @return array List of suggestions for the specified word. |
|
| 20 | - */ |
|
| 21 | - public function getSuggestions($locale, $word); |
|
| 14 | + /** |
|
| 15 | + * Returns suggestions of for a specific word. |
|
| 16 | + * |
|
| 17 | + * @param string $locale Locale code to check |
|
| 18 | + * @param string $word Specific word to get suggestions for. |
|
| 19 | + * @return array List of suggestions for the specified word. |
|
| 20 | + */ |
|
| 21 | + public function getSuggestions($locale, $word); |
|
| 22 | 22 | } |
@@ -5,227 +5,227 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class SpellController extends Controller |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Locales to spellcheck |
|
| 10 | - * |
|
| 11 | - * @var array |
|
| 12 | - * @config |
|
| 13 | - */ |
|
| 14 | - private static $locales = array(); |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Necessary permission required to spellcheck. Set to empty or null to disable restrictions. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - * @config |
|
| 21 | - */ |
|
| 22 | - private static $required_permission = 'CMS_ACCESS_CMSMain'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Enable security token for spellchecking |
|
| 26 | - * |
|
| 27 | - * @var bool |
|
| 28 | - * @config |
|
| 29 | - */ |
|
| 30 | - private static $enable_security_token = true; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Dependencies required by this controller |
|
| 34 | - * |
|
| 35 | - * @var array |
|
| 36 | - * @config |
|
| 37 | - */ |
|
| 38 | - private static $dependencies = array( |
|
| 39 | - 'Provider' => '%$SpellProvider' |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Spellcheck provider |
|
| 44 | - * |
|
| 45 | - * @var SpellProvider |
|
| 46 | - */ |
|
| 47 | - protected $provider = null; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Parsed request data |
|
| 51 | - * |
|
| 52 | - * @var array|null Null if not set or an array if parsed |
|
| 53 | - */ |
|
| 54 | - protected $data = null; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Get the current provider |
|
| 58 | - * |
|
| 59 | - * @return SpellProvider |
|
| 60 | - */ |
|
| 61 | - public function getProvider() |
|
| 62 | - { |
|
| 63 | - return $this->provider; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Gets locales to spellcheck for |
|
| 68 | - * |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - public static function get_locales() |
|
| 72 | - { |
|
| 73 | - // Default to current locale if none configured |
|
| 74 | - return self::config()->locales ?: array(i18n::get_locale()); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Set the provider to use |
|
| 79 | - * |
|
| 80 | - * @param SpellProvider $provider |
|
| 81 | - * @return $this |
|
| 82 | - */ |
|
| 83 | - public function setProvider(SpellProvider $provider) |
|
| 84 | - { |
|
| 85 | - $this->provider = $provider; |
|
| 86 | - return $this; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Parse the output response |
|
| 91 | - * |
|
| 92 | - * @param string $id Request ID |
|
| 93 | - * @param array|null $result Result data |
|
| 94 | - * @param array|null $error Error data |
|
| 95 | - * @param int $code HTTP Response code |
|
| 96 | - */ |
|
| 97 | - protected function result($id, $result, $error = null, $code = 200) |
|
| 98 | - { |
|
| 99 | - $this->response->setStatusCode($code); |
|
| 100 | - $this->response->setBody(json_encode(array( |
|
| 101 | - 'id' => $id ? preg_replace('/\W/', '', $id) : null, // Cleanup id |
|
| 102 | - 'result' => $result, |
|
| 103 | - 'error' => $error |
|
| 104 | - ))); |
|
| 105 | - return $this->response; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - protected function success($result) |
|
| 109 | - { |
|
| 110 | - $data = $this->getRequestData(); |
|
| 111 | - return $this->result($data['id'], $result); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Set the error |
|
| 116 | - * |
|
| 117 | - * @param string $message |
|
| 118 | - * @param int $code HTTP error code |
|
| 119 | - */ |
|
| 120 | - protected function error($message, $code) |
|
| 121 | - { |
|
| 122 | - $error = array( |
|
| 123 | - 'errstr' => $message, |
|
| 124 | - 'errfile' => '', |
|
| 125 | - 'errline' => null, |
|
| 126 | - 'errcontext' => '', |
|
| 127 | - 'level' => 'FATAL' |
|
| 128 | - ); |
|
| 129 | - return $this->result(null, null, $error, $code); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - public function index() |
|
| 133 | - { |
|
| 134 | - $this->setHeaders(); |
|
| 135 | - |
|
| 136 | - // Check security token |
|
| 137 | - if (self::config()->enable_security_token && !SecurityToken::inst()->checkRequest($this->request)) { |
|
| 138 | - return $this->error( |
|
| 139 | - _t( |
|
| 140 | - 'SpellController.SecurityMissing', |
|
| 141 | - 'Your session has expired. Please refresh your browser to continue.' |
|
| 142 | - ), |
|
| 143 | - 400 |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Check permission |
|
| 148 | - $permission = self::config()->required_permission; |
|
| 149 | - if ($permission && !Permission::check($permission)) { |
|
| 150 | - return $this->error(_t('SpellController.SecurityDenied', 'Permission Denied'), 403); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Check data |
|
| 154 | - $data = $this->getRequestData(); |
|
| 155 | - if (empty($data)) { |
|
| 156 | - return $this->error(_t('SpellController.MissingData', "Could not get raw post data"), 400); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Check params and request type |
|
| 160 | - if (!Director::is_ajax() || empty($data['method']) || empty($data['params']) || count($data['params']) < 2) { |
|
| 161 | - return $this->error(_t('SpellController.InvalidRequest', 'Invalid request'), 400); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // Check locale |
|
| 165 | - $params = $data['params']; |
|
| 166 | - $locale = $params[0]; |
|
| 167 | - if (!in_array($locale, self::get_locales())) { |
|
| 168 | - return $this->error(_t('SpellController.InvalidLocale', 'Not supported locale'), 400); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Check provider |
|
| 172 | - $provider = $this->getProvider(); |
|
| 173 | - if (empty($provider)) { |
|
| 174 | - return $this->error(_t('SpellController.MissingProviders', "No spellcheck module installed"), 500); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // Perform action |
|
| 178 | - try { |
|
| 179 | - $method = $data['method']; |
|
| 180 | - $words = $params[1]; |
|
| 181 | - switch ($method) { |
|
| 182 | - case 'checkWords': |
|
| 183 | - return $this->success($provider->checkWords($locale, $words)); |
|
| 184 | - case 'getSuggestions': |
|
| 185 | - return $this->success($provider->getSuggestions($locale, $words)); |
|
| 186 | - default: |
|
| 187 | - return $this->error( |
|
| 188 | - _t( |
|
| 189 | - 'SpellController.UnsupportedMethod', |
|
| 190 | - "Unsupported method '{method}'", |
|
| 191 | - array('method' => $method) |
|
| 192 | - ), |
|
| 193 | - 400 |
|
| 194 | - ); |
|
| 195 | - } |
|
| 196 | - } catch (SpellException $ex) { |
|
| 197 | - return $this->error($ex->getMessage(), $ex->getCode()); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Ensures the response has the correct headers |
|
| 203 | - */ |
|
| 204 | - protected function setHeaders() |
|
| 205 | - { |
|
| 206 | - // Set headers |
|
| 207 | - HTTP::set_cache_age(0); |
|
| 208 | - HTTP::add_cache_headers($this->response); |
|
| 209 | - $this->response |
|
| 210 | - ->addHeader('Content-Type', 'application/json') |
|
| 211 | - ->addHeader('Content-Encoding', 'UTF-8') |
|
| 212 | - ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Get request data |
|
| 217 | - * |
|
| 218 | - * @return array Parsed data with an id, method, and params key |
|
| 219 | - */ |
|
| 220 | - protected function getRequestData() |
|
| 221 | - { |
|
| 222 | - // Check if data needs to be parsed |
|
| 223 | - if ($this->data === null) { |
|
| 224 | - // Parse data from input |
|
| 225 | - $result = $this->request->requestVar('json_data') |
|
| 226 | - ?: file_get_contents("php://input"); |
|
| 227 | - $this->data = $result ? json_decode($result, true) : array(); |
|
| 228 | - } |
|
| 229 | - return $this->data; |
|
| 230 | - } |
|
| 8 | + /** |
|
| 9 | + * Locales to spellcheck |
|
| 10 | + * |
|
| 11 | + * @var array |
|
| 12 | + * @config |
|
| 13 | + */ |
|
| 14 | + private static $locales = array(); |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Necessary permission required to spellcheck. Set to empty or null to disable restrictions. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + * @config |
|
| 21 | + */ |
|
| 22 | + private static $required_permission = 'CMS_ACCESS_CMSMain'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Enable security token for spellchecking |
|
| 26 | + * |
|
| 27 | + * @var bool |
|
| 28 | + * @config |
|
| 29 | + */ |
|
| 30 | + private static $enable_security_token = true; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Dependencies required by this controller |
|
| 34 | + * |
|
| 35 | + * @var array |
|
| 36 | + * @config |
|
| 37 | + */ |
|
| 38 | + private static $dependencies = array( |
|
| 39 | + 'Provider' => '%$SpellProvider' |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Spellcheck provider |
|
| 44 | + * |
|
| 45 | + * @var SpellProvider |
|
| 46 | + */ |
|
| 47 | + protected $provider = null; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Parsed request data |
|
| 51 | + * |
|
| 52 | + * @var array|null Null if not set or an array if parsed |
|
| 53 | + */ |
|
| 54 | + protected $data = null; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Get the current provider |
|
| 58 | + * |
|
| 59 | + * @return SpellProvider |
|
| 60 | + */ |
|
| 61 | + public function getProvider() |
|
| 62 | + { |
|
| 63 | + return $this->provider; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Gets locales to spellcheck for |
|
| 68 | + * |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + public static function get_locales() |
|
| 72 | + { |
|
| 73 | + // Default to current locale if none configured |
|
| 74 | + return self::config()->locales ?: array(i18n::get_locale()); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Set the provider to use |
|
| 79 | + * |
|
| 80 | + * @param SpellProvider $provider |
|
| 81 | + * @return $this |
|
| 82 | + */ |
|
| 83 | + public function setProvider(SpellProvider $provider) |
|
| 84 | + { |
|
| 85 | + $this->provider = $provider; |
|
| 86 | + return $this; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Parse the output response |
|
| 91 | + * |
|
| 92 | + * @param string $id Request ID |
|
| 93 | + * @param array|null $result Result data |
|
| 94 | + * @param array|null $error Error data |
|
| 95 | + * @param int $code HTTP Response code |
|
| 96 | + */ |
|
| 97 | + protected function result($id, $result, $error = null, $code = 200) |
|
| 98 | + { |
|
| 99 | + $this->response->setStatusCode($code); |
|
| 100 | + $this->response->setBody(json_encode(array( |
|
| 101 | + 'id' => $id ? preg_replace('/\W/', '', $id) : null, // Cleanup id |
|
| 102 | + 'result' => $result, |
|
| 103 | + 'error' => $error |
|
| 104 | + ))); |
|
| 105 | + return $this->response; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + protected function success($result) |
|
| 109 | + { |
|
| 110 | + $data = $this->getRequestData(); |
|
| 111 | + return $this->result($data['id'], $result); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Set the error |
|
| 116 | + * |
|
| 117 | + * @param string $message |
|
| 118 | + * @param int $code HTTP error code |
|
| 119 | + */ |
|
| 120 | + protected function error($message, $code) |
|
| 121 | + { |
|
| 122 | + $error = array( |
|
| 123 | + 'errstr' => $message, |
|
| 124 | + 'errfile' => '', |
|
| 125 | + 'errline' => null, |
|
| 126 | + 'errcontext' => '', |
|
| 127 | + 'level' => 'FATAL' |
|
| 128 | + ); |
|
| 129 | + return $this->result(null, null, $error, $code); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + public function index() |
|
| 133 | + { |
|
| 134 | + $this->setHeaders(); |
|
| 135 | + |
|
| 136 | + // Check security token |
|
| 137 | + if (self::config()->enable_security_token && !SecurityToken::inst()->checkRequest($this->request)) { |
|
| 138 | + return $this->error( |
|
| 139 | + _t( |
|
| 140 | + 'SpellController.SecurityMissing', |
|
| 141 | + 'Your session has expired. Please refresh your browser to continue.' |
|
| 142 | + ), |
|
| 143 | + 400 |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Check permission |
|
| 148 | + $permission = self::config()->required_permission; |
|
| 149 | + if ($permission && !Permission::check($permission)) { |
|
| 150 | + return $this->error(_t('SpellController.SecurityDenied', 'Permission Denied'), 403); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Check data |
|
| 154 | + $data = $this->getRequestData(); |
|
| 155 | + if (empty($data)) { |
|
| 156 | + return $this->error(_t('SpellController.MissingData', "Could not get raw post data"), 400); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Check params and request type |
|
| 160 | + if (!Director::is_ajax() || empty($data['method']) || empty($data['params']) || count($data['params']) < 2) { |
|
| 161 | + return $this->error(_t('SpellController.InvalidRequest', 'Invalid request'), 400); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // Check locale |
|
| 165 | + $params = $data['params']; |
|
| 166 | + $locale = $params[0]; |
|
| 167 | + if (!in_array($locale, self::get_locales())) { |
|
| 168 | + return $this->error(_t('SpellController.InvalidLocale', 'Not supported locale'), 400); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Check provider |
|
| 172 | + $provider = $this->getProvider(); |
|
| 173 | + if (empty($provider)) { |
|
| 174 | + return $this->error(_t('SpellController.MissingProviders', "No spellcheck module installed"), 500); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // Perform action |
|
| 178 | + try { |
|
| 179 | + $method = $data['method']; |
|
| 180 | + $words = $params[1]; |
|
| 181 | + switch ($method) { |
|
| 182 | + case 'checkWords': |
|
| 183 | + return $this->success($provider->checkWords($locale, $words)); |
|
| 184 | + case 'getSuggestions': |
|
| 185 | + return $this->success($provider->getSuggestions($locale, $words)); |
|
| 186 | + default: |
|
| 187 | + return $this->error( |
|
| 188 | + _t( |
|
| 189 | + 'SpellController.UnsupportedMethod', |
|
| 190 | + "Unsupported method '{method}'", |
|
| 191 | + array('method' => $method) |
|
| 192 | + ), |
|
| 193 | + 400 |
|
| 194 | + ); |
|
| 195 | + } |
|
| 196 | + } catch (SpellException $ex) { |
|
| 197 | + return $this->error($ex->getMessage(), $ex->getCode()); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Ensures the response has the correct headers |
|
| 203 | + */ |
|
| 204 | + protected function setHeaders() |
|
| 205 | + { |
|
| 206 | + // Set headers |
|
| 207 | + HTTP::set_cache_age(0); |
|
| 208 | + HTTP::add_cache_headers($this->response); |
|
| 209 | + $this->response |
|
| 210 | + ->addHeader('Content-Type', 'application/json') |
|
| 211 | + ->addHeader('Content-Encoding', 'UTF-8') |
|
| 212 | + ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Get request data |
|
| 217 | + * |
|
| 218 | + * @return array Parsed data with an id, method, and params key |
|
| 219 | + */ |
|
| 220 | + protected function getRequestData() |
|
| 221 | + { |
|
| 222 | + // Check if data needs to be parsed |
|
| 223 | + if ($this->data === null) { |
|
| 224 | + // Parse data from input |
|
| 225 | + $result = $this->request->requestVar('json_data') |
|
| 226 | + ?: file_get_contents("php://input"); |
|
| 227 | + $this->data = $result ? json_decode($result, true) : array(); |
|
| 228 | + } |
|
| 229 | + return $this->data; |
|
| 230 | + } |
|
| 231 | 231 | } |
@@ -5,13 +5,13 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class SpellException extends Exception |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @param string $message |
|
| 10 | - * @param int $code HTTP error code |
|
| 11 | - * @param Exception $previous |
|
| 12 | - */ |
|
| 13 | - public function __construct($message, $code, $previous = null) |
|
| 14 | - { |
|
| 15 | - parent::__construct($message, $code, $previous); |
|
| 16 | - } |
|
| 8 | + /** |
|
| 9 | + * @param string $message |
|
| 10 | + * @param int $code HTTP error code |
|
| 11 | + * @param Exception $previous |
|
| 12 | + */ |
|
| 13 | + public function __construct($message, $code, $previous = null) |
|
| 14 | + { |
|
| 15 | + parent::__construct($message, $code, $previous); |
|
| 16 | + } |
|
| 17 | 17 | } |
@@ -2,36 +2,36 @@ |
||
| 2 | 2 | |
| 3 | 3 | class SpellRequestFilter implements RequestFilter |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * HtmlEditorConfig name to use |
|
| 7 | - * |
|
| 8 | - * @var string |
|
| 9 | - * @config |
|
| 10 | - */ |
|
| 11 | - private static $editor = 'cms'; |
|
| 5 | + /** |
|
| 6 | + * HtmlEditorConfig name to use |
|
| 7 | + * |
|
| 8 | + * @var string |
|
| 9 | + * @config |
|
| 10 | + */ |
|
| 11 | + private static $editor = 'cms'; |
|
| 12 | 12 | |
| 13 | - public function preRequest(\SS_HTTPRequest $request, \Session $session, \DataModel $model) |
|
| 14 | - { |
|
| 15 | - // Check languages to set |
|
| 16 | - $languages = array(); |
|
| 17 | - foreach (SpellController::get_locales() as $locale) { |
|
| 18 | - $languages[] = i18n::get_locale_name($locale).'='.$locale; |
|
| 19 | - } |
|
| 13 | + public function preRequest(\SS_HTTPRequest $request, \Session $session, \DataModel $model) |
|
| 14 | + { |
|
| 15 | + // Check languages to set |
|
| 16 | + $languages = array(); |
|
| 17 | + foreach (SpellController::get_locales() as $locale) { |
|
| 18 | + $languages[] = i18n::get_locale_name($locale).'='.$locale; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | |
| 22 | - // Set settings |
|
| 23 | - $editor = Config::inst()->get(__CLASS__, 'editor'); |
|
| 24 | - HtmlEditorConfig::get($editor)->enablePlugins('spellchecker'); |
|
| 25 | - HtmlEditorConfig::get($editor)->addButtonsToLine(2, 'spellchecker'); |
|
| 26 | - $token = SecurityToken::inst(); |
|
| 27 | - HtmlEditorConfig::get($editor)->setOption('spellchecker_rpc_url', $token->addToUrl('spellcheck/')); |
|
| 28 | - HtmlEditorConfig::get($editor)->setOption('browser_spellcheck', false); |
|
| 29 | - HtmlEditorConfig::get($editor)->setOption('spellchecker_languages', '+'.implode(', ', $languages)); |
|
| 30 | - return true; |
|
| 31 | - } |
|
| 22 | + // Set settings |
|
| 23 | + $editor = Config::inst()->get(__CLASS__, 'editor'); |
|
| 24 | + HtmlEditorConfig::get($editor)->enablePlugins('spellchecker'); |
|
| 25 | + HtmlEditorConfig::get($editor)->addButtonsToLine(2, 'spellchecker'); |
|
| 26 | + $token = SecurityToken::inst(); |
|
| 27 | + HtmlEditorConfig::get($editor)->setOption('spellchecker_rpc_url', $token->addToUrl('spellcheck/')); |
|
| 28 | + HtmlEditorConfig::get($editor)->setOption('browser_spellcheck', false); |
|
| 29 | + HtmlEditorConfig::get($editor)->setOption('spellchecker_languages', '+'.implode(', ', $languages)); |
|
| 30 | + return true; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, \DataModel $model) |
|
| 34 | - { |
|
| 35 | - return true; |
|
| 36 | - } |
|
| 33 | + public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, \DataModel $model) |
|
| 34 | + { |
|
| 35 | + return true; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -5,97 +5,97 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class HunSpellProvider implements SpellProvider |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * See https://gist.github.com/sebskuse/1244667 |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - * @config |
|
| 13 | - */ |
|
| 14 | - private static $pattern = "/^(?P<type>&)\s(?P<original>\w+)\s(?P<count>\d+)\s(?P<offset>\d+):\s(?P<misses>.*+)$/u"; |
|
| 8 | + /** |
|
| 9 | + * See https://gist.github.com/sebskuse/1244667 |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + * @config |
|
| 13 | + */ |
|
| 14 | + private static $pattern = "/^(?P<type>&)\s(?P<original>\w+)\s(?P<count>\d+)\s(?P<offset>\d+):\s(?P<misses>.*+)$/u"; |
|
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Invoke hunspell library |
|
| 19 | - * |
|
| 20 | - * @param string $locale |
|
| 21 | - * @param string $input Input text |
|
| 22 | - * @param string $stdout output |
|
| 23 | - * @param string $stderr error |
|
| 24 | - * @return int Exit code |
|
| 25 | - */ |
|
| 26 | - protected function invoke($locale, $input, &$stdout, &$stderr) |
|
| 27 | - { |
|
| 28 | - // Prepare arguments |
|
| 29 | - $command = 'hunspell -d ' . escapeshellarg($locale); |
|
| 30 | - $descriptorSpecs = array( |
|
| 31 | - 0 => array("pipe", "r"), |
|
| 32 | - 1 => array("pipe", "w"), |
|
| 33 | - 2 => array("pipe", "w") |
|
| 34 | - ); |
|
| 35 | - $env = array( |
|
| 36 | - 'LANG' => $locale . '.utf-8' |
|
| 37 | - ); |
|
| 38 | - // Invoke command |
|
| 39 | - $proc = proc_open($command, $descriptorSpecs, $pipes, null, $env); |
|
| 40 | - if (!is_resource($proc)) { |
|
| 41 | - return 255; |
|
| 42 | - } |
|
| 17 | + /** |
|
| 18 | + * Invoke hunspell library |
|
| 19 | + * |
|
| 20 | + * @param string $locale |
|
| 21 | + * @param string $input Input text |
|
| 22 | + * @param string $stdout output |
|
| 23 | + * @param string $stderr error |
|
| 24 | + * @return int Exit code |
|
| 25 | + */ |
|
| 26 | + protected function invoke($locale, $input, &$stdout, &$stderr) |
|
| 27 | + { |
|
| 28 | + // Prepare arguments |
|
| 29 | + $command = 'hunspell -d ' . escapeshellarg($locale); |
|
| 30 | + $descriptorSpecs = array( |
|
| 31 | + 0 => array("pipe", "r"), |
|
| 32 | + 1 => array("pipe", "w"), |
|
| 33 | + 2 => array("pipe", "w") |
|
| 34 | + ); |
|
| 35 | + $env = array( |
|
| 36 | + 'LANG' => $locale . '.utf-8' |
|
| 37 | + ); |
|
| 38 | + // Invoke command |
|
| 39 | + $proc = proc_open($command, $descriptorSpecs, $pipes, null, $env); |
|
| 40 | + if (!is_resource($proc)) { |
|
| 41 | + return 255; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - // Send content as input |
|
| 45 | - fwrite($pipes[0], $input); |
|
| 46 | - fclose($pipes[0]); |
|
| 44 | + // Send content as input |
|
| 45 | + fwrite($pipes[0], $input); |
|
| 46 | + fclose($pipes[0]); |
|
| 47 | 47 | |
| 48 | - // Get output |
|
| 49 | - $stdout = stream_get_contents($pipes[1]); |
|
| 50 | - fclose($pipes[1]); |
|
| 51 | - $stderr = stream_get_contents($pipes[2]); |
|
| 52 | - fclose($pipes[2]); |
|
| 48 | + // Get output |
|
| 49 | + $stdout = stream_get_contents($pipes[1]); |
|
| 50 | + fclose($pipes[1]); |
|
| 51 | + $stderr = stream_get_contents($pipes[2]); |
|
| 52 | + fclose($pipes[2]); |
|
| 53 | 53 | |
| 54 | - // Get result |
|
| 55 | - return proc_close($proc); |
|
| 56 | - } |
|
| 54 | + // Get result |
|
| 55 | + return proc_close($proc); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get results from hunspell |
|
| 60 | - * |
|
| 61 | - * @param string $locale |
|
| 62 | - * @param array $words |
|
| 63 | - * @return array List of incorrect words, each with their list of suggestions |
|
| 64 | - * @throws SpellException |
|
| 65 | - */ |
|
| 66 | - protected function getResults($locale, $words) |
|
| 67 | - { |
|
| 68 | - // Invoke HunSpell |
|
| 69 | - $input = implode(' ', $words); |
|
| 70 | - $return = $this->invoke($locale, $input, $stdout, $stderr); |
|
| 71 | - if ($stderr) { |
|
| 72 | - throw new SpellException($stderr, 500); |
|
| 73 | - } elseif ($return) { |
|
| 74 | - throw new SpellException("An unidentified error has occurred", 500); |
|
| 75 | - } |
|
| 58 | + /** |
|
| 59 | + * Get results from hunspell |
|
| 60 | + * |
|
| 61 | + * @param string $locale |
|
| 62 | + * @param array $words |
|
| 63 | + * @return array List of incorrect words, each with their list of suggestions |
|
| 64 | + * @throws SpellException |
|
| 65 | + */ |
|
| 66 | + protected function getResults($locale, $words) |
|
| 67 | + { |
|
| 68 | + // Invoke HunSpell |
|
| 69 | + $input = implode(' ', $words); |
|
| 70 | + $return = $this->invoke($locale, $input, $stdout, $stderr); |
|
| 71 | + if ($stderr) { |
|
| 72 | + throw new SpellException($stderr, 500); |
|
| 73 | + } elseif ($return) { |
|
| 74 | + throw new SpellException("An unidentified error has occurred", 500); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // Parse results |
|
| 78 | - $pattern = Config::inst()->get(__CLASS__, 'pattern'); |
|
| 79 | - $results = array(); |
|
| 80 | - foreach (preg_split('/$\R?^/m', $stdout) as $line) { |
|
| 81 | - if (preg_match($pattern, $line, $matches)) { |
|
| 82 | - $results[$matches['original']] = explode(', ', $matches['misses']); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - return $results; |
|
| 86 | - } |
|
| 77 | + // Parse results |
|
| 78 | + $pattern = Config::inst()->get(__CLASS__, 'pattern'); |
|
| 79 | + $results = array(); |
|
| 80 | + foreach (preg_split('/$\R?^/m', $stdout) as $line) { |
|
| 81 | + if (preg_match($pattern, $line, $matches)) { |
|
| 82 | + $results[$matches['original']] = explode(', ', $matches['misses']); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + return $results; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - public function checkWords($locale, $words) |
|
| 89 | - { |
|
| 90 | - $results = $this->getResults($locale, $words); |
|
| 91 | - return array_keys($results); |
|
| 92 | - } |
|
| 88 | + public function checkWords($locale, $words) |
|
| 89 | + { |
|
| 90 | + $results = $this->getResults($locale, $words); |
|
| 91 | + return array_keys($results); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - public function getSuggestions($locale, $word) |
|
| 95 | - { |
|
| 96 | - $results = $this->getResults($locale, array($word)); |
|
| 97 | - if (isset($results[$word])) { |
|
| 98 | - return $results[$word]; |
|
| 99 | - } |
|
| 100 | - } |
|
| 94 | + public function getSuggestions($locale, $word) |
|
| 95 | + { |
|
| 96 | + $results = $this->getResults($locale, array($word)); |
|
| 97 | + if (isset($results[$word])) { |
|
| 98 | + return $results[$word]; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -26,14 +26,14 @@ |
||
| 26 | 26 | protected function invoke($locale, $input, &$stdout, &$stderr) |
| 27 | 27 | { |
| 28 | 28 | // Prepare arguments |
| 29 | - $command = 'hunspell -d ' . escapeshellarg($locale); |
|
| 29 | + $command = 'hunspell -d '.escapeshellarg($locale); |
|
| 30 | 30 | $descriptorSpecs = array( |
| 31 | 31 | 0 => array("pipe", "r"), |
| 32 | 32 | 1 => array("pipe", "w"), |
| 33 | 33 | 2 => array("pipe", "w") |
| 34 | 34 | ); |
| 35 | 35 | $env = array( |
| 36 | - 'LANG' => $locale . '.utf-8' |
|
| 36 | + 'LANG' => $locale.'.utf-8' |
|
| 37 | 37 | ); |
| 38 | 38 | // Invoke command |
| 39 | 39 | $proc = proc_open($command, $descriptorSpecs, $pipes, null, $env); |