api.articles.php 29 KB

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