1: <?php
2:
3: /**
4: * This file contains the system property collection and item class.
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @copyright four for business AG <www.4fb.de>
9: * @license http://www.contenido.org/license/LIZENZ.txt
10: * @link http://www.4fb.de
11: * @link http://www.contenido.org
12: */
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: /**
17: * User password request collection
18: *
19: * @package Core
20: * @subpackage GenericDB_Model
21: */
22: class cApiUserPasswordRequestCollection extends ItemCollection {
23: /**
24: * Constructor to create an instance of this class.
25: *
26: * @param string|bool $where [optional]
27: * The where clause in the select, usable to run select by creating
28: * the instance.
29: *
30: * @throws cDbException
31: * @throws cInvalidArgumentException
32: * @global array $cfg
33: */
34: public function __construct($where = false) {
35: global $cfg;
36: parent::__construct($cfg['tab']['user_pw_request'], 'id_pwreq');
37: $this->_setItemClass('cApiUserPasswordRequest');
38: if ($where !== false) {
39: $this->select($where);
40: }
41: }
42:
43: /**
44: * Create a user password request by user id.
45: *
46: * @param string|array $data [optional]
47: * optional parameter for direct input of primary key value
48: * (string) or multiple column name - value pairs
49: *
50: * @return cApiUserPasswordRequest
51: * @throws cDbException
52: * @throws cException
53: * @throws cInvalidArgumentException
54: */
55: public function createNewItem($data = NULL) {
56: $item = parent::createNewItem($data);
57:
58: // check configuration setting for different password expiration
59: // value must be valid string for DateTime's time variable in its constructor
60: if (false === ($expiration = getEffectiveSetting('pw_request', 'user_password_reset_expiration'))
61: || 0 === cString::getStringLength($expiration)) {
62: $expiration = '+4 hour';
63: }
64: $time = new DateTime('+' . $expiration, new DateTimeZone('UTC'));
65: $item->set('expiration', $this->escape($time->format('Y-m-d H:i:s')));
66:
67: return $item;
68: }
69:
70: /**
71: * Removes the specified entries from the database by user's id.
72: *
73: * @param int $userid
74: * Specifies the user id
75: *
76: * @return bool
77: * True if the delete was successful
78: *
79: * @throws cDbException
80: * @throws cInvalidArgumentException
81: */
82: public function deleteByUserId($userid) {
83: $result = $this->deleteBy('user_id', $userid);
84: return ($result > 0) ? true : false;
85: }
86:
87: /**
88: * Removes the specified entries from the database by token.
89: *
90: * @param $token
91: *
92: * @return bool
93: * True if the delete was successful
94: *
95: * @throws cDbException
96: * @throws cInvalidArgumentException
97: */
98: public function deleteByToken($token) {
99: $result = $this->deleteBy('validation_token', $token);
100: return ($result > 0) ? true : false;
101: }
102:
103: /**
104: * Returns all password requests available in the system
105: *
106: * @param bool $userid [optional]
107: * search for a specific user id
108: * @param string $orderBy [optional]
109: * SQL order by part
110: * @return cApiUserPasswordRequest[]
111: * @throws cDbException
112: * @throws cException
113: */
114: public function fetchAvailableRequests($userid = false, $orderBy = 'id_pwreq ASC') {
115: $requests = array();
116:
117: if (false === $userid) {
118: $this->select('', '', $this->escape($orderBy));
119: } else {
120: $this->select('user_id = \'' . $this->escape($userid) . '\'', '', $this->escape($orderBy));
121: }
122: while (($oItem = $this->next()) !== false) {
123: $requests[] = clone $oItem;
124: }
125:
126: return $requests;
127: }
128:
129: /**
130: * Returns all non expired password requests
131: *
132: * @param bool $userid [optional]
133: * search for a specific user id
134: * @return array
135: * @throws cDbException
136: * @throws cException
137: */
138: public function fetchCurrentRequests($userid = false) {
139: $requests = array();
140:
141: $now = new DateTime('now', new DateTimeZone('UTC'));
142: $this->select('expiration > \'' . $this->escape($now->format('Y-m-d H:i:s')) . '\'');
143: while (($oItem = $this->next()) !== false) {
144: if (false === $userid) {
145: $requests[] = clone $oItem;
146: } elseif ($oItem->get('user_id') === $userid) {
147: $requests[] = clone $oItem;
148: }
149: }
150:
151: return $requests;
152: }
153: }
154:
155: /**
156: * User password request item
157: *
158: * @package Core
159: * @subpackage GenericDB_Model
160: */
161: class cApiUserPasswordRequest extends Item
162: {
163: /**
164: * Constructor to create an instance of this class.
165: *
166: * @param mixed $mId [optional]
167: * Specifies the ID of item to load
168: *
169: * @throws cDbException
170: * @throws cException
171: */
172: public function __construct($mId = false) {
173: global $cfg;
174: parent::__construct($cfg['tab']['user_pw_request'], 'id_pwreq');
175: $this->setFilters(array(), array());
176: if ($mId !== false) {
177: $this->loadByPrimaryKey($mId);
178: }
179: }
180: }
181: