Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
StringUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class StringUtil
41  {
42  const VALID_HASH_PATTERN = '~^[A-Z0-9\+=/ ]+~i'; // Not Coding Standard
43 
51  public static function getChoppedStringContent($string, $length, $ellipsis = '...')
52  {
53  assert('is_string($string) || $string === null');
54  assert('is_int($length)');
55  if ($string != null && strlen($string) > $length)
56  {
57  return substr($string, 0, ($length - 3)) . $ellipsis;
58  }
59  else
60  {
61  return $string;
62  }
63  }
64 
69  public static function resolveOrdinalIntegerAsStringContent($integer)
70  {
71  assert('is_int($integer)');
72  $ends = array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th');
73  if (($integer %100) >= 11 && ($integer%100) <= 13)
74  {
75  return $integer. 'th';
76  }
77  else
78  {
79  return $integer. $ends[$integer % 10];
80  }
81  }
82 
83  public static function renderFluidContent($content)
84  {
85  assert('$content == null || is_string($content)');
86  if ($content != null)
87  {
88  // Begin Not Coding Standard
89  Yii::app()->clientScript->registerScript('TruncateTitleText', "
90  $(function() {
91  $('.truncated-title').ThreeDots({ max_rows:1 });
92  });");
93  // End Not Coding Standard
94  $innerContent = ZurmoHtml::wrapLabel(strip_tags($content), 'ellipsis-content');
95  $content = ZurmoHtml::wrapLabel($innerContent, 'truncated-title');
96  return $content;
97  }
98  }
99 
100  public static function renderFluidTitleContent($title, $postTruncatedTitleContent = null)
101  {
102  assert('$title == null || is_string($title)');
103  $content = static::renderFluidContent($title);
104  $content .= $postTruncatedTitleContent;
105  return ZurmoHtml::tag('h1', array(), $content);
106  }
107 
111  public static function resolveCustomizedLabel()
112  {
113  return strtolower(preg_replace('/[^\da-z]/i', '', Yii::app()->label));
114  }
115 
116  public static function uncamelize($string )
117  {
118  $string[0] = strtolower($string[0]);
119  $uncamelizeFunction = create_function('$c', 'return "_" . strtolower($c[1]);');
120  return preg_replace_callback( '/([A-Z])/', $uncamelizeFunction, $string);
121  }
122 
123  public static function camelize($string, $capitaliseFirstCharacter = false, $delimiter = '_')
124  {
125  if ($capitaliseFirstCharacter)
126  {
127  $string[0] = strtoupper($string[0]);
128  }
129  $camelizeFunction = create_function('$character', 'return strtoupper($character[1]);');
130  return preg_replace_callback('/' . preg_quote($delimiter) . '([a-z])/', $camelizeFunction, $string);
131  }
132 
138  public static function prependNewLine(& $content, $isHtmlContent)
139  {
140  if ($isHtmlContent)
141  {
142  $content = ZurmoHtml::tag('br') . $content;
143  }
144  else
145  {
146  $content = PHP_EOL . $content;
147  }
148  }
149 
156  public static function generateRandomString($length = 10, $characterSet = null)
157  {
158  if (empty($characterSet))
159  {
160  $characterSet = implode(range("A", "Z")) . implode(range("a", "z")) . implode(range("0", "9"));
161  }
162  $characterSetLength = strlen($characterSet);
163  $randomString = '';
164  for ($i = 0; $i < $length; $i++)
165  {
166  $randomCharacter = $characterSet[rand(0, $characterSetLength - 1)];
167  $randomString .= $randomCharacter;
168  }
169  return $randomString;
170  }
171 
177  public static function startsWith($haystack, $needle)
178  {
179  return $needle === "" || strpos($haystack, $needle) === 0;
180  }
181 
187  public static function endsWith($haystack, $needle)
188  {
189  return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
190  }
191 
198  public static function addSchemeIfMissing($url, $scheme = 'http')
199  {
200  if (!preg_match("~^(?:f|ht)tps?://~i", $url))
201  {
202  $url = $scheme ."://" . $url;
203  }
204  return $url;
205  }
206 
207  public static function resolveHashForQueryStringArray($queryStringArray)
208  {
209  $queryString = http_build_query($queryStringArray);
210  $encryptedString = ZurmoPasswordSecurityUtil::encrypt($queryString);
211  if (!$encryptedString || !static::isValidHash($encryptedString))
212  {
213  throw new NotSupportedException();
214  }
215  $encryptedString = base64_encode($encryptedString);
216  return $encryptedString;
217  }
218 
219  public static function isValidHash($hash)
220  {
221  if (empty($hash))
222  {
223  return false;
224  }
225  $matches = array();
226  $matchesCount = preg_match_all(static::VALID_HASH_PATTERN, $hash, $matches);
227  if (!$matchesCount || ($matches[0][0] !== $hash))
228  {
229  return false;
230  }
231  return true;
232  }
233 
240  public static function convertToBytes($value)
241  {
242  $strippedValue = substr($value, 0, -1);
243  switch(strtoupper(substr($value, -1)))
244  {
245  case "K":
246  return $strippedValue * 1024;
247  case "M":
248  return $strippedValue * pow(1024, 2);
249  case "G":
250  return $strippedValue * pow(1024, 3);
251  default:
252  return $value;
253  }
254  }
255 
261  public static function makeWordsUpperCase($string)
262  {
263  return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
264  }
265  }
266 ?>
static resolveCustomizedLabel()
Definition: StringUtil.php:111
static wrapLabel($label, $class= 'z-label')
Definition: ZurmoHtml.php:656
static getChoppedStringContent($string, $length, $ellipsis= '...')
Definition: StringUtil.php:51
static startsWith($haystack, $needle)
Definition: StringUtil.php:177
static endsWith($haystack, $needle)
Definition: StringUtil.php:187
static encrypt($value, $salt=ZURMO_PASSWORD_SALT)
static makeWordsUpperCase($string)
Definition: StringUtil.php:261
static prependNewLine(&$content, $isHtmlContent)
Definition: StringUtil.php:138
static resolveOrdinalIntegerAsStringContent($integer)
Definition: StringUtil.php:69
static addSchemeIfMissing($url, $scheme= 'http')
Definition: StringUtil.php:198
static convertToBytes($value)
Definition: StringUtil.php:240
static generateRandomString($length=10, $characterSet=null)
Definition: StringUtil.php:156
Generated on Mon Jul 13 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.