PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/html/AppCode/expressionengine/modules/updated_sites/mod.updated_sites.php

https://github.com/w3bg/www.hsifin.com
PHP | 479 lines | 258 code | 95 blank | 126 comment | 50 complexity | 63a7a50fa2d39d16be067d92cb1472bc MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2010, EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/user_guide/license.html
  13. =====================================================
  14. File: mod.updated_sites.php
  15. -----------------------------------------------------
  16. Purpose: Updated Sites Functionality
  17. =====================================================
  18. */
  19. if ( ! defined('EXT'))
  20. {
  21. exit('Invalid file request');
  22. }
  23. class Updated_sites {
  24. var $return_data = ''; // Bah!
  25. var $LB = "\r\n"; // Line Break for Entry Output
  26. var $id = 1; // Id of Configuration
  27. var $allowed = array();
  28. var $prune = 500;
  29. var $throttle = 15; // Minutes between pings
  30. /**
  31. * Constructor
  32. */
  33. function Updated_sites()
  34. {
  35. // Make a local reference to the ExpressionEngine super object
  36. $this->EE =& get_instance();
  37. }
  38. // ------------------------------------------------------------------------
  39. /**
  40. * Incoming
  41. *
  42. * Incoming MetaWeblog API Requests
  43. *
  44. * @access public
  45. * @return mixed
  46. */
  47. function incoming()
  48. {
  49. // Load the XML-RPC Files
  50. if ( ! class_exists('XML_RPC'))
  51. {
  52. require APPPATH.'libraries/Xmlrpc'.EXT;
  53. }
  54. if ( ! class_exists('XML_RPC_Server'))
  55. {
  56. require APPPATH.'libraries/Xmlrpcs'.EXT;
  57. }
  58. // Specify Functions
  59. $functions = array( 'channelUpdates.extendedPing' => array(
  60. 'function' => 'Updated_sites.extended',
  61. 'signature' => array(array('string', 'string','string', 'string')),
  62. 'docstring' => 'Extended Pings for An EE Site'),
  63. 'channelUpdates.ping' => array(
  64. 'function' => 'Updated_sites.regular',
  65. 'signature' => array(array('string', 'string')),
  66. 'docstring' => 'Channels.com Pings for An EE Site')
  67. );
  68. // Instantiate the Server Class
  69. $server = new XML_RPC_Server($functions);
  70. }
  71. // ------------------------------------------------------------------------
  72. /**
  73. * Load Config
  74. *
  75. * Load Configuration Options
  76. *
  77. * @access private
  78. * @return void
  79. */
  80. function _load_config()
  81. {
  82. $this->EE->lang->loadfile('updated_sites');
  83. $this->id = ( ! $this->EE->input->get('id')) ? '1' : $this->EE->input->get_post('id');
  84. $query = $this->EE->db->query("SELECT updated_sites_allowed, updated_sites_prune FROM exp_updated_sites
  85. WHERE updated_sites_id = '".$this->EE->db->escape_str($this->id)."'");
  86. if ($query->num_rows() > 0)
  87. {
  88. $this->allowed = explode('|', trim($query->row('updated_sites_allowed') ));
  89. $this->prune = $query->row('updated_sites_prune') ;
  90. }
  91. }
  92. // ------------------------------------------------------------------------
  93. /**
  94. * Extended
  95. *
  96. * Extended Ping
  97. *
  98. * @access public
  99. * @param mixed
  100. * @return string
  101. */
  102. function extended($plist)
  103. {
  104. $parameters = $plist->output_parameters();
  105. $this->_load_config();
  106. if ($this->check_urls(array($parameters['1'], $parameters['2'], $parameters['3'])) !== TRUE)
  107. {
  108. return $this->error($this->EE->lang->line('invalid_access'));
  109. }
  110. if ($this->throttle_check($parameters['1']) !== TRUE)
  111. {
  112. return $this->error(str_replace('%X', $this->throttle, $this->EE->lang->line('too_many_pings')));
  113. }
  114. $data = array('ping_site_name' => $this->EE->security->xss_clean(strip_tags($parameters['0'])),
  115. 'ping_site_url' => $this->EE->security->xss_clean(strip_tags($parameters['1'])),
  116. 'ping_site_check' => $this->EE->security->xss_clean(strip_tags($parameters['2'])),
  117. 'ping_site_rss' => $this->EE->security->xss_clean(strip_tags($parameters['3'])),
  118. 'ping_date' => $this->EE->localize->now,
  119. 'ping_ipaddress' => $this->EE->input->ip_address(),
  120. 'ping_config_id' => $this->id);
  121. $this->EE->db->query($this->EE->db->insert_string('exp_updated_site_pings', $data));
  122. return $this->success();
  123. }
  124. // ------------------------------------------------------------------------
  125. /**
  126. * regular
  127. *
  128. * Regular/Decaf Channels.com Ping
  129. *
  130. * @access public
  131. * @param mixed
  132. * @return string
  133. */
  134. function regular($plist)
  135. {
  136. $parameters = $plist->output_parameters();
  137. $this->_load_config();
  138. if ($this->check_urls(array($parameters['1'])) !== TRUE)
  139. {
  140. return $this->error($this->EE->lang->line('invalid_access'));
  141. }
  142. if ($this->throttle_check($parameters['1']) !== TRUE)
  143. {
  144. return $this->error(str_replace('%X', $this->throttle, $this->EE->lang->line('too_many_pings')));
  145. }
  146. $data = array('ping_site_name' => $this->EE->security->xss_clean(strip_tags($parameters['0'])),
  147. 'ping_site_url' => $this->EE->security->xss_clean(strip_tags($parameters['1'])),
  148. 'ping_date' => $this->EE->localize->now,
  149. 'ping_ipaddress' => $this->EE->input->ip_address(),
  150. 'ping_config_id' => $this->id);
  151. $this->EE->db->query($this->EE->db->insert_string('exp_updated_site_pings', $data));
  152. return $this->success();
  153. }
  154. // ------------------------------------------------------------------------
  155. /**
  156. * Check URLs
  157. *
  158. * Validate Incoming URLs
  159. *
  160. * @access public
  161. * @param array
  162. * @return bool
  163. */
  164. function check_urls($urls)
  165. {
  166. if ( ! is_array($urls) OR count($urls) == 0 OR ! is_array($this->allowed) OR count($this->allowed) == 0)
  167. {
  168. return FALSE;
  169. }
  170. $approved = 'n';
  171. for($i=0, $s = count($urls); $i < $s && $approved == 'n'; ++$i)
  172. {
  173. if (trim($urls[$i]) == '')
  174. {
  175. continue;
  176. }
  177. if (stristr($urls[$i], '{') !== FALSE OR stristr($urls[$i], '}') !== FALSE)
  178. {
  179. return FALSE;
  180. }
  181. for ($l=0, $sl = count($this->allowed); $l < $sl && $approved == 'n'; ++$l)
  182. {
  183. if (trim($this->allowed[$l]) == '') continue;
  184. if (stristr($urls[$i], $this->allowed[$l]) !== FALSE)
  185. {
  186. $approved = 'y';
  187. }
  188. }
  189. }
  190. if ($approved == 'n')
  191. {
  192. return FALSE;
  193. }
  194. return TRUE;
  195. }
  196. // ------------------------------------------------------------------------
  197. /**
  198. * Throttle_check
  199. *
  200. * Security Check
  201. *
  202. * @access public
  203. * @param string
  204. * @return bool
  205. */
  206. function throttle_check($url)
  207. {
  208. /** ---------------------------------------------
  209. /** Throttling - Only one ping every X minutes
  210. /** ---------------------------------------------*/
  211. $query = $this->EE->db->query("SELECT COUNT(*) AS count
  212. FROM exp_updated_site_pings
  213. WHERE (ping_site_url = '".$this->EE->db->escape_str($url)."' OR ping_ipaddress = '".$this->EE->input->ip_address()."')
  214. AND ping_date > '".($this->EE->localize->now-($this->throttle*60))."'");
  215. if ($query->row('count') > 0)
  216. {
  217. return FALSE;
  218. }
  219. return TRUE;
  220. }
  221. // ------------------------------------------------------------------------
  222. /**
  223. * Error
  224. *
  225. * XML-RPC Error Message
  226. *
  227. * @access public
  228. * @param string
  229. * @return mixed
  230. */
  231. function error($message)
  232. {
  233. return new XML_RPC_Response('0','401', $message);
  234. }
  235. // ------------------------------------------------------------------------
  236. /**
  237. * Success
  238. *
  239. * USAGE: So Long and Thanks for All the Fish!
  240. *
  241. * @access public
  242. * @return void
  243. */
  244. function success()
  245. {
  246. /** ----------------------------------
  247. /** Prune Database
  248. /** ----------------------------------*/
  249. srand(time());
  250. if ((rand() % 100) < 5)
  251. {
  252. if ( ! is_numeric($this->prune) OR $this->prune == 0)
  253. {
  254. $this->prune = 500;
  255. }
  256. $query = $this->EE->db->query("SELECT MAX(ping_id) as ping_id FROM exp_updated_site_pings");
  257. $row = $query->row_array();
  258. if ( ! empty($row['ping_id']))
  259. {
  260. $this->EE->db->query("DELETE FROM exp_updated_site_pings WHERE ping_id < ".($query->row('ping_id') -$this->prune)."");
  261. }
  262. }
  263. // Send Success Message
  264. $response = new XML_RPC_Response(new XML_RPC_Values(array('flerror' => new XML_RPC_Values('0',"boolean"),
  265. 'message' => new XML_RPC_Values($this->EE->lang->line('successful_ping'),"string")),'struct')
  266. );
  267. return $response;
  268. }
  269. // ------------------------------------------------------------------------
  270. /**
  271. * Pings
  272. *
  273. * Entries Tag
  274. *
  275. * @access public
  276. * @return mixed
  277. */
  278. function pings()
  279. {
  280. // Build query
  281. $sql = "SELECT m.* FROM exp_updated_site_pings m, exp_updated_sites s
  282. WHERE m.ping_config_id = s.updated_sites_id ";
  283. if ($which = $this->EE->TMPL->fetch_param('which'))
  284. {
  285. $sql .= $this->EE->functions->sql_andor_string($which, 'updated_sites_short_name', 's');
  286. }
  287. $order = $this->EE->TMPL->fetch_param('orderby');
  288. $sort = $this->EE->TMPL->fetch_param('sort');
  289. switch($order)
  290. {
  291. case 'name' :
  292. $sql .= " ORDER BY m.ping_date ";
  293. break;
  294. case 'url' :
  295. $sql .= " ORDER BY m.ping_site_url ";
  296. break;
  297. case 'rss' :
  298. $sql .= " ORDER BY m.ping_site_url ";
  299. break;
  300. default:
  301. $sql .= " ORDER BY m.ping_date ";
  302. break;
  303. }
  304. if ($sort == FALSE OR ($sort != 'asc' AND $sort != 'desc'))
  305. {
  306. $sort = "desc";
  307. }
  308. $sql .= $sort;
  309. if ( ! $this->EE->TMPL->fetch_param('limit'))
  310. {
  311. $sql .= " LIMIT 100";
  312. }
  313. else
  314. {
  315. $sql .= " LIMIT ".$this->EE->TMPL->fetch_param('limit');
  316. }
  317. $query = $this->EE->db->query($sql);
  318. if ($query->num_rows() == 0)
  319. {
  320. return $this->EE->TMPL->no_results();
  321. }
  322. $total_results = count($query->result_array());
  323. foreach($query->result_array() as $count => $row)
  324. {
  325. $tagdata = $this->EE->TMPL->tagdata;
  326. $row['count'] = $count+1;
  327. $row['total_results'] = $total_results;
  328. // Conditionals
  329. $tagdata = $this->EE->functions->prep_conditionals($tagdata, $row);
  330. // Parse "single" variables
  331. foreach ($this->EE->TMPL->var_single as $key => $val)
  332. {
  333. // parse {switch} variable
  334. if (strncmp($key, 'switch', 6) == 0)
  335. {
  336. $sparam = $this->EE->functions->assign_parameters($key);
  337. $sw = '';
  338. if (isset($sparam['switch']))
  339. {
  340. $sopt = explode("|", $sparam['switch']);
  341. if (count($sopt) == 2)
  342. {
  343. if (isset($switch[$sparam['switch']]) AND $switch[$sparam['switch']] == $sopt['0'])
  344. {
  345. $switch[$sparam['switch']] = $sopt['1'];
  346. $sw = $sopt['1'];
  347. }
  348. else
  349. {
  350. $switch[$sparam['switch']] = $sopt['0'];
  351. $sw = $sopt['0'];
  352. }
  353. }
  354. }
  355. $tagdata = $this->EE->TMPL->swap_var_single($key, $sw, $tagdata);
  356. }
  357. // {ping_date}
  358. if (strncmp($key, 'ping_date', 9) == 0)
  359. {
  360. if ( ! isset($row['ping_date']) OR $row['ping_date'] == 0)
  361. {
  362. $date = '-';
  363. }
  364. else
  365. {
  366. $date = $this->EE->localize->decode_date($val, $row['ping_date']);
  367. }
  368. $tagdata = $this->EE->TMPL->swap_var_single($key, $date, $tagdata);
  369. }
  370. // Remaining Data
  371. if (in_array($key, array('ping_site_name', 'ping_site_url', 'ping_site_check', 'ping_site_rss', 'ping_ipaddress')))
  372. {
  373. $rdata = ( ! isset($row[$key]) OR $row[$key] == '') ? '-' : $row[$key];
  374. $tagdata = $this->EE->TMPL->swap_var_single($val, $rdata, $tagdata);
  375. }
  376. }
  377. $this->return_data .= $tagdata;
  378. }
  379. return $this->return_data;
  380. }
  381. }
  382. /* End of file mod.updated_sites.php */
  383. /* Location: ./system/expressionengine/modules/updated_sites/mod.updated_sites.php */