PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/work/plugins/vmpayment/klarna/klarna/api/pclasses/mysqlstorage.class.php

https://bitbucket.org/programmerlab/ourteam.co.in
PHP | 336 lines | 167 code | 22 blank | 147 comment | 15 complexity | bf3d0e982f2f79f1e8ad1dff168f88b8 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, 0BSD, MIT, LGPL-2.1
  1. <?php
  2. defined ('_JEXEC') or die();
  3. /**
  4. * MySQL Storage
  5. *
  6. * PHP Version 5.3
  7. *
  8. * @category Payment
  9. * @package KlarnaAPI
  10. * @author MS Dev <ms.modules@klarna.com>
  11. * @copyright 2012 Klarna AB (http://klarna.com)
  12. * @license http://opensource.org/licenses/BSD-2-Clause BSD-2
  13. * @link http://integration.klarna.com/
  14. */
  15. /**
  16. * Include the {@link PCStorage} interface.
  17. */
  18. require_once 'storage.intf.php';
  19. /**
  20. * MySQL storage class for KlarnaPClass
  21. *
  22. * This class is an MySQL implementation of the PCStorage interface.<br>
  23. * Config field pcURI needs to match format:
  24. * user:passwd@addr:port/dbName.dbTable<br>
  25. * Port can be omitted.<br>
  26. *
  27. * <b>Acceptable characters</b>:<br>
  28. * Username: [A-Za-z0-9_]<br>
  29. * Password: [A-Za-z0-9_]<br>
  30. * Address: [A-Za-z0-9_.]<br>
  31. * Port: [0-9]<br>
  32. * DB name: [A-Za-z0-9_]<br>
  33. * DB table: [A-Za-z0-9_]<br>
  34. *
  35. * To allow for more special characters, and to avoid having<br>
  36. * a regular expression that is too hard to understand, you can<br>
  37. * use an associative array:<br>
  38. * <code>
  39. * array(
  40. * "user" => "myuser",
  41. * "passwd" => "mypass",
  42. * "dsn" => "localhost",
  43. * "db" => "mydatabase",
  44. * "table" => "mytable"
  45. * );
  46. * </code>
  47. *
  48. * @category Payment
  49. * @package KlarnaAPI
  50. * @author MS Dev <ms.modules@klarna.com>
  51. * @copyright 2012 Klarna AB (http://klarna.com)
  52. * @license http://opensource.org/licenses/BSD-2-Clause BSD-2
  53. * @link http://integration.klarna.com/
  54. */
  55. class MySQLStorage extends PCStorage
  56. {
  57. /**
  58. * Database name.
  59. *
  60. * @var string
  61. */
  62. protected $dbName;
  63. /**
  64. * Database table.
  65. *
  66. * @var string
  67. */
  68. protected $dbTable;
  69. /**
  70. * Database address.
  71. *
  72. * @var string
  73. */
  74. protected $addr;
  75. /**
  76. * Database username.
  77. *
  78. * @var string
  79. */
  80. protected $user;
  81. /**
  82. * Database password.
  83. *
  84. * @var string
  85. */
  86. protected $passwd;
  87. /**
  88. * MySQL DB link resource.
  89. *
  90. * @var resource
  91. */
  92. protected $link;
  93. /**
  94. * return the name of the storage type
  95. *
  96. * @return string
  97. */
  98. public function getName()
  99. {
  100. return "mysql";
  101. }
  102. /**
  103. * Connects to the DB and checks if DB and table exists.
  104. *
  105. * @throws KlarnaException
  106. * @return void
  107. */
  108. protected function connect()
  109. {
  110. $this->link = mysql_connect($this->addr, $this->user, $this->passwd);
  111. if ($this->link === false) {
  112. throw new Klarna_DatabaseException(
  113. 'Failed to connect to database! ('.mysql_error().')'
  114. );
  115. }
  116. if (!mysql_query(
  117. "CREATE DATABASE IF NOT EXISTS `{$this->dbName}`",
  118. $this->link
  119. )
  120. ) {
  121. throw new Klarna_DatabaseException(
  122. 'Failed to create! ('.mysql_error().')'
  123. );
  124. }
  125. $create = mysql_query(
  126. "CREATE TABLE IF NOT EXISTS `{$this->dbName}`.`{$this->dbTable}` (
  127. `eid` int(10) unsigned NOT NULL,
  128. `id` int(10) unsigned NOT NULL,
  129. `type` tinyint(4) NOT NULL,
  130. `description` varchar(255) NOT NULL,
  131. `months` int(11) NOT NULL,
  132. `interestrate` decimal(11,2) NOT NULL,
  133. `invoicefee` decimal(11,2) NOT NULL,
  134. `startfee` decimal(11,2) NOT NULL,
  135. `minamount` decimal(11,2) NOT NULL,
  136. `country` int(11) NOT NULL,
  137. `expire` int(11) NOT NULL,
  138. KEY `id` (`id`)
  139. )", $this->link
  140. );
  141. if (!$create) {
  142. throw new Klarna_DatabaseException(
  143. 'Table not existing, failed to create! ('.mysql_error().')'
  144. );
  145. }
  146. }
  147. /**
  148. * Splits the URI in format: user:passwd@addr/dbName.dbTable<br>
  149. *
  150. * To allow for more special characters, and to avoid having<br>
  151. * a regular expression that is too hard to understand, you can<br>
  152. * use an associative array:<br>
  153. * <code>
  154. * array(
  155. * "user" => "myuser",
  156. * "passwd" => "mypass",
  157. * "dsn" => "localhost",
  158. * "db" => "mydatabase",
  159. * "table" => "mytable"
  160. * );
  161. * </code>
  162. *
  163. * @param string|array $uri Specified URI to database and table.
  164. *
  165. * @throws KlarnaException
  166. * @return void
  167. */
  168. protected function splitURI($uri)
  169. {
  170. if (is_array($uri)) {
  171. $this->user = $uri['user'];
  172. $this->passwd = $uri['passwd'];
  173. $this->addr = $uri['dsn'];
  174. $this->dbName = $uri['db'];
  175. $this->dbTable = $uri['table'];
  176. } else if (preg_match(
  177. '/^([\w-]+):([\w-]+)@([\w\.-]+|[\w\.-]+:[\d]+)\/([\w-]+).([\w-]+)$/',
  178. $uri,
  179. $arr
  180. ) === 1
  181. ) {
  182. /*
  183. [0] => user:passwd@addr/dbName.dbTable
  184. [1] => user
  185. [2] => passwd
  186. [3] => addr
  187. [4] => dbName
  188. [5] => dbTable
  189. */
  190. if (count($arr) != 6) {
  191. throw new Klarna_DatabaseException(
  192. 'URI is invalid! Missing field or invalid characters used!'
  193. );
  194. }
  195. $this->user = $arr[1];
  196. $this->passwd = $arr[2];
  197. $this->addr = $arr[3];
  198. $this->dbName = $arr[4];
  199. $this->dbTable = $arr[5];
  200. } else {
  201. throw new Klarna_DatabaseException(
  202. 'URI to MySQL is not valid! ( user:passwd@addr/dbName.dbTable )'
  203. );
  204. }
  205. }
  206. /**
  207. * Load pclasses
  208. *
  209. * @param string $uri pclass uri
  210. *
  211. * @throws KlarnaException
  212. * @return void
  213. */
  214. public function load($uri)
  215. {
  216. $this->splitURI($uri);
  217. $this->connect();
  218. $result = mysql_query(
  219. "SELECT * FROM `{$this->dbName}`.`{$this->dbTable}`",
  220. $this->link
  221. );
  222. if ($result === false) {
  223. throw new Klarna_DatabaseException(
  224. 'SELECT query failed! ('.mysql_error().')'
  225. );
  226. }
  227. while ($row = mysql_fetch_assoc($result)) {
  228. $this->addPClass(new KlarnaPClass($row));
  229. }
  230. }
  231. /**
  232. * Save pclasses to database
  233. *
  234. * @param string $uri pclass uri
  235. *
  236. * @throws KlarnaException
  237. * @return void
  238. */
  239. public function save($uri)
  240. {
  241. $this->splitURI($uri);
  242. $this->connect();
  243. if (!is_array($this->pclasses) || count($this->pclasses) == 0) {
  244. return;
  245. }
  246. foreach ($this->pclasses as $pclasses) {
  247. foreach ($pclasses as $pclass) {
  248. //Remove the pclass if it exists.
  249. mysql_query(
  250. "DELETE FROM `{$this->dbName}`.`{$this->dbTable}`
  251. WHERE `id` = '{$pclass->getId()}'
  252. AND `eid` = '{$pclass->getEid()}'"
  253. );
  254. //Insert it again.
  255. $result = mysql_query(
  256. "INSERT INTO `{$this->dbName}`.`{$this->dbTable}`
  257. (`eid`,
  258. `id`,
  259. `type`,
  260. `description`,
  261. `months`,
  262. `interestrate`,
  263. `invoicefee`,
  264. `startfee`,
  265. `minamount`,
  266. `country`,
  267. `expire`
  268. )
  269. VALUES
  270. ('{$pclass->getEid()}',
  271. '{$pclass->getId()}',
  272. '{$pclass->getType()}',
  273. '{$pclass->getDescription()}',
  274. '{$pclass->getMonths()}',
  275. '{$pclass->getInterestRate()}',
  276. '{$pclass->getInvoiceFee()}',
  277. '{$pclass->getStartFee()}',
  278. '{$pclass->getMinAmount()}',
  279. '{$pclass->getCountry()}',
  280. '{$pclass->getExpire()}')", $this->link
  281. );
  282. if ($result === false) {
  283. throw new Klarna_DatabaseException(
  284. 'INSERT INTO query failed! ('.mysql_error().')'
  285. );
  286. }
  287. }
  288. }
  289. }
  290. /**
  291. * Clear the pclasses
  292. *
  293. * @param string $uri pclass uri
  294. *
  295. * @throws KlarnaException
  296. * @return void
  297. */
  298. public function clear($uri)
  299. {
  300. try {
  301. $this->splitURI($uri);
  302. unset($this->pclasses);
  303. $this->connect();
  304. mysql_query(
  305. "DELETE FROM `{$this->dbName}`.`{$this->dbTable}`",
  306. $this->link
  307. );
  308. } catch(Exception $e) {
  309. throw new Klarna_DatabaseException(
  310. $e->getMessage(), $e->getCode()
  311. );
  312. }
  313. }
  314. }