Autodoc
  • Namespace
  • Function
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • ActiveRecord
  • Cache
  • Configs
  • Core
  • DateTime
  • Debug
  • DeleteOperation
  • Errors
  • Event
  • EventHook
  • Events
  • FileCache
  • FormattedString
  • Helpers
  • I18n
  • Image
  • Inflections
  • Inflector
  • Models
  • Module
  • Modules
  • Object
  • Operation
  • PingOperation
  • Prototype
  • Route
  • Routes
  • SaveOperation
  • Session
  • TimeZone
  • TimeZoneLocation
  • Uploaded
  • Vars
  • VarsIterator

Interfaces

  • StorageInterface
  • ToArray
  • ToArrayRecursive

Traits

  • PrototypeTrait
  • ToArrayRecursiveTrait

Exceptions

  • AlreadyAuthenticated
  • AuthenticationRequired
  • Exception
  • ModuleConstructorMissing
  • ModuleIsDisabled
  • ModuleNotDefined
  • OffsetError
  • OffsetNotDefined
  • OffsetNotReadable
  • OffsetNotWritable
  • PermissionRequired
  • PropertyError
  • PropertyIsReserved
  • PropertyNotDefined
  • PropertyNotReadable
  • PropertyNotWritable
  • RouteNotDefined
  • SecurityException

Constants

  • TOKEN_ALPHA
  • TOKEN_ALPHA_UPCASE
  • TOKEN_NUMERIC
  • TOKEN_SYMBOL
  • TOKEN_SYMBOL_WIDE

