annamaria-daneswood-25102012 /components/com_myblog/model/class.myblog.php

Language PHP Lines 646
MD5 Hash ec8d760980bd1f767300b56c173c53e2 Estimated Cost $10,458 (why?)
Repository https://bitbucket.org/dgough/annamaria-daneswood-25102012.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<?php 
(defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');

DEFINE("MB_SECTION",8);
DEFINE("MB_CATEGORY",18);

class blogContent extends CMSDBTable { 
	var $id =0;
	var $title =null;
	var $title_alias =null;
	var $introtext =null;
	var $images =null;
	var $fulltext =null;
	var $state =null;
	var $sectionid =null;
	var $mask =null;
	var $catid =null;
	var $created =null;
	var $created_by =null;
	var $created_by_alias=null;	
	var $modified =null;	
	var $modified_by =null;	
	var $checked_out =null;	
	var $checked_out_time=null;	
	var $publish_up =null;	
	var $publish_down =null;	
	var $urls =null;	
	var $attribs =null;	
	var $version =null;	
	var $parentid =null;	
	var $ordering =null;	
	var $metakey =null;	
	var $metadesc =null;	
	var $access =null;
	var $hits =null;	
	
	/* Custom params */
	var $permalink = null;
	var $tags	= null; // string, saperated by comma
	var $tagobj	= null;	// array, obj(id, name) of the tags
	var $rating = null;
	var $rating_count	= null;
	

	function blogContent() { 
		//$this->created=date( 'Y-m-d H:i:s' );
		$this->CMSDBTable( '#__content','id');
	
	}
	
	function autoCloseTags($string) {
		// automatically close HTML-Tags
		// (usefull e.g. if you want to extract part of a blog entry or news as preview/teaser)
		// coded by Constantin Gross <connum at googlemail dot com> / 3rd of June, 2006
		// feel free to leave comments or to improve this function!
		$donotclose=array('br','img','input'); //Tags that are not to be closed
		//prepare vars and arrays
		$tagstoclose='';
		$tags=array();
		//put all opened tags into an array
		preg_match_all("/<(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result);
		$openedtags=$result[1];
		$openedtags=array_reverse($openedtags); //this is just done so that the order of the closed tags in the end will be better
		//put all closed tags into an array
		preg_match_all("/<\/(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result2);
		$closedtags=$result2[1];
		//look up which tags still have to be closed and put them in an array
		for ($i=0;$i<count($openedtags);$i++) {
			if (in_array($openedtags[$i],$closedtags)) { unset($closedtags[array_search($openedtags[$i],$closedtags)]); }
			else array_push($tags, $openedtags[$i]);
		}
		$tags=array_reverse($tags); //now this reversion is done again for a better order of close-tags
		//prepare the close-tags for output
		for($x=0;$x<count($tags);$x++) {
			$add=strtolower(trim($tags[$x]));
			if(!in_array($add,$donotclose)) $tagstoclose.='</'.$add.'>';
		}
		//and finally
		return $tagstoclose;
	}

	function getParagraphCount($text)
	{
		$position = -1;
		$count	  = 0;

		while( ( $position = strpos($text , '</p>' , $position + 1) ) !== false )
		{
			$count++;
		}
			
		return $count;
	}
	
	function getBrowseText(&$row){
		global $_MY_CONFIG;
		
		if($_MY_CONFIG->get('useIntrotext')){
			if(empty($row->fulltext)){
				$ending = strpos($row->introtext, '</p>');
				
				$pos=-1;
				$pos_array = array();
				while (($pos=strpos($row->introtext,'</p>',$pos+1))!==false) 
					$pos_array[]=$pos;
				
				$pNum = $_MY_CONFIG->get('autoReadmorePCount');
				if (count($pos_array) <= $pNum) {
				   $row->text = $row->introtext;
				} else {
					$ending = $pos_array[$pNum-1];
					$row->introtext = substr($row->introtext, 0, $ending + 4);
					$row->introtext = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->introtext));
				}
			}
			else if( !empty($row->fulltext) && empty($row->introtext) )
			{
				// Strip x paragraphs
				
				$ending = strpos($row->fulltext, '</p>');
				
				$pos=-1;
				$pos_array = array();
				while (($pos=strpos($row->fulltext,'</p>',$pos+1))!==false) 
				$pos_array[]=$pos;
				
				$pNum = $_MY_CONFIG->get('autoReadmorePCount');
				if (count($pos_array) <= $pNum) {
					$row->text = $row->fulltext;
				} else {
					$ending = $pos_array[$pNum-1];
					$row->fulltext = substr($row->fulltext, 0, $ending + 4);
					$row->fulltext = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->fulltext));
				}
			}
			
			// If user set to display introtext but introtext might be empty
			// due to the way previous version of My Blog stores the entries.
			if( empty($row->introtext) )
			{
				$row->text = $row->fulltext;
			}
			else
			{
				$row->text = $row->introtext;
			}
		} else{
			if(empty($row->introtext))
				$row->text = $row->fulltext;
			else
				$row->text = $row->introtext;	
		}
		
		
// 		// Set the text content if introtext available, display introtext. Otherwise display fulltext
// 		if ($row->introtext && trim($row->introtext) != "" && $_MY_CONFIG->get('useIntrotext')=="1"){
// 			$row->text = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->introtext));
// 		} else {
// 			// Split the default intro text length if use intro text is set
// 			// and no introtext is present
// 			if($_MY_CONFIG->get('introLength') && $_MY_CONFIG->get('useIntrotext') == "1" && trim($row->introtext) == ""){
// 				//$row->text = $this->cms->trunchtml->trunchtml($row->fulltext, $_MY_CONFIG->get('introLength'));
// 				
// 				
// 				$ending = strpos($row->fulltext, '</p>');
// 				
// 				if ($ending === false) {
// 				   $row->text = $row->fulltext;
// 				} else {
// 					echo '<h1>hello'.$row->title.$ending.'</h1>';
// 					
// 					$row->text = substr($row->fulltext, 0, $ending + 4);
// 					$row->text = myCloseTags(preg_replace('#\s*<[^>]+>?\s*$#','',$row->text));
// 				}
// 	
// 			
// 			}else{
// 				// If introlength is not set or useIntrotext is disabled then use entire text.
// 				$row->text = $row->fulltext;
// 			}
// 		}
// 		
// 		// If text is still empty, use the introtext
// 		if(empty($row->text)){
// 			$row->text = $row->introtext;
// 		}
		
		
	}
	
	function _splitReadmoreOnSave(){
		// During save, the text in the editor will be stored in $this->fulltext.
		// If readmore is detected, we split it up and place it in introtext / fulltext
		// If it doesn't exists just place it in introtext like the default Joomla.
		
		
		// we are assuming everything is now in fulltext
		$this->fulltext = preg_replace('/<p id="readmore">(.*?)<\\/p>/i', '{readmore}', $this->fulltext);
		$pos = strpos($this->fulltext, '{readmore}');
		if ($pos === false) {
			$this->introtext = $this->fulltext;
			$this->fulltext = '';
		} 
		else
		{
			$this->introtext = substr($this->fulltext, 0, $pos);
			$this->fulltext  = substr($this->fulltext, $pos + 10);
		} 
	}
	
	// We can load the row using either numeric id or a permalink string
	function load($id){
		
		$originalid = $id;
		$db =& cmsInstance('CMSDb');
		
		if(is_numeric($id)){
			parent::load($id);
		} else {
			$sql = "SELECT contentid FROM #__myblog_permalinks WHERE permalink='{$id}'";
			$db->query($sql);
			$id = $db->get_value();
			
			// IF we cannot find it, need to try and convert ':' to '-'. Joomla 1.5
			// seems to convert this
			if(!$id){
				$id = str_replace(':', '-', $originalid);
				$sql = "SELECT contentid FROM #__myblog_permalinks WHERE permalink='{$id}'";
				
				$db->query($sql);
				$id = $db->get_value();
			}
			parent::load($id);
		}
		
		// get the permalink
		if(is_numeric($id) && ($id != 0)){
			$db->query("SELECT `permalink` FROM #__myblog_permalinks WHERE `contentid`='{$id}'");
			$this->permalink = $db->get_value();
		}
		
		// if the fulltext contain the '{readmore}', that means this is the old data and we need to clean them up a bit
		// $this->_split_fulltext_readmore();
		// @gotcha. if fulltext contain readmore, and {introtext}
		$pos = strpos($this->fulltext, '{readmore}');
		
		if ($pos === false) {
		} else {
			$this->introtext .= substr($this->fulltext, 0, $pos);
			$this->fulltext  = substr($this->fulltext, $pos + 10);
		}
		
		// We store all the text in the introtext if no {readmore} is present. 
		// Otherwise it is stored in introtext
		// and fulltext appropriately.
 		if(!empty($this->fulltext) && empty($this->introtext)){
 			$this->introtext = $this->fulltext;
 			$this->fulltext = '';
 		}
// 			// If introtext is empty, we assume all has been stored in fulltext section
// 			if(!empty($this->introtext)){
// 			//	$this->fulltext	= $this->fulltext;
// 			//else
// 				$this->fulltext	= $this->introtext . '{readmore}' . $this->fulltext;
// 				echo "<h2>Readmore in fulltext</h2>";
// 			}
// 		}
//		else
//			$this->fulltext	= $this->introtext; 

		// Load the tags into a string
		$sql = "SELECT `tag`.`id` ,`tag`.`name` "
			." \n FROM #__myblog_categories as tag ,  #__myblog_content_categories as c "
			." \n WHERE tag.`id` = c.category AND c.contentid='{$this->id}'";
		$db->query($sql);
		$this->tagobj = $db->get_object_list();
		$tags = array();
		if($this->tagobj){
			foreach($this->tagobj as $tag){
				$tags[] = $tag->name;
			}
		}
		$this->tags = implode(',', $tags);
		
		# Get the rating
		$db->query("SELECT *, round( rating_sum / rating_count ) AS rating FROM #__content_rating WHERE content_id='{$this->id}'");
		$rating = $db->first_row();
		if($rating){
			$this->rating = $rating->rating;
			$this->rating_count = $rating->rating_count;
		}
		
		$cms        =& cmsInstance('CMSCore');
		//Change all relative url to absolute url
		$this->introtext = str_replace('src="images', 'src="'. $cms->get_path('live') .'/images', $this->introtext);
		$this->fulltext  = str_replace('src="images', 'src="'. $cms->get_path('live') .'/images',  $this->fulltext);
		
		// Convert back to htmlentities
		$this->title        = htmlspecialchars($this->title);
		
		// make sure no bloody {readmore} tag ever
		$this->introtext = str_replace('{readmore}', '', $this->introtext);
		$this->fulltext = str_replace('{readmore}', '', $this->fulltext);

		# Trim all necessary text
		$this->introtext = trim($this->introtext);
		$this->fulltext = trim($this->fulltext);
		$this->permalink = trim($this->permalink);
		
		
	}
	
	function bind($vars, $editorSave=false){
		if(empty($vars))
			return;
			
		parent::bind($vars);

		// if saving from editor, everything is in the fulltext, need to split it
		if($editorSave && empty($vars['introtext'])){
			$this->_splitReadmoreOnSave();
		}
	}
	
	function store(){
		global $_MY_CONFIG, $mainframe;
		$temp = $this->permalink;
		$tags = $this->tags;
		
		$cms =& cmsInstance('CMSCore');
		$cms->load('libraries', 'user');
		$db	=& cmsInstance('CMSDb');
		
		unset($this->permalink);
		unset($this->tags);
		unset($this->rating);
		unset($this->rating_count);
		
		
		if(empty($this->publish_up)){
			$this->publish_up = $this->created;
		}
		
		if(empty($this->sectionid)){
			$this->sectionid = $_MY_CONFIG->get('postSection');
		}
		
		if(empty($this->catid)){
			$this->catid = $_MY_CONFIG->get('catid');
		}
		
		if(empty($this->created_by)){
			$this->created_by = $cms->user->id;
		}
		
		if($this->id != NULL && $this->id != 0){
			$this->modified = strftime("%Y-%m-%d %H:%M:%S", time() + ( $mainframe->getCfg('offset') * 60 * 60 ));	
		}
		
		//$this->_splitReadMoreOnSave();
		
		// Decode back to normal
		$this->title    = html_entity_decode($this->title);
		//$this->title = htmlspecialchars($this->title);
		
		parent::store();
		
		
		// restore custom fields
		$this->permalink = $temp;
		$this->tags =  $tags;
		
		// If the permalink is empty, we need to create them or update if necessary
		if(empty($this->permalink)){
			$this->permalink    = myGetPermalink($this->id);
		}
		
		if($this->id != 0){
			$sql = "SELECT COUNT(*) FROM `#__myblog_permalinks` WHERE `contentid`='{$this->id}'";
			$db->query($sql);
			$permalink_exist = $db->get_value();
			
			if($permalink_exist){
				$db->query("UPDATE `#__myblog_permalinks` SET `permalink`= '{$this->permalink}' WHERE `contentid`='{$this->id}'");
			} else {
				$db->query("INSERT INTO `#__myblog_permalinks` (`contentid`, `permalink`) VALUES ({$this->id}, '{$this->permalink}') ");
			}
		}
		
		// delete all previous tags
//		$db->query("DELETE FROM #__myblog_content_categories WHERE contentid='{$this->id}'");
		
// 		// Update the tags
// 		if($this->id != 0 AND !empty($this->tags)){
// 			myAddTags($this->id, $this->tags);
// // 			foreach($this->tags as $tag){
// // 				$insert[]	= '(' . $this->id . ',' . $tag
// // 			}
// // 			$tagsids_insert= array();
// // 			foreach($this->tags as $row){
// // 				$tagsids_insert[] = "({$this->id}, {$row})";
// // 			}
// // 			
// // 			$sql = "INSERT INTO `#__myblog_content_categories` "
// // 				."\n (`contentid` ,`category`) VALUES "
// // 				. implode(', ', $tagsids_insert);
// // 			
// // 			$db->query($sql);
// 		}
	}
	
	/**
	 * Return an array of strings with all the validation error of the given entry.	  	
	 * The data given will be blogContent object.
	 * 
	 * If no error is found, return an empty array	 	 
	 */	 	
	function validate_save(){
		$validate = array();
		
		# Title cannot be empty
		if(empty($this->title)){
			$validate[] = "Title is empty"; 
		} 
		
		# Fulltext area cannot be empty
		if(empty($this->fulltext)){
			$validate[] = "You cannot save a blank entry. "; 
		}
		
		# Check if permalink contains any unallowed characters and no duplicate is allowed
		if (preg_match('/[!@#$%\^&*\(\)\+=\{\}\[\]|\\<">,\\/\^\*;:\?\']/', $this->permalink)) {
			$validate[] = "Permanent link can only contain ASCII alphanumeric characters and.-_ only";
		} else {
			$db->query("SELECT count(*) from #__myblog_permalinks WHERE permalink='{$this->permalink}' and contentid!={$this->id}");
			
			if ($db->get_value() and $this->permalink != "") {
				$validate[] = "Permanent link has already been taken. Please choose a different permanent link.";
			} 
		}
		
		return $validate;
	}
} 

