From 516c678827e1f3e2435d8b0876f33b07f0d5bc85 Mon Sep 17 00:00:00 2001 From: ike Date: Sun, 19 Feb 2012 19:48:09 +0100 Subject: [PATCH] First commit: Addon for shortening long content and "show more" link. --- showmore.tgz | Bin 0 -> 1872 bytes showmore/README | 3 ++ showmore/showmore.css | 19 +++++++ showmore/showmore.php | 121 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 showmore.tgz create mode 100755 showmore/README create mode 100755 showmore/showmore.css create mode 100755 showmore/showmore.php diff --git a/showmore.tgz b/showmore.tgz new file mode 100644 index 0000000000000000000000000000000000000000..70c4beef90ce33de6c73724a4131aa0bed6202a6 GIT binary patch literal 1872 zcmV-W2e0@aiwFR2FgQ;D1MOICZ`-yO?$`KNoM}cS7q(=_ZZp`i_mX(H1)8>K+!YsV zVbBt73z12Iq~Zj_{`);7B{_U^&v>Z*yRVA+?{vF8&HvrrzW)E5 zQTw(Cd@CU{nlxcEK^d7P`8@nqJD4Y&s6Hfgt_HP(bsVW>AG+;!dWBzCL@Z*~QatTL z$4vcRy8*uDB9hzS+prJd$ZWQXmEKkne3=XSrC>y5xFwe3{nbwYKN{QhpDxp{e8&Ct zuY0|ty#BkI0c`zu_Vxc~jN|Vy-nHPxiyFLuw`4{8U|NDR-4fu`DVC9lQx$X8hZCM8 zB$X6WE|t{FzhG}*mO6f0fB!L4*hTc=EDo0hS>qSJz`w`nydTn75Nt0}f=x#3M|7m+ zi!mdTMv$eP!Q1nnKfzq^6@pw)I3wXA7MC%DpicN=JUGL4y_%;bMG5Scu10ICaLfmjoJYQTxEg{a5=!rim(^Y1W0wAP`RX3 z>1JFlu^6#e2gzYgAtVe|1gpkK<|iR8zW^48)Re|SowP=J&#&%Mt&$}rk^2SwA-Kn; z#TPUgI-64*P}eCPI*MMY0DD6RmV(ZQj_Yfj1yxgByP3e$@ZI1pzu`vLcO87Sh+;By zFq52M(+> zfbYR|p${(PP(vqwa7+r+k(ls(W(beMHNs|}j*#7}LsO!-y>{C*21x6}D87U+AyT3f z^8mDFh=1XX+`&d{*&VsygYsObdyH*jr z*~mFC=VEIOF?`fzY@4$32C#aVeHB$=mS$?#8AY}jm(Tc>0}St!9by>S4CgyKkE)^f zT?n@9&OI6?l%T!G0nSxqb{M_WS`)V1*ptjS+g@fs=Ew?QLXm|_(1IkyDH98Lq!Ub+ zd$VG;?6(RO=A-duLYGF>Xk+1hrr)1&woNWG1V*{GRx`WmRCO;VZz)_%?>=E;iB0Bn zGwU|+n=z?WHwbp>d$U`2ii-G%9Y#Q}@N$s4xd=4T;Wd95M-!5vrG!=MFi%(2DMy@~^zg-IM<3>}_Qc9L*Oz0aGBF91Jd zxL~J5<=nf)Zdt7bkyBk?rsz5~_im;;rkJ0zk$b1Tcbm#Mqt<<8Bucou3#QBjed%Bl zBr#Hv3AULjGRJ&rn8eSgSF)B_o7VX4C&@@ zV{K_5oA$S-4|UnzV~G|uBn32)v;xu1A$ku(bQ^gQ)*~Kj;-YIfeRp=s!)!&F@{B#W zgiSf#XjEL%PfLvDc}hY}%yWWg5AyB$`Zjg>&FQ<7$-m!^L1zfO|M=6J*C*h#g5Xp4 zBnVC?r=WueJ--bTEOQy_bRkI)jNdxo@^v43heh zifqsNgHye=?AEnzzGMHY7pv)Fk@P)akc{nx^OTq(?I&oq zM}#2PUKL&^P8GQcg(i(A + * based upon NSFW from Mike Macgirvin + * + */ + +function showmore_install() { + register_hook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body'); + register_hook('plugin_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings'); + register_hook('plugin_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post'); +} + +function showmore_uninstall() { + unregister_hook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body'); + unregister_hook('plugin_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings'); + unregister_hook('plugin_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post'); +} + +function showmore_addon_settings(&$a,&$s) { + + if(! local_user()) + return; + + /* Add our stylesheet to the page so we can make our settings look nice */ + + $a->page['htmlhead'] .= ''."\r\n"; + + $enable_checked = (intval(get_pconfig(local_user(),'showmore','disable')) ? '' : ' checked="checked"'); + $chars = get_pconfig(local_user(),'showmore','chars'); + if(!$chars) + $chars = '1100'; + + $s .= '
'; + $s .= '

' . t('"Show more" Settings').'

'; + $s .= '
'; + + $s .= ''; + $s .= ''; + $s .= '
'; + $s .= ''; + $s .= ''; + $s .= '
'; + + $s .= '
'; +// $s .= '
' . t('Use /expression/ to provide regular expressions') . '
'; + + return; +} + +function showmore_addon_settings_post(&$a,&$b) { + + if(! local_user()) + return; + + if($_POST['showmore-submit']) { + set_pconfig(local_user(),'showmore','chars',trim($_POST['showmore-chars'])); + $enable = ((x($_POST,'showmore-enable')) ? intval($_POST['showmore-enable']) : 0); + $disable = 1-$enable; + set_pconfig(local_user(),'showmore','disable', $disable); + info( t('Show More Settings saved.') . EOL); + } +} + +function showmore_prepare_body(&$a,&$b) { + + $words = null; + if(get_pconfig(local_user(),'showmore','disable')) + return; + + $chars = (int)get_pconfig(local_user(),'showmore','chars'); + if(!$chars) + $chars = 1100; + + if (strlen(strip_tags(trim($b['html']))) > $chars) { + $found = true; + $shortened = trim(showmore_cutitem($b['html'], $chars))."..."; + } + + if($found) { + $rnd = random_string(8); + $b['html'] = ''.$shortened." ". + ''.sprintf(t('Show More')).''. + ''; + } +} + +function showmore_cutitem($text, $limit) { + $text = trim($text); + + $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8"); + + $text = substr($text, 0, $limit); + + $pos1 = strrpos($text, "<"); + $pos2 = strrpos($text, ">"); + $pos3 = strrpos($text, "&"); + $pos4 = strrpos($text, ";"); + + if ($pos1 > $pos3) { + if ($pos1 > $pos2) + $text = substr($text, 0, $pos1); + } else { + if ($pos3 > $pos4) + $text = substr($text, 0, $pos3); + } + + $doc = new DOMDocument(); + $doc->preserveWhiteSpace = false; + + $doctype = ''; + @$doc->loadHTML($doctype."".$text.""); + + $text = $doc->saveHTML(); + $text = str_replace(array("", "", $doctype), array("", "", ""), $text); + + return($text); +} -- 2.39.5