service.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace MGModule\DNSManager2\models\whmcs\service;
  3. use \MGModule\DNSManager2 as main;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\IP;
  5. /**
  6. * Description of account
  7. * @Table(name=tblhosting,preventUpdate,prefixed=false)
  8. * @author Michal Czech <michael@modulesgarden.com>
  9. */
  10. class service extends main\mgLibs\models\orm{
  11. /**
  12. * @Column()
  13. * @var type
  14. */
  15. public $id;
  16. /**
  17. * @Column(name=userid,as=userid)
  18. * @var int
  19. */
  20. public $clientID = 0;
  21. public $userid;
  22. /**
  23. *
  24. * @var client
  25. */
  26. private $_client;
  27. /**
  28. * @Column(name=dedicatedip,as=dedicatedip)
  29. * @var string
  30. */
  31. public $dedicatedIP = null;
  32. /**
  33. * @Column(name=assignedips,as=assingedips)
  34. * @var array
  35. */
  36. public $IPList = array();
  37. /**
  38. *
  39. * @Column(name=server,as=serverid)
  40. * @var int
  41. */
  42. public $serverID;
  43. /**
  44. *
  45. * @var server
  46. */
  47. private $_server;
  48. /**
  49. *
  50. * @Column(name=packageid,as=pid)
  51. * @var int
  52. */
  53. public $productID;
  54. /**
  55. *
  56. * @var main\models\whmcs\product\product
  57. */
  58. private $_product;
  59. /**
  60. *
  61. * @Column()
  62. * @var string
  63. */
  64. public $domain;
  65. /**
  66. *
  67. * @Column()
  68. * @var string
  69. */
  70. public $username;
  71. /**
  72. *
  73. * @Column(as=passwordEncrypted)
  74. * @var string
  75. */
  76. public $password;
  77. /**
  78. *
  79. * @Column(name=orderid)
  80. * @var int
  81. */
  82. protected $_orderid;
  83. /**
  84. *
  85. * @Column(name=domainstatus,as=_domainstatus)
  86. * @var string
  87. */
  88. protected $_status;
  89. /**
  90. *
  91. * @Column(name=billingcycle,as=_billingcycle)
  92. * @var string
  93. */
  94. protected $_billingcycle;
  95. /**
  96. *
  97. * @var main\models\whmcs\service\customFields\repository
  98. */
  99. private $_customFields;
  100. /**
  101. *
  102. * @var main\models\whmcs\service\configOption\repository
  103. */
  104. private $_configOptions;
  105. /**
  106. * Load Account
  107. *
  108. * @author Michal Czech <michael@modulesgarden.com>
  109. * @param int $id
  110. * @param array $data
  111. * @throws main\mgLibs\exceptions\systemLoad
  112. */
  113. function __construct($id = null, $data = array()) {
  114. $this->id = $id;
  115. $this->load($data);
  116. }
  117. /**
  118. * Load Account Server
  119. * Function allows to easy overwrite server object
  120. *
  121. * @author Michal Czech <michael@modulesgarden.com>
  122. * @param int $id
  123. * @param array $data
  124. * @return main\models\whmcs\servers\server
  125. */
  126. protected function loadServer($id,$data = array()){
  127. return new main\models\whmcs\servers\server($id,$data);
  128. }
  129. /**
  130. * Load Product
  131. * Function allows to easy overwrite product object
  132. *
  133. * @author Michal Czech <michael@modulesgarden.com>
  134. * @param array $data
  135. * @return main\models\whmcs\product\product
  136. */
  137. protected function loadProduct($data = array()){
  138. return new main\models\whmcs\product\product($this->productID,$data);
  139. }
  140. /**
  141. * Load Client
  142. * Function allows to easy overwrite product object
  143. *
  144. * @author Michal Czech <michael@modulesgarden.com>
  145. * @param type $data
  146. * @return main\models\whmcs\service\client
  147. */
  148. protected function loadClient($data = array()){
  149. return new main\models\whmcs\client\client($this->clientID,$data);
  150. }
  151. /**
  152. * Get Server Connected With Service
  153. *
  154. * @author Michal Czech <michael@modulesgarden.com>
  155. * @return main\models\whmcs\servers\server
  156. */
  157. public function server(){
  158. if(empty($this->_server))
  159. {
  160. $this->_server = $this->loadServer($this->serverID);
  161. }
  162. return $this->_server;
  163. }
  164. /**
  165. * Get Client Connected with Service
  166. *
  167. * @author Michal Czech <michael@modulesgarden.com>
  168. * @return main\models\whmcs\client\client
  169. */
  170. function client(){
  171. if(empty($this->_client))
  172. {
  173. $this->_client = $this->loadClient();
  174. }
  175. return $this->_client;
  176. }
  177. function clientID(){
  178. return $this->clientID;
  179. }
  180. /**
  181. * Get Product Service
  182. *
  183. * @author Michal Czech <michael@modulesgarden.com>
  184. * @return main\models\whmcs\product\product
  185. */
  186. public function product(){
  187. if(empty($this->_product))
  188. {
  189. $this->_product = $this->loadProduct();
  190. }
  191. return $this->_product;
  192. }
  193. /**
  194. * Get Custom Fields
  195. *
  196. * @author Michal Czech <michael@modulesgarden.com>
  197. * @return customFields
  198. */
  199. function customFields(){
  200. if(empty($this->_customFields))
  201. {
  202. $this->_customFields = new main\models\whmcs\service\customFields\repository($this->id);
  203. }
  204. return $this->_customFields;
  205. }
  206. /**
  207. * Get Config Options
  208. *
  209. * @author Michal Czech <michael@modulesgarden.com>
  210. * @return configOptions
  211. */
  212. function configOptions(){
  213. if(empty($this->_configOptions))
  214. {
  215. $this->_configOptions = new main\models\whmcs\service\configOptions\repository($this->id);
  216. }
  217. return $this->_configOptions;
  218. }
  219. /**
  220. * Get Merged Configs from product configuration & custom fields & confi optins
  221. *
  222. * @author Michal Czech <michael@modulesgarden.com>
  223. * @return \stdClass
  224. */
  225. function mergedConfig(){
  226. $obj = new \stdClass();
  227. foreach ($this->product()->configuration as $name => $value)
  228. {
  229. if(!empty($value))
  230. {
  231. $obj->$name = $value;
  232. }
  233. }
  234. foreach ($this->customFields()->toArray(false) as $name => $value)
  235. {
  236. if(!empty($value))
  237. {
  238. $obj->$name = $value;
  239. }
  240. }
  241. foreach ($this->configOptions()->toArray(false) as $name => $value)
  242. {
  243. if(!empty($value))
  244. {
  245. $obj->$name = $value;
  246. }
  247. }
  248. return $obj;
  249. }
  250. /**
  251. * Save Account Settings
  252. *
  253. * @author Michal Czech <michael@modulesgarden.com>
  254. */
  255. function save($cols = array()){
  256. $cols['password'] = encrypt($this->password);
  257. if(($key = array_search($this->dedicatedIP, $this->IPList)) !== false) {
  258. unset($this->IPList[$key]);
  259. }
  260. $cols['assignedips'] = implode("\n", $this->IPList);
  261. parent::save($cols);
  262. }
  263. /**
  264. * Set Object Properties
  265. *
  266. * @author Michal Czech <michael@modulesgarden.com>
  267. * @param array $data
  268. * @throws main\mgLibs\exceptions\system
  269. */
  270. function load(array $data = array()){
  271. if(empty($this->id) && !empty($data['serviceid']))
  272. {
  273. $this->id = $data['serviceid'];
  274. }
  275. if($this->id !== null && empty($data))
  276. {
  277. $data = main\mgLibs\MySQL\query::select(
  278. static::fieldDeclaration()
  279. , static::tableName()
  280. , array(
  281. 'id' => $this->id
  282. )
  283. )->fetch();
  284. if(empty($data))
  285. {
  286. throw new main\mgLibs\exceptions\system('Unable to find Item with ID:'.$this->id);
  287. }
  288. }
  289. if(isset($data['passwordEncrypted']))
  290. {
  291. $data['password'] = decrypt($data['passwordEncrypted']);
  292. }
  293. if(!empty($data['dedicatedip']))
  294. {
  295. $ipo = new IP(trim($data['dedicatedip']));
  296. $this->dedicatedIP = $this->IPList[] = (string) $ipo;
  297. }
  298. if(!empty($data['assingedips']))
  299. {
  300. foreach(explode("\n",$data['assingedips']) as $ip)
  301. {
  302. if($ip) {
  303. $ipo = new IP(trim($ip));
  304. $ip = (string) $ipo;
  305. if(!in_array($ip, $this->IPList))
  306. {
  307. $this->IPList[] = trim($ip);
  308. }
  309. }
  310. }
  311. }
  312. if(!empty($data['_domainstatus']))
  313. {
  314. $this->_status = $data['_domainstatus'];
  315. $this->_billingcycle = $data['_billingcycle'];
  316. }
  317. if(!empty($data['userid']))
  318. {
  319. $this->userid = $data['userid'];
  320. $this->clientID = $data['userid'];
  321. $this->serverID = $data['serverid'];
  322. $this->domain = $data['domain'];
  323. $this->productID = $data['pid'];
  324. $this->username = $data['username'];
  325. $this->password = $data['password'];
  326. }
  327. if(!empty($data['server']))
  328. {
  329. $this->_server = $this->loadServer($data['serverid'], array(
  330. 'hostname' => $data['serverhostname']
  331. ,'username' => $data['serverusername']
  332. ,'password' => $data['serverpassword']
  333. ,'accesshash' => $data['serveraccesshash']
  334. ,'secure' => $data['serversecure']
  335. ,'ip' => $data['serverip']
  336. ));
  337. }
  338. if(!empty($data['customfields']))
  339. {
  340. $this->_customFields = new main\models\whmcs\service\customFields\repository($this->id,$data['customfields']);
  341. }
  342. if(!empty($data['configoptions']))
  343. {
  344. $this->_configOptions = new configOptions\repository($this->id);
  345. }
  346. if(!empty($data['_orderid']))
  347. {
  348. $this->_orderid = $data['_orderid'];
  349. }
  350. }
  351. function billingcycle(){
  352. if(empty($this->_billingcycle)){
  353. $this->load();
  354. }
  355. return $this->_billingcycle;
  356. }
  357. function getBillingCycleNumMonth(){
  358. switch ($this->billingcycle())
  359. {
  360. case 'Monthly':
  361. return 1;
  362. case 'Quarterly':
  363. return 3;
  364. case 'Semi-Annually':
  365. return 6;
  366. case 'Annually':
  367. return 12;
  368. case 'Biennially':
  369. return 24;
  370. case 'Triennially':
  371. return 36;
  372. }
  373. return 0;
  374. }
  375. public function status(){
  376. if(empty($this->_status)){
  377. $this->load();
  378. }
  379. return $this->_status;
  380. }
  381. function orderId(){
  382. if(empty($this->_orderid)){
  383. $this->load();
  384. }
  385. return $this->_orderid;
  386. }
  387. public function getIPs() {
  388. return $this->IPList;
  389. }
  390. }