/**
	 * Saves activity
	 *
	 * @param int       $id
	 * @param Activity  $stream
	 * @param UserTable $user
	 * @param UserTable $viewer
	 */
	private function saveActivity( $id, $stream, $user, $viewer )
	{
		global $_CB_framework, $_PLUGINS;

		$cbModerator					=	CBActivity::isModerator( (int) $viewer->get( 'id' ) );

		CBActivity::getTemplate( 'activity', false, false );

		$row							=	new ActivityTable();

		$row->load( (int) $id );

		$canAccess						=	false;

		if ( ! $row->get( 'id' ) ) {
			if ( CBActivity::canCreate( $user, $viewer, $stream ) ) {
				$canAccess				=	true;
			}
		} elseif ( ( ( $row->get( 'type' ) == 'status' ) || ( $row->get( 'subtype' ) == 'status' ) ) && ( $cbModerator || ( $viewer->get( 'id' ) == $row->get( 'user_id' ) ) ) ) {
			$canAccess					=	true;
		}

		if ( ! $canAccess ) {
			header( 'HTTP/1.0 404 Not Found' );
			exit();
		}

		$messageLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'message_limit', 400 ) );
		$showActions					=	(int) $stream->get( 'actions', 1 );
		$actionLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'actions_message_limit', 100 ) );
		$showLocations					=	(int) $stream->get( 'locations', 1 );
		$locationLimit					=	( $cbModerator ? 0 : (int) $stream->get( 'locations_address_limit', 200 ) );
		$showLinks						=	(int) $stream->get( 'links', 1 );
		$linkLimit						=	( $cbModerator ? 0 : (int) $stream->get( 'links_link_limit', 5 ) );
		$showTags						=	(int) $stream->get( 'tags', 1 );

		$message						=	trim( $this->input( 'message', $row->get( 'message', null, GetterInterface::HTML ), GetterInterface::HTML ) );

		// Remove duplicate spaces:
		$message						=	preg_replace( '/ {2,}/i', ' ', $message );
		// Remove duplicate tabs:
		$message						=	preg_replace( '/\t{2,}/i', "\t", $message );
		// Remove duplicate linebreaks:
		$message						=	preg_replace( '/(\r\n|\r|\n){2,}/i', '$1', $message );

		$row->set( 'user_id', $row->get( 'user_id', $viewer->get( 'id' ) ) );

		if ( $stream->get( 'type' ) && ( $stream->get( 'type' ) != 'status' ) ) {
			$row->set( 'type', $row->get( 'type', $stream->get( 'type' ) ) );
			$row->set( 'subtype', $row->get( 'subtype', 'status' ) );

			$parentDefault				=	null;
		} else {
			$row->set( 'type', $row->get( 'type', 'status' ) );

			$parentDefault				=	( $viewer->get( 'id' ) != $user->get( 'user_id' ) ? $user->get( 'user_id' ) : null );
		}

		$row->set( 'item', $row->get( 'item', $stream->get( 'item' ) ) );
		$row->set( 'parent', $row->get( 'parent', $stream->get( 'parent', $parentDefault ) ) );

		if ( $messageLimit && ( cbutf8_strlen( $message ) > $messageLimit ) ) {
			$message					=	cbutf8_substr( $message, 0, $messageLimit );
		}

		$row->set( 'message', $message );

		if ( $showActions ) {
			$action						=	$this->getInput()->subTree( 'actions' );
			$actionId					=	(int) $action->get( 'id', 0, GetterInterface::INT );
			$actionMessage				=	( $actionId ? trim( $action->get( 'message', '', GetterInterface::STRING ) ) : '' );

			// Remove linebreaks:
			$actionMessage				=	str_replace( array( "\n", "\r\n" ), ' ', $actionMessage );
			// Remove duplicate spaces:
			$actionMessage				=	preg_replace( '/ {2,}/i', ' ', $actionMessage );
			// Remove duplicate tabs:
			$actionMessage				=	preg_replace( '/\t{2,}/i', "\t", $actionMessage );

			if ( $actionLimit && ( cbutf8_strlen( $actionMessage ) > $actionLimit ) ) {
				$actionMessage			=	cbutf8_substr( $actionMessage, 0, $actionLimit );
			}

			$actionId					=	( $actionMessage ? $actionId : 0 );

			$newAction					=	array(	'id'		=>	$actionId,
													'message'	=>	( $actionId ? $actionMessage : '' ),
													'emote'		=>	( $actionId ? (int) $action->get( 'emote', 0, GetterInterface::INT ) : 0 )
												);

			$row->params()->set( 'action', $newAction );
		}

		if ( $showLocations ) {
			$location					=	$this->getInput()->subTree( 'location' );
			$locationId					=	(int) $location->get( 'id', 0, GetterInterface::INT );
			$locationPlace				=	( $locationId ? trim( $location->get( 'place', '', GetterInterface::STRING ) ) : '' );
			$locationAddress			=	( $locationId ? trim( $location->get( 'address', '', GetterInterface::STRING ) ) : '' );

			if ( $locationLimit && ( cbutf8_strlen( $locationPlace ) > $locationLimit ) ) {
				$locationPlace			=	cbutf8_substr( $locationPlace, 0, $locationLimit );
			}

			if ( $locationLimit && ( cbutf8_strlen( $locationAddress ) > $locationLimit ) ) {
				$locationAddress		=	cbutf8_substr( $locationAddress, 0, $locationLimit );
			}

			$locationId					=	( $locationPlace ? $locationId : 0 );

			$newLocation				=	array(	'id'		=>	$locationId,
													'place'		=>	( $locationId ? $locationPlace : '' ),
													'address'	=>	( $locationId ? $locationAddress : '' )
												);

			$row->params()->set( 'location', $newLocation );
		}

		if ( $showLinks ) {
			$links						=	$this->getInput()->subTree( 'links' );
			$newLinks					=	array();

			/** @var ParamsInterface[] $links */
			foreach ( $links as $i => $link ) {
				if ( $linkLimit && ( ( $i + 1 ) > $linkLimit ) ) {
					break;
				}

				$linkUrl				=	trim( $link->get( 'url', '', GetterInterface::STRING ) );

				if ( $linkUrl ) {
					$attachment			=	$stream->parser()->attachment( $linkUrl );

					if ( ! $attachment ) {
						continue;
					}

					$linkType			=	$attachment->get( 'type', '', GetterInterface::STRING );

					switch ( $linkType ) {
						case 'video':
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'video' )->subTree( 0 );
							break;
						case 'audio':
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'audio' )->subTree( 0 );
							break;
						case 'image':
						case 'url':
						default:
							$linkMedia	=	$attachment->subTree( 'media' )->subTree( 'image' )->subTree( 0 );
							break;
					}

					$newLinks[]			=	array(	'url'			=>	$linkUrl,
													'text'			=>	null,
													'title'			=>	trim( $link->get( 'title', $attachment->subTree( 'title' )->get( 0, '', GetterInterface::STRING ), GetterInterface::STRING ) ),
													'description'	=>	trim( $link->get( 'description', $attachment->subTree( 'description' )->get( 0, '', GetterInterface::STRING ), GetterInterface::STRING ) ),
													'media'			=>	array(	'url' => $linkMedia->get( 'url', '', GetterInterface::STRING ),
																				'mimetype' => $linkMedia->get( 'mimetype', '', GetterInterface::STRING ),
																				'extension' => $linkMedia->get( 'extension', '', GetterInterface::STRING ),
																				'custom' => ''
																			),
													'type'			=>	$linkType,
													'thumbnail'		=>	$link->get( 'thumbnail', 1, GetterInterface::INT ),
													'internal'		=>	0,
												);
				}
			}

			$row->params()->set( 'links', $newLinks );
		}

		if ( $row->get( 'id' ) ) {
			$row->params()->set( 'modified', $_CB_framework->getUTCDate() );
		}

		$row->set( 'params', $row->params()->asJson() );

		if ( $row->getError() || ( ! $row->check() ) ) {
			header( 'HTTP/1.0 500 Internal Server Error' );
			exit();
		}

		if ( $row->getError() || ( ! $row->store() ) ) {
			header( 'HTTP/1.0 500 Internal Server Error' );
			exit();
		}

		if ( $showTags ) {
			$tagsStream					=	$row->tags( $stream->source() );

			if ( $tagsStream ) {
				$tags					=	$this->input( 'tags', array(), GetterInterface::RAW );

				foreach ( $tagsStream->data() as $tag ) {
					/** @var TagTable $tag */
					if ( ! in_array( $tag->get( 'user' ), $tags ) ) {
						$tag->delete();

						$tagsStream->resetData();
					} else {
						$key			=	array_search( $tag->get( 'user' ), $tags );

						if ( $key !== false ) {
							unset( $tags[$key] );
						}
					}
				}

				foreach ( $tags as $tagUser ) {
					if ( is_numeric( $tagUser ) ) {
						$tagUser		=	(int) $tagUser;
					} else {
						$tagUser		=	Get::clean( $tagUser, GetterInterface::STRING );
					}

					$tag				=	new TagTable();

					$tag->set( 'user_id', (int) $tagsStream->user()->get( 'id' ) );
					$tag->set( 'type', $tagsStream->get( 'type', null, GetterInterface::STRING ) );
					$tag->set( 'subtype', $tagsStream->get( 'subtype', null, GetterInterface::STRING ) );
					$tag->set( 'item', $tagsStream->get( 'item', null, GetterInterface::STRING ) );
					$tag->set( 'parent', $tagsStream->get( 'parent', null, GetterInterface::STRING ) );
					$tag->set( 'user', $tagUser );

					$tag->store();

					$tagsStream->resetData();
				}
			}
		}

		$rows							=	array( &$row );

		if ( $stream->get( 'comments', 1 ) ) {
			CBActivity::preFetchComments( $rows, 'activity' );
		}

		if ( $stream->get( 'tags', 1 ) ) {
			CBActivity::preFetchTags( $rows, 'activity' );
		}

		CBActivity::preFetchUsers( $rows );

		$_PLUGINS->trigger( 'activity_onPushActivity', array( $stream, $row ) );

		echo HTML_cbactivityActivity::showActivity( $rows, $stream, 4, $user, $viewer, $this );

		header( 'HTTP/1.0 200 OK' );
		exit();
	}
