function.php 5.2 KB

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