PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Controller/Component/SeoToolsComponent.php

https://github.com/healthyplatforms/QACMS-SeoTools
PHP | 223 lines | 168 code | 44 blank | 11 comment | 16 complexity | 6fb8c6d5149844d18447339be683fab5 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class SeoToolsComponent extends Component {
  3. protected $_tools = array();
  4. /**
  5. * Controller reference
  6. *
  7. * @var Controller
  8. */
  9. protected $_controller = null;
  10. /**
  11. * Constructor
  12. *
  13. * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
  14. * @param array $settings Array of configuration settings.
  15. */
  16. public function initialize($Controller) {
  17. $this->_controller = $Controller;
  18. $Folder = new Folder(CakePlugin::path('SeoTools') . 'Controller' . DS . 'Component' . DS . 'Tools');
  19. $__tools = $Folder->read(true, false, true);
  20. $__tools = $__tools[0];
  21. $tools = array();
  22. $__yaml = array(
  23. 'name' => '--Unknow Tool--',
  24. 'description' => '',
  25. 'category' => 'Unknow',
  26. 'version' => '--',
  27. 'author' => 'Anonymous'
  28. );
  29. foreach ($__tools as $path) {
  30. $name = basename($path);
  31. $yaml = array();
  32. if (file_exists($path . DS . "{$name}.yaml")) {
  33. $yaml = Spyc::YAMLLoad($path . DS . "{$name}.yaml");
  34. $yaml['author'] = @htmlentities($yaml['author']);
  35. } else {
  36. continue;
  37. }
  38. $yaml = Set::merge($__yaml, $yaml);
  39. $yaml['path'] = str_replace(DS . DS, DS, $path . DS);
  40. $tools[$name] = $yaml;
  41. }
  42. $this->_tools = $tools;
  43. }
  44. public function toolsList() {
  45. return $this->_tools;
  46. }
  47. public function toolInfo($tool) {
  48. return $this->_tools[Inflector::camelize($tool)];
  49. }
  50. public function loadTool($tool) {
  51. $tool = Inflector::camelize($tool);
  52. if (!isset($this->_tools[$tool])) {
  53. return false;
  54. }
  55. if (!file_exists("{$this->_tools[$tool]['path']}{$tool}Component.php")) {
  56. return false;
  57. }
  58. include_once "{$this->_tools[$tool]['path']}{$tool}Component.php";
  59. $class = $tool . 'Component';
  60. $component = new $class($this->_controller->Components);
  61. if (method_exists($component, 'initialize')) {
  62. $component->initialize($this->_controller);
  63. }
  64. if (method_exists($component, 'startup')) {
  65. $component->startup($this->_controller);
  66. }
  67. $component->BaseTools = $this;
  68. return $component;
  69. }
  70. public function toInt($string) {
  71. return preg_replace('#[^0-9]#si', '', $string);
  72. }
  73. function getPage($url = null) {
  74. $url = $url === null ? $this->url['full'] : $url;
  75. if (function_exists('curl_init')) {
  76. $ch = curl_init($url);
  77. $options = array(
  78. CURLOPT_RETURNTRANSFER => true, // return web page
  79. CURLOPT_HEADER => false, // don't return headers
  80. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  81. CURLOPT_ENCODING => "", // handle all encodings
  82. CURLOPT_USERAGENT => env('HTTP_USER_AGENT'), // who am i
  83. CURLOPT_AUTOREFERER => true, // set referer on redirect
  84. CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
  85. CURLOPT_TIMEOUT => 120, // timeout on response
  86. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  87. );
  88. @curl_setopt_array($ch, $options);
  89. return curl_exec($ch);
  90. } else {
  91. return @file_get_contents($url);
  92. }
  93. }
  94. public function parseUrl($url) {
  95. $parse = parse_url('http://' . preg_replace('/http\:\/\//', '', $url));
  96. $parse['full'] = 'http://' . preg_replace('/http\:\/\//', '', $url);
  97. return $parse;
  98. }
  99. public function ping($domain, $reverse = false) {
  100. $parseUrl = $this->parseUrl($domain);
  101. $host = $parseUrl['host'];
  102. $response = '';
  103. ob_start();
  104. if (strpos(strtoupper(php_uname('s')), 'WIN') !== false) {
  105. $cmd = ($reverse) ? "ping -a -n 4 {$host}" : "ping -n 4 {$host}";
  106. system($cmd);
  107. } else {
  108. $cmd = ($reverse) ? "dig +noall +answer -x {$host}" : "ping -c4 -w4 {$host}";
  109. system ($cmd);
  110. system("killall ping");
  111. }
  112. $response = ob_get_contents();
  113. ob_end_clean();
  114. return $this->toUTF8($response);
  115. }
  116. public function html2text($s, $keep = '', $expand = 'script|style|noframes|select|option|noscript') {
  117. $s = ' ' . $s;
  118. $k = array();
  119. if (strlen($keep) > 0) {
  120. $k = explode('|',$keep);
  121. for ($i=0;$i<count($k);$i++) {
  122. $s = str_replace('<' . $k[$i],'[{(' . $k[$i],$s);
  123. $s = str_replace('</' . $k[$i],'[{(/' . $k[$i],$s);
  124. }
  125. }
  126. while(stripos($s,'<!--') > 0) {
  127. $pos[1] = stripos($s,'<!--');
  128. $pos[2] = stripos($s,'-->', $pos[1]);
  129. $len[1] = $pos[2] - $pos[1] + 3;
  130. $x = substr($s,$pos[1],$len[1]);
  131. $s = str_replace($x,'',$s);
  132. }
  133. if (strlen($expand) > 0) {
  134. $e = explode('|',$expand);
  135. for ($i = 0; $i < count($e); $i++) {
  136. while(stripos($s,'<' . $e[$i]) > 0) {
  137. $len[1] = strlen('<' . $e[$i]);
  138. $pos[1] = stripos($s,'<' . $e[$i]);
  139. $pos[2] = stripos($s,$e[$i] . '>', $pos[1] + $len[1]);
  140. $len[2] = $pos[2] - $pos[1] + $len[1];
  141. $x = substr($s,$pos[1],$len[2]);
  142. $s = str_replace($x,'',$s);
  143. }
  144. }
  145. }
  146. while(stripos($s,'<') > 0) {
  147. $pos[1] = stripos($s,'<');
  148. $pos[2] = stripos($s,'>', $pos[1]);
  149. $len[1] = $pos[2] - $pos[1] + 1;
  150. $x = substr($s,$pos[1],$len[1]);
  151. $s = str_replace($x,'',$s);
  152. }
  153. for ($i=0;$i<count($k);$i++) {
  154. $s = str_replace('[{(' . $k[$i],'<' . $k[$i],$s);
  155. $s = str_replace('[{(/' . $k[$i],'</' . $k[$i],$s);
  156. }
  157. $s = preg_replace(
  158. array(
  159. "/[\n\t]+/",
  160. '/\r/',
  161. '/[ ]{2,}/'
  162. ),
  163. array(
  164. ' ',
  165. ' ',
  166. ' '
  167. )
  168. , $s);
  169. return trim($s);
  170. }
  171. public function toUTF8($text) {
  172. $detect_pattern = '%(?:[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})+%xs';
  173. if (!preg_match($detect_pattern, $text)) {
  174. return utf8_encode($text);
  175. } else {
  176. return $text;
  177. }
  178. }
  179. }