| 1 | <?php |
|---|
| 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 | class Index_Services_Users extends Phwittr_Service_Abstract |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | private $_dao = null; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | public function init() |
|---|
| 48 | { |
|---|
| 49 | $this->_dao = $this->getDao('Users'); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | public function isUserNameAvailable($name) |
|---|
| 60 | { |
|---|
| 61 | $row = $this->getUserInfoByName($name); |
|---|
| 62 | if ($row === null) { |
|---|
| 63 | return true; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | return false; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | public function isEmailAvailable($email) |
|---|
| 77 | { |
|---|
| 78 | $row = $this->_dao->fetchRow($this->_dao->select() |
|---|
| 79 | ->where('email = ?', $email) |
|---|
| 80 | ->where('delete_flag = ?', 0)); |
|---|
| 81 | |
|---|
| 82 | if ($row === null) { |
|---|
| 83 | return true; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return false; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function register(array $params) |
|---|
| 97 | { |
|---|
| 98 | $data = array( |
|---|
| 99 | 'registration_flag' => 1, |
|---|
| 100 | 'updated_at' => strftime('%Y-%m-%d %H:%M:%S', time()) |
|---|
| 101 | ); |
|---|
| 102 | |
|---|
| 103 | $where = $this->_dbAdapter->quoteInto('id = ?', $params['id']); |
|---|
| 104 | $this->_dbAdapter->beginTransaction(); |
|---|
| 105 | try { |
|---|
| 106 | $id = $this->_dao->update($data, $where); |
|---|
| 107 | } catch(Exception $e) { |
|---|
| 108 | $this->_dbAdapter->rollBack(); |
|---|
| 109 | $this->_message = $e->getMessage(); |
|---|
| 110 | return false; |
|---|
| 111 | } |
|---|
| 112 | $this->_dbAdapter->commit(); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | $data['id'] = $id; |
|---|
| 116 | $data['user_name'] = $params['user_name']; |
|---|
| 117 | $data['email'] = $params['email']; |
|---|
| 118 | $data['private_flag'] = $params['private_flag']; |
|---|
| 119 | $data['userLevel'] = 'owner'; |
|---|
| 120 | $class = new stdClass; |
|---|
| 121 | foreach ($data as $key => $val) { |
|---|
| 122 | if ($key === 'password') { |
|---|
| 123 | continue; |
|---|
| 124 | } |
|---|
| 125 | $class->{$key} = $val; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | $auth = Zend_Auth::getInstance(); |
|---|
| 129 | $auth->getStorage()->write($class); |
|---|
| 130 | |
|---|
| 131 | return true; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | public function preRegister(array $params) |
|---|
| 143 | { |
|---|
| 144 | $registerKey = sha1(uniqid(mt_rand(), true)); |
|---|
| 145 | $data = array( |
|---|
| 146 | 'user_name' => $params['name'], |
|---|
| 147 | 'email' => $params['email'], |
|---|
| 148 | 'password' => $this->_createPassword($params['password']), |
|---|
| 149 | 'private_flag' => 0, |
|---|
| 150 | 'registration_key' => $registerKey, |
|---|
| 151 | 'registration_flag' => 0, |
|---|
| 152 | 'created_at' => strftime('%Y-%m-%d %H:%M:%S', time()), |
|---|
| 153 | 'delete_flag' => 0 |
|---|
| 154 | ); |
|---|
| 155 | |
|---|
| 156 | $this->_dbAdapter->beginTransaction(); |
|---|
| 157 | try { |
|---|
| 158 | $id = $this->_dao->insert($data); |
|---|
| 159 | } catch(Exception $e) { |
|---|
| 160 | $this->_dbAdapter->rollBack(); |
|---|
| 161 | $this->_message = $e->getMessage(); |
|---|
| 162 | return null; |
|---|
| 163 | } |
|---|
| 164 | $this->_dbAdapter->commit(); |
|---|
| 165 | |
|---|
| 166 | return $registerKey; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | public function getUserInfoByRegisterKey($key) |
|---|
| 177 | { |
|---|
| 178 | $row = $this->_dao->fetchRow($this->_dao->select() |
|---|
| 179 | ->where('registration_key = ?', $key) |
|---|
| 180 | ->where('registration_flag =?', 0) |
|---|
| 181 | ->where('delete_flag = ?', 0)); |
|---|
| 182 | |
|---|
| 183 | if (is_null($row)) { |
|---|
| 184 | return null; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return $row; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | public function update(array $params, $id) |
|---|
| 199 | { |
|---|
| 200 | $update = strftime('%Y-%m-%d %H:%M:%S', time()); |
|---|
| 201 | $data = array( |
|---|
| 202 | 'user_name' => $params['name'], |
|---|
| 203 | 'email' => $params['email'], |
|---|
| 204 | 'private_flag' => $params['private_flag'], |
|---|
| 205 | 'updated_at' => $update |
|---|
| 206 | ); |
|---|
| 207 | |
|---|
| 208 | $id = (is_numeric($id) ? (int)$id : $id); |
|---|
| 209 | $where = $this->_dbAdapter->quoteInto('id = ?', $id); |
|---|
| 210 | |
|---|
| 211 | $this->_dbAdapter->beginTransaction(); |
|---|
| 212 | try { |
|---|
| 213 | $this->_dao->update($data, $where); |
|---|
| 214 | } catch(Exception $e) { |
|---|
| 215 | $this->_dbAdapter->rollBack(); |
|---|
| 216 | $this->_message = $e->getMessage(); |
|---|
| 217 | return false; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | $this->_dbAdapter->commit(); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | $auth = Zend_Auth::getInstance()->getIdentity(); |
|---|
| 224 | $auth->user_name = $params['name']; |
|---|
| 225 | $auth->email = $params['email']; |
|---|
| 226 | $auth->private_flag = $params['private_flag']; |
|---|
| 227 | $auth->updated_at = $update; |
|---|
| 228 | Zend_Auth::getInstance()->getStorage()->write($auth); |
|---|
| 229 | |
|---|
| 230 | return true; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | public function updatePassword($password, $id) |
|---|
| 242 | { |
|---|
| 243 | if ($password === null) { |
|---|
| 244 | return false; |
|---|
| 245 | } |
|---|
| 246 | $update = strftime('%Y-%m-%d %H:%M:%S', time()); |
|---|
| 247 | $data = array( |
|---|
| 248 | 'password' => $this->_createPassword($password), |
|---|
| 249 | 'updated_at' => $update |
|---|
| 250 | ); |
|---|
| 251 | |
|---|
| 252 | $id = (is_numeric($id) ? (int)$id : $id); |
|---|
| 253 | $where = $this->_dbAdapter->quoteInto('id = ?', $id); |
|---|
| 254 | |
|---|
| 255 | $this->_dbAdapter->beginTransaction(); |
|---|
| 256 | try { |
|---|
| 257 | $this->_dao->update($data, $where); |
|---|
| 258 | } catch(Exception $e) { |
|---|
| 259 | $this->_dbAdapter->rollBack(); |
|---|
| 260 | $this->_message = $e->getMessage(); |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | $this->_dbAdapter->commit(); |
|---|
| 265 | |
|---|
| 266 | return true; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | public function updateImage($path, $id) |
|---|
| 278 | { |
|---|
| 279 | if ($path === null || $path === '') { |
|---|
| 280 | return false; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | $update = strftime('%Y-%m-%d %H:%M:%S', time()); |
|---|
| 284 | $data = array( |
|---|
| 285 | 'image' => $path, |
|---|
| 286 | 'updated_at' => $update |
|---|
| 287 | ); |
|---|
| 288 | |
|---|
| 289 | $id = (is_numeric($id) ? (int)$id : $id); |
|---|
| 290 | $where = $this->_dbAdapter->quoteInto('id = ?', $id); |
|---|
| 291 | |
|---|
| 292 | $this->_dbAdapter->beginTransaction(); |
|---|
| 293 | try { |
|---|
| 294 | $this->_dao->update($data, $where); |
|---|
| 295 | } catch(Exception $e) { |
|---|
| 296 | $this->_dbAdapter->rollBack(); |
|---|
| 297 | $this->_message = $e->getMessage(); |
|---|
| 298 | return false; |
|---|
| 299 | } |
|---|
| 300 | $this->_dbAdapter->commit(); |
|---|
| 301 | |
|---|
| 302 | return true; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | public function deleteImage($id) |
|---|
| 313 | { |
|---|
| 314 | if (!is_numeric($id)) { |
|---|
| 315 | return false; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | $update = strftime('%Y-%m-%d %H:%M:%S', time()); |
|---|
| 319 | $data = array( |
|---|
| 320 | 'image' => null, |
|---|
| 321 | 'updated_at' => $update |
|---|
| 322 | ); |
|---|
| 323 | $where = $this->_dbAdapter->quoteInto('id = ?', $id); |
|---|
| 324 | |
|---|
| 325 | $this->_dbAdapter->beginTransaction(); |
|---|
| 326 | try { |
|---|
| 327 | $this->_dao->update($data, $where); |
|---|
| 328 | } catch(Exception $e) { |
|---|
| 329 | $this->_dbAdapter->rollBack(); |
|---|
| 330 | $this->_message = $e->getMessage(); |
|---|
| 331 | return false; |
|---|
| 332 | } |
|---|
| 333 | $this->_dbAdapter->commit(); |
|---|
| 334 | |
|---|
| 335 | return true; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | private function _createPassword($plain) |
|---|
| 346 | { |
|---|
| 347 | $salt = substr(sha1(uniqid(mt_rand(), true)), 0, 16); |
|---|
| 348 | $password = sha1($salt . $plain) . ':' . $salt; |
|---|
| 349 | |
|---|
| 350 | return $password; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | public function getSalt($value) |
|---|
| 361 | { |
|---|
| 362 | if ($this->_isEmailAddress($value)) { |
|---|
| 363 | $column = 'email = ?'; |
|---|
| 364 | } else { |
|---|
| 365 | $column = 'user_name = ?'; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | $row = $this->_dao->fetchRow($this->_dao->select() |
|---|
| 369 | ->where($column, $value) |
|---|
| 370 | ->where('delete_flag = ?', 0)); |
|---|
| 371 | |
|---|
| 372 | if ($row === null) { |
|---|
| 373 | return null; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | $passwords = explode(':', $row->password); |
|---|
| 377 | if (!is_array($passwords)) { |
|---|
| 378 | return null; |
|---|
| 379 | } |
|---|
| 380 | $salt = $passwords[1]; |
|---|
| 381 | |
|---|
| 382 | return $salt; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | private function _isEmailAddress($value) |
|---|
| 393 | { |
|---|
| 394 | $validator = new Zend_Validate_EmailAddress(); |
|---|
| 395 | if ($validator->isValid($value) === false) { |
|---|
| 396 | return false; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | return true; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | public function authenticate($value, $password) |
|---|
| 411 | { |
|---|
| 412 | if ($this->_isEmailAddress($value)) { |
|---|
| 413 | $column = 'email'; |
|---|
| 414 | } else { |
|---|
| 415 | $column = 'user_name'; |
|---|
| 416 | } |
|---|
| 417 | $authAdapter = new Zend_Auth_Adapter_DbTable($this->_dbAdapter, 'users' , $column, 'password'); |
|---|
| 418 | $authAdapter->setCredentialTreatment('? AND delete_flag = "0"') |
|---|
| 419 | ->setIdentity($value) |
|---|
| 420 | ->setCredential($password); |
|---|
| 421 | |
|---|
| 422 | $ret = $authAdapter->authenticate(); |
|---|
| 423 | if ($ret->isValid() === false) { |
|---|
| 424 | return false; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | $data = $authAdapter->getResultRowObject(null, 'password'); |
|---|
| 428 | $data->userLevel = 'owner'; |
|---|
| 429 | $auth = Zend_Auth::getInstance(); |
|---|
| 430 | $auth->getStorage()->write($data); |
|---|
| 431 | |
|---|
| 432 | return true; |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | public function getUserInfoByName($name) |
|---|
| 443 | { |
|---|
| 444 | |
|---|
| 445 | $row = $this->_dao->fetchRow($this->_dao->select() |
|---|
| 446 | ->where('user_name = ?', $name) |
|---|
| 447 | ->where('delete_flag = ?', 0)); |
|---|
| 448 | |
|---|
| 449 | if (is_null($row)) { |
|---|
| 450 | return null; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | return $row; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | public function getUserInfoById($id) |
|---|
| 464 | { |
|---|
| 465 | if (!is_numeric($id)) { |
|---|
| 466 | return null; |
|---|
| 467 | } |
|---|
| 468 | $row = $this->_dao->fetchRow($this->_dao->select() |
|---|
| 469 | ->where('id = ?', $id) |
|---|
| 470 | ->where('delete_flag = ?', 0)); |
|---|
| 471 | |
|---|
| 472 | if (is_null($row)) { |
|---|
| 473 | return null; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | return $row; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|