/components/com_myblog/model/class.myblog.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012 · PHP · 645 lines · 406 code · 116 blank · 123 comment · 75 complexity · ec8d760980bd1f767300b56c173c53e2 MD5 · raw file

  1. <?php
  2. (defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. DEFINE("MB_SECTION",8);
  4. DEFINE("MB_CATEGORY",18);
  5. class blogContent extends CMSDBTable {
  6. var $id =0;
  7. var $title =null;
  8. var $title_alias =null;
  9. var $introtext =null;
  10. var $images =null;
  11. var $fulltext =null;
  12. var $state =null;
  13. var $sectionid =null;
  14. var $mask =null;
  15. var $catid =null;
  16. var $created =null;
  17. var $created_by =null;
  18. var $created_by_alias=null;
  19. var $modified =null;
  20. var $modified_by =null;
  21. var $checked_out =null;
  22. var $checked_out_time=null;
  23. var $publish_up =null;
  24. var $publish_down =null;
  25. var $urls =null;
  26. var $attribs =null;
  27. var $version =null;
  28. var $parentid =null;
  29. var $ordering =null;
  30. var $metakey =null;
  31. var $metadesc =null;
  32. var $access =null;
  33. var $hits =null;
  34. /* Custom params */
  35. var $permalink = null;
  36. var $tags = null; // string, saperated by comma
  37. var $tagobj = null; // array, obj(id, name) of the tags
  38. var $rating = null;
  39. var $rating_count = null;
  40. function blogContent() {
  41. //$this->created=date( 'Y-m-d H:i:s' );
  42. $this->CMSDBTable( '#__content','id');
  43. }
  44. function autoCloseTags($string) {
  45. // automatically close HTML-Tags
  46. // (usefull e.g. if you want to extract part of a blog entry or news as preview/teaser)
  47. // coded by Constantin Gross <connum at googlemail dot com> / 3rd of June, 2006
  48. // feel free to leave comments or to improve this function!
  49. $donotclose=array('br','img','input'); //Tags that are not to be closed
  50. //prepare vars and arrays
  51. $tagstoclose='';
  52. $tags=array();
  53. //put all opened tags into an array
  54. preg_match_all("/<(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result);
  55. $openedtags=$result[1];
  56. $openedtags=array_reverse($openedtags); //this is just done so that the order of the closed tags in the end will be better
  57. //put all closed tags into an array
  58. preg_match_all("/<\/(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result2);
  59. $closedtags=$result2[1];
  60. //look up which tags still have to be closed and put them in an array
  61. for ($i=0;$i<count($openedtags);$i++) {
  62. if (in_array($openedtags[$i],$closedtags)) { unset($closedtags[array_search($openedtags[$i],$closedtags)]); }
  63. else array_push($tags, $openedtags[$i]);
  64. }
  65. $tags=array_reverse($tags); //now this reversion is done again for a better order of close-tags
  66. //prepare the close-tags for output
  67. for($x=0;$x<count($tags);$x++) {
  68. $add=strtolower(trim($tags[$x]));
  69. if(!in_array($add,$donotclose)) $tagstoclose.='</'.$add.'>';
  70. }
  71. //and finally
  72. return $tagstoclose;
  73. }
  74. function getParagraphCount($text)
  75. {
  76. $position = -1;
  77. $count = 0;
  78. while( ( $position = strpos($text , '</p>' , $position + 1) ) !== false )
  79. {
  80. $count++;
  81. }
  82. return $count;
  83. }
  84. function getBrowseText(&$row){
  85. global $_MY_CONFIG;
  86. if($_MY_CONFIG->get('useIntrotext')){
  87. if(empty($row->fulltext)){
  88. $ending = strpos($row->introtext, '</p>');
  89. $pos=-1;
  90. $pos_array = array();
  91. while (($pos=strpos($row->introtext,'</p>',$pos+1))!==false)
  92. $pos_array[]=$pos;
  93. $pNum = $_MY_CONFIG->get('autoReadmorePCount');
  94. if (count($pos_array) <= $pNum) {
  95. $row->text = $row->introtext;
  96. } else {
  97. $ending = $pos_array[$pNum-1];
  98. $row->introtext = substr($row->introtext, 0, $ending + 4);
  99. $row->introtext = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->introtext));
  100. }
  101. }
  102. else if( !empty($row->fulltext) && empty($row->introtext) )
  103. {
  104. // Strip x paragraphs
  105. $ending = strpos($row->fulltext, '</p>');
  106. $pos=-1;
  107. $pos_array = array();
  108. while (($pos=strpos($row->fulltext,'</p>',$pos+1))!==false)
  109. $pos_array[]=$pos;
  110. $pNum = $_MY_CONFIG->get('autoReadmorePCount');
  111. if (count($pos_array) <= $pNum) {
  112. $row->text = $row->fulltext;
  113. } else {
  114. $ending = $pos_array[$pNum-1];
  115. $row->fulltext = substr($row->fulltext, 0, $ending + 4);
  116. $row->fulltext = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->fulltext));
  117. }
  118. }
  119. // If user set to display introtext but introtext might be empty
  120. // due to the way previous version of My Blog stores the entries.
  121. if( empty($row->introtext) )
  122. {
  123. $row->text = $row->fulltext;
  124. }
  125. else
  126. {
  127. $row->text = $row->introtext;
  128. }
  129. } else{
  130. if(empty($row->introtext))
  131. $row->text = $row->fulltext;
  132. else
  133. $row->text = $row->introtext;
  134. }
  135. // // Set the text content if introtext available, display introtext. Otherwise display fulltext
  136. // if ($row->introtext && trim($row->introtext) != "" && $_MY_CONFIG->get('useIntrotext')=="1"){
  137. // $row->text = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->introtext));
  138. // } else {
  139. // // Split the default intro text length if use intro text is set
  140. // // and no introtext is present
  141. // if($_MY_CONFIG->get('introLength') && $_MY_CONFIG->get('useIntrotext') == "1" && trim($row->introtext) == ""){
  142. // //$row->text = $this->cms->trunchtml->trunchtml($row->fulltext, $_MY_CONFIG->get('introLength'));
  143. //
  144. //
  145. // $ending = strpos($row->fulltext, '</p>');
  146. //
  147. // if ($ending === false) {
  148. // $row->text = $row->fulltext;
  149. // } else {
  150. // echo '<h1>hello'.$row->title.$ending.'</h1>';
  151. //
  152. // $row->text = substr($row->fulltext, 0, $ending + 4);
  153. // $row->text = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->text));
  154. // }
  155. //
  156. //
  157. // }else{
  158. // // If introlength is not set or useIntrotext is disabled then use entire text.
  159. // $row->text = $row->fulltext;
  160. // }
  161. // }
  162. //
  163. // // If text is still empty, use the introtext
  164. // if(empty($row->text)){
  165. // $row->text = $row->introtext;
  166. // }
  167. }
  168. function _splitReadmoreOnSave(){
  169. // During save, the text in the editor will be stored in $this->fulltext.
  170. // If readmore is detected, we split it up and place it in introtext / fulltext
  171. // If it doesn't exists just place it in introtext like the default Joomla.
  172. // we are assuming everything is now in fulltext
  173. $this->fulltext = preg_replace('/<p id="readmore">(.*?)<\\/p>/i', '{readmore}', $this->fulltext);
  174. $pos = strpos($this->fulltext, '{readmore}');
  175. if ($pos === false) {
  176. $this->introtext = $this->fulltext;
  177. $this->fulltext = '';
  178. }
  179. else
  180. {
  181. $this->introtext = substr($this->fulltext, 0, $pos);
  182. $this->fulltext = substr($this->fulltext, $pos + 10);
  183. }
  184. }
  185. // We can load the row using either numeric id or a permalink string
  186. function load($id){
  187. $originalid = $id;
  188. $db =& cmsInstance('CMSDb');
  189. if(is_numeric($id)){
  190. parent::load($id);
  191. } else {
  192. $sql = "SELECT contentid FROM #__myblog_permalinks WHERE permalink='{$id}'";
  193. $db->query($sql);
  194. $id = $db->get_value();
  195. // IF we cannot find it, need to try and convert ':' to '-'. Joomla 1.5
  196. // seems to convert this
  197. if(!$id){
  198. $id = str_replace(':', '-', $originalid);
  199. $sql = "SELECT contentid FROM #__myblog_permalinks WHERE permalink='{$id}'";
  200. $db->query($sql);
  201. $id = $db->get_value();
  202. }
  203. parent::load($id);
  204. }
  205. // get the permalink
  206. if(is_numeric($id) && ($id != 0)){
  207. $db->query("SELECT `permalink` FROM #__myblog_permalinks WHERE `contentid`='{$id}'");
  208. $this->permalink = $db->get_value();
  209. }
  210. // if the fulltext contain the '{readmore}', that means this is the old data and we need to clean them up a bit
  211. // $this->_split_fulltext_readmore();
  212. // @gotcha. if fulltext contain readmore, and {introtext}
  213. $pos = strpos($this->fulltext, '{readmore}');
  214. if ($pos === false) {
  215. } else {
  216. $this->introtext .= substr($this->fulltext, 0, $pos);
  217. $this->fulltext = substr($this->fulltext, $pos + 10);
  218. }
  219. // We store all the text in the introtext if no {readmore} is present.
  220. // Otherwise it is stored in introtext
  221. // and fulltext appropriately.
  222. if(!empty($this->fulltext) && empty($this->introtext)){
  223. $this->introtext = $this->fulltext;
  224. $this->fulltext = '';
  225. }
  226. // // If introtext is empty, we assume all has been stored in fulltext section
  227. // if(!empty($this->introtext)){
  228. // // $this->fulltext = $this->fulltext;
  229. // //else
  230. // $this->fulltext = $this->introtext . '{readmore}' . $this->fulltext;
  231. // echo "<h2>Readmore in fulltext</h2>";
  232. // }
  233. // }
  234. // else
  235. // $this->fulltext = $this->introtext;
  236. // Load the tags into a string
  237. $sql = "SELECT `tag`.`id` ,`tag`.`name` "
  238. ." \n FROM #__myblog_categories as tag , #__myblog_content_categories as c "
  239. ." \n WHERE tag.`id` = c.category AND c.contentid='{$this->id}'";
  240. $db->query($sql);
  241. $this->tagobj = $db->get_object_list();
  242. $tags = array();
  243. if($this->tagobj){
  244. foreach($this->tagobj as $tag){
  245. $tags[] = $tag->name;
  246. }
  247. }
  248. $this->tags = implode(',', $tags);
  249. # Get the rating
  250. $db->query("SELECT *, round( rating_sum / rating_count ) AS rating FROM #__content_rating WHERE content_id='{$this->id}'");
  251. $rating = $db->first_row();
  252. if($rating){
  253. $this->rating = $rating->rating;
  254. $this->rating_count = $rating->rating_count;
  255. }
  256. $cms =& cmsInstance('CMSCore');
  257. //Change all relative url to absolute url
  258. $this->introtext = str_replace('src="images', 'src="'. $cms->get_path('live') .'/images', $this->introtext);
  259. $this->fulltext = str_replace('src="images', 'src="'. $cms->get_path('live') .'/images', $this->fulltext);
  260. // Convert back to htmlentities
  261. $this->title = htmlspecialchars($this->title);
  262. // make sure no bloody {readmore} tag ever
  263. $this->introtext = str_replace('{readmore}', '', $this->introtext);
  264. $this->fulltext = str_replace('{readmore}', '', $this->fulltext);
  265. # Trim all necessary text
  266. $this->introtext = trim($this->introtext);
  267. $this->fulltext = trim($this->fulltext);
  268. $this->permalink = trim($this->permalink);
  269. }
  270. function bind($vars, $editorSave=false){
  271. if(empty($vars))
  272. return;
  273. parent::bind($vars);
  274. // if saving from editor, everything is in the fulltext, need to split it
  275. if($editorSave && empty($vars['introtext'])){
  276. $this->_splitReadmoreOnSave();
  277. }
  278. }
  279. function store(){
  280. global $_MY_CONFIG, $mainframe;
  281. $temp = $this->permalink;
  282. $tags = $this->tags;
  283. $cms =& cmsInstance('CMSCore');
  284. $cms->load('libraries', 'user');
  285. $db =& cmsInstance('CMSDb');
  286. unset($this->permalink);
  287. unset($this->tags);
  288. unset($this->rating);
  289. unset($this->rating_count);
  290. if(empty($this->publish_up)){
  291. $this->publish_up = $this->created;
  292. }
  293. if(empty($this->sectionid)){
  294. $this->sectionid = $_MY_CONFIG->get('postSection');
  295. }
  296. if(empty($this->catid)){
  297. $this->catid = $_MY_CONFIG->get('catid');
  298. }
  299. if(empty($this->created_by)){
  300. $this->created_by = $cms->user->id;
  301. }
  302. if($this->id != NULL && $this->id != 0){
  303. $this->modified = strftime("%Y-%m-%d %H:%M:%S", time() + ( $mainframe->getCfg('offset') * 60 * 60 ));
  304. }
  305. //$this->_splitReadMoreOnSave();
  306. // Decode back to normal
  307. $this->title = html_entity_decode($this->title);
  308. //$this->title = htmlspecialchars($this->title);
  309. parent::store();
  310. // restore custom fields
  311. $this->permalink = $temp;
  312. $this->tags = $tags;
  313. // If the permalink is empty, we need to create them or update if necessary
  314. if(empty($this->permalink)){
  315. $this->permalink = myGetPermalink($this->id);
  316. }
  317. if($this->id != 0){
  318. $sql = "SELECT COUNT(*) FROM `#__myblog_permalinks` WHERE `contentid`='{$this->id}'";
  319. $db->query($sql);
  320. $permalink_exist = $db->get_value();
  321. if($permalink_exist){
  322. $db->query("UPDATE `#__myblog_permalinks` SET `permalink`= '{$this->permalink}' WHERE `contentid`='{$this->id}'");
  323. } else {
  324. $db->query("INSERT INTO `#__myblog_permalinks` (`contentid`, `permalink`) VALUES ({$this->id}, '{$this->permalink}') ");
  325. }
  326. }
  327. // delete all previous tags
  328. // $db->query("DELETE FROM #__myblog_content_categories WHERE contentid='{$this->id}'");
  329. // // Update the tags
  330. // if($this->id != 0 AND !empty($this->tags)){
  331. // myAddTags($this->id, $this->tags);
  332. // // foreach($this->tags as $tag){
  333. // // $insert[] = '(' . $this->id . ',' . $tag
  334. // // }
  335. // // $tagsids_insert= array();
  336. // // foreach($this->tags as $row){
  337. // // $tagsids_insert[] = "({$this->id}, {$row})";
  338. // // }
  339. // //
  340. // // $sql = "INSERT INTO `#__myblog_content_categories` "
  341. // // ."\n (`contentid` ,`category`) VALUES "
  342. // // . implode(', ', $tagsids_insert);
  343. // //
  344. // // $db->query($sql);
  345. // }
  346. }
  347. /**
  348. * Return an array of strings with all the validation error of the given entry.
  349. * The data given will be blogContent object.
  350. *
  351. * If no error is found, return an empty array
  352. */
  353. function validate_save(){
  354. $validate = array();
  355. # Title cannot be empty
  356. if(empty($this->title)){
  357. $validate[] = "Title is empty";
  358. }
  359. # Fulltext area cannot be empty
  360. if(empty($this->fulltext)){
  361. $validate[] = "You cannot save a blank entry. ";
  362. }
  363. # Check if permalink contains any unallowed characters and no duplicate is allowed
  364. if (preg_match('/[!@#$%\^&*\(\)\+=\{\}\[\]|\\<">,\\/\^\*;:\?\']/', $this->permalink)) {
  365. $validate[] = "Permanent link can only contain ASCII alphanumeric characters and.-_ only";
  366. } else {
  367. $db->query("SELECT count(*) from #__myblog_permalinks WHERE permalink='{$this->permalink}' and contentid!={$this->id}");
  368. if ($db->get_value() and $this->permalink != "") {
  369. $validate[] = "Permanent link has already been taken. Please choose a different permanent link.";
  370. }
  371. }
  372. return $validate;
  373. }
  374. }
  375. class MyBlogImages extends CMSDBTable {
  376. var $id =null;
  377. var $filename =null;
  378. var $contentid =null;
  379. var $user_id=null;
  380. function MyBlogImages( &$db ) {
  381. $this->CMSDBTable( '#__myblog_images','id');
  382. }
  383. }
  384. class MyBlogMainFrame { var $_language =null;
  385. var $_access=true;
  386. var $_functions=null;
  387. function MyBlogMainFrame(){
  388. global $_MY_CONFIG;
  389. $cms =& cmsInstance('CMSCore');
  390. $cms->load('libraries', 'user');
  391. if(!$cms->user->id){
  392. $this->_access=false;
  393. }
  394. else
  395. {
  396. if($_MY_CONFIG->get('allowedUser'))
  397. {
  398. $allowedUser=explode(",",$_MY_CONFIG->get('allowedUser'));
  399. if(!in_array($cms->user->username,$allowedUser)){
  400. $this->$_access=false;
  401. }
  402. }
  403. }
  404. $this->_functions=array();
  405. }
  406. function validAccess(){
  407. return $this->_access;
  408. return true;
  409. }
  410. function loadAzVideoBot(){
  411. global $_MAMBOTS;
  412. if(myGetAzVideoBot()){
  413. // Call mambot?
  414. $cms =& cmsInstance('CMSCore');
  415. include_once($cms->get_path('plugins') . '/content/azvideobot.php');
  416. $this->registerFunction('onPrepareContent','mb_videobot',"","1");
  417. }
  418. }
  419. function loadPlugins(){
  420. global $_MAMBOTS;
  421. $cms =& cmsInstance('CMSCore');
  422. $db =& cmsInstance('CMSDb');
  423. $pluginsDir=$cms->get_path('plugins'). '/content/';
  424. $db->query( "SELECT mambots.element,mambots.ordering FROM #__myblog_mambots as mybots, #__mambots as mambots WHERE mybots.mambot_id=mambots.id and mambots.published='1' and mybots.my_published='1' and mambots.folder='content' and mambots.element !='jom_comment_bot' ORDER BY mambots.ordering");
  425. $rows = $db->get_object_list();
  426. if ($rows) {
  427. foreach ($rows as $row) {
  428. // Instead of including the mambots, we allow the mainframe mambot to include for us.
  429. // so that other module that has triggers would not come up with an error.
  430. $row->folder = 'content';
  431. $row->published = '1';
  432. $row->params = null; // no params
  433. @$_GET['task'] = 'view';
  434. @$_GET['option'] = 'com_content';
  435. $_MAMBOTS->loadBot($row->folder, $row->element, $row->published, $row->params);
  436. // $curr_file=$pluginsDir.$row->element.".php";
  437. //
  438. // if (file_exists($curr_file) and is_file($curr_file))
  439. // {
  440. // include_once($curr_file);
  441. // }
  442. }
  443. $mambotsToInit=@$_MAMBOTS->_events['onPrepareContent'];
  444. if ($mambotsToInit)
  445. {
  446. foreach ($mambotsToInit as $mambotToInit)
  447. {
  448. $funcName=$mambotToInit[0];
  449. $this->registerFunction('onPrepareContent',$funcName,"","1");
  450. }
  451. }
  452. $mambotsToInit=@$_MAMBOTS->_events['onBeforeDisplayContent'];
  453. if ($mambotsToInit)
  454. {
  455. foreach ($mambotsToInit as $mambotToInit)
  456. {
  457. $funcName=$mambotToInit[0];
  458. $this->registerFunction('onBeforeDisplayContent',$funcName,"","1");
  459. }
  460. }
  461. $mambotsToInit=@$_MAMBOTS->_events['onAfterDisplayContent'];
  462. if ($mambotsToInit)
  463. {
  464. foreach ($mambotsToInit as $mambotToInit)
  465. {
  466. $funcName=$mambotToInit[0];
  467. $this->registerFunction('onAfterDisplayContent',$funcName,"","1");
  468. }
  469. }
  470. }
  471. $pluginsDir= MY_COM_PATH.'/plugins';
  472. $db->query("SELECT filename,folder from #__myblog_bots");
  473. $rows = $db->get_object_list();
  474. if ($rows) {
  475. foreach ($rows as $row)
  476. {
  477. $curr_plugin_path=$pluginsDir."/".$row->folder."/".$row->filename.".php";
  478. if (is_file($curr_plugin_path)){
  479. include_once($curr_plugin_path);
  480. }
  481. }
  482. }
  483. }
  484. function trigger($triggerPoint,&$row,&$params,$page="0"){
  485. $result = "";
  486. if(isset($this->_functions[$triggerPoint])){
  487. foreach($this->_functions[$triggerPoint] as $func){
  488. $funcName=$func[1];
  489. $published=$func[0];
  490. //echo "<!-- params: $funcName , $published, -->";
  491. //echo "<!-- rowtext: " . htmlspecialchars($row->text) . " -->\n\n";
  492. if ($triggerPoint == "onBeforeDisplayContent" or $triggerPoint == "onAfterDisplayContent"){
  493. if(function_exists($funcName))
  494. $result .= @$funcName($row, $params, $page);
  495. }
  496. else{
  497. if(function_exists($funcName))
  498. $result .= @$funcName($published, $row, $params, $page);
  499. }
  500. //echo "<!-- rowtext AFTER: " . htmlspecialchars($row->text) . " -->\n\n";
  501. }
  502. }
  503. return $result;
  504. }
  505. function registerFunction($triggerPoint,$funcName,$filename,$published=""){
  506. global $database;
  507. $db =& cmsInstance('CMSDb');
  508. if(!isset($this->_functions[$triggerPoint]))
  509. $this->_functions[$triggerPoint]=array();
  510. $func=array();
  511. if ($published=="")
  512. {
  513. $db->query("SELECT published from #__myblog_bots WHERE filename='$filename'");
  514. $published=false;
  515. if (!($row=$database->loadRow())) {
  516. return;
  517. } else {
  518. $published=$row[0];
  519. $func[0]=$published;
  520. $func[1]=$funcName;
  521. $this->_functions[$triggerPoint][]=$func;
  522. }
  523. } else {
  524. $func[0]=$published;
  525. $func[1]=$funcName;
  526. $this->_functions[$triggerPoint][]=$func;
  527. }
  528. }
  529. }