Merge branch '6.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / captcha / CaptchaHandler.class.php
... / ...
CommitLineData
1<?php
2
3namespace wcf\system\captcha;
4
5use wcf\data\object\type\ObjectType;
6use wcf\data\object\type\ObjectTypeCache;
7use wcf\system\SingletonFactory;
8use wcf\system\WCF;
9
10/**
11 * Handles captchas.
12 *
13 * @author Matthias Schmidt
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 */
17class CaptchaHandler extends SingletonFactory
18{
19 /**
20 * available captcha object types
21 * @var ObjectType[]
22 */
23 protected $objectTypes = [];
24
25 /**
26 * Returns the available captcha types for selection.
27 *
28 * @return string[]
29 */
30 public function getCaptchaSelection()
31 {
32 $selection = [];
33 foreach ($this->objectTypes as $objectType) {
34 if ($objectType->getProcessor()->isAvailable()) {
35 $selection[$objectType->objectType] = WCF::getLanguage()->get('wcf.captcha.' . $objectType->objectType);
36 }
37 }
38
39 return $selection;
40 }
41
42 /**
43 * Returns the captcha object type with the given id or `null` if no such
44 * object type exists.
45 *
46 * @param int $objectTypeID
47 * @return ObjectType|null
48 */
49 public function getObjectType($objectTypeID)
50 {
51 return $this->objectTypes[$objectTypeID] ?? null;
52 }
53
54 /**
55 * Returns the captcha object type with the given name or null if no such
56 * object type exists.
57 *
58 * @param string $objectType
59 * @return ObjectType|null
60 */
61 public function getObjectTypeByName($objectType)
62 {
63 return ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.captcha', $objectType);
64 }
65
66 /**
67 * @inheritDoc
68 */
69 protected function init()
70 {
71 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.captcha');
72 foreach ($objectTypes as $objectType) {
73 $this->objectTypes[$objectType->objectTypeID] = $objectType;
74 }
75 }
76}