function.php 5.6 KB

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