GPGColonParser.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file GPGColonParser.php
  4. * @brief Colon parser for --list-keys and --list-secret-keys directive. Project home: https://github.com/vajayattila/GPGShell
  5. * @author Vajay Attila (vajay.attila@gmail.com)
  6. * @copyright MIT License (MIT)
  7. * @date 2018.09.20-2019.07.18
  8. * @version 1.0.1.2
  9. */
  10. class GPGColonParser{
  11. private $__structure;
  12. private $__lasttime;
  13. function __construct(){
  14. $structureJson=file_get_contents(__DIR__.'/GPGStructure.json');
  15. $this->__structure=json_decode($structureJson, true);
  16. //print_r($this->__structure);
  17. }
  18. function recordTypeIs($type, $fields){
  19. return $fields[0]===$type;
  20. }
  21. private function findTypeStructure($type){
  22. $return=FALSE;
  23. $array=$this->__structure['typeOfRecord'];
  24. foreach($array as $item){
  25. foreach($item as $key=>$stru){
  26. if($key==$type){
  27. $return=$stru;
  28. }
  29. }
  30. };
  31. return $return;
  32. }
  33. private function parse__($fields, $fieldcount, $caller){
  34. $structure=$this->findTypeStructure($fields[0]);
  35. $return=array(
  36. 'description' => $structure["description"],
  37. 'recordtype' => $fields[0],
  38. );
  39. for( $i=0; $i<$fieldcount; ++$i ){
  40. if(
  41. array_key_exists('fields', $structure) &&
  42. array_key_exists($i, $structure['fields']) &&
  43. array_key_exists('description', $structure['fields'][$i]) &&
  44. isset($fields) &&
  45. isset($fields[$i])
  46. ){
  47. $return['fields'][]=array(
  48. 'description' => $structure['fields'][$i]['description'],
  49. 'value' => $fields[$i],
  50. 'fieldinfo' => $structure['fields'][$i]['possibleValues'],
  51. 'multipleFlags' => $structure['fields'][$i]['multipleFlags']?'true': 'false'
  52. );
  53. }else{
  54. //user_error("Field index not found ($caller):".$i);
  55. break;
  56. }
  57. };
  58. return $return;
  59. }
  60. private function toUTCTime(&$return){
  61. if($return!=0){
  62. $return=gmdate("Y-m-d\TH:i:s\Z",$return);
  63. }
  64. return $return;
  65. }
  66. private function parsetru($fields){
  67. $return=$this->parse__($fields, 8, __FUNCTION__);
  68. $this->toUTCTime($return['fields'][3]['value']);
  69. $this->toUTCTime($return['fields'][4]['value']);
  70. return $return;
  71. }
  72. private function parsepub($fields){
  73. $return=$this->parse__($fields, 21, __FUNCTION__);
  74. $this->__lasttime=$return['fields'][5]['value'];
  75. $this->toUTCTime($return['fields'][5]['value']);
  76. $this->toUTCTime($return['fields'][6]['value']);
  77. return $return;
  78. }
  79. private function parsesig($fields){
  80. $return=array();//$this->parse__($fields, 21);
  81. return $return;
  82. }
  83. private function parsesec($fields){
  84. $return=$this->parse__($fields, 21, __FUNCTION__);
  85. $this->__lasttime=$return['fields'][5]['value'];
  86. $this->toUTCTime($return['fields'][5]['value']);
  87. $this->toUTCTime($return['fields'][6]['value']);
  88. return $return;
  89. }
  90. private function parseuid($fields){
  91. $return=$this->parse__($fields, 21, __FUNCTION__);
  92. //$this->__lasttime=$return['fields'][5]['value'];
  93. $this->toUTCTime($return['fields'][5]['value']);
  94. $this->toUTCTime($return['fields'][6]['value']);
  95. return $return;
  96. }
  97. private function parsesub($fields){
  98. $return=$this->parse__($fields, 21, __FUNCTION__);
  99. $this->__lasttime=$return['fields'][5]['value'];
  100. $this->toUTCTime($return['fields'][5]['value']);
  101. $this->toUTCTime($return['fields'][6]['value']);
  102. return $return;
  103. }
  104. private function parsessb($fields){
  105. $return=$this->parse__($fields, 21, __FUNCTION__);
  106. $this->toUTCTime($return['fields'][5]['value']);
  107. $this->toUTCTime($return['fields'][6]['value']);
  108. return $return;
  109. }
  110. private function parsefpr($fields){
  111. $return=$this->parse__($fields, 21, __FUNCTION__);
  112. $this->toUTCTime($return['fields'][5]['value']);
  113. $this->toUTCTime($return['fields'][6]['value']);
  114. return $return;
  115. }
  116. private function key($fields){
  117. $return=$fields[5];
  118. switch($fields[0]){
  119. case 'pkd':
  120. case 'tfs':
  121. case 'tru':
  122. case 'spk':
  123. case 'cfg':
  124. $return=$fields[0];
  125. break;
  126. case 'uid':
  127. case 'fpr':
  128. $return=$this->__lasttime;
  129. //$this->__lasttime=NULL;
  130. break;
  131. default:
  132. // already set
  133. };
  134. return $return;
  135. }
  136. function isSpecialRecord($fields){
  137. $return=false;
  138. if(isset($fields[0])){
  139. switch($fields[0]){
  140. case 'pkd':
  141. case 'tfs':
  142. case 'tru':
  143. case 'spk':
  144. case 'cfg':
  145. $return=true;
  146. break;
  147. default:
  148. // already set
  149. };
  150. }
  151. return $return;
  152. }
  153. function getValueDescription($field){
  154. $return=FALSE;
  155. if(isset($field['value'])&&isset($field['fieldinfo'])){
  156. $value=$field['value'];
  157. foreach($field['fieldinfo'] as $item){
  158. if($item['value']==$value){
  159. $return=$item['description'];
  160. break;
  161. };
  162. };
  163. };
  164. return $return;
  165. }
  166. function parseOutput($output){
  167. $return = array();
  168. //print_r($output);
  169. foreach($output as $line){
  170. if(is_array($line)){
  171. print_r($line);
  172. }
  173. $fields=explode(':', $line);
  174. $methodname='parse'.$fields[0];
  175. if(method_exists ( $this , $methodname )){
  176. $return[$this->key($fields)][]=
  177. call_user_func_array(array(__CLASS__, $methodname), array($fields));
  178. };
  179. };
  180. return $return;
  181. }
  182. }