Esempio n. 2
0
	/**
	 * @param cbautoactionsActionTable $trigger
	 * @param UserTable $user
	 * @return null|string
	 */
	public function execute( $trigger, $user )
	{
		global $_CB_framework, $_CB_database;

		if ( ! $this->installed() ) {
			if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
				var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NOT_INSTALLED', ':: Action [action] :: CB Activity is not installed', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
			}

			return null;
		}

		$return									=	null;

		foreach ( $trigger->getParams()->subTree( 'activity' ) as $row ) {
			/** @var ParamsInterface $row */
			$mode								=	$row->get( 'mode', 'activity', GetterInterface::STRING );
			$method								=	$row->get( 'method', 'create', GetterInterface::STRING );
			$owner								=	$row->get( 'owner', null, GetterInterface::STRING );

			if ( ! $owner ) {
				$owner							=	(int) $user->get( 'id' );
			} else {
				$owner							=	(int) $trigger->getSubstituteString( $owner );
			}

			$type								=	$trigger->getSubstituteString( $row->get( 'type', null, GetterInterface::STRING ) );
			$subtype							=	$trigger->getSubstituteString( $row->get( 'subtype', null, GetterInterface::STRING ) );
			$item								=	$trigger->getSubstituteString( $row->get( 'item', null, GetterInterface::STRING ) );
			$parent								=	$trigger->getSubstituteString( $row->get( 'parent', null, GetterInterface::STRING ) );

			if ( $mode == 'stream' ) {
				if ( $owner ) {
					$streamUser					=	CBuser::getUserDataInstance( (int) $owner );
				} else {
					$streamUser					=	CBuser::getMyUserDataInstance();
				}

				$source							=	$trigger->getSubstituteString( $row->get( 'source', null, GetterInterface::STRING ) );
				$direction						=	(int) $row->get( 'direction', 0, GetterInterface::INT );

				if ( $row->get( 'stream', 'activity', GetterInterface::STRING ) == 'comments' ) {
					$object						=	new Comments( $source, $streamUser, $direction );

					CBActivity::loadStreamDefaults( $object, $row->subTree( 'comments_stream' ), 'comments_' );
				} else {
					$object						=	new Activity( $source, $streamUser, $direction );

					CBActivity::loadStreamDefaults( $object, $row->subTree( 'activity_stream' ), 'activity_' );
				}

				if ( $type ) {
					$object->set( 'type', $type );
				}

				if ( $subtype ) {
					$object->set( 'subtype', $subtype );
				}

				if ( $item ) {
					$object->set( 'item', $item );
				}

				if ( $parent ) {
					$object->set( 'parent', $parent );
				}

				if ( $row->get( 'output', 'echo', GetterInterface::STRING ) == 'echo' ) {
					echo $object->stream( false );
				} else {
					$return						.=	$object->stream( false );
				}
			} elseif ( $method == 'delete' ) {
				$where							=	array();

				if ( $owner ) {
					$where[]					=	$_CB_database->NameQuote( 'user_id' ) . ' = ' . (int) $owner;
				}

				if ( $type ) {
					$where[]					=	$_CB_database->NameQuote( 'type' ) . ( strpos( $type, '%' ) !== false ? ' LIKE ' : ' = ' ) . $_CB_database->Quote( $type );
				}

				if ( $item ) {
					$where[]					=	$_CB_database->NameQuote( 'item' ) . ' = ' . $_CB_database->Quote( $item );
				}

				if ( $mode != 'hidden' ) {
					if ( $subtype ) {
						$where[]				=	$_CB_database->NameQuote( 'subtype' ) . ( strpos( $type, '%' ) !== false ? ' LIKE ' : ' = ' ) . $_CB_database->Quote( $subtype );
					}

					if ( $parent ) {
						$where[]				=	$_CB_database->NameQuote( 'parent' ) . ' = ' . $_CB_database->Quote( $parent );
					}
				}

				switch ( $mode ) {
					case 'hidden':
						$table					=	'#__comprofiler_plugin_activity_hidden';
						$class					=	'\CB\Plugin\Activity\Table\HiddenTable';
						break;
					case 'tag':
						$table					=	'#__comprofiler_plugin_activity_tags';
						$class					=	'\CB\Plugin\Activity\Table\TagTable';
						break;
					case 'comment':
						$table					=	'#__comprofiler_plugin_activity_comments';
						$class					=	'\CB\Plugin\Activity\Table\CommentTable';
						break;
					case 'activity':
					default:
						$table					=	'#__comprofiler_plugin_activity';
						$class					=	'\CB\Plugin\Activity\Table\ActivityTable';
						break;
				}

				$query							=	'SELECT *'
												.	"\n FROM " . $_CB_database->NameQuote( $table )
												.	( $where ? "\n WHERE " . implode( "\n AND ", $where ) : null );
				$_CB_database->setQuery( $query );
				$objects						=	$_CB_database->loadObjectList( null, $class, array( $_CB_database ) );

				/** @var TableInterface[] $objects */
				foreach ( $objects as $object ) {
					$object->delete();
				}
			} else {
				if ( ! $owner ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NO_OWNER', ':: Action [action] :: CB Activity skipped due to missing owner', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
					}

					continue;
				}

				if ( ! $type ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_NO_TYPE', ':: Action [action] :: CB Activity skipped due to missing type', array( '[action]' => (int) $trigger->get( 'id' ) ) ) );
					}

					continue;
				}

				switch ( $mode ) {
					case 'hidden':
						$object					=	new HiddenTable( $_CB_database );
						break;
					case 'tag':
						$object					=	new TagTable( $_CB_database );
						break;
					case 'comment':
						$object					=	new CommentTable( $_CB_database );
						break;
					case 'activity':
					default:
						$object					=	new ActivityTable( $_CB_database );
						break;
				}

				if ( $item ) {
					$load						=	array( 'user_id' => $owner, 'type' => $type, 'item' => $item );

					if ( $mode != 'hidden' ) {
						if ( $subtype ) {
							$load['subtype']	=	$subtype;
						}

						if ( $parent ) {
							$load['parent']		=	$parent;
						}
					}

					$object->load( $load );
				}

				$object->set( 'user_id', $owner );

				if ( $type ) {
					$object->set( 'type', $type );
				}

				if ( $mode != 'hidden' ) {
					if ( $subtype ) {
						$object->set( 'subtype', $subtype );
					}

					if ( $parent ) {
						$object->set( 'parent', $parent );
					}
				}

				if ( $item ) {
					$object->set( 'item', $item );
				}

				if ( $mode == 'activity' ) {
					$title						=	$trigger->getSubstituteString( $row->get( 'title', null, GetterInterface::RAW ) );

					if ( $title ) {
						$object->set( 'title', $title );
					}

					$date						=	$trigger->getSubstituteString( $row->get( 'date', null, GetterInterface::STRING ) );

					if ( $date ) {
						$object->set( 'date', $_CB_framework->getUTCDate( 'Y-m-d H:i:s', $date ) );
					}

					$action						=	$row->subTree( 'action' );
					$actionId					=	$action->get( 'id', null, GetterInterface::INT );

					if ( $actionId ) {
						$actionMessage			=	$trigger->getSubstituteString( $action->get( 'message', null, GetterInterface::STRING ) );

						if ( $actionMessage ) {
							$newAction			=	array(	'id'		=>	$actionId,
															'message'	=>	$actionMessage,
															'emote'		=>	$action->get( 'emote', '', GetterInterface::STRING )
														);

							$object->params()->set( 'action', $newAction );
						}
					}

					$location					=	$row->subTree( 'location' );
					$locationId					=	$location->get( 'id', null, GetterInterface::INT );

					if ( $locationId ) {
						$locationPlace			=	$trigger->getSubstituteString( $location->get( 'place', null, GetterInterface::STRING ) );

						if ( $locationPlace ) {
							$newLocation		=	array(	'id'		=>	$locationId,
															'place'		=>	$locationPlace,
															'address'	=>	$trigger->getSubstituteString( $location->get( 'address', null, GetterInterface::STRING ) )
														);

							$object->params()->set( 'location', $newLocation );
						}
					}

					$newLinks					=	array();

					foreach ( $row->subTree( 'links' ) as $link ) {
						/** @var ParamsInterface $link */
						$linkType				=	$trigger->getSubstituteString( $link->get( 'type', null, GetterInterface::STRING ) );
						$linkUrl				=	$trigger->getSubstituteString( $link->get( 'url', null, GetterInterface::STRING ) );

						if ( ( ! $linkType ) || ( ! $linkUrl ) ) {
							continue;
						}

						$linkMedia				=	$link->subTree( 'media' );

						$newLinks[]				=	array(	'url'			=>	$linkUrl,
															'text'			=>	$trigger->getSubstituteString( $link->get( 'text', null, GetterInterface::STRING ) ),
															'title'			=>	$trigger->getSubstituteString( $link->get( 'title', null, GetterInterface::STRING ) ),
															'description'	=>	$trigger->getSubstituteString( $link->get( 'description', null, GetterInterface::RAW ) ),
															'media'			=>	array(	'url' => $trigger->getSubstituteString( $linkMedia->get( 'url', null, GetterInterface::STRING ) ),
																						'mimetype' => $trigger->getSubstituteString( $linkMedia->get( 'mimetype', null, GetterInterface::STRING ) ),
																						'extension' => $trigger->getSubstituteString( $linkMedia->get( 'extension', null, GetterInterface::STRING ) ),
																						'custom' => $trigger->getSubstituteString( $linkMedia->get( 'custom', null, GetterInterface::RAW ) )
																					),
															'type'			=>	$linkType,
															'thumbnail'		=>	$link->get( 'thumbnail', 1, GetterInterface::INT ),
															'internal'		=>	$link->get( 'internal', 0, GetterInterface::INT )
														);
					}

					if ( $newLinks ) {
						$object->params()->set( 'links', $newLinks );
					}

					$comments					=	$row->subTree( 'comments' );

					$object->params()->set( 'comments', array(	'display'	=>	(int) $comments->get( 'display', 1, GetterInterface::INT ),
																'source'	=>	(int) $comments->get( 'source', 1, GetterInterface::INT )
															));

					$tags						=	$row->subTree( 'tags' );

					$object->params()->set( 'tags', array(	'display'	=>	(int) $tags->get( 'display', 1, GetterInterface::INT ),
															'source'	=>	(int) $tags->get( 'source', 1, GetterInterface::INT )
														));

					$object->set( 'params', $object->params()->asJson() );
				} elseif ( $mode == 'comment' ) {
					$tags						=	$row->subTree( 'replies' );

					$object->params()->set( 'replies', array( 'display' => (int) $tags->get( 'display', 1, GetterInterface::INT ) ));

					$object->set( 'params', $object->params()->asJson() );
				}

				if ( in_array( $mode, array( 'activity', 'comment' ) ) ) {
					$message					=	$trigger->getSubstituteString( $row->get( 'message', null, GetterInterface::RAW ), false );

					if ( $message ) {
						$object->set( 'message', $message );
					}
				}

				if ( ! $object->store() ) {
					if ( $trigger->getParams()->get( 'debug', false, GetterInterface::BOOLEAN ) ) {
						var_dump( CBTxt::T( 'AUTO_ACTION_ACTIVITY_CREATE_FAILED', ':: Action [action] :: CB Activity failed to save. Error: [error]', array( '[action]' => (int) $trigger->get( 'id' ), '[error]' => $object->getError() ) ) );
					}

					continue;
				}
			}
		}

		return $return;
	}