class MyBlogImages extends CMSDBTable { 
	var $id =null;
	var $filename =null;
	var $contentid =null;
	var $user_id=null;
	
	function MyBlogImages( &$db ) {
		$this->CMSDBTable( '#__myblog_images','id');
	} 
} 

class MyBlogMainFrame { var $_language =null;
	var $_access=true;	
	var $_functions=null;
	
	function MyBlogMainFrame(){ 
		global $_MY_CONFIG;
		$cms    =& cmsInstance('CMSCore');
		$cms->load('libraries', 'user');

		if(!$cms->user->id){ 
			$this->_access=false;
		} 
		else 
		{ 
			if($_MY_CONFIG->get('allowedUser'))
			{ 
				$allowedUser=explode(",",$_MY_CONFIG->get('allowedUser'));
				if(!in_array($cms->user->username,$allowedUser)){ 
					$this->$_access=false;
				} 
			} 
		} 
		$this->_functions=array();
	} 
	
	function validAccess(){ 
		return $this->_access;
		return true;
	} 

	function loadAzVideoBot(){
		global $_MAMBOTS;

		if(myGetAzVideoBot()){
			// Call mambot?
			$cms    	=& cmsInstance('CMSCore');
			include_once($cms->get_path('plugins') . '/content/azvideobot.php');

			$this->registerFunction('onPrepareContent','mb_videobot',"","1");
   		}
	}
	
