FormatHelper.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. <?php
  2. App::uses('TextHelper', 'View/Helper');
  3. /**
  4. * Format helper with basic html snippets
  5. *
  6. * TODO: make snippets more "css and background image" (instead of inline img links)
  7. * TODO: test cases
  8. *
  9. */
  10. class FormatHelper extends TextHelper {
  11. /**
  12. * Other helpers used by FormHelper
  13. *
  14. * @var array
  15. */
  16. public $helpers = array('Html', 'Form', 'Tools.Common', 'Tools.Gravatar', 'Tools.PhpThumb');
  17. /**
  18. * jqueryAccess: {id}Pro, {id}Contra
  19. */
  20. public function thumbs($id, $inactive = false, $inactiveTitle = null) {
  21. $class = 'Active';
  22. $upTitle = __('Zustimmen');
  23. $downTitle = __('Dagegen');
  24. if ($inactive === true) {
  25. $class = 'Inactive';
  26. $upTitle = $downTitle = !empty($inactiveTitle)?$inactiveTitle:__('alreadyVoted');
  27. }
  28. $ret = '<div class="thumbsUpDown">';
  29. $ret .= '<div id="'.$id.'Pro'.$class.'" rel="'.$id.'" class="thumbUp up'.$class.'" title="'.$upTitle.'"></div>';
  30. $ret .= '<div id="'.$id.'Contra'.$class.'" rel="'.$id.'" class="thumbDown down'.$class.'" title="'.$downTitle.'"></div>';
  31. $ret .= '<br class="clear"/>';
  32. $ret .= '</div>';
  33. return $ret;
  34. }
  35. /**
  36. * Display neighbor quicklinks
  37. *
  38. * @param array $neighbors (containing prev and next)
  39. * @param string $field: just field or Model.field syntax
  40. * @param array $options:
  41. * - name: title name: next{Record} (if none is provided, "record" is used - not translated!)
  42. * - slug: true/false (defaults to false)
  43. * - titleField: field or Model.field
  44. */
  45. public function neighbors($neighbors, $field, $options = array()) {
  46. if (mb_strpos($field, '.') !== false) {
  47. $fieldArray = explode('.', $field, 2);
  48. $alias = $fieldArray[0];
  49. $field = $fieldArray[1];
  50. }
  51. if (empty($alias)) {
  52. if (!empty($neighbors['prev'])) {
  53. $modelNames = array_keys($neighbors['prev']);
  54. $alias = $modelNames[0];
  55. } elseif (!empty($neighbors['next'])) {
  56. $modelNames = array_keys($neighbors['next']);
  57. $alias = $modelNames[0];
  58. }
  59. }
  60. if (empty($field)) {
  61. }
  62. $name = 'Record'; # translation further down!
  63. if (!empty($options['name'])) {
  64. $name = ucfirst($options['name']);
  65. }
  66. $prevSlug = $nextSlug = null;
  67. if (!empty($options['slug'])) {
  68. if (!empty($neighbors['prev'])) {
  69. $prevSlug = slug($neighbors['prev'][$alias][$field]);
  70. }
  71. if (!empty($neighbors['next'])) {
  72. $nextSlug = slug($neighbors['next'][$alias][$field]);
  73. }
  74. }
  75. $titleAlias = $alias;
  76. $titleField = $field;
  77. if (!empty($options['titleField'])) {
  78. if (mb_strpos($options['titleField'], '.') !== false) {
  79. $fieldArray = explode('.', $options['titleField'], 2);
  80. $titleAlias = $fieldArray[0];
  81. $titleField = $fieldArray[1];
  82. } else {
  83. $titleField = $options['titleField'];
  84. }
  85. }
  86. if (!isset($options['escape']) || $options['escape'] === false) {
  87. $titleField = h($titleField);
  88. }
  89. $ret = '<div class="nextPrevNavi">';
  90. if (!empty($neighbors['prev'])) {
  91. $url = array($neighbors['prev'][$alias]['id'], $prevSlug);
  92. if (!empty($options['url'])) {
  93. $url += $options['url'];
  94. }
  95. $ret.= $this->Html->link($this->cIcon(ICON_PREV, false).'&nbsp;'.__('prev'.$name), $url, array('escape'=>false, 'title'=>$neighbors['prev'][$titleAlias][$titleField]));
  96. } else {
  97. $ret.= $this->cIcon(ICON_PREV_DISABLED, __('noPrev'.$name)).'&nbsp;'.__('prev'.$name);
  98. }
  99. $ret.= '&nbsp;&nbsp;';
  100. if (!empty($neighbors['next'])) {
  101. $url = array($neighbors['next'][$alias]['id'], $prevSlug);
  102. if (!empty($options['url'])) {
  103. $url += $options['url'];
  104. }
  105. $ret.= $this->Html->link($this->cIcon(ICON_NEXT, false).'&nbsp;'.__('next'.$name), $url, array('escape'=>false, 'title'=>$neighbors['next'][$titleAlias][$titleField]));
  106. } else {
  107. $ret.= $this->cIcon(ICON_NEXT_DISABLED, __('noNext'.$name)).'&nbsp;'.__('next'.$name);
  108. }
  109. $ret .= '</div>';
  110. return $ret;
  111. }
  112. /**
  113. * Allows icons to be added on the fly
  114. * NOTE: overriding not allowed by default
  115. */
  116. public function addIcon($name = null, $pic = null, $title = null, $allowOverride = false) {
  117. if ($allowOverride === true || ($allowOverride !==true && !array_key_exists($name, $this->icons))) {
  118. if (!empty($name) && !empty($pic)) {
  119. $this->icons[$name] = array('pic'=>strtolower($pic),'title'=>(!empty($title)?$title:''));
  120. }
  121. }
  122. return false;
  123. }
  124. const GENDER_FEMALE = 2;
  125. const GENDER_MALE = 1;
  126. /**
  127. * //TODO: move to Format
  128. * displays gender icon
  129. */
  130. public function genderIcon($value = null, $type = null) {
  131. $value = (int)$value;
  132. if ($value == self::GENDER_FEMALE) {
  133. $icon = $this->icon('genderFemale', null, null, null, array('class'=>'gender'));
  134. } elseif ($value == self::GENDER_MALE) {
  135. $icon = $this->icon('genderMale', null, null, null, array('class'=>'gender'));
  136. } else {
  137. $icon = $this->icon('genderUnknown', null, null, null, array('class'=>'gender'));
  138. }
  139. return $icon;
  140. }
  141. /**
  142. * //TODO: move to Format?
  143. * returns img from customImgFolder
  144. * @param ARRAY options (ending [default: gif])
  145. */
  146. public function customIcon($folder, $icon = null, $checkExist = false, $options = array(), $attr = array()) {
  147. $attachment = 'default';
  148. $ending = 'gif';
  149. $image = null;
  150. if (!empty($options)) {
  151. if (!empty($options['ending'])) {
  152. $ending = $options['ending'];
  153. }
  154. if (!empty($options['backend'])) {
  155. $attachment = 'backend';
  156. }
  157. }
  158. if (empty($icon)) {
  159. } elseif ($checkExist === true && !file_exists(PATH_CONTENT.$folder.DS.$icon.'.'.$ending)) {
  160. } else {
  161. $image = $icon;
  162. }
  163. if ($image === null) {
  164. return $this->Html->image(IMG_ICONS.'custom'.'/'.$folder.'_'.$attachment.'.'.$ending, $attr);
  165. }
  166. return $this->Html->image(IMG_CONTENT.$folder.'/'.$image.'.'.$ending, $attr);
  167. }
  168. /**
  169. * //TODO: move to Format?
  170. * @param string $icon iso2 code (e.g. 'de' or 'gb')
  171. */
  172. public function countryIcon($icon = null, $returnFalseonFailure = false, $options = array(), $attr = array()) {
  173. $ending = 'gif';
  174. $image = 'unknown';
  175. if ($specific = Configure::read('Country.image_path')) {
  176. $wwwPath = $specific.'/';
  177. $path = PATH_IMAGES. $specific . DS; //.'country_flags'.DS
  178. } else {
  179. $wwwPath = '/tools/img/country_flags/';
  180. $path = App::pluginPath('Tools') . 'webroot' . DS. 'img' . DS . 'country_flags' . DS;
  181. }
  182. if (!empty($options) && is_array($options)) {
  183. if (!empty($options['ending'])) {
  184. $ending = $options['ending'];
  185. }
  186. }
  187. $icon = mb_strtolower($icon);
  188. if (empty($icon)) {
  189. if ($returnFalseonFailure) { return false; }
  190. } elseif (!file_exists($path . $icon . '.' . $ending)) {
  191. //die($path . $icon . '.' . $ending);
  192. trigger_error($path . $icon . '.' . $ending.' missing', E_USER_NOTICE);
  193. if ($returnFalseonFailure) { return false; }
  194. } else {
  195. $image = $icon;
  196. }
  197. return $this->Html->image($wwwPath . $image . '.' . $ending, $attr);
  198. }
  199. /**
  200. */
  201. public function importantIcon($icon, $value) {
  202. $ending = 'gif';
  203. $image = 'default';
  204. if (!empty($value)) {
  205. $image = 'important';
  206. }
  207. return $this->Html->image(IMG_ICONS.$icon.'_'.$image.'.'.$ending);
  208. }
  209. /**
  210. * @param value
  211. * @param array $options
  212. * - max (3/5, defaults to 5)
  213. * - normal: display an icon for normal as well (defaults to false)
  214. * - map: array (manually map values, if you use 1 based values no need for that)
  215. * - title, alt, ...
  216. * @return string html
  217. */
  218. public function priorityIcon($value, $options = array()) {
  219. $defaults = array(
  220. 'max' => 5,
  221. 'normal' => false,
  222. 'map' => array(),
  223. 'css' => true,
  224. );
  225. $options = array_merge($defaults, $options);
  226. extract($options);
  227. $matching = array(
  228. 1 => 'low',
  229. 2 => 'lower',
  230. 3 => 'normal',
  231. 4 => 'higher',
  232. 5 => 'high'
  233. );
  234. if (!empty($map)) {
  235. $value = $map[$value];
  236. }
  237. if (!$normal && $value == ($max+1)/2) {
  238. return '';
  239. }
  240. if ($max != 5) {
  241. if ($value == 2) {
  242. $value = 3;
  243. } elseif ($value == 3) {
  244. $value = 5;
  245. }
  246. }
  247. $attr = array(
  248. 'class' => 'prio-'.$matching[$value],
  249. 'title' => __('prio'.ucfirst($matching[$value])),
  250. );
  251. if (!$css) {
  252. $attr['alt'] = $matching[$value];
  253. }
  254. $attr = array_merge($attr, array_diff_key($options, $defaults));
  255. if ($css) {
  256. $html = $this->Html->tag('div', '&nbsp;', $attr);
  257. } else {
  258. $icon = 'priority_' . $matching[$value] . '.gif';
  259. $html = $this->Html->image('icons/'.$icon, $attr);
  260. }
  261. return $html;
  262. }
  263. /**
  264. * Display a font icon (fast and resource-efficient).
  265. * Uses http://fontawesome.io/icons/
  266. *
  267. * Options:
  268. * - size (int|string: 1...5 or large)
  269. * - rotate (integer: 90, 270, ...)
  270. * - spin (booelan: true/false)
  271. * - extra (array: muted, light, dark, border)
  272. * - pull (string: left, right)
  273. *
  274. * @param string|array $icon
  275. * @param array $options
  276. * @return void
  277. */
  278. public function fontIcon($icon, $options = array()) {
  279. $icon = (array)$icon;
  280. $class = array();
  281. foreach ($icon as $i) {
  282. $class[] = 'icon-' . $i;
  283. }
  284. if (!empty($options['extra'])) {
  285. foreach ($options['extra'] as $i) {
  286. $class[] = 'icon-' . $i;
  287. }
  288. }
  289. if (!empty($options['size'])) {
  290. $class[] = 'icon-' . ($options['size'] === 'large' ? 'large' : $options['size'] . 'x');
  291. }
  292. if (!empty($options['pull'])) {
  293. $class[] = 'pull-' . $options['pull'];
  294. }
  295. if (!empty($options['rotate'])) {
  296. $class[] = 'icon-rotate-' . (int)$options['rotate'];
  297. }
  298. if (!empty($options['spin'])) {
  299. $class[] = 'icon-spin';
  300. }
  301. return '<i class="' . implode(' ', $class) . '"></i>';
  302. }
  303. /**
  304. * Quick way of printing default icons (have to be 16px X 16px !!!)
  305. * @param type
  306. * @param title
  307. * @param alt (set to FALSE if no alt is supposed to be shown)
  308. * @param boolean automagic i18n translate [default true = __('xyz')]
  309. * @param options array ('class'=>'','width/height'=>'','onclick=>'') etc
  310. */
  311. public function icon($type, $t = null, $a = null, $translate = null, $options = array()) {
  312. $html='';
  313. # title
  314. if (isset($t) && $t === false) {
  315. $title='';
  316. } elseif (empty($t)) {
  317. } else {
  318. $title=$t;
  319. //$alt=$t; // alt=title as default
  320. }
  321. #alt
  322. if (isset($a) && $a===false) {
  323. $alt='';
  324. } elseif (empty($a)) {
  325. } else {
  326. $alt=$a;
  327. }
  328. if (array_key_exists($type, $this->icons)) {
  329. $pic = $this->icons[$type]['pic'];
  330. $title = (isset($title)?$title:$this->icons[$type]['title']);
  331. $alt = (isset($alt)?$alt:preg_replace('/[^a-zA-Z0-9]/', '', $this->icons[$type]['title']));
  332. if ($translate !== false) {
  333. $title = __($title);
  334. $alt = __($alt);
  335. }
  336. $alt = '['.$alt.']';
  337. } else {
  338. $pic = 'pixelspace.gif';
  339. $title = '';
  340. $alt = '';
  341. }
  342. $default_options=array('title'=>$title, 'alt'=>$alt, 'class'=>'icon');
  343. //$new_options['onclick']=$options['onclick'];
  344. $new_options= array_merge($default_options, $options);
  345. $html.=$this->Html->image('icons/'.$pic, $new_options);
  346. return $html;
  347. }
  348. /**
  349. * Custom Icons
  350. * @param string $icon (constant or filename)
  351. * @param array $options:
  352. * - translate, ...
  353. * @param array $attributes:
  354. * - title, alt, ...
  355. * THE REST IS DEPRECATED
  356. */
  357. public function cIcon($icon, $t=null, $a=null, $translate=true, $options=array()) {
  358. if (is_array($t)) {
  359. $translate = isset($t['translate']) ? $t['translate'] : true;
  360. $options = (array)$a;
  361. $a = isset($t['alt']) ? $t['alt'] : null; # deprecated
  362. $t = isset($t['title']) ? $t['title'] : null; # deprecated
  363. }
  364. $title = (isset($t) ? $t : ucfirst(extractPathInfo('filename', $icon)));
  365. //$alt = (isset($a)?$a:preg_replace('/[^a-zA-Z0-9]/', '', $title));
  366. $alt = (isset($a) ? $a : Inflector::slug($title, '-'));
  367. if ($translate !== false) {
  368. $title = __($title);
  369. $alt = __($alt);
  370. }
  371. $alt = '['.$alt.']';
  372. $defaultOptions = array('title'=>$title, 'alt'=>$alt, 'class'=>'icon');
  373. $options = array_merge($defaultOptions, $options);
  374. if (substr($icon, 0, 1) !== '/') {
  375. $icon = 'icons/'.$icon;
  376. }
  377. return $this->Html->image($icon, $options);
  378. }
  379. /**
  380. * @deprecated use RatingHelper::stars() instead
  381. * //TODO: move to Format?
  382. * print Star Bar
  383. * array $options: steps=1/0.5 [default:1]), show_zero=true/false [default:false], title=false/true [default:false]
  384. * array $attr: string 'title' (both single and span title empty => 'x of x' for span)
  385. * TODO: 0.5 steps!
  386. */
  387. public function showStars($current = null, $max = null, $options = array(), $attr = array()) {
  388. $res = '---';
  389. if (!empty($options['steps']) && $options['steps'] == 0.5) {
  390. $steps = 0.5;
  391. $current = ((int)(2*$current)/2);
  392. } else {
  393. $steps = 1;
  394. $current = (int)$current;
  395. }
  396. $min = (int)$current;
  397. $max = (int)$max;
  398. if ((!empty($current) || (!empty($options['show_zero']) && $current == 0)) && (!empty($max)) && $current <= $max) {
  399. if (!empty($options) && is_array($options)) {
  400. }
  401. $text = '';
  402. for ($i=0;$i<$min;$i++) {
  403. $attributes = array('alt'=>'#', 'class'=>'full');
  404. if (!empty($options['title'])) { $attributes['title'] = ($i+1).'/'.$max; } # ?
  405. $text.= $this->Html->image('icons/star_icon2.gif', $attributes);
  406. }
  407. for ($i=$min;$i<$max;$i++) {
  408. $attributes = array('alt'=>'-', 'class'=>'empty');
  409. if (!empty($options['title'])) { $attributes['title'] = ($i+1).'/'.$max; } # ?
  410. if ($steps == 0.5 && $current == $i+0.5) {
  411. $text.= $this->Html->image('icons/star_icon2_half.gif', $attributes);
  412. } else {
  413. $text.= $this->Html->image('icons/star_icon2_empty.gif', $attributes);
  414. }
  415. }
  416. $attributes = array('class'=>'starBar');
  417. $attributes = array_merge($attributes, $attr);
  418. if (empty($attributes['title']) && empty($options['title'])) {
  419. $attributes['title'] = ($current) . ' ' . __('of') . ' ' . $max;
  420. }
  421. $res = $this->Html->tag('span', $text, $attributes);
  422. //$res='<span title="ss" class="starBar">'.$text.'</span>';
  423. } else {
  424. if ($max > 3) {
  425. for ($i = 0; $i < $max - 3; $i++) {
  426. $res .= '-';
  427. }
  428. }
  429. }
  430. return $res;
  431. }
  432. /**
  433. * //TODO: move to Format?
  434. * addItemAttribute function
  435. *
  436. * Called to modify the attributes of the next <item> to be processed
  437. * Note that the content of a 'node' is processed before generating its wrapping <item> tag
  438. * TODO: refactor!!
  439. *
  440. * @param string $id
  441. * @param string $key
  442. * @param mixed $value
  443. * @return void
  444. */
  445. public function languageFlags() {
  446. $langs = Configure::read('LanguagesAvailable');
  447. $supportedLangs = array(
  448. 'de' => array('title'=>'Deutsch'),
  449. 'en' => array('title'=>'English'),
  450. 'it' => array('title'=>'Italiano'),
  451. );
  452. $language_change = __('Language').': ';
  453. $languages = array();
  454. foreach ($langs as $lang) {
  455. $languages[$lang] = $supportedLangs[$lang];
  456. }
  457. if ($sLang = (string)CakeSession::read('Config.language')) {
  458. $lang = $sLang;
  459. } else {
  460. $lang = '';
  461. }
  462. echo '<span class="country">';
  463. foreach ($languages as $code => $la) {
  464. if ($lang == $code) {
  465. $language_change .= $this->Html->image('language_flags/'.$code.'.gif', array('alt'=>$code,'title'=>$la['title'].' ('.__('active').')','class'=>'country_flag active')).'';
  466. } else {
  467. $language_change .= $this->Html->link($this->Html->image('language_flags/'.$code.'.gif', array('alt'=>$code,'title'=>$la['title'],'class'=>'country_flag')), '/lang/'.$code, array('escape'=>false)).'';
  468. }
  469. }
  470. $language_change .= '</span>'; //.__('(Translation not complete yet)');
  471. return $language_change;
  472. }
  473. /**
  474. * It is still believed that encoding will stop spam-bots being able to find your email address.
  475. * Nevertheless, encoded email address harvester are on the way (http://www.dreamweaverfever.com/experiments/spam/).
  476. *
  477. * //TODO: move to TextExt?
  478. * Helper Function to Obfuscate Email by inserting a span tag (not more! not very secure on its own...)
  479. * each part of this mail now does not make sense anymore on its own
  480. * (striptags will not work either)
  481. * @param string email: necessary (and valid - containing one @)
  482. */
  483. public function encodeEmail($mail) {
  484. list($mail1, $mail2) = explode('@', $mail);
  485. $encMail = $this->encodeText($mail1) . '<span>@</span>' . $this->encodeText($mail2);
  486. return $encMail;
  487. }
  488. /**
  489. * //TODO: move to TextExt?
  490. * Obfuscates Email (works without JS!) to avoid spam bots to get it
  491. * @param string mail: email to encode
  492. * @param string text: optional (if none is given, email will be text as well)
  493. * @param array attributes: html tag attributes
  494. * @param array params: ?subject=y&body=y to be attached to "mailto:xyz"
  495. * @return save string with js generated link around email (and non js fallback)
  496. */
  497. public function encodeEmailUrl($mail, $text = null, $params = array(), $attr = array()) {
  498. if (empty($class)) { $class='email';}
  499. $defaults = array(
  500. 'title'=>__('for use in an external mail client'),
  501. 'class' => 'email',
  502. 'escape' => false
  503. );
  504. if (empty($text)) {
  505. $text = $this->encodeEmail($mail);
  506. }
  507. $encMail = 'mailto:'.$mail;
  508. // additionally there could be a span tag in between: email<span syle="display:none"></span>@web.de
  509. $querystring = '';
  510. foreach ($params as $key => $val) {
  511. if ($querystring) {
  512. $querystring .= "&$key=" . rawurlencode($val);
  513. } else {
  514. $querystring = "?$key=" . rawurlencode($val);
  515. }
  516. }
  517. $attr = array_merge($defaults, $attr);
  518. $xmail = $this->Html->link('', $encMail . $querystring, $attr);
  519. $xmail1 = mb_substr($xmail, 0, count($xmail) - 5);
  520. $xmail2 = mb_substr($xmail, -4, 4);
  521. $len = mb_strlen($xmail1);
  522. $i=0;
  523. while ($i < $len) {
  524. $c = mt_rand(2, 6);
  525. $par[] = (mb_substr($xmail1, $i, $c));
  526. $i += $c;
  527. }
  528. $join = implode('\'+ \'', $par);
  529. return '<script language=javascript><!--
  530. document.write(\''.$join.'\');
  531. //--></script>
  532. '.$text.'
  533. <script language=javascript><!--
  534. document.write(\''.$xmail2.'\');
  535. //--></script>';
  536. }
  537. /**
  538. * //TODO: move to TextExt?
  539. * Encodes Piece of Text (without usage of JS!) to avoid spam bots to get it
  540. * @param STRING text to encode
  541. * @return string (randomly encoded)
  542. */
  543. public function encodeText($text) {
  544. $encmail = '';
  545. for ($i=0; $i<mb_strlen($text); $i++) {
  546. $encMod = mt_rand(0,2);
  547. switch ($encMod) {
  548. case 0: // None
  549. $encmail .= mb_substr($text, $i,1);
  550. break;
  551. case 1: // Decimal
  552. $encmail .= "&#".ord(mb_substr($text, $i,1)).';';
  553. break;
  554. case 2: // Hexadecimal
  555. $encmail .= "&#x".dechex(ord(mb_substr($text, $i,1))).';';
  556. break;
  557. }
  558. }
  559. return $encmail;
  560. }
  561. /**
  562. * //TODO: move to Format?
  563. * @param text: default FALSE; if TRUE, text instead of the image
  564. * @param ontitle: default FALSE; if it is embadded in a link, set to TRUE
  565. * @return image:Yes/No or text:Yes/No
  566. *
  567. * @todo $on=1, $text=false, $ontitle=false,... => in array(OPTIONS) packen
  568. */
  569. public function yesNo($v, $ontitle = null, $offtitle = null, $on = 1, $text=false, $notitle=false) {
  570. $ontitle = (!empty($ontitle) ? $ontitle : __('Ja'));
  571. $offtitle = (!empty($offtitle) ? $offtitle : __('Nein'));
  572. $sbez = array('0' => @substr($offtitle, 0, 1), '1' => @substr($ontitle, 0, 1));
  573. $bez = array('0' => $offtitle, '1' => $ontitle);
  574. if ($v == $on) {
  575. $icon = ICON_YES;
  576. $value = 1;
  577. } else {
  578. $icon = ICON_NO;
  579. $value = 0;
  580. }
  581. if ($text !== false) {
  582. $light = $bez[$value];
  583. } else {
  584. //$light='<img src="images/icons/'.$icon.'" alt="'.$sbez[$value].'" '.($notitle!==false?'title="'.$bez[$value].'"':'').'/>';
  585. //$light=$this->Html->image('',)<img src="images/icons/'.$icon.'" alt="'.$sbez[$value].'" '.($notitle!==false?'title="'.$bez[$value].'"':'').'/>';
  586. $options = array('title' => ($ontitle === false ? '' : $bez[$value]), 'alt' => $sbez[$value], 'class' => 'icon');
  587. $light = $this->Html->image('icons/' . $icon, $options);
  588. }
  589. return $light;
  590. }
  591. /**
  592. * Get url of a png img of a website (16x16 pixel)
  593. */
  594. public function siteIconUrl($domain) {
  595. if (strpos($domain, 'http') === 0) {
  596. # strip protocol
  597. $pieces = parse_url($domain);
  598. $domain = $pieces['host'];
  599. }
  600. return 'http://www.google.com/s2/favicons?domain='.$domain;
  601. }
  602. /**
  603. * Display a png img of a website (16x16 pixel)
  604. * if not available, will return a fallback image (a globe)
  605. * @param domain (preferably without protocol, e.g. "www.site.com")
  606. */
  607. public function siteIcon($domain, $options = array()) {
  608. $url = $this->siteIconUrl($domain);
  609. $options['width'] = 16;
  610. $options['height'] = 16;
  611. if (!isset($options['alt'])) {
  612. $options['alt'] = $domain;
  613. }
  614. if (!isset($options['title'])) {
  615. $options['title'] = $domain;
  616. }
  617. return $this->Html->image($url, $options);
  618. }
  619. /**
  620. * @param string $text
  621. * @param array $options (for generation):
  622. * - inline, font, size, background (optional)
  623. * @param array $tagAttributes (for image)
  624. * @return string result - as image
  625. */
  626. public function textAsImage($text, $options = array(), $attr = array()) {
  627. /*
  628. $image = new Imagick();
  629. //$image->newImage(218, 46, new ImagickPixel('white'));
  630. $image->setImageCompression(10); // Keine Auswirkung auf Dicke
  631. $draw = new ImagickDraw();
  632. $draw->setFont($font);
  633. $draw->setFontSize(22.0); // Keine Auswirkung auf Dicke
  634. $draw->setFontWeight(100); // 0-999 Keine Auswirkung auf Dicke
  635. $draw->annotation(5, 20, $text);
  636. $image->drawImage($draw);
  637. $image->setImageResolution(1200, 1200); // Keine Auswirkung auf Dicke
  638. $image->setImageFormat('gif');
  639. $image->writeImage(TMP.'x.gif');
  640. $image->trim($mw,0);
  641. */
  642. $defaults = array('alt'=>$text);
  643. $attr = array_merge($defaults, $attr);
  644. return $this->_textAsImage($text, $options, $attr);
  645. }
  646. /**
  647. * @return string htmlImage tag (or empty string on failure)
  648. */
  649. public function _textAsImage($text, $options = array(), $attr = array()) {
  650. $defaults = array('inline'=>true, 'font' => FILES.'linotype.ttf', 'size'=>18, 'color'=>'#7A7166');
  651. $options = array_merge($defaults, $options);
  652. if ($options['inline']) { # inline base 64 encoded
  653. $folder = CACHE.'imagick';
  654. } else {
  655. $folder = WWW_ROOT.'img'.DS.'content'.DS.'imagick';
  656. }
  657. $file = sha1($text.serialize($options)).'.'.($options['inline'] || !empty($options['background']) ? 'png' : 'gif');
  658. if (!file_exists($folder)) {
  659. mkdir($folder, 0755);
  660. }
  661. if (!file_exists($folder.DS.$file)) {
  662. $command = 'convert -background '.(!empty($options['background'])?'"'.$options['background'].'"':'transparent').' -font '.$options['font'].' -fill '.(!empty($options['color'])?'"'.$options['color'].'"':'transparent').' -pointsize '.$options['size'].' label:"'.$text.'" '.$folder.DS.$file;
  663. exec($command, $a, $r);
  664. if ($r !== 0) {
  665. return '';
  666. }
  667. }
  668. if ($options['inline']) {
  669. $res = file_get_contents($folder.DS.$file);
  670. $out = $this->Html->imageFromBlob($res, $attr);
  671. } else {
  672. $out = $this->Html->image($this->Html->url('/img/content/imagick/', true).$file, $attr);
  673. }
  674. return $out;
  675. }
  676. /**
  677. */
  678. public function disabledLink($text, $options = array()) {
  679. $defaults = array('class' => 'disabledLink', 'title' => __('notAvailable'));
  680. $options = array_merge($defaults, $options);
  681. return $this->Html->tag('span', $text, $options);
  682. }
  683. /**
  684. * Display communication action depending on the current rule/right
  685. */
  686. public function action($s) {
  687. //TODO
  688. }
  689. /**
  690. * Generate a pagination count: #1 etc for each pagiation record
  691. * respects order (ASC/DESC)
  692. * @param paginator array
  693. * @param count (current post count on this page)
  694. * @param dir (ASC/DESC)
  695. */
  696. public function absolutePaginateCount($paginator, $count, $dir = null) {
  697. if ($dir === null) {
  698. $dir = 'ASC';
  699. }
  700. $currentPage = $paginator['page'];
  701. $pageCount = $paginator['pageCount'];
  702. $totalCount = $paginator['count'];
  703. $limit = $paginator['limit'];
  704. $step = 1; //$paginator['step'];
  705. //pr($paginator);
  706. if ($dir === 'DESC') {
  707. $currentCount = $count + ($pageCount - $currentPage) * $limit * $step;
  708. if ($currentPage != $pageCount && $pageCount > 1) {
  709. $currentCount -= $pageCount * $limit * $step - $totalCount;
  710. }
  711. } else {
  712. $currentCount = $count + ($currentPage-1) * $limit * $step;
  713. }
  714. return $currentCount;
  715. }
  716. /**
  717. * @param float progress
  718. * @param array options:
  719. * - min, max
  720. * - steps
  721. * - decimals (how precise should the result be displayed)
  722. *
  723. */
  724. public function progressBar($progress, $options = array(), $htmlOptions = array()) {
  725. $defaults = array(
  726. 'min' => 0,
  727. 'max' => 100,
  728. 'steps' => 15,
  729. 'decimals' => 1 # TODO: rename to places!!!
  730. );
  731. $options = array_merge($defaults, $options);
  732. $current = (((float)$progress / $options['max']) - $options['min']);
  733. $percent = $current * 100;
  734. $current *= $options['steps'];
  735. $options['progress'] = number_format($current, $options['decimals'], null, '');
  736. $params = Router::queryString($options, array(), true);
  737. App::uses('NumericHelper', 'Tools.View/Helper');
  738. $this->Numeric = new NumericHelper(new View(null));
  739. $htmlDefaults = array('title' => $this->Numeric->format($percent, $options['decimals']) . ' ' . __('Percent'), 'class' => 'help');
  740. $htmlDefaults['alt'] = $htmlDefaults['title'];
  741. $htmlOptions = array_merge($htmlDefaults, $htmlOptions);
  742. //return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions);
  743. # bug in Html::webroot() ??? ommits ?...
  744. return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' .
  745. $htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />';
  746. }
  747. public function tip($type, $file, $title, $icon) {
  748. return $this->cIcon($icon, $title, null, null, array('class' => 'tip' . ucfirst($type) . ' hand', 'rel' => $file));
  749. }
  750. public function tipHelp($file) {
  751. return $this->tip('help', $file, 'Hilfe', ICON_HELP);
  752. }
  753. /**
  754. * Fixes utf8 problems of native php str_pad function
  755. * @param string $input
  756. * @param integer $padLength
  757. * @param string $padString
  758. * @param mixed $padType
  759. * @return string input
  760. */
  761. public function pad($input, $padLength, $padString, $padType = STR_PAD_RIGHT) {
  762. $length = mb_strlen($input);
  763. if ($padLength - $length > 0) {
  764. switch ($padType) {
  765. case STR_PAD_LEFT:
  766. $input = str_repeat($padString, $padLength-$length).$input;
  767. break;
  768. case STR_PAD_RIGHT:
  769. $input .= str_repeat($padString, $padLength-$length);
  770. break;
  771. }
  772. }
  773. return $input;
  774. }
  775. /**
  776. * deprecated
  777. * album image
  778. */
  779. public function image($id, $options = array(), $attr = array()) {
  780. if (!empty($options['h'])) {
  781. $attr['height'] = $options['h'];
  782. }
  783. if (!empty($options['w'])) {
  784. $attr['width'] = $options['w'];
  785. }
  786. return $this->Html->image($this->imageUrl($id, $options), $attr);
  787. }
  788. public function imageUrl($id, $options = array()) {
  789. return $this->Html->defaultUrl($this->imageUrlArray($id, $options), true);
  790. }
  791. public function imageUrlArray($id, $options = array()) {
  792. $urlArray = array('controller' => 'images', 'action' => 'display', $id);
  793. $urlArray = array_merge($urlArray, $options);
  794. return $urlArray;
  795. }
  796. /**
  797. * Album image
  798. */
  799. public function albumImage($image, $options = array(), $attr = array()) {
  800. $subfolder = $image['Album']['id'];
  801. $file = $image['Album']['id'] . DS . $image['Image']['filename'] . '.' . $image['Image']['ext'];
  802. $optionsArray = array(
  803. 'save_path' => WWW_ROOT . 'img'. DS. 'albums' . DS . $subfolder,
  804. 'display_path' => '/img/albums' . '/' . $subfolder,
  805. 'error_image_path' => '/img/userpics/22.jpg',
  806. 'src' => FILES . 'images' . DS . $file,
  807. // From here on out, you can pass any standard phpThumb parameters
  808. 'q' => 100,
  809. 'zc' => 1
  810. );
  811. $options = array_merge($optionsArray, $options);
  812. if (!file_exists($options['save_path'])) {
  813. App::uses('Folder', 'Utility');
  814. $f = new Folder($options['save_path'], true, 0777);
  815. //mkdir($options['save_path'], 0777);
  816. }
  817. $thumbnail = $this->PhpThumb->generate($options);
  818. return $this->Html->image($thumbnail['src'], $attr); //'width' => $thumbnail['w'], 'height' => $thumbnail['h']
  819. }
  820. public function albumImageLink($image, $options = array()) {
  821. $options = array_merge(array('escape' => false), $options);
  822. return $this->Html->defaultLink($this->albumImage($image, array('h' => 50)), $this->imageUrlArray($image['Image']['id'], array('h' => 50)),
  823. $options);
  824. }
  825. public function albumImageTexts($image, $options = array()) {
  826. $display = array(
  827. 'title' => true,
  828. 'description' => true,
  829. 'controls' => true,
  830. 'autoHide' => true
  831. );
  832. if (!isset($image['Album']) && isset($options['album'])) {
  833. $image['Album'] = $options['album'];
  834. }
  835. if ($image['Album']['user_id'] != UID || isset($options['controls']) && $options['controls'] === false) {
  836. $display['controls'] = false;
  837. }
  838. if (isset($options['autoHide']) && $options['autoHide'] === false) {
  839. $display['autoHide'] = false;
  840. }
  841. $res = $controls = $descr = '';
  842. if ($display['title']) { // && (!$display['autoHide'] || !empty($image['Image']['title']))
  843. $defTitle = ($display['autoHide'] ? '' : ': <i>kein Titel</i>');
  844. $title = 'Album \'' . h($image['Album']['title']) . '\'' . (!empty($image['Image']['title']) ? ': ' . h($image['Image']['title']) : $defTitle);
  845. $res .= '<div class="highslide-heading">' . $title . '</div>';
  846. }
  847. $navigation = '<div class="floatRight">&nbsp;' . $this->Html->defaultLink($this->cIcon(ICON_ALBUM, 'Zum Album wechseln'),
  848. array('controller' => 'albums', 'action' => 'view', $image['Album']['id'], slug($image['Album']['title'])), array('escape' => false)) .
  849. '</div>';
  850. if (!empty($image['User']['id'])) {
  851. $gender = '';
  852. if (isset($image['UserInfo']['gender'])) {
  853. $gender = $this->genderIcon($image['UserInfo']['gender']).' ';
  854. }
  855. $navigation .= '<div class="floatRight" style="margin-right: 6px;">'.__('Member').': '.$gender.$this->profileLink($image['User']['id'], $image['User']['username']).'</div>';
  856. }
  857. if ($display['controls']) {
  858. $controls = '<div class="floatRight">&nbsp;' . $this->Html->defaultLink($this->icon('edit'), array('controller' => 'images',
  859. 'action' => 'edit', $image['Image']['id']), array('escape' => false)) . '&nbsp;' . $this->Form->postLink($this->icon('delete'),
  860. array('plugin'=>false, 'admin'=>false, 'controller' => 'images', 'action' => 'delete', $image['Image']['id']), array('escape' => false), 'Sicher?') . '</div>';
  861. }
  862. if ($display['description']) {
  863. $defDescr = ($display['autoHide'] ? '' : '<i>keine Beschreibung</i>');
  864. $descr = (!empty($image['Image']['description']) ? nl2br(h($image['Image']['description'])) : $defDescr);
  865. }
  866. if ($display['controls'] || $display['description']) {
  867. $res .= '<div class="highslide-caption">' . $controls . $navigation . $descr . '</div>';
  868. }
  869. return $res;
  870. }
  871. /**
  872. * Takes username + userId and forms a profile link out of it
  873. * if username is empty, return "deleted" text without link
  874. * maybe move to custom/community helper etc?
  875. */
  876. public function profileLink($uid, $username, $text = null, $attr = array(), $options = array()) {
  877. if (empty($username)) {
  878. return '['.__('%s deleted', __('Account')).']';
  879. }
  880. $title = isset($text) ? $text : $username;
  881. $username = slug($username);
  882. $url = array('plugin' => false, 'admin' => false, 'controller' => 'members', 'action' => 'view', $uid, $username);
  883. if (!empty($options['hash'])) {
  884. $url['#'] = $options['hash'];
  885. }
  886. return $this->Html->link($title, $url, $attr);
  887. }
  888. /**
  889. * @param mixed $uid
  890. * @param string $username
  891. * @param boolean $full
  892. * @param array $options
  893. * - hash (string)
  894. * @return string url
  895. */
  896. public function profileUrl($uid, $username, $full = false, $options = array()) {
  897. return $this->Html->url(array('plugin' => false, 'admin' => false, 'controller' => 'members', 'action' => 'view', $uid, slug($username)), $full);
  898. }
  899. /*
  900. public function profileLinkById($uid) {
  901. $username = null; //TODO: get from static list
  902. //$username = slug($username);
  903. return $this->Html->link($username, array('admin'=>false,'controller'=>'members', 'action'=>'view', $uid, $username));
  904. }
  905. */
  906. /**
  907. * Better an element?
  908. */
  909. public function profilePic($uid, $e, $rights = null) {
  910. }
  911. /**
  912. * FormatHelper::languageFlag()
  913. *
  914. * @param mixed $iso2
  915. * @param mixed $options
  916. * @return string
  917. */
  918. public function languageFlag($iso2, $options = array()) {
  919. $flag = '';
  920. $defaults = array('alt' => $iso2, 'title' => strtoupper($iso2));
  921. $options = array_merge($defaults, $options);
  922. $flag .= $this->Html->image('language_flags/' . strtolower($iso2) . '.gif', $options);
  923. return $flag;
  924. }
  925. /**
  926. * FormatHelper::countryAndProvince()
  927. *
  928. * @param mixed $array
  929. * @param mixed $options
  930. * @return string
  931. */
  932. public function countryAndProvince($array, $options = array()) {
  933. $res = '<span class="help" title="%s">%s</span>';
  934. $countryTitle = '';
  935. if (!empty($array['Country']['name'])) {
  936. $country = $array['Country']['iso2'];
  937. $countryTitle .= h($array['Country']['name']) . '';
  938. } else {
  939. $country = '';
  940. }
  941. if (!empty($array['CountryProvince']['name'])) {
  942. $countyProvince = h($array['CountryProvince']['abbr']);
  943. $countryTitle .= (!empty($countryTitle) ? ' - ' : '') . h($array['CountryProvince']['name']);
  944. } else {
  945. $countyProvince = '';
  946. $countryTitle .= '';
  947. }
  948. $content = $this->countryIcon($country) . '&nbsp;' . $countyProvince;
  949. $res = sprintf($res, $countryTitle, $content);
  950. return $res;
  951. }
  952. //deprecated?
  953. public function avatar($setting, $uid, $email = null, $options = array()) {
  954. $options = array_merge(array('title' => __('Avatar')), $options);
  955. if ($setting == USER_AVATAR_OWN) {
  956. if (!empty($options['size'])) {
  957. $options['height'] = $options['width'] = $options['size'];
  958. unset($options['size']);
  959. }
  960. return $this->Html->image(IMG_AVATARS . $uid . '.' . AVATAR_ENDING, $options);
  961. } elseif ($setting == USER_AVATAR_GRAV) {
  962. return $this->Gravatar->image($email, $options);
  963. } else {
  964. return $this->Gravatar->image($uid, $options);
  965. }
  966. }
  967. /**
  968. * Display traffic light for status etc
  969. */
  970. public function statusLight($color = null, $title = null, $alt = null, $options = array()) {
  971. $icons = array(
  972. 'green', 'yellow', 'red', 'blue'
  973. /*
  974. 'red' => array(
  975. 'title'=>'',
  976. 'alt'=>''
  977. ),
  978. */
  979. );
  980. $icon = (in_array($color, $icons) ? $color : 'blank');
  981. $defaultOptions = array('title' => (!empty($title) ? $title : ucfirst(__('color' . ucfirst($color)))), 'alt' => (!empty($alt) ? $alt :
  982. __('color' . ucfirst($color))), 'class' => 'icon help');
  983. $options = array_merge($defaultOptions, $options);
  984. return $this->Html->image('icons/status_light_' . $icon . '.gif', $options);
  985. }
  986. /**
  987. * FormatHelper::onlineIcon()
  988. *
  989. * @param mixed $modified
  990. * @param mixed $options
  991. * @return string
  992. */
  993. public function onlineIcon($modified = null, $options = array()) {
  994. # from low (off) to high (on)
  995. $icons = array('healthbar0.gif', 'healthbar1.gif', 'healthbar1b.gif', 'healthbar2.gif', 'healthbar3.gif', 'healthbar4.gif', 'healthbar5.gif');
  996. # default = offline
  997. $res = $icons[0]; // inaktiv
  998. $time = strtotime($modified);
  999. $timeAgo = time() - $time; # in seconds
  1000. if ($timeAgo < 180) { # 3min // aktiv
  1001. $res = $icons[6];
  1002. } elseif ($timeAgo < 360) { # 6min
  1003. $res = $icons[5];
  1004. } elseif ($timeAgo < 540) { # 9min
  1005. $res = $icons[4];
  1006. } elseif ($timeAgo < 720) { # 12min
  1007. $res = $icons[3];
  1008. } elseif ($timeAgo < 900) { # 15min
  1009. $res = $icons[2];
  1010. } elseif ($timeAgo < 1080) { # 18min
  1011. $res = $icons[1];
  1012. }
  1013. return $this->Html->image('misc/' . $res, array('style' => 'width: 60px; height: 16px'));
  1014. }
  1015. /**
  1016. * Returns red colored if not ok
  1017. *
  1018. * @param $okValue
  1019. * @return Value
  1020. */
  1021. public function warning($value, $ok = false) {
  1022. if ($ok !== true) {
  1023. return $this->ok($value, false);
  1024. }
  1025. return $value;
  1026. }
  1027. /**
  1028. * Returns green on ok, red otherwise
  1029. * @param mixed $currentValue
  1030. * @param boolean $ok: true/false (defaults to false)
  1031. * //@param string $comparizonType
  1032. * //@param mixed $okValue
  1033. * @return string newValue nicely formatted/colored
  1034. */
  1035. public function ok($value, $ok = false) {
  1036. if ($ok === true) {
  1037. $value = '<span class="green" style="color:green">' . $value . '</span>';
  1038. } else {
  1039. $value = '<span class="red" style="color:red">' . $value . '</span>';
  1040. }
  1041. return $value;
  1042. }
  1043. /**
  1044. * test@test.de becomes t..t@t..t.de
  1045. * @param string $email: valid(!) email address
  1046. */
  1047. public static function hideEmail($mail) {
  1048. $mailParts = explode('@', $mail, 2);
  1049. $domainParts = explode('.', $mailParts[1], 2);
  1050. $email = mb_substr($mailParts[0], 0, 1) . '..' . mb_substr($mailParts[0], -1, 1) . '@' . mb_substr($domainParts[0], 0, 1) . '..' . mb_substr($domainParts[0],
  1051. -1, 1) . '.' . $domainParts[1];
  1052. return $email;
  1053. }
  1054. /**
  1055. * (intelligent) Shortening of a text string
  1056. * @param STRING textstring
  1057. * @param integer chars = max-length
  1058. * For options array:
  1059. * @param BOOLEAN strict (default: FALSE = intelligent shortening, cutting only between whole words)
  1060. * @param STRING ending (default: '...' no leading whitespace)
  1061. * @param BOOLEAN remain_lf (default: false = \n to ' ')
  1062. * Note: ONLY If intelligent:
  1063. * - the word supposed to be cut is removed completely (instead of remaining as last one)
  1064. * - Looses line breaks (for textarea content to work with this)!
  1065. * @deprecated use truncate instead
  1066. */
  1067. public function shortenText($textstring, $chars, $options = array()) {
  1068. $chars++; # add +1 for correct cut
  1069. $needsEnding = false;
  1070. #Options
  1071. $strict = false;
  1072. $ending = CHAR_HELLIP; //'...';
  1073. $remain_lf = false; // not implemented: choose if LF transformed to ' '
  1074. $class = 'help';
  1075. $escape = true;
  1076. $title = '';
  1077. if (!empty($options) && is_array($options)) {
  1078. if (!empty($options['strict']) && ($options['strict'] === true || $options['strict'] === false)) {
  1079. $strict = $options['strict'];
  1080. }
  1081. if (!empty($options['remain_lf']) && ($options['remain_lf'] === true || $options['remain_lf'] === false)) {
  1082. $remain_lf = $options['remain_lf'];
  1083. }
  1084. if (isset($options['title'])) {
  1085. $title = $options['title'];
  1086. if ($options['title'] === true) {
  1087. $title = $textstring;
  1088. }
  1089. }
  1090. if (isset($options['class']) && $options['class'] === false) {
  1091. $class = '';
  1092. }
  1093. if (isset($options['ending'])) {
  1094. $ending = (string )$options['ending'];
  1095. }
  1096. if (isset($options['escape'])) {
  1097. $escape = (bool)$options['escape'];
  1098. }
  1099. }
  1100. $textstring = trim($textstring);
  1101. # cut only between whole words
  1102. if ($strict !== true) {
  1103. $completeWordText = $textstring . ' ';
  1104. # transform line breaks to whitespaces (for textarea content etc.)
  1105. $completeWordTextLf = str_replace(LF, ' ', $completeWordText);
  1106. $completeWordText = $completeWordTextLf;
  1107. $completeWordText = substr($completeWordTextLf, 0, $chars);
  1108. # round the text to the previous entire word instead of cutting off part way through a word
  1109. $completeWordText = substr($completeWordText, 0, strrpos($completeWordText, ' '));
  1110. }
  1111. $textEnding = '';
  1112. if ($strict !== true && strlen($completeWordText) > 1) {
  1113. $text = trim($completeWordText);
  1114. # add ending only if result is shorter then original
  1115. if (strlen($text) < strlen(trim($completeWordTextLf))) {
  1116. $textEnding = ' ' . $ending; # additional whitespace as there is a new word added
  1117. }
  1118. } else {
  1119. $text = trim(substr($textstring, 0, $chars));
  1120. # add ending only if result is shorter then original
  1121. if (strlen($text) < strlen($textstring)) {
  1122. $textEnding = $ending;
  1123. }
  1124. }
  1125. if ($escape) {
  1126. $text = h($text);
  1127. $title = h($title);
  1128. }
  1129. $text .= $textEnding;
  1130. #TitleIfTooLong
  1131. if (!empty($title)) {
  1132. $text = '<span ' . (!empty($class) ? 'class="' . $class . '" ' : '') . 'title="' . $title . '">' . $text . '</span>';
  1133. }
  1134. return $text;
  1135. }
  1136. /**
  1137. *
  1138. * Inspired by the tab2space function found at:
  1139. * @see http://aidan.dotgeek.org/lib/?file=function.tab2space.php
  1140. */
  1141. public function tab2space($text, $spaces = 4) {
  1142. $spaces = str_repeat(" ", $spaces);
  1143. $text = preg_split("/\r\n|\r|\n/", trim($text));
  1144. $word_lengths = array();
  1145. $w_array = array();
  1146. // Store word lengths
  1147. foreach ($text as $line) {
  1148. $words = preg_split("/(\t+)/", $line, -1, PREG_SPLIT_DELIM_CAPTURE);
  1149. foreach (array_keys($words) as $i) {
  1150. $strlen = strlen($words[$i]);
  1151. $add = isset($word_lengths[$i]) && ($word_lengths[$i] < $strlen);
  1152. if ($add || !isset($word_lengths[$i])) {
  1153. $word_lengths[$i] = $strlen;
  1154. }
  1155. }
  1156. $w_array[] = $words;
  1157. }
  1158. // Clear $text
  1159. $text = '';
  1160. // Apply padding when appropriate and rebuild the string
  1161. foreach (array_keys($w_array) as $i) {
  1162. foreach (array_keys($w_array[$i]) as $ii) {
  1163. if (preg_match("/^\t+$/", $w_array[$i][$ii])) {
  1164. $w_array[$i][$ii] = str_pad($w_array[$i][$ii], $word_lengths[$ii], "\t");
  1165. } else {
  1166. $w_array[$i][$ii] = str_pad($w_array[$i][$ii], $word_lengths[$ii]);
  1167. }
  1168. }
  1169. $text .= str_replace("\t", $spaces, implode("", $w_array[$i])) . "\n";
  1170. }
  1171. // Finished
  1172. return $text;
  1173. }
  1174. /**
  1175. * Word Censoring Function
  1176. *
  1177. * Supply a string and an array of disallowed words and any
  1178. * matched words will be converted to #### or to the replacement
  1179. * word you've submitted.
  1180. * @param string the text string
  1181. * @param string the array of censoered words
  1182. * @param string the optional replacement value
  1183. * @return string
  1184. */
  1185. public function wordCensor($str, $censored, $replacement = null) {
  1186. if (empty($censored)) {
  1187. return $str;
  1188. }
  1189. $str = ' ' . $str . ' ';
  1190. // \w, \b and a few others do not match on a unicode character
  1191. // set for performance reasons. As a result words like ..ber
  1192. // will not match on a word boundary. Instead, we'll assume that
  1193. // a bad word will be bookended by any of these characters.
  1194. $delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
  1195. foreach ($censored as $badword) {
  1196. if ($replacement !== null) {
  1197. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
  1198. } else {
  1199. $str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",
  1200. $str);
  1201. }
  1202. }
  1203. return trim($str);
  1204. }
  1205. /**
  1206. * Translate a result array into a HTML table
  1207. *
  1208. * @author Aidan Lister <aidan@php.net>
  1209. * @version 1.3.2
  1210. * @link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
  1211. * @param array $array The result (numericaly keyed, associative inner) array.
  1212. * @param boolean $recursive Recursively generate tables for multi-dimensional arrays
  1213. * @param string $null String to output for blank cells
  1214. */
  1215. public function array2table($array, $options = array()) {
  1216. $defaults = array(
  1217. 'null' => '&nbsp;',
  1218. 'recursive' => false,
  1219. 'heading' => true,
  1220. 'escape' => true
  1221. );
  1222. $options = array_merge($defaults, $options);
  1223. // Sanity check
  1224. if (empty($array) || !is_array($array)) {
  1225. return false;
  1226. }
  1227. if (!isset($array[0]) || !is_array($array[0])) {
  1228. $array = array($array);
  1229. }
  1230. // Start the table
  1231. $table = "<table>\n";
  1232. if ($options['heading']) {
  1233. // The header
  1234. $table .= "\t<tr>";
  1235. // Take the keys from the first row as the headings
  1236. foreach (array_keys($array[0]) as $heading) {
  1237. $table .= '<th>' . ($options['escape'] ? h($heading) : $heading) . '</th>';
  1238. }
  1239. $table .= "</tr>\n";
  1240. }
  1241. // The body
  1242. foreach ($array as $row) {
  1243. $table .= "\t<tr>";
  1244. foreach ($row as $cell) {
  1245. $table .= '<td>';
  1246. // Cast objects
  1247. if (is_object($cell)) {
  1248. $cell = (array)$cell;
  1249. }
  1250. if ($options['recursive'] && is_array($cell) && !empty($cell)) {
  1251. // Recursive mode
  1252. $table .= "\n" . self::array2table($cell, $options) . "\n";
  1253. } else {
  1254. $table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h($cell) : $cell) : $options['null'];
  1255. }
  1256. $table .= '</td>';
  1257. }
  1258. $table .= "</tr>\n";
  1259. }
  1260. $table .= '</table>';
  1261. return $table;
  1262. }
  1263. public $icons = array(
  1264. 'up' => array(
  1265. 'pic'=>ICON_UP,
  1266. 'title'=>'Up',
  1267. ),
  1268. 'down' => array(
  1269. 'pic'=>ICON_DOWN,
  1270. 'title'=>'Down',
  1271. ),
  1272. 'edit' => array(
  1273. 'pic'=>ICON_EDIT,
  1274. 'title'=>'Edit',
  1275. ),
  1276. 'view' => array(
  1277. 'pic'=>ICON_VIEW,
  1278. 'title'=>'View',
  1279. ),
  1280. 'delete' => array(
  1281. 'pic'=>ICON_DELETE,
  1282. 'title'=>'Delete',
  1283. ),
  1284. 'reset' => array(
  1285. 'pic'=>ICON_RESET,
  1286. 'title'=>'Reset',
  1287. ),
  1288. 'help' => array(
  1289. 'pic'=>ICON_HELP,
  1290. 'title'=>'Help',
  1291. ),
  1292. 'loader' => array(
  1293. 'pic'=>'loader.white.gif',
  1294. 'title'=>'Loading...',
  1295. ),
  1296. 'loader-alt' => array(
  1297. 'pic'=>'loader.black.gif',
  1298. 'title'=>'Loading...',
  1299. ),
  1300. 'details' => array(
  1301. 'pic'=>ICON_DETAILS,
  1302. 'title'=>'Details',
  1303. ),
  1304. 'use' => array(
  1305. 'pic'=>ICON_USE,
  1306. 'title'=>'Use',
  1307. ),
  1308. 'yes' => array(
  1309. 'pic'=>ICON_YES,
  1310. 'title'=>'Yes',
  1311. ),
  1312. 'no' => array(
  1313. 'pic'=>ICON_NO,
  1314. 'title'=>'No',
  1315. ),
  1316. # deprecated from here down
  1317. 'close' => array(
  1318. 'pic'=>ICON_CLOCK,
  1319. 'title'=>'Close',
  1320. ),
  1321. 'reply' => array(
  1322. 'pic'=>ICON_REPLY,
  1323. 'title'=>'Reply',
  1324. ),
  1325. 'time' => array(
  1326. 'pic'=>ICON_CLOCK,
  1327. 'title'=>'Time',
  1328. ),
  1329. 'check' => array(
  1330. 'pic'=>ICON_CHECK,
  1331. 'title'=>'Check',
  1332. ),
  1333. 'role' => array(
  1334. 'pic'=>ICON_ROLE,
  1335. 'title'=>'Role',
  1336. ),
  1337. 'add' => array(
  1338. 'pic'=>ICON_ADD,
  1339. 'title'=>'Add',
  1340. ),
  1341. 'remove' => array(
  1342. 'pic'=>ICON_REMOVE,
  1343. 'title'=>'Remove',
  1344. ),
  1345. 'email' => array(
  1346. 'pic'=>ICON_EMAIL,
  1347. 'title'=>'Email',
  1348. ),
  1349. 'options' => array(
  1350. 'pic'=>ICON_SETTINGS,
  1351. 'title'=>'Options',
  1352. ),
  1353. 'lock' => array(
  1354. 'pic'=>ICON_LOCK,
  1355. 'title'=>'Locked',
  1356. ),
  1357. 'warning' => array(
  1358. 'pic'=>ICON_WARNING,
  1359. 'title'=>'Warning',
  1360. ),
  1361. 'genderUnknown' => array(
  1362. 'pic'=>'gender_icon.gif',
  1363. 'title'=>'genderUnknown',
  1364. ),
  1365. 'genderMale' => array(
  1366. 'pic'=>'gender_icon_m.gif',
  1367. 'title'=>'genderMale',
  1368. ),
  1369. 'genderFemale' => array(
  1370. 'pic'=>'gender_icon_f.gif',
  1371. 'title'=>'genderFemale',
  1372. ),
  1373. );
  1374. }
  1375. # default icons
  1376. if (!defined('ICON_UP')) {
  1377. define('ICON_UP', 'up.gif');
  1378. }
  1379. if (!defined('ICON_DOWN')) {
  1380. define('ICON_DOWN', 'down.gif');
  1381. }
  1382. if (!defined('ICON_EDIT')) {
  1383. define('ICON_EDIT', 'edit.gif');
  1384. }
  1385. if (!defined('ICON_VIEW')) {
  1386. define('ICON_VIEW', 'see.gif');
  1387. }
  1388. if (!defined('ICON_DELETE')) {
  1389. define('ICON_DELETE', 'delete.gif');
  1390. }
  1391. if (!defined('ICON_DETAILS')) {
  1392. define('ICON_DETAILS', 'loupe.gif');
  1393. }
  1394. if (!defined('ICON_OPTIONS')) {
  1395. define('ICON_OPTIONS', 'options.gif');
  1396. }
  1397. if (!defined('ICON_SETTINGS')) {
  1398. define('ICON_SETTINGS', 'options.gif');
  1399. }
  1400. if (!defined('ICON_USE')) {
  1401. define('ICON_USE', 'use.gif');
  1402. }
  1403. if (!defined('ICON_CLOSE')) {
  1404. define('ICON_CLOSE', 'close.gif');
  1405. }
  1406. if (!defined('ICON_REPLY')) {
  1407. define('ICON_REPLY', 'reply.gif');
  1408. }
  1409. if (!defined('ICON_RESET')) {
  1410. define('ICON_RESET', 'reset.gif');
  1411. }
  1412. if (!defined('ICON_HELP')) {
  1413. define('ICON_HELP', 'help.gif');
  1414. }
  1415. if (!defined('ICON_YES')) {
  1416. define('ICON_YES', 'yes.gif');
  1417. }
  1418. if (!defined('ICON_NO')) {
  1419. define('ICON_NO', 'no.gif');
  1420. }
  1421. if (!defined('ICON_CLOCK')) {
  1422. define('ICON_CLOCK', 'clock.gif');
  1423. }
  1424. if (!defined('ICON_CHECK')) {
  1425. define('ICON_CHECK', 'check.gif');
  1426. }
  1427. if (!defined('ICON_ROLE')) {
  1428. define('ICON_ROLE', 'role.gif');
  1429. }
  1430. if (!defined('ICON_ADD')) {
  1431. define('ICON_ADD', 'add.gif');
  1432. }
  1433. if (!defined('ICON_REMOVE')) {
  1434. define('ICON_REMOVE', 'remove.gif');
  1435. }
  1436. if (!defined('ICON_EMAIL')) {
  1437. define('ICON_EMAIL', 'email.gif');
  1438. }
  1439. if (!defined('ICON_LOCK')) {
  1440. define('ICON_LOCK', 'lock.gif');
  1441. }
  1442. if (!defined('ICON_WARNING')) {
  1443. define('ICON_WARNING', 'warning.png');
  1444. }
  1445. if (!defined('ICON_MAP')) {
  1446. define('ICON_MAP', 'map.gif');
  1447. }