function.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. require_once "../config.php";
  3. require_once "../share/function.php";
  4. require_once "../db/table.php";
  5. class Article extends Table
  6. {
  7. function __construct($redis=false) {
  8. parent::__construct(_FILE_DB_USER_ARTICLE_, "article", "", "",$redis);
  9. }
  10. public function getInfo($id){
  11. $output = array();
  12. if($this->redis!==false){
  13. if($this->redis->exists("article://".$id)===1){
  14. $output["id"]=$this->redis->hGet("article://".$id,"id");
  15. $output["title"]=$this->redis->hGet("article://".$id,"title");
  16. $output["subtitle"]=$this->redis->hGet("article://".$id,"subtitle");
  17. $output["owner"]=$this->redis->hGet("article://".$id,"owner");
  18. $output["summary"]=$this->redis->hGet("article://".$id,"summary");
  19. $output["lang"]=$this->redis->hGet("article://".$id,"lang");
  20. $output["tag"]=$this->redis->hGet("article://".$id,"tag");
  21. $output["status"]=$this->redis->hGet("article://".$id,"status");
  22. $output["create_time"]=$this->redis->hGet("article://".$id,"create_time");
  23. $output["modify_time"]=$this->redis->hGet("article://".$id,"modify_time");
  24. return $output;
  25. }
  26. }
  27. $query = "SELECT id,title,owner,subtitle,summary,lang,tag,status,create_time,modify_time FROM article WHERE id= ? ";
  28. $stmt = $this->dbh->prepare($query);
  29. $stmt->execute(array($id));
  30. $output = $stmt->fetch(PDO::FETCH_ASSOC);
  31. if($output){
  32. if($this->redis!==false){
  33. $this->redis->hSet("article://".$id,"id",$output["id"]);
  34. $this->redis->hSet("article://".$id,"title",$output["title"]);
  35. if(isset($output["subtitle"])){
  36. $this->redis->hSet("article://".$id,"subtitle",$output["subtitle"]);
  37. }
  38. $this->redis->hSet("article://".$id,"summary",$output["summary"]);
  39. $this->redis->hSet("article://".$id,"lang",$output["lang"]);
  40. $this->redis->hSet("article://".$id,"owner",$output["owner"]);
  41. $this->redis->hSet("article://".$id,"tag",$output["tag"]);
  42. $this->redis->hSet("article://".$id,"status",$output["status"]);
  43. $this->redis->hSet("article://".$id,"create_time",$output["create_time"]);
  44. $this->redis->hSet("article://".$id,"modify_time",$output["modify_time"]);
  45. }
  46. return $output;
  47. }
  48. else{
  49. return false;
  50. }
  51. }
  52. public function getContent($id){
  53. $output = array();
  54. if($this->redis!==false){
  55. if($this->redis->hExists("article://".$id,"content")===TRUE){
  56. $content=$this->redis->hGet("article://".$id,"content");
  57. return $content;
  58. }
  59. }
  60. $query = "SELECT content FROM article WHERE id= ? ";
  61. $stmt = $this->dbh->prepare($query);
  62. $stmt->execute(array($id));
  63. $output = $stmt->fetch(PDO::FETCH_ASSOC);
  64. if($output){
  65. if($this->redis!==false){
  66. $this->redis->hSet("article://".$id,"content",$output["content"]);
  67. }
  68. return $output["content"];
  69. }
  70. else{
  71. return false;
  72. }
  73. }
  74. public function getPower($id,$collectionId=""){
  75. #查询用户对此是否有权限
  76. if(isset($_COOKIE["userid"])){
  77. $userId = $_COOKIE["userid"];
  78. }
  79. else{
  80. $userId=0;
  81. }
  82. if($this->redis!==false){
  83. $power = $this->redis->hGet("power://article/".$id,$userId);
  84. if($power!==FALSE){
  85. return $power;
  86. }
  87. }
  88. $iPower = 0;
  89. $query = "SELECT owner,status FROM article WHERE id=? ";
  90. $stmt = $this->dbh->prepare($query);
  91. $stmt->execute(array($id));
  92. $channel = $stmt->fetch(PDO::FETCH_ASSOC);
  93. if($channel){
  94. if(!isset($_COOKIE["userid"])){
  95. #未登录用户
  96. if($channel["status"]==30){
  97. #全网公开有读取和建议权限
  98. return 10;
  99. }
  100. else{
  101. #其他状态没有任何权限
  102. return 0;
  103. }
  104. }
  105. else{
  106. if($channel["owner"]==$_COOKIE["userid"]){
  107. #自己的
  108. return 30;
  109. }
  110. else if($channel["status"]>=30){
  111. #全网公开的 可以提交pr
  112. $iPower = 10;
  113. }
  114. }
  115. }
  116. #查询共享权限,如果共享权限更大,覆盖上面的的
  117. $sharePower = share_get_res_power($_COOKIE["userid"],$id);
  118. if($collectionId!=""){
  119. $sharePowerCollection = share_get_res_power($_COOKIE["userid"],$collectionId);
  120. }
  121. else{
  122. $sharePowerCollection =0;
  123. }
  124. if($sharePower>$iPower){
  125. $iPower=$sharePower;
  126. }
  127. if($sharePowerCollection>$iPower){
  128. $iPower=$sharePowerCollection;
  129. }
  130. if($this->redis!==false){
  131. $this->redis->hSet("power://article/".$id,$_COOKIE["userid"],$iPower);
  132. }
  133. return $iPower;
  134. }
  135. }
  136. class ArticleList extends Table
  137. {
  138. function __construct($redis=false) {
  139. parent::__construct(_FILE_DB_USER_ARTICLE_, "article_list", "", "",$redis);
  140. }
  141. function upgrade($collectionId,$articleList=array()){
  142. # 更新 article_list 表
  143. $query = "DELETE FROM article_list WHERE collect_id = ? ";
  144. $stmt = $this->dbh->prepare($query);
  145. if($stmt){
  146. $stmt->execute(array($collectionId));
  147. }
  148. if(count($articleList)>0){
  149. /* 开始一个事务,关闭自动提交 */
  150. $this->dbh->beginTransaction();
  151. $query = "INSERT INTO article_list (collect_id, article_id,level,title) VALUES ( ?, ?, ? , ? )";
  152. $sth = $this->dbh->prepare($query);
  153. foreach ($articleList as $row) {
  154. $sth->execute(array($collectionId,$row["article"],$row["level"],$row["title"]));
  155. if($this->redis){
  156. #删除article权限缓存
  157. $this->redis->del("power://article/".$row["article"]);
  158. }
  159. }
  160. $this->dbh->commit();
  161. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  162. /* 识别错误且回滚更改 */
  163. $this->dbh->rollBack();
  164. $error = $this->dbh->errorInfo();;
  165. $respond['status']=1;
  166. $respond['message']=$error[2];
  167. }
  168. }
  169. }
  170. }
  171. ?>