Source for file api.articles.php

Documentation is available at api.articles.php

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

Documentation generated on Fri, 08 Jun 2007 12:21:16 +0300 by phpDocumentor 1.3.0RC3