PageRenderTime 70ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/s5-members/application/default/models/InvoiceLog.php

https://bitbucket.org/awylie199/s5t
PHP | 302 lines | 262 code | 14 blank | 26 comment | 30 complexity | 6288ba5934b15067c0dd1608b0cb6245 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, LGPL-3.0, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Class represents records from table invoice_log
  4. * {autogenerated}
  5. * @property int $log_id
  6. * @property timestamp $tm
  7. * @property int $invoice_id
  8. * @property int $user_id
  9. * @property string $paysys_id
  10. * @property string $type
  11. * @property string $title
  12. * @property string $details
  13. * @property string $remote_addr
  14. * @property int $is_processed
  15. * @see Am_Table
  16. * @package Am_Invoice
  17. * @todo COMPRESS()/UNCOMPRESS() values to save storage
  18. */
  19. class InvoiceLog extends Am_Record
  20. {
  21. protected $_mask = array();
  22. protected $_disablePostbackLog = false;
  23. protected $_disableMask = false;
  24. function toggleDisablePostbackLog($flag)
  25. {
  26. $this->_disablePostbackLog = (bool)$flag;
  27. return $this;
  28. }
  29. function setInvoice(Invoice $invoice)
  30. {
  31. $this->invoice_id = $invoice->invoice_id;
  32. $this->user_id = $invoice->user_id;
  33. return $this;
  34. }
  35. function setProcessed()
  36. {
  37. if (!$this->_disablePostbackLog)
  38. $this->updateQuick('is_processed', true);
  39. }
  40. function renderTable($kvRows)
  41. {
  42. $h = "<table class='logdetail'>\n";
  43. foreach ($kvRows as $k => $v)
  44. $h .= sprintf("<tr><th>%s</th><td>%s</td></tr>\n",
  45. Am_Html::escape($k), Am_Html::escape($v));
  46. $h .= "</table>\n";
  47. return $h;
  48. }
  49. function serializeArrayItem(XMLWriter $x, $item)
  50. {
  51. if (is_array($item)) {
  52. $x->startElement('params');
  53. foreach ($item as $k => $v) {
  54. $x->startElement('p');
  55. $x->writeAttribute('name', $k);
  56. if (is_array($v)) $x->writeAttribute('type', 'array');
  57. $this->serializeArrayItem($x, $v);
  58. $x->endElement();
  59. }
  60. $x->endElement();
  61. } else {
  62. $x->text(is_scalar($item) ? $item : serialize($item));
  63. }
  64. }
  65. function serializeItem($vars)
  66. {
  67. $x = new XMLWriter();
  68. $x->openMemory();
  69. $x->setIndent(true);
  70. $x->startElement('invoice-log-item');
  71. if (is_array($vars)) {
  72. $x->writeAttribute('type', 'array');
  73. $this->serializeArrayItem($x, $vars);
  74. } elseif ($vars instanceof Am_Mvc_Request) {
  75. $x->writeAttribute('type', 'incoming-request');
  76. $vars->toXml($x);
  77. } elseif ($vars instanceof Am_Paysystem_Action) {
  78. $x->writeAttribute('type', 'paysystem-action');
  79. $x->writeAttribute('class', get_class($vars));
  80. $vars->toXml($x);
  81. } elseif ($vars instanceof Am_HttpRequest) {
  82. $x->writeAttribute('type', 'outgoing-request');
  83. $vars->toXml($x, false);
  84. } elseif ($vars instanceof HTTP_Request2_Response) {
  85. $x->writeAttribute('type', 'outgoing-request-response');
  86. $x->startElement('status');
  87. $x->writeAttribute('code', $vars->getStatus());
  88. $x->writeAttribute('reason', $vars->getReasonPhrase());
  89. $x->endElement();
  90. $x->startElement('headers');
  91. foreach ($vars->getHeader(null) as $k => $v)
  92. {
  93. $x->startElement('header');
  94. $x->writeAttribute('name', $k);
  95. $x->text($v);
  96. $x->endElement();
  97. }
  98. $x->endElement();
  99. $x->startElement('body');
  100. $x->writeCdata($vars->getBody());
  101. $x->endElement();
  102. $x->endElement();
  103. }
  104. elseif ($vars instanceof Exception)
  105. {
  106. $x->writeAttribute('type', 'exception');
  107. $x->writeElement('type', get_class($vars));
  108. $x->writeElement('message', $vars->getMessage());
  109. $x->startElement('trace');
  110. foreach ($vars->getTrace() as $t)
  111. {
  112. $x->startElement('call');
  113. if (isset($t['class']))
  114. {
  115. $x->writeElement('function', $t['class'] . $t['type'] . $t['function']);
  116. } else {
  117. $x->writeElement('function', $t['function']);
  118. }
  119. $x->writeElement('file', @$t['file']);
  120. $x->writeElement('line', @$t['line']);
  121. $x->endElement();
  122. }
  123. $x->endElement();
  124. } elseif(is_object($vars) && method_exists($vars, 'toArray')) {
  125. $x->writeAttribute('type', 'array');
  126. $this->serializeArrayItem($x, $vars->toArray());
  127. } else {
  128. $x->writeAttribute('type', gettype($vars));
  129. $x->writeCdata((string)$vars);
  130. }
  131. $x->endElement(); // invoice-log-item
  132. return $x->flush();
  133. }
  134. function add($vars, $commit = true)
  135. {
  136. if ($vars instanceof Am_Mvc_Request)
  137. {
  138. $vars = $this->serializeItem($vars);
  139. }
  140. else
  141. {
  142. $vars = $this->serializeItem($vars);
  143. if (!$this->_disableMask)
  144. {
  145. // credit card# has length from 13 to 16 as defined in ccvs
  146. $vars = preg_replace('/(?<!line)>\d{3}</', '>***<', $vars);
  147. $vars = preg_replace('/CDATA\[\d{3}\]\]/', 'CDATA[***]]', $vars);
  148. $vars = preg_replace_callback('/(>)(\d{13,16})(<)/', array($this, '_maskCc'), $vars);
  149. $vars = preg_replace_callback('/(CDATA\[)(\d{13,16})(\]\])/', array($this, '_maskCc'), $vars);
  150. $vars = str_replace(array_keys($this->_mask), array_values($this->_mask), $vars);
  151. }
  152. }
  153. if (!empty($this->details))
  154. {
  155. $this->details = str_replace('</invoice-log>', $vars."</invoice-log>", $this->details);
  156. } else {
  157. $this->details =
  158. '<?xml version="1.0"?>'.PHP_EOL
  159. .'<invoice-log>'.PHP_EOL
  160. .$vars
  161. .'</invoice-log>';
  162. }
  163. if ($commit && !$this->_disablePostbackLog) $this->save();
  164. }
  165. public function update()
  166. {
  167. if (!$this->_disablePostbackLog)
  168. parent::update();
  169. return $this;
  170. }
  171. public function insert($reload = true)
  172. {
  173. if (!$this->_disablePostbackLog)
  174. parent::insert($reload);
  175. return $this;
  176. }
  177. /**
  178. * Automatically replace occurences of $data in log records to $replacement (***)
  179. * @link add()
  180. */
  181. function mask($data, $replacement=null)
  182. {
  183. $this->_mask[(string)$data] = $replacement ? $replacement : str_repeat('*', strlen($data));
  184. return $this;
  185. }
  186. function _maskCc($regs)
  187. {
  188. $cc = $regs[2];
  189. return $regs[1].str_repeat('*', strlen($cc)-4) . substr($cc, -4).$regs[3];
  190. }
  191. function getRenderedDetails()
  192. {
  193. if (empty($this->details)) return array();
  194. $ret = array();
  195. foreach ($this->getXmlDetails() as $a)
  196. {
  197. $popup = null;
  198. list($type, $source) = $a;
  199. if (preg_match('|^\s*<.*>\s*$|ms', $source))
  200. {
  201. $x = new SimpleXMLElement($source);
  202. if (($body = (string)$x->body))
  203. {
  204. list($t) = $x->xpath("headers/header[@name='content-type']");
  205. if ($t) {
  206. list($t) = explode(";", $t);
  207. }
  208. $popup = $this->renderVars($body, (string)$t);
  209. }
  210. }
  211. $source = highlight_string($source, true);
  212. $ret[] = array($type, $source, $popup);
  213. }
  214. return $ret;
  215. }
  216. /**
  217. * return @array details as array(type,xmlsource)
  218. */
  219. function getXmlDetails()
  220. {
  221. $ret = array();
  222. $x = simplexml_load_string($this->details);
  223. foreach($x->{'invoice-log-item'} as $i)
  224. {
  225. $ret[] = array((string)$i->attributes()->type, $i->asXML());
  226. }
  227. return $ret;
  228. }
  229. function renderVars($body, $type = null)
  230. {
  231. switch ($type) {
  232. case 'application/x-www-form-urlencoded':
  233. $str = urldecode($body);
  234. parse_str($str, $arr);
  235. if (get_magic_quotes_gpc())
  236. $arr = Am_Mvc_Request::ss($arr);
  237. break;
  238. case 'application/json' :
  239. $arr = Am_Mvc_Controller::decodeJson($body);
  240. break;
  241. default:
  242. $arr = array();
  243. }
  244. if (!count($arr)) return "";
  245. return print_r($arr, true);
  246. }
  247. /** @return Am_Mvc_Request|null */
  248. function getFirstRequest()
  249. {
  250. if (empty($this->details)) return;
  251. $x = new SimpleXMLElement($this->details);
  252. $k = 'invoice-log-item';
  253. foreach ($x->{'invoice-log-item'} as $item)
  254. {
  255. if ($item['type'] == 'incoming-request')
  256. return Am_Mvc_Request::fromXml($item);
  257. }
  258. }
  259. function toggleMask($flag = false)
  260. {
  261. $this->_disableMask = !$flag;
  262. }
  263. }
  264. class InvoiceLogTable extends Am_Table {
  265. protected $_key = 'log_id';
  266. protected $_table = '?_invoice_log';
  267. protected $_recordClass = 'InvoiceLog';
  268. public function insert(array $values, $returnInserted = false)
  269. {
  270. if (empty($values['tm']))
  271. $values['tm'] = $this->getDi()->sqlDateTime;
  272. if (empty($values['remote_addr']))
  273. $values['remote_addr'] = $_SERVER['REMOTE_ADDR'];
  274. return parent::insert($values, $returnInserted);
  275. }
  276. function log($invoice_id, $paysys_id,
  277. $title, $vars=null, $type='info', $remote_addr=null, $tm=null){
  278. $r = $this->createRecord();
  279. if ($invoice = $this->getDi()->invoiceTable->load($invoice_id, false))
  280. $r->setInvoice($invoice);
  281. $r->paysys_id = $paysys_id;
  282. $r->title = $title;
  283. $r->remote_addr = get_first($remote_addr, $_SERVER['REMOTE_ADDR']);
  284. $r->add($vars, false);
  285. $r->insert();
  286. return $r;
  287. }
  288. }