PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/paproject/web/includes/blocks/createcontent.php

https://github.com/paragjagdale/people-aggregator
PHP | 343 lines | 254 code | 34 blank | 55 comment | 87 complexity | 3a60dbbc16ae267b22bc100cbf3c9c37 MD5 | raw file
  1. <?php
  2. /** !
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * [filename] is a part of PeopleAggregator.
  5. * [description including history]
  6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. * @author [creator, or "Original Author"]
  8. * @license http://bit.ly/aVWqRV PayAsYouGo License
  9. * @copyright Copyright (c) 2010 Broadband Mechanics
  10. * @package PeopleAggregator
  11. */
  12. ?>
  13. <?php
  14. //TO DO: while saving content save function should be called once
  15. //variable for Blog save should be according to criteria specified
  16. require_once "api/BlogPost/BlogPost.php";
  17. require_once "api/Contribution/Contribution.php";
  18. require_once "api/Suggestion/Suggestion.php";
  19. require_once "web/includes/functions/auto_email_notify.php";
  20. require_once "api/Activities/Activities.php";
  21. require_once "api/api_constants.php";
  22. // echo "_POST <pre>".print_r($_POST,1)."</pre>"; exit;
  23. $user = get_user();
  24. if (isset($_POST['publish']) && $content_type == 'BlogPost') {
  25. if (!empty($_POST['attach_media_html'])) {
  26. $_POST["description"] .= "\n<br clear=\"all\"/><br /><br />\n".$_POST['attach_media_html'];
  27. }
  28. /* data_array is used to populate the form with the values in case of error */
  29. $data_array["blog_title"] = trim($_POST["blog_title"]);
  30. filter_all_post($_POST);
  31. $valid_post_types = array('BlogPost', 'Contribution', 'Suggestion');
  32. $type = (isset($_POST) && isset($_POST['blog_type']) && in_array($_POST['blog_type'], $valid_post_types))
  33. ? $_POST['blog_type'] : 'BlogPost';
  34. $pattern = '/(https?:\/\/)?(((www\.)?([a-zA-Z0-9_\.\-]*)\b\.[a-z]{2,4}(\.[a-z]{2})?)|(localhost))(:[0-9]*)?((\/[a-zA-Z0-9_\-\.]*)+)?(\.[a-z]*)?(\?\S+)?/';
  35. $redirect = (isset($_POST) && isset($_POST['redirect']) && preg_match($pattern, $_POST['redirect']))
  36. ? $_POST['redirect'] : '';
  37. $data_array['blog_type'] = trim($type);
  38. $data_array["description"] = trim($_POST["description"]);
  39. $data_array["tags"] = trim($_POST["tags"]);
  40. $error = FALSE;
  41. $post_err = "";
  42. if(preg_match_all('#<([^>]+)>#i', $data_array["blog_title"], $matches)) {
  43. $error = TRUE;
  44. $post_err.= "Title contains illegal HTML code: <br />";
  45. $found_tags = array();
  46. foreach($matches[1] as $html_tag) {
  47. if( 0 !== strpos(trim($html_tag), "/")) {
  48. $post_err.= htmlspecialchars("<$html_tag>") . "<br />";
  49. }
  50. }
  51. }
  52. if (empty($data_array["blog_title"])) {
  53. $error = TRUE;
  54. $post_err = "Post Title cannot be empty.<br />";
  55. }
  56. if (empty($data_array["description"]) ) {
  57. $error = TRUE;
  58. $post_err.= "Description cannot be empty.<br />";
  59. }
  60. // if no error then do the rest of work
  61. if ( !$error ) {
  62. ////////////get tags
  63. $terms = array();
  64. $tags = preg_split('/\s*,\s*/' , strtolower($_POST['tags']));
  65. $tags = array_unique($tags);
  66. foreach ($tags as $term) {
  67. $tr = trim($term);
  68. if ($tr) {
  69. $terms[] = $tr;
  70. }
  71. }
  72. /////////////////
  73. // check to see if user wants to edit the post
  74. // now just edit and redirect to permalink
  75. $track = null; // NOTE: by Zoran Hron; trackback never has been used !?
  76. if( !empty($cid) ) {
  77. $condition = array('content_id' => $cid);
  78. $is_active = ACTIVE;
  79. if (PA::is_moderated_content())
  80. $content = Content::load_all_content_for_moderation(NULL, $condition);
  81. if (!empty($content)) {
  82. $is_active = $content[0]['is_active'];
  83. }
  84. switch($type) {
  85. case 'BlogPost':
  86. default:
  87. $r = BlogPost::save_blogpost($cid, PA::$login_uid, $_POST["blog_title"], $_POST["description"], $track, $terms, -1, $is_active);
  88. break;
  89. case 'Contribution':
  90. $r = Contribution::save_contribution($cid, PA::$login_uid, $_POST["blog_title"], $_POST["description"], $track, $terms, -1, $is_active);
  91. if($type == 'Contribution' && $redirect != '') {
  92. $url_parts = parse_url($redirect);
  93. parse_str($url_parts['query'], $query_args);
  94. // save extra information about CC Contributions
  95. $contribution_id = null;
  96. $contribution_type = null;
  97. $contribution_title = null;
  98. if(isset($query_args['conversation_id'])) {
  99. $contribution_type = 'conversation';
  100. $contribution_id = $query_args['conversation_id'];
  101. }
  102. if(isset($query_args['issue_id'])) {
  103. $contribution_type = 'issue';
  104. $contribution_id = $query_args['issue_id'];
  105. }
  106. if(isset($query_args['title'])) {
  107. $contribution_title = $query_args['title'];
  108. unset($query_args['title']);
  109. }
  110. if(isset($contribution_id) && isset($contribution_type)) {
  111. $res = Dal::query("INSERT INTO {cc_contributions} (content_id, contribution_id, type, title) VALUES (?, ?, ?, ?)", array(intval($r['cid']), $contribution_id, $contribution_type, $contribution_title));
  112. }
  113. if(!isset($query_args['title'])) {
  114. $query_args['title'] = $_POST["blog_title"];
  115. }
  116. if(!isset($query_args['link'])) {
  117. $query_args['link'] = PA::$url.'/content/cid='.$r['cid'];
  118. }
  119. $redirect = $url_parts['scheme'].'://'.$url_parts['host'];
  120. $redirect .= (isset($url_parts['port']) && $url_parts['port'] != '' && $url_parts['port'] != '80') ? ':'.$url_parts['port'] : '';
  121. $redirect .= $url_parts['path'].'?'.http_build_query($query_args);
  122. }
  123. break;
  124. case 'Suggestion':
  125. $r = Suggestion::save_suggestion($cid, PA::$login_uid, $_POST["blog_title"], $_POST["description"], $track, $terms, -1, $is_active);
  126. break;
  127. }
  128. if($r['cid'] == $cid) {
  129. $login_required_str = null;
  130. $content_author_image = uihelper_resize_mk_user_img($user->picture, 80, 80,'alt="'.$user->first_name.'" align="left" style="padding: 0px 12px 12px 0px;"');
  131. if(PA::is_moderated_content()) {
  132. $login_required_str = '&login_required=true';
  133. }
  134. /*
  135. $network_owner = new User();
  136. $network_owner->load((int)PA::$network_info->owner_id);
  137. $network_owner_name = User::map_ids_to_logins(PA::$network_info->owner_id);
  138. $params['recipient_username'] = $network_owner->login_name;
  139. $params['recipient_firstname'] = $network_owner->first_name;
  140. $params['recipient_lastname'] = $network_owner->last_name;
  141. $params['cid'] = $r['cid'];
  142. $params['first_name'] = $user->first_name;
  143. $params['user_id'] = $user->user_id;
  144. $params['user_image'] = $content_author_image;
  145. $params['content_title'] = $_POST["blog_title"];
  146. $params['network_name'] = PA::$network_info->name;
  147. $_content_url = PA::$url . PA_ROUTE_CONTENT . '/cid='.$r['cid'].$login_required_str;
  148. $params['content_url'] = "<a href=\"$_content_url\">$_content_url</a>";
  149. $_content_moderation_url = PA::$url.'/'.FILE_NETWORK_MANAGE_CONTENT;
  150. $params['content_moderation_url'] = "<a href=\"$_content_moderation_url\">$_content_moderation_url</a>";
  151. $params['config_site_name'] = PA::$site_name;
  152. $params['network_owner_name'] = $network_owner_name[PA::$network_info->owner_id];
  153. auto_email_notification('content_modified', $params);
  154. */
  155. $content_obj = Content::load_content((int)$r['cid']);
  156. PANotify::send("content_modified", PA::$network_info, $user, $content_obj);
  157. //for rivers of people
  158. $activity = 'content_modified';
  159. $activity_extra['info'] = ($user->first_name.' modified blog post');
  160. $activity_extra['blog_name'] = $_POST["blog_title"];
  161. $activity_extra['blog_id'] = $r['cid'];
  162. $activity_extra['blog_url'] = PA::$url . PA_ROUTE_CONTENT . '/cid='.$r['cid'].$login_required_str;
  163. $extra = serialize($activity_extra);
  164. $object = $r['cid'];
  165. if(!PA::is_moderated_content()) {//Write to activity log only when moderation is off
  166. Activities::save($user->user_id, $activity, $object,$extra);
  167. }
  168. }
  169. //invalidate cache
  170. if( PA::$network_info ) {
  171. $nid = '_network_'.PA::$network_info->network_id;
  172. } else {
  173. $nid='';
  174. }
  175. //unique name
  176. $cache_id = 'content_'.$cid.$nid;
  177. CachedTemplate::invalidate_cache($cache_id);
  178. if (PA::is_moderated_content()) {
  179. $error_msg = '&msg_id=1004';
  180. } else {
  181. $error_msg = '&msg_id=7027';
  182. }
  183. $location = PA::$url . PA_ROUTE_CONTENT . "/cid=$cid".$error_msg;
  184. $location = (isset($redirect) && $redirect != '') ? $redirect : $location;
  185. header("location:$location");exit;
  186. }//.. end of edit
  187. // If we have come this far it means it is not edit and we have to create post
  188. //save post normally
  189. if (isset($_POST['route_to_pa_home']) && $_POST['route_to_pa_home'] == 1) {
  190. $display_on_homepage = DISPLAY_ON_HOMEPAGE;//its zero
  191. } else {
  192. $display_on_homepage = NO_DISPLAY_ON_HOMEPAGE;//This will not show up on homepage - flag has opposite values
  193. }
  194. $ccid = -1;
  195. if (!empty(PA::$config->simple['omit_routing'])) {
  196. $ccid = (!empty($_REQUEST['ccid'])) ? $_REQUEST['ccid'] : -1;
  197. }
  198. switch($type) {
  199. case 'BlogPost':
  200. default:
  201. $post_saved = BlogPost::save_blogpost(0, PA::$login_uid, $_POST["blog_title"], $_POST["description"], NULL, $terms, $ccid, 1, $display_on_homepage);
  202. break;
  203. case 'Contribution':
  204. $post_saved = Contribution::save_contribution(0, PA::$login_uid, $_POST["blog_title"], $_POST["description"], NULL, $terms, $ccid, 1, $display_on_homepage);
  205. if($type == 'Contribution' && $redirect != '') {
  206. $url_parts = parse_url($redirect);
  207. parse_str($url_parts['query'], $query_args);
  208. // save extra information about CC Contributions
  209. $contribution_id = null;
  210. $contribution_type = null;
  211. $contribution_title = null;
  212. if(isset($query_args['conversation_id'])) {
  213. $contribution_type = 'conversation';
  214. $contribution_id = $query_args['conversation_id'];
  215. }
  216. if(isset($query_args['issue_id'])) {
  217. $contribution_type = 'issue';
  218. $contribution_id = $query_args['issue_id'];
  219. }
  220. if(isset($query_args['title'])) {
  221. $contribution_title = $query_args['title'];
  222. unset($query_args['title']);
  223. }
  224. if(isset($contribution_id) && isset($contribution_type)) {
  225. $res = Dal::query("INSERT INTO {cc_contributions} (content_id, contribution_id, type, title) VALUES (?, ?, ?, ?)", array(intval($post_saved['cid']), $contribution_id, $contribution_type, $contribution_title));
  226. }
  227. if(!isset($query_args['title'])) {
  228. $query_args['title'] = $_POST["blog_title"];
  229. }
  230. if(!isset($query_args['link'])) {
  231. $query_args['link'] = PA::$url.'/content/cid='.$post_saved['cid'];
  232. }
  233. $redirect = $url_parts['scheme'].'://'.$url_parts['host'];
  234. $redirect .= (isset($url_parts['port']) && $url_parts['port'] != '' && $url_parts['port'] != '80') ? ':'.$url_parts['port'] : '';
  235. $redirect .= $url_parts['path'].'?'.http_build_query($query_args);
  236. }
  237. break;
  238. case 'Suggestion':
  239. $post_saved = Suggestion::save_suggestion(0, PA::$login_uid, $_POST["blog_title"], $_POST["description"], NULL, $terms, $ccid, 1, $display_on_homepage);
  240. break;
  241. }
  242. $permalink_cid = $post_saved['cid'];
  243. if (PA::is_moderated_content() && PA::$network_info->owner_id != $user->user_id) {
  244. Network::moderate_network_content(-1, $permalink_cid);// -1 for contents; not a part of any collection
  245. $error_msg = "&err=".urlencode(MessagesHandler::get_message(1004));
  246. }
  247. $login_required_str = null;
  248. if(PA::is_moderated_content()) {
  249. $login_required_str = '&login_required=true';
  250. }
  251. $content_obj = Content::load_content((int)$permalink_cid);
  252. PANotify::send("content_posted", PA::$network_info, $user, $content_obj);
  253. if ($display_on_homepage == DISPLAY_ON_HOMEPAGE) {
  254. PANotify::send("content_posted_to_comm_blog", PA::$network_info, $user, $content_obj);
  255. }
  256. //for rivers of people
  257. $activity = 'user_post_a_blog';
  258. $activity_extra['info'] = $user->first_name.'posted a new blog';
  259. $activity_extra['blog_name'] = $_POST["blog_title"];
  260. $activity_extra['blog_id'] = $permalink_cid;
  261. $activity_extra['blog_url'] = PA::$url . PA_ROUTE_CONTENT . '/cid=' . $permalink_cid . $login_required_str;
  262. $extra = serialize($activity_extra);
  263. $object = $permalink_cid;
  264. if (!PA::is_moderated_content()) {//Write to activity log only when moderation is off
  265. Activities::save($user->user_id, $activity, $object,$extra);
  266. }
  267. if (empty(PA::$config->simple['omit_routing'])) {
  268. //save post in groups
  269. $routed_to_groups = route2groups();
  270. }
  271. // save post to outputthis
  272. route_to_outputthis($_POST["blog_title"], $_POST["description"]);
  273. //we have saved it in all the locations lets redirect it to various locations
  274. if (!empty($_GET['ccid'])) {
  275. $gid = $_GET['ccid'];
  276. $group = ContentCollection::load_collection((int)$gid, PA::$login_uid);
  277. $is_member = Group::get_user_type((int)PA::$login_uid, (int)$gid);
  278. if ( $is_member == NOT_A_MEMBER) {
  279. $msg = "&msg_id=7028";
  280. } else {
  281. if(($group->reg_type == REG_MODERATED) || (PA::$extra['network_content_moderation'] == NET_YES)) {
  282. $msg = "&msg_id=1004";
  283. } else {
  284. $msg = "&msg_id=7027";
  285. }
  286. }
  287. // it means user is coming from group's page then redirect it to group
  288. //load group to see if group is if it is moderated
  289. $location = PA::$url . PA_ROUTE_GROUP . "/gid=".$_REQUEST['ccid'].$msg;
  290. $location = (isset($redirect) && $redirect != '') ? $redirect : $location;
  291. header("location:$location");exit;
  292. } else {
  293. //just redirect it to permalink page
  294. if (PA::is_moderated_content()) {
  295. $error_msg = "&msg_id=1004";
  296. } else {
  297. $error_msg = "&msg_id=7027";
  298. }
  299. // header("location:".PA::$url . PA_ROUTE_CONTENT . "/cid=".$permalink_cid.$error_msg);exit;
  300. $location = PA::$url .PA_ROUTE_USER_PRIVATE."?cid=".$permalink_cid.$error_msg;
  301. $location = (isset($redirect) && $redirect != '') ? $redirect : $location;
  302. header("location:$location");exit;
  303. }
  304. }
  305. else {//..end of !$error
  306. $post_err = 'Post could not be saved due to following errors:<br>'.$post_err;
  307. }
  308. }//$_POST
  309. ?>