	function loadPlugins(){ 
		global $_MAMBOTS;
		$cms    =& cmsInstance('CMSCore');
		$db 	=& cmsInstance('CMSDb');
		
		$pluginsDir=$cms->get_path('plugins'). '/content/';
		$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");	
		$rows = $db->get_object_list();
	
		if ($rows) { 
			foreach ($rows as $row) { 
			    // Instead of including the mambots, we allow the mainframe mambot to include for us.
			    // so that other module that has triggers would not come up with an error.
			    $row->folder 	= 'content';
			    $row->published = '1';
			    $row->params = null; // no params
			    
				@$_GET['task']  	= 'view';
				@$_GET['option']    = 'com_content';
				$_MAMBOTS->loadBot($row->folder, $row->element, $row->published, $row->params);

// 				$curr_file=$pluginsDir.$row->element.".php";
//
// 				if (file_exists($curr_file) and is_file($curr_file))
// 				{
// 					include_once($curr_file);
// 				}

			}
				
			$mambotsToInit=@$_MAMBOTS->_events['onPrepareContent'];
		
			if ($mambotsToInit) 
			{ 
				foreach ($mambotsToInit as $mambotToInit) 
				{ 
					$funcName=$mambotToInit[0];
		
					$this->registerFunction('onPrepareContent',$funcName,"","1");
				}
			}
			
			$mambotsToInit=@$_MAMBOTS->_events['onBeforeDisplayContent'];
		
			if ($mambotsToInit) 
			{ 
				foreach ($mambotsToInit as $mambotToInit) 
				{ 
					$funcName=$mambotToInit[0];
		
					$this->registerFunction('onBeforeDisplayContent',$funcName,"","1");
				}
			}
			
			$mambotsToInit=@$_MAMBOTS->_events['onAfterDisplayContent'];
		
			if ($mambotsToInit) 
			{ 
				foreach ($mambotsToInit as $mambotToInit) 
				{ 
					$funcName=$mambotToInit[0];
		
					$this->registerFunction('onAfterDisplayContent',$funcName,"","1");
				}
			}
		} 
		
		$pluginsDir= MY_COM_PATH.'/plugins';
		
		$db->query("SELECT filename,folder from #__myblog_bots");
		$rows = $db->get_object_list();
		
		if ($rows) { 
			foreach ($rows as $row) 
			{ 
				$curr_plugin_path=$pluginsDir."/".$row->folder."/".$row->filename.".php";
		
				if (is_file($curr_plugin_path)){ 
					include_once($curr_plugin_path);
				} 
			} 
		}
	} 
	
