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

/lib/Klarna/XMLrpc/MySQLStorage.php

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