/ojs/ojs-2.0.1/classes/subscription/SubscriptionDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite · PHP · 474 lines · 300 code · 58 blank · 116 comment · 66 complexity · ad2dcbdce0b69ef4c70330980f58f75a MD5 · raw file

  1. <?php
  2. /**
  3. * SubscriptionDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2004 The Public Knowledge Project
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package subscription
  9. *
  10. * Class for Subscription DAO.
  11. * Operations for retrieving and modifying Subscription objects.
  12. *
  13. * $Id: SubscriptionDAO.inc.php,v 1.6 2005/05/03 17:45:25 alec Exp $
  14. */
  15. import('subscription.Subscription');
  16. import('subscription.SubscriptionType');
  17. class SubscriptionDAO extends DAO {
  18. /**
  19. * Constructor.
  20. */
  21. function SubscriptionDAO() {
  22. parent::DAO();
  23. }
  24. /**
  25. * Retrieve a subscription by subscription ID.
  26. * @param $subscriptionId int
  27. * @return Subscription
  28. */
  29. function &getSubscription($subscriptionId) {
  30. $result = &$this->retrieve(
  31. 'SELECT * FROM subscriptions WHERE subscription_id = ?', $subscriptionId
  32. );
  33. if ($result->RecordCount() == 0) {
  34. return null;
  35. } else {
  36. return $this->_returnSubscriptionFromRow($result->GetRowAssoc(false));
  37. }
  38. }
  39. /**
  40. * Retrieve subscription journal ID by subscription ID.
  41. * @param $subscriptionId int
  42. * @return int
  43. */
  44. function getSubscriptionJournalId($subscriptionId) {
  45. $result = &$this->retrieve(
  46. 'SELECT journal_id FROM subscriptions WHERE subscription_id = ?', $subscriptionId
  47. );
  48. return isset($result->fields[0]) ? $result->fields[0] : 0;
  49. }
  50. /**
  51. * Retrieve subscription ID by user ID.
  52. * @param $userId int
  53. * @param $journalId int
  54. * @return int
  55. */
  56. function getSubscriptionIdByUser($userId, $journalId) {
  57. $result = &$this->retrieve(
  58. 'SELECT subscription_id
  59. FROM subscriptions
  60. WHERE user_id = ?
  61. AND journal_id = ?',
  62. array(
  63. $userId,
  64. $journalId
  65. )
  66. );
  67. return isset($result->fields[0]) ? $result->fields[0] : 0;
  68. }
  69. /**
  70. * Check if a subscription exists for a given user and journal.
  71. * @param $userId int
  72. * @param $journalId int
  73. * @return boolean
  74. */
  75. function subscriptionExistsByUser($userId, $journalId) {
  76. $result = &$this->retrieve(
  77. 'SELECT COUNT(*)
  78. FROM subscriptions
  79. WHERE user_id = ?
  80. AND journal_id = ?',
  81. array(
  82. $userId,
  83. $journalId
  84. )
  85. );
  86. return isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
  87. }
  88. /**
  89. * Internal function to return a Subscription object from a row.
  90. * @param $row array
  91. * @return Subscription
  92. */
  93. function &_returnSubscriptionFromRow(&$row) {
  94. $subscription = &new Subscription();
  95. $subscription->setSubscriptionId($row['subscription_id']);
  96. $subscription->setJournalId($row['journal_id']);
  97. $subscription->setUserId($row['user_id']);
  98. $subscription->setTypeId($row['type_id']);
  99. $subscription->setDateStart($row['date_start']);
  100. $subscription->setDateEnd($row['date_end']);
  101. $subscription->setMembership($row['membership']);
  102. $subscription->setDomain($row['domain']);
  103. $subscription->setIPRange($row['ip_range']);
  104. return $subscription;
  105. }
  106. /**
  107. * Insert a new Subscription.
  108. * @param $subscription Subscription
  109. * @return boolean
  110. */
  111. function insertSubscription(&$subscription) {
  112. $ret = $this->update(
  113. 'INSERT INTO subscriptions
  114. (journal_id, user_id, type_id, date_start, date_end, membership, domain, ip_range)
  115. VALUES
  116. (?, ?, ?, ?, ?, ?, ?, ?)',
  117. array(
  118. $subscription->getJournalId(),
  119. $subscription->getUserId(),
  120. $subscription->getTypeId(),
  121. $subscription->getDateStart(),
  122. $subscription->getDateEnd(),
  123. $subscription->getMembership(),
  124. $subscription->getDomain(),
  125. $subscription->getIPRange()
  126. )
  127. );
  128. if ($ret) {
  129. $subscription->setSubscriptionId($this->getInsertSubscriptionId());
  130. }
  131. return $ret;
  132. }
  133. /**
  134. * Update an existing subscription.
  135. * @param $subscription Subscription
  136. * @return boolean
  137. */
  138. function updateSubscription(&$subscription) {
  139. return $this->update(
  140. 'UPDATE subscriptions
  141. SET
  142. journal_id = ?,
  143. user_id = ?,
  144. type_id = ?,
  145. date_start = ?,
  146. date_end = ?,
  147. membership = ?,
  148. domain = ?,
  149. ip_range = ?
  150. WHERE subscription_id = ?',
  151. array(
  152. $subscription->getJournalId(),
  153. $subscription->getUserId(),
  154. $subscription->getTypeId(),
  155. $subscription->getDateStart(),
  156. $subscription->getDateEnd(),
  157. $subscription->getMembership(),
  158. $subscription->getDomain(),
  159. $subscription->getIPRange(),
  160. $subscription->getSubscriptionId()
  161. )
  162. );
  163. }
  164. /**
  165. * Delete a subscription by subscription ID.
  166. * @param $subscriptionId int
  167. * @return boolean
  168. */
  169. function deleteSubscriptionById($subscriptionId) {
  170. return $this->update(
  171. 'DELETE FROM subscriptions WHERE subscription_id = ?', $subscriptionId
  172. );
  173. }
  174. /**
  175. * Delete subscriptions by journal ID.
  176. * @param $journalId int
  177. */
  178. function deleteSubscriptionsByJournal($journalId) {
  179. return $this->update(
  180. 'DELETE FROM subscriptions WHERE journal_id = ?', $journalId
  181. );
  182. }
  183. /**
  184. * Delete all subscriptions by subscription type ID.
  185. * @param $subscriptionTypeId int
  186. * @return boolean
  187. */
  188. function deleteSubscriptionByTypeId($subscriptionTypeId) {
  189. return $this->update(
  190. 'DELETE FROM subscriptions WHERE type_id = ?', $subscriptionTypeId
  191. );
  192. }
  193. /**
  194. * Retrieve an array of subscriptions matching a particular journal ID.
  195. * @param $journalId int
  196. * @return object DAOResultFactory containing matching Subscriptions
  197. */
  198. function &getSubscriptionsByJournalId($journalId, $rangeInfo = null) {
  199. $result = &$this->retrieveRange(
  200. 'SELECT * FROM subscriptions WHERE journal_id = ?', $journalId, $rangeInfo
  201. );
  202. return new DAOResultFactory(&$result, $this, '_returnSubscriptionFromRow');
  203. }
  204. /**
  205. * Check whether there is a valid subscription for a given journal.
  206. * @param $domain string
  207. * @param $IP string
  208. * @param $userId int
  209. * @param $journalId int
  210. * @return boolean
  211. */
  212. function isValidSubscription($domain, $IP, $userId, $journalId) {
  213. $valid = false;
  214. if ($domain != null) {
  215. $valid = $this->isValidSubscriptionByDomain($domain, $journalId);
  216. if ($valid) { return true; }
  217. }
  218. if ($IP != null) {
  219. $valid = $this->isValidSubscriptionByIP($IP, $journalId);
  220. if ($valid) { return true; }
  221. }
  222. if ($userId != null) {
  223. return $this->isValidSubscriptionByUser($userId, $journalId);
  224. }
  225. return false;
  226. }
  227. /**
  228. * Check whether user with ID has a valid subscription for a given journal.
  229. * @param $userId int
  230. * @param $journalId int
  231. * @return boolean
  232. */
  233. function isValidSubscriptionByUser($userId, $journalId) {
  234. $result = &$this->retrieve(
  235. 'SELECT EXTRACT(DAY FROM date_end),
  236. EXTRACT(MONTH FROM date_end),
  237. EXTRACT(YEAR FROM date_end)
  238. FROM subscriptions, subscription_types
  239. WHERE subscriptions.user_id = ?
  240. AND subscriptions.journal_id = ?
  241. AND subscriptions.type_id = subscription_types.type_id
  242. AND (subscription_types.format & ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE .' = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . ')',
  243. array(
  244. $userId,
  245. $journalId
  246. ));
  247. if ($result->RecordCount() == 0) {
  248. return false;
  249. } else {
  250. $dayEnd = $result->fields[0];
  251. $monthEnd = $result->fields[1];
  252. $yearEnd = $result->fields[2];
  253. // Ensure subscription is still valid
  254. $curDate = getdate();
  255. if ( $curDate['year'] < $yearEnd ) {
  256. return true;
  257. } elseif (( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] < $monthEnd )) {
  258. return true;
  259. } elseif ((( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] == $monthEnd )) && ( $curDate['mday'] <= $dayEnd ) ) {
  260. return true;
  261. }
  262. }
  263. // By default, not a valid subscription
  264. return false;
  265. }
  266. /**
  267. * Check whether there is a valid subscription with given domain for a journal.
  268. * @param $domain string
  269. * @param $journalId int
  270. * @return boolean
  271. */
  272. function isValidSubscriptionByDomain($domain, $journalId) {
  273. $result = &$this->retrieve(
  274. 'SELECT EXTRACT(DAY FROM date_end),
  275. EXTRACT(MONTH FROM date_end),
  276. EXTRACT(YEAR FROM date_end),
  277. POSITION(UPPER(domain) IN UPPER(?))
  278. FROM subscriptions, subscription_types
  279. WHERE POSITION(UPPER(domain) IN UPPER(?)) != 0
  280. AND subscriptions.journal_id = ?
  281. AND subscriptions.type_id = subscription_types.type_id
  282. AND subscription_types.institutional = 1
  283. AND (subscription_types.format & ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE .' = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . ')',
  284. array(
  285. $domain,
  286. $domain,
  287. $journalId
  288. ));
  289. if ($result->RecordCount() == 0) {
  290. return false;
  291. } else {
  292. while (!$result->EOF) {
  293. $dayEnd = $result->fields[0];
  294. $monthEnd = $result->fields[1];
  295. $yearEnd = $result->fields[2];
  296. $posMatch = $result->fields[3];
  297. // Ensure we have a proper match (i.e. bar.com should not match foobar.com but should match foo.bar.com)
  298. if ( $posMatch > 1) {
  299. if ( substr($domain, $posMatch-2, 1) != '.') {
  300. $result->moveNext();
  301. continue;
  302. }
  303. }
  304. // Ensure subscription is still valid
  305. $curDate = getdate();
  306. if ( $curDate['year'] < $yearEnd ) {
  307. return true;
  308. } elseif (( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] < $monthEnd )) {
  309. return true;
  310. } elseif ((( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] == $monthEnd )) && ( $curDate['mday'] <= $dayEnd ) ) {
  311. return true;
  312. }
  313. $result->moveNext();
  314. }
  315. $result->Close();
  316. }
  317. // By default, not a valid subscription
  318. return false;
  319. }
  320. /**
  321. * Check whether there is a valid subscription for the given IP for a journal.
  322. * @param $IP string
  323. * @param $journalId int
  324. * @return boolean
  325. */
  326. function isValidSubscriptionByIP($IP, $journalId) {
  327. $result = &$this->retrieve(
  328. 'SELECT EXTRACT(DAY FROM date_end),
  329. EXTRACT(MONTH FROM date_end),
  330. EXTRACT(YEAR FROM date_end),
  331. ip_range
  332. FROM subscriptions, subscription_types
  333. WHERE ip_range IS NOT NULL
  334. AND subscriptions.journal_id = ?
  335. AND subscriptions.type_id = subscription_types.type_id
  336. AND subscription_types.institutional = 1
  337. AND (subscription_types.format & ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE .' = ' . SUBSCRIPTION_TYPE_FORMAT_ONLINE . ')',
  338. $journalId
  339. );
  340. if ($result->RecordCount() == 0) {
  341. return false;
  342. } else {
  343. $matchFound = false;
  344. $IP = sprintf('%u', ip2long($IP));
  345. while (!$result->EOF) {
  346. $ipRange = $result->fields[3];
  347. // Get all IPs and IP ranges
  348. $ipRanges = explode(SUBSCRIPTION_IP_RANGE_SEPERATOR, $ipRange);
  349. // Check each IP and IP range
  350. while (list(, $curIPString) = each($ipRanges)) {
  351. // Parse and check single IP string
  352. if (strpos($curIPString, SUBSCRIPTION_IP_RANGE_RANGE) === false) {
  353. // Check for wildcards in IP
  354. if (strpos($curIPString, SUBSCRIPTION_IP_RANGE_WILDCARD) === false) {
  355. $curIPString = sprintf('%u', ip2long(trim($curIPString)));
  356. if ($curIPString == $IP) {
  357. $matchFound = true;
  358. break;
  359. }
  360. } else {
  361. // Turn wildcard IP into IP range
  362. $ipStart = sprintf('%u', ip2long(str_replace(SUBSCRIPTION_IP_RANGE_WILDCARD, '0', trim($curIPString))));
  363. $ipEnd = sprintf('%u', ip2long(str_replace(SUBSCRIPTION_IP_RANGE_WILDCARD, '255', trim($curIPString))));
  364. if ($IP >= $ipStart && $IP <= $ipEnd) {
  365. $matchFound = true;
  366. break;
  367. }
  368. }
  369. // Parse and check IP range string
  370. } else {
  371. $ipStartAndEnd = explode(SUBSCRIPTION_IP_RANGE_RANGE, $curIPString);
  372. // Replace wildcards in start and end of range
  373. $ipStart = sprintf('%u', ip2long(str_replace(SUBSCRIPTION_IP_RANGE_WILDCARD, '0', trim($ipStartAndEnd[0]))));
  374. $ipEnd = sprintf('%u', ip2long(str_replace(SUBSCRIPTION_IP_RANGE_WILDCARD, '255', trim($ipStartAndEnd[1]))));
  375. if ($IP >= $ipStart && $IP <= $ipEnd) {
  376. $matchFound = true;
  377. break;
  378. }
  379. }
  380. }
  381. if ($matchFound == true) {
  382. break;
  383. } else {
  384. $result->moveNext();
  385. }
  386. }
  387. // Found a match. Ensure subscription is still valid
  388. if ($matchFound == true) {
  389. $dayEnd = $result->fields[0];
  390. $monthEnd = $result->fields[1];
  391. $yearEnd = $result->fields[2];
  392. $result->Close();
  393. $curDate = getdate();
  394. if ( $curDate['year'] < $yearEnd ) {
  395. return true;
  396. } elseif (( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] < $monthEnd )) {
  397. return true;
  398. } elseif ((( $curDate['year'] == $yearEnd ) && ( $curDate['mon'] == $monthEnd )) && ( $curDate['mday'] <= $dayEnd ) ) {
  399. return true;
  400. }
  401. } else {
  402. $result->Close();
  403. }
  404. }
  405. // By default, not a valid subscription
  406. return false;
  407. }
  408. /**
  409. * Get the ID of the last inserted subscription.
  410. * @return int
  411. */
  412. function getInsertSubscriptionId() {
  413. return $this->getInsertId('subscriptions', 'subscription_id');
  414. }
  415. }
  416. ?>