	function trigger($triggerPoint,&$row,&$params,$page="0"){
		
		$result = "";
		if(isset($this->_functions[$triggerPoint])){ 
			foreach($this->_functions[$triggerPoint] as $func){ 
				$funcName=$func[1];
				$published=$func[0];
				//echo "<!-- params: $funcName , $published, -->";
				//echo "<!-- rowtext: " . htmlspecialchars($row->text) . " -->\n\n";
				if ($triggerPoint == "onBeforeDisplayContent" or $triggerPoint == "onAfterDisplayContent"){
					if(function_exists($funcName))
					    $result .= @$funcName($row, $params, $page);
				}
				else{
					if(function_exists($funcName))
					    $result .= @$funcName($published, $row, $params, $page);
				}
				//echo "<!-- rowtext AFTER: " . htmlspecialchars($row->text) . " -->\n\n";
			} 
		}
		return $result;
	} 
	
	
	function registerFunction($triggerPoint,$funcName,$filename,$published=""){ 
		global $database;
		
		$db =& cmsInstance('CMSDb');
		
		if(!isset($this->_functions[$triggerPoint])) 
			$this->_functions[$triggerPoint]=array();
		
		$func=array();
		
		if ($published=="") 
		{ 
			$db->query("SELECT published from #__myblog_bots WHERE filename='$filename'");
			
			$published=false;
		
			if (!($row=$database->loadRow())) { 
				return;
			} else { 
				$published=$row[0];
		
				$func[0]=$published;
				
				$func[1]=$funcName;
				
				$this->_functions[$triggerPoint][]=$func;
				
			} 
		} else { 
			$func[0]=$published;
		
			$func[1]=$funcName;
			
			$this->_functions[$triggerPoint][]=$func;
			
		}
	}
} 
Back to Top