PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/preview.php

https://github.com/chrisgraham/ocPortal
PHP | 308 lines | 250 code | 32 blank | 26 comment | 70 complexity | 84320ac2c151265be2cdcdf77a9d40ff MD5 | raw file
  1. <?php /*
  2. ocPortal
  3. Copyright (c) ocProducts, 2004-2012
  4. See text/EN/licence.txt for full licencing information.
  5. NOTE TO PROGRAMMERS:
  6. Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
  7. **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****
  8. */
  9. /**
  10. * @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
  11. * @copyright ocProducts Ltd
  12. * @package core_form_interfaces
  13. */
  14. /**
  15. * Build up a preview based on what was submitted.
  16. *
  17. * @param boolean Whether to return additional data
  18. * @return mixed Either tempcode for the preview, or a tuple of details
  19. */
  20. function build_preview($multi_return=false)
  21. {
  22. // Check CAPTCHA if it is passed
  23. if (addon_installed('captcha'))
  24. {
  25. if (((array_key_exists('post',$_POST)) && ($_POST['post']!='')) && (array_key_exists('security_image',$_POST)))
  26. {
  27. require_code('captcha');
  28. enforce_captcha(false);
  29. }
  30. }
  31. require_code('attachments2');
  32. $hooks=find_all_hooks('systems','preview');
  33. $output=NULL;
  34. $new_post_value=NULL;
  35. $attachment_type=NULL;
  36. $forum_db=false;
  37. $limit_to=NULL;
  38. foreach (array_keys($hooks) as $hook)
  39. {
  40. require_code('hooks/systems/preview/'.$hook);
  41. $object=object_factory('Hook_Preview_'.$hook,true);
  42. if (is_null($object)) continue;
  43. $apply_bits=$object->applies();
  44. $applies=$apply_bits[0];
  45. if ($applies)
  46. {
  47. $attachment_type=$apply_bits[1];
  48. $forum_db=array_key_exists(2,$apply_bits)?$apply_bits[2]:false;
  49. $limit_to=array_key_exists(3,$apply_bits)?$apply_bits[3]:NULL;
  50. if (method_exists($object,'run')) list($output,$new_post_value)=$object->run();
  51. break;
  52. }
  53. }
  54. $validation=new ocp_tempcode();
  55. $keyword_density=new ocp_tempcode();
  56. $spelling=new ocp_tempcode();
  57. $meta_keywords=post_param('meta_keywords','');
  58. $spellcheck=post_param_integer('perform_spellcheck',0)==1;
  59. $keywordcheck=(post_param_integer('perform_keywordcheck',0)==1) && ($meta_keywords!='');
  60. if (post_param_integer('perform_validation',0)!=0)
  61. {
  62. foreach ($_POST as $key=>$val)
  63. {
  64. if (!is_string($val)) continue;
  65. $val=post_param($key,''); // stripslashes, and wysiwyg output handling
  66. $tempcodecss=(post_param_integer('tempcodecss__'.$key,0)==1);
  67. $supports_comcode=(post_param_integer('comcode__'.$key,0)==1);
  68. if ($supports_comcode)
  69. {
  70. $temp=$_FILES;
  71. $_FILES=array();
  72. $valt=comcode_to_tempcode($val);
  73. $_FILES=$temp;
  74. require_code('view_modes');
  75. require_code('obfuscate');
  76. require_code('validation');
  77. $validation->attach(do_xhtml_validation($valt->evaluate(),false,post_param_integer('perform_validation',0),true));
  78. } elseif ($tempcodecss)
  79. {
  80. $i=0;
  81. $color=post_param(strval($i),'');
  82. while ($color!='')
  83. {
  84. $val=str_replace('<color-'.strval($i).'>','#'.$color,$val);
  85. $i++;
  86. $color=post_param(strval($i),'');
  87. }
  88. $_val_orig=$val;
  89. require_lang('validation');
  90. require_css('adminzone');
  91. require_code('view_modes');
  92. require_code('obfuscate');
  93. require_code('validation');
  94. require_code('validation2');
  95. $error=check_css($_val_orig);
  96. $show=(count($error['errors'])!=0);
  97. if ($show)
  98. $validation->attach(display_validation_results($_val_orig,$error,true,true));
  99. }
  100. }
  101. }
  102. if ($spellcheck)
  103. {
  104. if (addon_installed('wordfilter'))
  105. {
  106. $words_skip=collapse_1d_complexity('w_replacement',$GLOBALS['SITE_DB']->query_select('wordfilter',array('w_replacement')));
  107. } else
  108. {
  109. $words_skip=array();
  110. }
  111. require_once(get_file_base().'/data/areaedit/plugins/SpellChecker/spell-check-logic.php');
  112. }
  113. $db=$forum_db?$GLOBALS['FORUM_DB']:$GLOBALS['SITE_DB'];
  114. $view_space_map=array();
  115. require_code('templates_view_space');
  116. foreach ($_POST as $key=>$val)
  117. {
  118. if (!is_string($val)) continue;
  119. if ((!is_null($limit_to)) && (!in_array($key,$limit_to))) continue;
  120. $val=post_param($key,''); // stripslashes, and wysiwyg output handling
  121. if ($val=='0') $val=do_lang('NO');
  122. if ($val=='1') $val=do_lang('YES');
  123. if ((substr($key,0,14)=='review_rating') || (substr($key,0,7)=='rating')) $val.='/10';
  124. $is_hidden=in_array($key,array('from_url','password','confirm_password','edit_password','MAX_FILE_SIZE','perform_validation','_validated','id','posting_ref_id','f_face','f_colour','f_size','http_referer')) || (strpos($key,'hour')!==false) || (strpos($key,'access_')!==false) || (strpos($key,'minute')!==false) || (strpos($key,'confirm')!==false) || (strpos($key,'pre_f_')!==false) || (strpos($key,'label_for__')!==false) || (strpos($key,'wysiwyg_version_of_')!==false) || (strpos($key,'is_wysiwyg')!==false) || (strpos($key,'require__')!==false) || (strpos($key,'tempcodecss__')!==false) || (strpos($key,'comcode__')!==false) || (strpos($key,'_parsed')!==false) || (preg_match('#^caption\d+$#',$key)!=0) || (preg_match('#^attachmenttype\d+$#',$key)!=0) || (substr($key,0,1)=='_') || (substr($key,0,9)=='hidFileID') || (substr($key,0,11)=='hidFileName');
  125. if (substr($key,0,14)=='tick_on_form__')
  126. {
  127. if (post_param_integer(substr($key,14),0)==1) $is_hidden=true; else $key=substr($key,14);
  128. }
  129. if (substr($key,-4)=='_day')
  130. {
  131. $key=substr($key,0,strlen($key)-4);
  132. $timestamp=get_input_date($key);
  133. if (is_null($timestamp))
  134. {
  135. $is_hidden=true;
  136. } else
  137. {
  138. $val=get_timezoned_date($timestamp,false,true,false,true);
  139. }
  140. }
  141. elseif ((substr($key,-6)=='_month') || (substr($key,-5)=='_year')) $is_hidden=true;
  142. $key_nice=post_param('label_for__'.$key,ucwords(str_replace('_',' ',$key)));
  143. if ($key_nice=='') $is_hidden=true;
  144. if (!$is_hidden)
  145. {
  146. if ($spellcheck)
  147. {
  148. require_code('comcode_from_html');
  149. $mispellings=spellchecklogic('check',strip_comcode(semihtml_to_comcode($val,true)),$words_skip,true);
  150. $_misspellings=array();
  151. foreach ($mispellings as $misspelling)
  152. {
  153. list($word_bad,$words_good)=$misspelling;
  154. $_misspellings[]=array('WORD'=>$word_bad,'CORRECTIONS'=>implode(', ',$words_good));
  155. }
  156. if (count($_misspellings)!=0)
  157. $spelling->attach(do_template('PREVIEW_SCRIPT_SPELLING',array('_GUID'=>'9649572982c01995a8f47c58d16fda39','FIELD'=>$key_nice,'MISSPELLINGS'=>$_misspellings)));
  158. }
  159. if (($keywordcheck) && ((strpos($val,' ')!==false) || ($key=='title')))
  160. {
  161. $keyword_explode=explode(',',$meta_keywords);
  162. $keywords=array();
  163. $word_count=str_word_count($val);
  164. if ($word_count!=0)
  165. {
  166. foreach ($keyword_explode as $meta_keyword)
  167. {
  168. $meta_keyword=trim($meta_keyword);
  169. if ($meta_keyword!='')
  170. {
  171. $density=substr_count($val,$meta_keyword)/$word_count;
  172. $ideal_density=1.0/(9.0*count($keyword_explode)); // Pretty rough -- common sense is needed
  173. $keywords[]=array('sort'=>$ideal_density,'KEYWORD'=>$meta_keyword,'IDEAL_DENSITY'=>strval(intval(round($ideal_density*100))),'DENSITY'=>strval(intval(round($density*100))));
  174. }
  175. }
  176. global $M_SORT_KEY;
  177. $M_SORT_KEY='sort';
  178. usort($keywords,'multi_sort');
  179. foreach ($keywords as $ti=>$meta_keyword)
  180. {
  181. unset($keywords[$ti]['sort']);
  182. }
  183. if (count($keywords)!=0)
  184. $keyword_density->attach(do_template('PREVIEW_SCRIPT_KEYWORD_DENSITY',array('_GUID'=>'4fa05e9f52023958a3594d1610b00747','FIELD'=>$key_nice,'KEYWORDS'=>$keywords)));
  185. }
  186. }
  187. }
  188. if (is_null($output))
  189. {
  190. if ((is_null($attachment_type)) || ($key!='post')) // Not an attachment-supporting field
  191. {
  192. $tempcodecss=(post_param_integer('tempcodecss__'.$key,0)==1);
  193. $supports_comcode=(post_param_integer('comcode__'.$key,0)==1);
  194. $preformatted=(post_param_integer('pre_f_'.$key,0)==1);
  195. if ($is_hidden) continue;
  196. if ($preformatted)
  197. {
  198. $valt=with_whitespace($val);
  199. } elseif ($supports_comcode)
  200. {
  201. $valt=comcode_to_tempcode($val);
  202. } elseif ($tempcodecss)
  203. {
  204. $i=0;
  205. $color=post_param(strval($i),'');
  206. while ($color!='')
  207. {
  208. $val=str_replace('<color-'.strval($i).'>','#'.$color,$val);
  209. $i++;
  210. $color=post_param(strval($i),'');
  211. }
  212. $_val_orig=$val;
  213. $valt=comcode_to_tempcode("[code=\"CSS\"]".$val."[/code]");
  214. } else
  215. {
  216. $valt=make_string_tempcode(escape_html($val));
  217. }
  218. $view_space_map[$key_nice]=$valt;
  219. } else // An attachment-supporting field
  220. {
  221. $tempcodecss=false;
  222. $posting_ref_id=post_param_integer('posting_ref_id');
  223. if ($posting_ref_id<0) fatal_exit(do_lang_tempcode('INTERNAL_ERROR'));
  224. $post_bits=do_comcode_attachments($val,$attachment_type,strval(-$posting_ref_id),true,$db);
  225. $new_post_value=$post_bits['comcode'];
  226. $view_space_map[$key_nice]=$post_bits['tempcode'];
  227. $val=$post_bits['tempcode'];
  228. $supports_comcode=true;
  229. }
  230. }
  231. }
  232. // Make attachments temporarily readable without any permission context
  233. global $COMCODE_ATTACHMENTS;
  234. $posting_ref_id=post_param_integer('posting_ref_id',NULL);
  235. if (!is_null($posting_ref_id))
  236. {
  237. if (array_key_exists(strval(-$posting_ref_id),$COMCODE_ATTACHMENTS))
  238. {
  239. foreach ($COMCODE_ATTACHMENTS[strval(-$posting_ref_id)] as $attachment)
  240. {
  241. $db->query_delete('attachment_refs',array('r_referer_type'=>'null','r_referer_id'=>strval(-$posting_ref_id),'a_id'=>$attachment['id']),'',1);
  242. $db->query_insert('attachment_refs',array('r_referer_type'=>'null','r_referer_id'=>strval(-$posting_ref_id),'a_id'=>$attachment['id']));
  243. }
  244. }
  245. }
  246. if (is_null($output))
  247. {
  248. if (count($view_space_map)==1)
  249. {
  250. $output=array_pop($view_space_map);
  251. } else
  252. {
  253. $view_space_fields=new ocp_tempcode();
  254. foreach ($view_space_map as $key=>$val)
  255. {
  256. $view_space_fields->attach(view_space_field($key,$val,true));
  257. }
  258. $output=do_template('VIEW_SPACE',array('_GUID'=>'3f548883b9eb37054c500d1088d9efa3','WIDTH'=>'170','FIELDS'=>$view_space_fields));
  259. }
  260. }
  261. // This is to get the Comcode attachments updated to the new IDs
  262. if (!is_null($new_post_value))
  263. {
  264. $new_post_value_html=comcode_to_tempcode($new_post_value,NULL,false,60,NULL,$db,true);
  265. if (strpos($new_post_value_html->evaluate(),'<!-- CC-error -->')===false)
  266. $output->attach(do_template('PREVIEW_SCRIPT_CODE',array('_GUID'=>'bc7432af91e1eaf212dc210f3bf2f756','NEW_POST_VALUE_HTML'=>$new_post_value_html,'NEW_POST_VALUE'=>$new_post_value)));
  267. }
  268. $output->handle_symbol_preprocessing();
  269. if ($multi_return) return array($output,$validation,$keyword_density,$spelling);
  270. return $output;
  271. }