| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Wikipedia Account Creation Assistance tool                                 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * ACC Development Team. Please see team.json for a list of contributors.     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *                                                                            * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This is free and unencumbered software released into the public domain.    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Please see LICENSE.md for the full licencing statement.                    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  ******************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Waca\DataObjects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Waca\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Waca\Exceptions\ApplicationLogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Waca\Exceptions\OptimisticLockFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Waca\PdoDatabase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Ban data object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class RequestData extends DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const TYPE_IPV4 = 'ipv4'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const TYPE_IPV6 = 'ipv6'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     const TYPE_CONFIRM_IPV4 = 'confirm-ipv4'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     const TYPE_CONFIRM_IPV6 = 'confirm-ipv6'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const TYPE_EMAIL = 'email'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     const TYPE_USERAGENT = 'useragent'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const TYPE_CONFIRM_USERAGENT = 'confirm-useragent'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const TYPE_CLIENTHINT = 'clienthint'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     const TYPE_CONFIRM_CLIENTHINT = 'confirm-clienthint'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** @var string|null */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public static function saveForRequest(Request $request, string $type, string $value, ?string $name = null): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $requestData = new RequestData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $requestData->setDatabase($request->getDatabase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $requestData->setRequest($request->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $requestData->setType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $requestData->setValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $requestData->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $requestData->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     public static function getForRequest(int $requestId, PdoDatabase $database, ?string $type = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $statement = $database->prepare(<<<SQL | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | SELECT * FROM requestdata | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | WHERE request = :request AND type LIKE COALESCE(:type, '%'); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | SQL | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $statement->bindValue(":request", $requestId); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $statement->bindValue(":type", $type); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $statement->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $result = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         /** @var RequestData $v */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $v->setDatabase($database); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $result[] = $v; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if ($this->isNew()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             // insert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $statement = $this->dbObject->prepare(<<<SQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | INSERT INTO `requestdata` (request, type, name, value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | VALUES (:request, :type, :name, :value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | SQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $statement->bindValue(":request", $this->request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $statement->bindValue(":type", $this->type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $statement->bindValue(":name", $this->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $statement->bindValue(":value", $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if ($statement->execute()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->id = (int)$this->dbObject->lastInsertId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 throw new Exception($statement->errorInfo()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             // update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             throw new ApplicationLogicException('Updates to RequestData are not supported.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function getRequest(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return $this->request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param int $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function setRequest(int $request): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function getType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function setType(string $type): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->type = $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function getName(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param string|null $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function setName(?string $name): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     public function getValue(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     public function setValue(string $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |