PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_customproperties/helper.php

https://github.com/Shigaru/shigaru
PHP | 471 lines | 355 code | 53 blank | 63 comment | 56 complexity | 339c0255626f6013e717b57811eb28a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Custom Properties for Joomla! 1.5.x
  4. * @package Custom Properties
  5. * @subpackage Component
  6. * version 1.98.3.4
  7. * @revision $Revision: 1.7 $
  8. * @author Andrea Forghieri
  9. * @copyright (C) 2007-2011 Andrea Forghieri, Solidsystem - http://www.solidsystem.it
  10. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL version 2
  11. */
  12. // Check to ensure this file is included in Joomla!
  13. defined( '_JEXEC' ) or die( 'Restricted access' );
  14. /**
  15. * Shows associated custom properties
  16. * @param cpContenElement $ce cp Content Element Object
  17. * @param cid = content_id to show the customproperties
  18. * @param object $params params object
  19. * @param bool $append_to_meta appends tags to meta keywords. Default false. Note: this function makes sense when only one article is displayed (cplugin). There's no point in appending the tags of the search result page, to meta.
  20. * @returns string html formatted cp_tags areal
  21. */
  22. function showTags($ce, $cid, $params, $append_to_meta=false){
  23. global $Itemid;
  24. $database = JFactory::getDBO();
  25. $user = JFactory::getUser();
  26. $aid = $user->get('aid',0);
  27. $result = "";
  28. $tagstring = "";
  29. $tagstrings = array();
  30. $show_tag_name = $params->get('show_tag_name', '0');
  31. $linked_tags = $params->get('linked_tags', '0');
  32. $url_format = $params->get('url_format' , '0');
  33. $use_itemid = $params->get('use_itemid', '1');
  34. $query = "SELECT DISTINCT f.id as fid, f.label as name, v.id as vid, v.label
  35. FROM #__custom_properties AS cp
  36. INNER JOIN #__custom_properties_fields AS f
  37. ON (cp.field_id = f.id )
  38. INNER JOIN #__custom_properties_values AS v
  39. ON (cp.value_id = v.id )
  40. WHERE cp.ref_table = '".$ce->table."'
  41. AND cp.content_id = '$cid'
  42. AND f.published = '1'
  43. AND f.access <= '$aid'
  44. ORDER BY f.ordering, v.ordering ";
  45. $database->setQuery($query);
  46. $database->getErrorMsg();
  47. $tags = $database->loadObjectList();
  48. $result .= "<div class=\"cp_tags\">\n";
  49. $result .= "<span class=\"cp_tag_label\">". JText::_('Tags').": </span>";
  50. $itemid_url = "";
  51. if($use_itemid == 1){
  52. $itemid_url = "&Itemid=".$Itemid ;
  53. }
  54. $tags_count = 0;
  55. foreach($tags as $tag){
  56. $tagstring = "";
  57. if($url_format == 0){
  58. $link = JRoute::_("index.php?option=com_customproperties&task=tag&tagId=". $tag->vid .$itemid_url);
  59. }
  60. else{
  61. $link = JRoute::_("index.php?option=com_customproperties&task=tag&tagName=". urlencode($tag->name.":".$tag->label) . $itemid_url);
  62. }
  63. $result .= "<span class=\"cp_tag cp_tag_".$tag->vid."\">";
  64. if($linked_tags) $result .= "<a href=\"$link\">";
  65. if($show_tag_name) $tagstring = htmlspecialchars($tag->name) . ": ";
  66. $tagstring .= htmlspecialchars($tag->label);
  67. $result .= $tagstring;
  68. if($linked_tags) $result .= "</a>\n";
  69. $result .= "</span> ";
  70. $tagstrings[] = $tagstring;
  71. $tags_count++;
  72. }
  73. $fe_tagging = false;
  74. if($params->get('frontend_tagging') == 1 && ($aid >= $params->get('editing_level'))){
  75. $fe_tagging = true;
  76. JHTML::_( 'behavior.modal' );
  77. $link = JRoute::_('index.php?option=com_customproperties&controller=tagging&view=tagging&tmpl=component&id='.$cid);
  78. $rel = "{handler: 'iframe', size: {x: 570, y: 400}}";
  79. $result .= "<span class=\"cp_tag\"><a class=\"modal\" rel=\"$rel\" href=\"$link\">".JText::_('Edit tags')."</a></span>\n";
  80. }
  81. $result .= "</div>\n";
  82. if($append_to_meta && count($tagstrings)){
  83. $document = JFactory::getDocument();
  84. $keywords = $document->getMetaData('keywords');
  85. $document->setMetaData('keywords', $keywords.','. join(', ', $tagstrings) );
  86. }
  87. // dont't show the div at all if no tags are found , and frontend taggin is not enabled'
  88. if($fe_tagging == false && $tags_count == 0){
  89. $result = "";
  90. }
  91. return $result;
  92. }
  93. /**
  94. * This function is taken from mosPrepareSearchContent:
  95. * @package Joomla
  96. * @subpackage Search
  97. * file : joomla.php
  98. * Modified by : Andrea Forghieri
  99. * Solidsystem
  100. */
  101. function prepareContent($text, $length=200){
  102. // strips tags won't remove the actual jscript
  103. $text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $text );
  104. $text = preg_replace( '/{.+?}/', '', $text);
  105. // replace line breaking tags with whitespace
  106. $text = preg_replace( "'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text );
  107. $text = strip_tags($text);
  108. return substr( $text, 0, $length) . (strlen($text) > $length ? '...' : '');
  109. }
  110. /** This functions strips images from HTML and puts them
  111. * into the 'images' field
  112. * @param row
  113. * @param strin $image_path Images path
  114. * @returns row
  115. */
  116. function stripImages(&$row, $image_path){
  117. if(empty($row)){return ;}
  118. preg_match("/<img[^>]*>/i", $row->introtext . $row->fulltext, $matches);
  119. if (!empty($matches))
  120. {
  121. foreach ($matches as $txtimg)
  122. {
  123. /* strip the img, gonna append later */
  124. $row->introtext = str_replace($txtimg,"",$row->introtext);
  125. $txtimg = urldecode($txtimg); // in case it's an URL
  126. if (strpos($txtimg, "http")!== false) {
  127. // image is remote
  128. if(preg_match('#src=[\"\']{1}(.*?)["\']#i',$txtimg,$imgsrcs) ){
  129. if (!empty($row->images)) {
  130. $row->images = $imgsrcs[1] . "\n" . $row->images;
  131. }
  132. else {
  133. $row->images = $imgsrcs[1];
  134. }
  135. }
  136. }
  137. elseif ( strpos($txtimg, $image_path) !== false ){
  138. // img is local
  139. if (strpos($txtimg, 'src="/') !== false) {
  140. preg_match("#src=\"\/" . addslashes($image_path) . "\/([\ \:\-\/\_A-Za-z0-9\.]+)\"#i",$txtimg,$imgsrcs);
  141. }
  142. else {
  143. preg_match("#src=\"" . addslashes($image_path) . "\/([\ \:\-\/\_A-Za-z0-9\.]+)\"#i",$txtimg,$imgsrcs);
  144. }
  145. if ($row->images != "") {
  146. $row->images = $row->images ."\n" . $imgsrcs[1] ;
  147. }
  148. else {
  149. $row->images = $imgsrcs[1];
  150. }
  151. }
  152. }
  153. }
  154. /* let's strip al mos tags */
  155. $row->introtext= preg_replace("/{[^}]*}/","",$row->introtext);
  156. return $row;
  157. }
  158. /** This function calculate the new dimension of an
  159. * image so that it fits inside a given box
  160. * @param $srcx , $srcy size of the image to be resized
  161. * @param $forcewidth, $forcheight size of the resize box
  162. * @param bool $aspect true / false, keep aspect
  163. * */
  164. function calcThumbSize($srcx, $srcy, &$forcedwidth, &$forcedheight, $aspect)
  165. {
  166. //TODO check this function (calc thumb size)
  167. $img_aspect = $srcx / $srcy > 1 ? "portrait" : "landscape";
  168. $box_aspect = $forcedwidth / $forcedheight > 1 ? "portrait" : "landscape";
  169. if($img_aspect == $box_aspect){
  170. $target = max($forcedheight , $forcedwidth);
  171. }
  172. else{
  173. $target = min($forcedheight , $forcedwidth);
  174. }
  175. if($srcx > $srcy){
  176. $ratio = $target / $srcx ;
  177. }
  178. else{
  179. $ratio = $target / $srcy ;
  180. }
  181. $resize = ( $srcx > $forcedwidth || $srcy > $forcedheight) ? true : false ;
  182. if($resize && $aspect){
  183. $forcedwidth = round($srcx * $ratio);
  184. $forcedheight = round($srcy * $ratio);
  185. }
  186. }
  187. /** This function returns the thumbnail preview.
  188. * If it does not exists or if it is deemed cont
  189. * current, a new one is generated.
  190. * @param $file image filename (without path)
  191. * @param $image_dir images directory respective of site url e.g. images/stories , no trailing slash
  192. * @param $width, $heigth size of the thumbnail
  193. * @param $extra extra attributes to be given to the returned HTML tag (class, title , alt)
  194. * @param $aspect true, false keep aspect ratio
  195. * @param integer $quality quality of the image (0 worst - 100 best)
  196. * @param string $thumb_dir directory to save the thumbnails into. Default: component/images
  197. * @return HTML tag or false if errors occurred
  198. */
  199. function getThumb($file, $image_dir, $width, $heigth, $extra="", $aspect=true, $quality = '75', $thumb_dir=null, $debug=false)
  200. {
  201. $found = false;
  202. $is_URL = false;
  203. if(empty($file)){return false;}
  204. if(empty($image_dir)){return false;}
  205. if(empty($thumb_dir)){$thumb_dir = 'components'.DS.'com_customproperties'.DS.'images';}
  206. // define some directories
  207. $dst_dir = preg_replace("#^(http:)?//?#", "", $file);
  208. $thumb_subdir = preg_replace('/^\./','',dirname($dst_dir));
  209. $images_base_path = JPATH_ROOT . DS. $image_dir;
  210. if(strpos($file, 'http') === 0){
  211. $image_path = $file;
  212. $is_URL = true;
  213. }
  214. else{
  215. $image_path = $images_base_path . DS . $file;
  216. }
  217. $thumb_base_path = JPATH_ROOT. DS .$thumb_dir. DS . $thumb_subdir;
  218. $thumb_base_url = str_replace( DS, '/', $thumb_dir .'/'. $thumb_subdir);
  219. $ext = substr(strrchr(basename($file), '.'), 1);
  220. /* make sure directory exists, otherwise create it */
  221. if(!is_writable( $thumb_base_path )){
  222. if(! mkdir($thumb_base_path, 0755, true) ){
  223. if($debug) return "<span class=\"alert\"> Can't generate thumbnails. Can't write to directory </span>";
  224. return false;
  225. }
  226. }
  227. $thumb_name = str_replace('.'.$ext, '_thumb.'.$ext, basename($file));
  228. $thumb_path = $thumb_base_path.DS.$thumb_name;
  229. $thumb_url = str_replace('//', '/', $thumb_base_url .'/'.$thumb_name);
  230. $image = '';
  231. if (file_exists($thumb_path))
  232. {
  233. // compare found thumb size with teorethical size, for chaching purposes
  234. $thumb_size = '';
  235. $twx = $thy = 0;
  236. if (function_exists( 'getimagesize' ))
  237. {
  238. $thumb_size = getimagesize( $thumb_path );
  239. if (is_array( $thumb_size ))
  240. {
  241. $twx = $thumb_size[0];
  242. $thy = $thumb_size[1];
  243. $size = 'width="'.$twx.'" height="'.$thy.'"';
  244. }
  245. $image_size = '';
  246. $wx = $hy = 0;
  247. $image_path = escapeFileName($image_path);
  248. $image_size = getimagesize( $image_path );
  249. if (is_array( $image_size ))
  250. {
  251. $wx = $image_size[0];
  252. $hy = $image_size[1];
  253. }
  254. }
  255. calcThumbSize($wx, $hy, $width, $heigth, $aspect);
  256. if($twx == $width && $thy == $heigth){
  257. $found = true;
  258. $size = 'width="'.$width.'" height="'.$heigth.'"';
  259. $image= '<img src="'.$thumb_url.'" '.$size.' '.$extra.'/>';
  260. }
  261. }
  262. if (!$found)
  263. {
  264. // make the thumbnails
  265. switch (strtolower($ext))
  266. {
  267. case 'jpg':
  268. case 'jpeg':
  269. case 'png':
  270. if(!cp_thumbit($image_path, $thumb_path, $ext, $width, $heigth, $aspect, $quality)){
  271. $image = "";
  272. if($debug) $image = "<span class=\"alert\"> Thumbnail creation failed. Missing source image $image_path.</span>";
  273. break;
  274. }
  275. $size = 'width="'.$width.'" height="'.$heigth.'"';
  276. $image= '<img src="'.$thumb_url.'" '.$size.' '.$extra.'/>';
  277. break;
  278. case 'gif':
  279. if (function_exists("imagegif")) {
  280. if(!cp_thumbit($image_path, $thumb_path, $ext, $width, $heigth, $aspect, $quality)){
  281. $image = "";
  282. if($debug) $image = "<span class=\"alert\"> Thumbnail creation failed. Missing source image $image_path.</span>";
  283. break;
  284. }
  285. $size = 'width="'.$width.'" height="'.$heigth.'"';
  286. $image= '<img src="'.$thumb_url.'" '.$size.' '.$extra.'/>';
  287. break;
  288. }
  289. else{
  290. $image = "";
  291. if($debug) $image = "<span class=\"alert\"> Can't generate thumbnails from gif, missing function.</span>";
  292. break;
  293. }
  294. default:
  295. $image = "";
  296. if($debug) $image = "<span class=\"alert\"> Image file type (". htmlspecialchars($ext).") not supported.</span>";
  297. break;
  298. }
  299. }
  300. return $image;
  301. }
  302. /** Function taken from Mini Front Page
  303. * Modified by Andrea Forghieri - Solidsystem
  304. *
  305. *
  306. * This function creates a thumbnail preview from an image
  307. * @param string $file source file (complete with path)
  308. * @param string $thumb filename to be given to the thumb (complete with path)
  309. * @param string $ext Extension of the source file
  310. * @param int &$newheight height of the thumbnail
  311. * @param int &$newwidth width of the thumbnail
  312. * @param bool aspect preserve aspect (true / false)
  313. * @param integer $quality quality of the image (0 worst - 100 best)
  314. * @return boolean return false if
  315. */
  316. function cp_thumbIt ($file, $thumb, $ext, &$new_width, &$new_height, $aspect, $quality)
  317. {
  318. $filename = escapeFileName($file);
  319. $img_info = getimagesize($file);
  320. if(!$img_info){return false;}
  321. $orig_width = $img_info[0];
  322. $orig_height = $img_info[1];
  323. calcThumbSize($orig_width, $orig_height, $new_width, $new_height, $aspect);
  324. switch (strtolower($ext)) {
  325. case 'jpg':
  326. case 'jpeg':
  327. $im = imagecreatefromjpeg($file);
  328. $tim = imagecreatetruecolor ($new_width, $new_height);
  329. cp_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
  330. imagedestroy($im);
  331. imagejpeg($tim, rawurldecode($thumb), $quality );
  332. imagedestroy($tim);
  333. break;
  334. case 'png':
  335. $im = imagecreatefrompng($file);
  336. $tim = imagecreatetruecolor ($new_width, $new_height);
  337. cp_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
  338. imagedestroy($im);
  339. imagepng($tim, rawurldecode($thumb));
  340. imagedestroy($tim);
  341. break;
  342. case 'gif':
  343. if (function_exists("imagegif")) {
  344. $im = imagecreatefromgif($file);
  345. $tim = imagecreatetruecolor ($new_width, $new_height);
  346. cp_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
  347. imagedestroy($im);
  348. imagegif($tim, rawurldecode($thumb));
  349. imagedestroy($tim);
  350. }
  351. break;
  352. default:
  353. break;
  354. }
  355. return true;
  356. }
  357. /** function copied verbatim from mod_minifrontpage
  358. * Template plaza
  359. */
  360. function cp_ImageCopyResampleBicubic (&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
  361. {
  362. if ($dst_w==$src_w && $dst_h==$src_h) {
  363. $dst_img = $src_img;
  364. return;
  365. }
  366. ImagePaletteCopy ($dst_img, $src_img);
  367. $rX = $src_w / $dst_w;
  368. $rY = $src_h / $dst_h;
  369. $w = 0;
  370. for ($y = $dst_y; $y < $dst_h; $y++)
  371. {
  372. $ow = $w; $w = round(($y + 1) * $rY);
  373. $t = 0;
  374. for ($x = $dst_x; $x < $dst_w; $x++)
  375. {
  376. $r = $g = $b = 0; $a = 0;
  377. $ot = $t; $t = round(($x + 1) * $rX);
  378. for ($u = 0; $u < ($w - $ow); $u++)
  379. {
  380. for ($p = 0; $p < ($t - $ot); $p++)
  381. {
  382. $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $ot + $p, $ow + $u));
  383. $r += $c['red'];
  384. $g += $c['green'];
  385. $b += $c['blue'];
  386. $a++;
  387. }
  388. }
  389. if(!$a) $a = 1;
  390. ImageSetPixel ($dst_img, $x, $y, ImageColorClosest ($dst_img, $r / $a, $g / $a, $b / $a));
  391. }
  392. }
  393. }
  394. /**
  395. * function to escape the path of an URL
  396. * @return string escaped url , of original file if the passed variable is not an url
  397. */
  398. function escapeFileName($file){
  399. if(strpos($file, 'http') === 0){
  400. $theURL = parse_url($file);
  401. $thePath = explode('/', $theURL['path']);
  402. $theEscapedPath = join('/', array_map('rawurlencode', $thePath) );
  403. $file = $theURL['scheme'] . '://' . $theURL['host']. '/'. $theEscapedPath;
  404. }
  405. return $file;
  406. }