PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/shop/engine/Shopware/Plugins/Default/Frontend/PigmbhKlarnaPayment/api/pclasses/mysqlstorage.class.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 308 lines | 126 code | 23 blank | 159 comment | 14 complexity | 587ed217eea2c0158a5053d3e7b5ba8b MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Copyright 2010 KLARNA AB. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are
  6. * permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this list of
  9. * conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  12. * of conditions and the following disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY KLARNA AB "AS IS" AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KLARNA AB OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * The views and conclusions contained in the software and documentation are those of the
  26. * authors and should not be interpreted as representing official policies, either expressed
  27. * or implied, of KLARNA AB.
  28. *
  29. * @package KlarnaAPI
  30. */
  31. /**
  32. * Include the {@link PCStorage} interface.
  33. */
  34. require_once('storage.intf.php');
  35. /**
  36. * MySQL storage class for KlarnaPClass
  37. *
  38. * This class is an MySQL implementation of the PCStorage interface.<br>
  39. * Config field pcURI needs to match format: user:passwd@addr:port/dbName.dbTable<br>
  40. * Port can be omitted.<br>
  41. *
  42. * <b>Acceptable characters</b>:<br>
  43. * Username: [A-Za-z0-9_]<br>
  44. * Password: [A-Za-z0-9_]<br>
  45. * Address: [A-Za-z0-9_.]<br>
  46. * Port: [0-9]<br>
  47. * DB name: [A-Za-z0-9_]<br>
  48. * DB table: [A-Za-z0-9_]<br>
  49. *
  50. * To allow for more special characters, and to avoid having<br>
  51. * a regular expression that is too hard to understand, you can<br>
  52. * use an associative array:<br>
  53. * <code>
  54. * array(
  55. * "user" => "myuser",
  56. * "passwd" => "mypass",
  57. * "dsn" => "localhost",
  58. * "db" => "mydatabase",
  59. * "table" => "mytable"
  60. * );
  61. * </code>
  62. *
  63. * @package KlarnaAPI
  64. * @deprecated Deprecated since 2.1, better to use PDO based SQLStorage
  65. * @see SQLStorage
  66. * @version 2.1.2
  67. * @since 2011-09-13
  68. * @link http://integration.klarna.com/
  69. * @copyright Copyright (c) 2010 Klarna AB (http://klarna.com)
  70. */
  71. class MySQLStorage extends PCStorage {
  72. /**
  73. * Database name.
  74. *
  75. * @ignore Do not show in PHPDoc.
  76. * @var string
  77. */
  78. protected $dbName;
  79. /**
  80. * Database table.
  81. *
  82. * @ignore Do not show in PHPDoc.
  83. * @var string
  84. */
  85. protected $dbTable;
  86. /**
  87. * Database address.
  88. *
  89. * @ignore Do not show in PHPDoc.
  90. * @var string
  91. */
  92. protected $addr;
  93. /**
  94. * Database username.
  95. *
  96. * @ignore Do not show in PHPDoc.
  97. * @var string
  98. */
  99. protected $user;
  100. /**
  101. * Database password.
  102. *
  103. * @ignore Do not show in PHPDoc.
  104. * @var string
  105. */
  106. protected $passwd;
  107. /**
  108. * MySQL DB link resource.
  109. *
  110. * @ignore Do not show in PHPDoc.
  111. * @var resource
  112. */
  113. protected $link;
  114. /**
  115. * Class constructor
  116. * @ignore Does nothing.
  117. */
  118. public function __construct() {
  119. }
  120. /**
  121. * Class destructor
  122. * @ignore Does nothing.
  123. */
  124. public function __destruct() {
  125. }
  126. /**
  127. * Connects to the DB and checks if DB and table exists.
  128. *
  129. * @ignore Do not show in PHPDoc.
  130. * @throws Exception
  131. * @return void
  132. */
  133. protected function connect() {
  134. $this->link = mysql_connect($this->addr, $this->user, $this->passwd);
  135. if($this->link === false) {
  136. throw new Exception('Failed to connect to database! ('.mysql_error().')');
  137. }
  138. if(!mysql_query('CREATE DATABASE IF NOT EXISTS `'.$this->dbName.'`', $this->link)) {
  139. throw new Exception('Database not existing, failed to create! ('.mysql_error().')');
  140. }
  141. $create = mysql_query(
  142. "CREATE TABLE IF NOT EXISTS `".$this->dbName."`.`".$this->dbTable."` (
  143. `eid` int(10) unsigned NOT NULL,
  144. `id` int(10) unsigned NOT NULL,
  145. `type` tinyint(4) NOT NULL,
  146. `description` varchar(255) NOT NULL,
  147. `months` int(11) NOT NULL,
  148. `interestrate` decimal(11,2) NOT NULL,
  149. `invoicefee` decimal(11,2) NOT NULL,
  150. `startfee` decimal(11,2) NOT NULL,
  151. `minamount` decimal(11,2) NOT NULL,
  152. `country` int(11) NOT NULL,
  153. `expire` int(11) NOT NULL,
  154. KEY `id` (`id`)
  155. )", $this->link
  156. );
  157. if(!$create) {
  158. throw new Exception('Table not existing, failed to create! ('.mysql_error().')');
  159. }
  160. }
  161. /**
  162. * Splits the URI in format: user:passwd@addr/dbName.dbTable<br>
  163. *
  164. * To allow for more special characters, and to avoid having<br>
  165. * a regular expression that is too hard to understand, you can<br>
  166. * use an associative array:<br>
  167. * <code>
  168. * array(
  169. * "user" => "myuser",
  170. * "passwd" => "mypass",
  171. * "dsn" => "localhost",
  172. * "db" => "mydatabase",
  173. * "table" => "mytable"
  174. * );
  175. * </code>
  176. *
  177. * @ignore Do not show in PHPDoc.
  178. * @param string|array $uri Specified URI to database and table.
  179. * @throws Exception
  180. * @return void
  181. */
  182. protected function splitURI($uri) {
  183. if(is_array($uri)) {
  184. $this->user = $uri['user'];
  185. $this->passwd = $uri['passwd'];
  186. $this->addr = $uri['dsn'];
  187. $this->dbName = $uri['db'];
  188. $this->dbTable = $uri['table'];
  189. }
  190. else if(preg_match('/^([\w-]+):([\w-]+)@([\w\.-]+|[\w\.-]+:[\d]+)\/([\w-]+).([\w-]+)$/', $uri, $arr) === 1) {
  191. /*
  192. [0] => user:passwd@addr/dbName.dbTable
  193. [1] => user
  194. [2] => passwd
  195. [3] => addr
  196. [4] => dbName
  197. [5] => dbTable
  198. */
  199. if(count($arr) != 6) {
  200. throw new Exception('URI is invalid! Missing field or invalid characters used!');
  201. }
  202. $this->user = $arr[1];
  203. $this->passwd = $arr[2];
  204. $this->addr = $arr[3];
  205. $this->dbName = $arr[4];
  206. $this->dbTable = $arr[5];
  207. }
  208. else {
  209. throw new Exception('URI to MySQL is not valid! ( user:passwd@addr/dbName.dbTable )');
  210. }
  211. }
  212. /**
  213. * @see PCStorage::load()
  214. */
  215. public function load($uri) {
  216. try {
  217. $this->splitURI($uri);
  218. $this->connect();
  219. if(($result = mysql_query('SELECT * FROM `'.$this->dbName.'`.`'.$this->dbTable.'`', $this->link)) === false) {
  220. throw new Exception('SELECT query failed! ('.mysql_error().')');
  221. }
  222. while($row = mysql_fetch_assoc($result)) {
  223. $this->addPClass(new KlarnaPClass($row));
  224. }
  225. }
  226. catch(Exception $e) {
  227. throw new KlarnaException("Error in " . __METHOD__ . ": " .$e->getMessage());
  228. }
  229. }
  230. /**
  231. * @see PCStorage::save()
  232. */
  233. public function save($uri) {
  234. try {
  235. $this->splitURI($uri);
  236. $this->connect();
  237. if(!is_array($this->pclasses) || count($this->pclasses) == 0) {
  238. return;
  239. }
  240. foreach($this->pclasses as $pclasses) {
  241. foreach($pclasses as $pclass) {
  242. //Remove the pclass if it exists.
  243. mysql_query("DELETE FROM `".$this->dbName.'`.`'.$this->dbTable."` WHERE `id` = '".$pclass->getId()."' AND `eid` = '".$pclass->getEid()."'");
  244. //Insert it again.
  245. $result = mysql_query(
  246. "INSERT INTO `".$this->dbName.'`.`'.$this->dbTable."`
  247. (`eid`, `id`, `type`, `description`, `months`, `interestrate`, `invoicefee`, `startfee`, `minamount`, `country`, `expire`)
  248. VALUES
  249. ('".$pclass->getEid()."',
  250. '".$pclass->getId()."',
  251. '".$pclass->getType()."',
  252. '".$pclass->getDescription()."',
  253. '".$pclass->getMonths()."',
  254. '".$pclass->getInterestRate()."',
  255. '".$pclass->getInvoiceFee()."',
  256. '".$pclass->getStartFee()."',
  257. '".$pclass->getMinAmount()."',
  258. '".$pclass->getCountry()."',
  259. '".$pclass->getExpire()."')", $this->link
  260. );
  261. if($result === false) {
  262. throw new Exception('INSERT INTO query failed! ('.mysql_error().')');
  263. }
  264. }
  265. }
  266. }
  267. catch(Exception $e) {
  268. throw new KlarnaException("Error in " . __METHOD__ . ": " . $e->getMessage());
  269. }
  270. }
  271. /**
  272. * @see PCStorage::clear()
  273. */
  274. public function clear($uri) {
  275. try {
  276. $this->splitURI($uri);
  277. unset($this->pclasses);
  278. $this->connect();
  279. mysql_query("DELETE FROM `".$this->dbName."`.`".$this->dbTable."`", $this->link);
  280. }
  281. catch(Exception $e) {
  282. throw new KlarnaException("Error in " . __METHOD__ . ": " . $e->getMessage());
  283. }
  284. }
  285. }