weave_basic_object.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either the GNU General Public License Version 2 or later (the "GPL"), or
  27. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. class wbo
  39. {
  40. var $wbo_hash = array();
  41. var $_collection;
  42. var $_error = array();
  43. function extract_json(&$json)
  44. {
  45. $extracted = is_string($json) ? json_decode($json, true) : $json;
  46. #need to check the json was valid here...
  47. if ($extracted === null)
  48. {
  49. $this->_error[] = "unable to extract from json";
  50. return false;
  51. }
  52. #must have an id, or all sorts of badness happens. However, it can be added later
  53. if (array_key_exists('id', $extracted))
  54. {
  55. $this->id($extracted['id']);
  56. }
  57. if (array_key_exists('parentid', $extracted))
  58. {
  59. $this->parentid($extracted['parentid']);
  60. }
  61. if (array_key_exists('predecessorid', $extracted))
  62. {
  63. $this->predecessorid($extracted['predecessorid']);
  64. }
  65. if (array_key_exists('sortindex', $extracted))
  66. {
  67. # Due to complicated logic in the getter, we need to validate
  68. # the value space of sortindex here.
  69. if (!is_numeric($extracted['sortindex'])) {
  70. $this->_error[] = "invalid sortindex";
  71. return false;
  72. }
  73. $this->sortindex($extracted['sortindex']);
  74. }
  75. if (array_key_exists('payload', $extracted))
  76. {
  77. $this->payload($extracted['payload']);
  78. }
  79. return true;
  80. }
  81. function populate(&$datahash)
  82. {
  83. if (array_key_exists('id', $datahash))
  84. $this->id($datahash['id']);
  85. if (array_key_exists('collection', $datahash))
  86. $this->collection($datahash['collection']);
  87. if (array_key_exists('parentid', $datahash))
  88. $this->parentid($datahash['parentid']);
  89. if (array_key_exists('modified', $datahash))
  90. $this->modified($datahash['modified']);
  91. if (array_key_exists('predecessorid', $datahash))
  92. $this->predecessorid($datahash['predecessorid']);
  93. if (array_key_exists('sortindex', $datahash))
  94. $this->sortindex($datahash['sortindex']);
  95. if (array_key_exists('payload', $datahash))
  96. $this->payload($datahash['payload']);
  97. }
  98. function id($id = null)
  99. {
  100. if (!is_null($id)) { $this->wbo_hash['id'] = (string)$id; }
  101. return array_key_exists('id', $this->wbo_hash) ? $this->wbo_hash['id'] : null;
  102. }
  103. function collection($collection = null)
  104. {
  105. if (!is_null($collection)){ $this->_collection = $collection; }
  106. return $this->_collection;
  107. }
  108. function parentid($parentid = null)
  109. {
  110. if (!is_null($parentid)){ $this->wbo_hash['parentid'] = (string)$parentid; }
  111. return array_key_exists('parentid', $this->wbo_hash) ? $this->wbo_hash['parentid'] : null;
  112. }
  113. function parentid_exists()
  114. {
  115. return array_key_exists('parentid', $this->wbo_hash);
  116. }
  117. function predecessorid($predecessorid = null)
  118. {
  119. if (!is_null($predecessorid)){ $this->wbo_hash['predecessorid'] = (string)$predecessorid; }
  120. return array_key_exists('predecessorid', $this->wbo_hash) ? $this->wbo_hash['predecessorid'] : null;
  121. }
  122. function predecessorid_exists()
  123. {
  124. return array_key_exists('predecessorid', $this->wbo_hash);
  125. }
  126. function modified($modified = null)
  127. {
  128. if (!is_null($modified)){ $this->wbo_hash['modified'] = round((float)$modified, 2); }
  129. return array_key_exists('modified', $this->wbo_hash) ? $this->wbo_hash['modified'] : null;
  130. }
  131. function modified_exists()
  132. {
  133. return array_key_exists('modified', $this->wbo_hash);
  134. }
  135. function payload($payload = null)
  136. {
  137. if (!is_null($payload)){ $this->wbo_hash['payload'] = $payload; }
  138. return array_key_exists('payload', $this->wbo_hash) ? $this->wbo_hash['payload'] : null;
  139. }
  140. function payload_exists()
  141. {
  142. return array_key_exists('payload', $this->wbo_hash);
  143. }
  144. function payload_size()
  145. {
  146. return mb_strlen($this->wbo_hash['payload'], '8bit');
  147. }
  148. function sortindex($index = null)
  149. {
  150. if (!is_null($index)){
  151. $this->wbo_hash['sortindex'] = (int)($index);
  152. }
  153. return array_key_exists('sortindex', $this->wbo_hash) ? $this->wbo_hash['sortindex'] : null;
  154. }
  155. function sortindex_exists()
  156. {
  157. return array_key_exists('sortindex', $this->wbo_hash);
  158. }
  159. function validate()
  160. {
  161. if (!$this->id() || mb_strlen($this->id(), '8bit') > 64 || strpos($this->id(), '/') !== false)
  162. { $this->_error[] = "invalid id"; }
  163. if ($this->parentid_exists() && mb_strlen($this->parentid(), '8bit') > 64)
  164. { $this->_error[] = "invalid parentid"; }
  165. if ($this->predecessorid_exists() && mb_strlen($this->predecessorid(), '8bit') > 64)
  166. { $this->_error[] = "invalid predecessorid"; }
  167. if (!is_numeric($this->modified()))
  168. { $this->_error[] = "invalid modified date"; }
  169. if (!$this->modified())
  170. { $this->_error[] = "no modification date"; }
  171. if (!$this->_collection || mb_strlen($this->_collection, '8bit') > 64)
  172. { $this->_error[] = "invalid collection"; }
  173. if ($this->sortindex_exists() &&
  174. (!is_numeric($this->wbo_hash['sortindex']) ||
  175. intval($this->sortindex()) > 999999999 ||
  176. intval($this->sortindex()) < -999999999 ))
  177. { $this->_error[] = "invalid sortindex"; }
  178. if ($this->payload_exists())
  179. {
  180. if (!is_string($this->wbo_hash['payload']))
  181. { $this->_error[] = "payload needs to be json-encoded"; }
  182. }
  183. return !$this->get_error();
  184. }
  185. function get_error()
  186. {
  187. return $this->_error;
  188. }
  189. function clear_error()
  190. {
  191. $this->_error = array();
  192. }
  193. function raw_hash()
  194. {
  195. return $this->wbo_hash;
  196. }
  197. function json()
  198. {
  199. return json_encode($this->wbo_hash);
  200. }
  201. }
  202. ?>