PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/bb-modules/mod_servicegps/Service.php

https://github.com/boxbilling/extensions
PHP | 222 lines | 165 code | 34 blank | 23 comment | 18 complexity | dbd069129ae1d8c426214eabccb080e7 MD5 | raw file
  1. <?php
  2. class Box_Mod_Servicegps_Service
  3. {
  4. public function validateOrderData(&$data)
  5. {
  6. if(!isset($data['imei']) || empty($data['imei'])) {
  7. throw new Exception('IMEI is required');
  8. }
  9. }
  10. /**
  11. * @param $order
  12. * @return void
  13. */
  14. public function create($order)
  15. {
  16. $model = R::dispense('service_gps');
  17. $model->client_id = $order->client_id;
  18. $model->created_at = date('c');
  19. $model->updated_at = date('c');
  20. R::store($model);
  21. return $model;
  22. }
  23. /**
  24. * @param $order
  25. * @return void
  26. */
  27. public function activate($order, $model)
  28. {
  29. if(!is_object($model)) {
  30. throw new Box_Exception('Could not activate order. Service was not created', null, 7456);
  31. }
  32. $c = json_decode($order->config, 1);
  33. $this->validateOrderData($c);
  34. $model->imei = $c['imei'];
  35. $model->updated_at = date('c');
  36. R::store($model);
  37. return true;
  38. }
  39. /**
  40. * Suspend VPS
  41. *
  42. * @param $order
  43. * @return void
  44. */
  45. public function suspend($order, $model)
  46. {
  47. if(!is_object($model)) {
  48. throw new Box_Exception('Could not suspend order. Service was not created', null, 7456);
  49. }
  50. $model->updated_at = date('c');
  51. R::store($model);
  52. return true;
  53. }
  54. /**
  55. * @param $order
  56. * @return void
  57. */
  58. public function unsuspend($order, $model)
  59. {
  60. if(!is_object($model)) {
  61. throw new Box_Exception('Could not unsuspend order. Service was not created', null, 7456);
  62. }
  63. $model->updated_at = date('c');
  64. R::store($model);
  65. return true;
  66. }
  67. /**
  68. * @param $order
  69. * @return void
  70. */
  71. public function delete($order, $model)
  72. {
  73. if(is_object($model)) {
  74. R::trash($model);
  75. }
  76. return true;
  77. }
  78. public function toApiArray($row)
  79. {
  80. return $row->export();
  81. }
  82. public function apiDataTracker($service, $data)
  83. {
  84. $from = (isset($data['date_from']) && !empty($data['date_from'])) ? $data['date_from'] : date('Y-m-d H:i:s',strtotime('-1day'));
  85. $to = (isset($data['date_to']) && !empty($data['date_to'])) ? $data['date_to'] : date('Y-m-d H:i:s');
  86. $params = array(
  87. 'imei' => $service->imei,
  88. 'from' => $from,
  89. 'to' => $to,
  90. );
  91. error_log(print_r($params, 1));
  92. $api = $this->getApi();
  93. return $api->data_tracker($params);
  94. }
  95. public function apiLastPosition($service)
  96. {
  97. $data = array(
  98. 'imei' => $service->imei,
  99. );
  100. $api = $this->getApi();
  101. return $api->data_lastposition($data);
  102. }
  103. public function apiConnection()
  104. {
  105. $api = $this->getApi();
  106. $api->data_ping();
  107. return true;
  108. }
  109. public function getModuleConfig()
  110. {
  111. return array(
  112. 'api_username' => '',
  113. 'api_password' => '',
  114. );
  115. }
  116. public function getApi()
  117. {
  118. $config = $this->getModuleConfig();
  119. return new GpsApi($config);
  120. }
  121. public function install()
  122. {
  123. $sql="
  124. CREATE TABLE IF NOT EXISTS `service_gps` (
  125. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  126. `client_id` bigint(20) DEFAULT NULL,
  127. `imei` varchar(255) DEFAULT NULL,
  128. `created_at` varchar(35) DEFAULT NULL,
  129. `updated_at` varchar(35) DEFAULT NULL,
  130. PRIMARY KEY (`id`),
  131. UNIQUE KEY `imei` (`imei`),
  132. KEY `client_id_idx` (`client_id`)
  133. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  134. ";
  135. Box_Db::getRb();
  136. R::exec($sql);
  137. }
  138. }
  139. class GpsApi
  140. {
  141. protected $_api_url = 'http://apigps.antanas.eu/api.php';
  142. public function __construct($options)
  143. {
  144. if (!extension_loaded('curl')) {
  145. throw new Exception('cURL extension is not enabled');
  146. }
  147. if(isset($options['api_username'])) {
  148. $this->_api_username = $options['api_username'];
  149. }
  150. if(isset($options['api_password'])) {
  151. $this->_api_password = $options['api_password'];
  152. }
  153. }
  154. public function __call($method, $arguments)
  155. {
  156. $data = array();
  157. if(isset($arguments[0]) && is_array($arguments[0])) {
  158. $data = $arguments[0];
  159. }
  160. $data['api_method'] = $method;
  161. $data['api_username'] = $this->_api_username;
  162. $data['api_password'] = $this->_api_password;
  163. $ch = curl_init();
  164. curl_setopt($ch, CURLOPT_URL, $this->_api_url);
  165. curl_setopt($ch, CURLOPT_POST, true);
  166. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  168. $result = curl_exec($ch);
  169. //error_log($result);
  170. if($result === false) {
  171. $e = new Exception(sprintf('Curl Error: "%s"', curl_error($ch)));
  172. curl_close($ch);
  173. throw $e;
  174. }
  175. curl_close($ch);
  176. $json = json_decode($result, 1);
  177. if(!is_array($json)) {
  178. throw new Exception(sprintf('Invalid Response "%s"', $result));
  179. }
  180. if(isset($json['error']) && !empty($json['error'])) {
  181. throw new Exception($json['error']['message'], $json['error']['code']);
  182. }
  183. if(!isset($json['result'])) {
  184. throw new Exception(sprintf('Invalid Response "%s"', $result));
  185. }
  186. return $json['result'];
  187. }
  188. }