/gadgets/Notification/Installer.php

https://github.com/jaws-project/jaws · PHP · 288 lines · 186 code · 38 blank · 64 comment · 33 complexity · 8ef1f3047737b32df0ecadd78f17dabb MD5 · raw file

  1. <?php
  2. /**
  3. * Notification Installer
  4. *
  5. * @category GadgetModel
  6. * @package Notification
  7. */
  8. class Notification_Installer extends Jaws_Gadget_Installer
  9. {
  10. /**
  11. * Gadget Registry keys
  12. *
  13. * @var array
  14. * @access private
  15. */
  16. var $_RegKeys = array(
  17. array('webpush_enabled', false),
  18. array('webpush_pvt_key', ''),
  19. array('webpush_pub_key', ''),
  20. array('webpush_anonymouse', false),
  21. array('processing', 'false'),
  22. array('last_update', '0'),
  23. array('default_longevity', 600), // seconds(10 min)
  24. array('queue_max_time', '1800'), // maximum time to execution an queue (seconds)
  25. array('eml_fetch_limit', '100'),
  26. array('sms_fetch_limit', '100'),
  27. array('web_fetch_limit', '100'),
  28. array('configuration', ''), // array(gadget_name=>(0,1, driver_name))
  29. );
  30. /**
  31. * Default ACL value of the gadget front-end
  32. *
  33. * @var bool
  34. * @access protected
  35. */
  36. var $default_acl = true;
  37. /**
  38. * Gadget ACLs
  39. *
  40. * @var array
  41. * @access private
  42. */
  43. var $_ACLKeys = array(
  44. 'NotificationDrivers',
  45. 'Messages',
  46. 'DeleteMessage',
  47. 'Settings',
  48. );
  49. /**
  50. * Installs the gadget
  51. *
  52. * @access public
  53. * @return mixed True on successful installation, Jaws_Error otherwise
  54. */
  55. function Install()
  56. {
  57. $result = $this->installSchema('schema.xml');
  58. if (Jaws_Error::IsError($result)) {
  59. return $result;
  60. }
  61. //
  62. $keyPair = $this->Generate_ECDSA_KeyPair();
  63. // registry keys
  64. $this->gadget->registry->update('webpush_pvt_key', $keyPair['private']);
  65. $this->gadget->registry->update('webpush_pub_key', $keyPair['public']);
  66. // Add listeners
  67. $this->gadget->event->insert('Notify');
  68. return true;
  69. }
  70. /**
  71. * Uninstalls the gadget
  72. *
  73. * @access public
  74. * @return mixed True on success and Jaws_Error otherwise
  75. */
  76. function Uninstall()
  77. {
  78. $tables = array(
  79. 'notification_message', 'notification_recipient',
  80. 'notification_driver'
  81. );
  82. foreach ($tables as $table) {
  83. $result = Jaws_DB::getInstance()->dropTable($table);
  84. if (Jaws_Error::IsError($result)) {
  85. $errMsg = Jaws::t('ERROR_GADGET_NOT_UNINSTALLED', $this->gadget->title);
  86. return new Jaws_Error($errMsg);
  87. }
  88. }
  89. return true;
  90. }
  91. /**
  92. * Generate new ECDSA key pair
  93. *
  94. * @access private
  95. * @return mixed ECDSA key pair
  96. */
  97. private function Generate_ECDSA_KeyPair()
  98. {
  99. $new_key_pair = openssl_pkey_new(
  100. array(
  101. "digest_alg" => OPENSSL_ALGO_SHA256,
  102. "private_key_bits" => 2048,
  103. "private_key_type" => OPENSSL_KEYTYPE_EC,
  104. "curve_name" => "prime256v1"
  105. )
  106. );
  107. openssl_pkey_export($new_key_pair, $privateKeyPEM);
  108. $pkeyDetails = openssl_pkey_get_details($new_key_pair);
  109. return array(
  110. 'private' => $privateKeyPEM,
  111. 'public' => Jaws_JWT::base64URLEncode(chr(4) . $pkeyDetails['ec']['x'].$pkeyDetails['ec']['y']),
  112. );
  113. }
  114. /**
  115. * Upgrades the gadget
  116. *
  117. * @access public
  118. * @param string $old Current version (in registry)
  119. * @param string $new New version (in the $gadgetInfo file)
  120. * @return mixed True on success, Jaws_Error otherwise
  121. */
  122. function Upgrade($old, $new)
  123. {
  124. if (version_compare($old, '1.1.0', '<')) {
  125. $result = $this->installSchema('1.1.0.xml', array(), '1.0.0.xml');
  126. if (Jaws_Error::IsError($result)) {
  127. return $result;
  128. }
  129. // registry keys
  130. $this->gadget->registry->delete('email_pop_count');
  131. $this->gadget->registry->delete('mobile_pop_count');
  132. $this->gadget->registry->insert('eml_fetch_limit', '100');
  133. $this->gadget->registry->insert('sms_fetch_limit', '100');
  134. }
  135. if (version_compare($old, '1.2.0', '<')) {
  136. $result = $this->installSchema('1.2.0.xml', array(), '1.1.0.xml');
  137. if (Jaws_Error::IsError($result)) {
  138. return $result;
  139. }
  140. }
  141. if (version_compare($old, '1.3.0', '<')) {
  142. $result = $this->installSchema('1.3.0.xml', array(), '1.2.0.xml');
  143. if (Jaws_Error::IsError($result)) {
  144. return $result;
  145. }
  146. $this->gadget->registry->insert('wp_fetch_limit', '100');
  147. }
  148. if (version_compare($old, '1.4.0', '<')) {
  149. $result = $this->installSchema('1.4.0.xml', array(), '1.3.0.xml');
  150. if (Jaws_Error::IsError($result)) {
  151. return $result;
  152. }
  153. }
  154. if (version_compare($old, '1.5.0', '<')) {
  155. //
  156. $keyPair = $this->Generate_ECDSA_KeyPair();
  157. // registry keys
  158. $this->gadget->registry->insert('webpush_pvt_key', $keyPair['private']);
  159. $this->gadget->registry->insert('webpush_pub_key', $keyPair['public']);
  160. }
  161. if (version_compare($old, '1.6.0', '<')) {
  162. // Add listener for login user
  163. $this->gadget->event->insert('LoginUser');
  164. }
  165. if (version_compare($old, '1.7.0', '<')) {
  166. // Add listener for login user
  167. $this->gadget->event->delete('LoginUser');
  168. }
  169. if (version_compare($old, '1.8.0', '<')) {
  170. // registry keys
  171. $this->gadget->registry->insert('webpush_enabled', false);
  172. }
  173. if (version_compare($old, '2.0.0', '<')) {
  174. $result = $this->installSchema('2.0.0.xml', array(), '1.4.0.xml');
  175. if (Jaws_Error::IsError($result)) {
  176. return $result;
  177. }
  178. // registry keys
  179. $this->gadget->registry->delete('wp_fetch_limit');
  180. $this->gadget->registry->insert('web_fetch_limit', '100');
  181. $this->gadget->registry->insert('webpush_anonymouse', false);
  182. }
  183. if (version_compare($old, '2.1.0', '<')) {
  184. $result = $this->installSchema('2.1.0.xml', array(), '2.0.0.xml');
  185. if (Jaws_Error::IsError($result)) {
  186. return $result;
  187. }
  188. }
  189. if (version_compare($old, '2.2.0', '<')) {
  190. $result = $this->installSchema('2.2.0.xml', array(), '2.1.0.xml');
  191. if (Jaws_Error::IsError($result)) {
  192. return $result;
  193. }
  194. }
  195. if (version_compare($old, '2.3.0', '<')) {
  196. $result = $this->installSchema('2.3.0.xml', array(), '2.2.0.xml');
  197. if (Jaws_Error::IsError($result)) {
  198. return $result;
  199. }
  200. $dropTables = array(
  201. 'notification_messages', 'notification_email',
  202. 'notification_mobile', 'notification_webpush'
  203. );
  204. foreach ($dropTables as $table) {
  205. $result = Jaws_DB::getInstance()->dropTable($table);
  206. if (Jaws_Error::IsError($result)) {
  207. // do nothing
  208. }
  209. }
  210. }
  211. if (version_compare($old, '2.4.0', '<')) {
  212. $result = $this->installSchema('2.4.0.xml', array(), '2.3.0.xml');
  213. if (Jaws_Error::IsError($result)) {
  214. return $result;
  215. }
  216. }
  217. if (version_compare($old, '2.5.0', '<')) {
  218. $result = $this->installSchema('2.5.0.xml', array(), '2.4.0.xml');
  219. if (Jaws_Error::IsError($result)) {
  220. return $result;
  221. }
  222. // registry keys
  223. $this->gadget->registry->delete('processing');
  224. }
  225. if (version_compare($old, '2.6.0', '<')) {
  226. $result = $this->installSchema('2.6.0.xml', array(), '2.5.0.xml');
  227. if (Jaws_Error::IsError($result)) {
  228. return $result;
  229. }
  230. }
  231. if (version_compare($old, '2.7.0', '<')) {
  232. Jaws_DB::getInstance()->truncateTable('notification_message');
  233. Jaws_DB::getInstance()->truncateTable('notification_recipient');
  234. $result = $this->installSchema('2.7.0.xml', array(), '2.6.0.xml');
  235. if (Jaws_Error::IsError($result)) {
  236. return $result;
  237. }
  238. }
  239. if (version_compare($old, '2.8.0', '<')) {
  240. // registry keys
  241. $this->gadget->registry->insert('default_longevity', 600);
  242. $result = $this->installSchema('schema.xml', array(), '2.7.0.xml');
  243. if (Jaws_Error::IsError($result)) {
  244. return $result;
  245. }
  246. }
  247. //FIXME add new ACLs (Message, DeleteMessage)
  248. return true;
  249. }
  250. }