weave_storage.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is Weave Basic Object Server
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Mozilla Labs.
  19. # Portions created by the Initial Developer are Copyright (C) 2008
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Toby Elliott (telliott@mozilla.com)
  24. # balu
  25. # Daniel Triendl <daniel@pew.cc>
  26. # Mark Straver <moonchild@palemoon.org>
  27. #
  28. # Alternatively, the contents of this file may be used under the terms of
  29. # either the GNU General Public License Version 2 or later (the "GPL"), or
  30. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. # in which case the provisions of the GPL or the LGPL are applicable instead
  32. # of those above. If you wish to allow use of your version of this file only
  33. # under the terms of either the GPL or the LGPL, and not to allow others to
  34. # use your version of this file under the terms of the MPL, indicate your
  35. # decision by deleting the provisions above and replace them with the notice
  36. # and other provisions required by the GPL or the LGPL. If you do not delete
  37. # the provisions above, a recipient may use your version of this file under
  38. # the terms of any one of the MPL, the GPL or the LGPL.
  39. #
  40. # ***** END LICENSE BLOCK *****
  41. require_once 'weave_basic_object.php';
  42. require_once 'weave_utils.php';
  43. require_once 'settings.php';
  44. class WeaveStorage
  45. {
  46. private $_username;
  47. private $_dbh;
  48. function __construct($username)
  49. {
  50. $this->_username = $username;
  51. log_error("Initalizing DB connecion!");
  52. try
  53. {
  54. if ( ! MYSQL_ENABLE )
  55. {
  56. $path = explode('/', $_SERVER['SCRIPT_FILENAME']);
  57. $db_name = SQLITE_FILE;
  58. array_pop($path);
  59. array_push($path, $db_name);
  60. $db_name = implode('/', $path);
  61. if ( ! file_exists($db_name) )
  62. {
  63. log_error("The required sqllite database is not present! $db_name");
  64. }
  65. log_error("Starting SQLite connection");
  66. $this->_dbh = new PDO('sqlite:' . $db_name);
  67. $this->_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  68. }
  69. else if ( MYSQL_ENABLE )
  70. {
  71. log_error("Starting MySQL connection");
  72. $this->_dbh = new PDO("mysql:host=". MYSQL_HOST .";dbname=". MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD);
  73. }
  74. }
  75. catch( PDOException $exception )
  76. {
  77. log_error("database unavailable " . $exception->getMessage());
  78. throw new Exception("Database unavailable " . $exception->getMessage() , 503);
  79. }
  80. }
  81. function get_connection()
  82. {
  83. return $this->_dbh;
  84. }
  85. function begin_transaction()
  86. {
  87. try
  88. {
  89. $this->_dbh->beginTransaction();
  90. }
  91. catch( PDOException $exception )
  92. {
  93. error_log("begin_transaction: " . $exception->getMessage());
  94. throw new Exception("Database unavailable", 503);
  95. }
  96. return 1;
  97. }
  98. function commit_transaction()
  99. {
  100. $this->_dbh->commit();
  101. return 1;
  102. }
  103. function get_max_timestamp($collection)
  104. {
  105. if (!$collection)
  106. {
  107. return 0;
  108. }
  109. try
  110. {
  111. $select_stmt = 'select max(modified) from wbo where username = :username and collection = :collection';
  112. $sth = $this->_dbh->prepare($select_stmt);
  113. $sth->bindParam(':username', $this->_username);
  114. $sth->bindParam(':collection', $collection);
  115. $sth->execute();
  116. }
  117. catch( PDOException $exception )
  118. {
  119. error_log("get_max_timestamp: " . $exception->getMessage());
  120. throw new Exception("Database unavailable", 503);
  121. }
  122. $result = $sth->fetchColumn();
  123. return round((float)$result, 2);
  124. }
  125. function get_collection_list()
  126. {
  127. try
  128. {
  129. $select_stmt = 'select distinct(collection) from wbo where username = :username';
  130. $sth = $this->_dbh->prepare($select_stmt);
  131. $sth->bindParam(':username', $this->_username);
  132. $sth->execute();
  133. }
  134. catch( PDOException $exception )
  135. {
  136. error_log("get_collection_list: " . $exception->getMessage());
  137. throw new Exception("Database unavailable", 503);
  138. }
  139. $collections = array();
  140. while ($result = $sth->fetchColumn())
  141. {
  142. $collections[] = $result;
  143. }
  144. return $collections;
  145. }
  146. function get_collection_list_with_timestamps()
  147. {
  148. try
  149. {
  150. $select_stmt = 'select collection, max(modified) as timestamp from wbo where username = :username group by collection';
  151. $sth = $this->_dbh->prepare($select_stmt);
  152. $sth->bindParam(':username', $this->_username);
  153. $sth->execute();
  154. }
  155. catch( PDOException $exception )
  156. {
  157. error_log("get_collection_list: " . $exception->getMessage());
  158. throw new Exception("Database unavailable", 503);
  159. }
  160. $collections = array();
  161. while ($result = $sth->fetch(PDO::FETCH_NUM))
  162. {
  163. $collections[$result[0]] = (float)$result[1];
  164. }
  165. return $collections;
  166. }
  167. function get_collection_list_with_counts()
  168. {
  169. try
  170. {
  171. $select_stmt = 'select collection, count(*) as ct from wbo where username = :username group by collection';
  172. $sth = $this->_dbh->prepare($select_stmt);
  173. $sth->bindParam(':username', $this->_username);
  174. $sth->execute();
  175. }
  176. catch( PDOException $exception )
  177. {
  178. error_log("get_collection_list_with_counts: " . $exception->getMessage());
  179. throw new Exception("Database unavailable", 503);
  180. }
  181. $collections = array();
  182. while ($result = $sth->fetch(PDO::FETCH_NUM))
  183. {
  184. $collections[$result[0]] = (int)$result[1];
  185. }
  186. return $collections;
  187. }
  188. function store_object(&$wbo)
  189. {
  190. try
  191. {
  192. if ( MYSQL_ENABLE )
  193. {
  194. $insert_stmt = 'insert into wbo (username, id, collection, parentid, predecessorid, sortindex, modified, payload, payload_size)
  195. values (:username, :id, :collection, :parentid, :predecessorid, :sortindex, :modified, :payload, :payload_size)
  196. on duplicate key update
  197. username=values(username), id=values(id), collection=values(collection), parentid=values(parentid),
  198. predecessorid=values(predecessorid), sortindex=values(sortindex), modified=values(modified), payload=values(payload),
  199. payload_size=values(payload_size)';
  200. }
  201. else
  202. {
  203. $insert_stmt = 'replace into wbo (username, id, collection, parentid, predecessorid, sortindex, modified, payload, payload_size)
  204. values (:username, :id, :collection, :parentid, :predecessorid, :sortindex, :modified, :payload, :payload_size)';
  205. }
  206. $sth = $this->_dbh->prepare($insert_stmt);
  207. $username = $this->_username;
  208. $id = $wbo->id();
  209. $collection = $wbo->collection();
  210. $parentid = $wbo->parentid();
  211. $predecessorid = $wbo->predecessorid();
  212. $sortindex = $wbo->sortindex();
  213. $modified = $wbo->modified();
  214. $payload = $wbo->payload();
  215. $payload_size = $wbo->payload_size();
  216. $sth->bindParam(':username', $username);
  217. $sth->bindParam(':id', $id);
  218. $sth->bindParam(':collection', $collection);
  219. $sth->bindParam(':parentid', $parentid);
  220. $sth->bindParam(':predecessorid', $predecessorid);
  221. $sth->bindParam(':sortindex', $sortindex);
  222. $sth->bindParam(':modified', $modified);
  223. $sth->bindParam(':payload', $payload);
  224. $sth->bindParam(':payload_size', $payload_size);
  225. $sth->execute();
  226. }
  227. catch( PDOException $exception )
  228. {
  229. error_log("store_object: " . $exception->getMessage());
  230. throw new Exception("Database unavailable", 503);
  231. }
  232. return 1;
  233. }
  234. function update_object(&$wbo)
  235. {
  236. $update = "update wbo set ";
  237. $params = array();
  238. $update_list = array();
  239. #make sure we have an id and collection. No point in continuing otherwise
  240. if (!$wbo->id() || !$wbo->collection())
  241. {
  242. error_log('Trying to update without a valid id or collection!');
  243. return 0;
  244. }
  245. if ($wbo->parentid_exists())
  246. {
  247. $update_list[] = "parentid = ?";
  248. $params[] = $wbo->parentid();
  249. }
  250. if ($wbo->predecessorid_exists())
  251. {
  252. $update_list[] = "predecessorid = ?";
  253. $params[] = $wbo->predecessorid();
  254. }
  255. if ($wbo->sortindex_exists())
  256. {
  257. $update_list[] = "sortindex = ?";
  258. $params[] = $wbo->sortindex();
  259. }
  260. if ($wbo->payload_exists())
  261. {
  262. $update_list[] = "payload = ?";
  263. $update_list[] = "payload_size = ?";
  264. $params[] = $wbo->payload();
  265. $params[] = $wbo->payload_size();
  266. }
  267. # Don't modify the timestamp on a non-payload/non-parent change change
  268. if ($wbo->parentid_exists() || $wbo->payload_exists())
  269. {
  270. #better make sure we have a modified date. Should have been handled earlier
  271. if (!$wbo->modified_exists())
  272. {
  273. error_log("Called update_object with no defined timestamp. Please check");
  274. $wbo->modified(microtime(1));
  275. }
  276. $update_list[] = "modified = ?";
  277. $params[] = $wbo->modified();
  278. }
  279. if (count($params) == 0)
  280. {
  281. return 0;
  282. }
  283. $update .= join($update_list, ",");
  284. $update .= " where username = ? and collection = ? and id = ?";
  285. $params[] = $this->_username;
  286. $params[] = $wbo->collection();
  287. $params[] = $wbo->id();
  288. try
  289. {
  290. $sth = $this->_dbh->prepare($update);
  291. $sth->execute($params);
  292. }
  293. catch( PDOException $exception )
  294. {
  295. error_log("update_object: " . $exception->getMessage());
  296. throw new Exception("Database unavailable", 503);
  297. }
  298. return 1;
  299. }
  300. function delete_object($collection, $id)
  301. {
  302. try
  303. {
  304. $delete_stmt = 'delete from wbo where username = :username and collection = :collection and id = :id';
  305. $sth = $this->_dbh->prepare($delete_stmt);
  306. $username = $this->_username;
  307. $sth->bindParam(':username', $username);
  308. $sth->bindParam(':collection', $collection);
  309. $sth->bindParam(':id', $id);
  310. $sth->execute();
  311. }
  312. catch( PDOException $exception )
  313. {
  314. error_log("delete_object: " . $exception->getMessage());
  315. throw new Exception("Database unavailable", 503);
  316. }
  317. return 1;
  318. }
  319. function delete_objects($collection, $id = null, $parentid = null, $predecessorid = null, $newer = null,
  320. $older = null, $sort = null, $limit = null, $offset = null, $ids = null,
  321. $index_above = null, $index_below = null)
  322. {
  323. $params = array();
  324. $select_stmt = '';
  325. if ($limit || $offset || $sort)
  326. {
  327. #sqlite can't do sort or limit deletes without special compiled versions
  328. #so, we need to grab the set, then delete it manually.
  329. $params = $this->retrieve_objects($collection, $id, 0, 0, $parentid, $predecessorid, $newer, $older, $sort, $limit, $offset, $ids, $index_above, $index_below);
  330. if (!count($params))
  331. {
  332. return 1; #nothing to delete
  333. }
  334. $paramqs = array();
  335. $select_stmt = "delete from wbo where username = ? and collection = ? and id in (" . join(", ", array_pad($paramqs, count($params), '?')) . ")";
  336. array_unshift($params, $collection);
  337. array_unshift($params, $username);
  338. }
  339. else
  340. {
  341. $select_stmt = "delete from wbo where username = ? and collection = ?";
  342. $params[] = $this->_username;
  343. $params[] = $collection;
  344. if ($id)
  345. {
  346. $select_stmt .= " and id = ?";
  347. $params[] = $id;
  348. }
  349. if ($ids && count($ids) > 0)
  350. {
  351. $qmarks = array();
  352. $select_stmt .= " and id in (";
  353. foreach ($ids as $temp)
  354. {
  355. $params[] = $temp;
  356. $qmarks[] = '?';
  357. }
  358. $select_stmt .= implode(",", $qmarks);
  359. $select_stmt .= ')';
  360. }
  361. if ($parentid)
  362. {
  363. $select_stmt .= " and parentid = ?";
  364. $params[] = $parentid;
  365. }
  366. if ($predecessorid)
  367. {
  368. $select_stmt .= " and predecessorid = ?";
  369. $params[] = $parentid;
  370. }
  371. if ($index_above)
  372. {
  373. $select_stmt .= " and sortindex > ?";
  374. $params[] = $parentid;
  375. }
  376. if ($index_below)
  377. {
  378. $select_stmt .= " and sortindex < ?";
  379. $params[] = $parentid;
  380. }
  381. if ($newer)
  382. {
  383. $select_stmt .= " and modified > ?";
  384. $params[] = $newer;
  385. }
  386. if ($older)
  387. {
  388. $select_stmt .= " and modified < ?";
  389. $params[] = $older;
  390. }
  391. if ($sort == 'index')
  392. {
  393. $select_stmt .= " order by sortindex desc";
  394. }
  395. else if ($sort == 'newest')
  396. {
  397. $select_stmt .= " order by modified desc";
  398. }
  399. else if ($sort == 'oldest')
  400. {
  401. $select_stmt .= " order by modified";
  402. }
  403. }
  404. try
  405. {
  406. $sth = $this->_dbh->prepare($select_stmt);
  407. $sth->execute($params);
  408. }
  409. catch( PDOException $exception )
  410. {
  411. error_log("delete_objects: " . $exception->getMessage());
  412. throw new Exception("Database unavailable", 503);
  413. }
  414. return 1;
  415. }
  416. function retrieve_object($collection, $id)
  417. {
  418. try
  419. {
  420. $select_stmt = 'select * from wbo where username = :username and collection = :collection and id = :id';
  421. $sth = $this->_dbh->prepare($select_stmt);
  422. $username = $this->_username;
  423. $sth->bindParam(':username', $username);
  424. $sth->bindParam(':collection', $collection);
  425. $sth->bindParam(':id', $id);
  426. $sth->execute();
  427. }
  428. catch( PDOException $exception )
  429. {
  430. error_log("retrieve_object: " . $exception->getMessage());
  431. throw new Exception("Database unavailable", 503);
  432. }
  433. $result = $sth->fetch(PDO::FETCH_ASSOC);
  434. $wbo = new wbo();
  435. $wbo->populate($result);
  436. return $wbo;
  437. }
  438. function retrieve_objects($collection, $id = null, $full = null, $direct_output = null, $parentid = null,
  439. $predecessorid = null, $newer = null, $older = null, $sort = null,
  440. $limit = null, $offset = null, $ids = null,
  441. $index_above = null, $index_below = null)
  442. {
  443. $full_list = $full ? '*' : 'id';
  444. $select_stmt = "select $full_list from wbo where username = ? and collection = ?";
  445. $params[] = $this->_username;
  446. $params[] = $collection;
  447. if ($id)
  448. {
  449. $select_stmt .= " and id = ?";
  450. $params[] = $id;
  451. }
  452. if ($ids && count($ids) > 0)
  453. {
  454. $qmarks = array();
  455. $select_stmt .= " and id in (";
  456. foreach ($ids as $temp)
  457. {
  458. $params[] = $temp;
  459. $qmarks[] = '?';
  460. }
  461. $select_stmt .= implode(",", $qmarks);
  462. $select_stmt .= ')';
  463. }
  464. if ($parentid)
  465. {
  466. $select_stmt .= " and parentid = ?";
  467. $params[] = $parentid;
  468. }
  469. if ($predecessorid)
  470. {
  471. $select_stmt .= " and predecessorid = ?";
  472. $params[] = $predecessorid;
  473. }
  474. if ($index_above)
  475. {
  476. $select_stmt .= " and sortindex > ?";
  477. $params[] = $parentid;
  478. }
  479. if ($index_below)
  480. {
  481. $select_stmt .= " and sortindex < ?";
  482. $params[] = $parentid;
  483. }
  484. if ($newer)
  485. {
  486. $select_stmt .= " and modified > ?";
  487. $params[] = $newer;
  488. }
  489. if ($older)
  490. {
  491. $select_stmt .= " and modified < ?";
  492. $params[] = $older;
  493. }
  494. if ($sort == 'index')
  495. {
  496. $select_stmt .= " order by sortindex desc";
  497. }
  498. else if ($sort == 'newest')
  499. {
  500. $select_stmt .= " order by modified desc";
  501. }
  502. else if ($sort == 'oldest')
  503. {
  504. $select_stmt .= " order by modified";
  505. }
  506. if ($limit)
  507. {
  508. $select_stmt .= " limit " . intval($limit);
  509. if ($offset)
  510. {
  511. $select_stmt .= " offset " . intval($offset);
  512. }
  513. }
  514. try
  515. {
  516. $sth = $this->_dbh->prepare($select_stmt);
  517. $sth->execute($params);
  518. }
  519. catch( PDOException $exception )
  520. {
  521. error_log("retrieve_collection: " . $exception->getMessage());
  522. throw new Exception("Database unavailable", 503);
  523. }
  524. if ($direct_output)
  525. return $direct_output->output($sth);
  526. $ids = array();
  527. while ($result = $sth->fetch(PDO::FETCH_ASSOC))
  528. {
  529. if ($full)
  530. {
  531. $wbo = new wbo();
  532. $wbo->populate($result);
  533. $ids[] = $wbo;
  534. }
  535. else
  536. $ids[] = $result{'id'};
  537. }
  538. return $ids;
  539. }
  540. function get_storage_total()
  541. {
  542. $username = $this->_username;
  543. $time = time();
  544. try
  545. {
  546. $select_stmt = 'select quota_usage, usage_time from users where username = :username';
  547. $sth = $this->_dbh->prepare($select_stmt);
  548. $sth->bindParam(':username', $username);
  549. $sth->execute();
  550. }
  551. catch( PDOException $exception )
  552. {
  553. error_log("get_storage_total (user field): " . $exception->getMessage());
  554. throw new Exception("Database unavailable", 503);
  555. }
  556. $result = $sth->fetch(PDO::FETCH_ASSOC);
  557. if ($result['quota_usage'] != NULL &&
  558. $result['usage_time'] != NULL &&
  559. ((int)$result['quota_usage'] != 0) &&
  560. ($time - (int)$result['usage_time'] < QUOTA_TTL) &&
  561. ((int)$result['quota_usage'] < MINQUOTA)) {
  562. # We have a usage size and it's recent enough and not close to the limit; use cached value
  563. return (int)$result['quota_usage'];
  564. }
  565. else
  566. {
  567. # We don't have a current cached value. Retrieve and store.
  568. try
  569. {
  570. $select_stmt = 'select round(sum(length(payload))/1024) from wbo where username = :username';
  571. $sth = $this->_dbh->prepare($select_stmt);
  572. $sth->bindParam(':username', $username);
  573. $sth->execute();
  574. }
  575. catch( PDOException $exception )
  576. {
  577. error_log("get_storage_total: " . $exception->getMessage());
  578. throw new Exception("Database unavailable", 503);
  579. }
  580. $usage = (int)$sth->fetchColumn();
  581. try
  582. {
  583. $update_stmt = 'update users set quota_usage = :usage, usage_time = :usage_time where username = :username';
  584. $sth = $this->_dbh->prepare($update_stmt);
  585. $sth->bindParam(':username', $username);
  586. $sth->bindParam(':usage', $usage);
  587. $sth->bindParam(':usage_time', $time);
  588. // error_log("Store query: update users set quota_usage = ".$usage.", usage_time = ".$time." where username = ".$username);
  589. $sth->execute();
  590. }
  591. catch( PDOException $exception )
  592. {
  593. error_log("get_storage_total (store): " . $exception->getMessage());
  594. throw new Exception("Database unavailable", 503);
  595. }
  596. return $usage;
  597. }
  598. }
  599. function get_collection_storage_totals()
  600. {
  601. try
  602. {
  603. $select_stmt = 'select collection, sum(payload_size) from wbo where username = :username group by collection';
  604. $sth = $this->_dbh->prepare($select_stmt);
  605. $username = $this->_username;
  606. $sth->bindParam(':username', $username);
  607. $sth->execute();
  608. }
  609. catch( PDOException $exception )
  610. {
  611. error_log("get_storage_total (" . $this->connection_details_string() . "): " . $exception->getMessage());
  612. throw new Exception("Database unavailable", 503);
  613. }
  614. $results = $sth->fetchAll(PDO::FETCH_NUM);
  615. $sth->closeCursor();
  616. $collections = array();
  617. foreach ($results as $result)
  618. {
  619. $collections[$result[0]] = (int)$result[1];
  620. }
  621. return $collections;
  622. }
  623. function get_user_quota()
  624. {
  625. return null;
  626. }
  627. function delete_storage($username)
  628. {
  629. log_error("delete storage");
  630. if (!$username)
  631. {
  632. throw new Exception("3", 404);
  633. }
  634. try
  635. {
  636. $delete_stmt = 'delete from wbo where username = :username';
  637. $sth = $this->_dbh->prepare($delete_stmt);
  638. $sth->bindParam(':username', $username);
  639. $sth->execute();
  640. $sth->closeCursor();
  641. }
  642. catch( PDOException $exception )
  643. {
  644. error_log("delete_user: " . $exception->getMessage());
  645. return 0;
  646. }
  647. return 1;
  648. }
  649. function delete_user($username)
  650. {
  651. log_error("delete User");
  652. if (!$username)
  653. {
  654. throw new Exception("3", 404);
  655. }
  656. try
  657. {
  658. $delete_stmt = 'delete from users where username = :username';
  659. $sth = $this->_dbh->prepare($delete_stmt);
  660. $sth->bindParam(':username', $username);
  661. $sth->execute();
  662. $sth->closeCursor();
  663. $delete_wbo_stmt = 'delete from wbo where username = :username';
  664. $sth = $this->_dbh->prepare($delete_wbo_stmt);
  665. $sth->bindParam(':username', $username);
  666. $sth->execute();
  667. }
  668. catch( PDOException $exception )
  669. {
  670. error_log("delete_user: " . $exception->getMessage());
  671. return 0;
  672. }
  673. return 1;
  674. }
  675. function store_user_login($username)
  676. {
  677. try
  678. {
  679. $update_statement = "update users set login = :logintime where username = :username";
  680. $time = time();
  681. $sth = $this->_dbh->prepare($update_statement);
  682. $sth->bindParam(':username', $username);
  683. $sth->bindParam(':logintime', $time);
  684. $sth->execute();
  685. }
  686. catch( PDOException $exception )
  687. {
  688. log_error("update login:" . $exception->getMessage());
  689. return 0;
  690. }
  691. return 1;
  692. }
  693. function clear_quota_usage($username)
  694. {
  695. try
  696. {
  697. $update_statement = "update users set quota_usage = 0 where username = :username";
  698. $sth = $this->_dbh->prepare($update_statement);
  699. $sth->bindParam(':username', $username);
  700. $sth->execute();
  701. }
  702. catch( PDOException $exception )
  703. {
  704. log_error("clear quota usage:" . $exception->getMessage());
  705. return 0;
  706. }
  707. return 1;
  708. }
  709. function create_user($username, $password)
  710. {
  711. log_error("Create User - Username: ".$username."|".$password);
  712. try
  713. {
  714. $create_statement = "insert into users (username, md5, login, quota_usage, usage_time)
  715. values (:username, :md5, null, 0, 0)";
  716. $sth = $this->_dbh->prepare($create_statement);
  717. $hash = WeaveHashFactory::factory();
  718. $password = $hash->hash($password);
  719. $sth->bindParam(':username', $username);
  720. $sth->bindParam(':md5', $password);
  721. $sth->execute();
  722. }
  723. catch( PDOException $exception )
  724. {
  725. log_error("create_user:" . $exception->getMessage());
  726. error_log("create_user:" . $exception->getMessage());
  727. return 0;
  728. }
  729. return 1;
  730. }
  731. function change_password($hash)
  732. {
  733. try
  734. {
  735. $update_statement = "update users set md5 = :md5 where username = :username";
  736. $sth = $this->_dbh->prepare($update_statement);
  737. $sth->bindParam(':username', $this->_username);
  738. $sth->bindParam(':md5', $hash);
  739. $sth->execute();
  740. }
  741. catch( PDOException $exception )
  742. {
  743. log_error("change_password:" . $exception->getMessage());
  744. return 0;
  745. }
  746. return 1;
  747. }
  748. #function checks if user exists
  749. function exists_user()
  750. {
  751. try
  752. {
  753. $select_stmt = 'select username from users where username = :username';
  754. $sth = $this->_dbh->prepare($select_stmt);
  755. $username = $this->_username;
  756. $sth->bindParam(':username', $username);
  757. $sth->execute();
  758. }
  759. catch( PDOException $exception )
  760. {
  761. error_log("exists_user: " . $exception->getMessage());
  762. throw new Exception("Database unavailable", 503);
  763. }
  764. if (!$result = $sth->fetch(PDO::FETCH_ASSOC))
  765. {
  766. return null;
  767. }
  768. return 1;
  769. }
  770. function get_password_hash()
  771. {
  772. log_error("auth-user: " . $this->_username);
  773. try
  774. {
  775. $select_stmt = 'select md5 from users where username = :username';
  776. $sth = $this->_dbh->prepare($select_stmt);
  777. $username = $this->_username;
  778. $sth->bindParam(':username', $username);
  779. $sth->execute();
  780. }
  781. catch( PDOException $exception )
  782. {
  783. error_log("get_password_hash: " . $exception->getMessage());
  784. throw new Exception("Database unavailable", 503);
  785. }
  786. $result = $sth->fetchColumn();
  787. if ($result === FALSE) $result = "";
  788. return $result;
  789. }
  790. }
  791. ?>