PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/engine/api.articles.php

https://github.com/foxadmin/ReloadCMS
PHP | 852 lines | 783 code | 39 blank | 30 comment | 282 complexity | 4038cef27af570af726cf1437b40769e 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('ARTICLES_PATH', DATA_PATH . 'a/');
  8. class articles{
  9. var $containers = array();
  10. var $categories = array();
  11. var $articles = array();
  12. var $container = '';
  13. var $index = array();
  14. var $config = array();
  15. var $last_error = '';
  16. //--------------------------Loading configuration-----------------------//
  17. function articles(){
  18. $this->config = @parse_ini_file(CONFIG_PATH . 'articles.ini');
  19. if (empty ($this->config['count_views'])) $this->config['count_views']='';
  20. if (empty ($this->config['count_comments'])) $this->config['count_comments']='';
  21. }
  22. // Containers
  23. function getContainers($all = 1){
  24. $res = rcms_scandir(ARTICLES_PATH, '', 'dir');
  25. foreach ($res as $dir){
  26. if($all == 2 || ($all == 1 && $dir != '#hidden') || ($all == 0 && $dir != '#hidden' && $dir != '#root')){
  27. $this->containers[$dir] = __(file_get_contents(ARTICLES_PATH . $dir . '/title'));
  28. }
  29. }
  30. return $this->containers;
  31. }
  32. function setWorkContainer($id){
  33. if(empty($id) || preg_replace("/#{0,1}[\d\w]+/i", '', $id) != '') {
  34. $this->last_error = __('Invalid ID');
  35. return false;
  36. }
  37. if(!is_dir(ARTICLES_PATH . $id)) {
  38. $this->last_error = __('Section with this ID doesn\'t exists');
  39. return false;
  40. }
  41. $this->container = $id;
  42. return $this->getIndex();
  43. }
  44. function createContainer($id, $title){
  45. if(empty($id) || preg_replace("/[\d\w]+/i", '', $id) != '') {
  46. $this->last_error = __('Invalid ID');
  47. return false;
  48. }
  49. if(is_dir(ARTICLES_PATH . $id) || is_file(ARTICLES_PATH . $id)) {
  50. $this->last_error = __('Section with this ID already exists');
  51. return false;
  52. }
  53. if(!rcms_mkdir(ARTICLES_PATH . $id)){
  54. $this->last_error = __('Cannot create directory');
  55. return false;
  56. }
  57. if(!file_write_contents(ARTICLES_PATH . $id . '/title', $title) || !file_write_contents(ARTICLES_PATH . $id . '/lst', 0)){
  58. $this->last_error = __('Cannot write to file');
  59. return false;
  60. }
  61. $this->containers[$id] = $title;
  62. return true;
  63. }
  64. function editContainer($id, $newid, $newtitle){
  65. if($id == '#root' || $id == '#hidden') {
  66. $this->last_error = __('Cannot rename system section');
  67. return false;
  68. }
  69. if(empty($id) || preg_replace("/[\d\w]+/i", '', $id) != '') {
  70. $this->last_error = __('Invalid ID');
  71. return false;
  72. }
  73. if(empty($newid) || preg_replace("/[\d\w]+/i", '', $newid) != '') {
  74. $this->last_error = __('Invalid ID');
  75. return false;
  76. }
  77. if(!is_dir(ARTICLES_PATH . $id)) {
  78. $this->last_error = __('Section with this ID doesn\'t exists');
  79. return false;
  80. }
  81. if($id != $newid && is_dir(ARTICLES_PATH . $newid)) {
  82. $this->last_error = __('Section with this ID already exists');
  83. return false;
  84. }
  85. if($id !== $newid && !rcms_rename_file(ARTICLES_PATH . $id, ARTICLES_PATH . $newid)){
  86. $this->last_error = __('Cannot change id');
  87. return false;
  88. }
  89. if(!file_write_contents(ARTICLES_PATH . $newid . '/title', $newtitle)){
  90. $this->last_error = __('Cannot write to file');
  91. return false;
  92. }
  93. unset($this->containers[$id]);
  94. $this->containers[$newid] = $newtitle;
  95. return true;
  96. }
  97. function removeContainer($id){
  98. if(empty($id) || preg_replace("/[\d\w]+/i", '', $id) != '') {
  99. $this->last_error = __('Invalid ID');
  100. return false;
  101. }
  102. if($id == '#root' || $id == '#hidden') {
  103. $this->last_error = __('Cannot remove system section');
  104. return false;
  105. }
  106. if(!is_dir(ARTICLES_PATH . $id)) {
  107. $this->last_error = __('Section with this ID doesn\'t exists');
  108. return false;
  109. }
  110. if(!rcms_delete_files(ARTICLES_PATH . $id, true)){
  111. $this->last_error = __('Cannot remove section');
  112. return false;
  113. }
  114. unset($this->containers[$id]);
  115. return true;
  116. }
  117. //---------------------------------------------------------------------------------//
  118. // Categories
  119. function getCategories($short = false, $parse = true, $ret_last_article = false){
  120. if(empty($this->container)){
  121. $this->last_error = __('No section selected!');
  122. return false;
  123. }
  124. if($this->container == '#root' || $this->container == '#hidden'){
  125. $this->last_error = __('This system section doesn\'t have categories');
  126. return false;
  127. }
  128. $return = array();
  129. $path = ARTICLES_PATH . $this->container . '/';
  130. if($categories = rcms_scandir($path, '', 'dir')) {
  131. foreach ($categories as $id => $category){
  132. if($data = $this->getCategory($category, $parse, $ret_last_article)){
  133. if(!$short) {
  134. $return[] = $data;
  135. } else {
  136. $return[$data['id']] = $data['title'];
  137. }
  138. }
  139. }
  140. }
  141. return $return;
  142. }
  143. function getCategory($cat_id = 0, $parse = true, $ret_last_article = false){
  144. $cat_id = (int) $cat_id;
  145. if(empty($this->container)){
  146. $this->last_error = __('No section selected!');
  147. return false;
  148. }
  149. if($this->container == '#root' || $this->container == '#hidden'){
  150. $this->last_error = __('This system section doesn\'t have categories');
  151. return false;
  152. }
  153. global $system;
  154. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  155. if(empty($cat_id)){
  156. $this->last_error = __('Invalid category ID');
  157. return false;
  158. }
  159. $cat_data = &$this->categories[$this->container][$cat_id];
  160. // Checking access level
  161. $cat_data['accesslevel'] = (!is_file($cat_prefix . 'access')) ? 0 : (int) file_get_contents($cat_prefix . 'access');
  162. if($cat_data['accesslevel'] > @$system->user['accesslevel'] && !$system->checkForRight('-any-')) {
  163. $this->last_error = __('Access denied');
  164. return false;
  165. }
  166. // If category exists
  167. if(is_dir($cat_prefix)){
  168. $cat_data['id'] = $cat_id;
  169. $cat_data['title'] = file_get_contents($cat_prefix . 'title');
  170. $cat_data['description'] = @file_get_contents($cat_prefix . 'description');
  171. if($parse) $cat_data['description'] = rcms_parse_text($cat_data['description'], true, false, true, false, true);
  172. $cat_data['articles_clv'] = sizeof(rcms_scandir($cat_prefix, '', 'dir'));
  173. if($ret_last_article && $cat_data['articles_clv'] > 0){
  174. $stat = $this->getStat('time', $cat_id, true);
  175. if(!empty($stat)) {
  176. $id = explode('.', $stat['key']);
  177. $cat_data['last_article'] = $this->getArticle($cat_id, $id[1], $parse, false, false, false);
  178. }
  179. }
  180. // Search for icon
  181. if(is_file($cat_prefix . 'icon.gif')) {
  182. $cat_data['icon'] = 'icon.gif';
  183. $cat_data['iconfull'] = $cat_prefix . 'icon.gif';
  184. } elseif(is_file($cat_prefix . 'icon.png')) {
  185. $cat_data['icon'] = 'icon.png';
  186. $cat_data['iconfull'] = $cat_prefix . 'icon.png';
  187. } elseif(is_file($cat_prefix . 'icon.jpg')) {
  188. $cat_data['icon'] = 'icon.jpg';
  189. $cat_data['iconfull'] = $cat_prefix . 'icon.jpg';
  190. }elseif(is_file($cat_prefix . 'icon.jpeg')) {
  191. $cat_data['icon'] = 'icon.jpeg';
  192. $cat_data['iconfull'] = $cat_prefix . 'icon.jpeg';
  193. } else $cat_data['icon'] = false;
  194. // Finish!
  195. return $cat_data;
  196. } else {
  197. $this->last_error = __('There are no category with this ID');
  198. return false;
  199. }
  200. }
  201. function createCategory($title, $desc = '', $icon = array(), $access = 0) {
  202. if(empty($this->container)){
  203. $this->last_error = __('No section selected!');
  204. return false;
  205. }
  206. if($this->container == '#root' || $this->container == '#hidden'){
  207. $this->last_error = __('This system section doesn\'t have categories');
  208. return false;
  209. }
  210. // If title is empty we cannot create category
  211. if(empty($title)) {
  212. $this->last_error = __('Title is empty');
  213. return false;
  214. }
  215. if(is_readable(ARTICLES_PATH . $this->container . '/lst')) {
  216. $cat_id = (int) @file_get_contents(ARTICLES_PATH . $this->container . '/lst') + 1;
  217. } else {
  218. $cat_id = 1;
  219. }
  220. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  221. $cat_data = &$this->categories[$this->container][$cat_id];
  222. rcms_mkdir($cat_prefix);
  223. // Now we can safely create category files
  224. file_write_contents($cat_prefix . 'title', $title);
  225. file_write_contents($cat_prefix . 'description', $desc);
  226. file_write_contents($cat_prefix . 'access', $access);
  227. file_write_contents($cat_prefix . 'lst', '0');
  228. // If there is an icon uploaded let's parse it
  229. if(!empty($icon) && empty($icon['error'])){
  230. $icon['name'] = basename($icon['name']);
  231. $icon['tmp'] = explode('.', $icon['name']);
  232. if($icon['type'] == 'image/gif' || $icon['type'] == 'image/jpeg' || $icon['type'] == 'image/png'){
  233. @move_uploaded_file($icon['tmp_name'], $cat_prefix . 'icon.' . $icon['tmp'][sizeof($icon['tmp'])-1]);
  234. } else {
  235. $this->last_error = __('Category created without icon');
  236. return false;
  237. }
  238. }
  239. file_write_contents(ARTICLES_PATH . $this->container . '/lst', $cat_id);
  240. return true;
  241. }
  242. function editCategory($cat_id, $title, $desc, $icon = array(), $access = 0, $killicon = false) {
  243. $cat_id = (int) $cat_id;
  244. if(empty($this->container)){
  245. $this->last_error = __('No section selected!');
  246. return false;
  247. }
  248. if($this->container == '#root' || $this->container == '#hidden'){
  249. $this->last_error = __('This system section doesn\'t have categories');
  250. return false;
  251. }
  252. // If title is empty we cannot create category
  253. if(empty($title)) {
  254. $this->last_error = __('Title is empty');
  255. return false;
  256. }
  257. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  258. $cat_data = &$this->categories[$this->container][$cat_id];
  259. if(!$cat_data = $this->getCategory($cat_id, false)) {
  260. $this->last_error = __('There are no category with this ID');
  261. return false;
  262. }
  263. file_write_contents($cat_prefix . 'title', $title);
  264. file_write_contents($cat_prefix . 'description', $desc);
  265. file_write_contents($cat_prefix . 'access', $access);
  266. if(!empty($killicon)) rcms_delete_files($cat_data['iconfull']);
  267. if(!empty($icon) && empty($icon['error'])){
  268. $icon['name'] = basename($icon['name']);
  269. $icon['tmp'] = explode('.', $icon['name']);
  270. if($icon['type'] == 'image/gif' || $icon['type'] == 'image/jpeg' || $icon['type'] == 'image/png'){
  271. @move_uploaded_file($icon['tmp_name'], $cat_prefix . 'icon.' . $icon['tmp'][sizeof($icon['tmp'])-1]);
  272. } else {
  273. $this->last_error = __('Category saved without icon');
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. function deleteCategory($cat_id) {
  280. $cat_id = (int) $cat_id;
  281. if(empty($this->container)){
  282. $this->last_error = __('No section selected!');
  283. return false;
  284. }
  285. if($this->container == '#root' || $this->container == '#hidden'){
  286. $this->last_error = __('This system section doesn\'t have categories');
  287. return false;
  288. }
  289. if(!$cat_data = $this->getCategory($cat_id, false)) {
  290. $this->last_error = __('There are no category with this ID');
  291. return false;
  292. }
  293. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  294. rcms_remove_index($cat_id, $this->index, true);
  295. $this->saveIndex();
  296. rcms_delete_files($cat_prefix, true);
  297. return true;
  298. }
  299. //---------------------------------------------------------------------------------//
  300. // Articles
  301. function getArticles($cat_id, $parse = true, $desc = false, $text = false) {
  302. $cat_id = (int) $cat_id;
  303. global $system;
  304. if($this->container !== '#root' && $this->container !== '#hidden'){
  305. if(!($category = $this->getCategory($cat_id))){
  306. return false;
  307. }
  308. if(!$system->checkForRight('-any-') && $category['accesslevel'] > (int)@$system->user['accesslevel']) {
  309. $this->last_error = __('Access denied');
  310. return false;
  311. }
  312. $dir = ARTICLES_PATH . $this->container . '/' . $cat_id;
  313. } else {
  314. $dir = ARTICLES_PATH . $this->container;
  315. }
  316. $return = array();
  317. if($articles = rcms_scandir($dir, '', 'dir')) {
  318. foreach ($articles as $art_id) {
  319. $return[] = $this->getArticle($cat_id, $art_id, $parse, $desc, $text);
  320. }
  321. }
  322. return $return;
  323. }
  324. function getArticle($cat_id, $art_id, $parse = true, $desc = false, $text = false, $cnt = false) {
  325. $cat_id = (int) $cat_id;
  326. $art_id = (int) $art_id;
  327. if(empty($this->container)){
  328. $this->last_error = __('No section selected!');
  329. return false;
  330. }
  331. global $system;
  332. if($this->container !== '#root' && $this->container !== '#hidden'){
  333. if(!($category = $this->getCategory($cat_id))){
  334. $this->last_error = __('There are no category with this ID');
  335. return false;
  336. }
  337. if($category['accesslevel'] > (int)@$system->user['accesslevel'] && !$system->checkForRight('-any-')) {
  338. $this->last_error = __('Access denied');
  339. return false;
  340. }
  341. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  342. $art_data = &$this->articles[$this->container][$cat_id][$art_id];
  343. } else {
  344. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  345. $art_data = &$this->articles[$this->container][$art_id];
  346. }
  347. if(is_file($art_prefix . 'define')){
  348. $art_data = rcms_parse_ini_file($art_prefix . 'define');
  349. if($cnt){
  350. $art_data['views']++;
  351. if(!write_ini_file($art_data, $art_prefix . 'define')){
  352. $this->last_error = __('Cannot write to file');
  353. } else {
  354. $this->index[$cat_id][$art_id]['view'] = $art_data['views'];
  355. $this->saveIndex();
  356. }
  357. }
  358. $art_data['text_nonempty'] = (filesize($art_prefix . 'full') < 1) ? false : true;
  359. if($desc) {
  360. $art_data['desc'] = file_get_contents($art_prefix . 'short');
  361. if ($parse) $art_data['desc'] = rcms_parse_text_by_mode($art_data['desc'], $art_data['mode']);
  362. }
  363. if($text) {
  364. $art_data['text'] = file_get_contents($art_prefix . 'full');
  365. if ($parse) $art_data['text'] = rcms_parse_text_by_mode($art_data['text'], $art_data['mode']);
  366. }
  367. $art_data['id'] = $art_id;
  368. $art_data['catid'] = $cat_id;
  369. $art_data['comcnt'] = $art_data['comcount'];
  370. $art_data['title'] = str_replace('&quot;', '"', $art_data['title']);
  371. return $art_data;
  372. } else {
  373. $this->last_error = __('There are no article with this ID');
  374. return false;
  375. }
  376. }
  377. function saveArticle($cat_id, $art_id, $title, $src, $keywords, $sef_desc, $desc, $text, $mode = 'text', $comments = 'yes') {
  378. $cat_id = (int) $cat_id;
  379. $art_id = (int) $art_id;
  380. global $system;
  381. if(empty($this->container)){
  382. $this->last_error = __('No section selected!');
  383. return false;
  384. }
  385. $new_flag = ($art_id == 0);
  386. if($this->container !== '#root' && $this->container !== '#hidden'){
  387. if(!($category = $this->getCategory($cat_id))){
  388. return false;
  389. }
  390. if($category['accesslevel'] > (int)@$system->user['accesslevel'] && !$system->checkForRight('-any-')) {
  391. $this->last_error = __('Access denied');
  392. return false;
  393. }
  394. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  395. if($new_flag) $art_id = @file_get_contents($cat_prefix . 'lst') + 1;
  396. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  397. $art_data = &$this->articles[$this->container][$cat_id][$art_id];
  398. } else {
  399. $cat_prefix = ARTICLES_PATH . $this->container . '/';
  400. if($new_flag) $art_id = @file_get_contents($cat_prefix . 'lst') + 1;
  401. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  402. $art_data = &$this->articles[$this->container][$art_id];
  403. }
  404. // For security reasons all html will be striped off
  405. $title = str_replace('"', '&quot;', trim(strip_tags($title)));
  406. $src = trim(strip_tags($src));
  407. $text = trim($text);
  408. $desc = trim($desc);
  409. // Now check for empty fields
  410. if(empty($title)) {
  411. $this->last_error = __('Title is empty');
  412. return false;
  413. }
  414. if(empty($src)) $src = "-";
  415. if(empty($text) && empty($desc)) {
  416. $this->last_error = __('Text is empty');
  417. return false;
  418. }
  419. if(empty($desc)) { //mod 2012
  420. $dot = array ('.', '!', '?', '>');
  421. if (mb_strlen($text) > 250) { //добавить настройку обрезки в админку
  422. $string = mb_substr($text, 0, 250);
  423. $last_pos = 0;
  424. foreach ($dot as $delimiter) { // Обрезка по последнему знаку препинания или закрытому тегу
  425. $pos = mb_strrpos ( $string, $delimiter);
  426. if (!empty($pos) && $last_pos<$pos) {
  427. $last_pos = $pos;
  428. $symbol = $delimiter;}
  429. }
  430. if (!isset ($symbol)) $desc = $string . '...'; //Ничего не найдено
  431. else $desc = mb_substr($string, 0, $last_pos) . '...';
  432. } else $desc = text;
  433. }
  434. if(!$new_flag && ($old = $this->getArticle($cat_id, $art_id, false, false, false, false)) === false){
  435. $this->last_error = __('There are no article with this ID');
  436. return false;
  437. }
  438. if(!is_dir($art_prefix)) rcms_mkdir($art_prefix);
  439. // Writing files
  440. if($new_flag){
  441. $add_data = array(
  442. 'author_nick' => $system->user['nickname'],
  443. 'author_name' => $system->user['username'],
  444. 'time' => rcms_get_time(),
  445. );
  446. } else {
  447. $add_data = array(
  448. 'author_nick' => $old['author_nick'],
  449. 'author_name' => $old['author_name'],
  450. 'time' => $old['time'],
  451. );
  452. }
  453. if(!write_ini_file(array_merge(array('title' => $title, 'src' => $src, 'keywords' => strip_tags($keywords), 'sef_desc' => strip_tags($sef_desc), 'comments' => $comments, 'views' => (!$new_flag) ? $old['views'] : 0, 'mode' => $mode, 'comcount' => (!$new_flag) ? $old['comcount'] : 0), $add_data), $art_prefix . 'define') || !file_write_contents($art_prefix . 'short', $desc) || !file_write_contents($art_prefix . 'full', $text)){
  454. $this->last_error = __('Error while saving article');
  455. return false;
  456. }
  457. if($new_flag && !file_write_contents($cat_prefix . 'lst', $art_id)){
  458. $this->last_error = __('Cannot update last article flag');
  459. return false;
  460. }
  461. if($this->container !== '#root' && $this->container !== '#hidden') {
  462. $this->index[$cat_id][$art_id]['time'] = $add_data['time'];
  463. $this->index[$cat_id][$art_id]['ccnt'] = (!$new_flag) ? $old['comcount'] : 0;
  464. $this->index[$cat_id][$art_id]['view'] = (!$new_flag) ? $old['views'] : 0;
  465. if($new_flag) $this->index[$cat_id][$art_id]['lcnt'] = 0;
  466. } else {
  467. $this->index[$art_id]['time'] = $add_data['time'];
  468. $this->index[$art_id]['ccnt'] = (!$new_flag) ? $old['comcount'] : 0;
  469. $this->index[$art_id]['view'] = (!$new_flag) ? $old['views'] : 0;
  470. if($new_flag) $this->index[$art_id]['lcnt'] = 0;
  471. }
  472. $_SESSION['art_id'] = $art_id;
  473. return $this->saveIndex();
  474. }
  475. function moveArticle($cat_id, $art_id, $new_cat_id) {
  476. $cat_id = (int) $cat_id;
  477. $art_id = (int) $art_id;
  478. $new_cat_id = (int) $new_cat_id;
  479. if(empty($this->container)){
  480. $this->last_error = __('No section selected!');
  481. return false;
  482. }
  483. if($this->container == '#root' || $this->container == '#hidden'){
  484. $this->last_error = __('This system section doesn\'t have categories');
  485. return false;
  486. }
  487. $cat_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/';
  488. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  489. if(!is_dir($art_prefix)){
  490. $this->last_error = __('Invalid article');
  491. return false;
  492. }
  493. $ncat_prefix = ARTICLES_PATH . $this->container . '/' . $new_cat_id . '/';
  494. if(!is_dir($ncat_prefix)){
  495. $this->last_error = __('Invalid target category');
  496. return false;
  497. }
  498. $new_art_id = @file_get_contents($ncat_prefix . 'lst') + 1;
  499. $nart_prefix = ARTICLES_PATH . $this->container . '/' . $new_cat_id . '/' . $new_art_id . '/';
  500. rcms_rename_file($art_prefix, $nart_prefix);
  501. file_write_contents($ncat_prefix . 'lst', file_get_contents($ncat_prefix . 'lst') + 1);
  502. $this->index[$new_cat_id][$new_art_id] = $this->index[$cat_id][$art_id];
  503. rcms_remove_index($art_id, $this->index[$cat_id], true);
  504. return $this->saveIndex();
  505. }
  506. function moveArticleToContainer($src_container, $cat_id, $art_id, $new_container, $new_cat_id = 0) {
  507. $cat_id = (int) $cat_id;
  508. $art_id = (int) $art_id;
  509. if(!$this->setWorkContainer($src_container)){
  510. return false;
  511. }
  512. if(!$this->setWorkContainer($new_container)){
  513. return false;
  514. }
  515. if(!$this->setWorkContainer($src_container)){
  516. return false;
  517. }
  518. if($src_container == '#root' || $src_container == '#hidden'){
  519. $cat_prefix = ARTICLES_PATH . $src_container . '/';
  520. $art_prefix = ARTICLES_PATH . $src_container . '/' . $art_id . '/';
  521. } else {
  522. $cat_prefix = ARTICLES_PATH . $src_container . '/' . $cat_id . '/';
  523. $art_prefix = ARTICLES_PATH . $src_container . '/' . $cat_id . '/' . $art_id . '/';
  524. }
  525. if($new_container == '#root' || $new_container == '#hidden'){
  526. $ncat_prefix = ARTICLES_PATH . $new_container . '/';
  527. } else {
  528. $ncat_prefix = ARTICLES_PATH . $new_container . '/' . $new_cat_id . '/';
  529. }
  530. if(!is_dir($art_prefix)){
  531. $this->last_error = __('Invalid article');
  532. return false;
  533. }
  534. if(!is_dir($ncat_prefix)){
  535. $this->last_error = __('Invalid target category');
  536. return false;
  537. }
  538. if($src_container == '#root' || $src_container == '#hidden'){
  539. $data = $this->index[$art_id];
  540. rcms_remove_index($art_id, $this->index, true);
  541. } else {
  542. $data = $this->index[$cat_id][$art_id];
  543. rcms_remove_index($art_id, $this->index[$cat_id], true);
  544. }
  545. $this->saveIndex();
  546. $new_art_id = @file_get_contents($ncat_prefix . 'lst') + 1;
  547. $nart_prefix = $ncat_prefix . $new_art_id . '/';
  548. rcms_rename_file($art_prefix, $nart_prefix);
  549. file_write_contents($ncat_prefix . 'lst', file_get_contents($ncat_prefix . 'lst') + 1);
  550. if(!$this->setWorkContainer($new_container)){
  551. return false;
  552. }
  553. if($new_container == '#root' || $new_container == '#hidden'){
  554. $this->index[$new_art_id] = $data;
  555. } else {
  556. $this->index[$new_cat_id][$new_art_id] = $data;
  557. }
  558. return $this->saveIndex();
  559. }
  560. function deleteArticle($cat_id, $art_id) {
  561. $cat_id = (int) $cat_id;
  562. $art_id = (int) $art_id;
  563. if(empty($this->container)){
  564. $this->last_error = __('No section selected!');
  565. return false;
  566. }
  567. global $system;
  568. if($this->container !== '#root' && $this->container !== '#hidden'){
  569. if(!($category = $this->getCategory($cat_id))){
  570. $this->last_error = __('There are no category with this ID');
  571. return false;
  572. }
  573. if($category['accesslevel'] > (int)@$system->user['accesslevel'] && !$system->checkForRight('-any-')) {
  574. $this->last_error = __('Access denied');
  575. return false;
  576. }
  577. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  578. $art_data = &$this->articles[$this->container][$cat_id][$art_id];
  579. } else {
  580. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  581. $art_data = &$this->articles[$this->container][$art_id];
  582. }
  583. rcms_delete_files($art_prefix, true);
  584. if($this->container !== '#root' && $this->container !== '#hidden') {
  585. rcms_remove_index($art_id, $this->index[$cat_id], true);
  586. unset($this->index[$cat_id][$art_id]);
  587. } else {
  588. rcms_remove_index($art_id, $this->index, true);
  589. }
  590. $this->saveIndex();
  591. return true;
  592. }
  593. //show text link to article
  594. function linktextArticle($text_nonempty, $comcnt, $views) {
  595. if ($this->config['count_comments'] OR $this->config['count_views']){
  596. $text = (($text_nonempty) ? __('Open article') : __('Comments'));
  597. $comcnt = ($this->config['count_comments'] ? $comcnt : '');
  598. $views = ($this->config['count_views'] ? $views : '');
  599. $result = $text . ' (' . $comcnt . (($this->config['count_comments'] && $this->config['count_views'])?'/':'').$views . ')';
  600. } else $result = __('Open article');
  601. return $result;
  602. }
  603. //---------------------------------------------------------------------------------//
  604. // Comments
  605. function getComments($cat_id, $art_id) {
  606. $cat_id = (int) $cat_id;
  607. $art_id = (int) $art_id;
  608. if(empty($this->container)){
  609. $this->last_error = __('No section selected!');
  610. return false;
  611. }
  612. if($this->container !== '#root' && $this->container !== '#hidden'){
  613. if(!($category = $this->getCategory($cat_id))){
  614. return false;
  615. }
  616. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  617. } else {
  618. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  619. }
  620. //New api.comments - 2011 used
  621. return get_last_messages(null, true, false, $art_prefix . 'comments');
  622. }
  623. function addComment($cat_id, $art_id, $text) {
  624. $cat_id = (int) $cat_id;
  625. $art_id = (int) $art_id;
  626. if(empty($this->container)){
  627. $this->last_error = __('No section selected!');
  628. return false;
  629. }
  630. global $system;
  631. if($this->container !== '#root' && $this->container !== '#hidden'){
  632. if(!($category = $this->getCategory($cat_id))){
  633. return false;
  634. }
  635. if($category['accesslevel'] > (int)@$system->user['accesslevel'] && !$system->checkForRight('-any-')) {
  636. $this->last_error = __('Access denied');
  637. return false;
  638. }
  639. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  640. } else {
  641. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  642. }
  643. if(is_file($art_prefix . 'define')){
  644. if($data = @file_get_contents($art_prefix . 'comments')) $data = unserialize($data); else $data = array();
  645. $article_data = rcms_parse_ini_file($art_prefix . 'define');
  646. // Forming new message
  647. $newmesg['author_user'] = $system->user['username'];
  648. $newmesg['author_nick'] = $system->user['nickname'];
  649. $newmesg['time'] = rcms_get_time();
  650. $newmesg['text'] = mb_substr($text, 0, 2048);
  651. $newmesg['author_ip'] = $_SERVER['REMOTE_ADDR']; // Matrix haz you neo ;)
  652. // Including new message
  653. $data[] = $newmesg;
  654. $save = serialize($data);
  655. iF(!file_write_contents($art_prefix . 'comments', serialize($data))){
  656. $this->last_error = __('Cannot write to file');
  657. return false;
  658. }
  659. $article_data['comcount']++;
  660. if($this->config['count_comments']&&!write_ini_file($article_data, $art_prefix . 'define')){
  661. $this->last_error = __('Cannot write to file');
  662. return false;
  663. }
  664. if($this->container !== '#root' && $this->container !== '#hidden') {
  665. $this->index[$cat_id][$art_id]['ccnt']++;
  666. $this->index[$cat_id][$art_id]['lcnt'] = $newmesg['time'];
  667. } else {
  668. $this->index[$art_id]['ccnt']++;
  669. $this->index[$art_id]['lcnt'] = $newmesg['time'];
  670. }
  671. $res = $this->saveIndex();
  672. return $res;
  673. } else {
  674. $this->last_error = __('There are no article with this ID');
  675. return false;
  676. }
  677. }
  678. function deleteComment($cat_id, $art_id, $comment) {
  679. $cat_id = (int) $cat_id;
  680. $art_id = (int) $art_id;
  681. if(empty($this->container)){
  682. $this->last_error = __('No section selected!');
  683. return false;
  684. }
  685. if($this->container !== '#root' && $this->container !== '#hidden'){
  686. if(!($category = $this->getCategory($cat_id))){
  687. return false;
  688. }
  689. $art_prefix = ARTICLES_PATH . $this->container . '/' . $cat_id . '/' . $art_id . '/';
  690. } else {
  691. $art_prefix = ARTICLES_PATH . $this->container . '/' . $art_id . '/';
  692. }
  693. if($data = @unserialize(@file_get_contents($art_prefix . 'comments'))){
  694. if(isset($data[$comment])) {
  695. rcms_remove_index($comment, $data, true);
  696. @file_write_contents($art_prefix . 'comments', serialize($data));
  697. $article_data = rcms_parse_ini_file($art_prefix . 'define');
  698. $article_data['comcount']--;
  699. @write_ini_file($article_data, $art_prefix . 'define');
  700. }
  701. if($this->container !== '#root' && $this->container !== '#hidden') {
  702. $this->index[$cat_id][$art_id]['ccnt']--;
  703. } else {
  704. $this->index[$art_id]['ccnt']--;
  705. }
  706. $res = $this->saveIndex();
  707. return $res;
  708. } else return false;
  709. }
  710. //---------------------------------------------------------------------------------//
  711. // Index file
  712. function getIndex(){
  713. if(empty($this->container)){
  714. $this->last_error = __('No section selected!');
  715. return false;
  716. }
  717. $data = @file_get_contents(ARTICLES_PATH . $this->container . '/index');
  718. if(($this->index = @unserialize($data)) === false){
  719. $this->index = array();
  720. }
  721. return true;
  722. }
  723. function getStat($param, $cat_id = 0, $recent_only = false){
  724. if(empty($this->container)){
  725. $this->last_error = __('No section selected!');
  726. return false;
  727. }
  728. $result = array();
  729. if(!$cat_id && $this->container !== '#root' && $this->container !== '#hidden'){
  730. foreach ($this->index as $cat_id => $arts_data){
  731. foreach ($arts_data as $art_id => $art_data){
  732. $result[$cat_id . '.' . $art_id] = $art_data[$param];
  733. }
  734. }
  735. } else {
  736. if($this->container !== '#root' && $this->container !== '#hidden') $arts_data = &$this->index[$cat_id];
  737. else $arts_data = &$this->index;
  738. if(!empty($arts_data)){
  739. foreach ($arts_data as $art_id => $art_data){
  740. $result[$cat_id . '.' . $art_id] = $art_data[$param];
  741. }
  742. }
  743. }
  744. natsort($result);
  745. $result = array_reverse($result);
  746. if($recent_only) {
  747. if(!empty($result)){
  748. reset($result);
  749. return each($result);
  750. } else {
  751. return false;
  752. }
  753. } else {
  754. return $result;
  755. }
  756. }
  757. function getLimitedStat($param, $limit, $reverse = false){
  758. if(empty($this->container)){
  759. $this->last_error = __('No section selected!');
  760. return false;
  761. }
  762. $result = array();
  763. $return = array();
  764. if($this->container !== '#root' && $this->container !== '#hidden'){
  765. foreach ($this->index as $cat_id => $arts_data){
  766. foreach ($arts_data as $art_id => $art_data){
  767. $result[$cat_id . '.' . $art_id] = $art_data[$param];
  768. }
  769. }
  770. } else {
  771. $arts_data = &$this->index;
  772. if(!empty($arts_data)){
  773. foreach ($arts_data as $art_id => $art_data){
  774. $result[$cat_id . '.' . $art_id] = $art_data[$param];
  775. }
  776. }
  777. }
  778. natsort($result);
  779. if($reverse) {
  780. $result = array_reverse($result);
  781. }
  782. $i = 1;
  783. $limits = explode(',', $limit);
  784. if(sizeof($limits) == 1){
  785. foreach ($result as $k => $v){
  786. if($i <= $limits[0]) $return[$k] = $v;
  787. $i++;
  788. }
  789. } else {
  790. foreach ($result as $k => $v){
  791. if($i <= $limit[1] && $i >= $limits[0]) $return[$k] = $v;
  792. $i++;
  793. }
  794. }
  795. return $return;
  796. }
  797. function saveIndex(){
  798. if(empty($this->container)){
  799. $this->last_error = __('No section selected!');
  800. return false;
  801. }
  802. if(($data = serialize($this->index)) === false){
  803. $this->last_error = __('Error while converting index');
  804. return false;
  805. }
  806. if(!file_write_contents(ARTICLES_PATH . $this->container . '/index', $data)){
  807. $this->last_error = __('Error while saving index');
  808. return false;
  809. }
  810. return true;
  811. }
  812. }
  813. ?>