PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/engine/api.pm.php

https://github.com/foxadmin/ReloadCMS
PHP | 206 lines | 187 code | 14 blank | 5 comment | 56 complexity | 3d0a43c2f1ecee2ab7ac654181446afc MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.com //
  5. // This product released under GNU General Public License v2 //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. define('RCMS_PM_DEFAULT_FILE', DATA_PATH.'/pm/'.$system->user['username'].'.dat');
  8. function pm_disabled()
  9. {
  10. $arr = parse_ini_file(CONFIG_PATH . 'disable.ini');
  11. return isset($arr['pm']);
  12. }
  13. function make_pm_file($file)
  14. {
  15. if (!file_exists($file)) {
  16. $f = fopen($file, "w");
  17. fclose($f);
  18. }
  19. }
  20. function getUserData($username){
  21. global $system;
  22. $result = @unserialize(@file_get_contents(USERS_PATH . basename($username)));
  23. if(empty($result)) return false; else return $result;
  24. }
  25. make_pm_file(RCMS_PM_DEFAULT_FILE);
  26. $_CACHE['gbook'] = array();
  27. function pm_get_msgs($page = 0, $parse = true, $limited = true, $file = RCMS_PM_DEFAULT_FILE) {
  28. global $_CACHE, $system;
  29. $all_pm = 0;
  30. $new_pm = 0;
  31. $pm_hint = __('No new messages');
  32. $ret = array($all_pm, $new_pm, $pm_hint);
  33. $data = &$_CACHE['gbook'][$file];
  34. if(!isset($data)) {
  35. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  36. }
  37. if(!empty($data)){
  38. $c = sizeof($data);
  39. $ndata = $rdata = array();
  40. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  41. $ndata = array_reverse($ndata, true);
  42. if($page !== null){
  43. $i = 0;
  44. while($i < (($page+1) * $system->config['perpage']) && $el = each($ndata)){
  45. if($i >= $page * $system->config['perpage']) $rdata[$el['key']] = $el['value'];
  46. $i++;
  47. }
  48. } else {
  49. $rdata = $ndata;
  50. }
  51. if($parse){
  52. foreach($rdata as $id => $msg){
  53. if(!empty($msg)) {
  54. $rdata[$id]['text'] = rcms_parse_text($msg['text'], !$limited, false, !$limited);
  55. $all_pm++;
  56. if ($msg['new'] == "1"){
  57. $new_pm++;
  58. $pm_hint = rcms_format_time('H:i:s d.m.Y', $msg['time']).' - '.__('last new message from ').$msg['username'].' ('.$msg['nickname'].')';
  59. }
  60. }
  61. }
  62. }
  63. return array($all_pm, $new_pm, $pm_hint);
  64. } else return $ret;
  65. }
  66. function pm_get_msg_by_id($num = 10, $parse = true, $limited, $mid = '0', $file = RCMS_PM_DEFAULT_FILE) {
  67. global $_CACHE, $system;
  68. $t='';
  69. $data = &$_CACHE['gbook'][$file];
  70. if(!isset($data)) {
  71. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  72. }
  73. if(!empty($data)){
  74. $ndata = $rdata = array();
  75. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  76. $ndata = array_reverse($ndata, true);
  77. if($num !== null){
  78. $i = 0;
  79. while($i < $num && $el = each($ndata)){
  80. $rdata[$el['key']] = $el['value'];
  81. $i++;
  82. }
  83. } else {
  84. $rdata = $ndata;
  85. }
  86. if($parse){
  87. $t= rcms_parse_text_by_mode($rdata[$mid]['text'],'text');
  88. }
  89. }
  90. return $t;
  91. }
  92. function pm_get_all_msgs($num = 10, $parse = true, $limited, $file = RCMS_PM_DEFAULT_FILE) {
  93. global $_CACHE, $system;
  94. $data = &$_CACHE['gbook'][$file];
  95. if(!isset($data)) {
  96. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  97. }
  98. if(!empty($data)){
  99. $ndata = $rdata = array();
  100. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  101. $ndata = array_reverse($ndata, true);
  102. if($num !== null){
  103. $i = 0;
  104. while($i < $num && $el = each($ndata)){
  105. $rdata[$el['key']] = $el['value'];
  106. $i++;
  107. }
  108. } else {
  109. $rdata = $ndata;
  110. }
  111. if($parse){
  112. foreach($rdata as $id => $msg){
  113. if(!empty($msg)) {
  114. $rdata[$id]['text'] = rcms_parse_text($msg['text'], !$limited, false, !$limited);
  115. $rdata[$id]['new'] = $msg['new'];
  116. $rdata[$id]['username'] = $msg['username'];
  117. $rdata[$id]['nickname'] = $msg['nickname'];
  118. $rdata[$id]['time'] = rcms_format_time('H:i:s d.m.Y', $msg['time']);
  119. }
  120. }
  121. }
  122. return $rdata;
  123. } else return array();
  124. }
  125. function pm_set_all_nonew($num = 10, $parse = true, $limited, $file = RCMS_PM_DEFAULT_FILE) {
  126. global $_CACHE, $system;
  127. $data = &$_CACHE['gbook'][$file];
  128. if(!isset($data)) {
  129. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  130. }
  131. if(!empty($data)){
  132. $ndata = $rdata = array();
  133. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  134. if($num !== null){
  135. $i = 0;
  136. while($i < $num && $el = each($ndata)){
  137. $rdata[$el['key']] = $el['value'];
  138. $i++;
  139. }
  140. } else {
  141. $rdata = $ndata;
  142. }
  143. if($parse){
  144. foreach($rdata as $id => $msg){
  145. if(!empty($msg)) {
  146. $msg['new'] = '0';
  147. $rdata[$id]=$msg;
  148. }
  149. }
  150. }
  151. return file_write_contents($file, serialize($rdata));
  152. }
  153. }
  154. function pm_get_pages_num($file = RCMS_PM_DEFAULT_FILE) {
  155. global $_CACHE, $system;
  156. $data = &$_CACHE['gbook'][$file];
  157. if(!isset($data)) {
  158. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  159. }
  160. if(!empty($system->config['perpage'])) {
  161. return ceil(sizeof($data)/$system->config['perpage']);
  162. } else return 1;
  163. }
  164. function pm_post_msg($username, $nickname, $text, $to) {
  165. global $_CACHE, $system;
  166. $text = trim($text);
  167. if(empty($text)) return false;
  168. if(!getUserData($to)) return false;
  169. $file = DATA_PATH.'/pm/'.$to.'.dat';
  170. make_pm_file($file);
  171. $data = &$_CACHE['gbook'][$file];
  172. if(!isset($data)) {
  173. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  174. }
  175. $newmesg['username'] = $username;
  176. $newmesg['nickname'] = htmlspecialchars($nickname);
  177. $newmesg['time'] = rcms_get_time();
  178. $newmesg['text'] = $text;
  179. $newmesg['new'] = '1';
  180. $data[] = $newmesg;
  181. return file_write_contents($file, serialize($data));
  182. }
  183. function pm_post_remove($id, $file = RCMS_PM_DEFAULT_FILE) {
  184. global $_CACHE;
  185. $data = &$_CACHE['gbook'][$file];
  186. if(!isset($data)) {
  187. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  188. }
  189. rcms_remove_index($id, $data, true);
  190. return file_write_contents($file, serialize($data));
  191. }
  192. ?>