Functions

  • array_flatten
  • array_insert
  • array_merge_recursive
  • camelize
  • capitalize
  • downcase
  • dump
  • escape
  • escape_all
  • exact_array_merge_recursive
  • excerpt
  • format
  • generate_token
  • generate_token_wide
  • generate_v4_uuid
  • get_autoconfig
  • humanize
  • hyphenate
  • log
  • log_error
  • log_info
  • log_success
  • log_time
  • normalize
  • normalize_namespace_part
  • normalize_url_path
  • pbkdf2
  • pluralize
  • remove_accents
  • shorten
  • singularize
  • sort_by_weight
  • stable_sort
  • strip_root
  • titleize
  • unaccent_compare
  • unaccent_compare_ci
  • underscore
  • upcase
  1 <?php
  2 
  3 /*
  4  * This file is part of the ICanBoogie package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace ICanBoogie;
 13 
 14 const TOKEN_NUMERIC = "23456789";
 15 const TOKEN_ALPHA = "abcdefghjkmnpqrstuvwxyz";
 16 const TOKEN_ALPHA_UPCASE = "ABCDEFGHJKLMNPQRTUVWXYZ";
 17 const TOKEN_SYMBOL = "!$=@#";
 18 const TOKEN_SYMBOL_WIDE = '%&()*+,-./:;<>?@[]^_`{|}~';
 19 
 20 define('ICanBoogie\TOKEN_NARROW', TOKEN_NUMERIC . TOKEN_ALPHA . TOKEN_SYMBOL);
 21 define('ICanBoogie\TOKEN_MEDIUM', TOKEN_NUMERIC . TOKEN_ALPHA . TOKEN_SYMBOL . TOKEN_ALPHA_UPCASE);
 22 define('ICanBoogie\TOKEN_WIDE', TOKEN_NUMERIC . TOKEN_ALPHA . TOKEN_SYMBOL . TOKEN_ALPHA_UPCASE . TOKEN_SYMBOL_WIDE);
 23 
 24 /**
 25  * Generate a password.
 26  *
 27  * @param int $length The length of the password. Default: 8
 28  * @param string $possible The characters that can be used to create the password.
 29  * If you defined your own, pay attention to ambiguous characters such as 0, O, 1, l, I...
 30  * Default: {@link TOKEN_NARROW}
 31  *
 32  * @return string
 33  */
 34 function generate_token($length=8, $possible=TOKEN_NARROW)
 35 {
 36     return Helpers::generate_token($length, $possible);
 37 }
 38 
 39 /**
 40  * Generate a 512 bit (64 chars) length token from {@link TOKEN_WIDE}.
 41  *
 42  * @param int $length=64
 43  * @param string $possible=TOKEN_WIDE
 44  *
 45  * @return string
 46  */
 47 function generate_token_wide()
 48 {
 49     return Helpers::generate_token(64, TOKEN_WIDE);
 50 }
 51 
 52 /** PBKDF2 Implementation (described in RFC 2898)
 53  *
 54  *  @param string $p password
 55  *  @param string $s salt
 56  *  @param int $c iteration count (use 1000 or higher)
 57  *  @param int $kl derived key length
 58  *  @param string $a hash algorithm
 59  *
 60  *  @return string derived key
 61  *
 62  *  @source http://www.itnewb.com/v/Encrypting-Passwords-with-PHP-for-Storage-Using-the-RSA-PBKDF2-Standard
 63  */
 64 function pbkdf2($p, $s, $c=1000, $kl=32, $a='sha256')
 65 {
 66     return Helpers::pbkdf2($p, $s, $c=1000, $kl=32, $a='sha256');
 67 }
 68 
 69 /**
 70  * Patchable helpers of the ICanBoogie package.
 71  *
 72  * @method string generate_token() generate_token($length=8, $possible=TOKEN_WIDE)
 73  * @method string pbkdf2() pbkdf2($p, $s, $c=1000, $kl=32, $a='sha256')
 74  */
 75 class Helpers
 76 {
 77     static private $jumptable = [
 78 
 79         'generate_token' => [ __CLASS__, 'generate_token' ],
 80         'pbkdf2' => [ __CLASS__, 'pbkdf2' ]
 81 
 82     ];
 83 
 84     /**
 85      * Calls the callback of a patchable function.
 86      *
 87      * @param string $name Name of the function.
 88      * @param array $arguments Arguments.
 89      *
 90      * @return mixed
 91      */
 92     static public function __callstatic($name, array $arguments)
 93     {
 94         return call_user_func_array(self::$jumptable[$name], $arguments);
 95     }
 96 
 97     /**
 98      * Patches a patchable function.
 99      *
100      * @param string $name Name of the function.
101      * @param collable $callback Callback.
102      *
103      * @throws \RuntimeException is attempt to patch an undefined function.
104      */
105     static public function patch($name, $callback)
106     {
107         if (empty(self::$jumptable[$name]))
108         {
109             throw new \RuntimeException("Undefined patchable: $name.");
110         }
111 
112         self::$jumptable[$name] = $callback;
113     }
114 
115     /*
116      * Default implementations
117      */
118 
119     static private function generate_token($length=8, $possible=TOKEN_NARROW)
120     {
121         $token = '';
122         $y = strlen($possible) - 1;
123 
124         while ($length--)
125         {
126             $i = mt_rand(0, $y);
127             $token .= $possible[$i];
128         }
129 
130         return $token;
131     }
132 
133     static private function pbkdf2($p, $s, $c=1000, $kl=32, $a='sha256')
134     {
135         $hl = strlen(hash($a, null, true)); # Hash length
136         $kb = ceil($kl / $hl); # Key blocks to compute
137         $dk = ''; # Derived key
138 
139         # Create key
140         for ($block = 1 ; $block <= $kb ; $block++)
141         {
142             # Initial hash for this block
143             $ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);
144             # Perform block iterations
145             for ( $i = 1; $i < $c; $i ++ )
146             # XOR each iterate
147             $ib ^= ($b = hash_hmac($a, $b, $p, true));
148             $dk .= $ib; # Append iterated block
149         }
150 
151         # Return derived key of correct length
152         return substr($dk, 0, $kl);
153     }
154 }
155 
156 /**
157  * Normalize a string to be suitable as a namespace part.
158  *
159  * @param string $part The string to normalize.
160  *
161  * @return string Normalized string.
162  */
163 function normalize_namespace_part($part)
164 {
165     return preg_replace_callback
166     (
167         '/[-\s_\.]\D/', function ($match)
168         {
169             $rc = ucfirst($match[0]{1});
170 
171             if ($match[0]{0} == '.')
172             {
173                 $rc = '\\' . $rc;
174             }
175 
176             return $rc;
177         },
178 
179         ' ' . $part
180     );
181 }
182 
183 /**
184  * Creates an excerpt of an HTML string.
185  *
186  * The following tags are preserved: A, P, CODE, DEL, EM, INS and STRONG.
187  *
188  * @param string $str HTML string.
189  * @param int $limit The maximum number of words.
190  *
191  * @return string
192  */
193 function excerpt($str, $limit=55)
194 {
195     static $allowed_tags = [
196 
197         'a', 'p', 'code', 'del', 'em', 'ins', 'strong'
198 
199     ];
200 
201     $str = strip_tags(trim($str), '<' . implode('><', $allowed_tags) . '>');
202     $str = preg_replace('#(<p>|<p\s+[^\>]+>)\s*</p>#', '', $str);
203 
204     $parts = preg_split('#<([^\s>]+)([^>]*)>#m', $str, 0, PREG_SPLIT_DELIM_CAPTURE);
205 
206     # i+0: text
207     # i+1: markup ('/' prefix for closing markups)
208     # i+2: markup attributes
209 
210     $rc = '';
211     $opened = [];
212 
213     foreach ($parts as $i => $part)
214     {
215         if ($i % 3 == 0)
216         {
217             $words = preg_split('#(\s+)#', $part, 0, PREG_SPLIT_DELIM_CAPTURE);
218 
219             foreach ($words as $w => $word)
220             {
221                 if ($w % 2 == 0)
222                 {
223                     if (!$word) // TODO-20100908: strip punctuation
224                     {
225                         continue;
226                     }
227 
228                     $rc .= $word;
229 
230                     if (!--$limit)
231                     {
232                         break;
233                     }
234                 }
235                 else
236                 {
237                     $rc .= $word;
238                 }
239             }
240 
241             if (!$limit)
242             {
243                 break;
244             }
245         }
246         else if ($i % 3 == 1)
247         {
248             if ($part[0] == '/')
249             {
250                 $rc .= '<' . $part . '>';
251 
252                 array_shift($opened);
253             }
254             else
255             {
256                 array_unshift($opened, $part);
257 
258                 $rc .= '<' . $part . $parts[$i + 1] . '>';
259             }
260         }
261     }
262 
263     if (!$limit)
264     {
265         $rc .= ' <span class="excerpt-warp">[…]</span>';
266     }
267 
268     if ($opened)
269     {
270         $rc .= '</' . implode('></', $opened) . '>';
271     }
272 
273     return $rc;
274 }
275 
276 /**
277  * Removes the `DOCUMENT_ROOT` from the provided path.
278  *
279  * Note: Because this function is usually used to create URL path from server path, the directory
280  * separator '\' is replaced by '/'.
281  *
282  * @param string $pathname
283  *
284  * @return string
285  */
286 function strip_root($pathname)
287 {
288     $root = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR);
289     $root = strtr($root, DIRECTORY_SEPARATOR == '/' ? '\\' : '/', DIRECTORY_SEPARATOR);
290     $pathname = strtr($pathname, DIRECTORY_SEPARATOR == '/' ? '\\' : '/', DIRECTORY_SEPARATOR);
291 
292     if ($root && strpos($pathname, $root) === 0)
293     {
294         $pathname = substr($pathname, strlen($root));
295     }
296 
297     if (DIRECTORY_SEPARATOR != '/')
298     {
299         $pathname = strtr($pathname, DIRECTORY_SEPARATOR, '/');
300     }
301 
302     return $pathname;
303 }
304 
305 /**
306  * Logs a message.
307  *
308  * @param string $message Message pattern.
309  * @param array $params The parameters used to format the message.
310  * @param string $message_id Message identifier.
311  * @param string $type Message type, one of "success", "error", "info" and "debug". Defaults to
312  * "debug".
313  */
314 function log($message, array $params=[], $message_id=null, $type='debug')
315 {
316     Debug::log($type, $message, $params, $message_id);
317 }
318 
319 /**
320  * Logs a success message.
321  *
322  * @param string $message Message pattern.
323  * @param array $params The parameters used to format the message.
324  * @param string $message_id Message identifier.
325  */
326 function log_success($message, array $params=[], $message_id=null)
327 {
328     Debug::log('success', $message, $params, $message_id);
329 }
330 
331 /**
332  * Logs an error message.
333  *
334  * @param string $message Message pattern.
335  * @param array $params The parameters used to format the message.
336  * @param string $message_id Message identifier.
337  */
338 function log_error($message, array $params=[], $message_id=null)
339 {
340     Debug::log('error', $message, $params, $message_id);
341 }
342 
343 /**
344  * Logs an info message.
345  *
346  * @param string $message Message pattern.
347  * @param array $params The parameters used to format the message.
348  * @param string $message_id Message identifier.
349  */
350 function log_info($message, array $params=[], $message_id=null)
351 {
352     Debug::log('info', $message, $params, $message_id);
353 }
354 
355 /**
356  * Logs a debug message associated with a timing information.
357  *
358  * @param string $message Message pattern.
359  * @param array $params The parameters used to format the message.
360  */
361 function log_time($message, array $params=[])
362 {
363     static $last;
364 
365     $now = microtime(true);
366 
367     $add = '<var>[';
368 
369     $add .= '∑' . number_format($now - $_SERVER['REQUEST_TIME_FLOAT'], 3, '\'', '') . '"';
370 
371     if ($last)
372     {
373         $add .= ', +' . number_format($now - $last, 3, '\'', '') . '"';
374     }
375 
376     $add .= ']</var>';
377 
378     $last = $now;
379 
380     $message = $add . ' ' . $message;
381 
382     log($message, $params);
383 }
384 
Autodoc API documentation generated by ApiGen 2.8.0