PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/cpommo/inc/helpers/pending.php

https://github.com/ccraig/cpoMMo
PHP | 344 lines | 236 code | 47 blank | 61 comment | 23 complexity | c355f39aa153930a3f460c0ba29f962b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Copyright (C) 2005, 2006, 2007, 2008 Brice Burgess <bhb@iceburg.net>
  4. *
  5. * This file is part of poMMo (http://www.pommo.org)
  6. *
  7. * poMMo is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2, or any later version.
  10. *
  11. * poMMo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  14. * the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with program; see the file docs/LICENSE. If not, write to the
  18. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. // TODO -> homogenize/reduce the get methods -- make more efficient!
  21. // include the pending prototype object
  22. $GLOBALS['pommo']->requireOnce($GLOBALS['pommo']->_baseDir. 'inc/classes/prototypes.php');
  23. class PommoPending {
  24. // make a pending template
  25. // accepts a pending template (assoc array)
  26. // return a pending object (array)
  27. function & make($in = array()) {
  28. $o = PommoType::pending();
  29. return PommoAPI::getParams($o, $in);
  30. }
  31. // make a pending template based off a database row (subscriber_pending schema)
  32. // accepts a pending template (assoc array)
  33. // return a pending object (array)
  34. function & makeDB(&$row) {
  35. $in = @array(
  36. 'id' => $row['pending_id'],
  37. 'subscriber_id' => $row['subscriber_id'],
  38. 'code' => $row['pending_code'],
  39. 'array' => unserialize($row['pending_array']),
  40. 'type' => $row['pending_type']);
  41. $o = PommoType::pending();
  42. return PommoAPI::getParams($o,$in);
  43. }
  44. // pending object validation
  45. // accepts a pending object (array)
  46. // has the magic behabior of serialzing the passed array (if exists)
  47. // returns true if pending object ($in) is valid, false if not
  48. function validate(&$in) {
  49. global $pommo;
  50. $logger =& $pommo->_logger;
  51. $invalid = array();
  52. if (!is_numeric($in['subscriber_id']))
  53. $invalid[] = 'subscriber_id';
  54. if (empty($in['code']))
  55. $invalid[] = 'code';
  56. if (!is_array($in['array']))
  57. $invalid[] = 'in array';
  58. switch($in['type']) {
  59. case 'add':
  60. case 'del':
  61. case 'change':
  62. case 'password':
  63. break;
  64. default:
  65. $invalid[] = 'type';
  66. }
  67. if (!empty($invalid)) {
  68. $logger->addErr("Pending Object failed validation on; ".implode(',',$invalid),1);
  69. return false;
  70. }
  71. return true;
  72. }
  73. // get a pending entry from a code
  74. // accepts a pending code (str)
  75. // returns pending object (array) or false if not found.
  76. function get($code = null){
  77. global $pommo;
  78. $dbo =& $pommo->_dbo;
  79. $o = array();
  80. $query = "
  81. SELECT *
  82. FROM ".$dbo->table['subscriber_pending']."
  83. WHERE pending_code='%s' LIMIT 1";
  84. $query = $dbo->prepare($query,array($code));
  85. while ($row = $dbo->getRows($query))
  86. $o = PommoPending::makeDB($row);
  87. return (empty($o)) ? false : $o;
  88. }
  89. // get a pending entry from a email address
  90. // only includes active && pending subscribers
  91. // accepts a pending code (str)
  92. // returns pending object (array) or false if not found.
  93. function getByEmail($email = null){
  94. global $pommo;
  95. $dbo =& $pommo->_dbo;
  96. $o = array();
  97. $query = "
  98. SELECT
  99. p.*
  100. FROM
  101. ".$dbo->table['subscriber_pending']." p,
  102. ".$dbo->table['subscribers']." s
  103. WHERE
  104. s.subscriber_id = p.subscriber_id
  105. AND s.email = '%s'
  106. AND s.status IN(1,2)
  107. LIMIT 1";
  108. $query = $dbo->prepare($query,array($email));
  109. while ($row = $dbo->getRows($query))
  110. $o = PommoPending::makeDB($row);
  111. return (empty($o)) ? false : $o;
  112. }
  113. // get a pending entry from a subscriber ID
  114. // only includes active && pending subscribers
  115. // accepts a subscriber ID (int)
  116. // returns pending object (array) or false if not found.
  117. function getBySubID($id = null){
  118. global $pommo;
  119. $dbo =& $pommo->_dbo;
  120. $o = array();
  121. $query = "
  122. SELECT *
  123. FROM ".$dbo->table['subscriber_pending']."
  124. WHERE subscriber_id=%i LIMIT 1";
  125. $query = $dbo->prepare($query,array($id));
  126. while ($row = $dbo->getRows($query))
  127. $o = PommoPending::makeDB($row);
  128. return (empty($o)) ? false : $o;
  129. }
  130. // checks to see if a subscriber ID has a pending request
  131. // accepts a subscriber ID (int)
  132. // returns true if pending exists, false if not (bool)
  133. function isPending($id = null){
  134. global $pommo;
  135. $dbo =& $pommo->_dbo;
  136. $query = "
  137. SELECT
  138. count(pending_id)
  139. FROM
  140. ".$dbo->table['subscriber_pending']."
  141. WHERE
  142. subscriber_id = %i
  143. LIMIT 1";
  144. $query = $dbo->prepare($query,array($id));
  145. return ($dbo->query($query,0) > 0) ? true : false;
  146. }
  147. // checks to see if a email has a pending request
  148. // only includes active && pending subscribers
  149. // accepts a email (str)
  150. // returns true if pending exists, false if not (bool)
  151. function & isEmailPending($email = null){
  152. global $pommo;
  153. $dbo =& $pommo->_dbo;
  154. $query = "
  155. SELECT
  156. count(p.pending_id)
  157. FROM
  158. ".$dbo->table['subscriber_pending']." p,
  159. ".$dbo->table['subscribers']." s
  160. WHERE
  161. s.subscriber_id = p.subscriber_id
  162. AND s.email = '%s'
  163. AND s.status IN(1,2)
  164. LIMIT 1";
  165. $query = $dbo->prepare($query,array($email));
  166. return ($dbo->query($query,0) > 0) ? true : false;
  167. }
  168. // adds a pending entry
  169. // accepts a subscriber object (array)
  170. // accepts a pending type (str) ['add','del','change','password']
  171. // returns the pending code (str) or FALSE if error
  172. function add(&$subscriber, $type = null) {
  173. global $pommo;
  174. $dbo =& $pommo->_dbo;
  175. $logger =& $pommo->_logger;
  176. switch ($type) {
  177. case 'add':
  178. case 'del':
  179. case 'change':
  180. case 'password':
  181. break;
  182. default:
  183. $logger->addErr('Unknown type passed to PommoPending::add');
  184. return false;
  185. }
  186. $p = array(
  187. 'subscriber_id' => $subscriber['id'],
  188. 'type' => $type,
  189. 'code' => PommoHelper::makeCode(),
  190. 'array' => ($type == 'change') ?
  191. $subscriber : array()
  192. );
  193. $pending = PommoPending::make($p);
  194. if (!PommoPending::validate($pending)) {
  195. $logger->addErr('PommoPending::add() failed validation');
  196. return false;
  197. }
  198. if(!empty($pending['array']))
  199. $pending['array'] = serialize($pending['array']);
  200. // check for pre-existing pending request
  201. if (PommoPending::isPending($pending['subscriber_id']))
  202. return false;
  203. $query = "
  204. INSERT INTO ".$dbo->table['subscriber_pending']."
  205. SET
  206. [pending_array='%S',]
  207. subscriber_id=%i,
  208. pending_type='%s',
  209. pending_code='%s'";
  210. $query = $dbo->prepare($query,array(
  211. $pending['array'],
  212. $pending['subscriber_id'],
  213. $pending['type'],
  214. $pending['code']));
  215. if (!$dbo->query($query))
  216. return false;
  217. return $pending['code'];
  218. }
  219. // removes a pending entry
  220. // accepts a pending object (array)
  221. // return success (bool)
  222. function cancel(&$in) {
  223. global $pommo;
  224. $dbo =& $pommo->_dbo;
  225. // if the user is pending to be added, remove entire subscriber.
  226. if ($in['type'] == 'add') {
  227. $pommo->requireOnce($pommo->_baseDir.'inc/helpers/subscribers.php');
  228. return PommoSubscriber::delete($in['subscriber_id']);
  229. }
  230. // else, only remove pending entry
  231. $query = "
  232. DELETE FROM ".$dbo->table['subscriber_pending']."
  233. WHERE pending_id=%i";
  234. $query = $dbo->prepare($query,array($in['id']));
  235. if (!$dbo->query($query)) {
  236. $logger->addErr('PommoPending::cancel() -> Error removing pending entry.');
  237. return false;
  238. }
  239. return true;
  240. }
  241. // performs a pending request
  242. // accepts a pending object (array)
  243. // returns success (bool)
  244. function perform(&$in) {
  245. global $pommo;
  246. $dbo =& $pommo->_dbo;
  247. $logger =& $pommo->_logger;
  248. if (!is_numeric($in['id']) || !is_numeric($in['subscriber_id'])) {
  249. $logger->addErr('PommoPending::perform() -> invalid pending object sent.');
  250. return false;
  251. }
  252. switch ($in['type']) {
  253. case 'add': // subscribe
  254. $query = "
  255. UPDATE ".$dbo->table['subscribers']."
  256. SET status=1
  257. WHERE subscriber_id=%i";
  258. $query = $dbo->prepare($query,array($in['subscriber_id']));
  259. if (!$dbo->query($query)) {
  260. $logger->addErr('PommoPending::perform() -> Error updating subscriber.');
  261. return false;
  262. }
  263. break;
  264. case 'change': // update
  265. $pommo->requireOnce($pommo->_baseDir. 'inc/helpers/subscribers.php');
  266. $subscriber =& $in['array'];
  267. if (!PommoSubscriber::update($subscriber,'REPLACE_ACTIVE')) {
  268. $logger->addErr('PommoPending::perform() -> Error updating subscriber.');
  269. return false;
  270. }
  271. break;
  272. case 'password' : // change (admin) password
  273. $pommo->requireOnce($pommo->_baseDir. 'inc/helpers/subscribers.php');
  274. $password = PommoHelper::makePassword();
  275. $config = PommoAPI::configGet(array(
  276. 'admin_username',
  277. 'admin_email'
  278. ));
  279. if(!PommoAPI::configUpdate(array('admin_password' => md5($password)),TRUE)) {
  280. $logger->addMsg('Error updating password.');
  281. return false;
  282. }
  283. $logger->addErr(sprintf(Pommo::_T('You may now %1$s login %2$s with username: %3$s and password: %4$s '), '<a href="'.$pommo->_baseUrl.'index.php">','</a>','<span style="font-size: 130%">' . $config['admin_username'] . '</span>', '<span style="font-size: 130%">' . $password . '</span>'));
  284. break;
  285. }
  286. $query = "
  287. DELETE FROM ".$dbo->table['subscriber_pending']."
  288. WHERE pending_id=%i";
  289. $query = $dbo->prepare($query,array($in['id']));
  290. if (!$dbo->query($query)) {
  291. $logger->addErr('PommoPending::perform() -> Error removing pending entry.');
  292. return false;
  293. }
  294. return true;
  295. }
  296. }
  297. ?>