Advertisement
p4geoff

Fix for invalid UTF-8 bytes during project add/edit

May 10th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. Index: module/Application/src/Application/View/Helper/Utf8Filter.php
  2. IDEA additional info:
  3. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  4. <+>UTF-8
  5. ===================================================================
  6. --- module/Application/src/Application/View/Helper/Utf8Filter.php (revision )
  7. +++ module/Application/src/Application/View/Helper/Utf8Filter.php (revision )
  8. @@ -0,0 +1,62 @@
  9. +<?php
  10. +/**
  11. + * Perforce Swarm
  12. + *
  13. + * @copyright 2013 Perforce Software. All rights reserved.
  14. + * @license Please see LICENSE.txt in top-level folder of this distribution.
  15. + * @version <release>/<patch>
  16. + */
  17. +
  18. +namespace Application\View\Helper;
  19. +
  20. +use P4\Filter\Utf8;
  21. +use Zend\View\Helper\AbstractHelper;
  22. +
  23. +class Utf8Filter extends AbstractHelper
  24. +{
  25. + protected $utf8Filter = null;
  26. +
  27. + /**
  28. + * Filter utf-8 input, invalid UTF-8 byte sequences will be replaced with
  29. + * an inverted question mark.
  30. + *
  31. + * @param string|null $value the utf-8 input to filter
  32. + * @returns string the filtered result
  33. + */
  34. + public function __invoke($value)
  35. + {
  36. + $this->utf8Filter = $this->utf8Filter ?: new Utf8;
  37. + return $this->utf8Filter->filter($value);
  38. + }
  39. +}
  40. +<?php
  41. +/**
  42. + * Perforce Swarm
  43. + *
  44. + * @copyright 2013 Perforce Software. All rights reserved.
  45. + * @license Please see LICENSE.txt in top-level folder of this distribution.
  46. + * @version <release>/<patch>
  47. + */
  48. +
  49. +namespace Application\View\Helper;
  50. +
  51. +use P4\Filter\Utf8;
  52. +use Zend\View\Helper\AbstractHelper;
  53. +
  54. +class Utf8Filter extends AbstractHelper
  55. +{
  56. + protected $utf8Filter = null;
  57. +
  58. + /**
  59. + * Filter utf-8 input, invalid UTF-8 byte sequences will be replaced with
  60. + * an inverted question mark.
  61. + *
  62. + * @param string|null $value the utf-8 input to filter
  63. + * @returns string the filtered result
  64. + */
  65. + public function __invoke($value)
  66. + {
  67. + $this->utf8Filter = $this->utf8Filter ?: new Utf8;
  68. + return $this->utf8Filter->filter($value);
  69. + }
  70. +}
  71. Index: module/Projects/view/projects/index/edit.phtml
  72. IDEA additional info:
  73. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  74. <+>UTF-8
  75. ===================================================================
  76. --- module/Projects/view/projects/index/edit.phtml (revision 4)
  77. +++ module/Projects/view/projects/index/edit.phtml (revision )
  78. @@ -196,8 +196,14 @@
  79. : '/project/add';
  80. $allMembers = array();
  81. foreach ($users as $user) {
  82. - $allMembers[] = $user->getId()
  83. - . ($user->getFullName() !== $user->getId() ? ' (' . $user->getFullName() . ')' : '');
  84. + // though unexpected, user id's/names can include invalid UTF-8 sequences so
  85. + // we filter them, otherwise our json_encode call could crash with an error.
  86. + // note, if the invalid sequence occurs in the user id portion; that user
  87. + // will not be an addable member.
  88. + $allMembers[] = $this->utf8Filter(
  89. + $user->getId()
  90. + . ($user->getFullName() !== $user->getId() ? ' (' . $user->getFullName() . ')' : '')
  91. + );
  92. }
  93. ?>
  94. <script type="text/javascript">
  95. Index: module/Application/config/module.config.php
  96. IDEA additional info:
  97. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  98. <+>UTF-8
  99. ===================================================================
  100. --- module/Application/config/module.config.php (revision 5)
  101. +++ module/Application/config/module.config.php (revision )
  102. @@ -229,7 +229,8 @@
  103. 'permissions' => 'Application\View\Helper\Permissions',
  104. 'preformat' => 'Application\View\Helper\Preformat',
  105. 'qualifiedUrl' => 'Application\View\Helper\QualifiedUrl',
  106. - 'truncate' => 'Application\View\Helper\Truncate'
  107. + 'truncate' => 'Application\View\Helper\Truncate',
  108. + 'utf8Filter' => 'Application\View\Helper\Utf8Filter'
  109. ),
  110. ),
  111. 'controller_plugins' => array(
  112. Index: module/Projects/src/Projects/Filter/Project.php
  113. IDEA additional info:
  114. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  115. <+>UTF-8
  116. ===================================================================
  117. --- module/Projects/src/Projects/Filter/Project.php (revision 1)
  118. +++ module/Projects/src/Projects/Filter/Project.php (revision )
  119. @@ -11,6 +11,7 @@
  120.  
  121. use Application\Filter\StringToId;
  122. use P4\Connection\ConnectionInterface as Connection;
  123. +use P4\Spec\User;
  124. use Projects\Model\Project as ProjectModel;
  125. use Zend\InputFilter\InputFilter;
  126.  
  127. @@ -78,6 +79,7 @@
  128. );
  129.  
  130. // ensure there is at least one team member
  131. + $knownIds = User::fetchAll(null, $p4)->invoke('getId');
  132. $this->add(
  133. array(
  134. 'name' => 'members',
  135. @@ -105,9 +107,15 @@
  136. array(
  137. 'name' => '\Application\Validator\Callback',
  138. 'options' => array(
  139. - 'callback' => function ($value) {
  140. + 'callback' => function ($value) use ($knownIds) {
  141. if (in_array(false, array_map('is_string', $value))) {
  142. return 'Member ids must be strings';
  143. + }
  144. +
  145. + foreach ($value as $id) {
  146. + if (!in_array($id, $knownIds)) {
  147. + return 'Unknown user id ' . $id;
  148. + }
  149. }
  150.  
  151. return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement