2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.02 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available at through the world-wide-web at |
10 | http://www.php.net/license/2_02.txt. |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 +----------------------------------------------------------------------+
21 * "$Id: phpcups.c 4530 2005-06-01 20:15:51Z mike $"
23 * Printing utilities for the Common UNIX Printing System (CUPS).
25 * Copyright 1997-2002 by Easy Software Products.
27 * These coded instructions, statements, and computer programs are the
28 * property of Easy Software Products and are protected by Federal
29 * copyright law. Distribution and use rights are outlined in the file
30 * "LICENSE.txt" which should have been included with this file. If this
31 * file is missing or damaged please contact Easy Software Products
34 * Attn: CUPS Licensing Information
35 * Easy Software Products
36 * 44141 Airport View Drive, Suite 204
37 * Hollywood, Maryland 20636-3111 USA
39 * Voice: (301) 373-9603
40 * EMail: cups-info@cups.org
41 * WWW: http://www.cups.org
50 #include "ext/standard/info.h"
51 #include "php_phpcups.h"
54 * Include necessary headers...
58 #include <cups/cups.h>
60 #include <cups/language.h>
61 #include <cups/string.h>
62 #include <cups/debug.h>
68 #if defined(WIN32) || defined(__EMX__)
72 #endif /* WIN32 || __EMX__ */
75 static int le_result
, le_link
, le_plink
;
84 static http_t
*cups_server
= NULL
; /* Current server connection */
85 static ipp_status_t last_error
= IPP_OK
; /* Last IPP error */
86 static char authstring
[HTTP_MAX_VALUE
] = "";
87 /* Authorization string */
88 static char pwdstring
[33] = ""; /* Last password string */
95 * *********************************************************************
101 /* If you declare any globals in php_phpcups.h uncomment this:
102 ZEND_DECLARE_MODULE_GLOBALS(phpcups)
105 /* True global resources - no need for thread safety here */
106 static int le_phpcups
;
109 * Every user visible function must have an entry in phpcups_functions[].
111 function_entry phpcups_functions
[] = {
112 PHP_FE(confirm_phpcups_compiled
,NULL
)
113 PHP_FE(cups_get_dest_list
, NULL
)
114 PHP_FE(cups_get_dest_options
, NULL
)
115 PHP_FE(cups_get_jobs
, NULL
)
116 PHP_FE(cups_cancel_job
, NULL
)
117 PHP_FE(cups_last_error
, NULL
)
118 PHP_FE(cups_print_file
, NULL
)
119 PHP_FE(cups_get_printer_attributes
, NULL
)
126 zend_module_entry phpcups_module_entry
= {
127 STANDARD_MODULE_HEADER
,
131 PHP_MSHUTDOWN(phpcups
),
132 PHP_RINIT(phpcups
), /* Replace with NULL if there's nothing to do at request start */
133 PHP_RSHUTDOWN(phpcups
), /* Replace with NULL if there's nothing to do at request end */
135 "0.1", /* Replace with version number for your extension */
136 STANDARD_MODULE_PROPERTIES
139 #ifdef COMPILE_DL_PHPCUPS
140 ZEND_GET_MODULE(phpcups
)
146 /* Remove comments and fill if you need to have entries in php.ini
148 STD_PHP_INI_ENTRY("phpcups.value", "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_phpcups_globals, phpcups_globals)
149 STD_PHP_INI_ENTRY("phpcups.string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_phpcups_globals, phpcups_globals)
156 /* Uncomment this function if you have INI entries
157 static void php_phpcups_init_globals(zend_phpcups_globals *phpcups_globals)
159 phpcups_globals->value = 0;
160 phpcups_globals->string = NULL;
167 PHP_MINIT_FUNCTION(phpcups
)
169 /* If you have INI entries, uncomment these lines
170 ZEND_INIT_MODULE_GLOBALS(phpcups, php_phpcups_init_globals, NULL);
171 REGISTER_INI_ENTRIES();
176 PHP_MSHUTDOWN_FUNCTION(phpcups
)
178 /* uncomment this line if you have INI entries
179 UNREGISTER_INI_ENTRIES();
184 /* Remove if there's nothing to do at request start */
185 PHP_RINIT_FUNCTION(phpcups
)
190 /* Remove if there's nothing to do at request end */
191 PHP_RSHUTDOWN_FUNCTION(phpcups
)
198 PHP_MINFO_FUNCTION(phpcups
)
200 php_info_print_table_start();
201 php_info_print_table_header(2, "phpcups support", "enabled");
202 php_info_print_table_end();
204 /* Remove comments if you have entries in php.ini
205 DISPLAY_INI_ENTRIES();
215 /* Remove the following function when you have succesfully modified config.m4
216 so that your module can be compiled into PHP, it exists only for testing
219 /* Every user-visible function in PHP should document itself in the source */
220 PHP_FUNCTION(confirm_phpcups_compiled
)
226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &arg
, &arg_len
) == FAILURE
) {
230 len
= sprintf(string
, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "phpcups", arg
);
231 RETURN_STRINGL(string
, len
, 1);
233 /* The previous line is meant for vim and emacs, so it can correctly fold and
234 unfold functions in source code. See the corresponding marks just before
235 function definition, where the functions purpose is also documented. Please
236 follow this convention for the convenience of others editing your code.
250 * Function: cups_get_dest_options
252 * Date: 8 April 2002 - TDB
254 * Parameters: d_name - String - Name of destination.
255 * d_instance - String - Name of instance on destination.
257 * Returns: Array of option "objects", with each object
258 * containing the members:
260 * name - String - Option name
261 * value - String - Option value
266 PHP_FUNCTION(cups_get_dest_options
)
285 cups_dest_t
*dests
, *dptr
;
289 array_init(return_value
);
291 if ((ZEND_NUM_ARGS() != 3) ||
292 (zend_get_parameters_ex(3,&d_server
,&d_name
,&d_instance
) != SUCCESS
))
297 convert_to_string_ex( d_server
);
298 convert_to_string_ex( d_name
);
299 convert_to_string_ex( d_instance
);
302 * Find the dest/instance we want options for.
304 bzero( c_server
, 256 );
305 bzero( c_name
, 256 );
306 bzero( c_instance
, 256 );
308 if ( (char *)(*d_server
)->value
.str
.val
!= NULL
)
309 strcpy( c_server
,(char *)(*d_server
)->value
.str
.val
);
310 if ( (char *)(*d_name
)->value
.str
.val
!= NULL
)
311 strcpy( c_name
,(char *)(*d_name
)->value
.str
.val
);
312 if ( (char *)(*d_instance
)->value
.str
.val
!= NULL
)
313 strcpy( c_instance
,(char *)(*d_instance
)->value
.str
.val
);
316 if (strlen(c_server
))
317 cupsSetServer(c_server
);
319 num_dests
= cupsGetDests(&dests
);
320 for (i
=0, j
= -1; (i
< num_dests
) && (j
< 0); i
++)
324 if (dptr
->name
== NULL
)
325 strcpy( l_name
, "" );
327 strcpy( l_name
, dptr
->name
);
329 if (dptr
->instance
== NULL
)
330 strcpy( l_instance
, "" );
332 strcpy( l_instance
, dptr
->instance
);
334 if ((!strcmp( l_name
, c_name
)) &&
335 (!strcmp( l_instance
, c_instance
)))
338 for (j
=0; j
< dptr
->num_options
; j
++ )
340 if ((dptr
->options
[j
].name
!= NULL
) &&
341 (dptr
->options
[j
].value
!= NULL
))
343 MAKE_STD_ZVAL(new_object
);
344 if (object_init(new_object
) == SUCCESS
)
346 add_property_string(new_object
,"name",dptr
->options
[j
].name
, 1 );
347 add_property_string(new_object
,"value",dptr
->options
[j
].value
,1);
348 add_next_index_zval( return_value
, new_object
);
354 cupsFreeDests(num_dests
,dests
);
361 * Function: cups_get_dest_list
363 * Date: 8 April 2002 - TDB
365 * Parameters: cups server (optional)
367 * Returns: Array of destination "objects", with each object
368 * containing the members:
370 * name - String - Name of destination.
371 * instance - String - Name of instance on destination.
372 * is_default - Long - 1 if default printer.
373 * num_options - Long - Number of options for destination.
378 PHP_FUNCTION(cups_get_dest_list
)
391 cups_dest_t
*dests
, *dptr
;
396 * Initialize the return array.
398 array_init(return_value
);
400 switch (ZEND_NUM_ARGS())
403 * Change servers if passed.
405 case 1: if (zend_get_parameters_ex(1,&z_server
) != SUCCESS
)
409 convert_to_string_ex( z_server
);
410 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
412 strcpy( c_server
,(char *)(*z_server
)->value
.str
.val
);
413 cupsSetServer( c_server
);
420 * First get the destination list from the cups server.
422 num_dests
= cupsGetDests(&dests
);
425 * Loop through the list, create and fill in the objects, and
426 * add them to the array.
429 for (i
=0; i
< num_dests
; i
++)
433 MAKE_STD_ZVAL(new_object
);
434 if (object_init(new_object
) == SUCCESS
)
436 if (strlen(c_server
))
437 add_property_string( new_object
, "server", c_server
, 1 );
439 add_property_string( new_object
, "server", "", 1 );
441 if (dptr
->name
!= NULL
)
442 add_property_string( new_object
, "name", dptr
->name
, 1 );
444 add_property_string( new_object
, "name", "", 1 );
446 if (dptr
->instance
!= NULL
)
447 add_property_string( new_object
, "instance", dptr
->instance
, 1 );
449 add_property_string( new_object
, "instance", "", 1 );
451 add_property_long( new_object
, "is_default", dptr
->is_default
);
452 add_property_long( new_object
, "num_options", dptr
->num_options
);
453 add_next_index_zval( return_value
, new_object
);
458 * free the list .....
460 cupsFreeDests(num_dests
,dests
);
466 * Function: cups_get_jobs
468 * Date: 8 April 2002 - TDB
470 * Parameters: server - String - Name or IP of cups server. Blank
473 * name - String - Name of destination to query.
475 * user - String - Username to get job list for.
476 * * Optional, default all users
478 * my_jobs - Long - Show only my jobs
479 * * Optional, default FALSE
481 * completed - Long - Show completed jobs
482 * * Optional, default FALSE
484 * Returns: Array of print job "objects", with each object
485 * containing the members:
488 * dest - String - Name of destination.
489 * title - String - Title of document.
490 * user - String - User who submitted job.
491 * format - String - Document format (MIME type)
492 * state - Long - Current state of the job.
493 * size - Long - Size in bytes of the job.
494 * priority - Long - Job priority.
495 * completed_time - Long - Time job completed (UNIX time).
496 * creation_time - Long - Time job created (UNIX time).
497 * processing_time- Long - Processing time in seconds?
502 PHP_FUNCTION( cups_get_jobs
)
530 bzero( p_server
, 256 );
531 bzero( p_name
, 256 );
532 bzero( p_user
, 256 );
537 * Initialize return value.
539 array_init(return_value
);
544 switch(ZEND_NUM_ARGS())
547 * server, destination only provided.
550 if (zend_get_parameters_ex( 2, &z_server
,&z_name
) == SUCCESS
)
552 convert_to_string_ex( z_server
);
553 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
554 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
555 convert_to_string_ex( z_name
);
556 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
557 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
562 * server, destination and user
565 if (zend_get_parameters_ex( 3, &z_server
, &z_name
,
566 &z_user
) == SUCCESS
)
568 convert_to_string_ex( z_name
);
569 convert_to_string_ex( z_user
);
570 convert_to_string_ex( z_server
);
571 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
572 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
573 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
574 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
575 if ( (char *)(*z_user
)->value
.str
.val
!= NULL
)
576 strcpy( p_user
,(char *)(*z_user
)->value
.str
.val
);
581 * server, destination, user, and myjobs
584 if (zend_get_parameters_ex( 4, &z_server
, &z_name
, &z_user
,
585 &z_myjobs
) == SUCCESS
)
587 convert_to_string_ex( z_name
);
588 convert_to_string_ex( z_user
);
589 convert_to_string_ex( z_server
);
590 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
591 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
592 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
593 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
594 if ( (char *)(*z_user
)->value
.str
.val
!= NULL
)
595 strcpy( p_user
,(char *)(*z_user
)->value
.str
.val
);
597 convert_to_string_ex( z_myjobs
);
598 p_myjobs
= strtoul((char *)(*z_myjobs
)->value
.str
.val
,NULL
,10);
603 * server, destination, user, myjobs, and completed
606 if (zend_get_parameters_ex( 5, &z_server
, &z_name
, &z_user
,
607 &z_myjobs
, &z_completed
) == SUCCESS
)
609 convert_to_string_ex( z_name
);
610 convert_to_string_ex( z_user
);
611 convert_to_string_ex( z_server
);
612 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
613 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
614 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
615 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
616 if ( (char *)(*z_user
)->value
.str
.val
!= NULL
)
617 strcpy( p_user
,(char *)(*z_user
)->value
.str
.val
);
619 convert_to_string_ex( z_myjobs
);
620 p_myjobs
= strtoul((char *)(*z_myjobs
)->value
.str
.val
,NULL
,10);
622 convert_to_string_ex( z_completed
);
623 p_completed
= strtoul((char *)(*z_completed
)->value
.str
.val
,NULL
,10);
629 * Set the cups server if given.
631 if (strlen(p_server
))
632 cupsSetServer(p_server
);
635 * Set the cups user if given, otherwise get all jobs.
643 * Get the jobs list from the CUPS server.
645 num_jobs
= cupsGetJobs(&jobs
,p_name
,p_myjobs
,p_completed
);
648 * Build the array of objects to return.
650 for (i
=0; i
< num_jobs
; i
++)
654 MAKE_STD_ZVAL(new_object
);
655 if (object_init(new_object
) == SUCCESS
)
657 add_property_long(new_object
,"id",jptr
->id
);
658 add_property_string(new_object
,"dest",jptr
->dest
, 1 );
659 add_property_string(new_object
,"title",jptr
->title
, 1 );
660 add_property_string(new_object
,"user",jptr
->user
, 1 );
661 add_property_string(new_object
,"format",jptr
->format
, 1 );
662 add_property_long(new_object
,"state",jptr
->state
);
663 add_property_long(new_object
,"size",jptr
->size
);
664 add_property_long(new_object
,"priority",jptr
->priority
);
665 add_property_long(new_object
,"completed_time",jptr
->completed_time
);
666 add_property_long(new_object
,"creation_time",jptr
->creation_time
);
667 add_property_long(new_object
,"processing_time",jptr
->processing_time
);
668 add_next_index_zval( return_value
, new_object
);
672 printf("\nError creating new object\n");
675 cupsFreeJobs(num_jobs
,jobs
);
682 * Function: cups_last_error
684 * Date: 8 April 2002 - TDB
688 * Returns: error number - Long
693 PHP_FUNCTION(cups_last_error
)
695 static char error
[1024];
700 bzero( c_server
, 256 );
702 switch (ZEND_NUM_ARGS())
705 * Change servers if passed.
707 case 1: if (zend_get_parameters_ex(1,&z_server
) != SUCCESS
)
711 convert_to_string_ex( z_server
);
712 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
714 strcpy( c_server
,(char *)(*z_server
)->value
.str
.val
);
715 cupsSetServer( c_server
);
719 sprintf( error
,"%d", cupsLastError());
721 RETURN_STRINGL(error
, strlen(error
)+1, 1);
730 * Function: cups_cancel_job
732 * Date: 8 April 2002 - TDB
734 * Parameters: server - String - Name or IP of cups server.
735 * name - String - Name of destination.
736 * job - Long - Job ID to cancel.
738 * Returns: error number - Long?????????
743 PHP_FUNCTION(cups_cancel_job
)
745 zval
**z_server
, **z_name
, **z_job
;
747 char p_server
[256], p_name
[256];
753 * Get parameters .....
755 if ((ZEND_NUM_ARGS() != 3) ||
756 (zend_get_parameters_ex( 3, &z_server
, &z_name
, &z_job
) != SUCCESS
))
761 convert_to_string_ex( z_server
);
762 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
764 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
765 cupsSetServer(p_server
);
768 convert_to_string_ex( z_name
);
769 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
770 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
772 convert_to_string_ex( z_job
);
773 p_job
= strtoul((char *)(*z_job
)->value
.str
.val
,NULL
,10);
775 if (strlen(p_server
))
776 cupsSetServer(p_server
);
780 * Errrr .... Cancel the job ......
782 ret_val
= cupsCancelJob(p_name
,p_job
);
784 RETURN_LONG(ret_val
);
789 cups_option_t
*_phpcups_parse_options( cups_option_t
*options
,
790 int *num_options
, char *param
)
792 char name
[1024], value
[1024];
794 sscanf(param
,"%1023[^=]=%1023s", name
, value
);
796 if (strlen(name
) && strlen(value
))
800 options
= (cups_option_t
*)emalloc(sizeof(cups_option_t
));
801 options
->name
= (char *)emalloc(strlen(name
)+1);
802 options
->value
= (char *)emalloc(strlen(value
)+1);
803 strcpy( options
->name
, name
);
804 strcpy( options
->value
, value
);
809 options
= (cups_option_t
*)erealloc(options
,
810 (*num_options
+1) * sizeof(cups_option_t
));
811 options
[*num_options
].name
= (char *)emalloc(strlen(name
)+1);
812 options
[*num_options
].value
= (char *)emalloc(strlen(value
)+1);
813 strcpy( options
[*num_options
].name
, name
);
814 strcpy( options
[*num_options
].value
, value
);
823 * Function: cups_print_file
825 * Date: 8 April 2002 - TDB
827 * Parameters: printer - String - Name of destination.
828 * filename - String - Name of file to print (full path).
829 * title - String - Title of document.
831 * Returns: Job ID - Long
836 PHP_FUNCTION(cups_print_file
)
838 zval
**z_server
, **z_printer
, **z_filename
, **z_title
,
839 **z_options
, **keydata
;
845 char p_filename
[256];
849 cups_option_t
*options
= NULL
;
854 bzero( p_server
, 256 );
855 bzero( p_printer
, 256 );
856 bzero( p_filename
, 256 );
857 bzero( p_title
, 256 );
861 int zend_num_args
= ZEND_NUM_ARGS();
862 switch (zend_num_args
)
865 * Server / Destination / filename only.
868 if (zend_get_parameters_ex( 3, &z_server
, &z_printer
,
869 &z_filename
) != SUCCESS
)
873 convert_to_string_ex( z_server
);
874 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
876 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
877 cupsSetServer(p_server
);
879 convert_to_string_ex( z_printer
);
880 if ( (char *)(*z_printer
)->value
.str
.val
!= NULL
)
881 strcpy( p_printer
,(char *)(*z_printer
)->value
.str
.val
);
882 convert_to_string_ex( z_filename
);
883 if ( (char *)(*z_filename
)->value
.str
.val
!= NULL
)
884 strcpy( p_filename
,(char *)(*z_filename
)->value
.str
.val
);
885 strcpy( p_title
,"untitled");
889 * Server, destination, filename and title.
892 if (zend_get_parameters_ex( 4, &z_server
,
895 &z_title
) != SUCCESS
)
900 convert_to_string_ex( z_server
);
901 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
903 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
904 cupsSetServer(p_server
);
907 convert_to_string_ex( z_printer
);
908 if ( (char *)(*z_printer
)->value
.str
.val
!= NULL
)
909 strcpy( p_printer
,(char *)(*z_printer
)->value
.str
.val
);
911 convert_to_string_ex( z_filename
);
912 if ( (char *)(*z_filename
)->value
.str
.val
!= NULL
)
913 strcpy( p_filename
,(char *)(*z_filename
)->value
.str
.val
);
915 convert_to_string_ex( z_title
);
916 if ( (char *)(*z_title
)->value
.str
.val
!= NULL
)
917 strcpy( p_title
,(char *)(*z_title
)->value
.str
.val
);
919 _zz_internal_log( "cups_print_file", p_server
);
920 _zz_internal_log( "cups_print_file", p_printer
);
921 _zz_internal_log( "cups_print_file", p_filename
);
922 _zz_internal_log( "cups_print_file", p_title
);
926 * Server, destination, filename and title plus printer options.
929 if (zend_get_parameters_ex( 5, &z_server
,
933 &z_options
) != SUCCESS
)
937 convert_to_string_ex( z_server
);
938 if ( (char *)(*z_server
)->value
.str
.val
!= NULL
)
940 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
941 cupsSetServer(p_server
);
943 convert_to_string_ex( z_printer
);
944 if ( (char *)(*z_printer
)->value
.str
.val
!= NULL
)
945 strcpy( p_printer
,(char *)(*z_printer
)->value
.str
.val
);
946 convert_to_string_ex( z_filename
);
947 if ( (char *)(*z_filename
)->value
.str
.val
!= NULL
)
948 strcpy( p_filename
,(char *)(*z_filename
)->value
.str
.val
);
949 convert_to_string_ex( z_title
);
950 if ( (char *)(*z_title
)->value
.str
.val
!= NULL
)
951 strcpy( p_title
,(char *)(*z_title
)->value
.str
.val
);
953 _zz_internal_log( "cups_print_file(server)", p_server
);
954 _zz_internal_log( "cups_print_file(printer)", p_printer
);
955 _zz_internal_log( "cups_print_file(filename)", p_filename
);
956 _zz_internal_log( "cups_print_file(title)", p_title
);
959 * Convert options array.
961 convert_to_array_ex( z_options
);
962 param_ht
= Z_ARRVAL_PP( z_options
);
963 count
= zend_hash_num_elements( param_ht
);
964 options
= emalloc(sizeof(zval
**) * count
);
967 zend_hash_internal_pointer_reset(param_ht
);
968 for (current
=0; current
< count
; current
++)
970 zend_hash_get_current_data(param_ht
,(void **)&keydata
);
971 switch(Z_TYPE_PP(keydata
))
974 convert_to_string_ex(keydata
);
975 strcpy(temp
,(char *)(*keydata
)->value
.str
.val
);
976 _zz_internal_log( "cups_print_file(option)", temp
);
977 options
= _phpcups_parse_options( options
, ¤t
,
981 zend_hash_move_forward(param_ht
);
985 default: WRONG_PARAM_COUNT
;
992 sprintf(temp
,"(2) - P: %s F: %s T: %s C: %d", p_printer
, p_filename
,
994 _zz_internal_log( "cups_print_file", temp
);
995 ret_val
= cupsPrintFile( p_printer
,p_filename
,p_title
,current
,options
);
996 for (current
=0; current
< count
; current
++)
998 efree( options
[current
].name
);
999 efree( options
[current
].value
);
1005 _zz_internal_log( "cups_print_file", "going to print");
1006 ret_val
= cupsPrintFile( p_printer
,p_filename
,p_title
,0,NULL
);
1009 RETURN_LONG(ret_val
);
1013 int _phpcups_get_printer_status(char *server
, int port
, char *name
);
1015 typedef struct printer_attrs_type
1024 printer_attrs_t
*printer_attrs
= NULL
;
1026 void free_attrs_list(void);
1027 void _phpcups_free_attrs_list(void);
1030 * Function: cups_get_printer_attributes
1032 * Date: 8 April 2002 - TDB
1034 * Parameters: name - String - Name of destination.
1036 * Returns: state - String
1041 PHP_FUNCTION(cups_get_printer_attributes
)
1043 zval
**z_server
, **z_port
, **z_name
;
1047 char p_server
[256], p_name
[256];
1054 * Initialize return value.
1056 array_init(return_value
);
1059 * Get parameters .....
1061 switch(ZEND_NUM_ARGS())
1064 // Destination name only, assume localhost
1067 if (zend_get_parameters_ex( 1, &z_name
) != SUCCESS
)
1071 strcpy( p_server
,"localhost" );
1072 convert_to_string_ex( z_name
);
1073 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
1074 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
1078 // Server and estination name only, assume port 631
1081 if (zend_get_parameters_ex( 2, &z_server
, &z_name
) != SUCCESS
)
1085 convert_to_string_ex( z_server
);
1086 if ((char *)(*z_server
)->value
.str
.val
!= NULL
)
1087 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
1088 convert_to_string_ex( z_name
);
1089 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
1090 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
1094 // Server, destination name and port.
1097 if(zend_get_parameters_ex(3,&z_server
,&z_port
,&z_name
) != SUCCESS
)
1101 convert_to_string_ex(z_server
);
1102 if ((char *)(*z_server
)->value
.str
.val
!= NULL
)
1103 strcpy( p_server
,(char *)(*z_server
)->value
.str
.val
);
1104 convert_to_string_ex(z_name
);
1105 if ( (char *)(*z_name
)->value
.str
.val
!= NULL
)
1106 strcpy( p_name
,(char *)(*z_name
)->value
.str
.val
);
1107 convert_to_string_ex(z_port
);
1108 if ( (char *)(*z_port
)->value
.str
.val
!= NULL
)
1109 p_port
= atoi( (char *)(*z_name
)->value
.str
.val
);
1112 default: WRONG_PARAM_COUNT
;
1115 printer_attrs
= NULL
;
1116 count
= _phpcups_get_printer_status( p_server
, p_port
, p_name
);
1119 * Create an array of objects containing name / value pairs.
1121 for (i
=0; i
< count
; i
++)
1123 if ((printer_attrs
[i
].name
!= NULL
) && (printer_attrs
[i
].value
!= NULL
))
1125 MAKE_STD_ZVAL(new_object
);
1126 if (object_init(new_object
) == SUCCESS
)
1128 add_property_string(new_object
,"name",printer_attrs
[i
].name
, 1 );
1129 add_property_string(new_object
,"value",printer_attrs
[i
].value
, 1 );
1130 add_next_index_zval( return_value
, new_object
);
1134 _phpcups_free_attrs_list();
1140 void _phpcups_free_attrs_list(void)
1147 for ( i
=0; i
< num_attrs
; i
++ )
1149 if (printer_attrs
[i
].name
!= NULL
)
1150 efree(printer_attrs
[i
].name
);
1151 if (printer_attrs
[i
].value
!= NULL
)
1152 efree(printer_attrs
[i
].value
);
1154 efree(printer_attrs
);
1164 int _phpcups_update_attrs_list( char *name
, char *value
)
1168 printer_attrs
= (printer_attrs_t
*)emalloc(sizeof(printer_attrs_t
));
1169 printer_attrs
->name
= (char *)emalloc(strlen(name
)+1);
1170 printer_attrs
->value
= (char *)emalloc(strlen(value
)+1);
1171 strcpy( printer_attrs
->name
, name
);
1172 strcpy( printer_attrs
->value
, value
);
1177 printer_attrs
= (printer_attrs_t
*)erealloc(printer_attrs
,
1178 (num_attrs
+ 1) * sizeof(printer_attrs_t
));
1180 printer_attrs
[num_attrs
].name
= (char *)emalloc(strlen(name
)+1);
1181 printer_attrs
[num_attrs
].value
= (char *)emalloc(strlen(value
)+1);
1182 strcpy( printer_attrs
[num_attrs
].name
, name
);
1183 strcpy( printer_attrs
[num_attrs
].value
, value
);
1191 int _phpcups_get_printer_status( char *server
, int port
, char *name
)
1193 http_encryption_t encrypt
= HTTP_ENCRYPT_IF_REQUESTED
;
1195 ipp_t
*request
, /* IPP Request */
1196 *response
; /* IPP Response */
1197 ipp_attribute_t
*attr
; /* Current attribute */
1198 cups_lang_t
*language
; /* Default language */
1200 char printer_uri
[1024];
1202 static char *req_attrs
[] = {"printer-state", "printer-state-reason" };
1208 last_error
= IPP_INTERNAL_ERROR
;
1213 * Try to connect to the server...
1216 if ((cups_server
= httpConnectEncrypt(server
, port
, encrypt
)) == NULL
)
1218 last_error
= IPP_SERVICE_UNAVAILABLE
;
1223 * Build a CUPS_GET_PRINTERS request, which requires the following
1226 * attributes-charset
1227 * attributes-natural-language
1228 * requested-attributes
1233 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1234 request
->request
.op
.request_id
= 1;
1236 language
= cupsLangDefault();
1238 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1239 "attributes-charset", NULL
, cupsLangEncoding(language
));
1241 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1242 "attributes-natural-language", NULL
, language
->language
);
1244 sprintf(printer_uri
, "ipp://localhost/printers/%-s", name
);
1245 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1246 "printer-uri", NULL
, printer_uri
);
1249 * Do the request and get back a response...
1253 printer_attrs
= NULL
;
1255 if ((response
= cupsDoRequest(cups_server
, request
, "/")) != NULL
)
1257 last_error
= response
->request
.status
.status_code
;
1259 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1262 if (attr
->num_values
< 1)
1265 if (attr
->name
!= NULL
&&
1266 strcasecmp(attr
->name
, "printer-state") == 0 &&
1267 attr
->value_tag
== IPP_TAG_ENUM
)
1269 strcpy( temp
, "unknown" );
1270 switch( attr
->values
[0].integer
)
1272 case 3: strcpy( temp
, "idle" );
1274 case 4: strcpy( temp
, "processing" );
1276 case 5: strcpy( temp
, "stopped" );
1280 _phpcups_update_attrs_list( attr
->name
, temp
);
1282 else if (attr
->name
!= NULL
&&
1283 (attr
->value_tag
== IPP_TAG_TEXT
||
1284 attr
->value_tag
== IPP_TAG_URI
||
1285 attr
->value_tag
== IPP_TAG_KEYWORD
||
1286 attr
->value_tag
== IPP_TAG_STRING
))
1288 for (i
=0; i
< attr
->num_values
; i
++)
1289 _phpcups_update_attrs_list(attr
->name
, attr
->values
[i
].string
.text
);
1291 else if (attr
->name
!= NULL
&&
1292 (attr
->value_tag
== IPP_TAG_ENUM
||
1293 attr
->value_tag
== IPP_TAG_BOOLEAN
||
1294 attr
->value_tag
== IPP_TAG_INTEGER
))
1296 for (i
=0; i
< attr
->num_values
; i
++)
1298 sprintf(temp
,"%-d", attr
->values
[i
].integer
);
1299 _phpcups_update_attrs_list(attr
->name
, temp
);
1302 else if (attr
->name
!= NULL
&&
1303 attr
->value_tag
== IPP_TAG_RESOLUTION
)
1305 for (i
=0; i
< attr
->num_values
; i
++)
1307 sprintf(temp
,"%-dx%-dx%-d",
1308 attr
->values
[i
].resolution
.xres
,
1309 attr
->values
[i
].resolution
.yres
,
1310 attr
->values
[i
].resolution
.units
);
1311 _phpcups_update_attrs_list(attr
->name
, temp
);
1314 else if (attr
->name
!= NULL
&&
1315 attr
->value_tag
== IPP_TAG_RANGE
)
1317 for (i
=0; i
< attr
->num_values
; i
++)
1319 sprintf(temp
,"%d-%d",
1320 attr
->values
[i
].range
.lower
,
1321 attr
->values
[i
].range
.upper
);
1322 _phpcups_update_attrs_list(attr
->name
, temp
);
1326 ippDelete(response
);
1330 last_error
= IPP_BAD_REQUEST
;
1337 void _zz_internal_log( char *func
, char *line
)
1341 if ((fp
= fopen("/var/log/cups/project.log","a")) == NULL
)
1344 fprintf(fp
,"phpcups: %s - %s\n", func
, line
);
1352 * End of "$Id: phpcups.c 4530 2005-06-01 20:15:51Z mike $".