-
Notifications
You must be signed in to change notification settings - Fork 0
/
alogo_utils.module
executable file
·442 lines (375 loc) · 10.4 KB
/
alogo_utils.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/**
* @file Alogo Utils
*/
/**
* Inserts or updated a record in the database based on a schema.
* This function aims at overcoming the fact that we don't always know if its an
* insert or update operation - which makes using drupal_write_record() not an option
* We use REPLACE INTO queries to work around this
*
* @param string $table
* @param stdClass $object
* @return bool
*/
function alogo_write_record($table, &$object) {
$schema = drupal_get_schema($table);
if (!$schema) {
return FALSE;
}
$pks = $schema['primary key'];
$query = "REPLACE INTO {$schema['name']} (";
$query .= implode(', ', array_keys($schema['fields']));
$query .= ') VALUES (';
$placeholders = array();
$quotes = array();
foreach ($schema['fields'] as $k => $v) {
// determine variable type
if (strpos('int', $v['type']) !== FALSE) {
$quotes = FALSE;
$placeholder = '%d';
}
else if (strpos('float', $v['type']) !== FALSE || strpos('double', $v['type']) !== FALSE) {
$quotes = FALSE;
$placeholder = '%f';
}
else {
// primary keys shouldn't really be binary in the first place
$placeholder = '%s';
$quotes = TRUE;
}
$placeholders[] = $placeholder;
$quotes[] = $quotes;
}
for ($i = 0, $n = count($placeholders); $i < $n; ++$i) {
if ($quotes[$i]) {
$query .= '"';
}
$query .= $placeholders[$i];
if ($quotes[$i]) {
$query .= '"';
}
if ($i !== ($n - 1)) {
$query .= ", ";
}
}
$query .= ")";
$array = (array) $object;
$result = db_query($query, $array);
return (bool) $result;
}
/**
* Retrieve a record from the databased based on a schema.
*
* @param string $table
* @param mixed $key A single value or an array of values
* @return array
*/
function alogo_read_record($table, $key) {
$key = (array) $key;
$schema = drupal_get_schema($table);
if (!$schema) {
return FALSE;
}
$pks = $schema['primary key'];
// make sure we have the same ammount of values as we do primary keys
$count = count($key);
if ($count !== count($pks)) {
return FALSE;
}
// build query
$query = "SELECT * FROM {$schema['name']} WHERE";
for ($i = 0; $i < $count; ++$i) {
// escape % signs
$name = str_replace('%', '%%', $pks[$i]);
$query .= " {$name} = ";
// determine variable type
if (is_numeric($key[$i])) {
$quotes = FALSE;
if (is_int($key[$i]) || ctype_digit($key[$i])) {
$placeholder = '%d';
}
else {
$placeholder = '%f';
}
}
else {
// primary keys shouldn't really be binary in the first place
$placeholder = '%s';
$quotes = TRUE;
}
if ($quotes) {
$query .= '"';
}
$query .= $placeholder;
if ($quotes) {
$query .= '"';
}
}
$result = db_query($query, $key);
return db_fetch_object($result);
}
/**
* Returns whether a user has a particular role
* Roles can either be a numeric RID or a string
*
* @param int|string $role
* @param int|stdClass $uid
* @return bool
*/
function alogo_user_has_role($role, $user = NULL) {
// defalts to current user
if (!isset($user)) {
global $user;
}
// accept a uid
else {
if (is_numeric($user)) {
$user = user_load($user);
}
}
// make sure we have a valid user object
if (is_object($user) && isset($user->roles) && is_array($user->roles)) {
return (is_numeric($role)) ? in_array($role, array_keys($user->roles)) : in_array($role, $user->roles);
}
return FALSE;
}
/**
* Implementation of hook_cron().
*/
function alogo_utils_cron() {
$resource = db_query("SHOW tables");
$tables = array();
while ($row = db_result($resource)) {
$tables[] = $row;
}
db_query('OPTIMIZE TABLE ' . implode(', ', $tables));
}
/**
* Convert a string to a slug, safe for use in urls
* @param string
* @return string
*/
function alogo_to_slug($string) {
$string = str_replace(array('/', ' ', '&'), '-', $string);
$accents = '/&([A-Za-z]{1,2})(tilde|grave|acute|circ|cedil|uml|lig);/';
return urlencode(strtolower(preg_replace($accents, '$1', htmlentities(utf8_decode($string)))));
}
/**
* Return absolute drupal site root
* @return string
*/
function alogo_docroot() {
$absolute_dir = dirname(__FILE__);
$relative_dir = drupal_get_path('module', 'alogo_utils');
return substr($absolute_dir, 0, -1 * (1 + strlen($relative_dir)));
}
/**
* Wrapper around krumo() that includes the class if not already done
* This is useful for debugging hook_init() and other places where krumo isn't normally available
*/
function alogo_krumo() {
include_once drupal_get_path('module', 'devel') . '/krumo/class.krumo.php';
krumo(func_get_args());
}
/**
* Calculate age from birthday
* @param string $bday
* - birthday (dd-mm-yyyy)
*/
function alogo_get_age($bday) {
list ($d, $m, $y) = explode('-', $bday);
$yd = date('Y') - $y;
if (date('md') < $m . $d) {
--$yd;
}
return $yd;
}
/**
* Clean the string removing all characters except the digits
* Talen from the module "Brazilian IDs"
*
* @param $cpf
* Original "dirty" cpf
* @return
* Returns the clean cpf
*/
function alogo_clean_cpf($cpf) {
return preg_replace('/[^0-9]/', '', $cpf);
}
/**
* Clean the string removing all characters except the digits
* Talen from the module "Brazilian IDs"
*
* @param $cnpj
* Original "dirty" cnpj
* @return
* Returns the clean cnpj
*/
function alogo_clean_cnpj($cnpj) {
return preg_replace('/[^0-9]/', '', $cnpj);
}
/**
* Validação de CPF
* @param string $cpf
* @param bool $strict
* @return bool
*/
function alogo_validate_cpf($cpf, $strict = FALSE) {
if ($strict === FALSE) {
$regex = '/^[0-9]{3}(.)?[0-9]{3}(.)?[0-9]{3}(-)?[0-9]{2}$/';
return (bool) preg_match($regex, $cpf);
}
else {
$cpf = alogo_clean_cpf($cpf);
$forbidden = array('00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999');
// Check if its not a forbidden combination
if (drupal_strlen($cpf) !== 11 || in_array($cpf, $forbidden)) {
return FALSE;
}
// Check the 11st and 12nd numbers
for ($numbers = 9; $numbers <= 10; $numbers++) {
$digit = 0;
for ($i = 0; $i < $numbers; $i++) {
$digit += $cpf{$i} * ($numbers + 1 - $i);
}
// Calculate the digit and check it
$digit = 11 - ($digit % 11);
if ($digit == 10 or $digit == 11) {
$digit = 0;
}
if ($digit != $cpf{$numbers}) {
return FALSE;
}
}
return TRUE;
}
}
/**
* Informa se o CPF é único
* Função feita para uso com o content_profile
*
* @param string $cpf
* @param int $uid
* @param string $profile_table
* @param string $profile_field
* @return bool
*/
function alogo_unique_cpf($cpf, $uid = NULL, $profile_table = 'content_type_profile', $profile_field = 'field_profile_cpf_value') {
$cpf_dirty = alogo_clean_cpf($cpf);
$query = <<<SQL
SELECT COUNT(*) FROM {$profile_table} ctt
INNER JOIN node n ON ctt.nid = n.nid
WHERE ctt.{$profile_field} = '%s' OR ctt.{$profile_field} = '%s'
SQL;
$args = array($cpf, $cpf_dirty);
if ($uid) {
$query .= " AND n.uid <> %d";
$args[] = $uid;
}
array_unshift($args, $query);
$resource = call_user_func_array('db_query', $args);
$result = db_result($resource);
return $result == 0;
}
/**
* Informa se o CNPJ é único
* Função feita para uso com o content_profile
*
* @param string $cnpj
* @param int $uid
* @param string $profile_table
* @param string $profile_field
* @return bool
*/
function alogo_unique_cnpj($cnpj, $uid = NULL, $profile_table = 'content_type_profile', $profile_field = 'field_profile_cnpj_value') {
$query = <<<SQL
SELECT COUNT(*) FROM {$profile_table} ctt
INNER JOIN node n ON ctt.nid = n.nid
WHERE ctt.{$profile_field} = '%s' OR ctt.{$profile_field} = '%s'
SQL;
$args = array($cnpj, $cnpj_dirty);
if ($uid) {
$query .= " AND n.uid <> %d";
$args[] = $uid;
}
array_unshift($args, $query);
$resource = call_user_func_array('db_query', $args);
$result = db_result($resource);
return $result == 0;
}
/**
* Validação de CNPJ
* @param string $cnpj
* @return bool
*/
function alogo_validate_cnpj($cnpj) {
$cnpj_dirty = $cnpj;
$cnpj = alogo_clean_cnpj($cnpj);
$forbidden = array('00000000000000', '11111111111111', '22222222222222', '33333333333333', '44444444444444', '55555555555555', '66666666666666', '77777777777777', '88888888888888', '99999999999999');
// Check if its not a forbidden combination
if (drupal_strlen($cnpj) !== 14 || in_array($cnpj, $forbidden)) {
return FALSE;
}
$i = 0;
while ($i < 14) {
$cnpj_d[$i] = drupal_substr($cnpj, $i, 1);
$i++;
}
$digit = ($cnpj[0] * 5) + ($cnpj[1] * 4) + ($cnpj[2] * 3) + ($cnpj[3] * 2) +
($cnpj[4] * 9) + ($cnpj[5] * 8) + ($cnpj[6] * 7) + ($cnpj[7] * 6) +
($cnpj[8] * 5) + ($cnpj[9] * 4) + ($cnpj[10] * 3) + ($cnpj[11] * 2);
// Calculate the digit and check it
$digit = 11 - ($digit % 11);
if ($digit == 10 or $digit == 11) {
$digit = 0;
}
if ($digit != $cnpj{12}) {
return FALSE;
}
$digit = ($cnpj[0] * 6) + ($cnpj[1] * 5) + ($cnpj[2] * 4) +
($cnpj[3] * 3) + ($cnpj[4] * 2) + ($cnpj[5] * 9) + ($cnpj[6] * 8) +
($cnpj[7] * 7) + ($cnpj[8] * 6) + ($cnpj[9] * 5) + ($cnpj[10] * 4) +
($cnpj[11] * 3) + ($digit * 2);
// Calculate the digit and check it
$digit = 11 - ($digit % 11);
if ($digit == 10 or $digit == 11) {
$digit = 0;
}
if ($digit != $cnpj{13}) {
return FALSE;
}
return TRUE;
}
/**
* Returns a fully-processed node form as an array
* This should work in cases where drupal_retrieve_form() doesn't
*
* @param string $type
* @return array
*/
function alogo_get_node_form($type) {
$node = new StdClass();
$node_state = array();
$node->type = $type;
// for some reason this isn't always included when we need it
module_load_include('inc', 'node', 'node.pages');
// grab the node form
$node_form = node_form($node_state, $node);
// run all hooks
drupal_prepare_form("{$type}_node_form", $node_form);
// there, we have the full node form as an array, horray
return $node_form;
}
/**
* Returns whether the email address is unique or not
* This is necessary in cases when we have a custom registration workflow
*
* @param string $email
* @return bool
*/
function alogo_unique_email($email) {
$query = "SELECT COUNT(*) FROM users WHERE mail = '%s'";
$result = db_result(db_query($query, $email));
return $result === 0;
}