PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/include/plugin.php

http://phpfor.googlecode.com/
PHP | 192 lines | 170 code | 19 blank | 3 comment | 25 complexity | 5c5bc40b68f9e6ba63d3e9e7a1cd4ef8 MD5 | raw file
  1. <?php
  2. include_once('shopObject.php');
  3. define(LOWER_CASE,1);
  4. define(UPPER_CASE,2);
  5. class plugin extends shopObject{
  6. var $plugin_type=null;
  7. var $plugin_name=null;
  8. var $_plugin_obj = null;
  9. function getType(){
  10. return array(
  11. 'payment'=>array('text'=>'????','type'=>'file','prefix'=>'pay.','case'=>LOWER_CASE),
  12. // 'shipping'=>array('text'=>'????','type'=>'file','prefix'=>'ship.'),
  13. 'dataio'=>array('text'=>'????','type'=>'file','prefix'=>'io.'),
  14. 'messenger'=>array('text'=>'????','type'=>'dir','prefix'=>'messenger.'),
  15. 'passport'=>array('text'=>'????','type'=>'file','prefix'=>'passport.'),
  16. 'pmtScheme'=>array('text'=>'????','type'=>'file','prefix'=>'pmt.'),
  17. 'schema'=>array('text'=>'????','type'=>'dir','prefix'=>'schema.'),
  18. // 'hooks'=>array('text'=>'????','type'=>'file','prefix'=>'hook.'),
  19. 'functions'=>array('text'=>'????','type'=>'func','prefix'=>''),
  20. );
  21. }
  22. function getFile($item) {
  23. $file_name = ($this->plugin_type=='dir')?
  24. PLUGIN_DIR.'/'.$this->plugin_name.'/'.$item.'/'.($this->prefix!==false?$this->prefix:$this->plugin_name).$item.'.php':
  25. PLUGIN_DIR.'/'.$this->plugin_name.'/'.($this->prefix!==false?$this->prefix:$this->plugin_name).$item.'.php';
  26. if (is_file($file_name)) {
  27. return $file_name;
  28. }else{
  29. return false;
  30. }
  31. }
  32. function _getClassName($item) {
  33. return preg_replace('/[\.-]+/','_',($this->prefix?$this->prefix:$this->plugin_name).$item);
  34. }
  35. function &load($item){
  36. if (!$this->_plugin_obj[$item]) {
  37. if ($file_name = $this->getFile($item)){
  38. include_once($file_name);
  39. $className = $this->_getClassName($item);
  40. $obj = new $className;
  41. return $obj;
  42. }else{
  43. trigger_error('plugin file error', E_USER_ERROR);
  44. }
  45. }
  46. return $this->_plugin_obj[$item];
  47. }
  48. function getHeader($file){
  49. if (($code = file_get_contents($file)) !== false) {
  50. $tokens = token_get_all ($code);
  51. foreach ($tokens as $token){
  52. if (is_array ($token)) {
  53. list ($type, $text) = $token;
  54. switch ($type) {
  55. case T_VARIABLE:
  56. case T_FUNCTION:
  57. case T_NEW:
  58. case T_CLASS:
  59. case T_VAR:
  60. return $result;
  61. case T_STRING:
  62. case T_WHITESPACE:
  63. break;
  64. case T_COMMENT:
  65. case T_ML_COMMENT:
  66. case 366:
  67. $result .= $text;
  68. break;
  69. }
  70. }
  71. }
  72. return $result;
  73. }
  74. }
  75. function getParams($item, $ifMethods=true,$withDesc = false){
  76. $t = array('name'=>$item);
  77. $file = $this->getFile($item);
  78. include_once($file);
  79. $className = $this->_getClassName($item);
  80. $t['class'] = $className;
  81. if(class_exists($className)){
  82. $o = new $className;
  83. $t =array_merge($t, get_object_vars($o));
  84. if ($ifMethods) {
  85. $t['methods'] = get_class_methods($className);
  86. }
  87. //for PHP4/PHP5 Compatibility
  88. $t['hasOptions'] = in_array('getoptions',$t['methods'])||in_array('getOptions',$t['methods']);
  89. if(in_array('extravars',$t['methods'])||in_array('extraVars',$t['methods'])){
  90. $obj = new $className;
  91. $t = array_merge($t,$obj->extraVars());
  92. }
  93. }
  94. if($withDesc){
  95. $t['desc'] = $this->getHeader($file);
  96. }
  97. return $t;
  98. }
  99. function filter($el){
  100. foreach ($this->_filter as $k => $v) {
  101. settype($v, 'array');
  102. foreach ($v as $v1) {
  103. if (isset($el[$k]) && $el[$k]==$v1) {
  104. return true;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. function getList($filter=array(), $ifMethods=true,$withDesc=false){
  111. $handle = opendir(PLUGIN_DIR.'/'.$this->plugin_name);
  112. $t = array();
  113. while(false!==($file=readdir($handle))){
  114. if($file{0} != '.') {
  115. if($this->plugin_case==LOWER_CASE){
  116. if($file!=strtolower($file)){
  117. continue;
  118. }
  119. }elseif($this->plugin_case==UPPER_CASE){
  120. if($file!=strtoupper($file)){
  121. continue;
  122. }
  123. }
  124. $params = null;
  125. if ($this->plugin_type=='dir') {
  126. $item = $file;
  127. if(is_dir(PLUGIN_DIR.'/'.$this->plugin_name.'/'.$file) && $this->getFile($item)){
  128. $params = $this->getParams($item, $ifMethods,$withDesc);
  129. }
  130. }else{ //file
  131. if(preg_match('/^'.($this->prefix!==false?str_replace('.','\.',$this->prefix):$this->plugin_name).'([a-z0-9\_]+)\.php/i',$file, $match)) {
  132. $item = $match[1];
  133. $params = $this->getParams($item, $ifMethods,$withDesc);
  134. $params['item'] = $item;
  135. }
  136. }
  137. if($params){
  138. $params['file'] = 'plugins/'.$this->plugin_name.'/'.$file;
  139. $t[$item] = $params;
  140. }
  141. }
  142. }
  143. closedir($handle);
  144. ksort($t);
  145. if($filter) {
  146. $this->_filter = $filter;
  147. return array_filter($t,array(&$this, 'filter'));
  148. }else{
  149. return $t;
  150. }
  151. }
  152. function getOptions($item,$valueOnly = false){
  153. $obj = $this->load($item);
  154. if(method_exists($obj,'getOptions')||method_exists($obj,'getoptions')){
  155. $options = $obj->getOptions(); foreach($options as $key=>$value){
  156. $v = $this->system->getConf('plugin.'.$this->plugin_name.'.'.$item.'.config.'.$key);
  157. if($valueOnly){
  158. $options[$key] = (is_null($v))?$options[$key]:$v;
  159. }else{
  160. $options[$key]['value'] = (is_null($v))?$options[$key]['value']:$v;
  161. }
  162. }
  163. return $options;
  164. }
  165. }
  166. function saveCfg($type,$data){
  167. foreach($data as $key=>$value){
  168. $this->system->setConf('plugin.'.$this->plugin_name.'.'.$type.'.config.'.$key,$value);
  169. }
  170. return true;
  171. }
  172. }
  173. ?>