PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/admin_functions.php

http://wordcraft.googlecode.com/
PHP | 210 lines | 101 code | 55 blank | 54 comment | 17 complexity | db2724d25bf32dba4c2279cd4974ab87 MD5 | raw file
Possible License(s): CC0-1.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Functions used in several areas of the admin
  4. *
  5. * @author Brian Moon <brian@moonspot.net>
  6. * @copyright 1997-Present Brian Moon
  7. * @package Wordcraft
  8. * @license http://wordcraft.googlecode.com/files/license.txt
  9. * @link http://wordcraft.googlecode.com/
  10. *
  11. */
  12. require_once "../include/url.php";
  13. /**
  14. * Displays an error message in the admin
  15. *
  16. * @param string $error_message The error to display
  17. * @param bool $exit If true, the script will exit after showing
  18. * the error message
  19. * @return mixed
  20. *
  21. */
  22. function wc_admin_error($error_message, $exit=true) {
  23. global $WC;
  24. if($exit) require_once "./header.php";
  25. ?>
  26. <div class="notice_error">
  27. <?php echo htmlspecialchars($error_message, ENT_COMPAT, "UTF-8"); ?>
  28. </div>
  29. <?php
  30. if($exit) require_once "./footer.php";
  31. if($exit){
  32. exit();
  33. }
  34. }
  35. /**
  36. * Shows a message such as a success event.
  37. *
  38. * @param string $message The message to be displayed
  39. * @param bool $exit If true, the script will exit after showing
  40. * the error message
  41. * @param string $redir A URL to redirect to after showing the message
  42. * @return mixed
  43. *
  44. */
  45. function wc_admin_message($message, $exit=true, $redir=null) {
  46. global $WC;
  47. if($exit) require_once "./header.php";
  48. ?>
  49. <div class="notice">
  50. <?php echo htmlspecialchars($message, ENT_COMPAT, "UTF-8"); ?>
  51. </div>
  52. <?php if(!empty($redir)){ ?>
  53. <meta http-equiv="refresh" content="3;url=<?=$redir?>">
  54. <?php } ?>
  55. <?php
  56. if($exit) require_once "./footer.php";
  57. if($exit){
  58. exit();
  59. }
  60. }
  61. /**
  62. * Sends linkbacks to other sites that posts link to
  63. *
  64. * @param int $post_id The id of the post to check for links
  65. * @return mixed
  66. *
  67. */
  68. function wc_admin_handle_linkbacks($post_id) {
  69. global $WCDATA, $WC;
  70. $post = wc_db_get_post($post_id);
  71. $post_url = wc_get_url("post", array($post_id, $post["uri"]));
  72. // handle pingbacks/trackbacks
  73. if(preg_match_all('!href=\s*"(.+?)"|href=\s*\'(.+?)\'|href=\s*([^ >]+)!si', $post["body"], $match)){
  74. $urls = array_unique(array_merge($match[1], $match[2], $match[3]));
  75. foreach($urls as $url){
  76. if(empty($url)) continue;
  77. $data = wc_admin_get_url($url, "HEAD");
  78. if(strpos($data, "X-Pingback")){
  79. preg_match('!X-Pingback: (.+?)\s!', $data, $match);
  80. $pingback_url = $match[1];
  81. }
  82. if(empty($pingback_url)){
  83. $data = @file_get_contents($url);
  84. if(preg_match('!<link.+?rel="pingback".*>!si', $data, $match)){
  85. if(preg_match('!href="(.+?)"|href=\'(.+?)\'|href=([^ >]+)!', $match[0], $match)){
  86. $pingback_url = max($match[1], $match[2], $match[3]);
  87. }
  88. } elseif(preg_match('!<rdf:Description.+?trackback:ping=[\'"](.+?)[\'"]!si', $data, $match)){
  89. $trackback_url = $match[1];
  90. }
  91. }
  92. if(!empty($pingback_url)){
  93. // do pingback
  94. $data ='<?xml version="1.0"?>';
  95. $data.='<methodCall>';
  96. $data.='<methodName>pingback.ping</methodName>';
  97. $data.='<params>';
  98. $data.='<param><value><string>'.str_replace("&", "&amp;", $post_url).'</string></value></param>';
  99. $data.='<param><value><string>'.str_replace("&", "&amp;", $url).'</string></value></param>';
  100. $data.='</params></methodCall>';
  101. wc_admin_get_url($pingback_url, "POST", $data);
  102. } elseif(!empty($trackback_url)) {
  103. // do trackback
  104. $data = "url=".urlencode($post_url);
  105. $data.= "&title=".urlencode($post["title"]);
  106. $data.= "&blog_name=".urlencode($WC["default_title"]);
  107. wc_admin_get_url($trackback_url, "POST", $data);
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Returns the contents of a URL
  114. *
  115. * @param string $url The URL to fetch
  116. * @param string $method The request type.
  117. * @param string $request_data Data to be passed in a POST request
  118. * @return mixed
  119. *
  120. */
  121. function wc_admin_get_url($url, $method="GET", $request_data="") {
  122. $data = "";
  123. $url_parts = parse_url($url);
  124. $host = ($url_parts["scheme"]=="http") ? $url_parts["host"] : "ssl://".$url_parts["host"];
  125. $port = (isset($url_parts["port"])) ? $url_parts["port"] : ( ($url_parts["scheme"]=="http") ? 80 : 443 );
  126. $uri = (isset($url_parts["path"])) ? $url_parts["path"] : "/";
  127. $url.= (isset($url_parts["query"])) ? $url_parts["query"] : "";
  128. $url.= (isset($url_parts["fragment"])) ? $url_parts["fragment"] : "";
  129. $fp = @fsockopen($host, $port, $errno, $errstr, 5);
  130. if($fp) {
  131. $packet = "$method $uri HTTP/1.0\r\nHost: $url_parts[host]\r\nContent-Length: ".strlen($request_data)."\r\n\r\n$request_data";
  132. fputs($fp, $packet);
  133. while(!feof($fp)){
  134. $data.= fread($fp, 256);
  135. }
  136. }
  137. return $data;
  138. }
  139. /**
  140. * Generates a secret token for use in post forms
  141. *
  142. * @return string
  143. */
  144. function wc_gen_form_secret() {
  145. static $secret;
  146. if(empty($secret)){
  147. $secret = uniqid();
  148. if(!isset($_SESSION["form_secrets"])) $_SESSION["form_secrets"] = array();
  149. array_unshift($_SESSION["form_secrets"], $secret);
  150. // keep the last 20 secrets around to allow for opening forms in tabs and such
  151. $_SESSION["form_secrets"] = array_slice($_SESSION["form_secrets"], 0, 20);
  152. }
  153. return $secret;
  154. }
  155. ?>