libtld 2.0.14
A library to determine the Top-Level Domain name of any Internet URI.
php_libtld.c
Go to the documentation of this file.
1/* TLD library -- PHP extension to call tld() and tld_check_uri() from PHP
2 * Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
215#include <php.h>
216#include "libtld/tld.h"
217
219PHP_FUNCTION(check_tld);
220ZEND_BEGIN_ARG_INFO_EX(arginfo_check_tld, 0, 0, 1)
221 ZEND_ARG_INFO(0, uri)
222ZEND_END_ARG_INFO()
223
226ZEND_BEGIN_ARG_INFO_EX(arginfo_check_uri, 0, 0, 1)
227 ZEND_ARG_INFO(0, uri)
228ZEND_END_ARG_INFO()
229
231PHP_FUNCTION(check_email);
232ZEND_BEGIN_ARG_INFO_EX(arginfo_check_email, 0, 0, 1)
233 ZEND_ARG_INFO(0, email)
234ZEND_END_ARG_INFO()
235
236/* {{{ pgsql_functions[]
237 */
238const zend_function_entry libtld_functions[] = {
239 PHP_FE(check_tld, arginfo_check_tld)
240 PHP_FE(check_uri, arginfo_check_uri)
241 PHP_FE(check_email, arginfo_check_email)
242 PHP_FE_END
243};
244/* }}} */
245
246/* {{{ pgsql_module_entry
247 */
248zend_module_entry libtld_module_entry = {
249 STANDARD_MODULE_HEADER,
250 "libtld",
252 NULL, //PHP_MINIT(libtld),
253 NULL, //PHP_MSHUTDOWN(libtld),
254 NULL, //PHP_RINIT(libtld),
255 NULL, //PHP_RSHUTDOWN(libtld),
256 NULL, //PHP_MINFO(libtld),
257 NO_VERSION_YET,
258 0, //PHP_MODULE_GLOBALS(libtld),
259 NULL, //PHP_GINIT(libtld),
260 NULL, // Global constructor
261 NULL, // Global destructor
262 NULL, // Post deactivate
263 STANDARD_MODULE_PROPERTIES_EX
264};
265/* }}} */
266
267zend_module_entry *get_module(void)
268{
269 return &libtld_module_entry;
270}
271
272
273/* {{{ proto mixed check_tld(string uri)
274 Check a URI and return the result or FALSE */
275PHP_FUNCTION(check_tld)
276{
277 char *query;
278 int query_len;
279 int argc = ZEND_NUM_ARGS();
280 struct tld_info info;
281 enum tld_result r;
282
283 if (argc != 1)
284 {
285 WRONG_PARAM_COUNT;
286 }
287
288 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE)
289 {
290 RETURN_FALSE;
291 }
292
293 r = tld(query, &info);
294
295 array_init(return_value);
296 add_assoc_long(return_value, "result", r);
297 add_assoc_long(return_value, "category", info.f_category);
298 add_assoc_long(return_value, "status", info.f_status);
299 add_assoc_long(return_value, "offset", info.f_offset);
300 if(info.f_country != NULL)
301 {
302 add_assoc_string(return_value, "country", (char *) info.f_country, 1);
303 }
304 if(info.f_tld != NULL)
305 {
306 add_assoc_string(return_value, "tld", (char *) info.f_tld, 1);
307 }
308}
309/* }}} */
310
311/* {{{ proto mixed check_uri(string uri, string protocols, int flags)
312 Check a complete URI for validity */
314{
315 char *query, *protocols;
316 int query_len, *protocols_len;
317 int argc = ZEND_NUM_ARGS();
318 long flags = 0;
319 struct tld_info info;
320 enum tld_result r;
321
322 if (argc != 3)
323 {
324 WRONG_PARAM_COUNT;
325 }
326
327 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl", &query, &query_len, &protocols, &protocols_len, &flags) == FAILURE)
328 {
329 RETURN_FALSE;
330 }
331
332 r = tld_check_uri(query, &info, protocols, flags);
333
334 array_init(return_value);
335 add_assoc_long(return_value, "result", r);
336 add_assoc_long(return_value, "category", info.f_category);
337 add_assoc_long(return_value, "status", info.f_status);
338 add_assoc_long(return_value, "offset", info.f_offset);
339 if(info.f_country != NULL)
340 {
341 add_assoc_string(return_value, "country", (char *) info.f_country, 1);
342 }
343 if(info.f_tld != NULL)
344 {
345 add_assoc_string(return_value, "tld", (char *) info.f_tld, 1);
346 }
347}
348/* }}} */
349
350/* {{{ proto mixed check_email(string eamil, int flags)
351 Check a string of emails as defined in an email field like the From: or To:
352 fields found in the email header. */
353PHP_FUNCTION(check_email)
354{
355 char *emails;
356 int emails_len;
357 int argc = ZEND_NUM_ARGS();
358 int idx;
359 long flags = 0;
360 struct tld_email_list *list;
361 struct tld_email email;
362 zval *email_arr;
363 enum tld_result r;
364
365 if (argc != 2)
366 {
367 WRONG_PARAM_COUNT;
368 }
369
370 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &emails, &emails_len, &flags) == FAILURE)
371 {
372 RETURN_FALSE;
373 }
374
376 if(list == NULL)
377 {
378 RETURN_FALSE;
379 }
380
381 array_init(return_value);
382
383 r = tld_email_parse(list, emails, flags);
384
385 idx = 0;
386 if(r == TLD_RESULT_SUCCESS)
387 {
388 while(tld_email_next(list, &email))
389 {
390 ALLOC_INIT_ZVAL(email_arr);
391 array_init(email_arr);
392 // only the group is optional because it is so rarely used
393 if(*email.f_group != '\0')
394 {
395 add_assoc_string(email_arr, "group", (char *) email.f_group, 1);
396 }
397 add_assoc_string(email_arr, "original_email", (char *) email.f_original_email, 1);
398 add_assoc_string(email_arr, "fullname", (char *) email.f_fullname, 1);
399 add_assoc_string(email_arr, "username", (char *) email.f_username, 1);
400 add_assoc_string(email_arr, "domain", (char *) email.f_domain, 1);
401 add_assoc_string(email_arr, "email_only", (char *) email.f_email_only, 1);
402 add_assoc_string(email_arr, "canonicalized_email", (char *) email.f_canonicalized_email, 1);
403
404 add_index_zval(return_value, idx, email_arr);
405 ++idx;
406 }
407 }
408 add_assoc_long(return_value, "count", idx);
409
410 // also return the result of the call
411 add_assoc_long(return_value, "result", r);
412
414}
415/* }}} */
416
417/* additional Doxygen documentation to not interfer with the PHP documentation. */
418
442/* vim: ts=4 sw=4 et
443 */
PHP_FUNCTION(check_tld)
Declaration of the check_tld() function in PHP.
Definition php_libtld.c:275
zend_module_entry * get_module(void)
Function called to retrieve the module information.
Definition php_libtld.c:267
const zend_function_entry libtld_functions[]
The list of functions we offer to PHP.
Definition php_libtld.c:238
zend_module_entry libtld_module_entry
The module definition.
Definition php_libtld.c:248
The C++ side of the email list implementation.
Definition tld.h:223
Parts of one email.
Definition tld.h:151
const char * f_group
The group this emails was defined in.
Definition tld.h:152
const char * f_canonicalized_email
The email including the display name.
Definition tld.h:158
const char * f_original_email
The email as read from the source.
Definition tld.h:153
const char * f_email_only
The complete email address without display name.
Definition tld.h:157
const char * f_username
The user being named in this email address.
Definition tld.h:155
const char * f_domain
The domain part of the email address.
Definition tld.h:156
const char * f_fullname
The user full or display name.
Definition tld.h:154
Set of information returned by the tld() function.
Definition tld.h:102
enum tld_category f_category
The category of the TLD.
Definition tld.h:103
enum tld_status f_status
The status of the TLD.
Definition tld.h:104
int f_offset
The offset to the TLD in the URI string you supplied.
Definition tld.h:107
char f_country[48]
The country where this TLD is used.
Definition tld.h:105
const char * f_tld
Pointer to the TLD in the URI string you supplied.
Definition tld.h:106
The public header of the libtld library.
LIBTLD_EXPORT void tld_email_free(struct tld_email_list *list)
Free the list of emails.
LIBTLD_EXPORT enum tld_result tld(const char *uri, struct tld_info *info)
Get information about the TLD for the specified URI.
Definition tld.cpp:1113
LIBTLD_EXPORT struct tld_email_list * tld_email_alloc()
Allocate a list of emails object.
LIBTLD_EXPORT enum tld_result tld_email_parse(struct tld_email_list *list, const char *emails, int flags)
Parse a list of emails in the email list object.
LIBTLD_EXPORT enum tld_result tld_check_uri(const char *uri, struct tld_info *info, const char *protocols, int flags)
Check that a URI is valid.
Definition tld.cpp:1311
tld_result
Definition tld.h:92
@ TLD_RESULT_SUCCESS
Success! The TLD of the specified URI is valid.
Definition tld.h:93
LIBTLD_EXPORT int tld_email_next(struct tld_email_list *list, struct tld_email *e)
Retrieve the next email.
void list()
List the default schemes accepted.
void check_uri(char const *uri)
Check the parameter as a URI.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.