FormatHelper.php 47 KB

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